1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2012, assimp team
All rights reserved.
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.
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
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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.
---------------------------------------------------------------------------
*/
/** @file Implementation of the DeterminePTypeHelperProcess and
* SortByPTypeProcess post-process steps.
*/
#include "AssimpPCH.h"
// internal headers
#include "ProcessHelper.h"
#include "SortByPTypeProcess.h"
using namespace Assimp;
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
SortByPTypeProcess::SortByPTypeProcess()
{
configRemoveMeshes = 0;
}
// ------------------------------------------------------------------------------------------------
// Destructor, private as well
SortByPTypeProcess::~SortByPTypeProcess()
{
// nothing to do here
}
// ------------------------------------------------------------------------------------------------
// Returns whether the processing step is present in the given flag field.
bool SortByPTypeProcess::IsActive( unsigned int pFlags) const
{
return (pFlags & aiProcess_SortByPType) != 0;
}
// ------------------------------------------------------------------------------------------------
void SortByPTypeProcess::SetupProperties(const Importer* pImp)
{
configRemoveMeshes = pImp->GetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE,0);
}
// ------------------------------------------------------------------------------------------------
// Update changed meshes in all nodes
void UpdateNodes(const std::vector<unsigned int>& replaceMeshIndex, aiNode* node)
{
// std::vector<unsigned int>::const_iterator it;
if (node->mNumMeshes)
{
unsigned int newSize = 0;
for (unsigned int m = 0; m< node->mNumMeshes; ++m)
{
unsigned int add = node->mMeshes[m]<<2;
for (unsigned int i = 0; i < 4;++i)
{
if (UINT_MAX != replaceMeshIndex[add+i])++newSize;
}
}
if (!newSize)
{
delete[] node->mMeshes;
node->mNumMeshes = 0;
node->mMeshes = NULL;
}
else
{
// Try to reuse the old array if possible
unsigned int* newMeshes = (newSize > node->mNumMeshes
? new unsigned int[newSize] : node->mMeshes);
for (unsigned int m = 0; m< node->mNumMeshes; ++m)
{
unsigned int add = node->mMeshes[m]<<2;
for (unsigned int i = 0; i < 4;++i)
{
if (UINT_MAX != replaceMeshIndex[add+i])
*newMeshes++ = replaceMeshIndex[add+i];
}
}
if (newSize > node->mNumMeshes)
delete[] node->mMeshes;
node->mMeshes = newMeshes-(node->mNumMeshes = newSize);
}
}
// call all subnodes recursively
for (unsigned int m = 0; m < node->mNumChildren; ++m)
UpdateNodes(replaceMeshIndex,node->mChildren[m]);
}
// ------------------------------------------------------------------------------------------------
// Executes the post processing step on the given imported data.
void SortByPTypeProcess::Execute( aiScene* pScene)
{
if (!pScene->mNumMeshes)
{
DefaultLogger::get()->debug("SortByPTypeProcess skipped, there are no meshes");
return;
}
DefaultLogger::get()->debug("SortByPTypeProcess begin");
unsigned int aiNumMeshesPerPType[4] = {0,0,0,0};
std::vector<aiMesh*> outMeshes;
outMeshes.reserve(pScene->mNumMeshes<<1u);
bool bAnyChanges = false;
std::vector<unsigned int> replaceMeshIndex(pScene->mNumMeshes*4,UINT_MAX);
std::vector<unsigned int>::iterator meshIdx = replaceMeshIndex.begin();
for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
{
aiMesh* mesh = pScene->mMeshes[i];
ai_assert(0 != mesh->mPrimitiveTypes);
// if there's just one primitive type in the mesh there's nothing to do for us
unsigned int num = 0;
if (mesh->mPrimitiveTypes & aiPrimitiveType_POINT)
{
++aiNumMeshesPerPType[0];
++num;
}
if (mesh->mPrimitiveTypes & aiPrimitiveType_LINE)
{
++aiNumMeshesPerPType[1];
++num;
}
if (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE)
{
++aiNumMeshesPerPType[2];
++num;
}
if (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON)
{
++aiNumMeshesPerPType[3];
++num;
}
if (1 == num)
{
if (!(configRemoveMeshes & mesh->mPrimitiveTypes))
{
*meshIdx = (unsigned int) outMeshes.size();
outMeshes.push_back(mesh);
}
else bAnyChanges = true;
meshIdx += 4;
continue;
}
bAnyChanges = true;
// reuse our current mesh arrays for the submesh
// with the largest numer of primitives
unsigned int aiNumPerPType[4] = {0,0,0,0};
aiFace* pFirstFace = mesh->mFaces;
aiFace* const pLastFace = pFirstFace + mesh->mNumFaces;
unsigned int numPolyVerts = 0;
for (;pFirstFace != pLastFace; ++pFirstFace)
{
if (pFirstFace->mNumIndices <= 3)
++aiNumPerPType[pFirstFace->mNumIndices-1];
else
{
++aiNumPerPType[3];
numPolyVerts += pFirstFace-> mNumIndices;
}
}
VertexWeightTable* avw = ComputeVertexBoneWeightTable(mesh);
for (unsigned int real = 0; real < 4; ++real,++meshIdx)
{
if ( !aiNumPerPType[real] || configRemoveMeshes & (1u << real))
{
continue;
}
*meshIdx = (unsigned int) outMeshes.size();
outMeshes.push_back(new aiMesh());
aiMesh* out = outMeshes.back();
// the name carries the adjacency information between the meshes
out->mName = mesh->mName;
// copy data members
out->mPrimitiveTypes = 1u << real;
out->mMaterialIndex = mesh->mMaterialIndex;
// allocate output storage
out->mNumFaces = aiNumPerPType[real];
aiFace* outFaces = out->mFaces = new aiFace[out->mNumFaces];
out->mNumVertices = (3 == real ? numPolyVerts : out->mNumFaces * (real+1));
aiVector3D *vert(NULL), *nor(NULL), *tan(NULL), *bit(NULL);
aiVector3D *uv [AI_MAX_NUMBER_OF_TEXTURECOORDS];
aiColor4D *cols [AI_MAX_NUMBER_OF_COLOR_SETS];
if (mesh->mVertices)
vert = out->mVertices = new aiVector3D[out->mNumVertices];
if (mesh->mNormals)
nor = out->mNormals = new aiVector3D[out->mNumVertices];
if (mesh->mTangents)
{
tan = out->mTangents = new aiVector3D[out->mNumVertices];
bit = out->mBitangents = new aiVector3D[out->mNumVertices];
}
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
{
if (mesh->mTextureCoords[i])
uv[i] = out->mTextureCoords[i] = new aiVector3D[out->mNumVertices];
else uv[i] = NULL;
out->mNumUVComponents[i] = mesh->mNumUVComponents[i];
}
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
{
if (mesh->mColors[i])
cols[i] = out->mColors[i] = new aiColor4D[out->mNumVertices];
else cols[i] = NULL;
}
typedef std::vector< aiVertexWeight > TempBoneInfo;
std::vector< TempBoneInfo > tempBones(mesh->mNumBones);
// try to guess how much storage we'll need
for (unsigned int q = 0; q < mesh->mNumBones;++q)
{
tempBones[q].reserve(mesh->mBones[q]->mNumWeights / (num-1));
}
unsigned int outIdx = 0;
for (unsigned int m = 0; m < mesh->mNumFaces; ++m)
{
aiFace& in = mesh->mFaces[m];
if ((real == 3 && in.mNumIndices <= 3) || (real != 3 && in.mNumIndices != real+1))
{
continue;
}
outFaces->mNumIndices = in.mNumIndices;
outFaces->mIndices = in.mIndices;
for (unsigned int q = 0; q < in.mNumIndices; ++q)
{
register unsigned int idx = in.mIndices[q];
// process all bones of this index
if (avw)
{
VertexWeightTable& tbl = avw[idx];
for (VertexWeightTable::const_iterator it = tbl.begin(), end = tbl.end();
it != end; ++it)
{
tempBones[ (*it).first ].push_back( aiVertexWeight(outIdx, (*it).second) );
}
}
if (vert)
{
*vert++ = mesh->mVertices[idx];
//mesh->mVertices[idx].x = get_qnan();
}
if (nor )*nor++ = mesh->mNormals[idx];
if (tan )
{
*tan++ = mesh->mTangents[idx];
*bit++ = mesh->mBitangents[idx];
}
for (unsigned int pp = 0; pp < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++pp)
{
if (!uv[pp])break;
*uv[pp]++ = mesh->mTextureCoords[pp][idx];
}
for (unsigned int pp = 0; pp < AI_MAX_NUMBER_OF_COLOR_SETS; ++pp)
{
if (!cols[pp])break;
*cols[pp]++ = mesh->mColors[pp][idx];
}
in.mIndices[q] = outIdx++;
}
in.mIndices = NULL;
++outFaces;
}
ai_assert(outFaces == out->mFaces + out->mNumFaces);
// now generate output bones
for (unsigned int q = 0; q < mesh->mNumBones;++q)
if (!tempBones[q].empty())++out->mNumBones;
if (out->mNumBones)
{
out->mBones = new aiBone*[out->mNumBones];
for (unsigned int q = 0, real = 0; q < mesh->mNumBones;++q)
{
TempBoneInfo& in = tempBones[q];
if (in.empty())continue;
aiBone* srcBone = mesh->mBones[q];
aiBone* bone = out->mBones[real] = new aiBone();
bone->mName = srcBone->mName;
bone->mOffsetMatrix = srcBone->mOffsetMatrix;
bone->mNumWeights = (unsigned int)in.size();
bone->mWeights = new aiVertexWeight[bone->mNumWeights];
::memcpy(bone->mWeights,&in[0],bone->mNumWeights*sizeof(aiVertexWeight));
++real;
}
}
}
// delete the per-vertex bone weights table
delete[] avw;
// delete the input mesh
delete mesh;
}
if (outMeshes.empty())
{
// This should not occur
throw DeadlyImportError("No meshes remaining");
}
// If we added at least one mesh process all nodes in the node
// graph and update their respective mesh indices.
if (bAnyChanges)
{
UpdateNodes(replaceMeshIndex,pScene->mRootNode);
}
if (outMeshes.size() != pScene->mNumMeshes)
{
delete[] pScene->mMeshes;
pScene->mNumMeshes = (unsigned int)outMeshes.size();
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
}
::memcpy(pScene->mMeshes,&outMeshes[0],pScene->mNumMeshes*sizeof(void*));
if (!DefaultLogger::isNullLogger())
{
char buffer[1024];
::sprintf(buffer,"Points: %i%s, Lines: %i%s, Triangles: %i%s, Polygons: %i%s (Meshes, X = removed)",
aiNumMeshesPerPType[0], (configRemoveMeshes & aiPrimitiveType_POINT ? "X" : ""),
aiNumMeshesPerPType[1], (configRemoveMeshes & aiPrimitiveType_LINE ? "X" : ""),
aiNumMeshesPerPType[2], (configRemoveMeshes & aiPrimitiveType_TRIANGLE ? "X" : ""),
aiNumMeshesPerPType[3], (configRemoveMeshes & aiPrimitiveType_POLYGON ? "X" : ""));
DefaultLogger::get()->info(buffer);
DefaultLogger::get()->debug("SortByPTypeProcess finished");
}
}
|