# Record TTriangle3

## Declaration

`type TTriangle3 = record`

## Description

Triangle in 3D space.

Triangle's three points must not be collinear, i.e. routines in this unit generally don't accept "degenerated" triangles that are not really triangles. So 3D triangle must unambiguously define some plane in the 3D space. The only function in this unit that is able to handle "degenerated" triangles is TTriangle3.IsValid, which is exactly used to check whether the triangle is degenerated.

## Overview

### Nested Types `TIndex = 0..2;`

### Fields `Data: packed [TIndex] of TVector3;`

### Methods `function ToString: string;` `function IsValid: boolean;` `function Direction: TVector3;` `function Normal: TVector3;` `function Plane: TVector4;` `function NormalizedPlane: TVector4;` `function Transform(const M: TMatrix4): TTriangle3;` `function Area: Single;` `function AreaSqr: Single;` `function RandomPoint: TVector3;` `function Barycentric(const Point: TVector3): TVector3;`

### Properties `property Items [constIndex:TIndex]: TVector3 read GetItems write SetItems;`

## Description

### Nested Types `TIndex = 0..2;`

### Fields `Data: packed [TIndex] of TVector3;`

### Methods `function ToString: string;` Multiline triangle description. `function IsValid: boolean;` Check does the triangle define a correct plane in 3D space. That is, check does the triangle not degenerate to a point or line segment (which can happen when some points are at the same position, or are colinear). `function Direction: TVector3;` Like a normal vector of a triangle (see Normal), but not necessarily normalized. `function Normal: TVector3;` Normal vector of a triangle. Returns vector pointing our from CCW triangle side (for right-handed coordinate system), and orthogonal to triangle plane. For degenerated triangles (when IsValid would return `False`), we return zero vector. `function Plane: TVector4;` Plane of the triangle. Note that this has many possible solutions (plane representation as equation `Ax + By + Cz + D = 0` is not unambiguous), this just returns some solution deterministically. It's guaranteed that the direction of this plane (i.e. first 3 items of returned vector) will be in the same direction as calcualted by Direction, which means that it points outward from CCW side of the triangle (assuming right-handed coord system). For NormalizedPlane, this direction is also normalized (makes a vector with length 1). This way NormalizedPlane calculates also Normal. For three points that do not define a plane, a plane with first three components = 0 is returned. In fact, the 4th component will be zero too in this case (for now), but don't depend on it. `function NormalizedPlane: TVector4;` `function Transform(const M: TMatrix4): TTriangle3;`

Transform triangle by 4x4 matrix. This simply transforms each triangle point.

###### Exceptions raised
ETransformedResultInvalid
Raised when matrix will transform some point to a direction (vector with 4th component equal zero). In this case we just cannot interpret the result as a 3D point. `function Area: Single;` Surface area of 3D triangle. This works for degenerated (equal to line segment or even single point) triangles too: returns 0 for them. `function AreaSqr: Single;` `function RandomPoint: TVector3;` Random triangle point, chosen with a constant density for triangle area. `function Barycentric(const Point: TVector3): TVector3;` For a given Point lying on a given Triangle, calculate it's barycentric coordinates. The resulting Barycentric coordinates can be used for linearly interpolating values along the triangle, as they satisfy the equation: ```Result.Data * Triangle.Data + Result.Data * Triangle.Data + Result.Data * Triangle.Data = Point```

### Properties `property Items [constIndex:TIndex]: TVector3 read GetItems write SetItems;` Access the points of the triangle. This is the same as accessing Data field, it is also the default record property so you can just write `MyTriangle` instead of `MyTriangle.Items` or `MyTriangle.Data`.

Generated by PasDoc 0.15.0.