Skip to content

Commit b099a45

Browse files
committed
[src] Move computeIntersectionNeighborTriangle into TearingAlgorithm and use existing methods from TriangleSetGeometryAlgo
1 parent 589fcb8 commit b099a45

File tree

4 files changed

+79
-72
lines changed

4 files changed

+79
-72
lines changed

src/Tearing/TearingAlgorithms.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,16 @@ class TearingAlgorithms
7171
TriangleSetGeometryAlgorithms<DataTypes>* _triangleGeo);
7272

7373
virtual ~TearingAlgorithms();
74-
75-
void computeFracturePath(FracturePath& my_fracturePath);
74+
75+
76+
/// <summary>
77+
/// computes the extremities of the (normalized) fracture PbPa on the edge of the triangle
78+
/// </summary>
79+
/// @param Pa - the point where the fracture starts
80+
/// @param normalizedFractureDirection - normalized fracture direction
81+
/// @return Pb - one of the extremities of fracture
82+
/// @return t - a parameter needed to calculate Pb
83+
TriangleID computeIntersectionNeighborTriangle(const Index ptAId, const Coord& ptA, const Coord& normalizedFractureDirection, Coord& Pb);
7684

7785
void computeFracturePath(const Coord& pA, Index triId, const Coord pB, const Coord pC);
7886

src/Tearing/TearingAlgorithms.inl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,67 @@ TearingAlgorithms<DataTypes>::~TearingAlgorithms()
5151
}
5252

5353

