PointTriangleCcdContactCalculation-inl.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013-2016, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_MATH_POINTTRIANGLECCDCONTACTCALCULATION_INL_H
#define SURGSIM_MATH_POINTTRIANGLECCDCONTACTCALCULATION_INL_H
#include <Eigen/Core>
#include <Eigen/Geometry>
#include "SurgSim/Math/CubicSolver.h"
namespace SurgSim
{
namespace Math
{
template <class T, int MOpt>
bool isPointInsideTriangle(
T time,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& P,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& A,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& B,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& C,
Eigen::Matrix<T, 3, 1, MOpt>* barycentricCoordinates)
{
Eigen::Matrix<T, 3, 1, MOpt> Pt = interpolate(P.first, P.second, time);
Eigen::Matrix<T, 3, 1, MOpt> At = interpolate(A.first, A.second, time);
Eigen::Matrix<T, 3, 1, MOpt> Bt = interpolate(B.first, B.second, time);
Eigen::Matrix<T, 3, 1, MOpt> Ct = interpolate(C.first, C.second, time);
bool result = Math::barycentricCoordinates(Pt, At, Bt, Ct, barycentricCoordinates);
for (int i = 0; i < 3; i++)
{
if (std::abs((*barycentricCoordinates)[i]) < Math::Geometry::ScalarEpsilon)
{
(*barycentricCoordinates)[i] = 0.0;
}
if (std::abs(1.0 - (*barycentricCoordinates)[i]) < Math::Geometry::ScalarEpsilon)
{
(*barycentricCoordinates)[i] = 1.0;
}
}
return (result &&
(*barycentricCoordinates)[0] >= 0.0 &&
(*barycentricCoordinates)[1] >= 0.0 &&
(*barycentricCoordinates)[2] >= 0.0);
}
template <class T, int MOpt> inline
bool calculateCcdContactPointTriangle(
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& P,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& A,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& B,
const std::pair<Eigen::Matrix<T, 3, 1, MOpt>, Eigen::Matrix<T, 3, 1, MOpt>>& C,
T* timeOfImpact, T* tv01Param, T* tv02Param)
{
std::array<T, 3> roots;
int numberOfRoots = timesOfCoplanarityInRange01(P, A, B, C, &roots);
// The roots are all in [0..1] and ordered ascendingly
for (int rootId = 0; rootId < numberOfRoots; ++rootId)
{
Eigen::Matrix<T, 3, 1, MOpt> baryCoords;
if (isPointInsideTriangle(roots[rootId], P, A, B, C, &baryCoords))
{
// The point P is in the triangle plane at time t, and is inside the triangle
*timeOfImpact = roots[rootId];
*tv01Param = baryCoords[1];
*tv02Param = baryCoords[2];
// None of these assertion should be necessary, but just to double check
SURGSIM_ASSERT(*timeOfImpact >= 0.0 && *timeOfImpact <= 1.0);
SURGSIM_ASSERT(*tv01Param >= 0.0);
SURGSIM_ASSERT(*tv02Param >= 0.0);
SURGSIM_ASSERT(*tv01Param + *tv02Param <= 1.0 + Geometry::ScalarEpsilon);
return true;
}
}
return false;
}
}; // namespace Math
}; // namespace SurgSim
#endif // SURGSIM_MATH_POINTTRIANGLECCDCONTACTCALCULATION_INL_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::Math::interpolate
Eigen::Quaternion< T, QOpt > interpolate(const Eigen::Quaternion< T, QOpt > &q0, const Eigen::Quaternion< T, QOpt > &q1, T t)
Interpolate (slerp) between 2 quaternions.
Definition: Quaternion.h:149
SurgSim::Math::timesOfCoplanarityInRange01
int timesOfCoplanarityInRange01(const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &A, const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &B, const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &C, const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &D, std::array< T, 3 > *timesOfCoplanarity)
Test when 4 points are coplanar in the range [0..1] given their linear motion.
Definition: Geometry.h:1840
SurgSim::Math::barycentricCoordinates
bool barycentricCoordinates(const Eigen::Matrix< T, 3, 1, MOpt > &pt, const Eigen::Matrix< T, 3, 1, MOpt > &sv0, const Eigen::Matrix< T, 3, 1, MOpt > &sv1, Eigen::Matrix< T, 2, 1, MOpt > *coordinates)
Calculate the barycentric coordinates of a point with respect to a line segment.
Definition: Geometry.h:71
CubicSolver.h
SurgSim::Math::isPointInsideTriangle
bool isPointInsideTriangle(const Eigen::Matrix< T, 3, 1, MOpt > &pt, const Eigen::Matrix< T, 3, 1, MOpt > &tv0, const Eigen::Matrix< T, 3, 1, MOpt > &tv1, const Eigen::Matrix< T, 3, 1, MOpt > &tv2, const Eigen::Matrix< T, 3, 1, MOpt > &tn)
Check if a point is inside a triangle.
Definition: Geometry.h:159
SurgSim::Math::calculateCcdContactPointTriangle
bool calculateCcdContactPointTriangle(const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &P, const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &A, const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &B, const std::pair< Eigen::Matrix< T, 3, 1, MOpt >, Eigen::Matrix< T, 3, 1, MOpt >> &C, T *timeOfImpact, T *tv01Param, T *tv02Param)
Continuous collision detection between a point P and a triangle ABC.
Definition: PointTriangleCcdContactCalculation-inl.h:84