Skip to content

Commit 8912732

Browse files
committed
Merge branch 'master' of https://github.com/sofa-framework/sofa
2 parents 17c7267 + 4dfaa0c commit 8912732

File tree

5 files changed

+217
-69
lines changed

5 files changed

+217
-69
lines changed

Sofa/Component/Topology/Container/Constant/src/sofa/component/topology/container/constant/MeshTopology.cpp

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ void MeshTopology::init()
518518

519519
const auto hexahedra = sofa::helper::getReadAccessor(d_seqHexahedra);
520520
const auto tetrahedra = sofa::helper::getReadAccessor(d_seqTetrahedra);
521+
const auto prisms = sofa::helper::getReadAccessor(d_seqPrisms);
522+
const auto pyramids = sofa::helper::getReadAccessor(d_seqPyramids);
521523
const auto quads = sofa::helper::getReadAccessor(d_seqQuads);
522524
const auto triangles = sofa::helper::getReadAccessor(d_seqTriangles);
523525
const auto edges = sofa::helper::getReadAccessor(d_seqEdges);
@@ -527,6 +529,10 @@ void MeshTopology::init()
527529
m_upperElementType = geometry::ElementType::HEXAHEDRON;
528530
else if (!tetrahedra.empty())
529531
m_upperElementType = sofa::geometry::ElementType::TETRAHEDRON;
532+
else if (!prisms.empty())
533+
m_upperElementType = sofa::geometry::ElementType::PRISM;
534+
else if (!pyramids.empty())
535+
m_upperElementType = sofa::geometry::ElementType::PYRAMID;
530536
else if (!quads.empty())
531537
m_upperElementType = sofa::geometry::ElementType::QUAD;
532538
else if (!triangles.empty())
@@ -545,28 +551,13 @@ void MeshTopology::init()
545551
// compute the number of points, if the topology is charged from the scene or if it was loaded from a MeshLoader without any points data.
546552
if (nbPoints==0)
547553
{
548-
unsigned int n = 0;
549-
const auto countPoints = [&n](const auto& seqElements)
550-
{
551-
for (const auto& element : seqElements)
552-
{
553-
for (const auto pointId : element)
554-
{
555-
if (n <= pointId)
556-
{
557-
n = 1 + pointId;
558-
}
559-
}
560-
}
561-
};
562-
563-
countPoints(edges);
564-
countPoints(triangles);
565-
countPoints(quads);
566-
countPoints(tetrahedra);
567-
countPoints(hexahedra);
568-
569-
nbPoints = n;
554+
nbPoints = std::max(nbPoints, countPoints(edges));
555+
nbPoints = std::max(nbPoints, countPoints(triangles));
556+
nbPoints = std::max(nbPoints, countPoints(quads));
557+
nbPoints = std::max(nbPoints, countPoints(tetrahedra));
558+
nbPoints = std::max(nbPoints, countPoints(hexahedra));
559+
nbPoints = std::max(nbPoints, countPoints(prisms));
560+
nbPoints = std::max(nbPoints, countPoints(pyramids));
570561
}
571562

572563

@@ -694,55 +685,58 @@ void MeshTopology::addPoint(SReal px, SReal py, SReal pz)
694685

695686
void MeshTopology::addEdge( Index a, Index b )
696687
{
697-
d_seqEdges.beginEdit()->push_back(Edge(a, b));
698-
d_seqEdges.endEdit();
699-
if (a >= nbPoints) nbPoints = a+1;
700-
if (b >= nbPoints) nbPoints = b+1;
688+
const Edge addedElement { a, b };
689+
auto seqElements = helper::getWriteOnlyAccessor(d_seqEdges);
690+
seqElements.push_back(addedElement);
691+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
701692
}
702693

703694
void MeshTopology::addTriangle( Index a, Index b, Index c )
704695
{
705-
d_seqTriangles.beginEdit()->push_back(Triangle(a, b, c) );
706-
d_seqTriangles.endEdit();
707-
if (a >= nbPoints) nbPoints = a+1;
708-
if (b >= nbPoints) nbPoints = b+1;
709-
if (c >= nbPoints) nbPoints = c+1;
696+
const Triangle addedElement { a, b, c };
697+
auto seqElements = helper::getWriteOnlyAccessor(d_seqTriangles);
698+
seqElements.push_back(addedElement);
699+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
710700
}
711701

