Skip to content

Commit

Permalink
Merge pull request assimp#5086 from assimp/kimkulling/refactoring_geo…
Browse files Browse the repository at this point in the history
…utils

Kimkulling/refactoring geoutils
kimkulling authored May 4, 2023
2 parents b887779 + 8cbfb44 commit cb22d53
Showing 31 changed files with 624 additions and 718 deletions.
4 changes: 0 additions & 4 deletions code/AssetLib/3DS/3DSHelper.h
Original file line number Diff line number Diff line change
@@ -397,10 +397,6 @@ struct Material {

Material(const Material &other) = default;

Material(Material &&other) AI_NO_EXCEPT = default;

Material &operator=(Material &&other) AI_NO_EXCEPT = default;

virtual ~Material() = default;

//! Name of the material
3 changes: 2 additions & 1 deletion code/AssetLib/IFC/IFCUtil.cpp
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "AssetLib/IFC/IFCUtil.h"
#include "Common/PolyTools.h"
#include "Geometry/GeometryUtils.h"
#include "PostProcessing/ProcessHelper.h"

namespace Assimp {
@@ -235,7 +236,7 @@ IfcVector3 TempMesh::ComputeLastPolygonNormal(bool normalize) const {
struct CompareVector {
bool operator () (const IfcVector3& a, const IfcVector3& b) const {
IfcVector3 d = a - b;
IfcFloat eps = ai_epsilon;
constexpr IfcFloat eps = ai_epsilon;
return d.x < -eps || (std::abs(d.x) < eps && d.y < -eps) || (std::abs(d.x) < eps && std::abs(d.y) < eps && d.z < -eps);
}
};
6 changes: 2 additions & 4 deletions code/AssetLib/LWO/LWOLoader.cpp
Original file line number Diff line number Diff line change
@@ -5,8 +5,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2022, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@@ -51,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "AssetLib/LWO/LWOLoader.h"
#include "PostProcessing/ConvertToLHProcess.h"
#include "PostProcessing/ProcessHelper.h"
#include "Geometry/GeometryUtils.h"

#include <assimp/ByteSwapper.h>
#include <assimp/SGSpatialSort.h>
@@ -528,7 +527,6 @@ void LWOImporter::ComputeNormals(aiMesh *mesh, const std::vector<unsigned int> &
continue;
vNormals += v;
}
mesh->mNormals[idx] = vNormals.Normalize();
}
}
}
@@ -549,14 +547,14 @@ void LWOImporter::ComputeNormals(aiMesh *mesh, const std::vector<unsigned int> &
const aiVector3D &v = faceNormals[*a];
vNormals += v;
}
vNormals.Normalize();
for (std::vector<unsigned int>::const_iterator a = poResult.begin(); a != poResult.end(); ++a) {
mesh->mNormals[*a] = vNormals;
vertexDone[*a] = true;
}
}
}
}
GeometryUtils::normalizeVectorArray(mesh->mNormals, mesh->mNormals, mesh->mNumVertices);
}

// ------------------------------------------------------------------------------------------------
2 changes: 0 additions & 2 deletions code/AssetLib/X3D/X3DExporter.hpp
Original file line number Diff line number Diff line change
@@ -58,8 +58,6 @@ class X3DExporter {
Value(value) {
// empty
}

SAttribute(SAttribute &&rhs) AI_NO_EXCEPT = default;
};

/***********************************************/
46 changes: 35 additions & 11 deletions code/Geometry/GeometryUtils.cpp
Original file line number Diff line number Diff line change
@@ -45,35 +45,59 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace Assimp {

// ------------------------------------------------------------------------------------------------
ai_real GeometryUtils::heron( ai_real a, ai_real b, ai_real c ) {
ai_real s = (a + b + c) / 2;
ai_real area = pow((s * ( s - a ) * ( s - b ) * ( s - c ) ), (ai_real)0.5 );
const ai_real s = (a + b + c) / 2;
const ai_real area = pow((s * ( s - a ) * ( s - b ) * ( s - c ) ), (ai_real)0.5 );
return area;
}

ai_real GeometryUtils::distance3D( const aiVector3D &vA, aiVector3D &vB ) {
// ------------------------------------------------------------------------------------------------
ai_real GeometryUtils::distance3D( const aiVector3D &vA, const aiVector3D &vB ) {
const ai_real lx = ( vB.x - vA.x );
const ai_real ly = ( vB.y - vA.y );
const ai_real lz = ( vB.z - vA.z );
ai_real a = lx*lx + ly*ly + lz*lz;
ai_real d = pow( a, (ai_real)0.5 );
const ai_real a = lx*lx + ly*ly + lz*lz;
const ai_real d = pow( a, (ai_real)0.5 );

return d;
}

// ------------------------------------------------------------------------------------------------
ai_real GeometryUtils::calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh ) {
ai_real area = 0;

aiVector3D vA( mesh->mVertices[ face.mIndices[ 0 ] ] );
aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] );
aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] );
const aiVector3D vA( mesh->mVertices[ face.mIndices[ 0 ] ] );
const aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] );
const aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] );

