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