712702
void MeshTopology::addQuad(Index a, Index b, Index c, Index d)
713703
{
714-
d_seqQuads.beginEdit()->push_back(Quad(a, b, c, d));
715-
d_seqQuads.endEdit();
716-
if (a >= nbPoints) nbPoints = a+1;
717-
if (b >= nbPoints) nbPoints = b+1;
718-
if (c >= nbPoints) nbPoints = c+1;
719-
if (d >= nbPoints) nbPoints = d+1;
704+
const Quad addedElement { a, b, c, d };
705+
auto seqElements = helper::getWriteOnlyAccessor(d_seqQuads);
706+
seqElements.push_back(addedElement);
707+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
720708
}
721709

722710
void MeshTopology::addTetra( Index a, Index b, Index c, Index d )
723711
{
724-
d_seqTetrahedra.beginEdit()->push_back(Tetra(a, b, c, d) );
725-
d_seqTetrahedra.endEdit();
726-
if (a >= nbPoints) nbPoints = a+1;
727-
if (b >= nbPoints) nbPoints = b+1;
728-
if (c >= nbPoints) nbPoints = c+1;
729-
if (d >= nbPoints) nbPoints = d+1;
712+
const Tetrahedron addedElement { a, b, c, d };
713+
auto seqElements = helper::getWriteOnlyAccessor(d_seqTetrahedra);
714+
seqElements.push_back(addedElement);
715+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
730716
}
731717

732718
void MeshTopology::addHexa(Index p1, Index p2, Index p3, Index p4, Index p5, Index p6, Index p7, Index p8)
733719
{
720+
const Hexahedron addedElement { p1, p2, p3, p4, p5, p6, p7, p8 };
721+
auto seqElements = helper::getWriteOnlyAccessor(d_seqHexahedra);
722+
seqElements.push_back(addedElement);
723+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
724+
}
734725

735-
d_seqHexahedra.beginEdit()->push_back(Hexa(p1, p2, p3, p4, p5, p6, p7, p8));
726+
void MeshTopology::addPrism(Index a, Index b, Index c, Index d, Index e, Index f)
727+
{
728+
const Prism addedElement { a, b, c, d, e, f };
729+
auto seqElements = helper::getWriteOnlyAccessor(d_seqPrisms);
730+
seqElements.push_back(addedElement);
731+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
732+
}
736733

737-
d_seqHexahedra.endEdit();
738-
if (p1 >= nbPoints) nbPoints = p1+1;
739-
if (p2 >= nbPoints) nbPoints = p2+1;
740-
if (p3 >= nbPoints) nbPoints = p3+1;
741-
if (p4 >= nbPoints) nbPoints = p4+1;
742-
if (p5 >= nbPoints) nbPoints = p5+1;
743-
if (p6 >= nbPoints) nbPoints = p6+1;
744-
if (p7 >= nbPoints) nbPoints = p7+1;
745-
if (p8 >= nbPoints) nbPoints = p8+1;
734+
void MeshTopology::addPyramid(Index a, Index b, Index c, Index d, Index e)
735+
{
736+
const Pyramid addedElement { a, b, c, d, e };
737+
auto seqElements = helper::getWriteOnlyAccessor(d_seqPyramids);
738+
seqElements.push_back(addedElement);
739+
nbPoints = std::max(nbPoints, countPoints(std::array{addedElement}));
746740
}
747741

748742
void MeshTopology::addUV(SReal u, SReal v)

Sofa/Component/Topology/Container/Constant/src/sofa/component/topology/container/constant/MeshTopology.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ class SOFA_COMPONENT_TOPOLOGY_CONTAINER_CONSTANT_API MeshTopology : public core:
250250
void addQuad( Index a, Index b, Index c, Index d ) override;
251251
void addTetra( Index a, Index b, Index c, Index d ) override;
252252
void addHexa( Index a, Index b, Index c, Index d, Index e, Index f, Index g, Index h ) override;
253+
void addPrism( Index a, Index b, Index c, Index d, Index e, Index f ) override;
254+
void addPyramid( Index a, Index b, Index c, Index d, Index e ) override;
253255

254256
/// get the current revision of this mesh (use to detect changes)
255257
int getRevision() const override { return revision; }
@@ -308,6 +310,23 @@ class SOFA_COMPONENT_TOPOLOGY_CONTAINER_CONSTANT_API MeshTopology : public core:
308310
protected:
309311
Size nbPoints;
310312

313+
template<typename ElementContainer>
314+
Size countPoints(const ElementContainer& seqElements)
315+
{
316+
Size n = 0;
317+
for (const auto& element : seqElements)
318+
{
319+
for (const auto pointId : element)
320+
{
321+
if (n <= pointId)
322+
{
323+
n = 1 + pointId;
324+
}
325+
}
326+
}
327+
return n;
328+
}
329+
311330
bool validTetrahedra;
312331
bool validHexahedra;
313332

