2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-12-22 18:25:01 +00:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup freestyle
|
|
|
|
|
* \brief A class to hold a bounding box
|
2012-12-22 18:25:01 +00:00
|
|
|
*/
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2013-11-29 14:50:59 -05:00
|
|
|
#include <algorithm>
|
2020-04-21 12:39:12 +02:00
|
|
|
#include <stdlib.h>
|
2013-08-24 17:15:59 +00:00
|
|
|
|
2013-08-24 11:42:00 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2024-11-13 13:39:49 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
2013-05-13 22:58:27 +00:00
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
namespace Freestyle {
|
|
|
|
|
|
2008-04-30 15:41:54 +00:00
|
|
|
template<class Point> class BBox {
|
2012-12-22 18:25:01 +00:00
|
|
|
public:
|
|
|
|
|
inline BBox()
|
|
|
|
|
{
|
|
|
|
|
_empty = true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
template<class T> inline BBox(const T &min_in, const T &max_in) : _min(min_in), _max(max_in)
|
|
|
|
|
{
|
|
|
|
|
_empty = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
template<class T> inline BBox(const BBox<T> &b) : _min(b.getMin()), _max(b.getMax())
|
|
|
|
|
{
|
|
|
|
|
_empty = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
template<class T> inline void extendToContain(const T &p)
|
|
|
|
|
{
|
|
|
|
|
if (_empty) {
|
|
|
|
|
_min = p;
|
|
|
|
|
_max = p;
|
|
|
|
|
_empty = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-25 12:51:50 +10:00
|
|
|
for (uint i = 0; i < Point::dim(); i++) {
|
2019-05-31 22:51:19 +10:00
|
|
|
if (p[i] < _min[i]) {
|
2012-12-22 18:25:01 +00:00
|
|
|
_min[i] = p[i];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
else if (p[i] > _max[i]) {
|
2012-12-22 18:25:01 +00:00
|
|
|
_max[i] = p[i];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2012-12-22 18:25:01 +00:00
|
|
|
}
|
|
|
|
|
_empty = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline void clear()
|
|
|
|
|
{
|
|
|
|
|
_empty = true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline bool empty() const
|
|
|
|
|
{
|
|
|
|
|
return _empty;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline const Point &getMin() const
|
|
|
|
|
{
|
|
|
|
|
return _min;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline const Point &getMax() const
|
|
|
|
|
{
|
|
|
|
|
return _max;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline BBox<Point> &operator=(const BBox<Point> &b)
|
|
|
|
|
{
|
2013-08-24 11:42:00 +00:00
|
|
|
BLI_assert(!b.empty());
|
2012-12-22 18:25:01 +00:00
|
|
|
_min = b.getMin();
|
|
|
|
|
_max = b.getMax();
|
|
|
|
|
_empty = false;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline BBox<Point> &operator+=(const BBox<Point> &b)
|
|
|
|
|
{
|
2013-08-24 11:42:00 +00:00
|
|
|
BLI_assert(!b.empty());
|
2012-12-22 18:25:01 +00:00
|
|
|
if (_empty) {
|
|
|
|
|
_min = b.getMin();
|
|
|
|
|
_max = b.getMax();
|
|
|
|
|
_empty = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-07-25 12:51:50 +10:00
|
|
|
for (uint i = 0; i < Point::dim(); i++) {
|
2019-05-31 22:51:19 +10:00
|
|
|
if (b.getMin()[i] < _min[i]) {
|
2012-12-22 18:25:01 +00:00
|
|
|
_min[i] = b.getMin()[i];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
if (b.getMax()[i] > _max[i]) {
|
2012-12-22 18:25:01 +00:00
|
|
|
_max[i] = b.getMax()[i];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-12-22 18:25:01 +00:00
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
inline bool inside(const Point &p)
|
|
|
|
|
{
|
2019-05-31 22:51:19 +10:00
|
|
|
if (empty()) {
|
2012-12-22 18:25:01 +00:00
|
|
|
return false;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2023-07-25 12:51:50 +10:00
|
|
|
for (uint i = 0; i < Point::dim(); i++) {
|
2019-05-31 22:51:19 +10:00
|
|
|
if ((_min[i] > p[i]) || (_max[i] < p[i])) {
|
2012-12-22 18:25:01 +00:00
|
|
|
return false;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2012-12-22 18:25:01 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-04-30 15:41:54 +00:00
|
|
|
private:
|
2012-12-22 18:25:01 +00:00
|
|
|
Point _min;
|
|
|
|
|
Point _max;
|
|
|
|
|
bool _empty;
|
2013-05-13 22:58:27 +00:00
|
|
|
|
|
|
|
|
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:BBox")
|
2008-04-30 15:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class Point> BBox<Point> &operator+(const BBox<Point> &b1, const BBox<Point> &b2)
|
|
|
|
|
{
|
2012-12-22 18:25:01 +00:00
|
|
|
Point new_min;
|
|
|
|
|
Point new_max;
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2023-07-25 12:51:50 +10:00
|
|
|
for (uint i = 0; i < Point::dim(); i++) {
|
2012-12-22 18:25:01 +00:00
|
|
|
new_min[i] = b1.getMin()[i] < b2.getMin()[i] ? b1.getMin()[i] : b2.getMin()[i];
|
|
|
|
|
new_max[i] = b1.getMax()[i] > b2.getMax()[i] ? b1.getMax()[i] : b2.getMax()[i];
|
|
|
|
|
}
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2012-12-22 18:25:01 +00:00
|
|
|
return BBox<Point>(new_min, new_max);
|
2008-04-30 15:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
} /* namespace Freestyle */
|