Skip to content

Commit

Permalink
Improve code quality metrics (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardeau authored Feb 8, 2024
1 parent ee426c3 commit cc909d2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 81 deletions.
4 changes: 2 additions & 2 deletions include/Delaunay2dMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace CCCoreLib
//! Returns triangles indexes array (pointer to)
/** Handle with care!
**/
inline int* getTriangleVertIndexesArray() { return m_triIndexes; }
inline int* getTriangleVertIndexesArray() { return m_triIndexes.data(); }

//! Filters out the triangles based on their edge length
/** Warning: may remove ALL triangles!
Expand All @@ -115,7 +115,7 @@ namespace CCCoreLib
GenericIndexedCloud* m_associatedCloud;

//! Triangle vertex indexes
int* m_triIndexes;
std::vector<int> m_triIndexes;

//! Iterator on the list of triangle vertex indexes
int* m_globalIterator;
Expand Down
2 changes: 1 addition & 1 deletion src/CloudSamplingTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ ReferenceCloud* CloudSamplingTools::sorFilter( GenericIndexedCloudPersist* input
for (unsigned i = 0; i < pointCount; ++i)
{
sumDist += meanDistances[i];
sumSquareDist += meanDistances[i] * meanDistances[i];
sumSquareDist += static_cast<double>(meanDistances[i]) * meanDistances[i];
}
avgDist = sumDist / pointCount;
stdDev = sqrt(std::abs(sumSquareDist / pointCount - avgDist*avgDist));
Expand Down
131 changes: 74 additions & 57 deletions src/Delaunay2dMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace CCCoreLib;

Delaunay2dMesh::Delaunay2dMesh()
: m_associatedCloud(nullptr)
, m_triIndexes(nullptr)
, m_triIndexes()
, m_globalIterator(nullptr)
, m_globalIteratorEnd(nullptr)
, m_numberOfTriangles(0)
Expand All @@ -31,8 +31,6 @@ Delaunay2dMesh::Delaunay2dMesh()
Delaunay2dMesh::~Delaunay2dMesh()
{
linkMeshWith(nullptr);

delete[] m_triIndexes;
}

bool Delaunay2dMesh::Available()
Expand Down Expand Up @@ -61,6 +59,10 @@ bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D,
const std::vector<int>& segments2D,
std::string &outputErrorStr )
{
m_numberOfTriangles = 0;
m_triIndexes.clear();
m_globalIteratorEnd = m_globalIterator = nullptr;

#if defined(CC_CORE_LIB_USES_CGAL_LIB)
//CGAL boilerplate
using K = CGAL::Exact_predicates_inexact_constructions_kernel;
Expand All @@ -78,40 +80,49 @@ bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D,
try
{
constraints.reserve(constrCount);
} catch (const std::bad_alloc&)
}
catch (const std::bad_alloc&)
{
outputErrorStr = "Not enough memory";
return false;
};

//We create the Constrained Delaunay Triangulation (CDT)
CDT cdt;
}

//We build the constraints
for(std::size_t i = 0; i < constrCount; ++i)
for (std::size_t i = 0; i < constrCount; ++i)
{
const CCVector2 * pt = &points2D[segments2D[i]];
constraints.emplace_back(cgalPoint(pt->x, pt->y), segments2D[i]);
const CCVector2& pt = points2D[segments2D[i]];
constraints.emplace_back(cgalPoint(pt.x, pt.y), segments2D[i]);
}
//The CDT is built according to the constraints

//We create the Constrained Delaunay Triangulation (CDT)
CDT cdt;
cdt.insert(constraints.begin(), constraints.end());

m_numberOfTriangles = static_cast<unsigned>(cdt.number_of_faces());
m_triIndexes = new int[cdt.number_of_faces() * 3];
try
{
m_triIndexes.resize(cdt.number_of_faces() * 3);
}
catch (const std::bad_alloc&)
{
outputErrorStr = "Not enough memory";
return false;
}

//The cgal data structure is converted into CC one
if (m_numberOfTriangles > 0) {
int faceCount = 0;
if (m_numberOfTriangles != 0)
{
size_t faceCount = 0;
for (CDT::Face_iterator face = cdt.faces_begin(); face != cdt.faces_end(); ++face, faceCount += 3)
{
m_triIndexes[0 + faceCount] = static_cast<int>(face->vertex(0)->info());
m_triIndexes[1 + faceCount] = static_cast<int>(face->vertex(1)->info());
m_triIndexes[2 + faceCount] = static_cast<int>(face->vertex(2)->info());
};
}
}

m_globalIterator = m_triIndexes;
m_globalIteratorEnd = m_triIndexes + 3*m_numberOfTriangles;
m_globalIterator = m_triIndexes.data();
m_globalIteratorEnd = m_globalIterator + 3*m_numberOfTriangles;
return true;
#else
outputErrorStr = "CGAL library not supported";
Expand All @@ -123,6 +134,10 @@ bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D,
std::size_t pointCountToUse,
std::string& outputErrorStr )
{
m_numberOfTriangles = 0;
m_triIndexes.clear();
m_globalIteratorEnd = m_globalIterator = nullptr;

#if defined(CC_CORE_LIB_USES_CGAL_LIB)
//CGAL boilerplate
using K = CGAL::Exact_predicates_inexact_constructions_kernel;
Expand Down Expand Up @@ -155,31 +170,32 @@ bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D,
{
outputErrorStr = "Not enough memory";
return false;
};

m_numberOfTriangles = 0;
if (m_triIndexes)
{
delete[] m_triIndexes;
m_triIndexes = nullptr;
}

for (std::size_t i = 0; i < pointCount; ++i)
{
const CCVector2 * pt = &points2D[i];
pts.emplace_back(cgalPoint(pt->x, pt->y), i);
const CCVector2& pt = points2D[i];
pts.emplace_back(cgalPoint(pt.x, pt.y), i);
}

//The delaunay triangulation is built according to the 2D point cloud
DT dt(pts.begin(), pts.end());

m_numberOfTriangles = static_cast<unsigned >(dt.number_of_faces());
m_triIndexes = new int[dt.number_of_faces()*3];
try
{
m_triIndexes.resize(dt.number_of_faces() * 3);
}
catch (const std::bad_alloc&)
{
outputErrorStr = "Not enough memory";
return false;
}

//The cgal data structure is converted into CC one
if (m_numberOfTriangles > 0)
if (m_numberOfTriangles != 0)
{
int faceCount = 0;
size_t faceCount = 0;
for (DT::Face_iterator face = dt.faces_begin(); face != dt.faces_end(); ++face, faceCount += 3)
{
m_triIndexes[0 + faceCount] = static_cast<int>(face->vertex(0)->info());
Expand All @@ -188,8 +204,8 @@ bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D,
};
}

m_globalIterator = m_triIndexes;
m_globalIteratorEnd = m_triIndexes + 3*m_numberOfTriangles;
m_globalIterator = m_triIndexes.data();
m_globalIteratorEnd = m_globalIterator + 3*m_numberOfTriangles;
return true;
#else
outputErrorStr = "CGAL library not supported";
Expand All @@ -201,7 +217,7 @@ bool Delaunay2dMesh::removeOuterTriangles( const std::vector<CCVector2>& vertice
const std::vector<CCVector2>& polygon2D,
bool removeOutside/*=true*/)
{
if (!m_triIndexes || m_numberOfTriangles == 0)
if (m_triIndexes.empty() || m_numberOfTriangles == 0)
return false;

//we expect the same number of 2D points as the actual number of points in the associated mesh (if any)
Expand All @@ -212,7 +228,7 @@ bool Delaunay2dMesh::removeOuterTriangles( const std::vector<CCVector2>& vertice

//test each triangle center
{
const int* _triIndexes = m_triIndexes;
const int* _triIndexes = m_triIndexes.data();
for (unsigned i = 0; i < m_numberOfTriangles; ++i, _triIndexes += 3)
{
//compute the triangle's barycenter
Expand All @@ -227,7 +243,7 @@ bool Delaunay2dMesh::removeOuterTriangles( const std::vector<CCVector2>& vertice
{
//we keep the corresponding triangle
if (lastValidIndex != i)
memcpy(m_triIndexes + 3 * lastValidIndex, _triIndexes, 3 * sizeof(int));
memcpy(m_triIndexes.data() + 3 * lastValidIndex, _triIndexes, 3 * sizeof(int));
++lastValidIndex;
}
}
Expand All @@ -238,19 +254,19 @@ bool Delaunay2dMesh::removeOuterTriangles( const std::vector<CCVector2>& vertice
if (m_numberOfTriangles)
{
//shouldn't fail as m_numberOfTriangles is smaller!
m_triIndexes = static_cast<int*>(realloc(m_triIndexes, sizeof(int) * 3 * m_numberOfTriangles));
m_triIndexes.resize(3 * m_numberOfTriangles);

//update iterators
m_globalIterator = m_triIndexes.data();
m_globalIteratorEnd = m_globalIterator + 3 * m_numberOfTriangles;
}
else
{
//no triangle left!
delete[] m_triIndexes;
m_triIndexes = nullptr;
m_triIndexes.clear();
m_globalIteratorEnd = m_globalIterator = nullptr;
}

//update iterators
m_globalIterator = m_triIndexes;
m_globalIteratorEnd = m_triIndexes + 3 * m_numberOfTriangles;

return true;
}

Expand All @@ -262,19 +278,19 @@ bool Delaunay2dMesh::removeTrianglesWithEdgesLongerThan(PointCoordinateType maxE
PointCoordinateType squareMaxEdgeLength = maxEdgeLength * maxEdgeLength;

unsigned lastValidIndex = 0;
const int* _triIndexes = m_triIndexes;
const int* _triIndexes = m_triIndexes.data();
for (unsigned i = 0; i < m_numberOfTriangles; ++i, _triIndexes += 3)
{
const CCVector3* A = m_associatedCloud->getPoint(_triIndexes[0]);
const CCVector3* B = m_associatedCloud->getPoint(_triIndexes[1]);
const CCVector3* C = m_associatedCloud->getPoint(_triIndexes[2]);

if ((*B - *A).norm2() <= squareMaxEdgeLength &&
if ( (*B - *A).norm2() <= squareMaxEdgeLength &&
(*C - *A).norm2() <= squareMaxEdgeLength &&
(*C - *B).norm2() <= squareMaxEdgeLength)
(*C - *B).norm2() <= squareMaxEdgeLength )
{
if (lastValidIndex != i)
memcpy(m_triIndexes + 3 * lastValidIndex, _triIndexes, sizeof(int) * 3);
memcpy(m_triIndexes.data() + 3 * lastValidIndex, _triIndexes, sizeof(int) * 3);
++lastValidIndex;
}
}
Expand All @@ -285,15 +301,16 @@ bool Delaunay2dMesh::removeTrianglesWithEdgesLongerThan(PointCoordinateType maxE
if (m_numberOfTriangles != 0)
{
//shouldn't fail as m_numberOfTriangles is smaller than before!
m_triIndexes = static_cast<int*>(realloc(m_triIndexes, sizeof(int) * 3 * m_numberOfTriangles));
m_triIndexes.resize(3 * m_numberOfTriangles);
}
else //no more triangles?!
else
{
delete m_triIndexes;
m_triIndexes = nullptr;
//no triangle left!
m_triIndexes.clear();
m_globalIteratorEnd = m_globalIterator = nullptr;
}
m_globalIterator = m_triIndexes;
m_globalIteratorEnd = m_triIndexes + 3 * m_numberOfTriangles;
m_globalIterator = m_triIndexes.data();
m_globalIteratorEnd = m_globalIterator + 3 * m_numberOfTriangles;
}

return true;
Expand All @@ -306,7 +323,7 @@ void Delaunay2dMesh::forEach(genericTriangleAction action)

SimpleTriangle tri;

const int* _triIndexes = m_triIndexes;
const int* _triIndexes = m_triIndexes.data();
for (unsigned i = 0; i < m_numberOfTriangles; ++i, _triIndexes += 3)
{
tri.A = *m_associatedCloud->getPoint(_triIndexes[0]);
Expand All @@ -318,7 +335,7 @@ void Delaunay2dMesh::forEach(genericTriangleAction action)

void Delaunay2dMesh::placeIteratorAtBeginning()
{
m_globalIterator = m_triIndexes;
m_globalIterator = m_triIndexes.data();
}

GenericTriangle* Delaunay2dMesh::_getNextTriangle()
Expand Down Expand Up @@ -352,7 +369,7 @@ GenericTriangle* Delaunay2dMesh::_getTriangle(unsigned triangleIndex)
{
assert(m_associatedCloud && triangleIndex < m_numberOfTriangles);

const int* tri = m_triIndexes + 3 * triangleIndex;
const int* tri = m_triIndexes.data() + 3 * triangleIndex;
m_associatedCloud->getPoint(*tri++, m_dumpTriangle.A);
m_associatedCloud->getPoint(*tri++, m_dumpTriangle.B);
m_associatedCloud->getPoint(*tri++, m_dumpTriangle.C);
Expand All @@ -364,7 +381,7 @@ void Delaunay2dMesh::getTriangleVertices(unsigned triangleIndex, CCVector3& A, C
{
assert(m_associatedCloud && triangleIndex < m_numberOfTriangles);

const int* tri = m_triIndexes + 3 * triangleIndex;
const int* tri = m_triIndexes.data() + 3 * triangleIndex;
m_associatedCloud->getPoint(*tri++, A);
m_associatedCloud->getPoint(*tri++, B);
m_associatedCloud->getPoint(*tri++, C);
Expand All @@ -374,7 +391,7 @@ VerticesIndexes* Delaunay2dMesh::getTriangleVertIndexes(unsigned triangleIndex)
{
assert(triangleIndex < m_numberOfTriangles);

return reinterpret_cast<VerticesIndexes*>(m_triIndexes + 3*triangleIndex);
return reinterpret_cast<VerticesIndexes*>(m_triIndexes.data() + 3*triangleIndex);
}

void Delaunay2dMesh::getBoundingBox(CCVector3& bbMin, CCVector3& bbMax)
Expand Down
2 changes: 1 addition & 1 deletion src/DistanceComputationTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2924,7 +2924,7 @@ ScalarType DistanceComputationTools::ComputeCloud2PlaneRobustMax( GenericCloud*

//we search the max @ 'percent'% (to avoid outliers)
std::vector<PointCoordinateType> tail;
std::size_t tailSize = static_cast<std::size_t>(ceil(static_cast<float>(count) * percent));
std::size_t tailSize = static_cast<std::size_t>(ceil(static_cast<double>(count) * percent));
tail.resize(tailSize);

//compute deviations
Expand Down
2 changes: 1 addition & 1 deletion src/FastMarchingForPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ float FastMarchingForPropagation::computeTCoefApprox(Cell* currentCell, Cell* ne
{
PropagationCell* cCell = static_cast<PropagationCell*>(currentCell);
PropagationCell* nCell = static_cast<PropagationCell*>(neighbourCell);
return expm1(m_jumpCoef * (cCell->f-nCell->f));
return static_cast<float>(expm1(m_jumpCoef * static_cast<double>(cCell->f - nCell->f)));
}

void FastMarchingForPropagation::findPeaks()
Expand Down
Loading

0 comments on commit cc909d2

Please sign in to comment.