Panda3D
 All Classes Functions Variables Enumerations
collisionPolygon.I
1 // Filename: collisionPolygon.I
2 // Created by: drose (25Apr00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: CollisionPolygon::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE CollisionPolygon::
22 CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
23  const LVecBase3 &c) {
24  LPoint3 array[3];
25  array[0] = a;
26  array[1] = b;
27  array[2] = c;
28  setup_points(array, array + 3);
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: CollisionPolygon::Constructor
33 // Access: Published
34 // Description:
35 ////////////////////////////////////////////////////////////////////
36 INLINE CollisionPolygon::
37 CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
38  const LVecBase3 &c, const LVecBase3 &d) {
39  LPoint3 array[4];
40  array[0] = a;
41  array[1] = b;
42  array[2] = c;
43  array[3] = d;
44  setup_points(array, array + 4);
45 }
46 
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: CollisionPolygon::Constructor
50 // Access: Published
51 // Description:
52 ////////////////////////////////////////////////////////////////////
53 INLINE CollisionPolygon::
54 CollisionPolygon(const LPoint3 *begin, const LPoint3 *end) {
55  setup_points(begin, end);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: CollisionPolygon::Constructor
60 // Access: Private
61 // Description: Creates an invalid polygon. Only used when reading
62 // from a bam file.
63 ////////////////////////////////////////////////////////////////////
64 INLINE CollisionPolygon::
65 CollisionPolygon() {
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: CollisionPolygon::get_num_points
70 // Access: Published
71 // Description: Returns the number of vertices of the
72 // CollisionPolygon.
73 ////////////////////////////////////////////////////////////////////
74 INLINE int CollisionPolygon::
75 get_num_points() const {
76  return _points.size();
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: CollisionPolygon::get_point
81 // Access: Published
82 // Description: Returns the nth vertex of the CollisionPolygon,
83 // expressed in 3-D space.
84 ////////////////////////////////////////////////////////////////////
86 get_point(int n) const {
87  nassertr(n >= 0 && n < (int)_points.size(), LPoint3::zero());
88  LMatrix4 to_3d_mat;
89  rederive_to_3d_mat(to_3d_mat);
90  return to_3d(_points[n]._p, to_3d_mat);
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: CollisionPolygon::verify_points
95 // Access: Published, Static
96 // Description: Verifies that the indicated set of points will define
97 // a valid CollisionPolygon: that is, at least three
98 // non-collinear points, with no points repeated.
99 ////////////////////////////////////////////////////////////////////
100 INLINE bool CollisionPolygon::
101 verify_points(const LPoint3 &a, const LPoint3 &b,
102  const LPoint3 &c) {
103  LPoint3 array[3];
104  array[0] = a;
105  array[1] = b;
106  array[2] = c;
107  return verify_points(array, array + 3);
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: CollisionPolygon::verify_points
112 // Access: Published, Static
113 // Description: Verifies that the indicated set of points will define
114 // a valid CollisionPolygon: that is, at least three
115 // non-collinear points, with no points repeated.
116 ////////////////////////////////////////////////////////////////////
117 INLINE bool CollisionPolygon::
118 verify_points(const LPoint3 &a, const LPoint3 &b,
119  const LPoint3 &c, const LPoint3 &d) {
120  LPoint3 array[4];
121  array[0] = a;
122  array[1] = b;
123  array[2] = c;
124  array[3] = d;
125  return verify_points(array, array + 4);
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: CollisionPolygon::flush_level
130 // Access: Public, Static
131 // Description: Flushes the PStatCollectors used during traversal.
132 ////////////////////////////////////////////////////////////////////
133 INLINE void CollisionPolygon::
135  _volume_pcollector.flush_level();
136  _test_pcollector.flush_level();
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: CollisionPolygon::is_right
141 // Access: Private, Static
142 // Description: Returns true if the 2-d v1 is to the right of v2.
143 ////////////////////////////////////////////////////////////////////
144 INLINE bool CollisionPolygon::
145 is_right(const LVector2 &v1, const LVector2 &v2) {
146  return (v1[0] * v2[1] - v1[1] * v2[0]) > 1.0e-6f;
147 }
148 
149 ////////////////////////////////////////////////////////////////////
150 // Function: CollisionPolygon::dist_to_line
151 // Access: Private, Static
152 // Description: Returns the linear distance of p to the line defined
153 // by f and f+v, where v is a normalized vector. The
154 // result is negative if p is left of the line, positive
155 // if it is right of the line.
156 ////////////////////////////////////////////////////////////////////
157 INLINE PN_stdfloat CollisionPolygon::
158 dist_to_line(const LPoint2 &p,
159  const LPoint2 &f, const LVector2 &v) {
160  LVector2 v1 = (p - f);
161  return (v1[0] * v[1] - v1[1] * v[0]);
162 }
163 
164 ////////////////////////////////////////////////////////////////////
165 // Function: CollisionPolygon::to_2d
166 // Access: Private
167 // Description: Assuming the indicated point in 3-d space lies within
168 // the polygon's plane, returns the corresponding point
169 // in the polygon's 2-d definition space.
170 ////////////////////////////////////////////////////////////////////
171 INLINE LPoint2 CollisionPolygon::
172 to_2d(const LVecBase3 &point3d) const {
173  LPoint3 point = LPoint3(point3d) * _to_2d_mat;
174  return LPoint2(point[0], point[2]);
175 }
176 
177 ////////////////////////////////////////////////////////////////////
178 // Function: CollisionPolygon::calc_to_3d_mat
179 // Access: Private
180 // Description: Fills the indicated matrix with the appropriate
181 // rotation transform to move points from the 2-d plane
182 // into the 3-d (X, 0, Z) plane.
183 ////////////////////////////////////////////////////////////////////
184 INLINE void CollisionPolygon::
185 calc_to_3d_mat(LMatrix4 &to_3d_mat) const {
186  // We have to be explicit about the coordinate system--we
187  // specifically mean CS_zup_right, because that points the forward
188  // vector down the Y axis and moves the coords in (X, 0, Z). We
189  // want this effect regardless of the user's coordinate system of
190  // choice.
191 
192  // The up vector, on the other hand, is completely arbitrary.
193 
194  look_at(to_3d_mat, -get_plane().get_normal(),
195  LVector3(0.0f, 0.0f, 1.0f), CS_zup_right);
196  to_3d_mat.set_row(3, get_plane().get_point());
197 }
198 
199 ////////////////////////////////////////////////////////////////////
200 // Function: CollisionPolygon::rederive_to_3d_mat
201 // Access: Private
202 // Description: Fills the indicated matrix with the appropriate
203 // rotation transform to move points from the 2-d plane
204 // into the 3-d (X, 0, Z) plane.
205 //
206 // This is essentially similar to calc_to_3d_mat, except
207 // that the matrix is rederived from whatever is stored
208 // in _to_2d_mat, guaranteeing that it will match
209 // whatever algorithm produced that one, even if it was
210 // produced on a different machine with different
211 // numerical precision.
212 ////////////////////////////////////////////////////////////////////
213 INLINE void CollisionPolygon::
214 rederive_to_3d_mat(LMatrix4 &to_3d_mat) const {
215  to_3d_mat.invert_from(_to_2d_mat);
216 }
217 
218 ////////////////////////////////////////////////////////////////////
219 // Function: CollisionPolygon::to_3d
220 // Access: Private, Static
221 // Description: Extrude the indicated point in the polygon's 2-d
222 // definition space back into 3-d coordinates.
223 ////////////////////////////////////////////////////////////////////
224 INLINE LPoint3 CollisionPolygon::
225 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat) {
226  return LPoint3(point2d[0], 0.0f, point2d[1]) * to_3d_mat;
227 }
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: CollisionPolygon::PointDef::Constructor
231 // Access: Public
232 // Description:
233 ////////////////////////////////////////////////////////////////////
234 INLINE CollisionPolygon::PointDef::
235 PointDef(const LPoint2 &p, const LVector2 &v) : _p(p), _v(v) {
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: CollisionPolygon::PointDef::Constructor
240 // Access: Public
241 // Description:
242 ////////////////////////////////////////////////////////////////////
243 INLINE CollisionPolygon::PointDef::
244 PointDef(PN_stdfloat x, PN_stdfloat y) : _p(x, y), _v(0.0f, 0.0f) {
245 }
246 
247 ////////////////////////////////////////////////////////////////////
248 // Function: CollisionPolygon::PointDef::Copy Constructor
249 // Access: Public
250 // Description:
251 ////////////////////////////////////////////////////////////////////
252 INLINE CollisionPolygon::PointDef::
253 PointDef(const CollisionPolygon::PointDef &copy) : _p(copy._p), _v(copy._v) {
254 }
255 
256 ////////////////////////////////////////////////////////////////////
257 // Function: CollisionPolygon::PointDef::Copy Assignment Operator
258 // Access: Public
259 // Description:
260 ////////////////////////////////////////////////////////////////////
261 INLINE void CollisionPolygon::PointDef::
262 operator = (const CollisionPolygon::PointDef &copy) {
263  _p = copy._p;
264  _v = copy._v;
265 }
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
static const LPoint3f & zero()
Returns a zero-length point.
Definition: lpoint3.h:258
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
static bool verify_points(const LPoint3 &a, const LPoint3 &b, const LPoint3 &c)
Verifies that the indicated set of points will define a valid CollisionPolygon: that is...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
int get_num_points() const
Returns the number of vertices of the CollisionPolygon.
bool invert_from(const LMatrix4f &other)
Computes the inverse of the other matrix, and stores the result in this matrix.
Definition: lmatrix.h:2173
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
LPoint3 get_point(int n) const
Returns the nth vertex of the CollisionPolygon, expressed in 3-D space.
This is a two-component vector offset.
Definition: lvector2.h:91
void set_row(int row, const LVecBase4f &v)
Replaces the indicated row of the matrix.
Definition: lmatrix.h:1187
This is a two-component point in space.
Definition: lpoint2.h:92
static void flush_level()
Flushes the PStatCollectors used during traversal.