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