Skip to content

Commit

Permalink
Coding - Resolving C6263 warnings
Browse files Browse the repository at this point in the history
Reorganizing code to not call alloca inside loop.
  alloca allocated memory from stack and free after finishing function
  • Loading branch information
dpasukhi committed Sep 9, 2024
1 parent ed20837 commit 0a0406e
Showing 1 changed file with 80 additions and 62 deletions.
142 changes: 80 additions & 62 deletions src/MeshVS/MeshVS_MeshPrsBuilder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,85 @@

IMPLEMENT_STANDARD_RTTIEXT(MeshVS_MeshPrsBuilder,MeshVS_PrsBuilder)

namespace
{
//================================================================
// Function : ProcessFace
// Purpose : Fill array with triangles for the face
//================================================================
static void ProcessFace(const Handle(MeshVS_HArray1OfSequenceOfInteger)& theTopo,
const TColStd_Array1OfReal& theNodes,
const Standard_Real* theCenter,
const Standard_Real theShrinkCoef,
const Standard_Boolean theIsShrinked,
const Standard_Boolean theIsShaded,
Handle(Graphic3d_ArrayOfPrimitives) theArray)
{
for (Standard_Integer aFaceIdx = theTopo->Lower(); aFaceIdx <= theTopo->Upper(); ++aFaceIdx)
{
const TColStd_SequenceOfInteger& aFaceNodes = theTopo->Value (aFaceIdx);
const Standard_Integer aNbPolyNodes = aFaceNodes.Length();

Standard_Real* aPolyNodesBuf = (Standard_Real*) alloca ((3 * aNbPolyNodes + 1) * sizeof (Standard_Real));
TColStd_Array1OfReal aPolyNodes (*aPolyNodesBuf, 0, 3 * aNbPolyNodes);

for (Standard_Integer aNodeIdx = 0; aNodeIdx < aNbPolyNodes; ++aNodeIdx)
{
Standard_Integer anIdx = aFaceNodes.Value (aNodeIdx + 1);

Standard_Real aX = theNodes.Value (theNodes.Lower() + 3 * anIdx + 0);
Standard_Real aY = theNodes.Value (theNodes.Lower() + 3 * anIdx + 1);
Standard_Real aZ = theNodes.Value (theNodes.Lower() + 3 * anIdx + 2);

if (theIsShrinked)
{
aX = theCenter[0] + theShrinkCoef * (aX - theCenter[0]);
aY = theCenter[1] + theShrinkCoef * (aY - theCenter[1]);
aZ = theCenter[2] + theShrinkCoef * (aZ - theCenter[2]);
}

aPolyNodes.SetValue (3 * aNodeIdx + 1, aX);
aPolyNodes.SetValue (3 * aNodeIdx + 2, aY);
aPolyNodes.SetValue (3 * aNodeIdx + 3, aZ);
}

gp_Vec aNorm;

if (theIsShaded)
{
aPolyNodes.SetValue (0, aNbPolyNodes);

if (!MeshVS_Tool::GetAverageNormal (aPolyNodes, aNorm))
{
aNorm.SetCoord (0.0, 0.0, 1.0);
}
}

for (Standard_Integer aNodeIdx = 0; aNodeIdx < aNbPolyNodes - 2; ++aNodeIdx) // triangulate polygon
{
for (Standard_Integer aSubIdx = 0; aSubIdx < 3; ++aSubIdx) // generate sub-triangle
{
if (theIsShaded)
{
theArray->AddVertex (aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 1),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 2),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 3),
aNorm.X(),
aNorm.Y(),
aNorm.Z());
}
else
{
theArray->AddVertex (aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 1),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 2),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 3));
}
}
}
}
}
}

//================================================================
// Function : Constructor MeshVS_MeshPrsBuilder
// Purpose :
Expand Down Expand Up @@ -838,68 +917,7 @@ void MeshVS_MeshPrsBuilder::AddVolumePrs (const Handle(MeshVS_HArray1OfSequenceO

if (aIsPolygons)
{
for (Standard_Integer aFaceIdx = theTopo->Lower(), topoup = theTopo->Upper(); aFaceIdx <= topoup; ++aFaceIdx)
{
const TColStd_SequenceOfInteger& aFaceNodes = theTopo->Value (aFaceIdx);
const Standard_Integer aNbPolyNodes = aFaceNodes.Length();

Standard_Real* aPolyNodesBuf = (Standard_Real*) alloca ((3 * aNbPolyNodes + 1) * sizeof (Standard_Real));
TColStd_Array1OfReal aPolyNodes (*aPolyNodesBuf, 0, 3 * aNbPolyNodes);

for (Standard_Integer aNodeIdx = 0; aNodeIdx < aNbPolyNodes; ++aNodeIdx)
{
Standard_Integer anIdx = aFaceNodes.Value (aNodeIdx + 1);

Standard_Real aX = theNodes.Value (aLow + 3 * anIdx + 0);
Standard_Real aY = theNodes.Value (aLow + 3 * anIdx + 1);
Standard_Real aZ = theNodes.Value (aLow + 3 * anIdx + 2);

if (theIsShrinked)
{
aX = aCenter[0] + theShrinkCoef * (aX - aCenter[0]);
aY = aCenter[1] + theShrinkCoef * (aY - aCenter[1]);
aZ = aCenter[2] + theShrinkCoef * (aZ - aCenter[2]);
}

aPolyNodes.SetValue (3 * aNodeIdx + 1, aX);
aPolyNodes.SetValue (3 * aNodeIdx + 2, aY);
aPolyNodes.SetValue (3 * aNodeIdx + 3, aZ);
}

gp_Vec aNorm;

if (theIsShaded)
{
aPolyNodes.SetValue (0, aNbPolyNodes);

if (!MeshVS_Tool::GetAverageNormal (aPolyNodes, aNorm))
{
aNorm.SetCoord (0.0, 0.0, 1.0);
}
}

for (Standard_Integer aNodeIdx = 0; aNodeIdx < aNbPolyNodes - 2; ++aNodeIdx) // triangulate polygon
{
for (Standard_Integer aSubIdx = 0; aSubIdx < 3; ++aSubIdx) // generate sub-triangle
{
if (theIsShaded)
{
theArray->AddVertex (aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 1),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 2),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 3),
aNorm.X(),
aNorm.Y(),
aNorm.Z());
}
else
{
theArray->AddVertex (aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 1),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 2),
aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 3));
}
}
}
}
ProcessFace (theTopo, theNodes, aCenter, theShrinkCoef, theIsShrinked, theIsSelected, theArray);
}
else if (theIsSelected)
{
Expand Down

0 comments on commit 0a0406e

Please sign in to comment.