1 // Copyright 2018 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #ifndef DRACO_CORE_BOUNDING_BOX_H_
16 #define DRACO_CORE_BOUNDING_BOX_H_
17 
18 #include "draco/core/vector_d.h"
19 
20 namespace draco {
21 
22 // Class for computing the bounding box of points in 3D space.
23 class BoundingBox {
24  public:
25   // Creates bounding box object with minimum and maximum points initialized to
26   // the largest positive and the smallest negative values, respectively. The
27   // resulting abstract bounding box effectively has no points and can be
28   // updated by providing any point to Update() method.
29   BoundingBox();
30 
31   // Creates bounding box object with minimum and maximum points initialized to
32   // |min_point| and |max_point|, respectively.
33   BoundingBox(const Vector3f &min_point, const Vector3f &max_point);
34 
35   // Returns the minimum point of the bounding box.
GetMinPoint()36   inline const Vector3f &GetMinPoint() const { return min_point_; }
37 
38   // Returns the maximum point of the bounding box.
GetMaxPoint()39   inline const Vector3f &GetMaxPoint() const { return max_point_; }
40 
41   // Conditionally updates the bounding box with a given |new_point|.
Update(const Vector3f & new_point)42   void Update(const Vector3f &new_point) {
43     for (int i = 0; i < 3; i++) {
44       if (new_point[i] < min_point_[i]) {
45         min_point_[i] = new_point[i];
46       }
47       if (new_point[i] > max_point_[i]) {
48         max_point_[i] = new_point[i];
49       }
50     }
51   }
52 
53   // Updates bounding box with minimum and maximum points of the |other|
54   // bounding box.
Update(const BoundingBox & other)55   void Update(const BoundingBox &other) {
56     Update(other.GetMinPoint());
57     Update(other.GetMaxPoint());
58   }
59 
60   // Returns the size of the bounding box along each axis.
Size()61   Vector3f Size() const { return max_point_ - min_point_; }
62 
63   // Returns the center of the bounding box.
Center()64   Vector3f Center() const { return (min_point_ + max_point_) / 2; }
65 
66  private:
67   Vector3f min_point_;
68   Vector3f max_point_;
69 };
70 }  // namespace draco
71 
72 #endif  //  DRACO_CORE_BOUNDING_BOX_H_
73