Skip to content

Commit

Permalink
Changed triangle vertex precision from float to double in multiple su…
Browse files Browse the repository at this point in the history
…bsystems to fix rare problems; Updated FocalEngine; Finalized new multithreading in the MeasurementGrid class; .RUG files now contain double-precision representation of vertices.
  • Loading branch information
Azzinoth committed May 10, 2024
1 parent e76328e commit db5d37a
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 149 deletions.
30 changes: 13 additions & 17 deletions SubSystems/ComplexityCore/ComplexityMetricInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ double ComplexityMetricInfo::GetTotalArea()
return TotalArea;
}

void ComplexityMetricInfo::FillTrianglesData(std::vector<float>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals)
void ComplexityMetricInfo::FillTrianglesData(std::vector<double>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals)
{
MeshData.Vertices = Vertices;
MeshData.Colors = Colors;
Expand All @@ -23,38 +23,40 @@ void ComplexityMetricInfo::FillTrianglesData(std::vector<float>& Vertices, std::
TrianglesCentroids.clear();
TotalArea = 0.0;

std::vector<glm::vec3> Triangle;
std::vector<glm::dvec3> Triangle;
Triangle.resize(3);
std::vector<glm::vec3> TriangleNormal;
TriangleNormal.resize(3);

for (size_t i = 0; i < Indices.size(); i += 3)
{
int VertexPosition = Indices[i] * 3;
Triangle[0] = glm::vec3(Vertices[VertexPosition], Vertices[VertexPosition + 1], Vertices[VertexPosition + 2]);
Triangle[0] = glm::dvec3(Vertices[VertexPosition], Vertices[VertexPosition + 1], Vertices[VertexPosition + 2]);

VertexPosition = Indices[i + 1] * 3;
Triangle[1] = glm::vec3(Vertices[VertexPosition], Vertices[VertexPosition + 1], Vertices[VertexPosition + 2]);
Triangle[1] = glm::dvec3(Vertices[VertexPosition], Vertices[VertexPosition + 1], Vertices[VertexPosition + 2]);

VertexPosition = Indices[i + 2] * 3;
Triangle[2] = glm::vec3(Vertices[VertexPosition], Vertices[VertexPosition + 1], Vertices[VertexPosition + 2]);
Triangle[2] = glm::dvec3(Vertices[VertexPosition], Vertices[VertexPosition + 1], Vertices[VertexPosition + 2]);

Triangles.push_back(Triangle);
TrianglesArea.push_back(GEOMETRY.CalculateTriangleArea(Triangle[0], Triangle[1], Triangle[2]));
TotalArea += static_cast<float>(TrianglesArea.back());
TotalArea += TrianglesArea.back();

TrianglesCentroids.push_back((Triangle[0] + Triangle[1] + Triangle[2]) / 3.0f);
TrianglesCentroids.push_back((Triangle[0] + Triangle[1] + Triangle[2]) / 3.0);

if (!Normals.empty())
{
VertexPosition = Indices[i] * 3;
Triangle[0] = glm::vec3(Normals[VertexPosition], Normals[VertexPosition + 1], Normals[VertexPosition + 2]);
TriangleNormal[0] = glm::vec3(Normals[VertexPosition], Normals[VertexPosition + 1], Normals[VertexPosition + 2]);

VertexPosition = Indices[i + 1] * 3;
Triangle[1] = glm::vec3(Normals[VertexPosition], Normals[VertexPosition + 1], Normals[VertexPosition + 2]);
TriangleNormal[1] = glm::vec3(Normals[VertexPosition], Normals[VertexPosition + 1], Normals[VertexPosition + 2]);

VertexPosition = Indices[i + 2] * 3;
Triangle[2] = glm::vec3(Normals[VertexPosition], Normals[VertexPosition + 1], Normals[VertexPosition + 2]);
TriangleNormal[2] = glm::vec3(Normals[VertexPosition], Normals[VertexPosition + 1], Normals[VertexPosition + 2]);

TrianglesNormals.push_back(Triangle);
TrianglesNormals.push_back(TriangleNormal);
}
}

Expand Down Expand Up @@ -199,12 +201,6 @@ void MeshLayer::FillRawData()
if (ParentComplexityMetricData == nullptr || TrianglesToData.empty())
return;

std::vector<float> PositionsVector;
for (size_t i = 0; i < ParentComplexityMetricData->MeshData.Vertices.size(); i++)
{
PositionsVector.push_back(ParentComplexityMetricData->MeshData.Vertices[i]);
}

std::vector<int> IndexVector;
for (size_t i = 0; i < ParentComplexityMetricData->MeshData.Indices.size(); i++)
{
Expand Down
10 changes: 4 additions & 6 deletions SubSystems/ComplexityCore/ComplexityMetricInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MeshLayer
// For purposes of complexity metric storing of all of raw data is redundant,but it is needed for saving RUG file.
struct RawMeshData
{
std::vector<float> Vertices;
std::vector<double> Vertices;
std::vector<float> Colors;
std::vector<float> UVs;
std::vector<float> Tangents;
Expand Down Expand Up @@ -148,15 +148,13 @@ class ComplexityMetricInfo
std::vector<int> TriangleSelected;

RawMeshData MeshData;

std::vector<std::vector<glm::vec3>> Triangles;
std::vector<std::vector<glm::dvec3>> Triangles;
std::vector<double> TrianglesArea;

std::vector<std::vector<glm::vec3>> TrianglesNormals;
std::vector<glm::vec3> TrianglesCentroids;

void FillTrianglesData(std::vector<float>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals);
std::vector<glm::dvec3> TrianglesCentroids;

void FillTrianglesData(std::vector<double>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals);

glm::vec3 GetAverageNormal();
void UpdateAverageNormal();
Expand Down
6 changes: 3 additions & 3 deletions SubSystems/ComplexityCore/ComplexityMetricManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ComplexityMetricManager* ComplexityMetricManager::Instance = nullptr;
ComplexityMetricManager::ComplexityMetricManager() {}
ComplexityMetricManager::~ComplexityMetricManager() {}

void ComplexityMetricManager::Init(std::vector<float>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals)
void ComplexityMetricManager::Init(std::vector<double>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals)
{
if (ActiveComplexityMetricInfo != nullptr)
delete ActiveComplexityMetricInfo;
Expand Down Expand Up @@ -34,7 +34,7 @@ void ComplexityMetricManager::ImportOBJ(const char* FileName, bool bForceOneMesh

if (FirstObject != nullptr)
{
COMPLEXITY_METRIC_MANAGER.Init(FirstObject->FVerC, FirstObject->fColorsC, FirstObject->FTexC, FirstObject->FTanC, FirstObject->FInd, FirstObject->FNorC);
COMPLEXITY_METRIC_MANAGER.Init(FirstObject->DVerC, FirstObject->FColorsC, FirstObject->FTexC, FirstObject->FTanC, FirstObject->FInd, FirstObject->FNorC);
}

for (size_t i = 0; i < ClientLoadCallbacks.size(); i++)
Expand Down Expand Up @@ -71,7 +71,7 @@ void ComplexityMetricManager::SaveToRUGFile(std::string FilePath)

int Count = static_cast<int>(ActiveComplexityMetricInfo->MeshData.Vertices.size());
File.write((char*)&Count, sizeof(int));
File.write((char*)ActiveComplexityMetricInfo->MeshData.Vertices.data(), sizeof(float) * Count);
File.write((char*)ActiveComplexityMetricInfo->MeshData.Vertices.data(), sizeof(double) * Count);

Count = static_cast<int>(ActiveComplexityMetricInfo->MeshData.Colors.size());
File.write((char*)&Count, sizeof(int));
Expand Down
4 changes: 2 additions & 2 deletions SubSystems/ComplexityCore/ComplexityMetricManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace FocalEngine;

#include "../EngineInclude.h"

#define APP_VERSION 0.62f
#define APP_VERSION 0.87f

const COMDLG_FILTERSPEC RUGOSITY_LOAD_FILE_FILTER[] =
{
Expand All @@ -23,7 +23,7 @@ class ComplexityMetricManager

ComplexityMetricInfo* ActiveComplexityMetricInfo = nullptr;

void Init(std::vector<float>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals);
void Init(std::vector<double>& Vertices, std::vector<float>& Colors, std::vector<float>& UVs, std::vector<float>& Tangents, std::vector<int>& Indices, std::vector<float>& Normals);
void ImportOBJ(const char* FileName, bool bForceOneMesh);

void AddLoadCallback(std::function<void()> Func);
Expand Down
9 changes: 5 additions & 4 deletions SubSystems/ComplexityCore/JitterManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void JitterManager::GatherCalcualtionThreadWork(void* OutputData)
float TimePerStep = float(TimeDifferenceInMS) / float(JITTER_MANAGER.TotalJitterIndex);

// For smoothing the time to finish.
JITTER_MANAGER.LastApproximationsOfTimeToFinish[JITTER_MANAGER.CurrentApproximationOfTimeToFinishIndex] = TimePerStep * StepsLeft;
JITTER_MANAGER.LastApproximationsOfTimeToFinish[JITTER_MANAGER.CurrentApproximationOfTimeToFinishIndex] = static_cast<uint64_t>(TimePerStep * StepsLeft);

JITTER_MANAGER.CurrentApproximationOfTimeToFinishIndex++;
if (JITTER_MANAGER.CurrentApproximationOfTimeToFinishIndex >= JITTER_MANAGER.LastApproximationsOfTimeToFinish.size())
Expand All @@ -126,7 +126,7 @@ void JitterManager::GatherCalcualtionThreadWork(void* OutputData)

int JitterManager::GetTimeToFinishInSeconds()
{
return JITTER_MANAGER.ApproximateTimeToFinishInMS / 1000;
return static_cast<int>(JITTER_MANAGER.ApproximateTimeToFinishInMS / 1000);
}

std::string JitterManager::GetTimeToFinishFormated()
Expand Down Expand Up @@ -292,7 +292,9 @@ void JitterManager::RunNextJitter()
THREAD_COUNT = THREAD_POOL.GetThreadCount() * 10;
// It shouuld be based on complexity of the algorithm
// not only triangle count.
THREAD_COUNT = TriangleCount / 10000.0;
THREAD_COUNT = static_cast<int>(TriangleCount / 10000.0);
if (THREAD_COUNT < 1)
THREAD_COUNT = 1;

int NumberOfTrianglesPerThread = static_cast<int>(TriangleCount / THREAD_COUNT);

Expand Down Expand Up @@ -422,7 +424,6 @@ void JitterManager::MoveResultDataFromGrid(MeasurementGrid* Grid)
Result[i] += Grid->TrianglesUserData[i];
CorrectValuesCounters[i]++;
}

}

if (JITTER_MANAGER.JitterDoneCount == JITTER_MANAGER.JitterToDoCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ double FractalDimensionLayerProducer::RunOnAllInternalNodesWithTriangles(GridNod
// Iterate through all the triangles
for (size_t j = 0; j < OuterNode->TrianglesInCell.size(); j++)
{
std::vector<glm::vec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[OuterNode->TrianglesInCell[j]];
std::vector<glm::dvec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[OuterNode->TrianglesInCell[j]];

// Calculate the grid cells that the triangle intersects or is contained in
FEAABB TriangleBBox = FEAABB(CurrentTriangle);
Expand Down
35 changes: 22 additions & 13 deletions SubSystems/ComplexityCore/Layers/RugosityLayerProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void RugosityLayerProducer::CalculateOneNodeRugosity(GridNode* CurrentNode)
if (COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->TrianglesArea[CurrentNode->TrianglesInCell[i]] == 0.0)
continue;

std::vector<glm::vec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[i]];
std::vector<glm::dvec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[i]];
for (size_t j = 0; j < CurrentTriangle.size(); j++)
{
ProjectedPoints.push_back(RUGOSITY_LAYER_PRODUCER.ProjectPointOntoPlane(Point_3(CurrentTriangle[j].x, CurrentTriangle[j].y, CurrentTriangle[j].z), PlaneToProjectOnto));
Expand All @@ -175,7 +175,7 @@ void RugosityLayerProducer::CalculateOneNodeRugosity(GridNode* CurrentNode)
if (COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->TrianglesArea[CurrentNode->TrianglesInCell[i]] == 0.0)
continue;

std::vector<glm::vec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[i]];
std::vector<glm::dvec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[i]];

Polygon_2 TempTriangle;

Expand Down Expand Up @@ -268,7 +268,7 @@ void RugosityLayerProducer::CalculateOneNodeRugosity(GridNode* CurrentNode)

for (int l = 0; l < CurrentNode->TrianglesInCell.size(); l++)
{
std::vector<glm::vec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[l]];
std::vector<glm::dvec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[l]];

double ProjectionArea = 0.0;
double OriginalArea = 0.0;
Expand All @@ -285,15 +285,24 @@ void RugosityLayerProducer::CalculateOneNodeRugosity(GridNode* CurrentNode)
}
else
{
std::vector<Point_2> ProjectedPoints;
for (size_t j = 0; j < CurrentTriangle.size(); j++)
OriginalArea = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->TrianglesArea[CurrentNode->TrianglesInCell[l]];

try
{
ProjectedPoints.push_back(RUGOSITY_LAYER_PRODUCER.ProjectPointOntoPlane(Point_3(CurrentTriangle[j].x, CurrentTriangle[j].y, CurrentTriangle[j].z), PlaneToProjectOnto));
}
std::vector<Point_2> ProjectedPoints;
for (size_t j = 0; j < CurrentTriangle.size(); j++)
{
ProjectedPoints.push_back(RUGOSITY_LAYER_PRODUCER.ProjectPointOntoPlane(Point_3(CurrentTriangle[j].x, CurrentTriangle[j].y, CurrentTriangle[j].z), PlaneToProjectOnto));
}

ProjectionArea = abs(CGAL::to_double(CGAL::area(ProjectedPoints[0], ProjectedPoints[1], ProjectedPoints[2])));
OriginalArea = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->TrianglesArea[CurrentNode->TrianglesInCell[l]];
Rugosities.push_back(OriginalArea / ProjectionArea);
ProjectionArea = abs(CGAL::to_double(CGAL::area(ProjectedPoints[0], ProjectedPoints[1], ProjectedPoints[2])));
}
catch (...)
{
ProjectionArea = OriginalArea;
}

Rugosities.push_back(static_cast<float>(OriginalArea / ProjectionArea));
}

if (OriginalArea == 0.0 || ProjectionArea == 0.0 || OriginalArea < FLT_EPSILON || ProjectionArea < FLT_EPSILON)
Expand Down Expand Up @@ -324,7 +333,7 @@ void RugosityLayerProducer::CalculateOneNodeRugosity(GridNode* CurrentNode)

if (RUGOSITY_LAYER_PRODUCER.bUseCGALVariant)
{
std::vector<float> FEVerticesFinal;
std::vector<double> FEVerticesFinal;
std::vector<int> FEIndicesFinal;

for (int l = 0; l < CurrentNode->TrianglesInCell.size(); l++)
Expand Down Expand Up @@ -395,7 +404,7 @@ void RugosityLayerProducer::CalculateOneNodeRugosity(GridNode* CurrentNode)
// ******* Getting average normal *******
for (size_t l = 0; l < CurrentNode->TrianglesInCell.size(); l++)
{
std::vector<glm::vec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[l]];
std::vector<glm::dvec3> CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentNode->TrianglesInCell[l]];
std::vector<glm::vec3> CurrentTriangleNormals = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->TrianglesNormals[CurrentNode->TrianglesInCell[l]];

if (RUGOSITY_LAYER_PRODUCER.bWeightedNormals)
Expand Down Expand Up @@ -654,7 +663,7 @@ void RugosityLayerProducer::RenderDebugInfoForSelectedNode(MeasurementGrid* Grid
{
const auto CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentlySelectedCell->TrianglesInCell[i]];

std::vector<glm::vec3> TranformedTrianglePoints = CurrentTriangle;
std::vector<glm::dvec3> TranformedTrianglePoints = CurrentTriangle;
for (size_t j = 0; j < TranformedTrianglePoints.size(); j++)
{
TranformedTrianglePoints[j] = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Position->GetTransformMatrix() * glm::vec4(TranformedTrianglePoints[j], 1.0f);
Expand Down
14 changes: 7 additions & 7 deletions SubSystems/ComplexityCore/Layers/TriangleEdgeLayerProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ MeshLayer TriangleEdgeLayerProducer::Calculate(int Mode)
uint64_t StartTime = TIME.GetTimeStamp(FE_TIME_RESOLUTION_NANOSECONDS);
for (size_t i = 0; i < COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles.size(); i++)
{
float Edge0Length = glm::distance(COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][0], COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][1]);
float Edge1Length = glm::distance(COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][1], COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][2]);
float Edge2Length = glm::distance(COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][2], COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][0]);
double Edge0Length = glm::distance(COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][0], COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][1]);
double Edge1Length = glm::distance(COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][1], COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][2]);
double Edge2Length = glm::distance(COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][2], COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[i][0]);

