Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-07 09:36:39

0001 /*
0002 Open Asset Import Library (assimp)
0003 ----------------------------------------------------------------------
0004 
0005 Copyright (c) 2006-2025, assimp team
0006 
0007 All rights reserved.
0008 
0009 Redistribution and use of this software in source and binary forms,
0010 with or without modification, are permitted provided that the
0011 following conditions are met:
0012 
0013 * Redistributions of source code must retain the above
0014   copyright notice, this list of conditions and the
0015   following disclaimer.
0016 
0017 * Redistributions in binary form must reproduce the above
0018   copyright notice, this list of conditions and the
0019   following disclaimer in the documentation and/or other
0020   materials provided with the distribution.
0021 
0022 * Neither the name of the assimp team, nor the names of its
0023   contributors may be used to endorse or promote products
0024   derived from this software without specific prior
0025   written permission of the assimp team.
0026 
0027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0028 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0029 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0030 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0031 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0032 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0033 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0034 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0035 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0037 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0038 
0039 ----------------------------------------------------------------------
0040 */
0041 
0042 /** Small helper classes to optimise finding vertizes close to a given location */
0043 #pragma once
0044 #ifndef AI_SPATIALSORT_H_INC
0045 #define AI_SPATIALSORT_H_INC
0046 
0047 #ifdef __GNUC__
0048 #pragma GCC system_header
0049 #endif
0050 
0051 #include <assimp/types.h>
0052 #include <vector>
0053 #include <limits>
0054 
0055 namespace Assimp {
0056 
0057 // ------------------------------------------------------------------------------------------------
0058 /** A little helper class to quickly find all vertices in the epsilon environment of a given
0059  * position. Construct an instance with an array of positions. The class stores the given positions
0060  * by their indices and sorts them by their distance to an arbitrary chosen plane.
0061  * You can then query the instance for all vertices close to a given position in an average O(log n)
0062  * time, with O(n) worst case complexity when all vertices lay on the plane. The plane is chosen
0063  * so that it avoids common planes in usual data sets. */
0064 // ------------------------------------------------------------------------------------------------
0065 class ASSIMP_API SpatialSort {
0066 public:
0067     SpatialSort();
0068 
0069     // ------------------------------------------------------------------------------------
0070     /** Constructs a spatially sorted representation from the given position array.
0071      * Supply the positions in its layout in memory, the class will only refer to them
0072      * by index.
0073      * @param pPositions Pointer to the first position vector of the array.
0074      * @param pNumPositions Number of vectors to expect in that array.
0075      * @param pElementOffset Offset in bytes from the beginning of one vector in memory
0076      *   to the beginning of the next vector. */
0077     SpatialSort(const aiVector3D *pPositions, unsigned int pNumPositions,
0078             unsigned int pElementOffset);
0079 
0080     /** Destructor */
0081     ~SpatialSort() = default;
0082 
0083     // ------------------------------------------------------------------------------------
0084     /** Sets the input data for the SpatialSort. This replaces existing data, if any.
0085      *  The new data receives new indices in ascending order.
0086      *
0087      * @param pPositions Pointer to the first position vector of the array.
0088      * @param pNumPositions Number of vectors to expect in that array.
0089      * @param pElementOffset Offset in bytes from the beginning of one vector in memory
0090      *   to the beginning of the next vector.
0091      * @param pFinalize Specifies whether the SpatialSort's internal representation
0092      *   is finalized after the new data has been added. Finalization is
0093      *   required in order to use #FindPosition() or #GenerateMappingTable().
0094      *   If you don't finalize yet, you can use #Append() to add data from
0095      *   other sources.*/
0096     void Fill(const aiVector3D *pPositions, unsigned int pNumPositions,
0097             unsigned int pElementOffset,
0098             bool pFinalize = true);
0099 
0100     // ------------------------------------------------------------------------------------
0101     /** Same as #Fill(), except the method appends to existing data in the #SpatialSort. */
0102     void Append(const aiVector3D *pPositions, unsigned int pNumPositions,
0103             unsigned int pElementOffset,
0104             bool pFinalize = true);
0105 
0106     // ------------------------------------------------------------------------------------
0107     /** Finalize the spatial hash data structure. This can be useful after
0108      *  multiple calls to #Append() with the pFinalize parameter set to false.
0109      *  This is finally required before one of #FindPositions() and #GenerateMappingTable()
0110      *  can be called to query the spatial sort.*/
0111     void Finalize();
0112 
0113     // ------------------------------------------------------------------------------------
0114     /** Returns an iterator for all positions close to the given position.
0115      * @param pPosition The position to look for vertices.
0116      * @param pRadius Maximal distance from the position a vertex may have to be counted in.
0117      * @param poResults The container to store the indices of the found positions.
0118      *   Will be emptied by the call so it may contain anything.
0119      * @return An iterator to iterate over all vertices in the given area.*/
0120     void FindPositions(const aiVector3D &pPosition, ai_real pRadius,
0121             std::vector<unsigned int> &poResults) const;
0122 
0123     // ------------------------------------------------------------------------------------
0124     /** Fills an array with indices of all positions identical to the given position. In
0125      *  opposite to FindPositions(), not an epsilon is used but a (very low) tolerance of
0126      *  four floating-point units.
0127      * @param pPosition The position to look for vertices.
0128      * @param poResults The container to store the indices of the found positions.
0129      *   Will be emptied by the call so it may contain anything.*/
0130     void FindIdenticalPositions(const aiVector3D &pPosition,
0131             std::vector<unsigned int> &poResults) const;
0132 
0133     // ------------------------------------------------------------------------------------
0134     /** Compute a table that maps each vertex ID referring to a spatially close
0135      *  enough position to the same output ID. Output IDs are assigned in ascending order
0136      *  from 0...n.
0137      * @param fill Will be filled with numPositions entries.
0138      * @param pRadius Maximal distance from the position a vertex may have to
0139      *   be counted in.
0140      *  @return Number of unique vertices (n).  */
0141     unsigned int GenerateMappingTable(std::vector<unsigned int> &fill,
0142             ai_real pRadius) const;
0143 
0144 protected:
0145     /** Return the distance to the sorting plane. */
0146     ai_real CalculateDistance(const aiVector3D &pPosition) const;
0147 
0148 protected:
0149     /** Normal of the sorting plane, normalized.
0150      */
0151     aiVector3D mPlaneNormal;
0152 
0153     /** The centroid of the positions, which is used as a point on the sorting plane
0154      * when calculating distance. This value is calculated in Finalize.
0155     */
0156     aiVector3D mCentroid;
0157 
0158     /** An entry in a spatially sorted position array. Consists of a vertex index,
0159      * its position and its pre-calculated distance from the reference plane */
0160     struct Entry {
0161         unsigned int mIndex; ///< The vertex referred by this entry
0162         aiVector3D mPosition; ///< Position
0163         /// Distance of this vertex to the sorting plane. This is set by Finalize.
0164         ai_real mDistance;
0165 
0166         Entry() AI_NO_EXCEPT
0167                 : mIndex(std::numeric_limits<unsigned int>::max()),
0168                   mPosition(),
0169                   mDistance(std::numeric_limits<ai_real>::max()) {
0170             // empty
0171         }
0172         Entry(unsigned int pIndex, const aiVector3D &pPosition) :
0173                 mIndex(pIndex), mPosition(pPosition), mDistance(std::numeric_limits<ai_real>::max()) {
0174             // empty
0175         }
0176 
0177         bool operator<(const Entry &e) const { return mDistance < e.mDistance; }
0178     };
0179 
0180     // all positions, sorted by distance to the sorting plane
0181     std::vector<Entry> mPositions;
0182 
0183     /// false until the Finalize method is called.
0184     bool mFinalized;
0185 };
0186 
0187 } // end of namespace Assimp
0188 
0189 #endif // AI_SPATIALSORT_H_INC