Blender  V2.93
BBox.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
24 #include <algorithm>
25 #include <stdlib.h>
26 
27 #include "BLI_utildefines.h"
28 
29 #ifdef WITH_CXX_GUARDEDALLOC
30 # include "MEM_guardedalloc.h"
31 #endif
32 
33 namespace Freestyle {
34 
35 template<class Point> class BBox {
36  public:
37  inline BBox()
38  {
39  _empty = true;
40  }
41 
42  template<class T> inline BBox(const T &min_in, const T &max_in) : _min(min_in), _max(max_in)
43  {
44  _empty = false;
45  }
46 
47  template<class T> inline BBox(const BBox<T> &b) : _min(b.getMin()), _max(b.getMax())
48  {
49  _empty = false;
50  }
51 
52  template<class T> inline void extendToContain(const T &p)
53  {
54  if (_empty) {
55  _min = p;
56  _max = p;
57  _empty = false;
58  return;
59  }
60  for (unsigned int i = 0; i < Point::dim(); i++) {
61  if (p[i] < _min[i]) {
62  _min[i] = p[i];
63  }
64  else if (p[i] > _max[i]) {
65  _max[i] = p[i];
66  }
67  }
68  _empty = false;
69  }
70 
71  inline void clear()
72  {
73  _empty = true;
74  }
75 
76  inline bool empty() const
77  {
78  return _empty;
79  }
80 
81  inline const Point &getMin() const
82  {
83  return _min;
84  }
85 
86  inline const Point &getMax() const
87  {
88  return _max;
89  }
90 
91  inline BBox<Point> &operator=(const BBox<Point> &b)
92  {
93  BLI_assert(!b.empty());
94  _min = b.getMin();
95  _max = b.getMax();
96  _empty = false;
97  return *this;
98  }
99 
101  {
102  BLI_assert(!b.empty());
103  if (_empty) {
104  _min = b.getMin();
105  _max = b.getMax();
106  _empty = false;
107  }
108  else {
109  for (unsigned int i = 0; i < Point::dim(); i++) {
110  if (b.getMin()[i] < _min[i]) {
111  _min[i] = b.getMin()[i];
112  }
113  if (b.getMax()[i] > _max[i]) {
114  _max[i] = b.getMax()[i];
115  }
116  }
117  }
118  return *this;
119  }
120 
121  inline bool inside(const Point &p)
122  {
123  if (empty()) {
124  return false;
125  }
126  for (unsigned int i = 0; i < Point::dim(); i++) {
127  if ((_min[i] > p[i]) || (_max[i] < p[i])) {
128  return false;
129  }
130  }
131  return true;
132  }
133 
134  private:
135  Point _min;
136  Point _max;
137  bool _empty;
138 
139 #ifdef WITH_CXX_GUARDEDALLOC
140  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:BBox")
141 #endif
142 };
143 
144 template<class Point> BBox<Point> &operator+(const BBox<Point> &b1, const BBox<Point> &b2)
145 {
146  Point new_min;
147  Point new_max;
148 
149  for (unsigned int i = 0; i < Point::dim(); i++) {
150  new_min[i] = b1.getMin()[i] < b2.getMin()[i] ? b1.getMin()[i] : b2.getMin()[i];
151  new_max[i] = b1.getMax()[i] > b2.getMax()[i] ? b1.getMax()[i] : b2.getMax()[i];
152  }
153 
154  return BBox<Point>(new_min, new_max);
155 }
156 
157 } /* namespace Freestyle */
#define BLI_assert(a)
Definition: BLI_assert.h:58
Read Guarded memory(de)allocation.
BBox< Point > & operator+=(const BBox< Point > &b)
Definition: BBox.h:100
BBox(const BBox< T > &b)
Definition: BBox.h:47
BBox< Point > & operator=(const BBox< Point > &b)
Definition: BBox.h:91
bool empty() const
Definition: BBox.h:76
bool inside(const Point &p)
Definition: BBox.h:121
const Point & getMax() const
Definition: BBox.h:86
BBox(const T &min_in, const T &max_in)
Definition: BBox.h:42
const Point & getMin() const
Definition: BBox.h:81
void extendToContain(const T &p)
Definition: BBox.h:52
void clear()
Definition: BBox.h:71
#define T
inherits from class Rep
Definition: AppCanvas.cpp:32
BBox< Point > & operator+(const BBox< Point > &b1, const BBox< Point > &b2)
Definition: BBox.h:144