FCL  0.6.0
Flexible Collision Library
morton-inl.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * Copyright (c) 2016, Toyota Research Institute
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Open Source Robotics Foundation nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
39 #ifndef FCL_MORTON_INL_H
40 #define FCL_MORTON_INL_H
41 
42 #include "fcl/broadphase/detail/morton.h"
43 
44 namespace fcl {
46 namespace detail {
47 
48 //==============================================================================
49 extern template
50 uint32 quantize(double x, uint32 n);
51 
52 //==============================================================================
53 extern template
54 struct morton_functor<double, uint32>;
55 
56 //==============================================================================
57 extern template
58 struct morton_functor<double, uint64>;
59 
60 //==============================================================================
61 template <typename S>
62 uint32 quantize(S x, uint32 n)
63 {
64  return std::max(std::min((uint32)(x * (S)n), uint32(n-1)), uint32(0));
65 }
66 
67 //==============================================================================
68 template<typename S>
69 morton_functor<S, uint32>::morton_functor(const AABB<S>& bbox)
70  : base(bbox.min_),
71  inv(1.0 / (bbox.max_[0] - bbox.min_[0]),
72  1.0 / (bbox.max_[1] - bbox.min_[1]),
73  1.0 / (bbox.max_[2] - bbox.min_[2]))
74 {
75  // Do nothing
76 }
77 
78 //==============================================================================
79 template<typename S>
80 uint32 morton_functor<S, uint32>::operator()(const Vector3<S>& point) const
81 {
82  uint32 x = detail::quantize((point[0] - base[0]) * inv[0], 1024u);
83  uint32 y = detail::quantize((point[1] - base[1]) * inv[1], 1024u);
84  uint32 z = detail::quantize((point[2] - base[2]) * inv[2], 1024u);
85 
86  return detail::morton_code(x, y, z);
87 }
88 
89 //==============================================================================
90 template<typename S>
91 morton_functor<S, uint64>::morton_functor(const AABB<S>& bbox)
92  : base(bbox.min_),
93  inv(1.0 / (bbox.max_[0] - bbox.min_[0]),
94  1.0 / (bbox.max_[1] - bbox.min_[1]),
95  1.0 / (bbox.max_[2] - bbox.min_[2]))
96 {
97  // Do nothing
98 }
99 
100 //==============================================================================
101 template<typename S>
102 uint64 morton_functor<S, uint64>::operator()(const Vector3<S>& point) const
103 {
104  uint32 x = detail::quantize((point[0] - base[0]) * inv[0], 1u << 20);
105  uint32 y = detail::quantize((point[1] - base[1]) * inv[1], 1u << 20);
106  uint32 z = detail::quantize((point[2] - base[2]) * inv[2], 1u << 20);
107 
108  return detail::morton_code60(x, y, z);
109 }
110 
111 //==============================================================================
112 template<typename S>
113 constexpr size_t morton_functor<S, uint64>::bits()
114 {
115  return 60;
116 }
117 
118 //==============================================================================
119 template<typename S>
120 constexpr size_t morton_functor<S, uint32>::bits()
121 {
122  return 30;
123 }
124 
125 //==============================================================================
126 template<typename S, size_t N>
127 morton_functor<S, std::bitset<N>>::morton_functor(const AABB<S>& bbox)
128  : base(bbox.min_),
129  inv(1.0 / (bbox.max_[0] - bbox.min_[0]),
130  1.0 / (bbox.max_[1] - bbox.min_[1]),
131  1.0 / (bbox.max_[2] - bbox.min_[2]))
132 {
133  // Do nothing
134 }
135 
136 //==============================================================================
137 template<typename S, size_t N>
138 std::bitset<N> morton_functor<S, std::bitset<N>>::operator()(
139  const Vector3<S>& point) const
140 {
141  S x = (point[0] - base[0]) * inv[0];
142  S y = (point[1] - base[1]) * inv[1];
143  S z = (point[2] - base[2]) * inv[2];
144  int start_bit = bits() - 1;
145  std::bitset<N> bset;
146 
147  x *= 2;
148  y *= 2;
149  z *= 2;
150 
151  for(size_t i = 0; i < bits()/3; ++i)
152  {
153  bset[start_bit--] = ((z < 1) ? 0 : 1);
154  bset[start_bit--] = ((y < 1) ? 0 : 1);
155  bset[start_bit--] = ((x < 1) ? 0 : 1);
156  x = ((x >= 1) ? 2*(x-1) : 2*x);
157  y = ((y >= 1) ? 2*(y-1) : 2*y);
158  z = ((z >= 1) ? 2*(z-1) : 2*z);
159  }
160 
161  return bset;
162 }
163 
164 //==============================================================================
165 template<typename S, size_t N>
166 constexpr size_t morton_functor<S, std::bitset<N>>::bits()
167 {
168  return N;
169 }
170 
171 } // namespace detail
173 } // namespace fcl
174 
175 #endif
Main namespace.
Definition: broadphase_bruteforce-inl.h:45