2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file Implementation of the post processing step to join identical vertices
|
|
|
|
* for all imported meshes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_JOINVERTICES_PROCESS
|
|
|
|
|
|
|
|
#include "JoinVerticesProcess.h"
|
|
|
|
#include "ProcessHelper.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/Vertex.h>
|
|
|
|
#include <assimp/TinyFormatter.h>
|
2022-05-14 07:56:38 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <stdio.h>
|
2018-07-05 13:38:58 +00:00
|
|
|
#include <unordered_set>
|
2022-05-13 18:59:16 +00:00
|
|
|
#include <unordered_map>
|
2023-09-27 08:20:56 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
2022-05-14 07:56:38 +00:00
|
|
|
bool JoinVerticesProcess::IsActive( unsigned int pFlags) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return (pFlags & aiProcess_JoinIdenticalVertices) != 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
2022-05-14 07:56:38 +00:00
|
|
|
void JoinVerticesProcess::Execute( aiScene* pScene) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("JoinVerticesProcess begin");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// get the total number of vertices BEFORE the step is executed
|
|
|
|
int iNumOldVertices = 0;
|
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
|
|
|
|
iNumOldVertices += pScene->mMeshes[a]->mNumVertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the step
|
|
|
|
int iNumVertices = 0;
|
2022-05-14 07:56:38 +00:00
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
iNumVertices += ProcessMesh( pScene->mMeshes[a],a);
|
2022-05-14 07:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pScene->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// if logging is active, print detailed statistics
|
2018-04-26 12:10:18 +00:00
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
|
|
|
if (iNumOldVertices == iNumVertices) {
|
|
|
|
ASSIMP_LOG_DEBUG("JoinVerticesProcess finished ");
|
2022-05-14 07:56:38 +00:00
|
|
|
return;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-01-16 08:12:35 +00:00
|
|
|
|
2022-05-14 07:56:38 +00:00
|
|
|
// Show statistics
|
|
|
|
ASSIMP_LOG_INFO("JoinVerticesProcess finished | Verts in: ", iNumOldVertices,
|
|
|
|
" out: ", iNumVertices, " | ~",
|
|
|
|
((iNumOldVertices - iNumVertices) / (float)iNumOldVertices) * 100.f );
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 16:34:27 +00:00
|
|
|
namespace {
|
|
|
|
|
2023-01-12 12:13:46 +00:00
|
|
|
bool areVerticesEqual(
|
|
|
|
const Vertex &lhs,
|
|
|
|
const Vertex &rhs,
|
|
|
|
unsigned numUVChannels,
|
|
|
|
unsigned numColorChannels) {
|
2018-04-26 16:34:27 +00:00
|
|
|
// A little helper to find locally close vertices faster.
|
|
|
|
// Try to reuse the lookup table from the last step.
|
|
|
|
const static float epsilon = 1e-5f;
|
|
|
|
// Squared because we check against squared length of the vector difference
|
|
|
|
static const float squareEpsilon = epsilon * epsilon;
|
|
|
|
|
2018-05-13 14:35:03 +00:00
|
|
|
// Square compare is useful for animeshes vertices compare
|
2018-04-26 16:34:27 +00:00
|
|
|
if ((lhs.position - rhs.position).SquareLength() > squareEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We just test the other attributes even if they're not present in the mesh.
|
|
|
|
// In this case they're initialized to 0 so the comparison succeeds.
|
|
|
|
// By this method the non-present attributes are effectively ignored in the comparison.
|
|
|
|
if ((lhs.normal - rhs.normal).SquareLength() > squareEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lhs.tangent - rhs.tangent).SquareLength() > squareEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lhs.bitangent - rhs.bitangent).SquareLength() > squareEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:13:46 +00:00
|
|
|
for (unsigned i = 0; i < numUVChannels; i++) {
|
|
|
|
if ((lhs.texcoords[i] - rhs.texcoords[i]).SquareLength() > squareEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < numColorChannels; i++) {
|
|
|
|
if (GetColorDifference(lhs.colors[i], rhs.colors[i]) > squareEpsilon) {
|
|
|
|
return false;
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:13:46 +00:00
|
|
|
|
2018-04-26 16:34:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class XMesh>
|
2023-09-26 05:20:13 +00:00
|
|
|
void updateXMeshVertices(XMesh *pMesh, std::vector<int> &uniqueVertices) {
|
2018-04-26 16:34:27 +00:00
|
|
|
// replace vertex data with the unique data sets
|
|
|
|
pMesh->mNumVertices = (unsigned int)uniqueVertices.size();
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// NOTE - we're *not* calling Vertex::SortBack() because it would check for
|
|
|
|
// presence of every single vertex component once PER VERTEX. And our CPU
|
|
|
|
// dislikes branches, even if they're easily predictable.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Position, if present (check made for aiAnimMesh)
|
2023-09-27 03:29:10 +00:00
|
|
|
if (pMesh->mVertices) {
|
|
|
|
std::unique_ptr<aiVector3D[]> oldVertices(pMesh->mVertices);
|
2018-04-26 16:34:27 +00:00
|
|
|
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
|
2023-09-26 05:20:13 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
|
|
|
|
pMesh->mVertices[a] = oldVertices[uniqueVertices[a]];
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normals, if present
|
2022-05-14 07:56:38 +00:00
|
|
|
if (pMesh->mNormals) {
|
2023-09-27 03:29:10 +00:00
|
|
|
std::unique_ptr<aiVector3D[]> oldNormals(pMesh->mNormals);
|
2018-04-26 16:34:27 +00:00
|
|
|
pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
|
2023-09-26 05:20:13 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
|
|
|
|
pMesh->mNormals[a] = oldNormals[uniqueVertices[a]];
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
// Tangents, if present
|
2022-05-14 07:56:38 +00:00
|
|
|
if (pMesh->mTangents) {
|
2023-09-27 03:29:10 +00:00
|
|
|
std::unique_ptr<aiVector3D[]> oldTangents(pMesh->mTangents);
|
2018-04-26 16:34:27 +00:00
|
|
|
pMesh->mTangents = new aiVector3D[pMesh->mNumVertices];
|
2023-09-26 05:20:13 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
|
|
|
|
pMesh->mTangents[a] = oldTangents[uniqueVertices[a]];
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
// Bitangents as well
|
2022-05-14 07:56:38 +00:00
|
|
|
if (pMesh->mBitangents) {
|
2023-09-27 03:29:10 +00:00
|
|
|
std::unique_ptr<aiVector3D[]> oldBitangents(pMesh->mBitangents);
|
2018-04-26 16:34:27 +00:00
|
|
|
pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices];
|
2023-09-26 05:20:13 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
|
|
|
|
pMesh->mBitangents[a] = oldBitangents[uniqueVertices[a]];
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
// Vertex colors
|
2022-05-14 07:56:38 +00:00
|
|
|
for (unsigned int a = 0; pMesh->HasVertexColors(a); a++) {
|
2023-09-27 03:29:10 +00:00
|
|
|
std::unique_ptr<aiColor4D[]> oldColors(pMesh->mColors[a]);
|
2018-04-26 16:34:27 +00:00
|
|
|
pMesh->mColors[a] = new aiColor4D[pMesh->mNumVertices];
|
2023-09-26 05:20:13 +00:00
|
|
|
for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
|
|
|
|
pMesh->mColors[a][b] = oldColors[uniqueVertices[b]];
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
// Texture coords
|
2022-05-14 07:56:38 +00:00
|
|
|
for (unsigned int a = 0; pMesh->HasTextureCoords(a); a++) {
|
2023-09-27 03:29:10 +00:00
|
|
|
std::unique_ptr<aiVector3D[]> oldTextureCoords(pMesh->mTextureCoords[a]);
|
2018-04-26 16:34:27 +00:00
|
|
|
pMesh->mTextureCoords[a] = new aiVector3D[pMesh->mNumVertices];
|
2023-09-26 05:20:13 +00:00
|
|
|
for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
|
|
|
|
pMesh->mTextureCoords[a][b] = oldTextureCoords[uniqueVertices[b]];
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-14 07:56:38 +00:00
|
|
|
|
2018-04-26 16:34:27 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Unites identical vertices in the given mesh
|
2022-05-13 19:10:19 +00:00
|
|
|
// combine hashes
|
2022-05-14 07:59:07 +00:00
|
|
|
inline void hash_combine(std::size_t &) {
|
|
|
|
// empty
|
2022-05-13 17:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Rest>
|
|
|
|
inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
|
|
|
|
std::hash<T> hasher;
|
|
|
|
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
|
|
|
|
hash_combine(seed, rest...);
|
|
|
|
}
|
2022-05-13 19:10:19 +00:00
|
|
|
//template specialization for std::hash for Vertex
|
2022-05-13 17:14:49 +00:00
|
|
|
template<>
|
2022-05-14 07:56:38 +00:00
|
|
|
struct std::hash<Vertex> {
|
|
|
|
std::size_t operator()(Vertex const& v) const noexcept {
|
|
|
|
size_t seed = 0;
|
|
|
|
hash_combine(seed, v.position.x ,v.position.y,v.position.z);
|
2023-01-16 08:12:35 +00:00
|
|
|
return seed;
|
2022-05-14 07:56:38 +00:00
|
|
|
}
|
2022-05-13 17:14:49 +00:00
|
|
|
};
|
2022-05-13 19:10:19 +00:00
|
|
|
//template specialization for std::equal_to for Vertex
|
2022-05-13 17:14:49 +00:00
|
|
|
template<>
|
|
|
|
struct std::equal_to<Vertex> {
|
2023-01-12 12:13:46 +00:00
|
|
|
equal_to(unsigned numUVChannels, unsigned numColorChannels) :
|
|
|
|
mNumUVChannels(numUVChannels),
|
|
|
|
mNumColorChannels(numColorChannels) {}
|
2022-05-13 17:14:49 +00:00
|
|
|
bool operator()(const Vertex &lhs, const Vertex &rhs) const {
|
2023-01-12 12:13:46 +00:00
|
|
|
return areVerticesEqual(lhs, rhs, mNumUVChannels, mNumColorChannels);
|
2022-05-13 17:14:49 +00:00
|
|
|
}
|
2023-01-12 12:13:46 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned mNumUVChannels;
|
|
|
|
unsigned mNumColorChannels;
|
2022-05-13 17:14:49 +00:00
|
|
|
};
|
2023-02-08 02:13:48 +00:00
|
|
|
|
2023-02-16 09:59:36 +00:00
|
|
|
static constexpr size_t JOINED_VERTICES_MARK = 0x80000000u;
|
2023-02-16 08:28:35 +00:00
|
|
|
|
2022-05-13 19:10:19 +00:00
|
|
|
// now start the JoinVerticesProcess
|
2022-05-14 07:56:38 +00:00
|
|
|
int JoinVerticesProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshIndex) {
|
2016-03-19 00:02:39 +00:00
|
|
|
static_assert( AI_MAX_NUMBER_OF_COLOR_SETS == 8, "AI_MAX_NUMBER_OF_COLOR_SETS == 8");
|
2022-05-14 07:56:38 +00:00
|
|
|
static_assert( AI_MAX_NUMBER_OF_TEXTURECOORDS == 8, "AI_MAX_NUMBER_OF_TEXTURECOORDS == 8");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Return early if we don't have any positions
|
|
|
|
if (!pMesh->HasPositions() || !pMesh->HasFaces()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-05 13:38:58 +00:00
|
|
|
// We should care only about used vertices, not all of them
|
|
|
|
// (this can happen due to original file vertices buffer being used by
|
|
|
|
// multiple meshes)
|
2023-01-23 15:39:06 +00:00
|
|
|
std::vector<bool> usedVertexIndicesMask;
|
|
|
|
usedVertexIndicesMask.resize(pMesh->mNumVertices, false);
|
2023-01-23 13:51:02 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
|
2018-07-05 13:38:58 +00:00
|
|
|
aiFace& face = pMesh->mFaces[a];
|
2023-01-23 13:51:02 +00:00
|
|
|
for (unsigned int b = 0; b < face.mNumIndices; b++) {
|
2023-01-23 15:39:06 +00:00
|
|
|
usedVertexIndicesMask[face.mIndices[b]] = true;
|
2018-07-05 13:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// We'll never have more vertices afterwards.
|
2023-09-26 05:20:13 +00:00
|
|
|
std::vector<int> uniqueVertices;
|
2015-05-19 03:57:13 +00:00
|
|
|
uniqueVertices.reserve( pMesh->mNumVertices);
|
|
|
|
|
|
|
|
// For each vertex the index of the vertex it was replaced by.
|
|
|
|
// Since the maximal number of vertices is 2^31-1, the most significand bit can be used to mark
|
|
|
|
// whether a new vertex was created for the index (true) or if it was replaced by an existing
|
|
|
|
// unique vertex (false). This saves an additional std::vector<bool> and greatly enhances
|
|
|
|
// branching performance.
|
2016-03-19 00:02:39 +00:00
|
|
|
static_assert(AI_MAX_VERTICES == 0x7fffffff, "AI_MAX_VERTICES == 0x7fffffff");
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<unsigned int> replaceIndex( pMesh->mNumVertices, 0xffffffff);
|
|
|
|
|
|
|
|
// float posEpsilonSqr;
|
2020-06-23 19:05:42 +00:00
|
|
|
SpatialSort *vertexFinder = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
SpatialSort _vertexFinder;
|
|
|
|
|
|
|
|
typedef std::pair<SpatialSort,float> SpatPair;
|
|
|
|
if (shared) {
|
|
|
|
std::vector<SpatPair >* avf;
|
|
|
|
shared->GetProperty(AI_SPP_SPATIAL_SORT,avf);
|
|
|
|
if (avf) {
|
|
|
|
SpatPair& blubb = (*avf)[meshIndex];
|
|
|
|
vertexFinder = &blubb.first;
|
|
|
|
// posEpsilonSqr = blubb.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!vertexFinder) {
|
|
|
|
// bad, need to compute it.
|
|
|
|
_vertexFinder.Fill(pMesh->mVertices, pMesh->mNumVertices, sizeof( aiVector3D));
|
|
|
|
vertexFinder = &_vertexFinder;
|
|
|
|
// posEpsilonSqr = ComputePositionEpsilon(pMesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Again, better waste some bytes than a realloc ...
|
|
|
|
std::vector<unsigned int> verticesFound;
|
|
|
|
verticesFound.reserve(10);
|
|
|
|
|
|
|
|
// Run an optimized code path if we don't have multiple UVs or vertex colors.
|
|
|
|
// This should yield false in more than 99% of all imports ...
|
2018-04-26 16:34:27 +00:00
|
|
|
const bool hasAnimMeshes = pMesh->mNumAnimMeshes > 0;
|
|
|
|
|
|
|
|
// We'll never have more vertices afterwards.
|
2023-09-26 05:20:13 +00:00
|
|
|
std::vector<std::vector<int>> uniqueAnimatedVertices;
|
2018-04-26 16:34:27 +00:00
|
|
|
if (hasAnimMeshes) {
|
|
|
|
uniqueAnimatedVertices.resize(pMesh->mNumAnimMeshes);
|
|
|
|
for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
|
|
|
|
uniqueAnimatedVertices[animMeshIndex].reserve(pMesh->mNumVertices);
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:13:46 +00:00
|
|
|
// a map that maps a vertex to its new index
|
|
|
|
const auto numBuckets = pMesh->mNumVertices;
|
|
|
|
const auto hasher = std::hash<Vertex>();
|
|
|
|
const auto comparator = std::equal_to<Vertex>(
|
|
|
|
pMesh->GetNumUVChannels(),
|
|
|
|
pMesh->GetNumColorChannels());
|
|
|
|
std::unordered_map<Vertex, int> vertex2Index(numBuckets, hasher, comparator);
|
2022-05-13 19:10:19 +00:00
|
|
|
// we can not end up with more vertices than we started with
|
2022-05-13 18:59:16 +00:00
|
|
|
vertex2Index.reserve(pMesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
// Now check each vertex if it brings something new to the table
|
2022-05-13 18:59:16 +00:00
|
|
|
int newIndex = 0;
|
2015-05-19 03:57:13 +00:00
|
|
|
for( unsigned int a = 0; a < pMesh->mNumVertices; a++) {
|
2022-05-13 19:10:19 +00:00
|
|
|
// if the vertex is unused Do nothing
|
2023-01-23 15:39:06 +00:00
|
|
|
if (!usedVertexIndicesMask[a]) {
|
2018-07-05 13:38:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
// collect the vertex data
|
|
|
|
Vertex v(pMesh,a);
|
2022-05-13 19:10:19 +00:00
|
|
|
// is the vertex already in the map?
|
2022-05-13 18:59:16 +00:00
|
|
|
auto it = vertex2Index.find(v);
|
2022-05-13 19:10:19 +00:00
|
|
|
// if the vertex is not in the map then it is a new vertex add it.
|
2022-05-13 18:59:16 +00:00
|
|
|
if (it == vertex2Index.end()) {
|
2022-05-13 19:10:19 +00:00
|
|
|
// this is a new vertex give it a new index
|
2022-05-13 18:59:16 +00:00
|
|
|
vertex2Index[v] = newIndex;
|
2022-05-13 19:10:19 +00:00
|
|
|
//keep track of its index and increment 1
|
2022-05-13 18:59:16 +00:00
|
|
|
replaceIndex[a] = newIndex++;
|
2022-05-13 19:10:19 +00:00
|
|
|
// add the vertex to the unique vertices
|
2023-09-26 05:20:13 +00:00
|
|
|
uniqueVertices.push_back(a);
|
2018-04-26 16:34:27 +00:00
|
|
|
if (hasAnimMeshes) {
|
|
|
|
for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
|
2023-09-26 05:20:13 +00:00
|
|
|
uniqueAnimatedVertices[animMeshIndex].emplace_back(a);
|
2018-04-26 16:34:27 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2022-05-14 07:56:38 +00:00
|
|
|
} else{
|
2022-05-13 19:10:19 +00:00
|
|
|
// if the vertex is already there just find the replace index that is appropriate to it
|
2023-02-08 02:13:48 +00:00
|
|
|
// mark it with JOINED_VERTICES_MARK
|
|
|
|
replaceIndex[a] = it->second | JOINED_VERTICES_MARK;
|
2022-05-14 07:56:38 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DefaultLogger::isNullLogger() && DefaultLogger::get()->getLogSeverity() == Logger::VERBOSE) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(
|
2015-05-19 03:57:13 +00:00
|
|
|
"Mesh ",meshIndex,
|
|
|
|
" (",
|
|
|
|
(pMesh->mName.length ? pMesh->mName.data : "unnamed"),
|
|
|
|
") | Verts in: ",pMesh->mNumVertices,
|
|
|
|
" out: ",
|
|
|
|
uniqueVertices.size(),
|
|
|
|
" | ~",
|
|
|
|
((pMesh->mNumVertices - uniqueVertices.size()) / (float)pMesh->mNumVertices) * 100.f,
|
|
|
|
"%"
|
2018-04-26 12:10:18 +00:00
|
|
|
);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 16:34:27 +00:00
|
|
|
updateXMeshVertices(pMesh, uniqueVertices);
|
|
|
|
if (hasAnimMeshes) {
|
|
|
|
for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
|
|
|
|
updateXMeshVertices(pMesh->mAnimMeshes[animMeshIndex], uniqueAnimatedVertices[animMeshIndex]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust the indices in all faces
|
2022-05-14 07:56:38 +00:00
|
|
|
for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiFace& face = pMesh->mFaces[a];
|
|
|
|
for( unsigned int b = 0; b < face.mNumIndices; b++) {
|
2023-02-08 02:13:48 +00:00
|
|
|
face.mIndices[b] = replaceIndex[face.mIndices[b]] & ~JOINED_VERTICES_MARK;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust bone vertex weights.
|
2016-02-05 17:53:23 +00:00
|
|
|
for( int a = 0; a < (int)pMesh->mNumBones; a++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiBone* bone = pMesh->mBones[a];
|
|
|
|
std::vector<aiVertexWeight> newWeights;
|
|
|
|
newWeights.reserve( bone->mNumWeights);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr != bone->mWeights) {
|
2016-02-05 17:53:23 +00:00
|
|
|
for ( unsigned int b = 0; b < bone->mNumWeights; b++ ) {
|
|
|
|
const aiVertexWeight& ow = bone->mWeights[ b ];
|
|
|
|
// if the vertex is a unique one, translate it
|
2023-02-08 02:13:48 +00:00
|
|
|
// filter out joined vertices by JOINED_VERTICES_MARK.
|
|
|
|
if ( !( replaceIndex[ ow.mVertexId ] & JOINED_VERTICES_MARK ) ) {
|
2016-02-05 17:53:23 +00:00
|
|
|
aiVertexWeight nw;
|
|
|
|
nw.mVertexId = replaceIndex[ ow.mVertexId ];
|
|
|
|
nw.mWeight = ow.mWeight;
|
|
|
|
newWeights.push_back( nw );
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2016-02-05 17:53:23 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_ERROR( "X-Export: aiBone shall contain weights, but pointer to them is nullptr." );
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newWeights.size() > 0) {
|
|
|
|
// kill the old and replace them with the translated weights
|
|
|
|
delete [] bone->mWeights;
|
|
|
|
bone->mNumWeights = (unsigned int)newWeights.size();
|
|
|
|
|
|
|
|
bone->mWeights = new aiVertexWeight[bone->mNumWeights];
|
|
|
|
memcpy( bone->mWeights, &newWeights[0], bone->mNumWeights * sizeof( aiVertexWeight));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pMesh->mNumVertices;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_JOINVERTICES_PROCESS
|