Flow123d  release_2.2.0-48-gb04af7f
triangle.cpp
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file triangle.cpp
15  * @brief
16  */
17 
18 #include "system/exc_common.hh"
20 #include "mesh/element_impls.hh"
21 
22 
23 
25 
28 }
29 
31  id = generateId();
32 
33  A1 = new TAbscissa();
34  A2 = new TAbscissa();
35  A3 = new TAbscissa();
36 
37  pl = new TPlain();
38 
39  area = 0;
40 }
41 
43  id = generateId();
44 
45  X1 = T.X1;
46  X2 = T.X2;
47  X3 = T.X3;
48 
49  update();
50 }
51 
52 TTriangle::TTriangle(const TPoint& P1, const TPoint& P2, const TPoint& P3) {
53  id = generateId();
54 
55  X1 = P1;
56  X2 = P2;
57  X3 = P3;
58 
59  update();
60 }
61 
62 
64  id = generateId();
65 
66  X1=TPoint(ele.node[0]->point()(0), ele.node[0]->point()(1), ele.node[0]->point()(2));
67  X2=TPoint(ele.node[1]->point()(0), ele.node[1]->point()(1), ele.node[1]->point()(2));
68  X3=TPoint(ele.node[2]->point()(0), ele.node[2]->point()(1), ele.node[2]->point()(2));
69 
70  update();
71 }
72 
74  A1 = new TAbscissa(X1, X2);
75  A2 = new TAbscissa(X2, X3);
76  A3 = new TAbscissa(X3, X1);
77 
78  pl = new TPlain(X1, X2, X3);
79 
80  ComputeArea();
81 }
82 
84  if (A1 != NULL) {
85  delete A1;
86  }
87  if (A2 != NULL) {
88  delete A2;
89  }
90  if (A3 != NULL) {
91  delete A3;
92  }
93 
94  if (pl != NULL) {
95  delete pl;
96  }
97 }
98 
99 const TPlain &TTriangle::GetPlain() const {
100  return *pl;
101 }
102 
103 const TAbscissa &TTriangle::GetAbscissa(const int i) const {
104  switch (i) {
105  case 1: return *A1;
106  break;
107  case 2: return *A2;
108  break;
109  case 3: return *A3;
110  break;
111  default:
112  THROW( ExcAssertMsg() << EI_Message( "Unknown number of the abscissa of the triangle.") );
113 
114  }
115 }
116 
117 const TPoint &TTriangle::GetPoint(int i) const {
118  switch (i) {
119  case 1: return X1;
120  break;
121  case 2: return X2;
122  break;
123  case 3: return X3;
124  break;
125  default: THROW( ExcAssertMsg() << EI_Message( "Unknown number of the point of the triangle.") );
126  }
127 }
128 
129 void TTriangle::SetPoints(const TPoint& P1, const TPoint& P2, const TPoint& P3) {
130  X1 = P1;
131  X2 = P2;
132  X3 = P3;
133 
134  A1->SetPoints(P1, P2);
135  A2->SetPoints(P2, P3);
136  A3->SetPoints(P3, P1);
137 
138  pl->SetPoints(P1, P2, P3);
139 
140  ComputeArea();
141 }
142 
144  TVector N = Cross(A1->GetVector(), A2->GetVector());
145  area = 0.5 * N.Length();
146 }
147 
149  return area;
150 }
151 
153  arma::vec3 minCoor;
154  arma::vec3 maxCoor;
155 
156  for (int i=0; i<3; ++i) {
157  minCoor(i) = GetMin(i+1);
158  maxCoor(i) = GetMax(i+1);
159  }
160 
161  boundingBox=BoundingBox(minCoor, maxCoor);
162  return boundingBox;
163 }
164 
165 double TTriangle::GetMin(int i) const {
166  double min = X1.Get(i);
167 
168  if (X2.Get(i) < min) {
169  min = X2.Get(i);
170  }
171  if (X3.Get(i) < min) {
172  min = X3.Get(i);
173  }
174 
175  return min;
176 }
177 
178 double TTriangle::GetMax(int i) const {
179  double max = X1.Get(i);
180 
181  if (X2.Get(i) > max) {
182  max = X2.Get(i);
183  }
184  if (X3.Get(i) > max) {
185  max = X3.Get(i);
186  }
187 
188  return max;
189 }
190 
192  area = t.area;
193 
194  *(*this).A1 = *t.A1;
195  *(*this).A2 = *t.A2;
196  *(*this).A3 = *t.A3;
197  *(*this).pl = *t.pl;
198  X1 = t.X1;
199  X2 = t.X2;
200  X3 = t.X3;
201 
202  return *this;
203 }
204 
205 bool TTriangle::IsInner(const TPoint& P) const {
206  TVector N1, N2, U1(X1, X2), U2(X2, X3), U3(X3, X1);
207  TVector Up1(X1, P), Up2(X2, P), Up3(X3, P);
208 
209  N1 = Cross(Up1, U1);
210  N2 = Cross(U1, U3);
211  if (Dot(N1, N2) < 0) {
212  return false;
213  }
214 
215  N1 = Cross(Up2, U2);
216  N2 = Cross(U2, U1);
217  if (Dot(N1, N2) < 0) {
218  return false;
219  }
220 
221  N1 = Cross(Up3, U3);
222  N2 = Cross(U3, U2);
223  if (Dot(N1, N2) < 0) {
224  return false;
225  }
226 
227  return true;
228 }
TAbscissa * A1
Definition: triangle.h:39
const TVector & GetVector() const
Definition: bisector.cpp:104
Bounding box in 3d ambient space.
Definition: bounding_box.hh:45
double GetMin(int) const
Definition: triangle.cpp:165
static int numberInstance
Definition: triangle.h:32
void SetPoints(const TPoint &, const TPoint &)
Definition: abscissa.cpp:65
void ComputeArea()
Definition: triangle.cpp:143
TPoint X2
Definition: triangle.h:36
void SetPoints(const TPoint &, const TPoint &, const TPoint &)
Definition: plain.cpp:154
TPoint X3
Definition: triangle.h:37
Node ** node
Definition: elements.h:79
Definition: plain.h:24
int generateId()
Definition: triangle.cpp:26
double GetArea()
Definition: triangle.cpp:148
TTriangle & operator=(const TTriangle &t)
Definition: triangle.cpp:191
TPoint X1
Definition: triangle.h:35
bool IsInner(const TPoint &) const
Definition: triangle.cpp:205
const TPoint & GetPoint(int) const
Definition: triangle.cpp:117
Definition: point.h:27
void SetPoints(const TPoint &, const TPoint &, const TPoint &)
Definition: triangle.cpp:129
~TTriangle()
Definition: triangle.cpp:83
TVector Cross(const TVector &, const TVector &)
Definition: vector.cpp:180
BoundingBox & get_bounding_box()
Definition: triangle.cpp:152
double Get(int) const
Definition: point.cpp:120
double GetMax(int) const
Definition: triangle.cpp:178
const TPlain & GetPlain() const
Definition: triangle.cpp:99
double Length() const
Definition: vector.cpp:157
double area
Definition: triangle.h:47
TAbscissa * A3
Definition: triangle.h:41
TAbscissa * A2
Definition: triangle.h:40
BoundingBox boundingBox
Definition: triangle.h:45
const TAbscissa & GetAbscissa(int) const
Definition: triangle.cpp:103
void update()
Definition: triangle.cpp:73
arma::vec3 & point()
Definition: nodes.hh:68
TPlain * pl
Definition: triangle.h:43
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
double Dot(const TVector &, const TVector &)
Definition: vector.cpp:197