Skip to content

Commit

Permalink
BUG: Fixes issue where any component shape was allowed for ComputeTri…
Browse files Browse the repository at this point in the history
…angleGeometrySizes (BlueQuartzSoftware#1134)

* BUG: Fixes issue where any component shape were allowed for ComputeTriangleGeometrySizes.cpp
* Fix all instances of "Face Labels" are only allowed to use 2 component input arrays
* ComputeTriangleGeomVolumes - fix possible walking off the end of the array.
	A negative value for the face label when used as an index will cast into an unsigned value and bad things are going to happen.
* BUG: Fixes negative volumes produced from calculations

---------

Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Jan 16, 2025
1 parent 218e0c9 commit 86e9e6d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,64 @@
#include "simplnx/Parameters/DataGroupSelectionParameter.hpp"
#include "simplnx/Parameters/DataObjectNameParameter.hpp"
#include "simplnx/Parameters/GeometrySelectionParameter.hpp"
#include "simplnx/Utilities/GeometryUtilities.hpp"
#include "simplnx/Utilities/Math/MatrixMath.hpp"
#include "simplnx/Utilities/ParallelDataAlgorithm.hpp"

#include "simplnx/Utilities/SIMPLConversion.hpp"

#include "simplnx/Utilities/ParallelDataAlgorithm.hpp"

using namespace nx::core;

namespace
{
constexpr nx::core::int32 k_MissingFeatureAttributeMatrix = -75769;

/**
* @brief The CalculateAreasImpl class implements a threaded algorithm that computes the area of each
* triangle for a set of triangles
*/
class CalculateAreasImpl
{
public:
CalculateAreasImpl(const TriangleGeom* triangleGeom, Float64AbstractDataStore& areas, const std::atomic_bool& shouldCancel)
: m_TriangleGeom(triangleGeom)
, m_Areas(areas)
, m_ShouldCancel(shouldCancel)
{
}
virtual ~CalculateAreasImpl() = default;

void convert(size_t start, size_t end) const
{
std::array<float, 3> cross = {0.0f, 0.0f, 0.0f};
for(size_t triangleIndex = start; triangleIndex < end; triangleIndex++)
{
if(m_ShouldCancel)
{
break;
}
std::array<Point3Df, 3> vertCoords;
m_TriangleGeom->getFaceCoordinates(triangleIndex, vertCoords);

auto vecA = (vertCoords[0] - vertCoords[1]).toArray();
auto vecB = (vertCoords[0] - vertCoords[2]).toArray();

MatrixMath::CrossProduct(vecA.data(), vecB.data(), cross.data());

m_Areas[triangleIndex] = 0.5F * MatrixMath::Magnitude3x1(cross.data());
}
}

void operator()(const Range& range) const
{
convert(range.min(), range.max());
}

private:
const TriangleGeom* m_TriangleGeom = nullptr;
Float64AbstractDataStore& m_Areas;
const std::atomic_bool& m_ShouldCancel;
};
} // namespace

namespace nx::core
Expand Down Expand Up @@ -126,7 +173,12 @@ Result<> ComputeTriangleAreasFilter::executeImpl(DataStructure& dataStructure, c
DataPath pCalculatedAreasDataPath = pTriangleGeometryDataPath.createChildPath(faceAttributeMatrix->getName()).createChildPath(pCalculatedAreasName);
auto& faceAreas = dataStructure.getDataAs<Float64Array>(pCalculatedAreasDataPath)->getDataStoreRef();

return nx::core::GeometryUtilities::ComputeTriangleAreas(triangleGeom, faceAreas, shouldCancel);
// Parallel algorithm to find duplicate nodes
ParallelDataAlgorithm dataAlg;
dataAlg.setRange(0ULL, static_cast<size_t>(triangleGeom->getNumberOfFaces()));
dataAlg.execute(CalculateAreasImpl(triangleGeom, faceAreas, shouldCancel));

return {};
}

namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "simplnx/Parameters/DataGroupSelectionParameter.hpp"
#include "simplnx/Parameters/DataObjectNameParameter.hpp"
#include "simplnx/Parameters/GeometrySelectionParameter.hpp"
#include "simplnx/Utilities/GeometryUtilities.hpp"
#include "simplnx/Utilities/Math/MatrixMath.hpp"
#include "simplnx/Utilities/ParallelDataAlgorithm.hpp"
#include "simplnx/Utilities/SIMPLConversion.hpp"
Expand All @@ -19,6 +18,56 @@ namespace

constexpr nx::core::int32 k_MissingFeatureAttributeMatrix = -75969;

/**
* @brief The CalculateAreasImpl class implements a threaded algorithm that computes the normal of each
* triangle for a set of triangles
*/
class CalculateNormalsImpl
{
public:
CalculateNormalsImpl(const TriangleGeom* triangleGeom, Float64AbstractDataStore& normals, const std::atomic_bool& shouldCancel)
: m_TriangleGeom(triangleGeom)
, m_Normals(normals)
, m_ShouldCancel(shouldCancel)
{
}
virtual ~CalculateNormalsImpl() = default;

void generate(size_t start, size_t end) const
{
std::array<float32, 3> normal = {0.0f, 0.0f, 0.0f};
for(size_t triangleIndex = start; triangleIndex < end; triangleIndex++)
{

if(m_ShouldCancel)
{
break;
}
std::array<Point3Df, 3> vertCoords;
m_TriangleGeom->getFaceCoordinates(triangleIndex, vertCoords);

auto vecA = (vertCoords[1] - vertCoords[0]).toArray();
auto vecB = (vertCoords[2] - vertCoords[0]).toArray();

MatrixMath::CrossProduct(vecA.data(), vecB.data(), normal.data());
MatrixMath::Normalize3x1(normal.data());

m_Normals[triangleIndex * 3] = static_cast<float64>(normal[0]);
m_Normals[triangleIndex * 3 + 1] = static_cast<float64>(normal[1]);
m_Normals[triangleIndex * 3 + 2] = static_cast<float64>(normal[2]);
}
}

void operator()(const Range& range) const
{
generate(range.min(), range.max());
}

private:
const TriangleGeom* m_TriangleGeom = nullptr;
Float64AbstractDataStore& m_Normals;
const std::atomic_bool& m_ShouldCancel;
};
} // namespace

namespace nx::core
Expand Down Expand Up @@ -123,7 +172,12 @@ Result<> TriangleNormalFilter::executeImpl(DataStructure& dataStructure, const A
DataPath pNormalsArrayPath = pTriangleGeometryDataPath.createChildPath(faceAttributeMatrix->getName()).createChildPath(pNormalsName);
auto& normals = dataStructure.getDataAs<Float64Array>(pNormalsArrayPath)->getDataStoreRef();

return nx::core::GeometryUtilities::ComputeTriangleNormals(triangleGeom, normals, shouldCancel);
// Parallel algorithm to find duplicate nodes
ParallelDataAlgorithm dataAlg;
dataAlg.setRange(0ULL, static_cast<size_t>(triangleGeom->getNumberOfFaces()));
dataAlg.execute(CalculateNormalsImpl(triangleGeom, normals, shouldCancel));

return {};
}

namespace
Expand Down
1 change: 0 additions & 1 deletion src/simplnx/Utilities/GeometryUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "simplnx/Common/Array.hpp"
#include "simplnx/Common/Result.hpp"
#include "simplnx/Utilities/Math/MatrixMath.hpp"

#include <cstdio>

Expand Down

0 comments on commit 86e9e6d

Please sign in to comment.