Panda3D

collisionBox.h

00001 // Filename: collisionBox.h
00002 // Created by:  amith tudur( 31Jul09 )
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 COLLISIONBOX_H
00016 #define COLLISIONBOX_H
00017 
00018 #include "pandabase.h"
00019 #include "collisionSolid.h"
00020 #include "parabola.h"
00021 #include "plane.h"
00022 #include "look_at.h"
00023 #include "clipPlaneAttrib.h"
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //       Class : CollisionBox
00027 // Description : A cuboid collision volume or object.
00028 ////////////////////////////////////////////////////////////////////
00029 class EXPCL_PANDA_COLLIDE CollisionBox : public CollisionSolid {
00030 PUBLISHED:
00031   INLINE CollisionBox(const LPoint3f &center, 
00032              float x, float y, float z);
00033   INLINE CollisionBox(float cx, float cy, float cz,
00034         float x,  float y,  float z);
00035   INLINE CollisionBox(const LPoint3f &min, const LPoint3f &max);
00036 
00037   virtual LPoint3f get_collision_origin() const;
00038 
00039 protected:
00040   INLINE CollisionBox();
00041 
00042 public:
00043   INLINE CollisionBox(const CollisionBox &copy);
00044   virtual CollisionSolid *make_copy();
00045 
00046   virtual PT(CollisionEntry)
00047   test_intersection(const CollisionEntry &entry) const;
00048   virtual void xform(const LMatrix4f &mat);
00049 
00050   virtual PStatCollector &get_volume_pcollector();
00051   virtual PStatCollector &get_test_pcollector();
00052 
00053   virtual void output(ostream &out) const;
00054   
00055   virtual LPoint3f get_approx_center() const;
00056   virtual LPoint3f get_min() const;
00057   virtual LPoint3f get_max() const;
00058 
00059   INLINE static void flush_level();
00060   void setup_box();
00061 
00062 PUBLISHED:
00063   INLINE_MATHUTIL int get_num_points() const;
00064   INLINE_MATHUTIL LPoint3f get_point_aabb(int n) const;
00065   INLINE_MATHUTIL LPoint3f get_point(int n) const;
00066   INLINE_MATHUTIL int get_num_planes() const;
00067   INLINE_MATHUTIL Planef set_plane(int n) const;
00068   INLINE_MATHUTIL Planef get_plane(int n) const;
00069   INLINE void set_center(const LPoint3f &center);
00070   INLINE void set_center(float x, float y, float z);
00071   INLINE const LPoint3f &get_center() const;
00072   INLINE float get_radius() const;
00073 
00074 protected:
00075   virtual PT(BoundingVolume) compute_internal_bounds() const;
00076   virtual PT(CollisionEntry)
00077   test_intersection_from_sphere(const CollisionEntry &entry) const;
00078   virtual PT(CollisionEntry)
00079   test_intersection_from_ray(const CollisionEntry &entry) const;
00080   virtual PT(CollisionEntry)
00081   test_intersection_from_segment(const CollisionEntry &entry) const;
00082   virtual PT(CollisionEntry)
00083   test_intersection_from_box(const CollisionEntry &entry) const;
00084   
00085   virtual void fill_viz_geom();
00086 
00087 protected:
00088   Vertexf compute_point(float latitude, float longitude) const;
00089 
00090 private:
00091   LPoint3f _center;
00092   LPoint3f _min;
00093   LPoint3f _max;
00094   float _x, _y, _z, _radius;
00095   LPoint3f _vertex[8]; // Each of the Eight Vertices of the Box
00096   Planef _planes[6]; //Points to each of the six sides of the Box
00097   
00098   static const int plane_def[6][4];
00099   
00100   static PStatCollector _volume_pcollector;
00101   static PStatCollector _test_pcollector;
00102 
00103 private:
00104   INLINE static bool is_right(const LVector2f &v1, const LVector2f &v2);
00105   INLINE static float dist_to_line(const LPoint2f &p,
00106                                    const LPoint2f &f, const LVector2f &v);
00107   static float dist_to_line_segment(const LPoint2f &p,
00108                                     const LPoint2f &f, const LPoint2f &t,
00109                                     const LVector2f &v);
00110   
00111 public:
00112   class PointDef {
00113   public:
00114     INLINE PointDef(const LPoint2f &p, const LVector2f &v);
00115     INLINE PointDef(float x, float y);
00116     INLINE PointDef(const PointDef &copy);
00117     INLINE void operator = (const PointDef &copy);
00118 
00119     LPoint2f _p;  // the point in 2-d space
00120     LVector2f _v; // the normalized vector to the next point
00121   };
00122   typedef pvector<PointDef> Points;
00123 
00124   static void compute_vectors(Points &points);
00125   void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
00126                     const Points &points) const;
00127 
00128   bool point_is_inside(const LPoint2f &p, const Points &points) const;
00129   float dist_to_polygon(const LPoint2f &p, const Points &points) const;
00130 
00131   void setup_points(const LPoint3f *begin, const LPoint3f *end, int plane);
00132   INLINE LPoint2f to_2d(const LVecBase3f &point3d, int plane) const;
00133   INLINE void calc_to_3d_mat(LMatrix4f &to_3d_mat, int plane) const;
00134   INLINE void rederive_to_3d_mat(LMatrix4f &to_3d_mat, int plane) const;
00135   INLINE static LPoint3f to_3d(const LVecBase2f &point2d, const LMatrix4f &to_3d_mat);
00136   LPoint3f legacy_to_3d(const LVecBase2f &point2d, int axis) const;
00137   bool clip_polygon(Points &new_points, const Points &source_points,
00138                     const Planef &plane,int plane_no) const;
00139   bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
00140                         const TransformState *net_transform, int plane_no) const;
00141 
00142 private:
00143   Points _points[6]; // one set of points for each of the six planes that make up the box
00144   LMatrix4f _to_2d_mat[6]; 
00145 
00146 public:
00147   INLINE Points get_plane_points( int n );
00148 
00149 public:
00150   static void register_with_read_factory();
00151   virtual void write_datagram(BamWriter *manager, Datagram &me);
00152 
00153 protected:
00154   static TypedWritable *make_CollisionBox(const FactoryParams &params);
00155   void fillin(DatagramIterator &scan, BamReader *manager);
00156 
00157 public:
00158   static TypeHandle get_class_type() {
00159     return _type_handle;
00160   }
00161   static void init_type() {
00162     CollisionSolid::init_type();
00163     register_type(_type_handle, "CollisionBox",
00164                   CollisionSolid::get_class_type());
00165   }
00166   virtual TypeHandle get_type() const {
00167     return get_class_type();
00168   }
00169   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00170 
00171 private:
00172   static TypeHandle _type_handle;
00173 };
00174 
00175 #include "collisionBox.I"
00176 
00177 #endif /* COLLISIONBOX_H */
 All Classes Functions Variables Enumerations