1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /** \class vrPlane vrPlane.h <vrmath/vrPlane.h> |
---|
3 | * \author Insoo Woo(iwoo@purdue.edu), Sung-Ye Kim (inside@purdue.edu) |
---|
4 | * \author PhD research assistants in PURPL at Purdue University |
---|
5 | * \version 1.0 |
---|
6 | * \date Nov. 2006-2007 |
---|
7 | */ |
---|
8 | #ifndef VRPLANE_H |
---|
9 | #define VRPLANE_H |
---|
10 | |
---|
11 | #include <vrmath/vrLinmath.h> |
---|
12 | #include <vrmath/vrMatrix4x4f.h> |
---|
13 | #include <vrmath/vrVector4f.h> |
---|
14 | #include <vrmath/vrVector3f.h> |
---|
15 | |
---|
16 | class vrPlane |
---|
17 | { |
---|
18 | public: |
---|
19 | bool intersect(const vrVector3f& p1, const vrVector3f& p2, vrVector3f& intersectPoint) const; |
---|
20 | bool intersect(const vrVector3f& p1, const vrVector3f& p2, vrVector4f& intersectPoint) const; |
---|
21 | void transform(vrMatrix4x4f& mat); |
---|
22 | |
---|
23 | /// normal vector |
---|
24 | vrVector3f normal; |
---|
25 | |
---|
26 | /// @brief the distance from the origin |
---|
27 | float distance; |
---|
28 | }; |
---|
29 | |
---|
30 | inline bool vrPlane::intersect(const vrVector3f& p1, const vrVector3f& p2, vrVector3f& intersectPoint) const |
---|
31 | { |
---|
32 | // http://astronomy.swin.edu.au/pbourke/geometry/planeline/ |
---|
33 | float numerator = normal.x * p1.x + normal.y * p1.y + normal.z * p1.z; |
---|
34 | float denominator = normal.x * (p1.x - p2.x) + normal.y * (p1.y - p2.y) + normal.z * (p1.z - p2.z); |
---|
35 | |
---|
36 | if (denominator == 0.0f) return false; |
---|
37 | |
---|
38 | float u = numerator / denominator; |
---|
39 | if ((u > 0) && (u < 1.0f)) { |
---|
40 | return true; |
---|
41 | } |
---|
42 | |
---|
43 | intersectPoint.x = p1.x + u * (p2.x - p1.x); |
---|
44 | intersectPoint.y = p1.y + u * (p2.y - p1.y); |
---|
45 | intersectPoint.z = p1.z + u * (p2.z - p1.z); |
---|
46 | |
---|
47 | return false; |
---|
48 | } |
---|
49 | |
---|
50 | inline bool vrPlane::intersect(const vrVector3f& p1, const vrVector3f& p2, vrVector4f& intersectPoint) const |
---|
51 | { |
---|
52 | // http://astronomy.swin.edu.au/pbourke/geometry/planeline/ |
---|
53 | float numerator = normal.x * p1.x + normal.y * p1.y + normal.z * p1.z; |
---|
54 | float denominator = normal.x * (p1.x - p2.x) + normal.y * (p1.y - p2.y) + normal.z * (p1.z - p2.z); |
---|
55 | if (denominator == 0.0f) return false; |
---|
56 | float u = numerator / denominator; |
---|
57 | |
---|
58 | if ((u > 0) && (u < 1.0f)) { |
---|
59 | return true; |
---|
60 | } |
---|
61 | |
---|
62 | intersectPoint.x = p1.x + u * (p2.x - p1.x); |
---|
63 | intersectPoint.y = p1.y + u * (p2.y - p1.y); |
---|
64 | intersectPoint.z = p1.z + u * (p2.z - p1.z); |
---|
65 | intersectPoint.w = 1.0f; |
---|
66 | |
---|
67 | return false; |
---|
68 | } |
---|
69 | |
---|
70 | #endif |
---|