Sofa/Component/Topology/Container/Constant/tests/MeshTopology_test.cpp

Lines changed: 139 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ using namespace sofa::testing;
4646
class MeshTopology_test : public BaseTest
4747
{
4848
public:
49-
bool testEmptyContainer();
49+
void doSetUp() override
50+
{
51+
m_topo = sofa::core::objectmodel::New< MeshTopology >();
52+
ASSERT_TRUE(m_topo != nullptr);
53+
ASSERT_EQ(m_topo->getNbPoints(), 0u);
54+
}
55+
56+
bool testEmptyContainer() const;
5057

5158
bool testHexahedronTopology();
5259
bool testTetrahedronTopology();
@@ -56,30 +63,41 @@ class MeshTopology_test : public BaseTest
5663

5764
bool testEdgeTopology();
5865
bool testVertexTopology();
66+
67+
void testAddPoint() const;
68+
void testAddEdge() const;
69+
void testAddTriangle() const;
70+
void testAddQuad() const;
71+
void testAddTetrahedron() const;
72+
void testAddHexahedron() const;
73+
void testAddPrism() const;
74+
void testAddPyramid() const;
75+
76+
protected:
77+
78+
MeshTopology::SPtr m_topo { nullptr };
5979

6080
};
6181

6282

63-
bool MeshTopology_test::testEmptyContainer()
83+
bool MeshTopology_test::testEmptyContainer() const
6484
{
65-
const MeshTopology::SPtr topoCon = sofa::core::objectmodel::New< MeshTopology >();
66-
67-
EXPECT_EQ(topoCon->getNbHexahedra(), 0);
68-
EXPECT_EQ(topoCon->getHexahedra().size(), 0);
85+
EXPECT_EQ(m_topo->getNbHexahedra(), 0);
86+
EXPECT_EQ(m_topo->getHexahedra().size(), 0);
6987

70-
EXPECT_EQ(topoCon->getNbHexahedra(), 0);
71-
EXPECT_EQ(topoCon->getTetrahedra().size(), 0);
88+
EXPECT_EQ(m_topo->getNbHexahedra(), 0);
89+
EXPECT_EQ(m_topo->getTetrahedra().size(), 0);
7290

73-
EXPECT_EQ(topoCon->getNbQuads(), 0);
74-
EXPECT_EQ(topoCon->getQuads().size(), 0);
91+
EXPECT_EQ(m_topo->getNbQuads(), 0);
92+
EXPECT_EQ(m_topo->getQuads().size(), 0);
7593

76-
EXPECT_EQ(topoCon->getNbTriangles(), 0);
77-
EXPECT_EQ(topoCon->getTriangles().size(), 0);
94+
EXPECT_EQ(m_topo->getNbTriangles(), 0);
95+
EXPECT_EQ(m_topo->getTriangles().size(), 0);
7896

79-
EXPECT_EQ(topoCon->getNbEdges(), 0);
80-
EXPECT_EQ(topoCon->getEdges().size(), 0);
97+
EXPECT_EQ(m_topo->getNbEdges(), 0);
98+
EXPECT_EQ(m_topo->getEdges().size(), 0);
8199

82-
EXPECT_EQ(topoCon->getNbPoints(), 0);
100+
EXPECT_EQ(m_topo->getNbPoints(), 0);
83101

84102
return true;
85103
}
@@ -576,11 +594,77 @@ bool MeshTopology_test::testVertexTopology()
576594
return true;
577595
}
578596

597+
void MeshTopology_test::testAddPoint() const
598+
{
599+
const auto before = m_topo->getNbPoints();
600+
m_topo->addPoint(0.0, 0.0, 0.0);
601+
EXPECT_EQ(m_topo->getNbPoints(), before + 1);
602+
}
603+
604+
void MeshTopology_test::testAddEdge() const
605+
{
606+
m_topo->addEdge(0, 1);
607+
EXPECT_EQ(m_topo->getNbPoints(), 2u);
579608

609+
m_topo->addEdge(2, 10);
610+
EXPECT_EQ(m_topo->getNbPoints(), 11u);
611+
}
580612

613+
void MeshTopology_test::testAddTriangle() const
614+
{
615+
m_topo->addTriangle(0, 1, 2);
616+
EXPECT_EQ(m_topo->getNbPoints(), 3u);
617+
618+
m_topo->addTriangle(5, 3, 4);
619+
EXPECT_EQ(m_topo->getNbPoints(), 6u);
620+
}
581621

