Panda3D

boundingVolume.h

00001 // Filename: boundingVolume.h
00002 // Created by:  drose (01Oct99)
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 BOUNDINGVOLUME_H
00016 #define BOUNDINGVOLUME_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "typedObject.h"
00021 #include "typedReferenceCount.h"
00022 #include "deletedChain.h"
00023 
00024 class GeometricBoundingVolume;
00025 class FiniteBoundingVolume;
00026 class BoundingSphere;
00027 class BoundingBox;
00028 class BoundingHexahedron;
00029 class BoundingLine;
00030 class BoundingPlane;
00031 class UnionBoundingVolume;
00032 class IntersectionBoundingVolume;
00033 
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //       Class : BoundingVolume
00037 // Description : This is an abstract class for any volume in any sense
00038 //               which can be said to define the locality of reference
00039 //               of a node in a graph, along with all of its
00040 //               descendants.  It is not necessarily a geometric
00041 //               volume (although see GeometricBoundingVolume); this
00042 //               is simply an abstract interface for bounds of any
00043 //               sort.
00044 ////////////////////////////////////////////////////////////////////
00045 class EXPCL_PANDA_MATHUTIL BoundingVolume : public TypedReferenceCount {
00046 public:
00047   INLINE_MATHUTIL BoundingVolume();
00048 
00049 PUBLISHED:
00050   virtual BoundingVolume *make_copy() const=0;
00051 
00052   INLINE_MATHUTIL bool is_empty() const;
00053   INLINE_MATHUTIL bool is_infinite() const;
00054 
00055   INLINE_MATHUTIL void set_infinite();
00056 
00057   INLINE_MATHUTIL bool extend_by(const BoundingVolume *vol);
00058 
00059   // It might be nice to make these template member functions so we
00060   // could have true STL-style first/last iterators, but that's
00061   // impossible for virtual functions.
00062   bool around(const BoundingVolume **first,
00063               const BoundingVolume **last);
00064 
00065   // The contains() functions return the union of one or more of these
00066   // bits.
00067   enum IntersectionFlags {
00068     // If no bits are set, it is known that there is no intersection.
00069     IF_no_intersection = 0,
00070 
00071     // IF_possible is set if there might be an intersection.
00072     IF_possible        = 0x01,
00073 
00074     // IF_some is set if there is definitely an intersection.  In this
00075     // case, IF_possible will also be set.
00076     IF_some            = 0x02,
00077 
00078     // IF_all is set if the other bounding volume is known to be
00079     // completely within this bounding volume: that is, there is no
00080     // part of the other bounding volume that does not intersect this
00081     // one.  It does *not* indicate the inverse; it is possible that
00082     // some part of this bounding volume does not intersect the other.
00083 
00084     // Also, the converse is not implied: if IF_all is not set, you
00085     // simply don't know whether the other volume is completely
00086     // contained within this one or not.
00087 
00088     // When IF_all is set, both IF_possible and IF_some will also be
00089     // set.
00090     IF_all             = 0x04,
00091 
00092     // IF_dont_understand is set if the particular volume/volume
00093     // intersection test has not been implemented.
00094     IF_dont_understand = 0x08
00095   };
00096 
00097   INLINE_MATHUTIL int contains(const BoundingVolume *vol) const;
00098 
00099   virtual void output(ostream &out) const=0;
00100   virtual void write(ostream &out, int indent_level = 0) const;
00101 
00102   // This enum is used to control the automatic generation of bounding
00103   // volumes.
00104   enum BoundsType {
00105     BT_default,
00106     BT_best,
00107     BT_sphere,
00108     BT_box,
00109   };
00110 
00111 public:
00112   virtual const GeometricBoundingVolume *as_geometric_bounding_volume() const;
00113   virtual const FiniteBoundingVolume *as_finite_bounding_volume() const;
00114   virtual const BoundingSphere *as_bounding_sphere() const;
00115   virtual const BoundingBox *as_bounding_box() const;
00116   virtual const BoundingHexahedron *as_bounding_hexahedron() const;
00117   virtual const BoundingLine *as_bounding_line() const;
00118   virtual const BoundingPlane *as_bounding_plane() const;
00119 
00120   static BoundsType string_bounds_type(const string &str);
00121 
00122 protected:
00123   enum Flags {
00124     F_empty        = 0x01,
00125     F_infinite     = 0x02
00126   };
00127   int _flags;
00128 
00129 protected:
00130   // The following functions support double-dispatch of virtual
00131   // methods, so we can easily extend_by() various types of bounding
00132   // volumes.
00133 
00134   // These functions are the first dispatch point.
00135   virtual bool extend_other(BoundingVolume *other) const=0;
00136   virtual bool around_other(BoundingVolume *other,
00137                             const BoundingVolume **first,
00138                             const BoundingVolume **last) const=0;
00139   virtual int contains_other(const BoundingVolume *other) const=0;
00140 
00141   // These functions are the second dispatch point.  They actually do
00142   // the work.
00143   virtual bool extend_by_sphere(const BoundingSphere *sphere);
00144   virtual bool extend_by_box(const BoundingBox *box);
00145   virtual bool extend_by_hexahedron(const BoundingHexahedron *hexahedron);
00146   virtual bool extend_by_line(const BoundingLine *line);
00147   virtual bool extend_by_plane(const BoundingPlane *plane);
00148   virtual bool extend_by_union(const UnionBoundingVolume *unionv);
00149   virtual bool extend_by_intersection(const IntersectionBoundingVolume *intersection);
00150   virtual bool extend_by_finite(const FiniteBoundingVolume *volume);
00151   virtual bool extend_by_geometric(const GeometricBoundingVolume *volume);
00152 
00153   virtual bool around_spheres(const BoundingVolume **first,
00154                               const BoundingVolume **last);
00155   virtual bool around_boxes(const BoundingVolume **first,
00156                             const BoundingVolume **last);
00157   virtual bool around_hexahedrons(const BoundingVolume **first,
00158                                   const BoundingVolume **last);
00159   virtual bool around_lines(const BoundingVolume **first,
00160                             const BoundingVolume **last);
00161   virtual bool around_planes(const BoundingVolume **first,
00162                              const BoundingVolume **last);
00163   virtual bool around_unions(const BoundingVolume **first,
00164                              const BoundingVolume **last);
00165   virtual bool around_intersections(const BoundingVolume **first,
00166                                     const BoundingVolume **last);
00167   virtual bool around_finite(const BoundingVolume **first,
00168                              const BoundingVolume **last);
00169   virtual bool around_geometric(const BoundingVolume **first,
00170                                 const BoundingVolume **last);
00171 
00172   virtual int contains_sphere(const BoundingSphere *sphere) const;
00173   virtual int contains_box(const BoundingBox *box) const;
00174   virtual int contains_hexahedron(const BoundingHexahedron *hexahedron) const;
00175   virtual int contains_line(const BoundingLine *line) const;
00176   virtual int contains_plane(const BoundingPlane *plane) const;
00177   virtual int contains_union(const UnionBoundingVolume *unionv) const;
00178   virtual int contains_intersection(const IntersectionBoundingVolume *intersection) const;
00179   virtual int contains_finite(const FiniteBoundingVolume *volume) const;
00180   virtual int contains_geometric(const GeometricBoundingVolume *volume) const;
00181 
00182 
00183 public:
00184   static TypeHandle get_class_type() {
00185     return _type_handle;
00186   }
00187   static void init_type() {
00188     TypedReferenceCount::init_type();
00189     register_type(_type_handle, "BoundingVolume",
00190                   TypedReferenceCount::get_class_type());
00191   }
00192   virtual TypeHandle get_type() const {
00193     return get_class_type();
00194   }
00195   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00196 
00197 private:
00198   static TypeHandle _type_handle;
00199 
00200   friend class BoundingSphere;
00201   friend class BoundingBox;
00202   friend class BoundingHexahedron;
00203   friend class BoundingLine;
00204   friend class BoundingPlane;
00205   friend class UnionBoundingVolume;
00206   friend class IntersectionBoundingVolume;
00207 };
00208 
00209 INLINE_MATHUTIL ostream &operator << (ostream &out, const BoundingVolume &bound);
00210 
00211 #include "boundingVolume.I"
00212 
00213 EXPCL_PANDA_MATHUTIL ostream &operator << (ostream &out, BoundingVolume::BoundsType type);
00214 EXPCL_PANDA_MATHUTIL istream &operator >> (istream &in, BoundingVolume::BoundsType &type);
00215 
00216 #endif
 All Classes Functions Variables Enumerations