UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IntrSegment2Triangle2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of WildMagic TIntrSegment2Triangle2
4
5#pragma once
6
7#include "VectorTypes.h"
8#include "IntVectorTypes.h"
9#include "SegmentTypes.h"
10#include "TriangleTypes.h"
11#include "VectorUtil.h"
12
14
15namespace UE
16{
17namespace Geometry
18{
19
20using namespace UE::Math;
21
26template <typename Real>
28{
29protected:
30 // Input
33
34public:
35 // Output
36 int Quantity = 0;
39
44
45
48 double Param0;
49 double Param1;
50
52 {
53 return Segment;
54 }
56 {
57 return Triangle;
58 }
69
75
76
78 {
79 Find(Tolerance);
80 return this;
81 }
82
83
84 bool Find(Real Tolerance = TMathUtil<Real>::ZeroTolerance)
85 {
87 {
89 }
90
91 // if segment extent is zero, segment is just a point, so just check the center vs the tri
92 // (Note this is also the case where Segment.Direction is the zero vector)
93 if (Segment.Extent == (Real)0)
94 {
95 int pos = 0, neg = 0;
96 for (int TriPrev = 2, TriIdx = 0; TriIdx < 3; TriPrev = TriIdx++)
97 {
100 Real EdgeLen = Normalize(EdgePerp);
101 if (EdgeLen == 0) // triangle is just a line segment; try edgeperp = one of the other edges
102 {
106 if (EdgeLen == 0) // triangle is just a point; go by distance between vertex and center
107 {
108 if ( DistanceSquared(Triangle.V[0], Segment.Center) <= Tolerance*Tolerance)
109 {
110 pos = neg = 0; // closer than tolerance; accept collision
111 }
112 else
113 {
114 pos = neg = 1;
115 }
116 break;
117 }
118 else // valid edge, test from the other side of the degenerate tri as well
119 {
123 if (OtherSideSign < -Tolerance)
124 {
125 neg++;
126 }
127 else if (OtherSideSign > Tolerance)
128 {
129 pos++;
130 }
131 }
132 }
133
134 Real SideSign = EdgePerp.Dot(ToPt);
135 if (SideSign < -Tolerance)
136 {
137 neg++;
138 }
139 else if (SideSign > Tolerance)
140 {
141 pos++;
142 }
143 }
144 if (pos == 0 || neg == 0)
145 {
146 // note: intersection is a single point but conceptually a segment intersection
147 // -- the whole (degenerate) segment intersects the triangle
150 Quantity = 2;
152 Param0 = 0;
154 Param1 = 0;
155 return true;
156 }
157 else
158 {
161 return false;
162 }
163 }
164
165 // FSegment2d Direction should always be normalized unless Extent is 0 (handled above)
167
168 TVector<Real> dist;
170 int positive = 0, negative = 0, zero = 0;
172 dist, sign, positive, negative, zero, Tolerance);
173
174 if (positive == 3 || negative == 3)
175 {
176 // No intersections.
177 Quantity = 0;
179 }
180 else
181 {
184
186 intr.Find();
187
188 Quantity = intr.NumIntersections;
189 if (Quantity == 2)
190 {
191 // Segment intersection.
193 Param0 = intr.GetIntersection(0);
195 Param1 = intr.GetIntersection(1);
197 }
198 else if (Quantity == 1)
199 {
200 // Point intersection.
202 Param0 = intr.GetIntersection(0);
204 }
205 else {
206 // No intersections.
208 }
209 }
210
214 }
215
216
217
218
219};
220
223
224} // end namespace UE::Geometry
225} // end namespace UE
#define checkSlow(expr)
Definition AssertionMacros.h:332
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EIntersectionResult
Definition VectorUtil.h:10
EIntersectionType
Definition VectorUtil.h:18
Definition MathUtil.h:150
Definition Intersector1.h:25
static void TriangleLineRelations(const TVector2< Real > &Origin, const TVector2< Real > &Direction, const TTriangle2< Real > &Tri, TVector< Real > &Dist, FVector3i &Sign, int &Positive, int &Negative, int &Zero, Real Tolerance=TMathUtil< Real >::ZeroTolerance)
Definition IntrLine2Triangle2.h:148
static bool GetInterval(const TVector2< Real > &Origin, const TVector2< Real > &Direction, const TTriangle2< Real > &Tri, const TVector< Real > &Dist, const FVector3i &Sign, TVector2< Real > &param)
Definition IntrLine2Triangle2.h:179
Definition IntrSegment2Triangle2.h:28
bool IsSimpleIntersection()
Definition IntrSegment2Triangle2.h:40
int Quantity
Definition IntrSegment2Triangle2.h:36
TSegment2< Real > Segment
Definition IntrSegment2Triangle2.h:31
bool Find(Real Tolerance=TMathUtil< Real >::ZeroTolerance)
Definition IntrSegment2Triangle2.h:84
void SetSegment(const TSegment2< Real > &SegmentIn)
Definition IntrSegment2Triangle2.h:59
TVector2< Real > Point1
Definition IntrSegment2Triangle2.h:47
TIntrSegment2Triangle2(TSegment2< Real > Seg, TTriangle2< Real > Tri)
Definition IntrSegment2Triangle2.h:72
TVector2< Real > Point0
Definition IntrSegment2Triangle2.h:46
EIntersectionResult Result
Definition IntrSegment2Triangle2.h:37
TSegment2< Real > GetSegment() const
Definition IntrSegment2Triangle2.h:51
TTriangle2< Real > Triangle
Definition IntrSegment2Triangle2.h:32
EIntersectionType Type
Definition IntrSegment2Triangle2.h:38
TTriangle2< Real > GetTriangle() const
Definition IntrSegment2Triangle2.h:55
double Param1
Definition IntrSegment2Triangle2.h:49
TIntrSegment2Triangle2 * Compute(Real Tolerance=TMathUtil< Real >::ZeroTolerance)
Definition IntrSegment2Triangle2.h:77
double Param0
Definition IntrSegment2Triangle2.h:48
TIntrSegment2Triangle2()
Definition IntrSegment2Triangle2.h:70
void SetTriangle(const TTriangle2< Real > &TriangleIn)
Definition IntrSegment2Triangle2.h:64
constexpr UE::Math::TVector2< T > PerpCW(const UE::Math::TVector2< T > &V)
Definition VectorTypes.h:26
T DistanceSquared(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:82
TIntrSegment2Triangle2< float > FIntrSegment2Triangle2f
Definition IntrSegment2Triangle2.h:221
T Normalize(UE::Math::TVector2< T > &Vector, const T Epsilon=0)
Definition VectorTypes.h:46
TIntrSegment2Triangle2< double > FIntrSegment2Triangle2d
Definition IntrSegment2Triangle2.h:222
constexpr bool IsNormalized(const UE::Math::TVector2< T > &Vector, const T Tolerance=TMathUtil< T >::ZeroTolerance)
Definition VectorTypes.h:40
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition IntVectorTypes.h:252
Definition SegmentTypes.h:23
T Extent
Definition SegmentTypes.h:30
TVector2< T > Direction
Definition SegmentTypes.h:28
TVector2< T > Center
Definition SegmentTypes.h:26
Definition TriangleTypes.h:39
TVector2< RealType > V[3]
Definition TriangleTypes.h:40
Definition Vector2D.h:38
T Dot(const TVector2< T > &V2) const
Definition Vector2D.h:1123
Definition Vector.h:51