CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
triangle.h
Go to the documentation of this file.
1 //========================================================================
2 // This software is free: you can redistribute it and/or modify
3 // it under the terms of the GNU Lesser General Public License Version 3,
4 // as published by the Free Software Foundation.
5 //
6 // This software is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU Lesser General Public License for more details.
10 //
11 // You should have received a copy of the GNU Lesser General Public License
12 // Version 3 in the file COPYING that came with this distribution.
13 // If not, see <http://www.gnu.org/licenses/>.
14 //========================================================================
20 //========================================================================
21 
22 #include <float.h>
23 #include "geometry.h"
24 
25 #ifndef TRIANGLE_H
26 #define TRIANGLE_H
27 
28 #define TRIANGLE_TEMP template <class num> inline
29 #define TRIANGLE_FUNC Triangle3d<num>
30 
31 template <class num> class Triangle3d;
32 
33 template <class num>
34 class Triangle3d{
35 private:
36  GVector::vector3d<num> p0,p1,p2;
38  GVector::vector3d<num> centroid;
39  num offset;
40 
41 public:
42  //Constructors, Setters
43  Triangle3d();
47  void calcValues();
48 
49  //Common Math Functions related to 3D triangles
54 
59 
60  //Getters
61  inline const GVector::vector3d<num>& P0() const {return p0;}
62  inline const GVector::vector3d<num>& P1() const {return p1;}
63  inline const GVector::vector3d<num>& P2() const {return p2;}
64  inline const GVector::vector3d<num>& Normal() const{return normal;}
65  inline const GVector::vector3d<num>& Centroid() const{return centroid;}
66  inline const num& Offset() const{return offset;}
67  inline void ToString(char* str) const;
68 
69  //Operator overloads
71  Triangle3d<num> operator*(num f) const;
73  Triangle3d<num> operator/(num f) const;
80  Triangle3d<num> operator-(GVector::vector3d<num> v) const;
84 };
85 
88 
89 TRIANGLE_TEMP
90 TRIANGLE_FUNC::Triangle3d()
91 {
92  p0.zero();
93  p1.zero();
94  p2.zero();
95  calcValues();
96  //updateRequired = true;
97 }
98 
99 TRIANGLE_TEMP
100 TRIANGLE_FUNC::Triangle3d(GVector::vector3d<num> _p0, GVector::vector3d<num> _p1, GVector::vector3d<num> _p2)
101 {
102  p0 = _p0;
103  p1 = _p1;
104  p2 = _p2;
105  calcValues();
106  //updateRequired = true;
107 }
108 
109 TRIANGLE_TEMP
110 void TRIANGLE_FUNC::ToString(char* str) const
111 {
112  sprintf(str, "%.3f,%.3f,%.3f %.3f,%.3f,%.3f %.3f,%.3f,%.3f",V3COMP(p0),V3COMP(p1),V3COMP(p2));
113 }
114 
115 TRIANGLE_TEMP
116 void TRIANGLE_FUNC::set(GVector::vector3d<num> _p0, GVector::vector3d<num> _p1, GVector::vector3d<num> _p2)
117 {
118  Triangle3d(_p0, _p1, _p2);
119 }
120 
121 
122 TRIANGLE_TEMP
123 void TRIANGLE_FUNC::calcValues()
124 {
125  centroid = (p0+p1+p2)/3.0;
126  normal = (p2-p1).cross(p0-p1).norm();
127  offset = -centroid.dot(normal);
128  //updateRequired = false;
129 }
130 
131 TRIANGLE_TEMP
132 num TRIANGLE_FUNC::closestDist(GVector::vector3d< num > p)
133 {
134  return ( p.dot(normal) + offset );
135 }
136 
137 TRIANGLE_TEMP
138 bool TRIANGLE_FUNC::intersects(Triangle3d< num > t)
139 {
140  printf("Unimplemented function %s\n",__PRETTY_FUNCTION__);
141  return false;
142 }
143 
144 TRIANGLE_TEMP
145 bool TRIANGLE_FUNC::liesAlongside(GVector::vector3d< num > p)
146 {
147  printf("Unimplemented function %s\n",__PRETTY_FUNCTION__);
148  return false;
149 }
150 
151 TRIANGLE_TEMP
152 GVector::vector3d< num > TRIANGLE_FUNC::closestPoint(GVector::vector3d< num > p)
153 {
154  return ( p - normal*(normal.dot(p)+offset) );
155 }
156 
157 #define TRIANGLE_UNARY_OPERATOR_SCALAR(op) \
158  TRIANGLE_TEMP \
159  Triangle3d<num>& TRIANGLE_FUNC::operator op (num f) \
160  { \
161  p0 = p0 op f; \
162  p1 = p1 op f; \
163  p2 = p2 op f; \
164  calcValues(); \
165  return(*this); \
166  }
167 
168 TRIANGLE_UNARY_OPERATOR_SCALAR(*=)
169 TRIANGLE_UNARY_OPERATOR_SCALAR(/=)
170 
171 #define TRIANGLE_BINARY_OPERATOR_SCALAR(op) \
172  TRIANGLE_TEMP \
173  Triangle3d<num> TRIANGLE_FUNC::operator op (num f) const\
174  { \
175  GVector::vector3d<num> p0_ = p0 op f; \
176  GVector::vector3d<num> p1_ = p1 op f; \
177  GVector::vector3d<num> p2_ = p2 op f; \
178  return(Triangle3d<num>(p0_,p1_,p2_)); \
179  }
180 
181 TRIANGLE_BINARY_OPERATOR_SCALAR(*)
182 TRIANGLE_BINARY_OPERATOR_SCALAR(/)
183 
184 #define TRIANGLE_BINARY_OPERATOR_VECTOR(op) \
185  TRIANGLE_TEMP \
186  Triangle3d<num> TRIANGLE_FUNC::operator op (GVector::vector3d<num> v) const\
187  { \
188  GVector::vector3d<num> p0_ = p0 op v; \
189  GVector::vector3d<num> p1_ = p1 op v; \
190  GVector::vector3d<num> p2_ = p2 op v; \
191  return(Triangle3d<num>(p0_,p1_,p2_)); \
192  }
193 
194 TRIANGLE_BINARY_OPERATOR_VECTOR(+)
195 TRIANGLE_BINARY_OPERATOR_VECTOR(-)
196 
197 #define TRIANGLE_UNARY_OPERATOR_VECTOR(op) \
198  TRIANGLE_TEMP \
199  Triangle3d<num>& TRIANGLE_FUNC::operator op (GVector::vector3d<num> v) \
200  { \
201  p0 = p0 op v; \
202  p1 = p1 op v; \
203  p2 = p2 op v; \
204  calcValues(); \
205  return(*this); \
206  }
207 
208 TRIANGLE_UNARY_OPERATOR_VECTOR(+=)
209 TRIANGLE_UNARY_OPERATOR_VECTOR(-=)
210 
211 #endif //TRIANGLE_H
Triangle3d< num > operator/(num f) const
returns this triangle scaled by 1/f
Triangle3d< num > & operator*=(num f)
scales this triangle by f
Triangle3d< num > & operator/=(num f)
scales this triangle by 1/f
Triangle3d< num > operator+(GVector::vector3d< num > v) const
returns this triangle translated by vector v
Triangle3d< num > & operator+=(GVector::vector3d< num > v)
translates this triangle by vector v
num closestDist(GVector::vector3d< num > p)
Closest distance from this triangle plane to p.
bool intersects(Triangle3d< num > t)
Return true if the triangle t intersect this triangle.
GVector::vector3d< num > closestPoint(GVector::vector3d< num > p)
Closest point on this triangle plane to p.
bool liesAlongside(GVector::vector3d< num > p)
Returns true if p lies alongside this triangle.
Triangle3d< num > operator*(num f) const
returns this triangle scaled by f
void calcValues()
Calculate derived values (normal, centroid)