/src/assimp/code/Common/SGSpatialSort.cpp
Line | Count | Source |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2026, assimp team |
7 | | |
8 | | All rights reserved. |
9 | | |
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: |
13 | | |
14 | | * Redistributions of source code must retain the above |
15 | | copyright notice, this list of conditions and the |
16 | | following disclaimer. |
17 | | |
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. |
22 | | |
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. |
27 | | |
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 | | */ |
41 | | |
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 */ |
45 | | |
46 | | #include <assimp/SGSpatialSort.h> |
47 | | |
48 | | using namespace Assimp; |
49 | | |
50 | | // ------------------------------------------------------------------------------------------------ |
51 | 0 | SGSpatialSort::SGSpatialSort() { |
52 | | // define the reference plane. We choose some arbitrary vector away from all basic axes |
53 | | // in the hope that no model spreads all its vertices along this plane. |
54 | 0 | mPlaneNormal.Set( 0.8523f, 0.34321f, 0.5736f); |
55 | 0 | mPlaneNormal.Normalize(); |
56 | 0 | } |
57 | | |
58 | | // ------------------------------------------------------------------------------------------------ |
59 | | void SGSpatialSort::Add(const aiVector3D& vPosition, unsigned int index, |
60 | | unsigned int smoothingGroup) |
61 | 0 | { |
62 | | // store position by index and distance |
63 | 0 | float distance = vPosition * mPlaneNormal; |
64 | 0 | mPositions.emplace_back( index, vPosition, |
65 | 0 | distance, smoothingGroup); |
66 | 0 | } |
67 | | // ------------------------------------------------------------------------------------------------ |
68 | | void SGSpatialSort::Prepare() |
69 | 0 | { |
70 | | // now sort the array ascending by distance. |
71 | 0 | std::sort( this->mPositions.begin(), this->mPositions.end()); |
72 | 0 | } |
73 | | // ------------------------------------------------------------------------------------------------ |
74 | | // Returns an iterator for all positions close to the given position. |
75 | | void SGSpatialSort::FindPositions( const aiVector3D& pPosition, |
76 | | uint32_t pSG, |
77 | | float pRadius, |
78 | | std::vector<unsigned int>& poResults, |
79 | | bool exactMatch /*= false*/) const |
80 | 0 | { |
81 | 0 | float dist = pPosition * mPlaneNormal; |
82 | 0 | float minDist = dist - pRadius, maxDist = dist + pRadius; |
83 | | |
84 | | // clear the array |
85 | 0 | poResults.clear(); |
86 | | |
87 | | // quick check for positions outside the range |
88 | 0 | if( mPositions.empty() ) |
89 | 0 | return; |
90 | 0 | if( maxDist < mPositions.front().mDistance) |
91 | 0 | return; |
92 | 0 | if( minDist > mPositions.back().mDistance) |
93 | 0 | return; |
94 | | |
95 | | // do a binary search for the minimal distance to start the iteration there |
96 | 0 | unsigned int index = (unsigned int)mPositions.size() / 2; |
97 | 0 | unsigned int binaryStepSize = (unsigned int)mPositions.size() / 4; |
98 | 0 | while( binaryStepSize > 1) |
99 | 0 | { |
100 | 0 | if( mPositions[index].mDistance < minDist) |
101 | 0 | index += binaryStepSize; |
102 | 0 | else |
103 | 0 | index -= binaryStepSize; |
104 | |
|
105 | 0 | binaryStepSize /= 2; |
106 | 0 | } |
107 | | |
108 | | // depending on the direction of the last step we need to single step a bit back or forth |
109 | | // to find the actual beginning element of the range |
110 | 0 | while( index > 0 && mPositions[index].mDistance > minDist) |
111 | 0 | index--; |
112 | 0 | while( index < (mPositions.size() - 1) && mPositions[index].mDistance < minDist) |
113 | 0 | index++; |
114 | | |
115 | | // Mow start iterating from there until the first position lays outside of the distance range. |
116 | | // Add all positions inside the distance range within the given radius to the result array |
117 | |
|
118 | 0 | float squareEpsilon = pRadius * pRadius; |
119 | 0 | std::vector<Entry>::const_iterator it = mPositions.begin() + index; |
120 | 0 | std::vector<Entry>::const_iterator end = mPositions.end(); |
121 | |
|
122 | 0 | if (exactMatch) |
123 | 0 | { |
124 | 0 | while( it->mDistance < maxDist) |
125 | 0 | { |
126 | 0 | if((it->mPosition - pPosition).SquareLength() < squareEpsilon && it->mSmoothGroups == pSG) |
127 | 0 | { |
128 | 0 | poResults.push_back( it->mIndex); |
129 | 0 | } |
130 | 0 | ++it; |
131 | 0 | if( end == it )break; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | else |
135 | 0 | { |
136 | | // if the given smoothing group is 0, we'll return all surrounding vertices |
137 | 0 | if (!pSG) |
138 | 0 | { |
139 | 0 | while( it->mDistance < maxDist) |
140 | 0 | { |
141 | 0 | if((it->mPosition - pPosition).SquareLength() < squareEpsilon) |
142 | 0 | poResults.push_back( it->mIndex); |
143 | 0 | ++it; |
144 | 0 | if( end == it)break; |
145 | 0 | } |
146 | 0 | } |
147 | 0 | else while( it->mDistance < maxDist) |
148 | 0 | { |
149 | 0 | if((it->mPosition - pPosition).SquareLength() < squareEpsilon && |
150 | 0 | (it->mSmoothGroups & pSG || !it->mSmoothGroups)) |
151 | 0 | { |
152 | 0 | poResults.push_back( it->mIndex); |
153 | 0 | } |
154 | 0 | ++it; |
155 | 0 | if( end == it)break; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | |