Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_tetrahedron.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2016 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "a_tetrahedron.h"
17 #include "a_plane.h"
18 #include "a_segment.h"
19 
20 #include "a_debug.h"
21 #include <sstream>
22 
23 //---------------------------------------------------------------------------
24 const std::string a_tetrahedron::help()
25 {
26  std::ostringstream o;
27  o << "*************" << std::endl;
28  o << "a_tetrahedron:" << std::endl;
29  o << "*************" << std::endl;
30  o << "This is a 'tetrahedron' class" << std::endl;
31  o << std::endl;
32  o << "Create a tetrahedron:" << std::endl;
33  o << " a_tetrahedron a" << std::endl;
34  o << " a_tetrahedron a p1 p2 p3 p4" << std::endl;
35  o << std::endl;
36  o << "Commands:" << std::endl;
37  o << "p1, p2, p3, p4: get or set apices" << std::endl;
38  o << "a[_ref]: returns apex" << std::endl;
39  o << "s _ref: returns side of tetrahedron as a_segment" << std::endl;
40  o << "f _ref: returns face of tetrahedron as a_triangle" << std::endl;
41  o << "c: get centre" << std::endl;
42  o << "V: get volume" << std::endl;
43  o << "translate _x _y _z: translate." << std::endl;
44  o << "rotate _xaxis _yaxis _zaxis: rotate." << std::endl;
45  o << a_geom_base::help();
46  return o.str();
47 }
48 //---------------------------------------------------------------------------
50 {
51  bool test = true;
52  for (unsigned short i = 0; i<4; i++)
53  test = test&&(p_[i]==p[i]);
54  return test;
55 }
56 //---------------------------------------------------------------------------
58 {
59  for (unsigned short i = 0; i<4; i++)
60  p_[i] = p[i];
61  return *this;
62 }
63 //---------------------------------------------------------------------------
64 a_segment a_tetrahedron::s(const int ref) const
65 {
66  unsigned short r=ref%4;
67  unsigned short u=0;
68  unsigned short v=3;
69  if (r==3||r==4) u=1;
70  else if (r==5) u=2;
71  if (r==0) v=1;
72  else if (r==1||r==3) v=2;
73  return a_segment(p_[u],p_[v]);
74 }
75 //---------------------------------------------------------------------------
76 a_triangle a_tetrahedron::f(const int ref) const
77 {
78  unsigned short r=ref%4;
79  a_triangle t(p_[0],p_[1],p_[2]);
80  a_point c = this->c();
81  a_point n, k;
82  switch (r)
83  {
84  case 0:
85  n = t.normal();
86  k = p_[3]-c;
87  if (n*k>0.) t.invert();
88  break;
89  case 1:
90  t.set(p_[0],p_[1],p_[3]);
91  n = t.normal();
92  k = p_[2]-c;
93  if (n*k>0.) t.invert();
94  break;
95  case 2:
96  t.set(p_[0],p_[2],p_[3]);
97  n = t.normal();
98  k = p_[1]-c;
99  if (n*k>0.) t.invert();
100  break;
101  case 3:
102  t.set(p_[1],p_[2],p_[3]);
103  n = t.normal();
104  k = p_[0]-c;
105  if (n*k>0.) t.invert();
106  break;
107  }
108  return t;
109 }
110 //-rotate--------------------------------------------------------------------
111 a_tetrahedron& a_tetrahedron::translate(double x, double y, double z)
112 {
113  for (unsigned short i = 0; i<4; i++)
114  p_[i].translate(x,y,z);
115  return *this;
116 }
117 //-rotate--------------------------------------------------------------------
118 a_tetrahedron& a_tetrahedron::rotate(const a_point& x_axis, const a_point& y_axis, const a_point& z_axis)
119 {
120  for (unsigned short i = 0; i<4; i++)
121  p_[i].rotate(x_axis, y_axis, z_axis);
122  return *this;
123 }
124 //---------------------------------------------------------------------------
125 double a_tetrahedron::V() const
126 {
127  return cross(p_[1]-p_[0],p_[2]-p_[0])*(p_[3]-p_[0])/6.;
128 }
129 //---------------------------------------------------------------------------
130 double a_tetrahedron::S() const
131 {
132  double s = 0.;
133  for (unsigned short i = 0; i<4; i++)
134  s+= this->f(i).S();
135  return s;
136 }
137 //---------------------------------------------------------------------------
138 bool a_tetrahedron::contains(const a_point& p) const
139 {
140  for (unsigned short i = 0; i<4; i++)
141  {
142  a_triangle t =this->f(i);
143  a_point n = t.normal();
144  a_point c = t.c();
145  if ((p-c)*n>verysmall_)
146  return false;
147  }
148  return true;
149 }
150 //---------------------------------------------------------------------------
152 {
153  double m = 1.e30;
154  double M = -1.e30;
155  for (unsigned short i = 0; i<4; i++)
156  {
157  a_segment s = this->s(i);
158  double l = s.length();
159  if (l<m) m=l;
160  if (l>M) M=l;
161  }
162  if (M==0)
163  return 0.;
164  else
165  return m/M;
166 }
167 //***************************************************************************
168 //---------------------------------------------------------------------------
169 void a_tetrahedron::read(std::istream &in)
170 {
171  for (unsigned short i = 0; i<4; i++)
172  in >> p_[i];
173 }
174 //---------------------------------------------------------------------------
175 void a_tetrahedron::write(std::ostream &o) const
176 {
177  for (unsigned short i = 0; i<4; i++)
178  o << p_[i] << " ";
179 }
a_point cross(const a_point &a, const a_point &b)
Definition: a_point.cxx:284
double verysmall_
Definition: a_base.h:62
static const std::string help()
Definition: a_geom_base.cxx:21
a segment
Definition: a_segment.h:29
double length() const
Definition: a_segment.h:57
a tetrahedron
Definition: a_tetrahedron.h:31
static const std::string help()
a_point c() const
get centre of gravity
Definition: a_tetrahedron.h:57
a_point p_[4]
Definition: a_tetrahedron.h:76
virtual void read(std::istream &i)
bool contains(const a_point &p) const
check whether point lies inside triangle
a_point p(const int ref) const
Definition: a_tetrahedron.h:44
a_segment s(const int ref) const
get segment i (0-3) of tetrahedron
virtual void write(std::ostream &o) const
a_tetrahedron & rotate(const a_point &x_axis, const a_point &y_axis, const a_point &z_axis)
bool operator==(const a_tetrahedron &p)
a_triangle f(const int ref) const
double quality() const
quality: ratio between smaller and bigger segment
double S() const
get surface
a_tetrahedron & operator=(const a_tetrahedron &p)
double V() const
get volume
a_tetrahedron & translate(double x, double y, double z)
a triangle
Definition: a_triangle.h:31
a_triangle & invert()
Definition: a_triangle.cxx:94
double S() const
Definition: a_triangle.cxx:107
void set(const a_point &p1, const a_point &p2, const a_point &p3)
Definition: a_triangle.h:50
a_point c() const
get centre of gravity
Definition: a_triangle.h:61
a_point normal() const
Definition: a_triangle.cxx:102