582-
TEST_F(MeshTopology_test, testEmptyContainer)
622+
void MeshTopology_test::testAddQuad() const
583623
{
624+
m_topo->addQuad(0, 1, 2, 3);
625+
EXPECT_EQ(m_topo->getNbPoints(), 4u);
626+
627+
m_topo->addQuad(7, 6, 5, 4);
628+
EXPECT_EQ(m_topo->getNbPoints(), 8u);
629+
}
630+
631+
void MeshTopology_test::testAddTetrahedron() const
632+
{
633+
m_topo->addTetra(0, 1, 2, 3);
634+
EXPECT_EQ(m_topo->getNbPoints(), 4u);
635+
636+
m_topo->addTetra(2, 5, 9, 3);
637+
EXPECT_EQ(m_topo->getNbPoints(), 10u);
638+
}
639+
640+
void MeshTopology_test::testAddHexahedron() const
641+
{
642+
m_topo->addHexa(0, 1, 2, 3, 4, 5, 6, 7);
643+
EXPECT_EQ(m_topo->getNbPoints(), 8u);
644+
645+
m_topo->addHexa(10, 11, 12, 13, 14, 15, 16, 31);
646+
EXPECT_EQ(m_topo->getNbPoints(), 32u);
647+
}
648+
649+
void MeshTopology_test::testAddPrism() const
650+
{
651+
m_topo->addPrism(0, 1, 2, 3, 4, 5);
652+
EXPECT_EQ(m_topo->getNbPoints(), 6u);
653+
654+
m_topo->addPrism(4, 8, 6, 7, 2, 12);
655+
EXPECT_EQ(m_topo->getNbPoints(), 13u);
656+
}
657+
658+
void MeshTopology_test::testAddPyramid() const
659+
{
660+
m_topo->addPyramid(0, 1, 2, 3, 4);
661+
EXPECT_EQ(m_topo->getNbPoints(), 5u);
662+
663+
m_topo->addPyramid(10, 11, 12, 13, 20);
664+
EXPECT_EQ(m_topo->getNbPoints(), 21u);
665+
}
666+
667+
TEST_F(MeshTopology_test, testEmptyContainer) {
584668
ASSERT_TRUE(testEmptyContainer());
585669
}
586670

@@ -615,6 +699,45 @@ TEST_F(MeshTopology_test, testEdgeTopology)
615699
// ASSERT_TRUE(testVertexTopology());
616700
//}
617701

702+
TEST_F(MeshTopology_test, testAddPoint)
703+
{
704+
testAddPoint();
705+
}
706+
707+
TEST_F(MeshTopology_test, testAddEdge)
708+
{
709+
testAddEdge();
710+
}
711+
712+
TEST_F(MeshTopology_test, testAddTriangle)
713+
{
714+
testAddTriangle();
715+
}
716+
717+
TEST_F(MeshTopology_test, testAddQuad)
718+
{
719+
testAddQuad();
720+
}
721+
722+
TEST_F(MeshTopology_test, testAddTetrahedron)
723+
{
724+
testAddTetrahedron();
725+
}
726+
727+
TEST_F(MeshTopology_test, testAddHexahedron)
728+
{
729+
testAddHexahedron();
730+
}
731+
732+
TEST_F(MeshTopology_test, testAddPrism)
733+
{
734+
testAddPrism();
735+
}
736+
737+
TEST_F(MeshTopology_test, testAddPyramid)
738+
{
739+
testAddPyramid();
740+
}
618741

619742

620743
// TODO epernod 2018-07-05: test element on Border

Sofa/framework/Core/src/sofa/core/topology/BaseMeshTopology.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ void BaseMeshTopology::addHexa(Index, Index, Index, Index, Index, Index, Index,
355355
msg_error() << "addHexa() not supported.";
356356
}
357357

358+
void BaseMeshTopology::addPrism(Index a, Index b, Index c, Index d, Index e, Index f)
359+
{
360+
msg_error() << "addPrism() not supported.";
361+
}
362+
363+
void BaseMeshTopology::addPyramid(Index a, Index b, Index c, Index d, Index e)
364+
{
365+
msg_error() << "addPyramid() not supported.";
366+
}
367+
358368
void BaseMeshTopology::reOrientateTriangle(TriangleID /*id*/)
359369
{
360370
msg_error() << "reOrientateTriangle() not supported.";

0 commit comments

Comments
 (0)