Tulip  5.7.4
Large graphs analysis and drawing
BoundingBox.h
1 /*
2  *
3  * This file is part of Tulip (https://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 <vector>
25 #include <tulip/Vector.h>
26 #include <tulip/tulipconf.h>
27 
28 namespace tlp {
29 /**
30  * \ingroup Structures
31  * \brief This class represents the 3D bounding box of an object.
32  * It is mostly used to determine whether or not two object are in a state of collision.
33  *
34  * It is defined by two 3D points, the first one (A) being the lowest point, the second (B) being
35  the highest.
36  * As a bounding box is a mathematical entity describing the lowest and highest points, whether
37  these points are in the top-left corner or
38  * lower-right corner depends on the axes we use.
39  * Below is a crude ASCII-art description of the axes we use in our 3D world and the points where
40  the min and max are thus positioned.
41  * Through the rest of this class's documentation, it will be assumed that this is the convention.
42  *
43  *
44  * @verbatim
45  y
46  |
47  |
48  |_____ x
49  /
50  /
51  z
52 
53  _________ B
54  / /|
55  / / |
56  /________/ |
57  | | |
58  | | |
59  | | /
60  |________|/
61  A
62 
63  @endverbatim
64  *
65  *
66  */
67 struct TLP_SCOPE BoundingBox : public Array<Vec3f, 2> {
68 
69  /**
70  * @brief Creates an invalid boundig box.
71  * The minimum is (1, 1, 1) and the maximum is (-1, -1, -1).
72  *
73  **/
75 
76  /**
77  * @brief Creates a bounding box that must be valid.
78  * Validity is checked in debug mode by an assert.
79  *
80  * @param min The lower left closest point of the box.
81  * @param max The higher right most farther point of the box.
82  * @param compute indicates whether the bounding box has to be computed with the min/max args
83  *
84  **/
85  BoundingBox(const tlp::Vec3f &min, const tlp::Vec3f &max, bool checkMinMax = false);
86 
87  /**
88  * @brief restore an invalid state.
89  **/
90  void clear();
91 
92  /**
93  * @brief init the bounding box with the point passed as parameter.
94  * @param coord A point in the 3D space
95  **/
96  inline void init(const tlp::Vec3f &coord) {
97  (*this)[0] = (*this)[1] = coord;
98  }
99 
100  /**
101  * @brief Returns the geometrical center of the bounding box.
102  * An assertion is raised in debug mode if the BoundingBox is not valid.
103  *
104  * @return The center of the bounding box :Vec3f
105  **/
106  inline Vec3f center() const {
107  assert(isValid());
108  return ((*this)[0] + (*this)[1]) / 2.f;
109  }
110 
111  /**
112  * @brief Returns the width of the bounding box
113  * An assertion is raised in debug mode if the BoundingBox is not valid.
114  *
115  **/
116  inline float width() const {
117  assert(isValid());
118  return ((*this)[1][0] - (*this)[0][0]);
119  }
120 
121  /**
122  * @brief Returns the height of the bounding box
123  * An assertion is raised in debug mode if the bounding box is not valid.
124  *
125  **/
126  inline float height() const {
127  assert(isValid());
128  return ((*this)[1][1] - (*this)[0][1]);
129  }
130 
131  /**
132  * @brief Returns the depth of the bounding box
133  * An assertion is raised in debug mode if the bounding box is not valid.
134  *
135  **/
136  inline float depth() const {
137  assert(isValid());
138  return ((*this)[1][2] - (*this)[0][2]);
139  }
140 
141  /**
142  * @brief Expands the bounding box to one containing the point passed as parameter.
143  * If the parameter is inside the bounding box, it remains unchanged.
144  *
145  * @param coord A point in the 3D space we want the bounding box to encompass.
146  * @param noCheck if true current bounding box validity is not checked
147  * @return void
148  **/
149  void expand(const tlp::Vec3f &coord, bool noCheck = false);
150 
151  /**
152  * @brief Expands the bounding box to one containing all the point of the vector passed as
153  *parameter.
154  *
155  * @param coords A vector of point in the 3D space we want the bounding box to encompass.
156  * @return void
157  **/
158  inline void expand(const std::vector<tlp::Vec3f> &coords) {
159  auto it = coords.begin();
160  expand(*it);
161  for (++it; it < coords.end(); ++it)
162  expand(*it, true);
163  }
164 
165  /**
166  * @brief Expands the bounding box to one containing the bounding box passed as parameter.
167  * If the parameter is inside the bounding box, it remains unchanged.
168  *
169  * @param bb A bounding box.
170  * @param noCheck if true current bounding box validity is not checked
171  * @return void
172  **/
173  void expand(const tlp::BoundingBox &bb, bool noCheck = false);
174 
175  /**
176  * @brief Translates the bounding box by the displacement given by the vector passed as parameter.
177  *
178  * @param vec The displacement vector in 3D space to translate this bounding box by.
179  * @return void
180  **/
181  void translate(const tlp::Vec3f &vec);
182 
183  /**
184  * @brief Scales the bounding box, i.e. multiplying its components by a vector passed as
185  *parameter.
186  *
187  * @param factor The factor vector to scale this bounding box by.
188  * @return void
189  **/
190  void scale(const tlp::Vec3f &factor);
191 
192  /**
193  * @brief Checks whether the bounding box's lowest point is less than it's highest point.
194  * "Less Than" means axis-by-axis comparison, i.e. x1 < x2 && y1 < y2 && z1 < z2.
195  *
196  * @return bool Whether this bounding box is valid.
197  **/
198  bool isValid() const;
199 
200  /**
201  * @brief Checks if the given vector is inside the current bounding box. If the bounding box is
202  *invalid the result is always false.
203  * @param coord A point in the 3D space.
204  * @param noCheck if true current bounding box validity is not checked
205  * @return bool Whether coord is in the bounding box.
206  **/
207  bool contains(const tlp::Vec3f &coord, bool noCheck = false) const;
208 
209  /**
210  * @brief Checks if the given bounding box is inside the current bounding box. If one of the
211  *bounding boxes is invalid the result is always false.
212  * @param boundingBox The bounding box to test inclusion
213  * @return bool Whether boundingBox is in the bounding box.
214  **/
215  bool contains(const tlp::BoundingBox &boundingBox) const;
216 
217  /**
218  * @brief Checks if the given bounding box intersects the current one. If one of the bounding box
219  *is invalid return false.
220  * @param boundingBox The bounding box to compare with.
221  * @return bool Whether the bounding boxes intersect.
222  **/
223  bool intersect(const tlp::BoundingBox &boundingBox) const;
224 
225  /**
226  * @brief Checks if the bounding box intersects a given line segment. If the bounding box is
227  *invalid the result is always false.
228  * @param segStart the start point of the line segment on which to check intersection
229  * @param segEnd the end point of the line segment on which to check intersection
230  * @return bool Whether the line segment intersects the bounding box
231  **/
232  bool intersect(const Vec3f &segStart, const Vec3f &segEnd) const;
233 
234  /**
235  * @brief The vector passed as parameter is modified to contain the 8 points of the bounding box.
236  * The points are, in order :
237  * 0: lower leftmost closest point (the bounding box's minimum)
238  * 1: lower rightmost closest point
239  * 2: highest rightmost closest point
240  * 3: highest leftmost closest point
241  * 4: lower rightmost farthest point
242  * 5: lower rightmost farthest point
243  * 6: highest rightmost farthest point
244  * 7: highest leftmost farthest point
245  *
246  * Crude ASCII art again, sorry for your eyes.
247  *
248  * @verbatim
249 
250  6_________ 7
251  /| /|
252  / | / |
253  3/__|_____/2 |
254  | |_____|__|
255  | /4 | /5
256  | / | /
257  |/_______|/
258  0 1
259 
260  @endverbatim
261  *
262  * @param bb A vector in which to put the points of the bounding box.
263  * @return void
264  **/
265  void getCompleteBB(Vec3f bb[8]) const;
266 };
267 } // namespace tlp
268 
269 #endif // Tulip_BOUNDINGBOX_H
This class represents the 3D bounding box of an object. It is mostly used to determine whether or not...
Definition: BoundingBox.h:67
void init(const tlp::Vec3f &coord)
init the bounding box with the point passed as parameter.
Definition: BoundingBox.h:96
void getCompleteBB(Vec3f bb[8]) const
The vector passed as parameter is modified to contain the 8 points of the bounding box....
bool contains(const tlp::Vec3f &coord, bool noCheck=false) const
Checks if the given vector is inside the current bounding box. If the bounding box is invalid the res...
bool intersect(const Vec3f &segStart, const Vec3f &segEnd) const
Checks if the bounding box intersects a given line segment. If the bounding box is invalid the result...
void clear()
restore an invalid state.
Vec3f center() const
Returns the geometrical center of the bounding box. An assertion is raised in debug mode if the Bound...
Definition: BoundingBox.h:106
BoundingBox()
Creates an invalid boundig box. The minimum is (1, 1, 1) and the maximum is (-1, -1,...
BoundingBox(const tlp::Vec3f &min, const tlp::Vec3f &max, bool checkMinMax=false)
Creates a bounding box that must be valid. Validity is checked in debug mode by an assert.
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:126
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:136
bool contains(const tlp::BoundingBox &boundingBox) const
Checks if the given bounding box is inside the current bounding box. If one of the bounding boxes is ...
bool isValid() const
Checks whether the bounding box's lowest point is less than it's highest point. "Less Than" means axi...
bool intersect(const tlp::BoundingBox &boundingBox) const
Checks if the given bounding box intersects the current one. If one of the bounding box is invalid re...
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:116
void expand(const tlp::Vec3f &coord, bool noCheck=false)
Expands the bounding box to one containing the point passed as parameter. If the parameter is inside ...
void expand(const std::vector< tlp::Vec3f > &coords)
Expands the bounding box to one containing all the point of the vector passed as parameter.
Definition: BoundingBox.h:158
void expand(const tlp::BoundingBox &bb, bool noCheck=false)
Expands the bounding box to one containing the bounding box passed as parameter. If the parameter is ...
void translate(const tlp::Vec3f &vec)
Translates the bounding box by the displacement given by the vector passed as parameter.
void scale(const tlp::Vec3f &factor)
Scales the bounding box, i.e. multiplying its components by a vector passed as parameter.