Flow123d  release_2.2.0-914-gf1a3a4f
tetrahedron.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 tetrahedron.cpp
15  * @brief
16  */
17 
18 #include <cmath>
19 #include "system/exc_common.hh"
20 
24 
25 namespace ngh {
26 
28 
29 
31  return TTetrahedron::numberInstance++;
32 }
33 
35  id = generateId();
36 
37  A1 = new TAbscissa();
38  A2 = new TAbscissa();
39  A3 = new TAbscissa();
40  A4 = new TAbscissa();
41  A5 = new TAbscissa();
42  A6 = new TAbscissa();
43 
44  volume = 0.0;
45 }
46 
48  const TPoint& X3, const TPoint& X4) {
49  id = generateId();
50 
51  this->X1 = X1;
52  this->X2 = X2;
53  this->X3 = X3;
54  this->X4 = X4;
55 
56  T1.SetPoints(X2, X3, X4);
57  T2.SetPoints(X1, X3, X4);
58  T3.SetPoints(X1, X2, X4);
59  T4.SetPoints(X1, X2, X3);
60 
61  A1 = new TAbscissa(X1, X2);
62  A2 = new TAbscissa(X2, X3);
63  A3 = new TAbscissa(X3, X1);
64  A4 = new TAbscissa(X1, X4);
65  A5 = new TAbscissa(X2, X4);
66  A6 = new TAbscissa(X3, X4);
67 
68  ComputeVolume();
69 }
70 
72  delete A1;
73  delete A2;
74  delete A3;
75  delete A4;
76  delete A5;
77  delete A6;
78 }
79 
80 const TTriangle &TTetrahedron::GetTriangle(int i) const {
81  switch (i) {
82  case 1: return T1;
83  break;
84  case 2: return T2;
85  break;
86  case 3: return T3;
87  break;
88  case 4: return T4;
89  break;
90  default: THROW( ExcAssertMsg() << EI_Message("Unknown number of the triangle of the tetrahedron.") );
91  }
92 }
93 
94 const TAbscissa &TTetrahedron::GetAbscissa(int i) const {
95  switch (i) {
96  case 1: return *A1;
97  break;
98  case 2: return *A2;
99  break;
100  case 3: return *A3;
101  break;
102  case 4: return *A4;
103  break;
104  case 5: return *A5;
105  break;
106  case 6: return *A6;
107  break;
108  default: THROW( ExcAssertMsg() << EI_Message("Unknown number of the triangle of the tetrahedron.") );
109  }
110 }
111 
112 const TPoint &TTetrahedron::GetPoint(int i) const {
113  switch (i) {
114  case 1: return X1;
115  break;
116  case 2: return X2;
117  break;
118  case 3: return X3;
119  break;
120  case 4: return X4;
121  break;
122  default: THROW( ExcAssertMsg() << EI_Message("Unknown number of the point of the tetrahedron.") );
123  }
124 }
125 
126 double TTetrahedron::GetMin(int i) const {
127  double min = X1.Get(i);
128 
129  if (X2.Get(i) < min) {
130  min = X2.Get(i);
131  }
132  if (X3.Get(i) < min) {
133  min = X3.Get(i);
134  }
135  if (X4.Get(i) < min) {
136  min = X4.Get(i);
137  }
138 
139  return min;
140 }
141 
142 double TTetrahedron::GetMax(int i) const {
143  double max = X1.Get(i);
144 
145  if (X2.Get(i) > max) {
146  max = X2.Get(i);
147  }
148  if (X3.Get(i) > max) {
149  max = X3.Get(i);
150  }
151  if (X4.Get(i) > max) {
152  max = X4.Get(i);
153  }
154 
155  return max;
156 }
157 
159  return volume;
160 }
161 
163  double a[ 3 ][ 3 ];
164 
165  a[ 0 ][ 0 ] = X2.X() - X1.X();
166  a[ 0 ][ 1 ] = X2.Y() - X1.Y();
167  a[ 0 ][ 2 ] = X2.Z() - X1.Z();
168  a[ 1 ][ 0 ] = X3.X() - X1.X();
169  a[ 1 ][ 1 ] = X3.Y() - X1.Y();
170  a[ 1 ][ 2 ] = X3.Z() - X1.Z();
171  a[ 2 ][ 0 ] = X4.X() - X1.X();
172  a[ 2 ][ 1 ] = X4.Y() - X1.Y();
173  a[ 2 ][ 2 ] = X4.Z() - X1.Z();
174 
175  volume = fabs(Determinant3(a)) / 6.0;
176 }
177 
178 void TTetrahedron::SetPoints(const TPoint& P1, const TPoint& P2, const TPoint& P3, const TPoint& P4) {
179  X1 = P1;
180  X2 = P2;
181  X3 = P3;
182  X4 = P4;
183 
184  T1.SetPoints(P2, P3, P4);
185  T2.SetPoints(P1, P3, P4);
186  T3.SetPoints(P1, P2, P4);
187  T4.SetPoints(P1, P2, P3);
188 
189  A1->SetPoints(P1, P2);
190  A2->SetPoints(P2, P3);
191  A3->SetPoints(P3, P1);
192  A4->SetPoints(P1, P4);
193  A5->SetPoints(P2, P4);
194  A6->SetPoints(P3, P4);
195 
196  ComputeVolume();
197 }
198 
199 bool TTetrahedron::IsInner(const TPoint& P) const {
200  TVector N, U1(X1, X2), U2(X1, X3), U3(X1, X4), U4(X2, X3), U5(X2, X4), U6(X2, X1);
201  TVector Up1(X1, P), Up2(X2, P);
202 
203  N = Cross(U1, U2); //X4 is on opposite side of plain X1,X2,X3 than P
204  if (Dot(N, U3) * Dot(N, Up1) < 0) {
205  return false;
206  }
207 
208  N = Cross(U1, U3); //X3 x P
209  if (Dot(N, U2) * Dot(N, Up1) < 0) {
210  return false;
211  }
212 
213  N = Cross(U2, U3); //X2 x P
214  if (Dot(N, U1) * Dot(N, Up1) < 0) {
215  return false;
216  }
217 
218  N = Cross(U4, U5); //X1 x P
219  if (Dot(N, U6) * Dot(N, Up2) < 0) {
220  return false;
221  }
222 
223  return true;
224 }
225 
226 } // namespace ngh
TAbscissa * A3
Definition: tetrahedron.h:43
void SetPoints(const TPoint &P1, const TPoint &P2, const TPoint &P3, const TPoint &P4)
const TAbscissa & GetAbscissa(int) const
Definition: tetrahedron.cpp:94
bool IsInner(const TPoint &) const
Definition: abscissa.h:25
double Get(int) const
Definition: point.cpp:123
double GetMax(int) const
double Y() const
Definition: point.cpp:115
TAbscissa * A5
Definition: tetrahedron.h:45
static int numberInstance
Definition: tetrahedron.h:28
void SetPoints(const TPoint &, const TPoint &)
Definition: abscissa.cpp:68
TAbscissa * A1
Definition: tetrahedron.h:41
double Determinant3(double[3][3])
Definition: mathfce.cpp:37
double X() const
Definition: point.cpp:111
double Z() const
Definition: point.cpp:119
double Dot(const TVector &, const TVector &)
Definition: vector.cpp:199
const TPoint & GetPoint(int) const
TAbscissa * A2
Definition: tetrahedron.h:42
const TTriangle & GetTriangle(int) const
Definition: tetrahedron.cpp:80
void SetPoints(const TPoint &, const TPoint &, const TPoint &)
Definition: triangle.cpp:129
TAbscissa * A6
Definition: tetrahedron.h:46
TVector Cross(const TVector &, const TVector &)
Definition: vector.cpp:182
double GetMin(int) const
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
TAbscissa * A4
Definition: tetrahedron.h:44