1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
50 const size_t MAX_STACK_SIZE = 255;
52 bool checkIfNodeOutsideSphere(const Octree::Node& node, const Point3f& c, float r)
54 if (node.x_max < (c.x - r) || node.y_max < (c.y - r) || node.z_max < (c.z - r) )
57 if ( (c.x + r) < node.x_min || (c.y + r) < node.y_min || (c.z + r) < node.z_min )
63 bool checkIfNodeInsideSphere(const Octree::Node& node, const Point3f& c, float r)
67 float d2_xmin = (node.x_min - c.x) * (node.x_min - c.x);
68 float d2_ymin = (node.y_min - c.y) * (node.y_min - c.y);
69 float d2_zmin = (node.z_min - c.z) * (node.z_min - c.z);
71 if (d2_xmin + d2_ymin + d2_zmin > r)
74 float d2_zmax = (node.z_max - c.z) * (node.z_max - c.z);
76 if (d2_xmin + d2_ymin + d2_zmax > r)
79 float d2_ymax = (node.y_max - c.y) * (node.y_max - c.y);
81 if (d2_xmin + d2_ymax + d2_zmin > r)
84 if (d2_xmin + d2_ymax + d2_zmax > r)
87 float d2_xmax = (node.x_max - c.x) * (node.x_max - c.x);
89 if (d2_xmax + d2_ymin + d2_zmin > r)
92 if (d2_xmax + d2_ymin + d2_zmax > r)
95 if (d2_xmax + d2_ymax + d2_zmin > r)
98 if (d2_xmax + d2_ymax + d2_zmax > r)
104 void fillMinMax(const vector<Point3f>& points, Octree::Node& node)
106 node.x_max = node.y_max = node.z_max = std::numeric_limits<float>::min();
107 node.x_min = node.y_min = node.z_min = std::numeric_limits<float>::max();
109 for(size_t i = 0; i < points.size(); ++i)
111 const Point3f& point = points[i];
113 if (node.x_max < point.x)
114 node.x_max = point.x;
116 if (node.y_max < point.y)
117 node.y_max = point.y;
119 if (node.z_max < point.z)
120 node.z_max = point.z;
122 if (node.x_min > point.x)
123 node.x_min = point.x;
125 if (node.y_min > point.y)
126 node.y_min = point.y;
128 if (node.z_min > point.z)
129 node.z_min = point.z;
133 size_t findSubboxForPoint(const Point3f& point, const Octree::Node& node)
135 size_t ind_x = point.x < (node.x_max + node.x_min) / 2 ? 0 : 1;
136 size_t ind_y = point.y < (node.y_max + node.y_min) / 2 ? 0 : 1;
137 size_t ind_z = point.z < (node.z_max + node.z_min) / 2 ? 0 : 1;
139 return (ind_x << 2) + (ind_y << 1) + (ind_z << 0);
141 void initChildBox(const Octree::Node& perent, size_t boxIndex, Octree::Node& child)
143 child.x_min = child.x_max = (perent.x_max + perent.x_min) / 2;
144 child.y_min = child.y_max = (perent.y_max + perent.y_min) / 2;
145 child.z_min = child.z_max = (perent.z_max + perent.z_min) / 2;
147 if ((boxIndex >> 0) & 1)
148 child.z_max = perent.z_max;
150 child.z_min = perent.z_min;
152 if ((boxIndex >> 1) & 1)
153 child.y_max = perent.y_max;
155 child.y_min = perent.y_min;
157 if ((boxIndex >> 2) & 1)
158 child.x_max = perent.x_max;
160 child.x_min = perent.x_min;
163 ////////////////////////////////////////////////////////////////////////////////////////
164 /////////////////////////// Octree //////////////////////////////////////
165 ////////////////////////////////////////////////////////////////////////////////////////
171 Octree::Octree( const vector<Point3f>& points3d, int maxLevels, int minPoints )
173 buildTree(points3d, maxLevels, minPoints);
180 void Octree::getPointsWithinSphere( const Point3f& center, float radius, vector<Point3f>& out ) const
187 int stack[MAX_STACK_SIZE];
193 const Node& cur = nodes[stack[pos--]];
195 if (checkIfNodeOutsideSphere(cur, center, radius))
198 if(checkIfNodeInsideSphere(cur, center, radius))
200 size_t sz = out.size();
201 out.resize(sz + cur.end - cur.begin);
202 for(int i = cur.begin; i < cur.end; ++i)
203 out[sz++] = points[i];;
209 double r2 = radius * radius;
210 size_t sz = out.size();
211 out.resize(sz + (cur.end - cur.begin));
213 for(int i = cur.begin; i < cur.end; ++i)
215 const Point3f& point = points[i];
217 double dx = (point.x - center.x);
218 double dy = (point.y - center.y);
219 double dz = (point.z - center.z);
221 double dist2 = dx*dx + dy*dy + dz*dz;
231 stack[++pos] = cur.children[0];
234 stack[++pos] = cur.children[1];
237 stack[++pos] = cur.children[2];
240 stack[++pos] = cur.children[3];
243 stack[++pos] = cur.children[4];
246 stack[++pos] = cur.children[5];
249 stack[++pos] = cur.children[6];
252 stack[++pos] = cur.children[7];
256 void Octree::buildTree( const vector<Point3f>& points3d, int maxLevels, int minPoints)
258 assert( (size_t)maxLevels * 8 < MAX_STACK_SIZE );
259 points.resize(points3d.size());
260 std::copy(points3d.begin(), points3d.end(), points.begin());
261 this->minPoints = minPoints;
264 nodes.push_back(Node());
265 Node& root = nodes[0];
266 fillMinMax(points, root);
269 root.maxLevels = maxLevels;
271 root.end = (int)points.size();
272 for( int i = 0; i < 8; i++ )
273 root.children[i] = 0;
275 if (maxLevels != 1 && (root.end - root.begin) > minPoints)
282 void Octree::buildNext(size_t node_ind)
284 size_t size = nodes[node_ind].end - nodes[node_ind].begin;
286 vector<size_t> boxBorders(9, 0);
287 vector<size_t> boxIndeces(size);
288 vector<Point3f> tempPoints(size);
290 for(int i = nodes[node_ind].begin, j = 0; i < nodes[node_ind].end; ++i, ++j)
292 const Point3f& p = points[i];
294 size_t subbox_ind = findSubboxForPoint(p, nodes[node_ind]);
296 boxBorders[subbox_ind+1]++;
297 boxIndeces[j] = subbox_ind;
301 for(size_t i = 1; i < boxBorders.size(); ++i)
302 boxBorders[i] += boxBorders[i-1];
304 vector<size_t> writeInds(boxBorders.begin(), boxBorders.end());
306 for(size_t i = 0; i < size; ++i)
308 size_t boxIndex = boxIndeces[i];
309 Point3f& curPoint = tempPoints[i];
311 size_t copyTo = nodes[node_ind].begin + writeInds[boxIndex]++;
312 points[copyTo] = curPoint;
315 for(size_t i = 0; i < 8; ++i)
317 if (boxBorders[i] == boxBorders[i+1])
320 nodes.push_back(Node());
321 Node& child = nodes.back();
322 initChildBox(nodes[node_ind], i, child);
325 child.maxLevels = nodes[node_ind].maxLevels - 1;
326 child.begin = nodes[node_ind].begin + (int)boxBorders[i+0];
327 child.end = nodes[node_ind].begin + (int)boxBorders[i+1];
328 for( int k = 0; k < 8; k++ )
329 child.children[k] = 0;
331 nodes[node_ind].children[i] = (int)(nodes.size() - 1);
333 if (child.maxLevels != 1 && (child.end - child.begin) > minPoints )
335 child.isLeaf = false;
336 buildNext(nodes.size() - 1);