UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DistLine3Triangle3.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of geometry3Sharp DistLine3Triangle3
4// which was ported from WildMagic 5
5
6#pragma once
7
8#include "VectorTypes.h"
9#include "TriangleTypes.h"
10#include "SegmentTypes.h"
11#include "DistLine3Segment3.h"
12
13namespace UE
14{
15namespace Geometry
16{
17
18using namespace UE::Math;
19
23template <typename Real>
25{
26public:
27 // Input
30
31 // Output
32 Real DistanceSquared = -1.0;
35
36
40
41 Real Get()
42 {
44 }
46 {
47 return ComputeResult();
48 }
49
51 {
52 if (DistanceSquared >= 0)
53 {
54 return DistanceSquared;
55 }
56
57 // Test if Line intersects Triangle. If so, the squared distance is zero.
62 Real NdD = normal.Dot(Line.Direction);
64 {
65 // The line and triangle are not parallel, so the line intersects
66 // the plane of the triangle.
68 TVector<Real> U, V;
70 Real UdE0 = U.Dot(edge0);
71 Real UdE1 = U.Dot(edge1);
72 Real UdDiff = U.Dot(diff);
73 Real VdE0 = V.Dot(edge0);
74 Real VdE1 = V.Dot(edge1);
75 Real VdDiff = V.Dot(diff);
76 Real invDet = (1) / (UdE0 * VdE1 - UdE1 * VdE0);
77
78 // Barycentric coordinates for the point of intersection.
79 Real b1 = (VdE1 * UdDiff - UdE1 * VdDiff) * invDet;
80 Real b2 = (UdE0 * VdDiff - VdE0 * UdDiff) * invDet;
81 Real b0 = 1 - b1 - b2;
82
83 if (b0 >= 0 && b1 >= 0 && b2 >= 0)
84 {
85 // Line parameter for the point of intersection.
86 Real DdE0 = Line.Direction.Dot(edge0);
87 Real DdE1 = Line.Direction.Dot(edge1);
89 LineParam = b1 * DdE0 + b2 * DdE1 - DdDiff;
90
91 // Barycentric coordinates for the point of intersection.
93
94 // The intersection point is inside or on the Triangle.
98 return 0;
99 }
100 }
101
102 // Either (1) the Line is not parallel to the Triangle and the point of
103 // intersection of the Line and the plane of the Triangle is outside the
104 // Triangle or (2) the Line and Triangle are parallel. Regardless, the
105 // closest point on the Triangle is on an edge of the Triangle. Compare
106 // the Line to all three edges of the Triangle.
108 for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
109 {
112 Real sqrDistTmp = queryLS.GetSquared();
113 if (sqrDistTmp < sqrDist)
114 {
115 LineClosest = queryLS.LineClosest;
116 TriangleClosest = queryLS.SegmentClosest;
118 LineParam = queryLS.LineParameter;
119 Real ratio = queryLS.SegmentParameter / segment.Extent;
120 TriangleBaryCoords[i0] = (0.5) * (1 - ratio);
122 TriangleBaryCoords[3 - i0 - i1] = 0;
123 }
124 }
125
127 return DistanceSquared;
128 }
129};
130
133
134} // end namespace UE::Geometry
135} // end namespace UE
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition MathUtil.h:150
static RealType Sqrt(const RealType Value)
Definition MathUtil.h:342
Definition DistLine3Segment3.h:24
Definition DistLine3Triangle3.h:25
TTriangle3< Real > Triangle
Definition DistLine3Triangle3.h:29
TVector< Real > LineClosest
Definition DistLine3Triangle3.h:34
TLine3< Real > Line
Definition DistLine3Triangle3.h:28
TVector< Real > TriangleClosest
Definition DistLine3Triangle3.h:34
TVector< Real > TriangleBaryCoords
Definition DistLine3Triangle3.h:34
Real Get()
Definition DistLine3Triangle3.h:41
Real ComputeResult()
Definition DistLine3Triangle3.h:50
Real GetSquared()
Definition DistLine3Triangle3.h:45
Real DistanceSquared
Definition DistLine3Triangle3.h:32
TDistLine3Triangle3(const TLine3< Real > &LineIn, const TTriangle3< Real > &TriangleIn)
Definition DistLine3Triangle3.h:37
Real LineParam
Definition DistLine3Triangle3.h:33
void MakePerpVectors(const TVector< RealType > &Normal, TVector< RealType > &OutPerp1, TVector< RealType > &OutPerp2)
Definition VectorUtil.h:211
TDistLine3Triangle3< float > FDistLine3Triangle3f
Definition DistLine3Triangle3.h:131
TDistLine3Triangle3< double > FDistLine3Triangle3d
Definition DistLine3Triangle3.h:132
T Normalize(UE::Math::TVector2< T > &Vector, const T Epsilon=0)
Definition VectorTypes.h:46
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition LineTypes.h:148
TVector< T > Direction
Definition LineTypes.h:153
TVector< T > Origin
Definition LineTypes.h:150
Definition SegmentTypes.h:447
T Extent
Definition SegmentTypes.h:454
Definition TriangleTypes.h:263
TVector< RealType > V[3]
Definition TriangleTypes.h:264
Definition Vector.h:51
UE_FORCEINLINE_HINT TVector< T > Cross(const TVector< T > &V2) const
Definition Vector.h:1535
UE_FORCEINLINE_HINT T Dot(const TVector< T > &V) const
Definition Vector.h:1553