Logo ROOT   6.08/07
Reference Guide
TGLBoundingBox.h
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Richard Maunder 25/05/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TGLBoundingBox
13 #define ROOT_TGLBoundingBox
14 
15 #ifndef ROOT_TGLUtil
16 #include "TGLUtil.h"
17 #endif
18 
19 //////////////////////////////////////////////////////////////////////////
20 // //
21 // TGLBoundingBox //
22 // //
23 // Concrete class describing an orientated (free) or axis aligned box //
24 // of 8 verticies. Supports methods for setting aligned or orientated //
25 // boxes, find volume, axes, extents, centers, face planes etc. //
26 // Also tests for overlap testing of planes and other bounding boxes, //
27 // with fast sphere approximation. //
28 //////////////////////////////////////////////////////////////////////////
29 
30 // TODO: Create more compact version + axis aligned version, both with lazy
31 // sphere testing.
33 {
34 private:
35  // Fields
36 
37  // Box vertices are indexed thus (OpenGL is left handed by default)
38  // y
39  // |
40  // |
41  // |________x
42  // / 3-------2
43  // / /| /|
44  // z 7-------6 |
45  // | 0-----|-1
46  // |/ |/
47  // 4-------5
48  //
49  // 0123 'far' face
50  // 4567 'near' face
51  //
52  // This could be more compact:
53  // For orientated box 3 vertices which form plane cutting box
54  // diagonally (e.g. 0,5,6 or 1,3,6 etc) would fix in space.
55  // For axis aligned 2 verticies would suffice.
56  // Rest could be calculated on demand - however speed more important
57  // than memory considerations
58  TGLVertex3 fVertex[8]; //! the 8 bounding box vertices
59  Double_t fVolume; //! box volume - cached for speed
60  Double_t fDiagonal; //! max box diagonal - cached for speed
61  TGLVector3 fAxes[3]; //! box axes in global frame - cached for speed
62  TGLVector3 fAxesNorm[3];//! normalised box axes in global frame - cached for speed
63 
64  // Methods
65  void UpdateCache();
66  Bool_t ValidIndex(UInt_t index) const { return (index < 8); }
67  Double_t Min(UInt_t index) const;
68  Double_t Max(UInt_t index) const;
69 
70 public:
73  TGLBoundingBox(const Double_t vertex[8][3]);
74  TGLBoundingBox(const TGLVertex3 & lowVertex, const TGLVertex3 & highVertex);
75  TGLBoundingBox(const TGLBoundingBox & other);
76  virtual ~TGLBoundingBox(); // ClassDef introduces virtual fns
77 
78  // Set orientated box
79  TGLBoundingBox & operator =(const TGLBoundingBox & other);
80  void Set(const TGLVertex3 vertex[8]);
81  void Set(const Double_t vertex[8][3]);
82  void Set(const TGLBoundingBox & other);
83  void SetEmpty();
84 
85  // Set axis aligned box
86  void SetAligned(const TGLVertex3 & lowVertex, const TGLVertex3 & highVertex); // axis aligned
87  void SetAligned(UInt_t nbPnts, const Double_t * pnts); // axis aligned
88  void MergeAligned(const TGLBoundingBox & other);
89  void ExpandAligned(const TGLVertex3 & point);
90 
91  // Manipulation
92  void Transform(const TGLMatrix & matrix);
93  void Scale(Double_t factor);
94  void Scale(Double_t xFactor, Double_t yFactor, Double_t zFactor);
95  void Translate(const TGLVector3 & offset);
96 
97  // Single vertex accessors
98  const TGLVertex3 & operator [] (UInt_t index) const;
99  const TGLVertex3 & Vertex(UInt_t index) const;
100  Double_t XMin() const { return Min(0); }
101  Double_t XMax() const { return Max(0); }
102  Double_t YMin() const { return Min(1); }
103  Double_t YMax() const { return Max(1); }
104  Double_t ZMin() const { return Min(2); }
105  Double_t ZMax() const { return Max(2); }
106  TGLVertex3 MinAAVertex() const;
107  TGLVertex3 MaxAAVertex() const;
108 
109  // Multiple vertices accessors
110  const TGLVertex3* Vertices() const; // All 8 box vertices
111  Int_t NumVertices() const { return 8; }
112 
114  const std::vector<UInt_t> & FaceVertices(EFace face) const; // 4 box face vertices
115 
116  // Other properties
117  TGLVertex3 Center() const;
118  TGLVector3 Extents() const;
119  const TGLVector3 & Axis(UInt_t i, Bool_t normalised = kTRUE) const;
120  Bool_t IsEmpty() const;
121  Double_t Volume() const { return fVolume; }
122  Double_t Diagonal() const { return fDiagonal; }
123  void PlaneSet(TGLPlaneSet_t & planeSet) const;
124  TGLPlane GetNearPlane() const;
125 
126  // Overlap testing
127  Rgl::EOverlap Overlap(const TGLPlane & plane) const;
128  Rgl::EOverlap Overlap(const TGLBoundingBox & box) const;
129 
130  void Draw(Bool_t solid = kFALSE) const;
131  void Dump() const;
132 
133  ClassDef(TGLBoundingBox,0); // a 3D orientated bounding box
134 };
135 
136 //______________________________________________________________________________
138 {
139  // Check for self-assignment
140  if (this != &other) {
141  Set(other);
142  }
143  return *this;
144 }
145 
146 //______________________________________________________________________________
147 inline const TGLVertex3 & TGLBoundingBox::operator [] (UInt_t index) const
148 {
149  return fVertex[index];
150 }
151 
152 //______________________________________________________________________________
153 inline const TGLVertex3 & TGLBoundingBox::Vertex(UInt_t index) const
154 {
155  return fVertex[index];
156 }
157 
158 //______________________________________________________________________________
160 {
161  return fVertex;
162 }
163 
164 //______________________________________________________________________________
166 {
167  // Return the local axis entents of the box
168  return TGLVector3(Axis(0,kFALSE).Mag(),
169  Axis(1,kFALSE).Mag(),
170  Axis(2,kFALSE).Mag());
171 }
172 
173 //______________________________________________________________________________
175 {
176  // Return the center vertex of the box
177  return TGLVertex3((fVertex[0].X() + fVertex[6].X())/2.0,
178  (fVertex[0].Y() + fVertex[6].Y())/2.0,
179  (fVertex[0].Z() + fVertex[6].Z())/2.0);
180 }
181 
182 //______________________________________________________________________________
183 inline const TGLVector3 & TGLBoundingBox::Axis(UInt_t i, Bool_t normalised) const
184 {
185  // Return a vector representing axis of index i (0:X, 1:Y, 2:Z).
186  // Vector can be as-is (edge, magnitude == extent) or normalised (default)
187  // y
188  // |
189  // |
190  // |________x
191  // / 3-------2
192  // / /| /|
193  // z 7-------6 |
194  // | 0-----|-1
195  // |/ |/
196  // 4-------5
197  //
198 
199  if (normalised) {
200  return fAxesNorm[i];
201  } else {
202  return fAxes[i];
203  }
204 }
205 
206 //______________________________________________________________________________
208 {
209  // Return kTRUE if box has zero diagonal - kFALSE otherwise
210 
211  // TODO: Round errors - should have epsilon test
212  return (Diagonal() == 0.0);
213 }
214 
215 #endif // ROOT_TGLBoundingBox
void SetAligned(const TGLVertex3 &lowVertex, const TGLVertex3 &highVertex)
Set ALIGNED box from two low/high vertices.
Double_t ZMax() const
TGLVertex3 Center() const
TGLVector3 Extents() const
TGLVertex3 fVertex[8]
void Dump() const
Output to std::cout the vertices, center and volume of box.
const TGLVertex3 & Vertex(UInt_t index) const
void Set(const TGLVertex3 vertex[8])
Set a bounding box from provided 8 vertices.
16 component (4x4) transform matrix - column MAJOR as per GL.
Definition: TGLUtil.h:600
TGLVector3 fAxes[3]
max box diagonal - cached for speed
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:254
Int_t NumVertices() const
TGLVector3 fAxesNorm[3]
box axes in global frame - cached for speed
Double_t YMax() const
Double_t Max(UInt_t index) const
Find maximum vertex value for axis of index X(0), Y(1), Z(2)
Double_t XMin() const
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
Bool_t ValidIndex(UInt_t index) const
TGLVertex3 MinAAVertex() const
Find minimum vertex values.
Bool_t IsEmpty() const
#define ClassDef(name, id)
Definition: Rtypes.h:254
void Transform(const TGLMatrix &matrix)
Transform all vertices with matrix.
3 component (x/y/z) vertex class.
Definition: TGLUtil.h:86
TGLPlane GetNearPlane() const
Return the near-plane.
3 component (x/y/z) vector class.
Definition: TGLUtil.h:250
void UpdateCache()
normalised box axes in global frame - cached for speed
void PlaneSet(TGLPlaneSet_t &planeSet) const
Fill out supplied plane set vector with TGLPlane objects representing six faces of box...
Double_t Min(UInt_t index) const
Find minimum vertex value for axis of index X(0), Y(1), Z(2)
TGLBoundingBox()
Construct an empty bounding box.
Double_t Diagonal() const
REAL * vertex
Definition: triangle.c:512
TGLVertex3 MaxAAVertex() const
Find maximum vertex values.
const TGLVertex3 * Vertices() const
void ExpandAligned(const TGLVertex3 &point)
Expand current bbox so that it includes the point.
Double_t fDiagonal
box volume - cached for speed
virtual ~TGLBoundingBox()
Destroy bounding box.
unsigned int UInt_t
Definition: RtypesCore.h:42
const TGLVertex3 & operator[](UInt_t index) const
Double_t XMax() const
void MergeAligned(const TGLBoundingBox &other)
Expand current bbox so that it includes other&#39;s bbox.
void Scale(Double_t factor)
Isotropically scale bounding box along it&#39;s LOCAL axes, preserving center.
std::vector< TGLPlane > TGLPlaneSet_t
Definition: TGLUtil.h:574
double Double_t
Definition: RtypesCore.h:55
void Draw(Bool_t solid=kFALSE) const
Draw the bounding box as either wireframe (default) of solid using current GL color.
void SetEmpty()
Set bounding box empty - all vertices at (0,0,0)
Double_t fVolume
the 8 bounding box vertices
Concrete class describing an orientated (free) or axis aligned box of 8 vertices. ...
const std::vector< UInt_t > & FaceVertices(EFace face) const
return a vector of face vertices y | | |________x / 3----—2 / /| /| z 7----—6 | | 0--—|-1 |/ |/ 4-...
TGLBoundingBox & operator=(const TGLBoundingBox &other)
EOverlap
Definition: TGLUtil.h:37
Double_t YMin() const
void Translate(const TGLVector3 &offset)
Translate all vertices by offset.
const TGLVector3 & Axis(UInt_t i, Bool_t normalised=kTRUE) const
Double_t Volume() const
Rgl::EOverlap Overlap(const TGLPlane &plane) const
Find overlap (Inside, Outside, Partial) of plane c.f. bounding box.
Double_t ZMin() const
const Bool_t kTRUE
Definition: Rtypes.h:91
3D plane class - of format Ax + By + Cz + D = 0
Definition: TGLUtil.h:529