Unit CastleTriangles

Description

Triangles. In 2D, 3D and 4D space.

Uses

Overview

Classes, Interfaces, Objects and Records

Name Description
Record TTriangle2 Triangle in 2D space.
Record TTriangle3 Triangle in 3D space.
Record TTriangle4 Triangle in 4D (or 3D homogeneous) space.
Record TTriangleGeometry Triangle expressed in particular coordinate system, for TTriangle.
Object TFaceIndex Describe a range of indexes where the face (polygon and such) is located.
Record TTriangle Triangle in 3D.

Functions and Procedures

function Triangle3(const p0, p1, p2: TVector3): TTriangle3;
function IndexedTriangleNormal(const Indexes: TVector3Cardinal; VerticesArray: PVector3; VerticesStride: integer): TVector3;
function IndexedConvexPolygonNormal( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer; const ResultForIncorrectPoly: TVector3): TVector3; overload;
function IndexedConvexPolygonNormal( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer; const VertsStride: PtrUInt; const ResultForIncorrectPoly: TVector3): TVector3; overload;
function IndexedConvexPolygonArea( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer): Single; overload;
function IndexedConvexPolygonArea( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer; const VertsStride: PtrUInt): Single; overload;
function IsPolygon2dCCW(Verts: PVector2Array; const VertsCount: Integer): Single; overload;
function IsPolygon2dCCW(const Verts: array of TVector2): Single; overload;
function Polygon2dArea(Verts: PVector2Array; const VertsCount: Integer): Single; overload;
function Polygon2dArea(const Verts: array of TVector2): Single; overload;
function IsPointOnTrianglePlaneWithinTriangle(const P: TVector3; const Tri: TTriangle3; const TriDir: TVector3): boolean; overload;
function IsPointWithinTriangle2D(const P: TVector2; const Tri: TTriangle2): boolean; overload;
function IsPointWithinTriangle2D(const P: TVector2; const Tri: TTriangle3): boolean; overload;
function IsTriangleSegmentCollision(const Tri: TTriangle3; const TriPlane: TVector4; const Pos1, Pos2: TVector3): boolean; overload;
function IsTriangleSegmentCollision(const Tri: TTriangle3; const Pos1, Pos2: TVector3): boolean; overload;
function IsTriangleSphereCollision(const Tri: TTriangle3; const TriPlane: TVector4; const SphereCenter: TVector3; SphereRadius: Single): boolean; overload;
function IsTriangleSphereCollision(const Tri: TTriangle3; const SphereCenter: TVector3; SphereRadius: Single): boolean; overload;
function IsTriangleSphereCollision2D(const Tri: TTriangle2; const SphereCenter: TVector2; SphereRadius: Single): boolean; overload;
function IsTriangleSphereCollision2D(const Tri: TTriangle3; const SphereCenter: TVector2; SphereRadius: Single): boolean; overload;
function TryTriangleSegmentCollision(var Intersection: TVector3; const Tri: TTriangle3; const TriPlane: TVector4; const Pos1, Pos2: TVector3): boolean; overload;
function TryTriangleSegmentDirCollision(var Intersection: TVector3; const Tri: TTriangle3; const TriPlane: TVector4; const Segment0, SegmentVector: TVector3): boolean; overload;
function TryTriangleSegmentDirCollision(var Intersection: TVector3; var T: Single; const Tri: TTriangle3; const TriPlane: TVector4; const Segment0, SegmentVector: TVector3): boolean; overload;
function TryTriangleRayCollision(var Intersection: TVector3; const Tri: TTriangle3; const TriPlane: TVector4; const RayOrigin, RayDirection: TVector3): boolean; overload;
function TryTriangleRayCollision(var Intersection: TVector3; var T: Single; const Tri: TTriangle3; const TriPlane: TVector4; const RayOrigin, RayDirection: TVector3): boolean; overload;
function TriangleDirection(const p0, p1, p2: TVector3): TVector3; overload;
function TriangleDir(const p0, p1, p2: TVector3): TVector3; overload; deprecated 'use TriangleDirection';
function TriangleNormal(const p0, p1, p2: TVector3): TVector3; overload;
function TrianglePlane(const p0, p1, p2: TVector3): TVector4; overload;
function TriangleDir(const T: TTriangle3): TVector3; overload; deprecated 'use Triangle.Direction';
function TriangleNormal(const T: TTriangle3): TVector3; overload; deprecated 'use Triangle.Normal';
function TrianglePlane(const T: TTriangle3): TVector4; overload; deprecated 'use Triangle.Plane';
function TriangleTransform(const T: TTriangle3; const M: TMatrix4): TTriangle3; deprecated 'use Triangle.Transform';
function TriangleNormPlane(const T: TTriangle3): TVector4; deprecated 'use Triangle.NormalizedPlane';
function TriangleArea(const T: TTriangle3): Single; deprecated 'use Triangle.Area';
function Barycentric(const T: TTriangle3; const Point: TVector3): TVector3; deprecated 'use Triangle.Barycentric';
function TriangleToNiceStr(const T: TTriangle3): string; deprecated 'use T.ToString';

