Blender V5.0
BBox.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11
12#include <algorithm>
13#include <stdlib.h>
14
15#include "BLI_utildefines.h"
16
17#include "MEM_guardedalloc.h"
18
19namespace Freestyle {
20
21template<class Point> class BBox {
22 public:
23 inline BBox()
24 {
25 _empty = true;
26 }
27
28 template<class T> inline BBox(const T &min_in, const T &max_in) : _min(min_in), _max(max_in)
29 {
30 _empty = false;
31 }
32
33 template<class T> inline BBox(const BBox<T> &b) : _min(b.getMin()), _max(b.getMax())
34 {
35 _empty = false;
36 }
37
38 template<class T> inline void extendToContain(const T &p)
39 {
40 if (_empty) {
41 _min = p;
42 _max = p;
43 _empty = false;
44 return;
45 }
46 for (uint i = 0; i < Point::dim(); i++) {
47 if (p[i] < _min[i]) {
48 _min[i] = p[i];
49 }
50 else if (p[i] > _max[i]) {
51 _max[i] = p[i];
52 }
53 }
54 _empty = false;
55 }
56
57 inline void clear()
58 {
59 _empty = true;
60 }
61
62 inline bool empty() const
63 {
64 return _empty;
65 }
66
67 inline const Point &getMin() const
68 {
69 return _min;
70 }
71
72 inline const Point &getMax() const
73 {
74 return _max;
75 }
76
78 {
79 BLI_assert(!b.empty());
80 _min = b.getMin();
81 _max = b.getMax();
82 _empty = false;
83 return *this;
84 }
85
87 {
88 BLI_assert(!b.empty());
89 if (_empty) {
90 _min = b.getMin();
91 _max = b.getMax();
92 _empty = false;
93 }
94 else {
95 for (uint i = 0; i < Point::dim(); i++) {
96 if (b.getMin()[i] < _min[i]) {
97 _min[i] = b.getMin()[i];
98 }
99 if (b.getMax()[i] > _max[i]) {
100 _max[i] = b.getMax()[i];
101 }
102 }
103 }
104 return *this;
105 }
106
107 inline bool inside(const Point &p)
108 {
109 if (empty()) {
110 return false;
111 }
112 for (uint i = 0; i < Point::dim(); i++) {
113 if ((_min[i] > p[i]) || (_max[i] < p[i])) {
114 return false;
115 }
116 }
117 return true;
118 }
119
120 private:
121 Point _min;
122 Point _max;
123 bool _empty;
124
125 MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:BBox")
126};
127
128template<class Point> BBox<Point> &operator+(const BBox<Point> &b1, const BBox<Point> &b2)
129{
130 Point new_min;
131 Point new_max;
132
133 for (uint i = 0; i < Point::dim(); i++) {
134 new_min[i] = b1.getMin()[i] < b2.getMin()[i] ? b1.getMin()[i] : b2.getMin()[i];
135 new_max[i] = b1.getMax()[i] > b2.getMax()[i] ? b1.getMax()[i] : b2.getMax()[i];
136 }
137
138 return BBox<Point>(new_min, new_max);
139}
140
141} /* namespace Freestyle */
#define BLI_assert(a)
Definition BLI_assert.h:46
unsigned int uint
Read Guarded memory(de)allocation.
BBox(const BBox< T > &b)
Definition BBox.h:33
BBox< Point > & operator+=(const BBox< Point > &b)
Definition BBox.h:86
const Point & getMin() const
Definition BBox.h:67
const Point & getMax() const
Definition BBox.h:72
bool empty() const
Definition BBox.h:62
bool inside(const Point &p)
Definition BBox.h:107
BBox(const T &min_in, const T &max_in)
Definition BBox.h:28
BBox< Point > & operator=(const BBox< Point > &b)
Definition BBox.h:77
void extendToContain(const T &p)
Definition BBox.h:38
void clear()
Definition BBox.h:57
#define T
inherits from class Rep
Definition AppCanvas.cpp:20
BBox< Point > & operator+(const BBox< Point > &b1, const BBox< Point > &b2)
Definition BBox.h:128
i
Definition text_draw.cc:230