1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42/** Small helper classes to optimize finding vertices close to a given location
43 */
44#ifndef AI_D3DSSPATIALSORT_H_INC
45#define AI_D3DSSPATIALSORT_H_INC
46
47#include <assimp/types.h>
48#include <vector>
49#include <stdint.h>
50
51namespace Assimp {
52
53// ----------------------------------------------------------------------------------
54/** Specialized version of SpatialSort to support smoothing groups
55 * This is used in by the 3DS, ASE and LWO loaders. 3DS and ASE share their
56 * normal computation code in SmoothingGroups.inl, the LWO loader has its own
57 * implementation to handle all details of its file format correctly.
58 */
59// ----------------------------------------------------------------------------------
60class SGSpatialSort
61{
62public:
63
64 SGSpatialSort();
65
66 // -------------------------------------------------------------------
67 /** Construction from a given face array, handling smoothing groups
68 * properly
69 */
70 explicit SGSpatialSort(const std::vector<aiVector3D>& vPositions);
71
72 // -------------------------------------------------------------------
73 /** Add a vertex to the spatial sort
74 * @param vPosition Vertex position to be added
75 * @param index Index of the vrtex
76 * @param smoothingGroup SmoothingGroup for this vertex
77 */
78 void Add(const aiVector3D& vPosition, unsigned int index,
79 unsigned int smoothingGroup);
80
81 // -------------------------------------------------------------------
82 /** Prepare the spatial sorter for use. This step runs in O(logn)
83 */
84 void Prepare();
85
86 /** Destructor */
87 ~SGSpatialSort();
88
89 // -------------------------------------------------------------------
90 /** Returns an iterator for all positions close to the given position.
91 * @param pPosition The position to look for vertices.
92 * @param pSG Only included vertices with at least one shared smooth group
93 * @param pRadius Maximal distance from the position a vertex may have
94 * to be counted in.
95 * @param poResults The container to store the indices of the found
96 * positions. Will be emptied by the call so it may contain anything.
97 * @param exactMatch Specifies whether smoothing groups are bit masks
98 * (false) or integral values (true). In the latter case, a vertex
99 * cannot belong to more than one smoothing group.
100 * @return An iterator to iterate over all vertices in the given area.
101 */
102 // -------------------------------------------------------------------
103 void FindPositions( const aiVector3D& pPosition, uint32_t pSG,
104 float pRadius, std::vector<unsigned int>& poResults,
105 bool exactMatch = false) const;
106
107protected:
108 /** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
109 aiVector3D mPlaneNormal;
110
111 // -------------------------------------------------------------------
112 /** An entry in a spatially sorted position array. Consists of a
113 * vertex index, its position and its precalculated distance from
114 * the reference plane */
115 // -------------------------------------------------------------------
116 struct Entry
117 {
118 unsigned int mIndex; ///< The vertex referred by this entry
119 aiVector3D mPosition; ///< Position
120 uint32_t mSmoothGroups;
121 float mDistance; ///< Distance of this vertex to the sorting plane
122
123 Entry() { /** intentionally not initialized.*/ }
124 Entry( unsigned int pIndex, const aiVector3D& pPosition, float pDistance,uint32_t pSG)
125 :
126 mIndex( pIndex),
127 mPosition( pPosition),
128 mSmoothGroups (pSG),
129 mDistance( pDistance)
130 { }
131
132 bool operator < (const Entry& e) const { return mDistance < e.mDistance; }
133 };
134
135 // all positions, sorted by distance to the sorting plane
136 std::vector<Entry> mPositions;
137};
138
139} // end of namespace Assimp
140
141#endif // AI_SPATIALSORT_H_INC
142