Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:30:10

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