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