ai_real a( distance3D( vA, vB ) );
ai_real b( distance3D( vB, vC ) );
ai_real c( distance3D( vC, vA ) );
const ai_real a = distance3D( vA, vB );
const ai_real b = distance3D( vB, vC );
const ai_real c = distance3D( vC, vA );
area = heron( a, b, c );

return area;
}

// ------------------------------------------------------------------------------------------------
// Check whether a ray intersects a plane and find the intersection point
bool GeometryUtils::PlaneIntersect(const aiRay& ray, const aiVector3D& planePos,
const aiVector3D& planeNormal, aiVector3D& pos) {
const ai_real b = planeNormal * (planePos - ray.pos);
ai_real h = ray.dir * planeNormal;
if ((h < 10e-5 && h > -10e-5) || (h = b/h) < 0)
return false;

pos = ray.pos + (ray.dir * h);
return true;
}

// ------------------------------------------------------------------------------------------------
void GeometryUtils::normalizeVectorArray(aiVector3D *vectorArrayIn, aiVector3D *vectorArrayOut,
size_t numVectors) {
for (size_t i=0; i<numVectors; ++i) {
vectorArrayOut[i] = vectorArrayIn[i].Normalize();
}
}

} // namespace Assimp
18 changes: 16 additions & 2 deletions code/Geometry/GeometryUtils.h
Original file line number Diff line number Diff line change
@@ -47,21 +47,35 @@ namespace Assimp {
// ---------------------------------------------------------------------------
/// @brief This helper class supports some basic geometry algorithms.
// ---------------------------------------------------------------------------
class GeometryUtils {
class ASSIMP_API GeometryUtils {
public:
static ai_real heron( ai_real a, ai_real b, ai_real c );

/// @brief Will compute the distance between 2 3D-vectors
/// @param vA Vector a.
/// @param vB Vector b.
/// @return The distance.
static ai_real distance3D( const aiVector3D &vA, aiVector3D &vB );
static ai_real distance3D( const aiVector3D &vA, const aiVector3D &vB );

/// @brief Will calculate the area of a triangle described by a aiFace.
/// @param face The face
/// @param mesh The mesh containing the face
/// @return The area.
static ai_real calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh );

/// @brief Will calculate the intersection between a ray and a plane
/// @param ray The ray to test for
/// @param planePos A point on the plane
/// @param planeNormal The plane normal to describe its orientation
/// @param pos The position of the intersection.
/// @return true is an intersection was detected, false if not.
static bool PlaneIntersect(const aiRay& ray, const aiVector3D& planePos, const aiVector3D& planeNormal, aiVector3D& pos);

/// @brief Will normalize an array of vectors.
/// @param vectorArrayIn The incoming arra of vectors.
/// @param vectorArrayOut The normalized vectors.
/// @param numVectors The array size.
static void normalizeVectorArray(aiVector3D *vectorArrayIn, aiVector3D *vectorArrayOut, size_t numVectors);
};

} // namespace Assimp
36 changes: 12 additions & 24 deletions code/PostProcessing/ArmaturePopulate.cpp
Original file line number Diff line number Diff line change
@@ -43,15 +43,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/DefaultLogger.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <iostream>

