Skip to content

Commit

Permalink
[Topology.Container] Add new method computeSegmentTriangleIntersectio…
Browse files Browse the repository at this point in the history
…nInPlane in TriangleSetGeometryAlgorithm (sofa-framework#5188)

* Update TriangleSetGeometryAlgorithms.h

* Update TriangleSetGeometryAlgorithms.inl

* [TopologyTest] Add comments

* [Topology.Dynamic_test] factorize some code in TriangleSetTopology_test

* [Topology.Dynamic_test] Add some tests in TriangleSetTopology_test to test adding and removing triangles

* typo errors

* [Topology.Dynamic_test] Add tests in TriangleSetTopology_test tto test TriangleSegmentIntersection inside same plane or with projection

* Update TriangleSetTopology_test.cpp

* Update TriangleSetTopology_test.cpp

* Update TriangleSetTopology_test.cpp

* Fix namespace
  • Loading branch information
epernod authored Feb 10, 2025
1 parent 5448a14 commit 5120328
Show file tree
Hide file tree
Showing 3 changed files with 482 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,27 @@ class TriangleSetGeometryAlgorithms : public EdgeSetGeometryAlgorithms<DataTypes
* @param baryCoef : barycoef of the intersection point on the edge
* @param coord_kmin : barycoef of the intersection point on the vecteur AB.
*/


bool computeSegmentTriangleIntersection(bool is_entered,
const sofa::type::Vec<3, Real>& a,
const sofa::type::Vec<3, Real>& b,
const TriangleID ind_t,
sofa::type::vector<PointID>& indices,
Real& baryCoef, Real& coord_kmin) const;

/** \brief Computes the intersection between the edges of the Triangle triId and the vector [AB] projected into this Triangle frame.
* @param ptA : first input point
* @param ptB : last input point
* @param triId : index of the triangle whose edges will be checked
* @param intersectedEdges : output list of Edge global Ids that are intersected by vector AB (size could be 0, 1 or 2)
* @param baryCoefs : output list of barycoef corresponding to the relative position of the intersection on the edge (same size and order as @sa intersectedEdges)
*/
bool computeSegmentTriangleIntersectionInPlane(
const sofa::type::Vec<3, Real>& ptA,
const sofa::type::Vec<3, Real>& ptB,
const TriangleID triId,
sofa::type::vector<EdgeID>& intersectedEdges,
sofa::type::vector<Real>& baryCoefs) const;

/** \brief Computes the intersections of the vector from point a to point b and the triangle indexed by t
*
* @param a : first input point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,62 @@ void TriangleSetGeometryAlgorithms< DataTypes >::prepareVertexDuplication(const
}
}


template<class DataTypes>
bool TriangleSetGeometryAlgorithms< DataTypes >::computeSegmentTriangleIntersectionInPlane(
const sofa::type::Vec<3, Real>& ptA,
const sofa::type::Vec<3, Real>& ptB,
const TriangleID triId,
sofa::type::vector<EdgeID>& intersectedEdges,
sofa::type::vector<Real>& baryCoefs) const
{
// Get coordinates of each vertex of the triangle
const typename DataTypes::VecCoord& coords = (this->object->read(core::ConstVecCoordId::position())->getValue());
const Triangle& tri = this->m_topology->getTriangle(triId);

const typename DataTypes::Coord& c0 = coords[tri[0]];
const typename DataTypes::Coord& c1 = coords[tri[1]];
const typename DataTypes::Coord& c2 = coords[tri[2]];
type::fixed_array<Vec3, 3> triP = { Vec3(c0[0], c0[1], c0[2]), Vec3(c1[0], c1[1], c1[2]), Vec3(c2[0], c2[1], c2[2]) };

// Project A and B into triangle plan
Vec3 v_normal = (triP[2] - triP[0]).cross(triP[1] - triP[0]);
v_normal.normalize();
const Vec3 pa_proj = ptA - v_normal * dot(ptA - triP[0], v_normal);
const Vec3 pb_proj = ptB - v_normal * dot(ptB - triP[0], v_normal);

// check intersection between AB and each edge of the triangle
const sofa::type::fixed_array<EdgeID, 3>& edgesInTri = this->m_topology->getEdgesInTriangle(triId);
for (const EdgeID& edgeId : edgesInTri)
{
const Edge& edge = this->m_topology->getEdge(edgeId);
Edge localIds;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (edge[i] == tri[j])
{
localIds[i] = j;
break;
}
}
}

type::Vec2 baryCoords(type::NOINIT);
bool res = geometry::Edge::intersectionWithEdge(triP[localIds[0]], triP[localIds[1]], pa_proj, pb_proj, baryCoords);

if (res)
{
intersectedEdges.push_back(edgeId);
baryCoefs.push_back(baryCoords[0]);
}
}

return !intersectedEdges.empty();
}


// Computes the intersection of the segment from point a to point b and the triangle indexed by t
template<class DataTypes>
bool TriangleSetGeometryAlgorithms< DataTypes >::computeSegmentTriangleIntersection(bool is_entered,
Expand Down
Loading

0 comments on commit 5120328

Please sign in to comment.