UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DistLine3Segment3.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of geometry3Sharp DistLine3Segment3
4// which was ported from WildMagic 5
5
6#pragma once
7
8#include "VectorTypes.h"
9#include "SegmentTypes.h"
10#include "LineTypes.h"
11
12namespace UE
13{
14namespace Geometry
15{
16
17using namespace UE::Math;
18
22template <typename Real>
24{
25public:
26 // Input
29
30 // Output
31 Real DistanceSquared = -1.0;
34
35
39
40 Real Get()
41 {
43 }
45 {
46 return ComputeResult();
47 }
48
50 {
51 if (DistanceSquared >= 0)
52 {
53 return DistanceSquared;
54 }
55
58 Real b0 = diff.Dot(Line.Direction);
59 Real c = diff.SquaredLength();
60 Real det = TMathUtil<Real>::Abs(1 - a01 * a01);
61 Real b1, s0, s1, sqrDist, extDet;
62
64 {
65 // The Line and Segment are not parallel.
66 b1 = -diff.Dot(Segment.Direction);
67 s1 = a01 * b0 - b1;
69
70 if (s1 >= -extDet)
71 {
72 if (s1 <= extDet)
73 {
74 // Two interior points are closest, one on the Line and one
75 // on the Segment.
76 Real invDet = (1) / det;
77 s0 = (a01 * b1 - b0) * invDet;
78 s1 *= invDet;
79 sqrDist = s0 * (s0 + a01 * s1 + (2) * b0) +
80 s1 * (a01 * s0 + s1 + (2) * b1) + c;
81 }
82 else
83 {
84 // The endpoint e1 of the Segment and an interior point of
85 // the Line are closest.
87 s0 = -(a01 * s1 + b0);
88 sqrDist = -s0 * s0 + s1 * (s1 + (2) * b1) + c;
89 }
90 }
91 else
92 {
93 // The end point e0 of the Segment and an interior point of the
94 // Line are closest.
95 s1 = -Segment.Extent;
96 s0 = -(a01 * s1 + b0);
97 sqrDist = -s0 * s0 + s1 * (s1 + (2) * b1) + c;
98 }
99 }
100 else
101 {
102 // The Line and Segment are parallel. Choose the closest pair so that
103 // one point is at Segment center.
104 s1 = 0;
105 s0 = -b0;
106 sqrDist = b0 * s0 + c;
107 }
108
113
114 // Account for numerical round-off errors.
115 if (sqrDist < 0)
116 {
117 sqrDist = 0;
118 }
119
121
122 return DistanceSquared;
123 }
124};
125
128
132template <typename Real>
134{
135 double SegmentLength = Segment.Length();
136
137 TVector<Real> SegmentStart = Segment.StartPoint();
138 // Point at length t along segment is SegmentStart + Segment.Direction * t
139
140 TVector<Real> ProjectedSegmentStart = Line.Origin + (SegmentStart - Line.Origin).Dot(Line.Direction) * Line.Direction;
141 TVector<Real> ProjectedSegmentDirection = Segment.Direction.Dot(Line.Direction) * Line.Direction;
142 // Projected point onto line at t is ProjectedSegmentStart + ProjectedSegmentDirection * t
143
144 // Difference of point on segment and projected point is the vector that we actually want to integrate over t.
147
148 // Squared length of this vector is sum of component-wise squares, and after doing some math, you get that
149 // the integral from 0 to SegmentLength is:
150 // p_0.p_0(SegmentLength) + p_0.p_v(SegmentLength^2) + p_v.p_v (SegmentLength^3)/3
151 // Rearranging a bit:
152 return ((P_v.Dot(P_v) * SegmentLength / 3.0 + P_0.Dot(P_v)) * SegmentLength + P_0.Dot(P_0)) * SegmentLength;
153}
154
155} // end namespace UE::Geometry
156} // 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
static RealType Abs(const RealType Value)
Definition MathUtil.h:215
Definition DistLine3Segment3.h:24
Real ComputeResult()
Definition DistLine3Segment3.h:49
TVector< Real > LineClosest
Definition DistLine3Segment3.h:33
Real GetSquared()
Definition DistLine3Segment3.h:44
Real Get()
Definition DistLine3Segment3.h:40
TLine3< Real > Line
Definition DistLine3Segment3.h:27
Real SegmentParameter
Definition DistLine3Segment3.h:32
Real LineParameter
Definition DistLine3Segment3.h:32
TSegment3< Real > Segment
Definition DistLine3Segment3.h:28
TDistLine3Segment3(const TLine3< Real > &LineIn, const TSegment3< Real > &SegmentIn)
Definition DistLine3Segment3.h:36
Real DistanceSquared
Definition DistLine3Segment3.h:31
TVector< Real > SegmentClosest
Definition DistLine3Segment3.h:33
TDistLine3Segment3< double > FDistLine3Segment3d
Definition DistLine3Segment3.h:127
TDistLine3Segment3< float > FDistLine3Segment3f
Definition DistLine3Segment3.h:126
double SquaredDistanceFromLineIntegratedAlongSegment(const TLine3< Real > &Line, const TSegment3< Real > &Segment)
Definition DistLine3Segment3.h:133
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
TVector< T > Center
Definition SegmentTypes.h:450
TVector< T > Direction
Definition SegmentTypes.h:452
Definition Vector.h:51
UE_FORCEINLINE_HINT T Dot(const TVector< T > &V) const
Definition Vector.h:1553