2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +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 FindInstancesProcess.cpp
|
|
|
|
* @brief Implementation of the aiProcess_FindInstances postprocessing step
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "FindInstancesProcess.h"
|
2016-04-05 21:23:53 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
FindInstancesProcess::FindInstancesProcess()
|
2015-05-19 03:57:13 +00:00
|
|
|
: configSpeedFlag (false)
|
2015-05-19 03:48:29 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
FindInstancesProcess::~FindInstancesProcess()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
|
|
|
bool FindInstancesProcess::IsActive( unsigned int pFlags) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
// FindInstances makes absolutely no sense together with PreTransformVertices
|
|
|
|
// fixme: spawn error message somewhere else?
|
|
|
|
return 0 != (pFlags & aiProcess_FindInstances) && 0 == (pFlags & aiProcess_PreTransformVertices);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup properties for the step
|
|
|
|
void FindInstancesProcess::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
// AI_CONFIG_FAVOUR_SPEED
|
|
|
|
configSpeedFlag = (0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED,0));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Compare the bones of two meshes
|
|
|
|
bool CompareBones(const aiMesh* orig, const aiMesh* inst)
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int i = 0; i < orig->mNumBones;++i) {
|
|
|
|
aiBone* aha = orig->mBones[i];
|
|
|
|
aiBone* oha = inst->mBones[i];
|
|
|
|
|
|
|
|
if (aha->mNumWeights != oha->mNumWeights ||
|
|
|
|
aha->mOffsetMatrix != oha->mOffsetMatrix) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare weight per weight ---
|
|
|
|
for (unsigned int n = 0; n < aha->mNumWeights;++n) {
|
|
|
|
if (aha->mWeights[n].mVertexId != oha->mWeights[n].mVertexId ||
|
|
|
|
(aha->mWeights[n].mWeight - oha->mWeights[n].mWeight) < 10e-3f) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Update mesh indices in the node graph
|
|
|
|
void UpdateMeshIndices(aiNode* node, unsigned int* lookup)
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int n = 0; n < node->mNumMeshes;++n)
|
|
|
|
node->mMeshes[n] = lookup[node->mMeshes[n]];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int n = 0; n < node->mNumChildren;++n)
|
|
|
|
UpdateMeshIndices(node->mChildren[n],lookup);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
|
|
|
void FindInstancesProcess::Execute( aiScene* pScene)
|
|
|
|
{
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("FindInstancesProcess begin");
|
2015-05-19 03:57:13 +00:00
|
|
|
if (pScene->mNumMeshes) {
|
|
|
|
|
|
|
|
// use a pseudo hash for all meshes in the scene to quickly find
|
|
|
|
// the ones which are possibly equal. This step is executed early
|
|
|
|
// in the pipeline, so we could, depending on the file format,
|
|
|
|
// have several thousand small meshes. That's too much for a brute
|
|
|
|
// everyone-against-everyone check involving up to 10 comparisons
|
|
|
|
// each.
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<uint64_t[]> hashes (new uint64_t[pScene->mNumMeshes]);
|
|
|
|
std::unique_ptr<unsigned int[]> remapping (new unsigned int[pScene->mNumMeshes]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
unsigned int numMeshesOut = 0;
|
|
|
|
for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
|
|
|
|
|
|
|
|
aiMesh* inst = pScene->mMeshes[i];
|
|
|
|
hashes[i] = GetMeshHash(inst);
|
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
// Find an appropriate epsilon
|
2019-05-22 18:00:33 +00:00
|
|
|
// to compare position differences against
|
|
|
|
float epsilon = ComputePositionEpsilon(inst);
|
|
|
|
epsilon *= epsilon;
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
for (int a = i-1; a >= 0; --a) {
|
|
|
|
if (hashes[i] == hashes[a])
|
|
|
|
{
|
|
|
|
aiMesh* orig = pScene->mMeshes[a];
|
|
|
|
if (!orig)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check for hash collision .. we needn't check
|
|
|
|
// the vertex format, it *must* match due to the
|
|
|
|
// (brilliant) construction of the hash
|
|
|
|
if (orig->mNumBones != inst->mNumBones ||
|
|
|
|
orig->mNumFaces != inst->mNumFaces ||
|
|
|
|
orig->mNumVertices != inst->mNumVertices ||
|
|
|
|
orig->mMaterialIndex != inst->mMaterialIndex ||
|
|
|
|
orig->mPrimitiveTypes != inst->mPrimitiveTypes)
|
|
|
|
continue;
|
|
|
|
|
2019-05-22 18:00:33 +00:00
|
|
|
// up to now the meshes are equal. Now compare vertex positions, normals,
|
2015-05-19 03:57:13 +00:00
|
|
|
// tangents and bitangents using this epsilon.
|
|
|
|
if (orig->HasPositions()) {
|
|
|
|
if(!CompareArrays(orig->mVertices,inst->mVertices,orig->mNumVertices,epsilon))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (orig->HasNormals()) {
|
|
|
|
if(!CompareArrays(orig->mNormals,inst->mNormals,orig->mNumVertices,epsilon))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (orig->HasTangentsAndBitangents()) {
|
|
|
|
if (!CompareArrays(orig->mTangents,inst->mTangents,orig->mNumVertices,epsilon) ||
|
|
|
|
!CompareArrays(orig->mBitangents,inst->mBitangents,orig->mNumVertices,epsilon))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a constant epsilon for colors and UV coordinates
|
|
|
|
static const float uvEpsilon = 10e-4f;
|
|
|
|
{
|
2019-03-13 15:58:18 +00:00
|
|
|
unsigned int j, end = orig->GetNumUVChannels();
|
|
|
|
for(j = 0; j < end; ++j) {
|
|
|
|
if (!orig->mTextureCoords[j]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-13 15:58:18 +00:00
|
|
|
if(!CompareArrays(orig->mTextureCoords[j],inst->mTextureCoords[j],orig->mNumVertices,uvEpsilon)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 15:58:18 +00:00
|
|
|
if (j != end) {
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2019-03-13 15:58:18 +00:00
|
|
|
unsigned int j, end = orig->GetNumColorChannels();
|
|
|
|
for(j = 0; j < end; ++j) {
|
|
|
|
if (!orig->mColors[j]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-13 15:58:18 +00:00
|
|
|
if(!CompareArrays(orig->mColors[j],inst->mColors[j],orig->mNumVertices,uvEpsilon)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 15:58:18 +00:00
|
|
|
if (j != end) {
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These two checks are actually quite expensive and almost *never* required.
|
|
|
|
// Almost. That's why they're still here. But there's no reason to do them
|
|
|
|
// in speed-targeted imports.
|
|
|
|
if (!configSpeedFlag) {
|
|
|
|
|
|
|
|
// It seems to be strange, but we really need to check whether the
|
|
|
|
// bones are identical too. Although it's extremely unprobable
|
|
|
|
// that they're not if control reaches here, we need to deal
|
|
|
|
// with unprobable cases, too. It could still be that there are
|
|
|
|
// equal shapes which are deformed differently.
|
|
|
|
if (!CompareBones(orig,inst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// For completeness ... compare even the index buffers for equality
|
|
|
|
// face order & winding order doesn't care. Input data is in verbose format.
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<unsigned int[]> ftbl_orig(new unsigned int[orig->mNumVertices]);
|
|
|
|
std::unique_ptr<unsigned int[]> ftbl_inst(new unsigned int[orig->mNumVertices]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
for (unsigned int tt = 0; tt < orig->mNumFaces;++tt) {
|
|
|
|
aiFace& f = orig->mFaces[tt];
|
|
|
|
for (unsigned int nn = 0; nn < f.mNumIndices;++nn)
|
|
|
|
ftbl_orig[f.mIndices[nn]] = tt;
|
|
|
|
|
|
|
|
aiFace& f2 = inst->mFaces[tt];
|
|
|
|
for (unsigned int nn = 0; nn < f2.mNumIndices;++nn)
|
|
|
|
ftbl_inst[f2.mIndices[nn]] = tt;
|
|
|
|
}
|
|
|
|
if (0 != ::memcmp(ftbl_inst.get(),ftbl_orig.get(),orig->mNumVertices*sizeof(unsigned int)))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're still here. Or in other words: 'inst' is an instance of 'orig'.
|
|
|
|
// Place a marker in our list that we can easily update mesh indices.
|
|
|
|
remapping[i] = remapping[a];
|
|
|
|
|
|
|
|
// Delete the instanced mesh, we don't need it anymore
|
|
|
|
delete inst;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMeshes[i] = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find a match for the current mesh: keep it
|
|
|
|
if (pScene->mMeshes[i]) {
|
|
|
|
remapping[i] = numMeshesOut++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ai_assert(0 != numMeshesOut);
|
|
|
|
if (numMeshesOut != pScene->mNumMeshes) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// Collapse the meshes array by removing all nullptr entries
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int real = 0, i = 0; real < numMeshesOut; ++i) {
|
|
|
|
if (pScene->mMeshes[i])
|
|
|
|
pScene->mMeshes[real++] = pScene->mMeshes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// And update the node graph with our nice lookup table
|
|
|
|
UpdateMeshIndices(pScene->mRootNode,remapping.get());
|
|
|
|
|
|
|
|
// write to log
|
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_INFO( "FindInstancesProcess finished. Found ", (pScene->mNumMeshes - numMeshesOut), " instances" );
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
pScene->mNumMeshes = numMeshesOut;
|
2018-04-26 12:10:18 +00:00
|
|
|
} else {
|
|
|
|
ASSIMP_LOG_DEBUG("FindInstancesProcess finished. No instanced meshes found");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|