Panda3D
collisionPolygon.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file collisionPolygon.I
10  * @author drose
11  * @date 2000-04-25
12  */
13 
14 /**
15  *
16  */
17 INLINE CollisionPolygon::
18 CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
19  const LVecBase3 &c) {
20  LPoint3 array[3];
21  array[0] = a;
22  array[1] = b;
23  array[2] = c;
24  setup_points(array, array + 3);
25 }
26 
27 /**
28  *
29  */
30 INLINE CollisionPolygon::
31 CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
32  const LVecBase3 &c, const LVecBase3 &d) {
33  LPoint3 array[4];
34  array[0] = a;
35  array[1] = b;
36  array[2] = c;
37  array[3] = d;
38  setup_points(array, array + 4);
39 }
40 
41 
42 /**
43  *
44  */
45 INLINE CollisionPolygon::
46 CollisionPolygon(const LPoint3 *begin, const LPoint3 *end) {
47  setup_points(begin, end);
48 }
49 
50 /**
51  * Creates an invalid polygon. Only used when reading from a bam file.
52  */
53 INLINE CollisionPolygon::
54 CollisionPolygon() {
55 }
56 
57 /**
58  * Returns the number of vertices of the CollisionPolygon.
59  */
60 INLINE size_t CollisionPolygon::
61 get_num_points() const {
62  return _points.size();
63 }
64 
65 /**
66  * Returns the nth vertex of the CollisionPolygon, expressed in 3-D space.
67  */
68 INLINE LPoint3 CollisionPolygon::
69 get_point(size_t n) const {
70  nassertr(n < _points.size(), LPoint3::zero());
71  LMatrix4 to_3d_mat;
72  rederive_to_3d_mat(to_3d_mat);
73  return to_3d(_points[n]._p, to_3d_mat);
74 }
75 
76 /**
77  * Verifies that the indicated set of points will define a valid
78  * CollisionPolygon: that is, at least three non-collinear points, with no
79  * points repeated.
80  */
81 INLINE bool CollisionPolygon::
82 verify_points(const LPoint3 &a, const LPoint3 &b,
83  const LPoint3 &c) {
84  LPoint3 array[3];
85  array[0] = a;
86  array[1] = b;
87  array[2] = c;
88  return verify_points(array, array + 3);
89 }
90 
91 /**
92  * Verifies that the indicated set of points will define a valid
93  * CollisionPolygon: that is, at least three non-collinear points, with no
94  * points repeated.
95  */
96 INLINE bool CollisionPolygon::
97 verify_points(const LPoint3 &a, const LPoint3 &b,
98  const LPoint3 &c, const LPoint3 &d) {
99  LPoint3 array[4];
100  array[0] = a;
101  array[1] = b;
102  array[2] = c;
103  array[3] = d;
104  return verify_points(array, array + 4);
105 }
106 
107 /**
108  * Flushes the PStatCollectors used during traversal.
109  */
110 INLINE void CollisionPolygon::
112  _volume_pcollector.flush_level();
113  _test_pcollector.flush_level();
114 }
115 
116 /**
117  * Returns true if the 2-d v1 is to the right of v2.
118  */
119 INLINE bool CollisionPolygon::
120 is_right(const LVector2 &v1, const LVector2 &v2) {
121  return (v1[0] * v2[1] - v1[1] * v2[0]) > 1.0e-6f;
122 }
123 
124 /**
125  * Returns the linear distance of p to the line defined by f and f+v, where v
126  * is a normalized vector. The result is negative if p is left of the line,
127  * positive if it is right of the line.
128  */
129 INLINE PN_stdfloat CollisionPolygon::
130 dist_to_line(const LPoint2 &p,
131  const LPoint2 &f, const LVector2 &v) {
132  LVector2 v1 = (p - f);
133  return (v1[0] * v[1] - v1[1] * v[0]);
134 }
135 
136 /**
137  * Assuming the indicated point in 3-d space lies within the polygon's plane,
138  * returns the corresponding point in the polygon's 2-d definition space.
139  */
140 INLINE LPoint2 CollisionPolygon::
141 to_2d(const LVecBase3 &point3d) const {
142  LPoint3 point = LPoint3(point3d) * _to_2d_mat;
143  return LPoint2(point[0], point[2]);
144 }
145 
146 /**
147  * Fills the indicated matrix with the appropriate rotation transform to move
148  * points from the 2-d plane into the 3-d (X, 0, Z) plane.
149  */
150 INLINE void CollisionPolygon::
151 calc_to_3d_mat(LMatrix4 &to_3d_mat) const {
152  // We have to be explicit about the coordinate system--we specifically mean
153  // CS_zup_right, because that points the forward vector down the Y axis and
154  // moves the coords in (X, 0, Z). We want this effect regardless of the
155  // user's coordinate system of choice.
156 
157  // The up vector, on the other hand, is completely arbitrary.
158 
159  look_at(to_3d_mat, -get_plane().get_normal(),
160  LVector3(0.0f, 0.0f, 1.0f), CS_zup_right);
161  to_3d_mat.set_row(3, get_plane().get_point());
162 }
163 
164 /**
165  * Fills the indicated matrix with the appropriate rotation transform to move
166  * points from the 2-d plane into the 3-d (X, 0, Z) plane.
167  *
168  * This is essentially similar to calc_to_3d_mat, except that the matrix is
169  * rederived from whatever is stored in _to_2d_mat, guaranteeing that it will
170  * match whatever algorithm produced that one, even if it was produced on a
171  * different machine with different numerical precision.
172  */
173 INLINE void CollisionPolygon::
174 rederive_to_3d_mat(LMatrix4 &to_3d_mat) const {
175  to_3d_mat.invert_from(_to_2d_mat);
176 }
177 
178 /**
179  * Extrude the indicated point in the polygon's 2-d definition space back into
180  * 3-d coordinates.
181  */
182 INLINE LPoint3 CollisionPolygon::
183 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat) {
184  return LPoint3(point2d[0], 0.0f, point2d[1]) * to_3d_mat;
185 }
186 
187 /**
188  *
189  */
190 INLINE CollisionPolygon::PointDef::
191 PointDef(const LPoint2 &p, const LVector2 &v) : _p(p), _v(v) {
192 }
193 
194 /**
195  *
196  */
197 INLINE CollisionPolygon::PointDef::
198 PointDef(PN_stdfloat x, PN_stdfloat y) : _p(x, y), _v(0.0f, 0.0f) {
199 }
200 
201 /**
202  *
203  */
204 INLINE CollisionPolygon::PointDef::
205 PointDef(const CollisionPolygon::PointDef &copy) : _p(copy._p), _v(copy._v) {
206 }
207 
208 /**
209  *
210  */
211 INLINE void CollisionPolygon::PointDef::
212 operator = (const CollisionPolygon::PointDef &copy) {
213  _p = copy._p;
214  _v = copy._v;
215 }
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,...
get_point
Returns the nth vertex of the CollisionPolygon, expressed in 3-D space.
static void flush_level()
Flushes the PStatCollectors used during traversal.