assimp/include/assimp/SpatialSort.h

191 lines
8.6 KiB
C
Raw Normal View History

/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
2022-01-10 20:13:43 +00:00
Copyright (c) 2006-2022, assimp team
2018-01-28 18:42:05 +00:00
2017-05-09 17:57:36 +00:00
All rights reserved.
2015-05-19 03:52:10 +00:00
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
2015-05-19 03:52:10 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2015-05-19 03:52:10 +00:00
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2015-05-19 03:52:10 +00:00
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
/** Small helper classes to optimise finding vertizes close to a given location */
2019-10-11 11:27:36 +00:00
#pragma once
#ifndef AI_SPATIALSORT_H_INC
#define AI_SPATIALSORT_H_INC
2019-10-11 11:27:36 +00:00
#ifdef __GNUC__
#pragma GCC system_header
2019-10-11 11:27:36 +00:00
#endif
2016-06-06 20:04:29 +00:00
#include <assimp/types.h>
#include <vector>
#include <limits>
namespace Assimp {
// ------------------------------------------------------------------------------------------------
/** A little helper class to quickly find all vertices in the epsilon environment of a given
* position. Construct an instance with an array of positions. The class stores the given positions
* by their indices and sorts them by their distance to an arbitrary chosen plane.
2015-05-19 03:52:10 +00:00
* You can then query the instance for all vertices close to a given position in an average O(log n)
* time, with O(n) worst case complexity when all vertices lay on the plane. The plane is chosen
* so that it avoids common planes in usual data sets. */
// ------------------------------------------------------------------------------------------------
class ASSIMP_API SpatialSort {
public:
2015-05-19 03:57:13 +00:00
SpatialSort();
// ------------------------------------------------------------------------------------
/** Constructs a spatially sorted representation from the given position array.
* Supply the positions in its layout in memory, the class will only refer to them
* by index.
* @param pPositions Pointer to the first position vector of the array.
* @param pNumPositions Number of vectors to expect in that array.
* @param pElementOffset Offset in bytes from the beginning of one vector in memory
* to the beginning of the next vector. */
SpatialSort(const aiVector3D *pPositions, unsigned int pNumPositions,
unsigned int pElementOffset);
2015-05-19 03:57:13 +00:00
/** Destructor */
~SpatialSort();
2015-05-19 03:57:13 +00:00
// ------------------------------------------------------------------------------------
/** Sets the input data for the SpatialSort. This replaces existing data, if any.
* The new data receives new indices in ascending order.
*
* @param pPositions Pointer to the first position vector of the array.
* @param pNumPositions Number of vectors to expect in that array.
* @param pElementOffset Offset in bytes from the beginning of one vector in memory
* to the beginning of the next vector.
* @param pFinalize Specifies whether the SpatialSort's internal representation
* is finalized after the new data has been added. Finalization is
* required in order to use #FindPosition() or #GenerateMappingTable().
* If you don't finalize yet, you can use #Append() to add data from
* other sources.*/
void Fill(const aiVector3D *pPositions, unsigned int pNumPositions,
unsigned int pElementOffset,
bool pFinalize = true);
2015-05-19 03:57:13 +00:00
// ------------------------------------------------------------------------------------
/** Same as #Fill(), except the method appends to existing data in the #SpatialSort. */
void Append(const aiVector3D *pPositions, unsigned int pNumPositions,
unsigned int pElementOffset,
bool pFinalize = true);
2015-05-19 03:57:13 +00:00
// ------------------------------------------------------------------------------------
/** Finalize the spatial hash data structure. This can be useful after
* multiple calls to #Append() with the pFinalize parameter set to false.
* This is finally required before one of #FindPositions() and #GenerateMappingTable()
* can be called to query the spatial sort.*/
void Finalize();
// ------------------------------------------------------------------------------------
/** Returns an iterator for all positions close to the given position.
* @param pPosition The position to look for vertices.
* @param pRadius Maximal distance from the position a vertex may have to be counted in.
* @param poResults The container to store the indices of the found positions.
* Will be emptied by the call so it may contain anything.
* @return An iterator to iterate over all vertices in the given area.*/
void FindPositions(const aiVector3D &pPosition, ai_real pRadius,
std::vector<unsigned int> &poResults) const;
2015-05-19 03:57:13 +00:00
// ------------------------------------------------------------------------------------
2016-04-03 00:38:00 +00:00
/** Fills an array with indices of all positions identical to the given position. In
2015-05-19 03:57:13 +00:00
* opposite to FindPositions(), not an epsilon is used but a (very low) tolerance of
* four floating-point units.
* @param pPosition The position to look for vertices.
* @param poResults The container to store the indices of the found positions.
* Will be emptied by the call so it may contain anything.*/
void FindIdenticalPositions(const aiVector3D &pPosition,
std::vector<unsigned int> &poResults) const;
2015-05-19 03:57:13 +00:00
// ------------------------------------------------------------------------------------
/** Compute a table that maps each vertex ID referring to a spatially close
* enough position to the same output ID. Output IDs are assigned in ascending order
* from 0...n.
* @param fill Will be filled with numPositions entries.
* @param pRadius Maximal distance from the position a vertex may have to
* be counted in.
* @return Number of unique vertices (n). */
unsigned int GenerateMappingTable(std::vector<unsigned int> &fill,
ai_real pRadius) const;
protected:
2021-10-21 07:39:44 +00:00
/** Return the distance to the sorting plane. */
ai_real CalculateDistance(const aiVector3D &pPosition) const;
protected:
/** Normal of the sorting plane, normalized.
*/
2015-05-19 03:57:13 +00:00
aiVector3D mPlaneNormal;
2021-10-21 07:39:44 +00:00
/** The centroid of the positions, which is used as a point on the sorting plane
* when calculating distance. This value is calculated in Finalize.
*/
aiVector3D mCentroid;
2015-05-19 03:57:13 +00:00
/** An entry in a spatially sorted position array. Consists of a vertex index,
* its position and its pre-calculated distance from the reference plane */
struct Entry {
2015-05-19 03:57:13 +00:00
unsigned int mIndex; ///< The vertex referred by this entry
aiVector3D mPosition; ///< Position
/// Distance of this vertex to the sorting plane. This is set by Finalize.
2023-01-16 08:12:35 +00:00
ai_real mDistance;
2015-05-19 03:57:13 +00:00
2018-09-21 14:25:27 +00:00
Entry() AI_NO_EXCEPT
: mIndex(std::numeric_limits<unsigned int>::max()),
mPosition(),
mDistance(std::numeric_limits<ai_real>::max()) {
// empty
}
Entry(unsigned int pIndex, const aiVector3D &pPosition) :
2021-10-21 07:39:44 +00:00
mIndex(pIndex), mPosition(pPosition), mDistance(std::numeric_limits<ai_real>::max()) {
// empty
}
2015-05-19 03:57:13 +00:00
bool operator<(const Entry &e) const { return mDistance < e.mDistance; }
2015-05-19 03:57:13 +00:00
};
// all positions, sorted by distance to the sorting plane
std::vector<Entry> mPositions;
2021-10-20 14:08:08 +00:00
/// false until the Finalize method is called.
bool mFinalized;
};
} // end of namespace Assimp
#endif // AI_SPATIALSORT_H_INC