1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2019, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15   copyright notice, this list of conditions and the
16   following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19   copyright notice, this list of conditions and the
20   following disclaimer in the documentation and/or other
21   materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24   contributors may be used to endorse or promote products
25   derived from this software without specific prior
26   written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
42 
43 /** @file  FindInstancesProcess.h
44  *  @brief Declares the aiProcess_FindInstances post-process step
45  */
46 #ifndef AI_FINDINSTANCES_H_INC
47 #define AI_FINDINSTANCES_H_INC
48 
49 #include "Common/BaseProcess.h"
50 #include "PostProcessing/ProcessHelper.h"
51 
52 class FindInstancesProcessTest;
53 namespace Assimp    {
54 
55 // -------------------------------------------------------------------------------
56 /** @brief Get a pseudo(!)-hash representing a mesh.
57  *
58  *  The hash is built from number of vertices, faces, primitive types,
59  *  .... but *not* from the real mesh data. The funcction is not a perfect hash.
60  *  @param in Input mesh
61  *  @return Hash.
62  */
63 inline
GetMeshHash(aiMesh * in)64 uint64_t GetMeshHash(aiMesh* in) {
65     ai_assert(nullptr != in);
66 
67     // ... get an unique value representing the vertex format of the mesh
68     const unsigned int fhash = GetMeshVFormatUnique(in);
69 
70     // and bake it with number of vertices/faces/bones/matidx/ptypes
71     return ((uint64_t)fhash << 32u) | ((
72         (in->mNumBones << 16u) ^  (in->mNumVertices)       ^
73         (in->mNumFaces<<4u)    ^  (in->mMaterialIndex<<15) ^
74         (in->mPrimitiveTypes<<28)) & 0xffffffff );
75 }
76 
77 // -------------------------------------------------------------------------------
78 /** @brief Perform a component-wise comparison of two arrays
79  *
80  *  @param first First array
81  *  @param second Second array
82  *  @param size Size of both arrays
83  *  @param e Epsilon
84  *  @return true if the arrays are identical
85  */
86 inline
CompareArrays(const aiVector3D * first,const aiVector3D * second,unsigned int size,float e)87 bool CompareArrays(const aiVector3D* first, const aiVector3D* second,
88         unsigned int size, float e) {
89     for (const aiVector3D* end = first+size; first != end; ++first,++second) {
90         if ( (*first - *second).SquareLength() >= e)
91             return false;
92     }
93     return true;
94 }
95 
96 // and the same for colors ...
CompareArrays(const aiColor4D * first,const aiColor4D * second,unsigned int size,float e)97 inline bool CompareArrays(const aiColor4D* first, const aiColor4D* second,
98     unsigned int size, float e)
99 {
100     for (const aiColor4D* end = first+size; first != end; ++first,++second) {
101         if ( GetColorDifference(*first,*second) >= e)
102             return false;
103     }
104     return true;
105 }
106 
107 // ---------------------------------------------------------------------------
108 /** @brief A post-processing steps to search for instanced meshes
109 */
110 class FindInstancesProcess : public BaseProcess
111 {
112 public:
113 
114     FindInstancesProcess();
115     ~FindInstancesProcess();
116 
117 public:
118     // -------------------------------------------------------------------
119     // Check whether step is active in given flags combination
120     bool IsActive( unsigned int pFlags) const;
121 
122     // -------------------------------------------------------------------
123     // Execute step on a given scene
124     void Execute( aiScene* pScene);
125 
126     // -------------------------------------------------------------------
127     // Setup properties prior to executing the process
128     void SetupProperties(const Importer* pImp);
129 
130 private:
131 
132     bool configSpeedFlag;
133 
134 }; // ! end class FindInstancesProcess
135 }  // ! end namespace Assimp
136 
137 #endif // !! AI_FINDINSTANCES_H_INC
138