54+
template<class DataTypes>
55+
inline TriangleID TearingAlgorithms<DataTypes>::computeIntersectionNeighborTriangle(const Index ptAId, const Coord& ptA, const Coord& normalizedFractureDirection, Coord& ptB)
56+
{
57+
// Get triangle in the fracture direction
58+
TriangleID theTriId = m_triangleGeo->getTriangleInDirection(ptAId, normalizedFractureDirection);
59+
60+
// If not, could be on the border. Return invalidID
61+
if (theTriId > this->m_topology->getNbTriangles() - 1)
62+
return sofa::InvalidID;
63+
64+
// If it exists, get triangle and search for ptAId local index in the triangle
65+
const Triangle& theTri = this->m_topology->getTriangle(theTriId);
66+
PointID localAId = sofa::InvalidID;
67+
for (PointID vertex_id = 0; vertex_id < 3; vertex_id++)
68+
{
69+
if (theTri[vertex_id] == ptAId)
70+
{
71+
localAId = vertex_id;
72+
break;
73+
}
74+
}
75+
76+
// Get the opposite edge
77+
EdgeID oppositeEdgeId = this->m_topology->getEdgesInTriangle(theTriId)[localAId];
78+
Edge oppositeEdge = this->m_topology->getEdge(oppositeEdgeId);
79+
80+
81+
//Building point B such that to be sure that AB intersects CD, based on "Losange"
82+
const Coord pE0 = this->m_triangleGeo->getPointPosition(oppositeEdge[0]);
83+
const Coord pE1 = this->m_triangleGeo->getPointPosition(oppositeEdge[1]);
84+
85+
const Real AC_length = (pE0 - ptA).norm();
86+
const Real AD_length = (pE1 - ptA).norm();
87+
const Real Length = AC_length + AD_length;
88+
89+
ptB = ptA + Length * normalizedFractureDirection;
90+
91+
// Compute intersection on the opposite edge
92+
sofa::type::vector<EdgeID> intersectedEdges;
93+
sofa::type::vector<Real> baryCoefs;
94+
m_triangleGeo->computeSegmentTriangleIntersectionInPlane(ptA, ptB, theTriId, intersectedEdges, baryCoefs);
95+
96+
bool found = false;
97+
for (unsigned int i=0; i< intersectedEdges.size(); ++i)
98+
{
99+
if (intersectedEdges[i] == oppositeEdgeId)
100+
{
101+
found = true;
102+
ptB = pE0 * baryCoefs[i] + pE1 * (1 - baryCoefs[i]);
103+
break;
104+
}
105+
}
106+
107+
if (!found)
108+
return sofa::InvalidID;
109+
else
110+
return theTriId;
111+
}
112+
113+
114+
54115
template <class DataTypes>
55116
void TearingAlgorithms<DataTypes>::computeFracturePath(const Coord& pA, Index triId, const Coord pB, const Coord pC)
56117
{

src/Tearing/TearingEngine.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ class TearingEngine : public BaseTearingEngine<DataTypes>
8686
/// @return Pc - one of the extremities of fracture
8787
bool computeEndPointsNeighboringTriangles(const Coord& Pa, const Coord& fractureDirection, Coord& Pb, Coord& Pc);
8888

89-
/// <summary>
90-
/// computes the extremities of the (normalized) fracture PbPa on the edge of the triangle
91-
/// </summary>
92-
/// @param Pa - the point where the fracture starts
93-
/// @param normalizedFractureDirection - normalized fracture direction
94-
/// @return Pb - one of the extremities of fracture
95-
/// @return t - a parameter needed to calculate Pb
96-
bool computeIntersectionNeighborTriangle(Coord normalizedFractureDirection, Coord Pa, Coord& Pb, Real& t);
97-
9889
void algoFracturePath() override;
9990

10091
void computeFracturePath() override;

src/Tearing/TearingEngine.inl

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,57 +41,6 @@ using sofa::type::Vec3;
4141
// --------------------------------------------------------------------------------------
4242
// --- Computation methods
4343
// --------------------------------------------------------------------------------------
44-
template<class DataTypes>
45-
inline bool TearingEngine<DataTypes>::computeIntersectionNeighborTriangle(Coord normalizedFractureDirection, Coord Pa, Coord& Pb, Real& t)
46-
{
47-
SOFA_UNUSED(Pa);
48-
49-
if (m_maxStressVertexIndex == InvalidID)
50-
return false;
51-
52-
// Get Geometry Algorithm
53-
TriangleSetGeometryAlgorithms<DataTypes>* _triangleGeo = nullptr;
54-
this->m_topology->getContext()->get(_triangleGeo);
55-
if (!_triangleGeo)
56-
{
57-
msg_error() << "Missing component: Unable to get TriangleSetGeometryAlgorithms from the current context.";
58-
sofa::core::objectmodel::BaseObject::d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
59-
return false;
60-
}
61-
62-
63-
Index triangle_id = _triangleGeo->getTriangleInDirection(m_maxStressVertexIndex, normalizedFractureDirection);
64-
if (triangle_id > this->m_topology->getNbTriangles() - 1)
65-
return false;
66-
67-
68-
const Triangle& VertexIndicies = this->m_topology->getTriangle(triangle_id);
69-
70-
constexpr size_t numVertices = 3;
71-
Index B_id = -1, C_id = -1;
72-
73-
for (unsigned int vertex_id = 0; vertex_id < numVertices; vertex_id++)
74-
{
75-
if (VertexIndicies[vertex_id] == m_maxStressVertexIndex)
76-
{
77-
B_id = VertexIndicies[(vertex_id + 1) % 3];
78-
C_id = VertexIndicies[(vertex_id + 2) % 3];
79-
break;
80-
}
81-
82-
}
83-
84-
helper::ReadAccessor< Data<VecCoord> > x(d_input_positions);
85-
Coord A = x[m_maxStressVertexIndex];
86-
Coord B = x[B_id];
87-
Coord C = x[C_id];
88-
89-
if (rayTriangleIntersection(A, B, C, normalizedFractureDirection, t, Pb))
90-
return true;
91-
else
92-
return false;
93-
94-
}
9544

9645
template<class DataTypes>
9746
inline bool TearingEngine<DataTypes>::computeEndPointsNeighboringTriangles(const Coord& Pa, const Coord& fractureDirection, Coord& Pb, Coord& Pc)
@@ -100,22 +49,18 @@ inline bool TearingEngine<DataTypes>::computeEndPointsNeighboringTriangles(const
10049
bool t_c_ok = false;
10150

10251
Coord dir_b = fractureDirection / fractureDirection.norm();
103-
104-
Real t_b;
105-
if (computeIntersectionNeighborTriangle(dir_b,Pa, Pb, t_b))
106-
t_b_ok = true;
52+
TriangleID t_b = this->m_tearingAlgo->computeIntersectionNeighborTriangle(m_maxStressVertexIndex, Pa, dir_b, Pb);
10753

108-
10954
Coord dir_c = -dir_b;
110-
Real t_c;
111-
if (computeIntersectionNeighborTriangle(dir_c,Pa,Pc, t_c))
112-
t_c_ok = true;
55+
TriangleID t_c = this->m_tearingAlgo->computeIntersectionNeighborTriangle(m_maxStressVertexIndex, Pa, dir_c, Pc);
56+
11357

114-
if (!(t_b_ok && t_c_ok))
58+
if (t_b == sofa::InvalidID || t_c == sofa::InvalidID)
11559
{
11660
msg_warning() << "Not able to build the fracture path through neighboring triangles.";
11761
return false;
11862
}
63+
11964
return true;
12065
}
12166

@@ -172,7 +117,9 @@ void TearingEngine<DataTypes>::computeFracturePath()
172117

173118
if (this->d_fractureMaxLength.getValue() == 0.0)
174119
{
175-
this->computeEndPointsNeighboringTriangles(Pa, fractureDirection, Pb, Pc);
120+
bool checkEndsPoints = this->computeEndPointsNeighboringTriangles(Pa, fractureDirection, Pb, Pc);
121+
if (!checkEndsPoints)
122+
return;
176123
}
177124
else
178125
{

0 commit comments

Comments
 (0)