SegmentSegmentCcdContactCalculation-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_SEGMENTSEGMENTCCDCONTACTCALCULATION_INL_H
#define SURGSIM_MATH_SEGMENTSEGMENTCCDCONTACTCALCULATION_INL_H
#include <Eigen/Core>
#include <Eigen/Geometry>
#include "SurgSim/Math/CubicSolver.h"
#include "SurgSim/Math/Scalar.h"
namespace SurgSim
{
namespace Math
{
template <class T, int MOpt>
bool areSegmentsIntersecting(
T time,
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,
Eigen::Matrix<T, 2, 1, MOpt>* barycentricCoordinates)
{
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);
Eigen::Matrix<T, 3, 1, MOpt> Dt = interpolate(D.first, D.second, time);
// P = A + alpha.AB and P = C + beta.CD
// A + alpha.AB = C + beta.CD
// (AB -CD).(alpha) = (AC) which is a 3x2 linear system A.x = b
// (beta )
// Let's solve it using (A^t.A)^-1.(A^t.A) = Id2x2
// (A^t.A)^-1.A^t.(A.x) = (A^t.A)^-1.A^t.b
// x = (A^t.A)^-1.A^t.b
Eigen::Matrix<T, 3, 2> matrixA;
matrixA.col(0) = Bt - At;
matrixA.col(1) = -(Dt - Ct);
Eigen::Matrix<T, 3, 1, MOpt> b = Ct - At;
Eigen::Matrix<T, 2, 2> inv;
bool invertible;
(matrixA.transpose() * matrixA).computeInverseWithCheck(inv, invertible);
if (!invertible)
{
return false;
}
*barycentricCoordinates = inv * matrixA.transpose() * b;
for (int i = 0; i < 2; 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
(*barycentricCoordinates)[0] >= 0.0 && (*barycentricCoordinates)[0] <= 1.0 &&
(*barycentricCoordinates)[1] >= 0.0 && (*barycentricCoordinates)[1] <= 1.0;
}
template <class T, int MOpt> inline
bool calculateCcdContactSegmentSegment(
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,
T* timeOfImpact, T* s0p1Factor, T* s1p1Factor)
{
std::array<T, 3> roots;
int numberOfRoots = timesOfCoplanarityInRange01(A, B, C, D, &roots);
// The roots are all in [0..1] and ordered ascendingly
for (int rootId = 0; rootId < numberOfRoots; ++rootId)
{
Eigen::Matrix<T, 2, 1, MOpt> barycentricCoordinates;
if (areSegmentsIntersecting(roots[rootId], A, B, C, D, &barycentricCoordinates))
{
// The segments AB and CD are coplanar at time t, and they intersect
*timeOfImpact = roots[rootId];
*s0p1Factor = barycentricCoordinates[0];
*s1p1Factor = barycentricCoordinates[1];
// None of these assertion should be necessary, but just to double check
SURGSIM_ASSERT(*timeOfImpact >= 0.0 && *timeOfImpact <= 1.0);
SURGSIM_ASSERT(*s0p1Factor >= 0.0 && *s0p1Factor <= 1.0);
SURGSIM_ASSERT(*s1p1Factor >= 0.0 && *s1p1Factor <= 1.0);
return true;
}
}
return false;
}
}; // namespace Math
}; // namespace SurgSim
#endif // SURGSIM_MATH_SEGMENTSEGMENTCCDCONTACTCALCULATION_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::calculateCcdContactSegmentSegment
bool calculateCcdContactSegmentSegment(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, T *timeOfImpact, T *s0p1Factor, T *s1p1Factor)
Continuous collision detection between two segments AB and CD.
Definition: SegmentSegmentCcdContactCalculation-inl.h:107
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
Scalar.h
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::areSegmentsIntersecting
bool areSegmentsIntersecting(T time, 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, Eigen::Matrix< T, 2, 1, MOpt > *barycentricCoordinates)
Check if 2 segments intersect at a given time of their motion.
Definition: SegmentSegmentCcdContactCalculation-inl.h:40