Panda3D

collisionPolygon.h

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 #include "pvector.h"
00024 
00025 class GeomNode;
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //       Class : CollisionPolygon
00029 // Description :
00030 ////////////////////////////////////////////////////////////////////
00031 class EXPCL_PANDA_COLLIDE CollisionPolygon : public CollisionPlane {
00032 PUBLISHED:
00033   INLINE CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
00034                           const LVecBase3 &c);
00035   INLINE CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
00036                           const LVecBase3 &c, const LVecBase3 &d);
00037   INLINE CollisionPolygon(const LPoint3 *begin, const LPoint3 *end);
00038 
00039 private:
00040   INLINE CollisionPolygon();
00041 
00042 public:
00043   CollisionPolygon(const CollisionPolygon &copy);
00044 
00045   virtual CollisionSolid *make_copy();
00046 
00047 PUBLISHED:
00048   virtual LPoint3 get_collision_origin() const;
00049 
00050   INLINE int get_num_points() const;
00051   INLINE LPoint3 get_point(int n) const;
00052   MAKE_SEQ(get_points, get_num_points, get_point);
00053 
00054 
00055   INLINE static bool verify_points(const LPoint3 &a, const LPoint3 &b,
00056                                    const LPoint3 &c);
00057   INLINE static bool verify_points(const LPoint3 &a, const LPoint3 &b,
00058                                    const LPoint3 &c, const LPoint3 &d);
00059   static bool verify_points(const LPoint3 *begin, const LPoint3 *end);
00060 
00061   bool is_valid() const;
00062   bool is_concave() const;
00063 
00064 public:
00065   virtual void xform(const LMatrix4 &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 LVector2 &v1, const LVector2 &v2);
00097   INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
00098                                    const LPoint2 &f, const LVector2 &v);
00099   static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
00100                                     const LPoint2 &f, const LPoint2 &t,
00101                                     const LVector2 &v);
00102   
00103 private:
00104   class PointDef {
00105   public:
00106     INLINE PointDef(const LPoint2 &p, const LVector2 &v);
00107     INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
00108     INLINE PointDef(const PointDef &copy);
00109     INLINE void operator = (const PointDef &copy);
00110 
00111     LPoint2 _p;  // the point in 2-d space
00112     LVector2 _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 LPoint2 &p, const Points &points) const;
00121   PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
00122 
00123   void setup_points(const LPoint3 *begin, const LPoint3 *end);
00124   INLINE LPoint2 to_2d(const LVecBase3 &point3d) const;
00125   INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat) const;
00126   INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat) const;
00127   INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
00128   LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
00129 
00130   bool clip_polygon(Points &new_points, const Points &source_points,
00131                     const LPlane &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   LMatrix4 _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 &params);
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 
 All Classes Functions Variables Enumerations