BALL 1.5.0
Loading...
Searching...
No Matches
plane3.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_MATHS_PLANE3_H
6#define BALL_MATHS_PLANE3_H
7
8#ifdef BALL_HAS_IEEEFP_H
9# include <ieeefp.h>
10#endif
11
12#include <cmath>
13#include <iostream>
14
15#ifndef BALL_MATHS_LINE3_H
16# include <BALL/MATHS/line3.h>
17#endif
18
19#ifndef BALL_MATHS_VECTOR3_H
20# include <BALL/MATHS/vector3.h>
21#endif
22
23#ifndef BALL_MATHS_COMMON_H
24# include <BALL/MATHS/common.h>
25#endif
26
27namespace BALL
28{
33
34 template <typename T>
35 class TPlane3;
36
41 template <typename T>
42 std::istream& operator >> (std::istream& s, TPlane3<T>& plane);
43
44 template <typename T>
45 std::ostream& operator << (std::ostream& s, const TPlane3<T>& plane);
47
51 template <typename T>
52 class TPlane3
53 {
54 public:
55
57
58
61
62
67
68 : p(),
69 n()
70 {
71 }
72
77 TPlane3(const TPlane3& plane)
78
79 : p(plane.p),
80 n(plane.n)
81 {
82 }
83
89 TPlane3(const TVector3<T>& point, const TVector3<T>& normal)
90
91 : p(point),
92 n(normal)
93 {
94 }
95
101 TPlane3(const TVector3<T>& a, const TVector3<T>& b, const TVector3<T>& c)
102
103 : p(a),
104 n((a - b) % (b - c))
105 {
106 }
107
114 TPlane3(const T& a, const T& b, const T& c, const T& d)
115 {
116 n = TVector3<T>(a, b, c);
117 if (a == 0 && b == 0 && c == 0)
118 {
119 throw Exception::DivisionByZero(__FILE__, __LINE__);
120 }
121 if (!Maths::isZero(a))
122 {
123 p.set(-d / a, 0, 0);
124 }
125 else if (!Maths::isZero(b))
126 {
127 p.set(0, -d / b, 0);
128 }
129 else if (!Maths::isZero(c))
130 {
131 p.set(0, 0, -d / c);
132 }
133 }
134
139 virtual ~TPlane3()
140
141 {
142 }
143
147 virtual void clear()
148
149 {
150 n.clear();
151 p.clear();
152 }
153
155
159
161 void swap(TPlane3& plane)
162 {
163 TVector3<T> temp_point(p);
164 p = plane.p;
165 plane.p = temp_point;
166
167 temp_point = n;
168 n = plane.n;
169 plane.n = temp_point;
170 }
171
176 void set(const TPlane3& plane)
177
178 {
179 p = plane.p;
180 n = plane.n;
181 }
182
187 void set(const TVector3<T>& point, const TVector3<T>& normal)
188
189 {
190 p = point;
191 n = normal;
192 }
193
199 void set(const TVector3<T>& a, const TVector3<T>& b, const TVector3<T>& c)
200
201 {
202 p = a;
203 n = (a - b) % (b - c);
204 }
205
211
212 {
213 p = plane.p;
214 n = plane.n;
215
216 return *this;
217 }
218
223 void get(TPlane3& plane) const
224
225 {
226 plane.p = p;
227 plane.n = n;
228 }
229
234 void get(TVector3<T>& point, TVector3<T>& normal) const
235
236 {
237 point = p;
238 normal = n;
239 }
241
245
252 {
253 T length = n.getLength();
254 // throw an exception on zero length normal
255 if (length == 0.0)
256 {
257 throw Exception::DivisionByZero(__FILE__, __LINE__);
258 }
259
260 n /= length;
261 }
262
269 void hessify()
270
271 {
272 normalize();
273 if (Maths::isLess(n * p, 0))
274 {
275 n.negate();
276 }
277 }
278
280
284
288 bool operator == (const TPlane3& plane) const
289
290 {
291 return (p == plane.p && n == plane.n);
292 }
293
297 bool operator != (const TPlane3& plane) const
298
299 {
300 return (p != plane.p || n != plane.n);
301 }
302
307 bool has(const TVector3<T>& point) const
308
309 {
310 return Maths::isZero(n * (point - p));
311 }
312
317 bool has(const TLine3<T>& line) const
318
319 {
320 return (Maths::isZero(n * line.d) && has(line.p));
321 }
323
327
332 bool isValid() const
333
334 {
335 return true;
336 }
337
344 void dump(std::ostream& s = std::cout, Size depth = 0) const
345
346 {
348
349 BALL_DUMP_HEADER(s, this, this);
350
351 BALL_DUMP_DEPTH(s, depth);
352 s << " position: " << p << std::endl;
353
354 BALL_DUMP_DEPTH(s, depth);
355 s << " normal: " << n << std::endl;
356
358 }
360
367
371
373 };
375
379 template <typename T>
380 std::istream& operator >> (std::istream& s, TPlane3<T>& plane)
381
382 {
383 char c;
384 s >> c >> plane.p >> plane.n >> c;
385 return s;
386 }
387
391 template <typename T>
392 std::ostream& operator << (std::ostream& s, const TPlane3<T>& plane)
393
394 {
395 return (s << '(' << plane.p << ' ' << plane.n << ')');
396 }
397
402
403} // namespace BALL
404
405#endif // BALL_MATHS_PLANE3_H
#define BALL_DUMP_STREAM_PREFIX(os)
Definition macros.h:391
#define BALL_DUMP_STREAM_SUFFIX(os)
Definition macros.h:395
#define BALL_DUMP_DEPTH(os, depth)
Definition macros.h:390
#define BALL_DUMP_HEADER(os, cl, ob)
Definition macros.h:393
#define BALL_CREATE(name)
Definition create.h:62
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
TPlane3< float > Plane3
Definition plane3.h:401
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
bool isLess(const T1 &a, const T2 &b)
bool isZero(const T &t)
TVector3< T > p
Definition line3.h:347
TVector3< T > d
Definition line3.h:351
TPlane3(const TVector3< T > &point, const TVector3< T > &normal)
Definition plane3.h:89
bool isValid() const
Definition plane3.h:332
TPlane3(const TVector3< T > &a, const TVector3< T > &b, const TVector3< T > &c)
Definition plane3.h:101
TPlane3(const TPlane3 &plane)
Definition plane3.h:77
bool has(const TVector3< T > &point) const
Definition plane3.h:307
void normalize()
Definition plane3.h:251
void get(TVector3< T > &point, TVector3< T > &normal) const
Definition plane3.h:234
TVector3< T > p
Definition plane3.h:366
bool operator==(const TPlane3 &plane) const
Definition plane3.h:288
void set(const TVector3< T > &a, const TVector3< T > &b, const TVector3< T > &c)
Definition plane3.h:199
bool has(const TLine3< T > &line) const
Definition plane3.h:317
virtual void clear()
Definition plane3.h:147
void swap(TPlane3 &plane)
Definition plane3.h:161
TPlane3(const T &a, const T &b, const T &c, const T &d)
Definition plane3.h:114
TPlane3 & operator=(const TPlane3 &plane)
Definition plane3.h:210
TVector3< T > n
Definition plane3.h:370
virtual ~TPlane3()
Definition plane3.h:139
void dump(std::ostream &s=std::cout, Size depth=0) const
Definition plane3.h:344
void set(const TVector3< T > &point, const TVector3< T > &normal)
Definition plane3.h:187
void get(TPlane3 &plane) const
Definition plane3.h:223
void set(const TPlane3 &plane)
Definition plane3.h:176
void hessify()
Definition plane3.h:269
bool operator!=(const TPlane3 &plane) const
Definition plane3.h:297