Tulip  5.2.1
Large graphs analysis and drawing
BoundingBox.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef Tulip_BOUNDINGBOX_H
21 #define Tulip_BOUNDINGBOX_H
22 
23 #include <cassert>
24 #include <tulip/Vector.h>
25 #include <tulip/tulipconf.h>
26 
27 namespace tlp {
28 /**
29  * \ingroup Structures
30  * \brief This class represents the 3D bounding box of an object.
31  * It is mostly used to determine whether or not two object are in a state of collision.
32  *
33  * It is defined by two 3d points, the first one (A) being the lowest point, the second (B) being
34  the highest.
35  * As a bounding box is a mathematical entity describing the lowest and highest points, whether
36  these points are in the top-left corner or
37  * lower-right corner depends on the axes we use.
38  * Below is a crude ASCII-art description of the axes we use in our 3D world and the points where
39  the min and max are thus positioned.
40  * Through the rest of this class's documentation, it will be assumed that this is the convention.
41  *
42  *
43  * @verbatim
44  y
45  |
46  |
47  |_____ x
48  /
49  /
50  z
51 
52  _________ B
53  / /|
54  / / |
55  /________/ |
56  | | |
57  | | |
58  | | /
59  |________|/
60  A
61 
62  @endverbatim
63  *
64  *
65  */
66 struct TLP_SCOPE BoundingBox : public Array<Vec3f, 2> {
67 
68  /**
69  * @brief Creates an invalid boundig box.
70  * The minimum is (1, 1, 1) and the maximum is (-1, -1, -1).
71  *
72  **/
73  BoundingBox();
74 
75  /**
76  * @brief Creates a bounding box that must be valid.
77  * Validity is checked in debug mode by an assert.
78  *
79  * @param min The lower left closest point of the box.
80  * @param max The higher right most farther point of the box.
81  * @param compute indicates whether the bounding box has to be computed with the min/max args
82  *
83  **/
84  BoundingBox(const tlp::Vec3f &min, const tlp::Vec3f &max, bool checkMinMax = false);
85 
86  /**
87  * @brief Returns the geometrical center of the bounding box.
88  * An assertion is raised in debug mode if the BoundingBox is not valid.
89  *
90  * @return The center of the bounding box :Vec3f
91  **/
92  inline Vec3f center() const {
93  assert(isValid());
94  return ((*this)[0] + (*this)[1]) / 2.f;
95  }
96 
97  /**
98  * @brief Returns the width of the bounding box
99  * An assertion is raised in debug mode if the BoundingBox is not valid.
100  *
101  **/
102  inline float width() const {
103  assert(isValid());
104  return ((*this)[1][0] - (*this)[0][0]);
105  }
106 
107  /**
108  * @brief Returns the height of the bounding box
109  * An assertion is raised in debug mode if the bounding box is not valid.
110  *
111  **/
112  inline float height() const {
113  assert(isValid());
114  return ((*this)[1][1] - (*this)[0][1]);
115  }
116 
117  /**
118  * @brief Returns the depth of the bounding box
119  * An assertion is raised in debug mode if the bounding box is not valid.
120  *
121  **/
122  inline float depth() const {
123  assert(isValid());
124  return ((*this)[1][2] - (*this)[0][2]);
125  }
126 
127  /**
128  * @brief Expands the bounding box to one containing the vector passed as parameter.
129  * If the parameter is inside the bounding box, it remains unchanged.
130  *
131  * @param coord A point in the 3D space we want the bounding box to encompass.
132  * @return void
133  **/
134  void expand(const tlp::Vec3f &coord);
135 
136  /**
137  * @brief Expands the bounding box to one containing the bounding box passed as parameter.
138  * If the parameter is inside the bounding box, it remains unchanged.
139  *
140  * @param bb A bounding box.
141  * @return void
142  **/
143  void expand(const tlp::BoundingBox &bb, bool noCheck = false);
144 
145  /**
146  * @brief Translates the bounding box by the displacement given by the vector passed as parameter.
147  *
148  * @param vec The displacement vector in 3D space to translate this bounding box by.
149  * @return void
150  **/
151  void translate(const tlp::Vec3f &vec);
152 
153  /**
154  * @brief Scales the bounding box, i.e. multiplying its components by a vector passed as
155  *parameter.
156  *
157  * @param factor The factor vector to scale this bounding box by.
158  * @return void
159  **/
160  void scale(const tlp::Vec3f &factor);
161 
162  /**
163  * @brief Checks whether the bounding box's lowest point is less than it's highest point.
164  * "Less Than" means axis-by-axis comparison, i.e. x1 < x2 && y1 < y2 && z1 < z2.
165  *
166  * @return bool Whether this bounding box is valid.
167  **/
168  bool isValid() const;
169 
170  /**
171  * @brief Checks if the given vector is inside the current bounding box. If the bounding box is
172  *invalid the result is always false.
173  * @param coord A point in the 3D space.
174  * @return bool Wether coord is in the bounding box.
175  **/
176  bool contains(const tlp::Vec3f &coord) const;
177 
178  /**
179  * @brief Checks if the given bounding box is inside the current bounding box. If one of the
180  *bounding boxes is invalid the result is always false.
181  * @param boundingBox The bounding box to test inclusion
182  * @return bool Wether boundingBox is in the bounding box.
183  **/
184  bool contains(const tlp::BoundingBox &boundingBox) const;
185 
186  /**
187  * @brief Checks if the given bounding box intersect the current one. If one of the bounding box
188  *is invalid return false.
189  * @param boundingBox The bounding box to compare with.
190  * @return bool Wether the bounding boxes intersect.
191  **/
192  bool intersect(const tlp::BoundingBox &boundingBox) const;
193 
194  /**
195  * @brief Checks if the bounding box intersects a given line segment. If the bounding box is
196  *invalid the result is always false.
197  * @param segStart the start point of the line segment on which to check intersection
198  * @param segEnd the end point of the line segment on which to check intersection
199  * @return bool Wether the line segment intersects the bounding box
200  **/
201  bool intersect(const Vec3f &segStart, const Vec3f &segEnd) const;
202 
203  /**
204  * @brief The vector passed as parameter is modified to contain the 8 points of the bounding box.
205  * The points are, in order :
206  * 0: lower leftmost closest point (the bounding box's minimum)
207  * 1: lower rightmost closest point
208  * 2: highest rightmost closest point
209  * 3: highest leftmost closest point
210  * 4: lower rightmost farthest point
211  * 5: lower rightmost farthest point
212  * 6: highest rightmost farthest point
213  * 7: highest leftmost farthest point
214  *
215  * Crude ASCII art again, sorry for your eyes.
216  *
217  * @verbatim
218 
219  6_________ 7
220  /| /|
221  / | / |
222  3/__|_____/2 |
223  | |_____|__|
224  | /4 | /5
225  | / | /
226  |/_______|/
227  0 1
228 
229  @endverbatim
230  *
231  * @param bb A vector in which to put the points of the bounding box.
232  * @return void
233  **/
234  void getCompleteBB(Vec3f bb[8]) const;
235 };
236 } // namespace tlp
237 
238 #endif // Tulip_BOUNDINGBOX_H
float width() const
Returns the width of the bounding box An assertion is raised in debug mode if the BoundingBox is not ...
Definition: BoundingBox.h:102
This class represents the 3D bounding box of an object. It is mostly used to determine whether or not...
Definition: BoundingBox.h:66
Vec3f center() const
Returns the geometrical center of the bounding box. An assertion is raised in debug mode if the Bound...
Definition: BoundingBox.h:92
float depth() const
Returns the depth of the bounding box An assertion is raised in debug mode if the bounding box is not...
Definition: BoundingBox.h:122
float height() const
Returns the height of the bounding box An assertion is raised in debug mode if the bounding box is no...
Definition: BoundingBox.h:112