Panda3D
|
00001 // Filename: collisionPolygon.h 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 #ifndef COLLISIONPOLYGON_H 00016 #define COLLISIONPOLYGON_H 00017 00018 #include "pandabase.h" 00019 00020 #include "collisionPlane.h" 00021 #include "clipPlaneAttrib.h" 00022 #include "look_at.h" 00023 00024 #include "vector_LPoint2f.h" 00025 00026 class GeomNode; 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Class : CollisionPolygon 00030 // Description : 00031 //////////////////////////////////////////////////////////////////// 00032 class EXPCL_PANDA_COLLIDE CollisionPolygon : public CollisionPlane { 00033 PUBLISHED: 00034 INLINE CollisionPolygon(const LVecBase3f &a, const LVecBase3f &b, 00035 const LVecBase3f &c); 00036 INLINE CollisionPolygon(const LVecBase3f &a, const LVecBase3f &b, 00037 const LVecBase3f &c, const LVecBase3f &d); 00038 INLINE CollisionPolygon(const LPoint3f *begin, const LPoint3f *end); 00039 00040 private: 00041 INLINE CollisionPolygon(); 00042 00043 public: 00044 CollisionPolygon(const CollisionPolygon ©); 00045 00046 virtual CollisionSolid *make_copy(); 00047 00048 PUBLISHED: 00049 virtual LPoint3f get_collision_origin() const; 00050 00051 INLINE int get_num_points() const; 00052 INLINE LPoint3f get_point(int n) const; 00053 MAKE_SEQ(get_points, get_num_points, get_point); 00054 00055 INLINE static bool verify_points(const LPoint3f &a, const LPoint3f &b, 00056 const LPoint3f &c); 00057 INLINE static bool verify_points(const LPoint3f &a, const LPoint3f &b, 00058 const LPoint3f &c, const LPoint3f &d); 00059 static bool verify_points(const LPoint3f *begin, const LPoint3f *end); 00060 00061 bool is_valid() const; 00062 bool is_concave() const; 00063 00064 public: 00065 virtual void xform(const LMatrix4f &mat); 00066 00067 virtual PT(PandaNode) get_viz(const CullTraverser *trav, 00068 const CullTraverserData &data, 00069 bool bounds_only) const; 00070 00071 virtual PStatCollector &get_volume_pcollector(); 00072 virtual PStatCollector &get_test_pcollector(); 00073 00074 virtual void output(ostream &out) const; 00075 virtual void write(ostream &out, int indent_level = 0) const; 00076 00077 INLINE static void flush_level(); 00078 00079 protected: 00080 virtual PT(BoundingVolume) compute_internal_bounds() const; 00081 00082 virtual PT(CollisionEntry) 00083 test_intersection_from_sphere(const CollisionEntry &entry) const; 00084 virtual PT(CollisionEntry) 00085 test_intersection_from_line(const CollisionEntry &entry) const; 00086 virtual PT(CollisionEntry) 00087 test_intersection_from_ray(const CollisionEntry &entry) const; 00088 virtual PT(CollisionEntry) 00089 test_intersection_from_segment(const CollisionEntry &entry) const; 00090 virtual PT(CollisionEntry) 00091 test_intersection_from_parabola(const CollisionEntry &entry) const; 00092 00093 virtual void fill_viz_geom(); 00094 00095 private: 00096 INLINE static bool is_right(const LVector2f &v1, const LVector2f &v2); 00097 INLINE static float dist_to_line(const LPoint2f &p, 00098 const LPoint2f &f, const LVector2f &v); 00099 static float dist_to_line_segment(const LPoint2f &p, 00100 const LPoint2f &f, const LPoint2f &t, 00101 const LVector2f &v); 00102 00103 private: 00104 class PointDef { 00105 public: 00106 INLINE PointDef(const LPoint2f &p, const LVector2f &v); 00107 INLINE PointDef(float x, float y); 00108 INLINE PointDef(const PointDef ©); 00109 INLINE void operator = (const PointDef ©); 00110 00111 LPoint2f _p; // the point in 2-d space 00112 LVector2f _v; // the normalized vector to the next point 00113 }; 00114 typedef pvector<PointDef> Points; 00115 00116 static void compute_vectors(Points &points); 00117 void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node, 00118 const Points &points) const; 00119 00120 bool point_is_inside(const LPoint2f &p, const Points &points) const; 00121 float dist_to_polygon(const LPoint2f &p, const Points &points) const; 00122 00123 void setup_points(const LPoint3f *begin, const LPoint3f *end); 00124 INLINE LPoint2f to_2d(const LVecBase3f &point3d) const; 00125 INLINE void calc_to_3d_mat(LMatrix4f &to_3d_mat) const; 00126 INLINE void rederive_to_3d_mat(LMatrix4f &to_3d_mat) const; 00127 INLINE static LPoint3f to_3d(const LVecBase2f &point2d, const LMatrix4f &to_3d_mat); 00128 LPoint3f legacy_to_3d(const LVecBase2f &point2d, int axis) const; 00129 00130 bool clip_polygon(Points &new_points, const Points &source_points, 00131 const Planef &plane) const; 00132 bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa, 00133 const TransformState *net_transform) const; 00134 00135 private: 00136 Points _points; 00137 LMatrix4f _to_2d_mat; 00138 00139 static PStatCollector _volume_pcollector; 00140 static PStatCollector _test_pcollector; 00141 00142 public: 00143 static void register_with_read_factory(); 00144 virtual void write_datagram(BamWriter* manager, Datagram &me); 00145 00146 static TypedWritable *make_CollisionPolygon(const FactoryParams ¶ms); 00147 00148 protected: 00149 void fillin(DatagramIterator& scan, BamReader* manager); 00150 00151 public: 00152 static TypeHandle get_class_type() { 00153 return _type_handle; 00154 } 00155 static void init_type() { 00156 CollisionPlane::init_type(); 00157 register_type(_type_handle, "CollisionPolygon", 00158 CollisionPlane::get_class_type()); 00159 } 00160 virtual TypeHandle get_type() const { 00161 return get_class_type(); 00162 } 00163 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00164 00165 private: 00166 static TypeHandle _type_handle; 00167 }; 00168 00169 #include "collisionPolygon.I" 00170 00171 #endif 00172 00173