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