2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
#include <limits>
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
namespace
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
using namespace cv;
|
2010-05-12 01:44:00 +08:00
|
|
|
const size_t MAX_STACK_SIZE = 255;
|
|
|
|
const size_t MAX_LEAFS = 8;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
bool checkIfNodeOutsideSphere(const Octree::Node& node, const Point3f& c, float r)
|
|
|
|
{
|
|
|
|
if (node.x_max < (c.x - r) || node.y_max < (c.y - r) || node.z_max < (c.z - r))
|
|
|
|
return true;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ((c.x + r) < node.x_min || (c.y + r) < node.y_min || (c.z + r) < node.z_min)
|
|
|
|
return true;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
bool checkIfNodeInsideSphere(const Octree::Node& node, const Point3f& c, float r)
|
|
|
|
{
|
|
|
|
r *= r;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float d2_xmin = (node.x_min - c.x) * (node.x_min - c.x);
|
|
|
|
float d2_ymin = (node.y_min - c.y) * (node.y_min - c.y);
|
|
|
|
float d2_zmin = (node.z_min - c.z) * (node.z_min - c.z);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmin + d2_ymin + d2_zmin > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float d2_zmax = (node.z_max - c.z) * (node.z_max - c.z);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmin + d2_ymin + d2_zmax > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float d2_ymax = (node.y_max - c.y) * (node.y_max - c.y);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmin + d2_ymax + d2_zmin > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmin + d2_ymax + d2_zmax > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float d2_xmax = (node.x_max - c.x) * (node.x_max - c.x);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmax + d2_ymin + d2_zmin > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmax + d2_ymin + d2_zmax > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmax + d2_ymax + d2_zmin > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (d2_xmax + d2_ymax + d2_zmax > r)
|
|
|
|
return false;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
void fillMinMax(const vector<Point3f>& points, Octree::Node& node)
|
|
|
|
{
|
|
|
|
node.x_max = node.y_max = node.z_max = std::numeric_limits<float>::min();
|
|
|
|
node.x_min = node.y_min = node.z_min = std::numeric_limits<float>::max();
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (size_t i = 0; i < points.size(); ++i)
|
|
|
|
{
|
|
|
|
const Point3f& point = points[i];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (node.x_max < point.x)
|
|
|
|
node.x_max = point.x;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (node.y_max < point.y)
|
|
|
|
node.y_max = point.y;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (node.z_max < point.z)
|
|
|
|
node.z_max = point.z;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (node.x_min > point.x)
|
|
|
|
node.x_min = point.x;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (node.y_min > point.y)
|
|
|
|
node.y_min = point.y;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (node.z_min > point.z)
|
|
|
|
node.z_min = point.z;
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
size_t findSubboxForPoint(const Point3f& point, const Octree::Node& node)
|
|
|
|
{
|
|
|
|
size_t ind_x = point.x < (node.x_max + node.x_min) / 2 ? 0 : 1;
|
|
|
|
size_t ind_y = point.y < (node.y_max + node.y_min) / 2 ? 0 : 1;
|
|
|
|
size_t ind_z = point.z < (node.z_max + node.z_min) / 2 ? 0 : 1;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return (ind_x << 2) + (ind_y << 1) + (ind_z << 0);
|
|
|
|
}
|
|
|
|
void initChildBox(const Octree::Node& parent, size_t boxIndex, Octree::Node& child)
|
|
|
|
{
|
|
|
|
child.x_min = child.x_max = (parent.x_max + parent.x_min) / 2;
|
|
|
|
child.y_min = child.y_max = (parent.y_max + parent.y_min) / 2;
|
|
|
|
child.z_min = child.z_max = (parent.z_max + parent.z_min) / 2;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ((boxIndex >> 0) & 1)
|
|
|
|
child.z_max = parent.z_max;
|
|
|
|
else
|
|
|
|
child.z_min = parent.z_min;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ((boxIndex >> 1) & 1)
|
|
|
|
child.y_max = parent.y_max;
|
|
|
|
else
|
|
|
|
child.y_min = parent.y_min;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ((boxIndex >> 2) & 1)
|
|
|
|
child.x_max = parent.x_max;
|
|
|
|
else
|
|
|
|
child.x_min = parent.x_min;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
|
|
|
}//namespace
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////// Octree //////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
2012-06-15 21:04:17 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
Octree::Octree()
|
|
|
|
{
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
|
|
|
Octree::Octree(const vector<Point3f>& points3d, int maxLevels, int _minPoints)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
buildTree(points3d, maxLevels, _minPoints);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
Octree::~Octree()
|
|
|
|
{
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
void Octree::getPointsWithinSphere(const Point3f& center, float radius, vector<Point3f>& out) const
|
|
|
|
{
|
|
|
|
out.clear();
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (nodes.empty())
|
|
|
|
return;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int stack[MAX_STACK_SIZE];
|
|
|
|
int pos = 0;
|
|
|
|
stack[pos] = 0;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
while (pos >= 0)
|
|
|
|
{
|
|
|
|
const Node& cur = nodes[stack[pos--]];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (checkIfNodeOutsideSphere(cur, center, radius))
|
|
|
|
continue;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (checkIfNodeInsideSphere(cur, center, radius))
|
|
|
|
{
|
|
|
|
size_t sz = out.size();
|
|
|
|
out.resize(sz + cur.end - cur.begin);
|
|
|
|
for (int i = cur.begin; i < cur.end; ++i)
|
|
|
|
out[sz++] = points[i];
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.isLeaf)
|
|
|
|
{
|
|
|
|
double r2 = radius * radius;
|
|
|
|
size_t sz = out.size();
|
|
|
|
out.resize(sz + (cur.end - cur.begin));
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (int i = cur.begin; i < cur.end; ++i)
|
|
|
|
{
|
|
|
|
const Point3f& point = points[i];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
double dx = (point.x - center.x);
|
|
|
|
double dy = (point.y - center.y);
|
|
|
|
double dz = (point.z - center.z);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
double dist2 = dx * dx + dy * dy + dz * dz;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (dist2 < r2)
|
|
|
|
out[sz++] = point;
|
|
|
|
};
|
|
|
|
out.resize(sz);
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[0])
|
|
|
|
stack[++pos] = cur.children[0];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[1])
|
|
|
|
stack[++pos] = cur.children[1];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[2])
|
|
|
|
stack[++pos] = cur.children[2];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[3])
|
|
|
|
stack[++pos] = cur.children[3];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[4])
|
|
|
|
stack[++pos] = cur.children[4];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[5])
|
|
|
|
stack[++pos] = cur.children[5];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[6])
|
|
|
|
stack[++pos] = cur.children[6];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (cur.children[7])
|
|
|
|
stack[++pos] = cur.children[7];
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
|
|
|
void Octree::buildTree(const vector<Point3f>& points3d, int maxLevels, int _minPoints)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
assert((size_t)maxLevels * 8 < MAX_STACK_SIZE);
|
|
|
|
points.resize(points3d.size());
|
|
|
|
std::copy(points3d.begin(), points3d.end(), points.begin());
|
2012-06-15 21:04:17 +08:00
|
|
|
minPoints = _minPoints;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
nodes.clear();
|
|
|
|
nodes.push_back(Node());
|
|
|
|
Node& root = nodes[0];
|
|
|
|
fillMinMax(points, root);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
root.isLeaf = true;
|
|
|
|
root.maxLevels = maxLevels;
|
|
|
|
root.begin = 0;
|
|
|
|
root.end = (int)points.size();
|
|
|
|
for (size_t i = 0; i < MAX_LEAFS; i++)
|
|
|
|
root.children[i] = 0;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
|
|
|
if (maxLevels != 1 && (root.end - root.begin) > _minPoints)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
root.isLeaf = false;
|
|
|
|
buildNext(0);
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
void Octree::buildNext(size_t nodeInd)
|
|
|
|
{
|
|
|
|
size_t size = nodes[nodeInd].end - nodes[nodeInd].begin;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
vector<size_t> boxBorders(MAX_LEAFS+1, 0);
|
|
|
|
vector<size_t> boxIndices(size);
|
|
|
|
vector<Point3f> tempPoints(size);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (int i = nodes[nodeInd].begin, j = 0; i < nodes[nodeInd].end; ++i, ++j)
|
|
|
|
{
|
|
|
|
const Point3f& p = points[i];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
size_t subboxInd = findSubboxForPoint(p, nodes[nodeInd]);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
boxBorders[subboxInd+1]++;
|
|
|
|
boxIndices[j] = subboxInd;
|
|
|
|
tempPoints[j] = p;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (size_t i = 1; i < boxBorders.size(); ++i)
|
|
|
|
boxBorders[i] += boxBorders[i-1];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
vector<size_t> writeInds(boxBorders.begin(), boxBorders.end());
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t boxIndex = boxIndices[i];
|
|
|
|
Point3f& curPoint = tempPoints[i];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
size_t copyTo = nodes[nodeInd].begin + writeInds[boxIndex]++;
|
|
|
|
points[copyTo] = curPoint;
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (size_t i = 0; i < MAX_LEAFS; ++i)
|
|
|
|
{
|
|
|
|
if (boxBorders[i] == boxBorders[i+1])
|
|
|
|
continue;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
nodes.push_back(Node());
|
|
|
|
Node& child = nodes.back();
|
|
|
|
initChildBox(nodes[nodeInd], i, child);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
child.isLeaf = true;
|
|
|
|
child.maxLevels = nodes[nodeInd].maxLevels - 1;
|
|
|
|
child.begin = nodes[nodeInd].begin + (int)boxBorders[i+0];
|
|
|
|
child.end = nodes[nodeInd].begin + (int)boxBorders[i+1];
|
|
|
|
for (size_t k = 0; k < MAX_LEAFS; k++)
|
|
|
|
child.children[k] = 0;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
nodes[nodeInd].children[i] = (int)(nodes.size() - 1);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (child.maxLevels != 1 && (child.end - child.begin) > minPoints)
|
|
|
|
{
|
|
|
|
child.isLeaf = false;
|
|
|
|
buildNext(nodes.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|