/src/assimp/code/PostProcessing/GenBoundingBoxesProcess.cpp
Line | Count | Source |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2026, assimp team |
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 following |
12 | | 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 | | #ifndef ASSIMP_BUILD_NO_GENBOUNDINGBOXES_PROCESS |
43 | | |
44 | | #include "PostProcessing/GenBoundingBoxesProcess.h" |
45 | | |
46 | | #include <assimp/postprocess.h> |
47 | | #include <assimp/scene.h> |
48 | | |
49 | | #include <limits> |
50 | | |
51 | | namespace Assimp { |
52 | | |
53 | 119k | bool GenBoundingBoxesProcess::IsActive(unsigned int pFlags) const { |
54 | 119k | return 0 != ( pFlags & aiProcess_GenBoundingBoxes ); |
55 | 119k | } |
56 | | |
57 | 10.7k | void checkMesh(aiMesh* mesh, aiVector3D& min, aiVector3D& max) { |
58 | 10.7k | ai_assert(nullptr != mesh); |
59 | | |
60 | 10.7k | if (0 == mesh->mNumVertices) { |
61 | 0 | return; |
62 | 0 | } |
63 | | |
64 | 32.4M | for (unsigned int i = 0; i < mesh->mNumVertices; ++i) { |
65 | 32.4M | const aiVector3D &pos = mesh->mVertices[i]; |
66 | 32.4M | if (pos.x < min.x) { |
67 | 24.9k | min.x = pos.x; |
68 | 24.9k | } |
69 | 32.4M | if (pos.y < min.y) { |
70 | 28.9k | min.y = pos.y; |
71 | 28.9k | } |
72 | 32.4M | if (pos.z < min.z) { |
73 | 21.6k | min.z = pos.z; |
74 | 21.6k | } |
75 | | |
76 | 32.4M | if (pos.x > max.x) { |
77 | 24.5k | max.x = pos.x; |
78 | 24.5k | } |
79 | 32.4M | if (pos.y > max.y) { |
80 | 24.9k | max.y = pos.y; |
81 | 24.9k | } |
82 | 32.4M | if (pos.z > max.z) { |
83 | 23.0k | max.z = pos.z; |
84 | 23.0k | } |
85 | 32.4M | } |
86 | 10.7k | } |
87 | | |
88 | 3.28k | void GenBoundingBoxesProcess::Execute(aiScene* pScene) { |
89 | 3.28k | if (nullptr == pScene) { |
90 | 0 | return; |
91 | 0 | } |
92 | | |
93 | 14.4k | for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { |
94 | 11.1k | aiMesh* mesh = pScene->mMeshes[i]; |
95 | 11.1k | if (nullptr == mesh || 0 == mesh->mNumVertices) { |
96 | 451 | continue; |
97 | 451 | } |
98 | | |
99 | 10.7k | constexpr ai_real kMaxVal = std::numeric_limits<ai_real>::max(); |
100 | 10.7k | aiVector3D min(kMaxVal, kMaxVal, kMaxVal), max(-kMaxVal, -kMaxVal, -kMaxVal); |
101 | 10.7k | checkMesh(mesh, min, max); |
102 | 10.7k | mesh->mAABB.mMin = min; |
103 | 10.7k | mesh->mAABB.mMax = max; |
104 | 10.7k | } |
105 | 3.28k | } |
106 | | |
107 | | } // Namespace Assimp |
108 | | |
109 | | #endif // ASSIMP_BUILD_NO_GENBOUNDINGBOXES_PROCESS |