Panda3D
 All Classes Functions Variables Enumerations
collisionPolygon.h
1 // Filename: collisionPolygon.h
2 // Created by: drose (25Apr00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef COLLISIONPOLYGON_H
16 #define COLLISIONPOLYGON_H
17 
18 #include "pandabase.h"
19 
20 #include "collisionPlane.h"
21 #include "clipPlaneAttrib.h"
22 #include "look_at.h"
23 #include "pvector.h"
24 
25 class GeomNode;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Class : CollisionPolygon
29 // Description :
30 ////////////////////////////////////////////////////////////////////
31 class EXPCL_PANDA_COLLIDE CollisionPolygon : public CollisionPlane {
32 PUBLISHED:
33  INLINE CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
34  const LVecBase3 &c);
35  INLINE CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
36  const LVecBase3 &c, const LVecBase3 &d);
37  INLINE CollisionPolygon(const LPoint3 *begin, const LPoint3 *end);
38 
39 private:
40  INLINE CollisionPolygon();
41 
42 public:
43  CollisionPolygon(const CollisionPolygon &copy);
44 
45  virtual CollisionSolid *make_copy();
46 
47 PUBLISHED:
48  virtual LPoint3 get_collision_origin() const;
49 
50  INLINE int get_num_points() const;
51  INLINE LPoint3 get_point(int n) const;
52  MAKE_SEQ(get_points, get_num_points, get_point);
53 
54 
55  INLINE static bool verify_points(const LPoint3 &a, const LPoint3 &b,
56  const LPoint3 &c);
57  INLINE static bool verify_points(const LPoint3 &a, const LPoint3 &b,
58  const LPoint3 &c, const LPoint3 &d);
59  static bool verify_points(const LPoint3 *begin, const LPoint3 *end);
60 
61  bool is_valid() const;
62  bool is_concave() const;
63 
64 public:
65  virtual void xform(const LMatrix4 &mat);
66 
67  virtual PT(PandaNode) get_viz(const CullTraverser *trav,
68  const CullTraverserData &data,
69  bool bounds_only) const;
70 
73 
74  virtual void output(ostream &out) const;
75  virtual void write(ostream &out, int indent_level = 0) const;
76 
77  INLINE static void flush_level();
78 
79 protected:
80  virtual PT(BoundingVolume) compute_internal_bounds() const;
81 
82  virtual PT(CollisionEntry)
83  test_intersection_from_sphere(const CollisionEntry &entry) const;
84  virtual PT(CollisionEntry)
85  test_intersection_from_line(const CollisionEntry &entry) const;
86  virtual PT(CollisionEntry)
87  test_intersection_from_ray(const CollisionEntry &entry) const;
88  virtual PT(CollisionEntry)
89  test_intersection_from_segment(const CollisionEntry &entry) const;
90  virtual PT(CollisionEntry)
91  test_intersection_from_parabola(const CollisionEntry &entry) const;
92 
93  virtual void fill_viz_geom();
94 
95 private:
96  INLINE static bool is_right(const LVector2 &v1, const LVector2 &v2);
97  INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
98  const LPoint2 &f, const LVector2 &v);
99  static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
100  const LPoint2 &f, const LPoint2 &t,
101  const LVector2 &v);
102 
103 private:
104  class PointDef {
105  public:
106  INLINE PointDef(const LPoint2 &p, const LVector2 &v);
107  INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
108  INLINE PointDef(const PointDef &copy);
109  INLINE void operator = (const PointDef &copy);
110 
111  LPoint2 _p; // the point in 2-d space
112  LVector2 _v; // the normalized vector to the next point
113  };
114  typedef pvector<PointDef> Points;
115 
116  static void compute_vectors(Points &points);
117  void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
118  const Points &points) const;
119 
120  bool point_is_inside(const LPoint2 &p, const Points &points) const;
121  PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
122 
123  void setup_points(const LPoint3 *begin, const LPoint3 *end);
124  INLINE LPoint2 to_2d(const LVecBase3 &point3d) const;
125  INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat) const;
126  INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat) const;
127  INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
128  LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
129 
130  bool clip_polygon(Points &new_points, const Points &source_points,
131  const LPlane &plane) const;
132  bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
133  const TransformState *net_transform) const;
134 
135 private:
136  Points _points;
137  LMatrix4 _to_2d_mat;
138 
139  static PStatCollector _volume_pcollector;
140  static PStatCollector _test_pcollector;
141 
142 public:
143  static void register_with_read_factory();
144  virtual void write_datagram(BamWriter* manager, Datagram &me);
145 
146  static TypedWritable *make_CollisionPolygon(const FactoryParams &params);
147 
148 protected:
149  void fillin(DatagramIterator& scan, BamReader* manager);
150 
151 public:
152  static TypeHandle get_class_type() {
153  return _type_handle;
154  }
155  static void init_type() {
156  CollisionPlane::init_type();
157  register_type(_type_handle, "CollisionPolygon",
158  CollisionPlane::get_class_type());
159  }
160  virtual TypeHandle get_type() const {
161  return get_class_type();
162  }
163  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
164 
165 private:
166  static TypeHandle _type_handle;
167 };
168 
169 #include "collisionPolygon.I"
170 
171 #endif
172 
173 
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the &quot;origin&quot; of the solid for collision purposes.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
The abstract base class for all things that can collide with other things in the world, and all the things they can collide with (except geometry).
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This collects together the pieces of data that are accumulated for each node while walking the scene ...
This functions similarly to a LightAttrib.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats...
virtual void xform(const LMatrix4 &mat)
Transforms the solid by the indicated matrix.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
Defines a single collision event.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
virtual PStatCollector & get_test_pcollector()
Returns a PStatCollector that is used to count the number of intersection tests made against a solid ...
virtual PStatCollector & get_volume_pcollector()
Returns a PStatCollector that is used to count the number of bounding volume tests made against a sol...
This is a two-component vector offset.
Definition: lvector2.h:91
static void register_with_read_factory()
Factory method to generate a CollisionPlane object.
static void flush_level()
Flushes the PStatCollectors used during traversal.
A class to retrieve the individual data elements previously stored in a Datagram. ...
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37