Fermat
bbox_inline.h
1 /*
2  * Copyright (c) 2010-2018, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 namespace cugar {
29 
30 template <typename Vector_t>
32  m_min( field_traits<value_type>::max() ),
33  m_max( field_traits<value_type>::min() )
34 {
35 }
36 template <typename Vector_t>
37 Bbox<Vector_t>::Bbox(const Vector_t& v) :
38  m_min( v ),
39  m_max( v )
40 {
41 }
42 template <typename Vector_t>
43 Bbox<Vector_t>::Bbox(const Vector_t& v1, const Vector_t& v2) :
44  m_min( v1 ),
45  m_max( v2 )
46 {
47 }
48 template <typename Vector_t>
50 {
51  for (uint32 i = 0; i < m_min.dimension(); i++)
52  {
53  m_min[i] = ::cugar::min( bb1[0][i], bb2[0][i] );
54  m_max[i] = ::cugar::max( bb1[1][i], bb2[1][i] );
55  }
56 }
57 template <typename Vector_t>
59  m_min( bb.m_min ),
60  m_max( bb.m_max )
61 {
62 }
63 
64 template <typename Vector_t>
66  const Vector_t& v)
67 {
68  for (uint32 i = 0; i < m_min.dimension(); i++)
69  {
70  m_min[i] = ::cugar::min( m_min[i], v[i] );
71  m_max[i] = ::cugar::max( m_max[i], v[i] );
72  }
73 }
74 template <typename Vector_t>
76  const Bbox& bbox)
77 {
78  for (uint32 i = 0; i < m_min.dimension(); i++)
79  {
80  m_min[i] = ::cugar::min( m_min[i], bbox.m_min[i] );
81  m_max[i] = ::cugar::max( m_max[i], bbox.m_max[i] );
82  }
83 }
84 template <typename Vector_t>
86 {
87  for (uint32 i = 0; i < m_min.dimension(); i++)
88  {
89  m_min[i] = field_traits<value_type>::max();
90  m_max[i] = field_traits<value_type>::min();
91  }
92 }
93 
94 template <typename Vector_t>
96 {
97  m_min = bb.m_min;
98  m_max = bb.m_max;
99  return *this;
100 }
101 
102 template <typename Vector_t>
103 uint32 largest_axis(const Bbox<Vector_t>& bbox)
104 {
105  typedef typename Vector_t::value_type value_type;
106 
107  const Vector_t edge( bbox[1] - bbox[0] );
108  uint32 axis = 0;
109  value_type v = edge[0];
110 
111  for (uint32 i = 1; i < edge.dimension(); i++)
112  {
113  if (v < edge[i])
114  {
115  v = edge[i];
116  axis = i;
117  }
118  }
119  return axis;
120 }
121 
125 template <typename Vector_t>
126 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Vector_t extents(const Bbox<Vector_t>& bbox)
127 {
128  return bbox[1] - bbox[0];
129 }
130 
131 // compute the area of a 2d bbox
132 //
133 // \param bbox bbox object
134 float area(const Bbox2f& bbox)
135 {
136  const Vector2f edge = bbox[1] - bbox[0];
137  return edge[0] * edge[1];
138 }
139 
140 // compute the area of a 3d bbox
141 //
142 // \param bbox bbox object
143 float area(const Bbox3f& bbox)
144 {
145  const Vector3f edge = bbox[1] - bbox[0];
146  return edge[0] * edge[1] + edge[2] * (edge[0] + edge[1]);
147 }
148 
149 // point-in-bbox inclusion predicate
150 //
151 // \param bbox bbox object
152 // \param p point to test for inclusion
153 template <typename Vector_t>
154 bool contains(const Bbox<Vector_t>& bbox, const Vector_t& p)
155 {
156  for (uint32 i = 0; i < p.dimension(); ++i)
157  {
158  if (p[i] < bbox[0][i] ||
159  p[i] > bbox[1][i])
160  return false;
161  }
162  return true;
163 }
164 
165 // bbox-in-bbox inclusion predicate
166 //
167 // \param bbox bbox object
168 // \param c candidate to test for inclusion
169 template <typename Vector_t>
170 bool contains(const Bbox<Vector_t>& bbox, const Bbox<Vector_t>& c)
171 {
172  for (uint32 i = 0; i < c[0].dimension(); ++i)
173  {
174  if (c[0][i] < bbox[0][i] ||
175  c[1][i] > bbox[1][i])
176  return false;
177  }
178  return true;
179 }
180 
181 // point-to-bbox squared distance
182 //
183 // \param bbox bbox object
184 // \param p point
185 template <typename Vector_t>
186 float sq_distance(const Bbox<Vector_t>& bbox, const Vector_t& p)
187 {
188  float r = 0.0f;
189  for (uint32 i = 0; i < p.dimension(); ++i)
190  {
191  const float dist = cugar::max( bbox[0][i] - p[i], 0.0f ) +
192  cugar::max( p[i] - bbox[1][i], 0.0f );
193 
194  r += dist*dist;
195  }
196  return r;
197 }
198 
199 } // namespace cugar
float sq_distance(const Bbox< Vector_t > &bbox, const Vector_t &p)
Definition: bbox_inline.h:186
Vector_t m_max
max corner
Definition: bbox.h:126
Definition: bbox.h:59
bool contains(const Bbox< Vector_t > &bbox, const Bbox< Vector_t > &c)
Definition: bbox_inline.h:170
Definition: numbers.h:1020
Definition: vector.h:54
Vector_t m_min
min corner
Definition: bbox.h:125
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
uint32 largest_axis(const Bbox< Vector_t > &bbox)
Definition: bbox_inline.h:103
CUGAR_HOST_DEVICE float area(const Bbox2f &bbox)
Definition: bbox_inline.h:134
CUGAR_HOST CUGAR_DEVICE Bbox()
Definition: bbox_inline.h:31
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Vector_t extents(const Bbox< Vector_t > &bbox)
Definition: bbox_inline.h:126