vrshoot

view libs/assimp/SGSpatialSort.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 /** @file Implementation of the helper class to quickly find
43 vertices close to a given position. Special implementation for
44 the 3ds loader handling smooth groups correctly */
46 #include "AssimpPCH.h"
47 #include "SGSpatialSort.h"
49 using namespace Assimp;
52 // ------------------------------------------------------------------------------------------------
53 SGSpatialSort::SGSpatialSort()
54 {
55 // define the reference plane. We choose some arbitrary vector away from all basic axises
56 // in the hope that no model spreads all its vertices along this plane.
57 mPlaneNormal.Set( 0.8523f, 0.34321f, 0.5736f);
58 mPlaneNormal.Normalize();
59 }
60 // ------------------------------------------------------------------------------------------------
61 // Destructor
62 SGSpatialSort::~SGSpatialSort()
63 {
64 // nothing to do here, everything destructs automatically
65 }
66 // ------------------------------------------------------------------------------------------------
67 void SGSpatialSort::Add(const aiVector3D& vPosition, unsigned int index,
68 unsigned int smoothingGroup)
69 {
70 // store position by index and distance
71 float distance = vPosition * mPlaneNormal;
72 mPositions.push_back( Entry( index, vPosition,
73 distance, smoothingGroup));
74 }
75 // ------------------------------------------------------------------------------------------------
76 void SGSpatialSort::Prepare()
77 {
78 // now sort the array ascending by distance.
79 std::sort( this->mPositions.begin(), this->mPositions.end());
80 }
81 // ------------------------------------------------------------------------------------------------
82 // Returns an iterator for all positions close to the given position.
83 void SGSpatialSort::FindPositions( const aiVector3D& pPosition,
84 uint32_t pSG,
85 float pRadius,
86 std::vector<unsigned int>& poResults,
87 bool exactMatch /*= false*/) const
88 {
89 float dist = pPosition * mPlaneNormal;
90 float minDist = dist - pRadius, maxDist = dist + pRadius;
92 // clear the array in this strange fashion because a simple clear() would also deallocate
93 // the array which we want to avoid
94 poResults.erase( poResults.begin(), poResults.end());
96 // quick check for positions outside the range
97 if( mPositions.size() == 0)
98 return;
99 if( maxDist < mPositions.front().mDistance)
100 return;
101 if( minDist > mPositions.back().mDistance)
102 return;
104 // do a binary search for the minimal distance to start the iteration there
105 unsigned int index = (unsigned int)mPositions.size() / 2;
106 unsigned int binaryStepSize = (unsigned int)mPositions.size() / 4;
107 while( binaryStepSize > 1)
108 {
109 if( mPositions[index].mDistance < minDist)
110 index += binaryStepSize;
111 else
112 index -= binaryStepSize;
114 binaryStepSize /= 2;
115 }
117 // depending on the direction of the last step we need to single step a bit back or forth
118 // to find the actual beginning element of the range
119 while( index > 0 && mPositions[index].mDistance > minDist)
120 index--;
121 while( index < (mPositions.size() - 1) && mPositions[index].mDistance < minDist)
122 index++;
124 // Mow start iterating from there until the first position lays outside of the distance range.
125 // Add all positions inside the distance range within the given radius to the result aray
127 float squareEpsilon = pRadius * pRadius;
128 std::vector<Entry>::const_iterator it = mPositions.begin() + index;
129 std::vector<Entry>::const_iterator end = mPositions.end();
131 if (exactMatch)
132 {
133 while( it->mDistance < maxDist)
134 {
135 if((it->mPosition - pPosition).SquareLength() < squareEpsilon && it->mSmoothGroups == pSG)
136 {
137 poResults.push_back( it->mIndex);
138 }
139 ++it;
140 if( end == it )break;
141 }
142 }
143 else
144 {
145 // if the given smoothing group is 0, we'll return all surrounding vertices
146 if (!pSG)
147 {
148 while( it->mDistance < maxDist)
149 {
150 if((it->mPosition - pPosition).SquareLength() < squareEpsilon)
151 poResults.push_back( it->mIndex);
152 ++it;
153 if( end == it)break;
154 }
155 }
156 else while( it->mDistance < maxDist)
157 {
158 if((it->mPosition - pPosition).SquareLength() < squareEpsilon &&
159 (it->mSmoothGroups & pSG || !it->mSmoothGroups))
160 {
161 poResults.push_back( it->mIndex);
162 }
163 ++it;
164 if( end == it)break;
165 }
166 }
167 }