/src/assimp/code/Geometry/GeometryUtils.cpp
Line | Count | Source |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2025, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | #include "GeometryUtils.h" |
43 | | |
44 | | #include <assimp/vector3.h> |
45 | | |
46 | | namespace Assimp { |
47 | | |
48 | | // ------------------------------------------------------------------------------------------------ |
49 | 930k | ai_real GeometryUtils::heron( ai_real a, ai_real b, ai_real c ) { |
50 | 930k | const ai_real s = (a + b + c) * 0.5; |
51 | 930k | const ai_real area = pow((s * ( s - a ) * ( s - b ) * ( s - c ) ), static_cast<ai_real>(0.5)); |
52 | 930k | return area; |
53 | 930k | } |
54 | | |
55 | | // ------------------------------------------------------------------------------------------------ |
56 | 2.79M | ai_real GeometryUtils::distance3D( const aiVector3D &vA, const aiVector3D &vB ) { |
57 | 2.79M | const ai_real lx = ( vB.x - vA.x ); |
58 | 2.79M | const ai_real ly = ( vB.y - vA.y ); |
59 | 2.79M | const ai_real lz = ( vB.z - vA.z ); |
60 | 2.79M | const ai_real a = lx*lx + ly*ly + lz*lz; |
61 | 2.79M | const ai_real d = pow( a, static_cast<ai_real>(0.5)); |
62 | | |
63 | 2.79M | return d; |
64 | 2.79M | } |
65 | | |
66 | | // ------------------------------------------------------------------------------------------------ |
67 | 930k | ai_real GeometryUtils::calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh ) { |
68 | 930k | ai_real area = 0; |
69 | 930k | if (mesh == nullptr) { |
70 | 0 | return area; |
71 | 0 | } |
72 | | |
73 | 930k | const aiVector3D vA( mesh->mVertices[ face.mIndices[ 0 ] ] ); |
74 | 930k | const aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] ); |
75 | 930k | const aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] ); |
76 | | |
77 | 930k | const ai_real a = distance3D( vA, vB ); |
78 | 930k | const ai_real b = distance3D( vB, vC ); |
79 | 930k | const ai_real c = distance3D( vC, vA ); |
80 | 930k | area = heron( a, b, c ); |
81 | | |
82 | 930k | return area; |
83 | 930k | } |
84 | | |
85 | | // ------------------------------------------------------------------------------------------------ |
86 | | // Check whether a ray intersects a plane and find the intersection point |
87 | 0 | bool GeometryUtils::PlaneIntersect(const aiRay& ray, const aiVector3D& planePos, const aiVector3D& planeNormal, aiVector3D& pos) { |
88 | 0 | const ai_real b = planeNormal * (planePos - ray.pos); |
89 | 0 | ai_real h = ray.dir * planeNormal; |
90 | 0 | if ((h < 10e-5 && h > -10e-5) || (h = b/h) < 0) |
91 | 0 | return false; |
92 | | |
93 | 0 | pos = ray.pos + (ray.dir * h); |
94 | 0 | return true; |
95 | 0 | } |
96 | | |
97 | | // ------------------------------------------------------------------------------------------------ |
98 | 0 | void GeometryUtils::normalizeVectorArray(aiVector3D *vectorArrayIn, aiVector3D *vectorArrayOut, size_t numVectors) { |
99 | 0 | if (vectorArrayIn == nullptr || vectorArrayOut == nullptr) { |
100 | 0 | return; |
101 | 0 | } |
102 | | |
103 | 0 | for (size_t i=0; i<numVectors; ++i) { |
104 | 0 | vectorArrayOut[i] = vectorArrayIn[i].Normalize(); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | } // namespace Assimp |