trase
BBox.hpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of trase.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
37 
38 #ifndef BBOX_H_
39 #define BBOX_H_
40 
41 #include <algorithm>
42 #include <limits>
43 
44 #include "util/Vector.hpp"
45 
46 namespace trase {
47 
54 template <typename T, int N> struct bbox {
55  using vector_t = Vector<T, N>;
56 
60  vector_t bmin;
61 
65  vector_t bmax;
66 
67  inline bbox()
68  : bmin(vector_t::Constant(std::numeric_limits<T>::max())),
69  bmax(vector_t::Constant(-std::numeric_limits<T>::max())) {}
70 
71  inline explicit bbox(const vector_t &p)
72  : bmin(p), bmax(p + std::numeric_limits<T>::epsilon()) {}
73 
74  inline bbox(const vector_t &min, const vector_t &max)
75  : bmin(min), bmax(max) {}
76 
77  vector_t delta() const { return bmax - bmin; }
78  const vector_t &min() const { return bmin; }
79  const vector_t &max() const { return bmax; }
80 
84  inline bbox &operator+=(const bbox &arg) {
85  for (int i = 0; i < N; ++i) {
86  bmin[i] = std::min(bmin[i], arg.bmin[i]);
87  bmax[i] = std::max(bmax[i], arg.bmax[i]);
88  }
89  return *this;
90  }
91 
95  inline bbox operator+(const bbox &arg) {
96  bbox bounds = *this;
97  bounds += arg;
98  return bounds;
99  }
100 
104  inline bbox &operator+=(const vector_t &arg) {
105  bmin += arg;
106  bmax += arg;
107  return *this;
108  }
109 
113  inline bbox operator+(const vector_t &arg) {
114  bbox bounds = *this;
115  bounds += arg;
116  return bounds;
117  }
118 
122  inline bbox &operator*=(const vector_t &arg) {
123  bmin =
124  static_cast<T>(0.5) *
125  ((bmax * (static_cast<T>(1) - arg) + bmin * (static_cast<T>(1) + arg)));
126  bmax =
127  static_cast<T>(0.5) *
128  ((bmax * (static_cast<T>(1) + arg) + bmin * (static_cast<T>(1) - arg)));
129  return *this;
130  }
131 
135  inline bbox operator*(const vector_t &arg) const {
136  bbox bounds = *this;
137  bounds *= arg;
138  return bounds;
139  }
140 
144  inline bool operator<(const bbox &arg) {
145  bbox bounds;
146  bool within = true;
147  for (int i = 0; i < N; ++i) {
148  within |= bmin[i] >= arg.bmin[i];
149  within |= bmax[i] < arg.bmax[i];
150  }
151  return within;
152  }
153 
157  inline bool operator<=(const bbox &arg) {
158  bbox bounds;
159  bool within = true;
160  for (int i = 0; i < N; ++i) {
161  within |= bmin[i] >= arg.bmin[i];
162  within |= bmax[i] <= arg.bmax[i];
163  }
164  return within;
165  }
166 
170  inline bool is_empty() {
171  for (int i = 0; i < N; ++i) {
172  if (bmax[i] < bmin[i] + 3 * std::numeric_limits<double>::epsilon()) {
173  return true;
174  }
175  }
176  return false;
177  }
178 
188  vector_t to_coords(const bbox<T, N> &other, vector_t point,
189  bool y_inv = true) const {
190  vector_t len_ratio = other.delta() / delta();
191 
192  // Get the relative position and invert y by default (e.g. limits->pixels)
193  vector_t rel_pos = point - bmin;
194  if (y_inv && N > 1) {
195  rel_pos[1] = bmax[1] - point[1];
196  }
197 
198  return other.bmin + rel_pos * len_ratio;
199  }
200 };
201 
209 template <typename T, int N>
210 std::ostream &operator<<(std::ostream &out, const bbox<T, N> &b) {
211  return out << "bbox(" << b.bmin << "<->" << b.bmax << ")";
212 }
213 
214 using bfloat2_t = bbox<float, 2>;
215 using bfloat1_t = bbox<float, 1>;
216 
217 } // namespace trase
218 
219 #endif
Contains the minimum and maximum extents of a hypercube in D dimensional space.
Definition: BBox.hpp:54
vector_t bmin
minimum point in the box (i.e.
Definition: BBox.hpp:60
bbox operator+(const vector_t &arg)
Definition: BBox.hpp:113
bbox & operator+=(const bbox &arg)
Definition: BBox.hpp:84
bbox operator*(const vector_t &arg) const
Definition: BBox.hpp:135
bbox operator+(const bbox &arg)
Definition: BBox.hpp:95
bool is_empty()
Definition: BBox.hpp:170
bbox & operator*=(const vector_t &arg)
Definition: BBox.hpp:122
An N-dimensional vector class.
Definition: Vector.hpp:59
bool operator<(const bbox &arg)
Definition: BBox.hpp:144
vector_t bmax
maximum point in the box (i.e.
Definition: BBox.hpp:65
vector_t to_coords(const bbox< T, N > &other, vector_t point, bool y_inv=true) const
given two bboxes representing the same cube in space, convert from one coordinate system to another ...
Definition: BBox.hpp:188
bool operator<=(const bbox &arg)
Definition: BBox.hpp:157
Definition: Backend.cpp:39
bbox & operator+=(const vector_t &arg)
Definition: BBox.hpp:104