Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Topology.Container] Add new method computeSegmentTriangleIntersectionInPlane in TriangleSetGeometryAlgorithm #5188

Merged
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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It changes nothing to the computation afterwards, but isn't this the opposite of the normal ?

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