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