Types

PTriangle2 = ˆTTriangle2;
TTriangle3List = specialize TStructList<TTriangle3>;
PTriangle3 = ˆTTriangle3;
PTriangle4 = ˆTTriangle4;
TTriangle2Single = TTriangle2 deprecated 'use TTriangle2';
TTriangle3Single = TTriangle3 deprecated 'use TTriangle3';
TTriangle4Single = TTriangle4 deprecated 'use TTriangle4';
PTriangle2Single = PTriangle2 deprecated 'use PTriangle2';
PTriangle3Single = PTriangle3 deprecated 'use PTriangle3';
PTriangle4Single = PTriangle4 deprecated 'use PTriangle4';
TFaceIndexesList = specialize TStructList<TFaceIndex>;
TMailboxTag = Int64;
PTriangle = ˆTTriangle;
TTriangleList = specialize TStructList<TTriangle>;
TTriangleIgnoreFunc = function (const Sender: TObject; const Triangle: PTriangle): boolean of object;
T3DTriangleGeometry = TTriangleGeometry deprecated 'use TTriangleGeometry';
T3DTriangle = TTriangle deprecated 'use TTriangle';
P3DTriangle = PTriangle deprecated 'use PTriangle';
T3DTriangleIgnoreFunc = TTriangleIgnoreFunc deprecated 'use TTriangleIgnoreFunc';

Constants

UnknownFaceIndex: TFaceIndex = (IndexBegin: -1; IndexEnd: -1);

Variables

TriangleCollisionTestsCounter: Cardinal;

Description

Functions and Procedures

function Triangle3(const p0, p1, p2: TVector3): TTriangle3;
 
function IndexedTriangleNormal(const Indexes: TVector3Cardinal; VerticesArray: PVector3; VerticesStride: integer): TVector3;

Normal vector of a triangle defined as three indexes intro vertex array. VerticesStride is the shift between vertex values in the array, VerticesStride = 0 behaves like VerticesStride = SizeOf(TVector3).

function IndexedConvexPolygonNormal( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer; const ResultForIncorrectPoly: TVector3): TVector3; overload;

Calculates normalized normal vector for polygon composed from indexed vertices. Polygon is defines as vertices Vertsˆ[Indices[0]], Vertsˆ[Indices[1]] ... Vertsˆ[Indices[IndicesCount-1]]. Returns normal pointing from CCW.

It's secured against invalid indexes on Indices list (that's the only reason why it takes VertsCount parameter, after all): they are ignored.

If the polygon is degenerated, that is it doesn't determine a plane in 3D space (this includes, but is not limited, to cases when there are less than 3 valid points, like when IndicesCount < 3) then it returns ResultForIncorrectPoly.

function IndexedConvexPolygonNormal( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer; const VertsStride: PtrUInt; const ResultForIncorrectPoly: TVector3): TVector3; overload;
 
function IndexedConvexPolygonArea( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer): Single; overload;

Surface area of indexed convex polygon. Polygon is defines as vertices Vertsˆ[Indices[0]], Vertsˆ[Indices[1]] ... Vertsˆ[Indices[IndicesCount-1]].

It's secured against invalid indexes on Indices list (that's the only reason why it takes VertsCount parameter, after all): they are ignored.

function IndexedConvexPolygonArea( Indices: PLongintArray; IndicesCount: integer; Verts: PVector3Array; const VertsCount: Integer; const VertsStride: PtrUInt): Single; overload;
 
function IsPolygon2dCCW(Verts: PVector2Array; const VertsCount: Integer): Single; overload;