namespace Assimp {

/// The default class constructor.
ArmaturePopulate::ArmaturePopulate() = default;
static bool IsBoneNode(const aiString &bone_name, std::vector<aiBone *> &bones) {
for (aiBone *bone : bones) {
if (bone->mName == bone_name) {
return true;
}
}

/// The class destructor.
ArmaturePopulate::~ArmaturePopulate() = default;
return false;
}

bool ArmaturePopulate::IsActive(unsigned int pFlags) const {
return (pFlags & aiProcess_PopulateArmatureData) != 0;
@@ -70,17 +73,16 @@ void ArmaturePopulate::Execute(aiScene *out) {
BuildBoneList(out->mRootNode, out->mRootNode, out, bones);
BuildNodeList(out->mRootNode, nodes);

BuildBoneStack(out->mRootNode, out->mRootNode, out, bones, bone_stack, nodes);
BuildBoneStack(out->mRootNode, out, bones, bone_stack, nodes);

ASSIMP_LOG_DEBUG("Bone stack size: ", bone_stack.size());

for (std::pair<aiBone *, aiNode *> kvp : bone_stack) {
aiBone *bone = kvp.first;
aiNode *bone_node = kvp.second;
ASSIMP_LOG_VERBOSE_DEBUG("active node lookup: ", bone->mName.C_Str());

// lcl transform grab - done in generate_nodes :)

// bone->mOffsetMatrix = bone_node->mTransformation;
aiNode *armature = GetArmatureRoot(bone_node, bones);

ai_assert(armature);
@@ -159,8 +161,7 @@ void ArmaturePopulate::BuildNodeList(const aiNode *current_node,
// A bone stack allows us to have multiple armatures, with the same bone names
// A bone stack allows us also to retrieve bones true transform even with
// duplicate names :)
void ArmaturePopulate::BuildBoneStack(aiNode *,
const aiNode *root_node,
void ArmaturePopulate::BuildBoneStack(const aiNode *root_node,
const aiScene*,
const std::vector<aiBone *> &bones,
std::map<aiBone *, aiNode *> &bone_stack,
@@ -196,8 +197,7 @@ void ArmaturePopulate::BuildBoneStack(aiNode *,
// This is required to be detected for a bone initially, it will recurse up
// until it cannot find another bone and return the node No known failure
// points. (yet)
aiNode *ArmaturePopulate::GetArmatureRoot(aiNode *bone_node,
std::vector<aiBone *> &bone_list) {
aiNode *ArmaturePopulate::GetArmatureRoot(aiNode *bone_node, std::vector<aiBone *> &bone_list) {
while (nullptr != bone_node) {
if (!IsBoneNode(bone_node->mName, bone_list)) {
ASSIMP_LOG_VERBOSE_DEBUG("GetArmatureRoot() Found valid armature: ", bone_node->mName.C_Str());
@@ -212,18 +212,6 @@ aiNode *ArmaturePopulate::GetArmatureRoot(aiNode *bone_node,
return nullptr;
}

// Simple IsBoneNode check if this could be a bone
bool ArmaturePopulate::IsBoneNode(const aiString &bone_name,
std::vector<aiBone *> &bones) {
for (aiBone *bone : bones) {
if (bone->mName == bone_name) {
return true;
}
}

return false;
}

// Pop this node by name from the stack if found
// Used in multiple armature situations with duplicate node / bone names
// Known flaw: cannot have nodes with bone names, will be fixed in later release
10 changes: 3 additions & 7 deletions code/PostProcessing/ArmaturePopulate.h
Original file line number Diff line number Diff line change
@@ -69,10 +69,10 @@ namespace Assimp {
class ASSIMP_API ArmaturePopulate : public BaseProcess {
public:
/// The default class constructor.
ArmaturePopulate();
ArmaturePopulate() = default;

/// The class destructor.
virtual ~ArmaturePopulate();
virtual ~ArmaturePopulate() = default;

/// Overwritten, @see BaseProcess
virtual bool IsActive( unsigned int pFlags ) const;
@@ -86,9 +86,6 @@ class ASSIMP_API ArmaturePopulate : public BaseProcess {
static aiNode *GetArmatureRoot(aiNode *bone_node,
std::vector<aiBone *> &bone_list);

static bool IsBoneNode(const aiString &bone_name,
std::vector<aiBone *> &bones);

static aiNode *GetNodeFromStack(const aiString &node_name,
std::vector<aiNode *> &nodes);

@@ -99,7 +96,7 @@ class ASSIMP_API ArmaturePopulate : public BaseProcess {
const aiScene *scene,
std::vector<aiBone *> &bones);

static void BuildBoneStack(aiNode *current_node, const aiNode *root_node,
static void BuildBoneStack(const aiNode *root_node,
const aiScene *scene,
const std::vector<aiBone *> &bones,
std::map<aiBone *, aiNode *> &bone_stack,
@@ -108,5 +105,4 @@ class ASSIMP_API ArmaturePopulate : public BaseProcess {

} // Namespace Assimp


#endif // SCALE_PROCESS_H_
Loading

0 comments on commit cb22d53

Please sign in to comment.