Logo ROOT   6.12/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 #include "TGLUtil.h"
16 
17 //////////////////////////////////////////////////////////////////////////
18 // //
19 // TGLBoundingBox //
20 // //
21 // Concrete class describing an orientated (free) or axis aligned box //
22 // of 8 verticies. Supports methods for setting aligned or orientated //
23 // boxes, find volume, axes, extents, centers, face planes etc. //
24 // Also tests for overlap testing of planes and other bounding boxes, //
25 // with fast sphere approximation. //
26 //////////////////////////////////////////////////////////////////////////
27 
28 // TODO: Create more compact version + axis aligned version, both with lazy
29 // sphere testing.
31 {
32 private:
33  // Fields
34 
35  // Box vertices are indexed thus (OpenGL is left handed by default)
36  // y
37  // |
38  // |
39  // |________x
40  // / 3-------2
41  // / /| /|
42  // z 7-------6 |
43  // | 0-----|-1
44  // |/ |/
45  // 4-------5
46  //
47  // 0123 'far' face
48  // 4567 'near' face
49  //
50  // This could be more compact:
51  // For orientated box 3 vertices which form plane cutting box
52  // diagonally (e.g. 0,5,6 or 1,3,6 etc) would fix in space.
53  // For axis aligned 2 verticies would suffice.
54  // Rest could be calculated on demand - however speed more important
55  // than memory considerations
56  TGLVertex3 fVertex[8]; //! the 8 bounding box vertices
57  Double_t fVolume; //! box volume - cached for speed
58  Double_t fDiagonal; //! max box diagonal - cached for speed
59  TGLVector3 fAxes[3]; //! box axes in global frame - cached for speed
60  TGLVector3 fAxesNorm[3];//! normalised box axes in global frame - cached for speed
61 
62  // Methods
63  void UpdateCache();
64  Bool_t ValidIndex(UInt_t index) const { return (index < 8); }
65  Double_t Min(UInt_t index) const;
66  Double_t Max(UInt_t index) const;
67 
68 public:
71  TGLBoundingBox(const Double_t vertex[8][3]);
72  TGLBoundingBox(const TGLVertex3 & lowVertex, const TGLVertex3 & highVertex);
73  TGLBoundingBox(const TGLBoundingBox & other);
74  virtual ~TGLBoundingBox(); // ClassDef introduces virtual fns
75 
76  // Set orientated box
77  TGLBoundingBox & operator =(const TGLBoundingBox & other);
78  void Set(const TGLVertex3 vertex[8]);
79  void Set(const Double_t vertex[8][3]);
80  void Set(const TGLBoundingBox & other);
81  void SetEmpty();
82 
83  // Set axis aligned box
84  void SetAligned(const TGLVertex3 & lowVertex, const TGLVertex3 & highVertex); // axis aligned
85  void SetAligned(UInt_t nbPnts, const Double_t * pnts); // axis aligned
86  void MergeAligned(const TGLBoundingBox & other);
87  void ExpandAligned(const TGLVertex3 & point);
88 
89  // Manipulation
90  void Transform(const TGLMatrix & matrix);
91  void Scale(Double_t factor);
92  void Scale(Double_t xFactor, Double_t yFactor, Double_t zFactor);
93  void Translate(const TGLVector3 & offset);
94 
95  // Single vertex accessors
96  const TGLVertex3 & operator [] (UInt_t index) const;
97  const TGLVertex3 & Vertex(UInt_t index) const;
98  Double_t XMin() const { return Min(0); }
99  Double_t XMax() const { return Max(0); }
100  Double_t YMin() const { return Min(1); }
101  Double_t YMax() const { return Max(1); }
102  Double_t ZMin() const { return Min(2); }
103  Double_t ZMax() const { return Max(2); }
104  TGLVertex3 MinAAVertex() const;
105  TGLVertex3 MaxAAVertex() const;
106 
107  // Multiple vertices accessors
108  const TGLVertex3* Vertices() const; // All 8 box vertices
109  Int_t NumVertices() const { return 8; }
110 
112  const std::vector<UInt_t> & FaceVertices(EFace face) const; // 4 box face vertices
113 
114  // Other properties
115  TGLVertex3 Center() const;
116  TGLVector3 Extents() const;
117  const TGLVector3 & Axis(UInt_t i, Bool_t normalised = kTRUE) const;
118  Bool_t IsEmpty() const;
119  Double_t Volume() const { return fVolume; }
120  Double_t Diagonal() const { return fDiagonal; }
121  void PlaneSet(TGLPlaneSet_t & planeSet) const;
122  TGLPlane GetNearPlane() const;
123 
124  // Overlap testing
125  Rgl::EOverlap Overlap(const TGLPlane & plane) const;
126  Rgl::EOverlap Overlap(const TGLBoundingBox & box) const;
127 
128  void Draw(Bool_t solid = kFALSE) const;
129  void Dump() const;
130 
131  ClassDef(TGLBoundingBox,0); // a 3D orientated bounding box
132 };
133 
134 //______________________________________________________________________________
136 {
137  // Check for self-assignment
138  if (this != &other) {
139  Set(other);
140  }
141  return *this;
142 }
143 
144 //______________________________________________________________________________
145 inline const TGLVertex3 & TGLBoundingBox::operator [] (UInt_t index) const
146 {
147  return fVertex[index];
148 }
149 
150 //______________________________________________________________________________
151 inline const TGLVertex3 & TGLBoundingBox::Vertex(UInt_t index) const
152 {
153  return fVertex[index];
154 }
155 
156 //______________________________________________________________________________
158 {
159  return fVertex;
160 }
161 
162 //______________________________________________________________________________
164 {
165  // Return the local axis entents of the box
166  return TGLVector3(Axis(0,kFALSE).Mag(),
167  Axis(1,kFALSE).Mag(),
168  Axis(2,kFALSE).Mag());
169 }
170 
171 //______________________________________________________________________________
173 {
174  // Return the center vertex of the box
175  return TGLVertex3((fVertex[0].X() + fVertex[6].X())/2.0,
176  (fVertex[0].Y() + fVertex[6].Y())/2.0,
177  (fVertex[0].Z() + fVertex[6].Z())/2.0);
178 }
179 
180 //______________________________________________________________________________
181 inline const TGLVector3 & TGLBoundingBox::Axis(UInt_t i, Bool_t normalised) const
182 {
183  // Return a vector representing axis of index i (0:X, 1:Y, 2:Z).
184  // Vector can be as-is (edge, magnitude == extent) or normalised (default)
185  // y
186  // |
187  // |
188  // |________x
189  // / 3-------2
190  // / /| /|
191  // z 7-------6 |
192  // | 0-----|-1
193  // |/ |/
194  // 4-------5
195  //
196 
197  if (normalised) {
198  return fAxesNorm[i];
199  } else {
200  return fAxes[i];
201  }
202 }
203 
204 //______________________________________________________________________________
206 {
207  // Return kTRUE if box has zero diagonal - kFALSE otherwise
208 
209  // TODO: Round errors - should have epsilon test
210  return (Diagonal() == 0.0);
211 }
212 
213 #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:596
TGLVector3 fAxes[3]
max box diagonal - cached for speed
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:252
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
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:320
void Transform(const TGLMatrix &matrix)
Transform all vertices with matrix.
3 component (x/y/z) vertex class.
Definition: TGLUtil.h:82
TGLPlane GetNearPlane() const
Return the near-plane.
3 component (x/y/z) vector class.
Definition: TGLUtil.h:246
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.
const Bool_t kFALSE
Definition: RtypesCore.h:88
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:570
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:33
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: RtypesCore.h:87
3D plane class - of format Ax + By + Cz + D = 0
Definition: TGLUtil.h:525