JNR
laPlane3.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef M_PLANE3
30 #define M_PLANE3
31 
33 
34 
36 //
38 
39 class laPlane3
40 {
41 public:
42  double d;
43  laPoint3 normal;
44 
46 
47  laPlane3(void) { /*(don't nulify to avoid slowdown)*/ };
49 
51  laPlane3(laPoint3 &a, laPoint3 &b, laPoint3 &c) { build(a,b,c); }
52 
54  //
55  inline void build(laPoint3 &a, laPoint3 &b, laPoint3 &c) {
56  laPoint3 v1( b-a );
57  laPoint3 v2( c-a );
58  normal = v1.cross( v2 );
59 
60  /*normal[0] = (b.y() - a.y())*(c.z() - a.z()) - (b.z() - a.z())*(c.y() - a.y());
61  normal[1] = (b.z() - a.z())*(c.x() - a.x()) - (b.x() - a.x())*(c.z() - a.z());
62  normal[2] = (b.x() - a.x())*(c.y() - a.y()) - (b.y() - a.y())*(c.x() - a.x());
63  if(fabs(a)<1)a=1;
64  if(fabs(b)<1)b=1;
65  if(fabs(c)<1)c=1;*/
66 
67  double len = normal.lenght();
68  normal.normalize();
69 
70  /*len = sqrt(normal.x()*normal.x() + b*b+c*c);
71  if(fabs(len)<1)len=1;
72  normal[0] = normal.x()/len;
73  normal[1] = normal.y()/len;
74  normal[2] = normal.z()/len;*/
75 
76  d = -1 * normal.dot(a); //( normal.x()*a.x() + normal.y()*a.y() + normal.z()*a.z() );
77  }
79 
81  inline double distance(laPoint3 &p){
82  return normal.dot(p) + d;
83  }
84 
86  inline double side(laPoint3 &p){
87  //double val = a*p.x() + b*p.y() + c*p.z() + d;
88  return M_SIGN( distance(p) );
89  }
90 };
92 #endif //#ifndef M_PLANE3
laPlane3(void)
Default constructor.
Definition: laPlane3.h:48
void build(laPoint3 &a, laPoint3 &b, laPoint3 &c)
Build laPlane3 from three points ( laPoint3 objects )
Definition: laPlane3.h:55
double d
Distance from the origin of the coordinate system.
Definition: laPlane3.h:42
laPoint3 normal
Normal vector.
Definition: laPlane3.h:43
laPlane3(laPoint3 &a, laPoint3 &b, laPoint3 &c)
Build laPlane3 from three points ( laPoint3 objects )
Definition: laPlane3.h:51
3D plane
Definition: laPlane3.h:39
double side(laPoint3 &p)
Return {+1; -1}, depending on the side of the plane where the specified point lies.
Definition: laPlane3.h:86
#define M_SIGN(a)
Return sign (+1 or -1)
double distance(laPoint3 &p)
Find the shortest distance between a point and the plane.
Definition: laPlane3.h:81