本文整理汇总了C++中vec3f::dist2方法的典型用法代码示例。如果您正苦于以下问题:C++ vec3f::dist2方法的具体用法?C++ vec3f::dist2怎么用?C++ vec3f::dist2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vec3f
的用法示例。
在下文中一共展示了vec3f::dist2方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: NearestPointInTriangle
// Given a point and a triangle (defined by three points), compute the closest point
// in the triangle. Clamp the point so it's confined to the area of the triangle.
vec3f NearestPointInTriangle(const vec3f &point, const vec3f &triangle0,
const vec3f &triangle1, const vec3f &triangle2)
{
vec3f zeroVector(0, 0, 0);
vec3f nearestPoint;
vec3f lineDelta0 = triangle1 - triangle0;
vec3f lineDelta1 = triangle2 - triangle0;
// Handle degenerate triangles
if ( (lineDelta0 == zeroVector) || (lineDelta1 == zeroVector) )
{
nearestPoint = NearestPointInLineSegment(point, triangle1, triangle2);
}
else if ( lineDelta0 == lineDelta1 )
{
nearestPoint = NearestPointInLineSegment(point, triangle0, triangle1);
}
else
{
static vec3f axis[3];
axis[0] = NearestPointInLine(triangle0, triangle1, triangle2);
axis[1] = NearestPointInLine(triangle1, triangle0, triangle2);
axis[2] = NearestPointInLine(triangle2, triangle0, triangle1);
float axisDot[3];
axisDot[0] = (triangle0-axis[0]).dot(point-axis[0]);
axisDot[1] = (triangle1-axis[1]).dot(point-axis[1]);
axisDot[2] = (triangle2-axis[2]).dot(point-axis[2]);
bool bForce = true;
float bestMagnitude2 = 0;
float closeMagnitude2;
vec3f closePoint;
if ( axisDot[0] < 0 )
{
closePoint = NearestPointInLineSegment(point, triangle1, triangle2);
closeMagnitude2 = point.dist2(closePoint);
if ( bForce || (bestMagnitude2 > closeMagnitude2) )
{
bForce = false;
bestMagnitude2 = closeMagnitude2;
nearestPoint = closePoint;
}
}
if ( axisDot[1] < 0 )
{
closePoint = NearestPointInLineSegment(point, triangle0, triangle2);
closeMagnitude2 = point.dist2(closePoint);
if ( bForce || (bestMagnitude2 > closeMagnitude2) )
{
bForce = false;
bestMagnitude2 = closeMagnitude2;
nearestPoint = closePoint;
}
}
if ( axisDot[2] < 0 )
{
closePoint = NearestPointInLineSegment(point, triangle0, triangle1);
closeMagnitude2 = point.dist2(closePoint);
if ( bForce || (bestMagnitude2 > closeMagnitude2) )
{
bForce = false;
bestMagnitude2 = closeMagnitude2;
nearestPoint = closePoint;
}
}
// If bForce is true at this point, it means the nearest point lies
// inside the triangle; use the nearest-point-on-a-plane equation
if ( bForce )
{
vec3f normal;
// Get the normal of the polygon (doesn't have to be a unit vector)
normal.cross(lineDelta0, lineDelta1);
vec3f pointDelta = point - triangle0;
float delta = normal.dot(pointDelta) / normal.dot(normal);
nearestPoint = point - delta*normal;
}
}
return nearestPoint;
}