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