Panda3D
 All Classes Functions Variables Enumerations
boundingVolume.h
1 // Filename: boundingVolume.h
2 // Created by: drose (01Oct99)
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 BOUNDINGVOLUME_H
16 #define BOUNDINGVOLUME_H
17 
18 #include "pandabase.h"
19 
20 #include "typedObject.h"
21 #include "typedReferenceCount.h"
22 #include "deletedChain.h"
23 
26 class BoundingSphere;
27 class BoundingBox;
28 class BoundingHexahedron;
29 class BoundingLine;
30 class BoundingPlane;
33 
34 
35 ////////////////////////////////////////////////////////////////////
36 // Class : BoundingVolume
37 // Description : This is an abstract class for any volume in any sense
38 // which can be said to define the locality of reference
39 // of a node in a graph, along with all of its
40 // descendants. It is not necessarily a geometric
41 // volume (although see GeometricBoundingVolume); this
42 // is simply an abstract interface for bounds of any
43 // sort.
44 ////////////////////////////////////////////////////////////////////
45 class EXPCL_PANDA_MATHUTIL BoundingVolume : public TypedReferenceCount {
46 public:
47  INLINE_MATHUTIL BoundingVolume();
48 
49 PUBLISHED:
50  virtual BoundingVolume *make_copy() const=0;
51 
52  INLINE_MATHUTIL bool is_empty() const;
53  INLINE_MATHUTIL bool is_infinite() const;
54 
55  INLINE_MATHUTIL void set_infinite();
56 
57  INLINE_MATHUTIL bool extend_by(const BoundingVolume *vol);
58 
59 public:
60  // It might be nice to make these template member functions so we
61  // could have true STL-style first/last iterators, but that's
62  // impossible for virtual functions.
63  bool around(const BoundingVolume **first,
64  const BoundingVolume **last);
65 
66 PUBLISHED:
67  // The contains() functions return the union of one or more of these
68  // bits.
69  enum IntersectionFlags {
70  // If no bits are set, it is known that there is no intersection.
71  IF_no_intersection = 0,
72 
73  // IF_possible is set if there might be an intersection.
74  IF_possible = 0x01,
75 
76  // IF_some is set if there is definitely an intersection. In this
77  // case, IF_possible will also be set.
78  IF_some = 0x02,
79 
80  // IF_all is set if the other bounding volume is known to be
81  // completely within this bounding volume: that is, there is no
82  // part of the other bounding volume that does not intersect this
83  // one. It does *not* indicate the inverse; it is possible that
84  // some part of this bounding volume does not intersect the other.
85 
86  // Also, the converse is not implied: if IF_all is not set, you
87  // simply don't know whether the other volume is completely
88  // contained within this one or not.
89 
90  // When IF_all is set, both IF_possible and IF_some will also be
91  // set.
92  IF_all = 0x04,
93 
94  // IF_dont_understand is set if the particular volume/volume
95  // intersection test has not been implemented.
96  IF_dont_understand = 0x08
97  };
98 
99  INLINE_MATHUTIL int contains(const BoundingVolume *vol) const;
100 
101  virtual void output(ostream &out) const=0;
102  virtual void write(ostream &out, int indent_level = 0) const;
103 
104  // This enum is used to control the automatic generation of bounding
105  // volumes.
106  enum BoundsType {
107  BT_default,
108  BT_best,
109  BT_sphere,
110  BT_box,
111  BT_fastest,
112  };
113 
114 public:
115  virtual const GeometricBoundingVolume *as_geometric_bounding_volume() const;
116  virtual const FiniteBoundingVolume *as_finite_bounding_volume() const;
117  virtual const BoundingSphere *as_bounding_sphere() const;
118  virtual const BoundingBox *as_bounding_box() const;
119  virtual const BoundingHexahedron *as_bounding_hexahedron() const;
120  virtual const BoundingLine *as_bounding_line() const;
121  virtual const BoundingPlane *as_bounding_plane() const;
122 
123  static BoundsType string_bounds_type(const string &str);
124 
125 protected:
126  enum Flags {
127  F_empty = 0x01,
128  F_infinite = 0x02
129  };
130  int _flags;
131 
132 protected:
133  // The following functions support double-dispatch of virtual
134  // methods, so we can easily extend_by() various types of bounding
135  // volumes.
136 
137  // These functions are the first dispatch point.
138  virtual bool extend_other(BoundingVolume *other) const=0;
139  virtual bool around_other(BoundingVolume *other,
140  const BoundingVolume **first,
141  const BoundingVolume **last) const=0;
142  virtual int contains_other(const BoundingVolume *other) const=0;
143 
144  // These functions are the second dispatch point. They actually do
145  // the work.
146  virtual bool extend_by_sphere(const BoundingSphere *sphere);
147  virtual bool extend_by_box(const BoundingBox *box);
148  virtual bool extend_by_hexahedron(const BoundingHexahedron *hexahedron);
149  virtual bool extend_by_line(const BoundingLine *line);
150  virtual bool extend_by_plane(const BoundingPlane *plane);
151  virtual bool extend_by_union(const UnionBoundingVolume *unionv);
152  virtual bool extend_by_intersection(const IntersectionBoundingVolume *intersection);
153  virtual bool extend_by_finite(const FiniteBoundingVolume *volume);
154  virtual bool extend_by_geometric(const GeometricBoundingVolume *volume);
155 
156  virtual bool around_spheres(const BoundingVolume **first,
157  const BoundingVolume **last);
158  virtual bool around_boxes(const BoundingVolume **first,
159  const BoundingVolume **last);
160  virtual bool around_hexahedrons(const BoundingVolume **first,
161  const BoundingVolume **last);
162  virtual bool around_lines(const BoundingVolume **first,
163  const BoundingVolume **last);
164  virtual bool around_planes(const BoundingVolume **first,
165  const BoundingVolume **last);
166  virtual bool around_unions(const BoundingVolume **first,
167  const BoundingVolume **last);
168  virtual bool around_intersections(const BoundingVolume **first,
169  const BoundingVolume **last);
170  virtual bool around_finite(const BoundingVolume **first,
171  const BoundingVolume **last);
172  virtual bool around_geometric(const BoundingVolume **first,
173  const BoundingVolume **last);
174 
175  virtual int contains_sphere(const BoundingSphere *sphere) const;
176  virtual int contains_box(const BoundingBox *box) const;
177  virtual int contains_hexahedron(const BoundingHexahedron *hexahedron) const;
178  virtual int contains_line(const BoundingLine *line) const;
179  virtual int contains_plane(const BoundingPlane *plane) const;
180  virtual int contains_union(const UnionBoundingVolume *unionv) const;
181  virtual int contains_intersection(const IntersectionBoundingVolume *intersection) const;
182  virtual int contains_finite(const FiniteBoundingVolume *volume) const;
183  virtual int contains_geometric(const GeometricBoundingVolume *volume) const;
184 
185 
186 public:
187  static TypeHandle get_class_type() {
188  return _type_handle;
189  }
190  static void init_type() {
191  TypedReferenceCount::init_type();
192  register_type(_type_handle, "BoundingVolume",
193  TypedReferenceCount::get_class_type());
194  }
195  virtual TypeHandle get_type() const {
196  return get_class_type();
197  }
198  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
199 
200 private:
201  static TypeHandle _type_handle;
202 
203  friend class BoundingSphere;
204  friend class BoundingBox;
205  friend class BoundingHexahedron;
206  friend class BoundingLine;
207  friend class BoundingPlane;
208  friend class UnionBoundingVolume;
209  friend class IntersectionBoundingVolume;
210 };
211 
212 INLINE_MATHUTIL ostream &operator << (ostream &out, const BoundingVolume &bound);
213 
214 #include "boundingVolume.I"
215 
216 EXPCL_PANDA_MATHUTIL ostream &operator << (ostream &out, BoundingVolume::BoundsType type);
217 EXPCL_PANDA_MATHUTIL istream &operator >> (istream &in, BoundingVolume::BoundsType &type);
218 
219 #endif
An axis-aligned bounding box; that is, a minimum and maximum coordinate triple.
Definition: boundingBox.h:31
This defines a bounding sphere, consisting of a center and a radius.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This funny bounding volume is an infinite plane that divides space into two regions: the part behind ...
Definition: boundingPlane.h:31
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
This special bounding volume is the intersection of all of its constituent bounding volumes...
This special bounding volume is the union of all of its constituent bounding volumes.
A special kind of GeometricBoundingVolume that is known to be finite.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This defines a bounding convex hexahedron.
This funny bounding volume is an infinite line with no thickness and extending to infinity in both di...
Definition: boundingLine.h:33