if (Mode == 0)
{
Result.TrianglesToData.push_back(std::max(Edge0Length, std::max(Edge1Length, Edge2Length)));
Result.TrianglesToData.push_back(static_cast<float>(std::max(Edge0Length, std::max(Edge1Length, Edge2Length))));
}
else if (Mode == 1)
{
Result.TrianglesToData.push_back(std::min(Edge0Length, std::min(Edge1Length, Edge2Length)));
Result.TrianglesToData.push_back(static_cast<float>(std::min(Edge0Length, std::min(Edge1Length, Edge2Length))));
}
else if (Mode == 2)
{
Result.TrianglesToData.push_back((Edge0Length + Edge1Length + Edge2Length) / 3.0f);
Result.TrianglesToData.push_back(static_cast<float>((Edge0Length + Edge1Length + Edge2Length) / 3.0));
}
else
{
Result.TrianglesToData.push_back((Edge0Length + Edge1Length + Edge2Length) / 3.0f);
Result.TrianglesToData.push_back(static_cast<float>((Edge0Length + Edge1Length + Edge2Length) / 3.0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void VectorDispersionLayerProducer::RenderDebugInfoForSelectedNode(MeasurementGr
{
const auto CurrentTriangle = COMPLEXITY_METRIC_MANAGER.ActiveComplexityMetricInfo->Triangles[CurrentlySelectedCell->TrianglesInCell[i]];

std::vector<glm::vec3> TranformedTrianglePoints = CurrentTriangle;
std::vector<glm::dvec3> TranformedTrianglePoints = CurrentTriangle;
for (size_t j = 0; j < TranformedTrianglePoints.size(); j++)
{
TranformedTrianglePoints[j] = MESH_MANAGER.ActiveEntity->Transform.GetTransformMatrix() * glm::vec4(TranformedTrianglePoints[j], 1.0f);
Expand Down
Loading

0 comments on commit db5d37a

Please sign in to comment.