Are the polygon points ordered CCW (counter-clockwise). When viewed with typical camera settings, that is +Y goes up and +X goes right.

Polygon doesn't have to be convex. Polygon doesn't have to have all triangles valid, that is it's OK if some polygon triangles degenerate into points or line segments.

Returns something > 0 if polygon is CCW, or < 0 when it's not. Returns zero when polygon has area 0.

function IsPolygon2dCCW(const Verts: array of TVector2): Single; overload;
 
function Polygon2dArea(Verts: PVector2Array; const VertsCount: Integer): Single; overload;

Calculate polygon area.

Polygon doesn't have to be convex. Polygon doesn't have to have all triangles valid, that is it's OK if some polygon triangles degenerate into points or line segments.

function Polygon2dArea(const Verts: array of TVector2): Single; overload;
 
function IsPointOnTrianglePlaneWithinTriangle(const P: TVector3; const Tri: TTriangle3; const TriDir: TVector3): boolean; overload;

Assuming a point lies on a triangle plane, check does it lie inside a triangle. Give first 3 components of triangle plane as TriDir.

function IsPointWithinTriangle2D(const P: TVector2; const Tri: TTriangle2): boolean; overload;

Check does point lie inside a triangle, in 2D.

function IsPointWithinTriangle2D(const P: TVector2; const Tri: TTriangle3): boolean; overload;
 
function IsTriangleSegmentCollision(const Tri: TTriangle3; const TriPlane: TVector4; const Pos1, Pos2: TVector3): boolean; overload;

Check triangle with line segment collision. You can pass the triangle plane along with a triangle, this will speed calculation.

function IsTriangleSegmentCollision(const Tri: TTriangle3; const Pos1, Pos2: TVector3): boolean; overload;
 
function IsTriangleSphereCollision(const Tri: TTriangle3; const TriPlane: TVector4; const SphereCenter: TVector3; SphereRadius: Single): boolean; overload;
 
function IsTriangleSphereCollision(const Tri: TTriangle3; const SphereCenter: TVector3; SphereRadius: Single): boolean; overload;
 
function IsTriangleSphereCollision2D(const Tri: TTriangle2; const SphereCenter: TVector2; SphereRadius: Single): boolean; overload;

Test collision between triangle and sphere in 2D. If you use overloaded version with TTriangle3, the Z coordinate of the triangle corners is simply ignored, so everything is projected on the Z=0 plane.

function IsTriangleSphereCollision2D(const Tri: TTriangle3; const SphereCenter: TVector2; SphereRadius: Single): boolean; overload;
 
function TryTriangleSegmentCollision(var Intersection: TVector3; const Tri: TTriangle3; const TriPlane: TVector4; const Pos1, Pos2: TVector3): boolean; overload;

Calculate triangle with line segment collision. You can pass the triangle plane along with a triangle, this will speed calculation.

When there's no intersection, returns False and doesn't modify Intersection or T.

function TryTriangleSegmentDirCollision(var Intersection: TVector3; const Tri: TTriangle3; const TriPlane: TVector4; const Segment0, SegmentVector: TVector3): boolean; overload;
 
function TryTriangleSegmentDirCollision(var Intersection: TVector3; var T: Single; const Tri: TTriangle3; const TriPlane: TVector4; const Segment0, SegmentVector: TVector3): boolean; overload;
 
function TryTriangleRayCollision(var Intersection: TVector3; const Tri: TTriangle3; const TriPlane: TVector4; const RayOrigin, RayDirection: TVector3): boolean; overload;

Calculate triangle with ray collision. You can pass the triangle plane along with a triangle, this will speed calculation.

When there's no intersection, returns False and doesn't modify Intersection or T.

function TryTriangleRayCollision(var Intersection: TVector3; var T: Single; const Tri: TTriangle3; const TriPlane: TVector4; const RayOrigin, RayDirection: TVector3): boolean; overload;
 
function TriangleDirection(const p0, p1, p2: TVector3): TVector3; overload;
 
function TriangleDir(const p0, p1, p2: TVector3): TVector3; overload; deprecated 'use TriangleDirection';

Warning: this symbol is deprecated: use TriangleDirection

 
function TriangleNormal(const p0, p1, p2: TVector3): TVector3; overload;
 
function TrianglePlane(const p0, p1, p2: TVector3): TVector4; overload;
 
function TriangleDir(const T: TTriangle3): TVector3; overload; deprecated 'use Triangle.Direction';

