27#ifndef EASY3D_CORE_GENERIC_BOX_H
28#define EASY3D_CORE_GENERIC_BOX_H
33#include <easy3d/core/vec.h>
44 template<
int DIM,
typename FT>
56 : min_(std::numeric_limits<FT>::
max())
57 , max_(-std::numeric_limits<FT>::
max()) {
87 for (
int i = 0; i < DIM; ++i) {
88 if (max_[i] >= min_[i] - std::numeric_limits<FT>::epsilon())
98 min_ =
Point(std::numeric_limits<FT>::max());
99 max_ =
Point(-std::numeric_limits<FT>::max());
130 assert(axis >= 0 && axis < DIM);
142 assert(axis >= 0 && axis < DIM);
154 assert(axis >= 0 && axis < DIM);
156 return max_[axis] - min_[axis];
165 FT max_value = max_[0] - min_[0];
166 for (
int i = 1; i < DIM; ++i)
167 max_value = std::max(max_value, max_[i] - min_[i]);
176 FT min_value = max_[0] - min_[0];
177 for (
int i = 1; i < DIM; ++i)
178 min_value = std::min(min_value, max_[i] - min_[i]);
187 unsigned int axis = 0;
188 FT max_value = max_[0] - min_[0];
189 for (
int i = 1; i < DIM; ++i) {
190 FT
range = max_[i] - min_[i];
191 if (
range > max_value) {
204 unsigned int axis = 0;
205 FT min_value = max_[0] - min_[0];
206 for (
int i = 1; i < DIM; ++i) {
207 FT
range = max_[i] - min_[i];
208 if (
range < min_value) {
222 return FT(0.5) * (min_ + max_);
239 for (
int i = 0; i < DIM; ++i) {
240 FT d = max_[i] - min_[i];
243 return std::sqrt(sqr_len);
263 return FT(2.0) * (ext[0] * ext[1] + ext[0] * ext[2] + ext[1] * ext[2]);
265 return ext[0] * ext[1];
267 std::cerr <<
"surface_area() is for 2D and 3D boxes" << std::endl;
278 for (
int i = 0; i < DIM; ++i) {
279 min_[i] = std::min(min_[i], p[i]);
280 max_[i] = std::max(max_[i], p[i]);
330 for (
int i = 0; i < DIM; ++i) {
331 if (p[i] <= min_[i] || p[i] >= max_[i])
352 for (
int i = 0; i < DIM; ++i) {
372 template<
int DIM,
typename FT>
389 template<
int DIM,
typename FT>
402 template<
int DIM,
typename FT>
408 template<
int DIM,
typename FT>
411 template<
int DIM,
typename FT>
412 GenericBox<DIM, FT>
box_intersection(GenericBox<DIM, FT>
const& a, GenericBox<DIM, FT>
const& b) {
414 return GenericBox<DIM, FT>(
comp_max(a.min_point(), b.min_point()),
comp_min(a.max_point(), b.max_point()));
416 return GenericBox<DIM, FT>();
GenericBox represents the bounding box of shapes.
Definition box.h:45
bool contains(Point const &p) const
Check if this box contains a point p.
Definition box.h:329
Point center() const
Return the center of the box.
Definition box.h:220
GenericBox()
Construct a box uninitialized (which is invalid).
Definition box.h:55
FT max_coord(unsigned int axis) const
Return a component of the coordinates of the max corner.
Definition box.h:141
bool contains(thisclass const &b) const
Check if this box contains another box b.
Definition box.h:342
const Point & min_point() const
Return the coordinates of the min corner (const version).
Definition box.h:106
FT range(unsigned int axis) const
Return the range of the box along the axis.
Definition box.h:153
Vector diagonal_vector() const
Return the diagonal vector of the box.
Definition box.h:230
FT radius() const
Return the radius of the box (i.e., half of its diagonal length).
Definition box.h:252
Point & min_point()
Return the coordinates of the min corner.
Definition box.h:111
FT min_coord(unsigned int axis) const
Return a component of the coordinates of the min corner.
Definition box.h:129
FT max_range() const
Return the max range.
Definition box.h:164
FT diagonal_length() const
Return the length of the diagonal of the box.
Definition box.h:236
FT min_range() const
Return the min range.
Definition box.h:175
GenericBox< DIM, FT > thisclass
This class.
Definition box.h:49
thisclass & operator+=(const thisclass &b)
Modify this box by adding another box b.
Definition box.h:316
void grow(const thisclass &b)
Add a box to this box. This will compute its new extent.
Definition box.h:292
unsigned int min_range_axis() const
Return the axis index of the min range of the box.
Definition box.h:203
const Point & max_point() const
Return the coordinates of the max corner (const version).
Definition box.h:117
bool is_valid() const
Check if the box is valid.
Definition box.h:86
FT surface_area() const
Return the surface area of the box.
Definition box.h:260
GenericBox(const Point &c, FT r)
Construct a box from its center and radius.
Definition box.h:75
thisclass operator+(const thisclass &b) const
Return the bounding box of 'this' and another box b.
Definition box.h:304
Vec< DIM, FT > Vector
Vector type.
Definition box.h:48
void clear()
Invalidate the box.
Definition box.h:97
Vec< DIM, FT > Point
Point type.
Definition box.h:47
unsigned int max_range_axis() const
Return the axis index of the max range of the box.
Definition box.h:186
GenericBox(const Point &pmin, const Point &pmax)
Construct a box from its diagonal corners.
Definition box.h:65
bool intersects(thisclass const &b) const
Check if this box intersects another box b.
Definition box.h:351
void grow(const Point &p)
Add a point to this box. This will compute its new extent.
Definition box.h:276
Point & max_point()
Return the coordinates of the max corner.
Definition box.h:122
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
thisclass & normalize()
Normalizes this vector.
Definition vec.h:135
GenericBox< DIM, FT > box_union(GenericBox< DIM, FT > const &a, GenericBox< DIM, FT > const &b)
Compute the union of two boxes.
Definition box.h:390
GenericBox< DIM, FT > box_intersection(GenericBox< DIM, FT > const &a, GenericBox< DIM, FT > const &b)
Compute the intersection of two boxes.
Definition box.h:403
Definition collider.cpp:182
Vec< N, T > comp_min(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise minimum vector.
Definition vec.h:1027
Vec< N, T > comp_max(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise maximum vector.
Definition vec.h:1036
FT max()
Function returning maximum representable value for a given type.
bool has_nan(const GenericBox< DIM, FT > &box)
Check if the representation of a box has NaN.
Definition box.h:373