Triangle

Struct Triangle 

Source
#[repr(C)]
pub struct Triangle { pub a: Point3, pub b: Point3, pub c: Point3, }
Expand description

A triangle defined by three vertices in 3D space.

A triangle is the simplest polygon in 3D space, consisting of three vertices connected by three edges. It’s a fundamental building block in computer graphics, computational geometry, and physics simulations.

The triangle is defined by three vertices A, B, and C. The orientation and properties are computed based on the relative positions of these vertices.

§Examples

use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

// Create a triangle
let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);

§Mathematical Properties

  • Area = 0.5 * |(B-A) × (C-A)|
  • Normal = normalize((B-A) × (C-A))
  • Center = (A + B + C) / 3

Fields§

§a: Point3

First vertex of the triangle

§b: Point3

Second vertex of the triangle

§c: Point3

Third vertex of the triangle

Implementations§

Source§

impl Triangle

Source

pub fn new( a: impl Into<[Real; 3]>, b: impl Into<[Real; 3]>, c: impl Into<[Real; 3]>, ) -> Self

Creates a new triangle from three vertices.

§Arguments
  • a - First vertex
  • b - Second vertex
  • c - Third vertex
§Returns

A new Triangle instance.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);
Source

pub fn center(&self) -> Point3

Computes the geometric center (centroid) of the triangle.

The centroid is calculated as the average of all three vertices: C = (A + B + C) / 3

§Returns

The centroid coordinates as a Point3.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(2.0, 0.0, 0.0),
    Point3::new(0.0, 2.0, 0.0),
);

let center = triangle.center();
assert_eq!(center, Point3::new(2.0/3.0, 2.0/3.0, 0.0));
Source

pub fn normal(&self) -> UnitVec3

Computes the normal vector of the triangle.

The normal is calculated using the cross product of two edges: N = normalize((B-A) × (C-A))

The direction follows the right-hand rule based on the vertex order (A, B, C).

§Returns

The unit normal vector as a UnitVec3.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::{Point3, UnitVec3};

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);

let normal = triangle.normal();
// Triangle lies in the XY plane, normal points in +Z direction
assert!(normal.z > 0.9); // Close to 1.0 for unit vector
Source

pub fn area(&self) -> Real

Computes the area of the triangle.

The area is calculated using half the magnitude of the cross product: Area = 0.5 * |(B-A) × (C-A)|

§Returns

The area as a Real value.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(2.0, 0.0, 0.0),
    Point3::new(0.0, 2.0, 0.0),
);

let area = triangle.area();
assert_eq!(area, 2.0); // Right triangle with legs of length 2
Source

pub fn is_degenerate(&self) -> bool

Checks if the triangle is degenerate (has zero area).

A triangle is degenerate if all three vertices are collinear, which means the cross product of two edges has zero (or near-zero) magnitude.

§Returns

true if the triangle is degenerate, false otherwise.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

// Non-degenerate triangle
let triangle1 = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);
assert!(!triangle1.is_degenerate());

// Degenerate triangle (all points on a line)
let triangle2 = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(2.0, 0.0, 0.0),
);
assert!(triangle2.is_degenerate());

Trait Implementations§

Source§

impl Clone for Triangle

Source§

fn clone(&self) -> Triangle

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ComputeAabb3 for Triangle

Source§

fn compute_aabb(&self) -> Aabb3

Compute the AABB in local space.
Source§

impl Debug for Triangle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Triangle

Source§

fn default() -> Triangle

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Triangle

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Triangle

Source§

fn eq(&self, other: &Triangle) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Triangle

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Triangle

Source§

impl StructuralPartialEq for Triangle

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,