Panda3D
 All Classes Functions Variables Enumerations
collisionPolygon.I
00001 // Filename: collisionPolygon.I
00002 // Created by:  drose (25Apr00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: CollisionPolygon::Constructor
00018 //       Access: Published
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE CollisionPolygon::
00022 CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
00023                  const LVecBase3 &c) {
00024   LPoint3 array[3];
00025   array[0] = a;
00026   array[1] = b;
00027   array[2] = c;
00028   setup_points(array, array + 3);
00029 }
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: CollisionPolygon::Constructor
00033 //       Access: Published
00034 //  Description:
00035 ////////////////////////////////////////////////////////////////////
00036 INLINE CollisionPolygon::
00037 CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
00038                  const LVecBase3 &c, const LVecBase3 &d) {
00039   LPoint3 array[4];
00040   array[0] = a;
00041   array[1] = b;
00042   array[2] = c;
00043   array[3] = d;
00044   setup_points(array, array + 4);
00045 }
00046 
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: CollisionPolygon::Constructor
00050 //       Access: Published
00051 //  Description:
00052 ////////////////////////////////////////////////////////////////////
00053 INLINE CollisionPolygon::
00054 CollisionPolygon(const LPoint3 *begin, const LPoint3 *end) {
00055   setup_points(begin, end);
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: CollisionPolygon::Constructor
00060 //       Access: Private
00061 //  Description: Creates an invalid polygon.  Only used when reading
00062 //               from a bam file.
00063 ////////////////////////////////////////////////////////////////////
00064 INLINE CollisionPolygon::
00065 CollisionPolygon() {
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: CollisionPolygon::get_num_points
00070 //       Access: Published
00071 //  Description: Returns the number of vertices of the
00072 //               CollisionPolygon.
00073 ////////////////////////////////////////////////////////////////////
00074 INLINE int CollisionPolygon::
00075 get_num_points() const {
00076   return _points.size();
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: CollisionPolygon::get_point
00081 //       Access: Published
00082 //  Description: Returns the nth vertex of the CollisionPolygon,
00083 //               expressed in 3-D space.
00084 ////////////////////////////////////////////////////////////////////
00085 INLINE LPoint3 CollisionPolygon::
00086 get_point(int n) const {
00087   nassertr(n >= 0 && n < (int)_points.size(), LPoint3::zero());
00088   LMatrix4 to_3d_mat;
00089   rederive_to_3d_mat(to_3d_mat);
00090   return to_3d(_points[n]._p, to_3d_mat);
00091 }
00092 
00093 ////////////////////////////////////////////////////////////////////
00094 //     Function: CollisionPolygon::verify_points
00095 //       Access: Published, Static
00096 //  Description: Verifies that the indicated set of points will define
00097 //               a valid CollisionPolygon: that is, at least three
00098 //               non-collinear points, with no points repeated.
00099 ////////////////////////////////////////////////////////////////////
00100 INLINE bool CollisionPolygon::
00101 verify_points(const LPoint3 &a, const LPoint3 &b,
00102               const LPoint3 &c) {
00103   LPoint3 array[3];
00104   array[0] = a;
00105   array[1] = b;
00106   array[2] = c;
00107   return verify_points(array, array + 3);
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: CollisionPolygon::verify_points
00112 //       Access: Published, Static
00113 //  Description: Verifies that the indicated set of points will define
00114 //               a valid CollisionPolygon: that is, at least three
00115 //               non-collinear points, with no points repeated.
00116 ////////////////////////////////////////////////////////////////////
00117 INLINE bool CollisionPolygon::
00118 verify_points(const LPoint3 &a, const LPoint3 &b,
00119               const LPoint3 &c, const LPoint3 &d) {
00120   LPoint3 array[4];
00121   array[0] = a;
00122   array[1] = b;
00123   array[2] = c;
00124   array[3] = d;
00125   return verify_points(array, array + 4);
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: CollisionPolygon::flush_level
00130 //       Access: Public, Static
00131 //  Description: Flushes the PStatCollectors used during traversal.
00132 ////////////////////////////////////////////////////////////////////
00133 INLINE void CollisionPolygon::
00134 flush_level() {
00135   _volume_pcollector.flush_level();
00136   _test_pcollector.flush_level();
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: CollisionPolygon::is_right
00141 //       Access: Private, Static
00142 //  Description: Returns true if the 2-d v1 is to the right of v2.
00143 ////////////////////////////////////////////////////////////////////
00144 INLINE bool CollisionPolygon::
00145 is_right(const LVector2 &v1, const LVector2 &v2) {
00146   return (v1[0] * v2[1] - v1[1] * v2[0]) > 1.0e-6f;
00147 }
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: CollisionPolygon::dist_to_line
00151 //       Access: Private, Static
00152 //  Description: Returns the linear distance of p to the line defined
00153 //               by f and f+v, where v is a normalized vector.  The
00154 //               result is negative if p is left of the line, positive
00155 //               if it is right of the line.
00156 ////////////////////////////////////////////////////////////////////
00157 INLINE PN_stdfloat CollisionPolygon::
00158 dist_to_line(const LPoint2 &p,
00159              const LPoint2 &f, const LVector2 &v) {
00160   LVector2 v1 = (p - f);
00161   return (v1[0] * v[1] - v1[1] * v[0]);
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: CollisionPolygon::to_2d
00166 //       Access: Private
00167 //  Description: Assuming the indicated point in 3-d space lies within
00168 //               the polygon's plane, returns the corresponding point
00169 //               in the polygon's 2-d definition space.
00170 ////////////////////////////////////////////////////////////////////
00171 INLINE LPoint2 CollisionPolygon::
00172 to_2d(const LVecBase3 &point3d) const {
00173   LPoint3 point = LPoint3(point3d) * _to_2d_mat;
00174   return LPoint2(point[0], point[2]);
00175 }
00176 
00177 ////////////////////////////////////////////////////////////////////
00178 //     Function: CollisionPolygon::calc_to_3d_mat
00179 //       Access: Private
00180 //  Description: Fills the indicated matrix with the appropriate
00181 //               rotation transform to move points from the 2-d plane
00182 //               into the 3-d (X, 0, Z) plane.
00183 ////////////////////////////////////////////////////////////////////
00184 INLINE void CollisionPolygon::
00185 calc_to_3d_mat(LMatrix4 &to_3d_mat) const {
00186   // We have to be explicit about the coordinate system--we
00187   // specifically mean CS_zup_right, because that points the forward
00188   // vector down the Y axis and moves the coords in (X, 0, Z).  We
00189   // want this effect regardless of the user's coordinate system of
00190   // choice.
00191 
00192   // The up vector, on the other hand, is completely arbitrary.
00193 
00194   look_at(to_3d_mat, -get_plane().get_normal(), 
00195           LVector3(0.0f, 0.0f, 1.0f), CS_zup_right);
00196   to_3d_mat.set_row(3, get_plane().get_point());
00197 }
00198 
00199 ////////////////////////////////////////////////////////////////////
00200 //     Function: CollisionPolygon::rederive_to_3d_mat
00201 //       Access: Private
00202 //  Description: Fills the indicated matrix with the appropriate
00203 //               rotation transform to move points from the 2-d plane
00204 //               into the 3-d (X, 0, Z) plane.
00205 //
00206 //               This is essentially similar to calc_to_3d_mat, except
00207 //               that the matrix is rederived from whatever is stored
00208 //               in _to_2d_mat, guaranteeing that it will match
00209 //               whatever algorithm produced that one, even if it was
00210 //               produced on a different machine with different
00211 //               numerical precision.
00212 ////////////////////////////////////////////////////////////////////
00213 INLINE void CollisionPolygon::
00214 rederive_to_3d_mat(LMatrix4 &to_3d_mat) const {
00215   to_3d_mat.invert_from(_to_2d_mat);
00216 }
00217 
00218 ////////////////////////////////////////////////////////////////////
00219 //     Function: CollisionPolygon::to_3d
00220 //       Access: Private, Static
00221 //  Description: Extrude the indicated point in the polygon's 2-d
00222 //               definition space back into 3-d coordinates.
00223 ////////////////////////////////////////////////////////////////////
00224 INLINE LPoint3 CollisionPolygon::
00225 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat) {
00226   return LPoint3(point2d[0], 0.0f, point2d[1]) * to_3d_mat;
00227 }
00228 
00229 ////////////////////////////////////////////////////////////////////
00230 //     Function: CollisionPolygon::PointDef::Constructor
00231 //       Access: Public
00232 //  Description: 
00233 ////////////////////////////////////////////////////////////////////
00234 INLINE CollisionPolygon::PointDef::
00235 PointDef(const LPoint2 &p, const LVector2 &v) : _p(p), _v(v) {
00236 }
00237 
00238 ////////////////////////////////////////////////////////////////////
00239 //     Function: CollisionPolygon::PointDef::Constructor
00240 //       Access: Public
00241 //  Description: 
00242 ////////////////////////////////////////////////////////////////////
00243 INLINE CollisionPolygon::PointDef::
00244 PointDef(PN_stdfloat x, PN_stdfloat y) : _p(x, y), _v(0.0f, 0.0f) {
00245 }
00246 
00247 ////////////////////////////////////////////////////////////////////
00248 //     Function: CollisionPolygon::PointDef::Copy Constructor
00249 //       Access: Public
00250 //  Description: 
00251 ////////////////////////////////////////////////////////////////////
00252 INLINE CollisionPolygon::PointDef::
00253 PointDef(const CollisionPolygon::PointDef &copy) : _p(copy._p), _v(copy._v) {
00254 }
00255 
00256 ////////////////////////////////////////////////////////////////////
00257 //     Function: CollisionPolygon::PointDef::Copy Assignment Operator
00258 //       Access: Public
00259 //  Description: 
00260 ////////////////////////////////////////////////////////////////////
00261 INLINE void CollisionPolygon::PointDef::
00262 operator = (const CollisionPolygon::PointDef &copy) {
00263   _p = copy._p;
00264   _v = copy._v;
00265 }
 All Classes Functions Variables Enumerations