18 #ifndef ION_MATH_RANGE_H_
19 #define ION_MATH_RANGE_H_
41 operator T()
const {
return t; }
51 static const T Zero() {
return T(0); }
54 template <
typename U>
friend std::istream&
operator>>(
69 template <
int Dimension,
typename T>
99 template <
int Dimension,
typename T>
113 Set(min_point, max_point);
144 min_point_ = min_point;
145 max_point_ = max_point;
156 min_point_ = min_point;
157 max_point_ = min_point + size;
169 min_point_ + ((max_point_ - min_point_) / static_cast<T>(2));
195 const bool m0_empty = m0.
IsEmpty();
196 const bool m1_empty = m1.
IsEmpty();
197 if (m0_empty || m1_empty)
198 return m0_empty == m1_empty;
200 return m0.min_point_ == m1.min_point_ && m0.max_point_ == m1.max_point_;
209 void ExtendMinByPoint(
const Endpoint& p);
212 void ExtendMaxByPoint(
const Endpoint& p);
219 template <
int Dimension,
typename T>
220 std::ostream& operator<<(std::ostream& out, const Range<Dimension, T>& r) {
222 return out <<
"R[EMPTY]";
224 return out <<
"R[" << r.GetMinPoint() <<
", " << r.GetMaxPoint() <<
"]";
228 template <
int Dimension,
typename T>
237 if (in >> min_point >> base::GetExpectedChar<','> >> max_point
239 r.
Set(min_point, max_point);
253 template <
int Dimension,
typename T>
255 for (
int i = 0; i < Dimension; ++i) {
257 min_point_[i] =
static_cast<T>(1);
258 max_point_[i] =
static_cast<T>(0);
263 template <
int Dimension,
typename T>
265 for (
int i = 0; i < Dimension; ++i) {
266 if (min_point_[i] > max_point_[i])
272 template <
int Dimension,
typename T>
276 if (0 <= i && i < Dimension)
277 min_point_[i] =
value;
280 template <
int Dimension,
typename T>
284 if (0 <= i && i < Dimension)
285 max_point_[i] =
value;
288 template <
int Dimension,
typename T>
291 min_point_ = max_point_ = p;
298 template <
int Dimension,
typename T>
312 template <
int Dimension,
typename T>
314 for (
int i = 0; i < Dimension; ++i) {
315 if (p[i] < min_point_[i] || p[i] > max_point_[i])
321 template <
int Dimension,
typename T>
326 template <
int Dimension,
typename T>
330 for (
int i = 0; i < Dimension; ++i) {
331 if (min_point_[i] > r_max_point[i])
return false;
332 if (max_point_[i] < r_min_point[i])
return false;
338 template <
int Dimension,
typename T>
340 for (
int i = 0; i < Dimension; ++i) {
341 min_point_[i] = std::min(p[i], min_point_[i]);
346 template <
int Dimension,
typename T>
347 void Range<Dimension, T>::ExtendMaxByPoint(
const Endpoint& p) {
348 for (
int i = 0; i < Dimension; ++i) {
349 max_point_[i] = std::max(p[i], max_point_[i]);
403 max_point_[0] = -1.0;
404 max_point_[1] = -1.0;
405 max_point_[2] = -1.0;
411 min_point_[0] > max_point_[0] ||
412 min_point_[1] > max_point_[1] ||
413 min_point_[2] > max_point_[2];
419 p[0] >= min_point_[0] && p[0] <= max_point_[0] &&
420 p[1] >= min_point_[1] && p[1] <= max_point_[1] &&
421 p[2] >= min_point_[2] && p[2] <= max_point_[2];
425 inline void Range3d::ExtendMinByPoint(
const Point3d& p) {
426 min_point_[0] = std::min(p[0], min_point_[0]);
427 min_point_[1] = std::min(p[1], min_point_[1]);
428 min_point_[2] = std::min(p[2], min_point_[2]);
432 inline void Range3d::ExtendMaxByPoint(
const Point3d& p) {
433 max_point_[0] = std::max(p[0], max_point_[0]);
434 max_point_[1] = std::max(p[1], max_point_[1]);
435 max_point_[2] = std::max(p[2], max_point_[2]);
439 inline void Range3f::MakeEmpty() {
440 min_point_[0] = 1.0f;
441 min_point_[1] = 1.0f;
442 min_point_[2] = 1.0f;
443 max_point_[0] = -1.0f;
444 max_point_[1] = -1.0f;
445 max_point_[2] = -1.0f;
449 inline bool Range3f::IsEmpty()
const {
451 min_point_[0] > max_point_[0] ||
452 min_point_[1] > max_point_[1] ||
453 min_point_[2] > max_point_[2];
457 inline bool Range3f::ContainsPoint(
const Point3f& p)
const {
459 p[0] >= min_point_[0] && p[0] <= max_point_[0] &&
460 p[1] >= min_point_[1] && p[1] <= max_point_[1] &&
461 p[2] >= min_point_[2] && p[2] <= max_point_[2];
465 inline void Range3f::ExtendMinByPoint(
const Point3f& p) {
466 min_point_[0] = std::min(p[0], min_point_[0]);
467 min_point_[1] = std::min(p[1], min_point_[1]);
468 min_point_[2] = std::min(p[2], min_point_[2]);
472 inline void Range3f::ExtendMaxByPoint(
const Point3f& p) {
473 max_point_[0] = std::max(p[0], max_point_[0]);
474 max_point_[1] = std::max(p[1], max_point_[1]);
475 max_point_[2] = std::max(p[2], max_point_[2]);
481 #endif // ION_MATH_RANGE_H_
Range< 4, double > Range4d
Range()
The default constructor defines an empty Range.
static const Range BuildWithSize(const Endpoint &min_point, const Size &size)
Convenience function that returns a Range from a minimum point and the Range size.
const Endpoint & GetMinPoint() const
Returns the minimum or maximum endpoint.
void SetMaxComponent(int i, T value)
Range1TWrapper< T, 1 > Size
Range< 3, int32 > Range3i
Range< 4, uint32 > Range4ui
Range(const Endpoint &min_point, const Endpoint &max_point)
Constructor that takes the minimum and maximum points.
const Size GetSize() const
Returns the size of the range, or the zero vector if the Range is empty.
The Range class defines an N-dimensional interval defined by minimum and maximum N-dimensional endpoi...
void MakeEmpty()
Sets the Range to be empty.
void ExtendByPoint(const Endpoint &p)
Extends the Range if necessary to contain the given point.
static const Vector Zero()
Returns a Vector containing all zeroes.
Range< 1, int32 > Range1i
Range< 2, double > Range2d
bool IntersectsRange(const Range &r) const
Returns true if this Range overlaps the given Range, i.e., there exists at least one point contained ...
Range< 4, float > Range4f
void SetWithSize(const Endpoint &min_point, const Size &size)
Sets the Range from the minimum point and Range size.
#define DCHECK_GT(val1, val2)
bool ContainsRange(const Range &r) const
Returns true if this Range fully contains the given Range by testing both min/max points of input Ran...
Range1TWrapper< T, 0 > Endpoint
friend std::istream & operator>>(std::istream &in, Range1TWrapper< U, 0 > &w)
Allows reading wrapped value from a stream.
Range< 2, float > Range2f
Range< 2, uint16 > Range2ui16
Range< 2, int32 > Range2i
Range< 1, uint32 > Range1ui
const Endpoint GetCenter() const
Returns the point at the center of the range, or the origin if the range is empty.
Range< 3, uint16 > Range3ui16
Range< 3, uint32 > Range3ui
Range< 1, uint16 > Range1ui16
The RangeBase class makes it possible to treat Ranges with Dimension=1 specially. ...
Range< 3, uint8 > Range3ui8
T & operator[](int index)
Range< 3, float > Range3f
const T & operator[](int index) const
Range< 1, int16 > Range1i16
Range< 4, int8 > Range4i8
Range< 3, double > Range3d
This struct allows the Endpoint and Size types in a Range<1, T> to be treated like those of higher-di...
Range< 4, uint8 > Range4ui8
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
bool IsEmpty() const
Returns true if the Range is empty, meaning that the minimum value is strictly greater than the maxim...
void SetMaxPoint(const Endpoint &p)
Copyright 2016 Google Inc.
Range< 3, int8 > Range3i8
void SetMinPoint(const Endpoint &p)
Modifies the minimum or maximum endpoint, or both.
Range< 2, uint8 > Range2ui8
RangeBase< Dimension, T > BaseType
std::istream & GetExpectedString(std::istream &in, const std::string &expected)
Attempts to read a string from the stream and returns the stream.
Range< 2, int8 > Range2i8
#define DCHECK_EQ(val1, val2)
Range< 3, int16 > Range3i16
Range< 4, uint16 > Range4ui16
bool ContainsPoint(const Endpoint &p) const
Returns true if the Range contains the given point.
#define DCHECK_LE(val1, val2)
Range1TWrapper(const T &t_in)
friend bool operator!=(const Range &m0, const Range &m1)
void Set(const Endpoint &min_point, const Endpoint &max_point)
static const Point Zero()
Returns a Point containing all zeroes.
Range< 1, int8 > Range1i8
Dimension- and type-specific typedefs.
Range< 4, int16 > Range4i16
Range< 4, int32 > Range4i
Range< 2, uint32 > Range2ui
Range< 1, double > Range1d
std::istream & operator>>(std::istream &in, Angle< T > &a)
const Endpoint & GetMaxPoint() const
void SetMinComponent(int i, T value)
Modifies a single element for the minimum or maximum endpoint.
Vector< Dimension, T > Size
Convenience typedef for the size of a Range.
BaseType::Endpoint Endpoint
void ExtendByRange(const Range &r)
Extends the Range if necessary to contain the given Range.
Range< 2, int16 > Range2i16
Range< 1, uint8 > Range1ui8
friend bool operator==(const Range &m0, const Range &m1)
Exact equality and inequality comparisons.
Point< Dimension, T > Endpoint
Convenience typedef for a Range endpoint type.
Range< 1, float > Range1f