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.
43 #include "precomp.hpp"
49 const size_t MAX_STACK_SIZE = 255;
50 const size_t MAX_LEAFS = 8;
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& parent, size_t boxIndex, Octree::Node& child)
143 child.x_min = child.x_max = (parent.x_max + parent.x_min) / 2;
144 child.y_min = child.y_max = (parent.y_max + parent.y_min) / 2;
145 child.z_min = child.z_max = (parent.z_max + parent.z_min) / 2;
147 if ((boxIndex >> 0) & 1)
148 child.z_max = parent.z_max;
150 child.z_min = parent.z_min;
152 if ((boxIndex >> 1) & 1)
153 child.y_max = parent.y_max;
155 child.y_min = parent.y_min;
157 if ((boxIndex >> 2) & 1)
158 child.x_max = parent.x_max;
160 child.x_min = parent.x_min;
165 ////////////////////////////////////////////////////////////////////////////////////////
166 /////////////////////////// Octree //////////////////////////////////////
167 ////////////////////////////////////////////////////////////////////////////////////////
174 Octree::Octree(const vector<Point3f>& points3d, int maxLevels, int _minPoints)
176 buildTree(points3d, maxLevels, _minPoints);
183 void Octree::getPointsWithinSphere(const Point3f& center, float radius, vector<Point3f>& out) const
190 int stack[MAX_STACK_SIZE];
196 const Node& cur = nodes[stack[pos--]];
198 if (checkIfNodeOutsideSphere(cur, center, radius))
201 if (checkIfNodeInsideSphere(cur, center, radius))
203 size_t sz = out.size();
204 out.resize(sz + cur.end - cur.begin);
205 for (int i = cur.begin; i < cur.end; ++i)
206 out[sz++] = points[i];
212 double r2 = radius * radius;
213 size_t sz = out.size();
214 out.resize(sz + (cur.end - cur.begin));
216 for (int i = cur.begin; i < cur.end; ++i)
218 const Point3f& point = points[i];
220 double dx = (point.x - center.x);
221 double dy = (point.y - center.y);
222 double dz = (point.z - center.z);
224 double dist2 = dx * dx + dy * dy + dz * dz;
234 stack[++pos] = cur.children[0];
237 stack[++pos] = cur.children[1];
240 stack[++pos] = cur.children[2];
243 stack[++pos] = cur.children[3];
246 stack[++pos] = cur.children[4];
249 stack[++pos] = cur.children[5];
252 stack[++pos] = cur.children[6];
255 stack[++pos] = cur.children[7];
259 void Octree::buildTree(const vector<Point3f>& points3d, int maxLevels, int _minPoints)
261 assert((size_t)maxLevels * 8 < MAX_STACK_SIZE);
262 points.resize(points3d.size());
263 std::copy(points3d.begin(), points3d.end(), points.begin());
264 minPoints = _minPoints;
267 nodes.push_back(Node());
268 Node& root = nodes[0];
269 fillMinMax(points, root);
272 root.maxLevels = maxLevels;
274 root.end = (int)points.size();
275 for (size_t i = 0; i < MAX_LEAFS; i++)
276 root.children[i] = 0;
278 if (maxLevels != 1 && (root.end - root.begin) > _minPoints)
285 void Octree::buildNext(size_t nodeInd)
287 size_t size = nodes[nodeInd].end - nodes[nodeInd].begin;
289 vector<size_t> boxBorders(MAX_LEAFS+1, 0);
290 vector<size_t> boxIndices(size);
291 vector<Point3f> tempPoints(size);
293 for (int i = nodes[nodeInd].begin, j = 0; i < nodes[nodeInd].end; ++i, ++j)
295 const Point3f& p = points[i];
297 size_t subboxInd = findSubboxForPoint(p, nodes[nodeInd]);
299 boxBorders[subboxInd+1]++;
300 boxIndices[j] = subboxInd;
304 for (size_t i = 1; i < boxBorders.size(); ++i)
305 boxBorders[i] += boxBorders[i-1];
307 vector<size_t> writeInds(boxBorders.begin(), boxBorders.end());
309 for (size_t i = 0; i < size; ++i)
311 size_t boxIndex = boxIndices[i];
312 Point3f& curPoint = tempPoints[i];
314 size_t copyTo = nodes[nodeInd].begin + writeInds[boxIndex]++;
315 points[copyTo] = curPoint;
318 for (size_t i = 0; i < MAX_LEAFS; ++i)
320 if (boxBorders[i] == boxBorders[i+1])
323 nodes.push_back(Node());
324 Node& child = nodes.back();
325 initChildBox(nodes[nodeInd], i, child);
328 child.maxLevels = nodes[nodeInd].maxLevels - 1;
329 child.begin = nodes[nodeInd].begin + (int)boxBorders[i+0];
330 child.end = nodes[nodeInd].begin + (int)boxBorders[i+1];
331 for (size_t k = 0; k < MAX_LEAFS; k++)
332 child.children[k] = 0;
334 nodes[nodeInd].children[i] = (int)(nodes.size() - 1);
336 if (child.maxLevels != 1 && (child.end - child.begin) > minPoints)
338 child.isLeaf = false;
339 buildNext(nodes.size() - 1);