Panda3D
collisionPolygon.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file collisionPolygon.h
10  * @author drose
11  * @date 2000-04-25
12  */
13 
14 #ifndef COLLISIONPOLYGON_H
15 #define COLLISIONPOLYGON_H
16 
17 #include "pandabase.h"
18 
19 #include "collisionPlane.h"
20 #include "clipPlaneAttrib.h"
21 #include "look_at.h"
22 #include "pvector.h"
23 
24 class GeomNode;
25 
26 /**
27  *
28  */
29 class EXPCL_PANDA_COLLIDE CollisionPolygon : public CollisionPlane {
30 PUBLISHED:
31  INLINE CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
32  const LVecBase3 &c);
33  INLINE CollisionPolygon(const LVecBase3 &a, const LVecBase3 &b,
34  const LVecBase3 &c, const LVecBase3 &d);
35  INLINE CollisionPolygon(const LPoint3 *begin, const LPoint3 *end);
36 
37 private:
38  INLINE CollisionPolygon();
39 
40 public:
42 
43  virtual CollisionSolid *make_copy();
44 
45 PUBLISHED:
46  virtual LPoint3 get_collision_origin() const;
47 
48  INLINE size_t get_num_points() const;
49  INLINE LPoint3 get_point(size_t n) const;
50  MAKE_SEQ(get_points, get_num_points, get_point);
51 
52 
53  INLINE static bool verify_points(const LPoint3 &a, const LPoint3 &b,
54  const LPoint3 &c);
55  INLINE static bool verify_points(const LPoint3 &a, const LPoint3 &b,
56  const LPoint3 &c, const LPoint3 &d);
57  static bool verify_points(const LPoint3 *begin, const LPoint3 *end);
58 
59  bool is_valid() const;
60  bool is_concave() const;
61 
62 PUBLISHED:
63  MAKE_SEQ_PROPERTY(points, get_num_points, get_point);
64  MAKE_PROPERTY(valid, is_valid);
65  MAKE_PROPERTY(concave, is_concave);
66 
67 public:
68  virtual void xform(const LMatrix4 &mat);
69 
70  virtual PT(PandaNode) get_viz(const CullTraverser *trav,
71  const CullTraverserData &data,
72  bool bounds_only) const;
73 
76 
77  virtual void output(std::ostream &out) const;
78  virtual void write(std::ostream &out, int indent_level = 0) const;
79 
80  INLINE static void flush_level();
81 
82 protected:
83  virtual PT(BoundingVolume) compute_internal_bounds() const;
84 
85  virtual PT(CollisionEntry)
86  test_intersection_from_sphere(const CollisionEntry &entry) const;
87  virtual PT(CollisionEntry)
88  test_intersection_from_line(const CollisionEntry &entry) const;
89  virtual PT(CollisionEntry)
90  test_intersection_from_ray(const CollisionEntry &entry) const;
91  virtual PT(CollisionEntry)
92  test_intersection_from_segment(const CollisionEntry &entry) const;
93  virtual PT(CollisionEntry)
94  test_intersection_from_parabola(const CollisionEntry &entry) const;
95  virtual PT(CollisionEntry)
96  test_intersection_from_box(const CollisionEntry &entry) const;
97 
98  virtual void fill_viz_geom();
99 
100 private:
101  INLINE static bool is_right(const LVector2 &v1, const LVector2 &v2);
102  INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
103  const LPoint2 &f, const LVector2 &v);
104  static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
105  const LPoint2 &f, const LPoint2 &t,
106  const LVector2 &v);
107 
108 private:
109  class PointDef {
110  public:
111  INLINE PointDef(const LPoint2 &p, const LVector2 &v);
112  INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
113  INLINE PointDef(const PointDef &copy);
114  INLINE void operator = (const PointDef &copy);
115 
116  LPoint2 _p; // the point in 2-d space
117  LVector2 _v; // the normalized vector to the next point
118  };
119  typedef pvector<PointDef> Points;
120 
121  static void compute_vectors(Points &points);
122  void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
123  const Points &points) const;
124 
125  bool point_is_inside(const LPoint2 &p, const Points &points) const;
126  PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
127  void project(const LVector3 &axis, PN_stdfloat &center, PN_stdfloat &extent) const;
128 
129  void setup_points(const LPoint3 *begin, const LPoint3 *end);
130  INLINE LPoint2 to_2d(const LVecBase3 &point3d) const;
131  INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat) const;
132  INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat) const;
133  INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
134  LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
135 
136  bool clip_polygon(Points &new_points, const Points &source_points,
137  const LPlane &plane) const;
138  bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
139  const TransformState *net_transform) const;
140 
141 private:
142  Points _points;
143  LMatrix4 _to_2d_mat;
144 
145  static PStatCollector _volume_pcollector;
146  static PStatCollector _test_pcollector;
147 
148 public:
149  static void register_with_read_factory();
150  virtual void write_datagram(BamWriter* manager, Datagram &me);
151 
152  static TypedWritable *make_CollisionPolygon(const FactoryParams &params);
153 
154 protected:
155  void fillin(DatagramIterator& scan, BamReader* manager);
156 
157 public:
158  static TypeHandle get_class_type() {
159  return _type_handle;
160  }
161  static void init_type() {
162  CollisionPlane::init_type();
163  register_type(_type_handle, "CollisionPolygon",
164  CollisionPlane::get_class_type());
165  }
166  virtual TypeHandle get_type() const {
167  return get_class_type();
168  }
169  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
170 
171 private:
172  static TypeHandle _type_handle;
173 };
174 
175 #include "collisionPolygon.I"
176 
177 #endif
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:64
Indicates a coordinate-system transform on vertices.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
The abstract base class for all things that can collide with other things in the world,...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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 the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
bool is_right(const LVector2 &v1, const LVector2 &v2)
Returns true if the 2-d v1 is to the right of v2.
Definition: pgItem.cxx:50
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.
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the "origin" of the solid for collision purposes.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:36
virtual PStatCollector & get_test_pcollector()
Returns a PStatCollector that is used to count the number of intersection tests made against a solid ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual PStatCollector & get_volume_pcollector()
Returns a PStatCollector that is used to count the number of bounding volume tests made against a sol...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.