gfm

This module implements some abstract geometric  shapes:

  • Line segments.
  • Triangle.
  • Circles/spheres.
  • Rays
  • Planes
  • Frustum


struct  Segment(T, int N);

A  Segment is 2 points.

When considered like a vector, it represents the arrow from a to b.


alias  seg2f = Segment!(float, 2).Segment;

2D float segment.


alias  seg3f = Segment!(float, 3).Segment;

3D float segment.


alias  seg2d = Segment!(double, 2).Segment;

2D double segment.


alias  seg3d = Segment!(double, 3).Segment;

3D double segment.


alias  seg2i = Segment!(int, 2).Segment;

2D integer segment.


alias  seg3i = Segment!(int, 3).Segment;

3D integer segment.


struct  Triangle(T, int N);

A  Triangle is 3 points.


const pure nothrow @nogc T  area();

Returns
Area of a 2D triangle.

const pure nothrow @nogc T  signedArea();

Returns
Signed area of a 2D triangle.

const pure nothrow @nogc Vector!(T, 3)  computeNormal();

Returns
Triangle normal.

alias  triangle2f = Triangle!(float, 2).Triangle;

2D float triangle.


alias  triangle3f = Triangle!(float, 3).Triangle;

3D float triangle.


alias  triangle2d = Triangle!(double, 2).Triangle;

2D double triangle.


alias  triangle3d = Triangle!(double, 3).Triangle;

3D double triangle.


struct  Sphere(T, int N);

A  Sphere is a point + a radius.


pure nothrow @nogc this(in point_t center_, T radius_);

Creates a sphere from a point and a radius.


const pure nothrow @nogc bool  contains(in Sphere s);

Sphere  contains point test.

Returns
true if the point is inside the sphere.

const pure nothrow @nogc T  squaredDistanceTo(point_t p);

Sphere vs point Euclidean distance squared.


const pure nothrow @nogc bool  intersects(Sphere s);

Sphere vs sphere intersection.

Returns
true if the spheres intersect.

const pure nothrow @nogc T  distanceTo(point_t p);

Sphere vs point Euclidean distance.


const pure nothrow @nogc T  area();

Returns
Circle  area.

alias  sphere2f = Sphere!(float, 2).Sphere;

2D float sphere (ie. a circle).


alias  sphere3f = Sphere!(float, 3).Sphere;

3D float sphere.


alias  sphere2d = Sphere!(double, 2).Sphere;

2D double sphere (ie. a circle).


alias  sphere3d = Sphere!(double, 3).Sphere;

3D double sphere (ie. a circle).


struct  Ray(T, int N);

A  Ray ir a point + a direction.


const pure nothrow @nogc point_t  progress(T t);

Returns
A point further along the ray direction.

const pure nothrow @nogc bool  intersect(Triangle!(T, 3) triangle, out T t, out T u, out T v);

Ray vs triangle intersection.

See Also
"Fast, Minimum Storage Ray/Triangle intersection", Mommer & Trumbore (1997)
Returns
Barycentric coordinates, the intersection point is at (1 - u - v) * A + u * B + v * C.

alias  ray2f = Ray!(float, 2).Ray;

2D float ray.


alias  ray3f = Ray!(float, 3).Ray;

3D float ray.


alias  ray2d = Ray!(double, 2).Ray;

2D double ray.


alias  ray3d = Ray!(double, 3).Ray;

3D double ray.


struct  Plane(T) if (isFloatingPoint!T);

3D plane.

See Also
Flipcode article by Nate Miller www.flipcode.com/archives/Plane_Class.shtml.

vec3!T  n;

Normal (always stored normalized).


pure nothrow @nogc this(vec4!T abcd);

Create from four coordinates.


pure nothrow @nogc this(vec3!T origin, vec3!T normal);

Create from a point and a normal.


pure nothrow @nogc this(vec3!T A, vec3!T B, vec3!T C);

Create from 3 non-aligned points.


pure nothrow @nogc ref Plane  opAssign(Plane other);

Assign a plane with another plane.


const pure nothrow @nogc T  signedDistanceTo(vec3!T point);

Returns
signed distance between a point and the plane.

const pure nothrow @nogc T  distanceTo(vec3!T point);

Returns
absolute distance between a point and the plane.

const pure nothrow @nogc bool  isFront(vec3!T point);

Returns
true if the point is in front of the plane.

const pure nothrow @nogc bool  isBack(vec3!T point);

Returns
true if the point is in the back of the plane.

const pure nothrow @nogc bool  isOn(vec3!T point, T epsilon);

Returns
true if the point is on the plane, with a given epsilon.

alias  planef = Plane!float.Plane;

3D float plane.


alias  planed = Plane!double.Plane;

3D double plane.


struct  Frustum(T) if (isFloatingPoint!T);

3D frustum.

See Also
Flipcode article by Dion Picco www.flipcode.com/archives/Frustum_Culling.shtml.
Known Bugs
verify proper signedness of half-spaces

pure nothrow @nogc this(Plane!T left, Plane!T right, Plane!T top, Plane!T bottom, Plane!T near, Plane!T far);

Create a frustum from 6 planes.


object is outside the frustum


object intersects with the frustum


object is inside the frustum


const pure nothrow @nogc bool  contains(vec3!T point);

Point vs frustum intersection.


const pure nothrow @nogc int  contains(Sphere!(T, 3) sphere);

Sphere vs frustum intersection.

Returns
Frustum.OUTSIDE, Frustum.INTERSECT or Frustum.INSIDE.

const pure nothrow @nogc int  contains(box3!T box);

AABB vs frustum intersection.

Returns
Frustum.OUTSIDE, Frustum.INTERSECT or Frustum.INSIDE.

enum auto  isSegment(T);

True if

T
is a kind of Segment


enum auto  isTriangle(T);

True if

T
is a kind of Triangle


enum auto  isSphere(T);

True if

T
is a kind of Sphere


enum auto  isRay(T);

True if

T
is a kind of Ray


enum auto  isPlane(T);

True if

T
is a kind of Plane


enum auto  isFrustum(T);

True if

T
is a kind of Frustum


enum auto  isSegment2D(T);

True if

T
is a kind of 2 dimensional Segment


enum auto  isTriangle2D(T);

True if

T
is a kind of 2 dimensional Triangle


enum auto  isSphere2D(T);

True if

T
is a kind of 2 dimensional Sphere


enum auto  isRay2D(T);

True if

T
is a kind of 2 dimensional Ray


enum auto  isSegment3D(T);

True if

T
is a kind of 3 dimensional Segment


enum auto  isTriangle3D(T);

True if

T
is a kind of 3 dimensional Triangle


enum auto  isSphere3D(T);

True if

T
is a kind of 3 dimensional Sphere


enum auto  isRay3D(T);

True if

T
is a kind of 3 dimensional Ray


template  DimensionType(T : Segment!U, U...)
template  DimensionType(T : Triangle!U, U...)
template  DimensionType(T : Sphere!U, U...)
template  DimensionType(T : Ray!U, U...)
template  DimensionType(T : Plane!U, U)
template  DimensionType(T : Frustum!U, U)

Get the numeric type used to measure a shape's dimensions.

Examples
static assert(is(DimensionType!seg2i          == int));
static assert(is(DimensionType!triangle3d     == double));
static assert(is(DimensionType!sphere2d       == double));
static assert(is(DimensionType!ray3f          == float));
static assert(is(DimensionType!planed         == double));
static assert(is(DimensionType!(Frustum!real) == real));