Warning: this symbol is deprecated: use Triangle.Direction

 
function TriangleNormal(const T: TTriangle3): TVector3; overload; deprecated 'use Triangle.Normal';

Warning: this symbol is deprecated: use Triangle.Normal

 
function TrianglePlane(const T: TTriangle3): TVector4; overload; deprecated 'use Triangle.Plane';

Warning: this symbol is deprecated: use Triangle.Plane

 
function TriangleTransform(const T: TTriangle3; const M: TMatrix4): TTriangle3; deprecated 'use Triangle.Transform';

Warning: this symbol is deprecated: use Triangle.Transform

 
function TriangleNormPlane(const T: TTriangle3): TVector4; deprecated 'use Triangle.NormalizedPlane';

Warning: this symbol is deprecated: use Triangle.NormalizedPlane

 
function TriangleArea(const T: TTriangle3): Single; deprecated 'use Triangle.Area';

Warning: this symbol is deprecated: use Triangle.Area

 
function Barycentric(const T: TTriangle3; const Point: TVector3): TVector3; deprecated 'use Triangle.Barycentric';

Warning: this symbol is deprecated: use Triangle.Barycentric

 
function TriangleToNiceStr(const T: TTriangle3): string; deprecated 'use T.ToString';

Warning: this symbol is deprecated: use T.ToString

 

Types

PTriangle2 = ˆTTriangle2;
 
TTriangle3List = specialize TStructList<TTriangle3>;
 
PTriangle3 = ˆTTriangle3;
 
PTriangle4 = ˆTTriangle4;
 
TTriangle2Single = TTriangle2 deprecated 'use TTriangle2';

Warning: this symbol is deprecated: use TTriangle2

 
TTriangle3Single = TTriangle3 deprecated 'use TTriangle3';

Warning: this symbol is deprecated: use TTriangle3

 
TTriangle4Single = TTriangle4 deprecated 'use TTriangle4';

Warning: this symbol is deprecated: use TTriangle4

 
PTriangle2Single = PTriangle2 deprecated 'use PTriangle2';

Warning: this symbol is deprecated: use PTriangle2

 
PTriangle3Single = PTriangle3 deprecated 'use PTriangle3';

Warning: this symbol is deprecated: use PTriangle3

 
PTriangle4Single = PTriangle4 deprecated 'use PTriangle4';

Warning: this symbol is deprecated: use PTriangle4

 
TFaceIndexesList = specialize TStructList<TFaceIndex>;
 
TMailboxTag = Int64;

TTriangle ——————————————————————

PTriangle = ˆTTriangle;
 
TTriangleList = specialize TStructList<TTriangle>;
 
TTriangleIgnoreFunc = function (const Sender: TObject; const Triangle: PTriangle): boolean of object;

Return for given Triangle do we want to ignore collisions with it. For now, Sender is always TTriangleOctree.

T3DTriangleGeometry = TTriangleGeometry deprecated 'use TTriangleGeometry';

Warning: this symbol is deprecated: use TTriangleGeometry

 
T3DTriangle = TTriangle deprecated 'use TTriangle';

Warning: this symbol is deprecated: use TTriangle

 
P3DTriangle = PTriangle deprecated 'use PTriangle';

Warning: this symbol is deprecated: use PTriangle

 
T3DTriangleIgnoreFunc = TTriangleIgnoreFunc deprecated 'use TTriangleIgnoreFunc';

Warning: this symbol is deprecated: use TTriangleIgnoreFunc

 

Constants

UnknownFaceIndex: TFaceIndex = (IndexBegin: -1; IndexEnd: -1);
 

Variables

TriangleCollisionTestsCounter: Cardinal;

Counter of collision tests done by TTriangle when the actual collision calculation had to be done. This counts all calls to TTriangle.SegmentDirCollision and TTriangle.RayCollision when the result had to be actually geometrically calculated (result was not in the cache aka "mailbox").

It is especially useful to look at this after using some spatial data structure, like an octree. The goal of tree structures is to minimize this number.

It is a global variable, because that's the most comfortable way to use it. Triangles are usually wrapped in an octree (like TTriangleOctree), or even in an octree of octrees (like TShapeOctree). Tracking collisions using the global variable is most comfortable, instead of spending time on propagating this (purely debugging) information through the octree structures.


Generated by PasDoc 0.15.0.