File: closest.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,060 kB
  • ctags: 33,549
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (154 lines) | stat: -rw-r--r-- 5,308 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

/****************************************************************************
History

$Log: not supported by cvs2svn $
Revision 1.1  2005/09/26 18:33:16  m_di_benedetto
First Commit.


****************************************************************************/

#ifndef __VCGLIB_AABBBINARYTREE_CLOSEST_H
#define __VCGLIB_AABBBINARYTREE_CLOSEST_H

// stl headers
#include <limits>
#include <vector>

// vcg headers
#include <vcg/math/base.h>
#include <vcg/space/index/aabb_binary_tree/base.h>
#include <wrap/utils.h>

/***************************************************************************/

namespace vcg {

template <class TREETYPE>
class AABBBinaryTreeClosest {
public:
	typedef AABBBinaryTreeClosest<TREETYPE> ClassType;
	typedef TREETYPE TreeType;
	typedef typename TreeType::ScalarType ScalarType;
	typedef typename TreeType::CoordType CoordType;
	typedef typename TreeType::NodeType NodeType;
	typedef typename TreeType::ObjPtr ObjPtr;

	template <class OBJPOINTDISTANCEFUNCT>
	static inline ObjPtr Closest(TreeType & tree, OBJPOINTDISTANCEFUNCT & getPointDistance, const CoordType & p, const ScalarType & maxDist, ScalarType & minDist, CoordType & q) {
		typedef OBJPOINTDISTANCEFUNCT ObjPointDistanceFunct;
		typedef std::vector<NodeType *> NodePtrVector;
		typedef typename NodePtrVector::const_iterator NodePtrVector_ci;

		NodeType * pRoot = tree.pRoot;

		if (pRoot == 0) {
			return (0);
		}

		NodePtrVector clist1;
		NodePtrVector clist2;
		NodePtrVector leaves;

		NodePtrVector * candidates = &clist1;
		NodePtrVector * newCandidates = &clist2;

		clist1.reserve(tree.pObjects.size());
		clist2.reserve(tree.pObjects.size());
		leaves.reserve(tree.pObjects.size());

		clist1.resize(0);
		clist2.resize(0);
		leaves.resize(0);

		ScalarType minMaxDist = maxDist * maxDist;

		candidates->push_back(pRoot);

		while (!candidates->empty()) {
			newCandidates->resize(0);

			for (NodePtrVector_ci bv=candidates->begin(); bv!=candidates->end(); ++bv) {
				const CoordType dc = Abs(p - (*bv)->boxCenter);
				const ScalarType maxDist = (dc + (*bv)->boxHalfDims).SquaredNorm();
				(*bv)->ScalarValue() = LowClampToZero(dc - (*bv)->boxHalfDims).SquaredNorm();
				if (maxDist < minMaxDist) {
					minMaxDist = maxDist;
				}
			}

			for (NodePtrVector_ci ci=candidates->begin(); ci!=candidates->end(); ++ci) {
				if ((*ci)->ScalarValue() < minMaxDist) {
					if ((*ci)->IsLeaf()) {
						leaves.push_back(*ci);
					}
					else {
						if ((*ci)->children[0] != 0) {
							newCandidates->push_back((*ci)->children[0]);
						}
						if ((*ci)->children[1] != 0) {
							newCandidates->push_back((*ci)->children[1]);
						}
					}
				}
			}
			NodePtrVector * cSwap = candidates;
			candidates = newCandidates;
			newCandidates = cSwap;
		}

		clist1.clear();
		clist2.clear();

		ObjPtr closestObject = 0;
		CoordType closestPoint;
		ScalarType closestDist = math::Sqrt(minMaxDist) + std::numeric_limits<ScalarType>::epsilon();
		ScalarType closestDistSq = closestDist * closestDist;


		for (NodePtrVector_ci ci=leaves.begin(); ci!=leaves.end(); ++ci) {
			if ((*ci)->ScalarValue() < closestDistSq) {
				for (typename TreeType::ObjPtrVectorConstIterator si=(*ci)->oBegin; si!=(*ci)->oEnd; ++si) {
					if (getPointDistance(*(*si), p, closestDist, closestPoint)) {
						closestDistSq = closestDist * closestDist;
						closestObject = (*si);
            q = closestPoint;
            minDist = closestDist;
					}
				}
			}
		}

		leaves.clear();

		return (closestObject);
	}

};

} // end namespace vcg

#endif // #ifndef __VCGLIB_AABBBINARYTREE_CLOSEST_H