plane.h
1 /*************************************************************************/
2 /* plane.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef PLANE_H
30 #define PLANE_H
31 
32 
33 #include "vector3.h"
34 
35 class Plane {
36 public:
37 
38  Vector3 normal;
39  real_t d;
40 
41 
42  void set_normal(const Vector3& p_normal);
43  _FORCE_INLINE_ Vector3 get_normal() const { return normal; };
44 
45  void normalize();
46  Plane normalized() const;
47 
48  /* Plane-Point operations */
49 
50  _FORCE_INLINE_ Vector3 center() const { return normal*d; }
51  Vector3 get_any_point() const;
52  Vector3 get_any_perpendicular_normal() const;
53 
54  _FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const;
55  _FORCE_INLINE_ real_t distance_to(const Vector3 &p_point) const;
56  _FORCE_INLINE_ bool has_point(const Vector3 &p_point,real_t _epsilon=CMP_EPSILON) const;
57 
58  /* intersections */
59 
60  bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result=0) const;
61  bool intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3* p_intersection) const;
62  bool intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3* p_intersection) const;
63 
64  _FORCE_INLINE_ Vector3 project(const Vector3& p_point) const {
65 
66  return p_point - normal * distance_to(p_point);
67  }
68 
69  /* misc */
70 
71  Plane operator-() const { return Plane(-normal,-d); }
72  bool is_almost_like(const Plane& p_plane) const;
73 
74  _FORCE_INLINE_ bool operator==(const Plane& p_plane) const;
75  _FORCE_INLINE_ bool operator!=(const Plane& p_plane) const;
76  operator String() const;
77 
78  _FORCE_INLINE_ Plane() { d=0; }
79  _FORCE_INLINE_ Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) : normal(p_a,p_b,p_c), d(p_d) { };
80 
81  _FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d);
82  _FORCE_INLINE_ Plane(const Vector3 &p_point, const Vector3& p_normal);
83  _FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2,const Vector3 &p_point3,ClockDirection p_dir = CLOCKWISE);
84 
85 
86 };
87 
88 
89 
90 bool Plane::is_point_over(const Vector3 &p_point) const {
91 
92  return (normal.dot(p_point) > d);
93 }
94 
95 real_t Plane::distance_to(const Vector3 &p_point) const {
96 
97  return (normal.dot(p_point)-d);
98 }
99 
100 bool Plane::has_point(const Vector3 &p_point,real_t _epsilon) const {
101 
102  float dist=normal.dot(p_point) - d;
103  dist=ABS(dist);
104  return ( dist <= _epsilon);
105 
106 }
107 
108 Plane::Plane(const Vector3 &p_normal, real_t p_d) {
109 
110  normal=p_normal;
111  d=p_d;
112 }
113 
114 Plane::Plane(const Vector3 &p_point, const Vector3& p_normal) {
115 
116  normal=p_normal;
117  d=p_normal.dot(p_point);
118 }
119 
120 Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3,ClockDirection p_dir) {
121 
122  if (p_dir == CLOCKWISE)
123  normal=(p_point1-p_point3).cross(p_point1-p_point2);
124  else
125  normal=(p_point1-p_point2).cross(p_point1-p_point3);
126 
127 
128  normal.normalize();
129  d = normal.dot(p_point1);
130 
131 
132 }
133 
134 bool Plane::operator==(const Plane& p_plane) const {
135 
136  return normal==p_plane.normal && d == p_plane.d;
137 }
138 
139 bool Plane::operator!=(const Plane& p_plane) const {
140 
141  return normal!=p_plane.normal || d != p_plane.d;
142 
143 }
144 
145 #endif // PLANE_H
146 
void normalize()
Point is coplanar, CMP_EPSILON for precision.
Definition: plane.cpp:41
Definition: vector3.h:38
Definition: plane.h:35
Definition: ustring.h:64
_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const
Point is over plane.
Definition: plane.h:90