GEOMETRY(2)GEOMETRY(2)
NAME
Flerp, fberp, fclamp, Pt2, Vec2, addpt2, subpt2, mulpt2, divpt2, lerp2, berp2, dotvec2, vec2len, normvec2, edgeptcmp, ptinpoly, Pt3, Vec3, addpt3, subpt3, mulpt3, divpt3, lerp3, berp3, dotvec3, crossvec3, vec3len, normvec3, lineXsphere, ptincylinder, ptincone, identity, addm, subm, mulm, smulm, transposem, detm, tracem, minorm, cofactorm, adjm, invm, xform, identity3, addm3, subm3, mulm3, smulm3, transposem3, detm3, tracem3, minorm3, cofactorm3, adjm3, invm3, xform3, Quat, Quatvec, addq, subq, mulq, smulq, sdivq, dotq, invq, qlen, normq, slerp, qsandwich, qsandwichpt3, qrotate, rframematrix, rframematrix3, rframexform, rframexform3, invrframexform, invrframexform3, centroid, barycoords, centroid3, vfmt, Vfmt, GEOMfmtinstall – computational geometry library
SYNOPSIS
unhandled troff command .de
unhandled troff command ..
unhandled troff command .PB
#include <u.h> #include <libc.h> #include <geometry.h>
unhandled troff command .PB
#define DEG 0.01745329251994330 /* π/180 */
unhandled troff command .PB
typedef struct Point2 Point2; typedef struct Point3 Point3; typedef double Matrix[3][3]; typedef double Matrix3[4][4]; typedef struct Quaternion Quaternion; typedef struct RFrame RFrame; typedef struct RFrame3 RFrame3;
unhandled troff command .PB
struct Point2 { double x, y, w; };
unhandled troff command .PB
struct Point3 { double x, y, z, w; };
unhandled troff command .PB
struct Quaternion { double r, i, j, k; };
unhandled troff command .PB
struct RFrame { Point2 p; Point2 bx, by; };
unhandled troff command .PB
struct RFrame3 { Point3 p; Point3 bx, by, bz; };
unhandled troff command .PB
extern Point2 ZP2; extern Point3 ZP3;
unhandled troff command .PB
/* utils */ double flerp(double a, double b, double t) double fberp(double a, double b, double c, Point3 bc) double fclamp(double n, double min, double max)
unhandled troff command .PB
/* Point2 */ Point2 Pt2(double x, double y, double w) Point2 Vec2(double x, double y) Point2 addpt2(Point2 a, Point2 b) Point2 subpt2(Point2 a, Point2 b) Point2 mulpt2(Point2 p, double s) Point2 divpt2(Point2 p, double s) Point2 lerp2(Point2 a, Point2 b, double t) Point2 berp2(Point2 a, Point2 b, Point2 c, Point3 bc) double dotvec2(Point2 a, Point2 b) double vec2len(Point2 v) Point2 normvec2(Point2 v) Point2 centroid(Point2 *pts, ulong npts) Point3 barycoords(Point2 p0, Point2 p1, Point2 p2, Point2 p) int edgeptcmp(Point2 e0, Point2 e1, Point2 p) int ptinpoly(Point2 p, Point2 *pts, ulong npts)
unhandled troff command .PB
/* Point3 */ Point3 Pt3(double x, double y, double z, double w) Point3 Vec3(double x, double y, double z) Point3 addpt3(Point3 a, Point3 b) Point3 subpt3(Point3 a, Point3 b) Point3 mulpt3(Point3 p, double s) Point3 divpt3(Point3 p, double s) Point3 lerp3(Point3 a, Point3 b, double t) Point3 berp3(Point3 a, Point3 b, Point3 c, Point3 bc) double dotvec3(Point3 a, Point3 b) Point3 crossvec3(Point3 a, Point3 b) double vec3len(Point3 v) Point3 normvec3(Point3 v) Point3 centroid3(Point3 *pts, ulong npts) int lineXsphere(Point3 *rp, Point3 p0, Point3 p1, Point3 c, double rad, int isaray) int ptincylinder(Point3 p, Point3 p0, Point3 p1, double r) int ptincone(Point3 p, Point3 p0, Point3 p1, double br)
unhandled troff command .PB
/* Matrix */ void identity(Matrix m) void addm(Matrix a, Matrix b) void subm(Matrix a, Matrix b) void mulm(Matrix a, Matrix b) void smulm(Matrix m, double s) void transposem(Matrix m) double detm(Matrix m) double tracem(Matrix m) double minorm(Matrix m, int row, int col) double cofactorm(Matrix m, int row, int col) void adjm(Matrix m) void invm(Matrix m) Point2 xform(Point2 p, Matrix m)
unhandled troff command .PB
/* Matrix3 */ void identity3(Matrix3 m) void addm3(Matrix3 a, Matrix3 b) void subm3(Matrix3 a, Matrix3 b) void mulm3(Matrix3 a, Matrix3 b) void smulm3(Matrix3 m, double s) void transposem3(Matrix3 m) double detm3(Matrix3 m) double tracem3(Matrix3 m) double minorm3(Matrix3 m, int row, int col) double cofactorm3(Matrix3 m, int row, int col) void adjm3(Matrix3 m) void invm3(Matrix3 m) Point3 xform3(Point3 p, Matrix3 m)
unhandled troff command .PB
/* Quaternion */ Quaternion Quat(double r, double i, double j, double k) Quaternion Quatvec(double r, Point3 v) Quaternion addq(Quaternion a, Quaternion b) Quaternion subq(Quaternion a, Quaternion b) Quaternion mulq(Quaternion q, Quaternion r) Quaternion smulq(Quaternion q, double s) Quaternion sdivq(Quaternion q, double s) double dotq(Quaternion q, Quaternion r) Quaternion invq(Quaternion q) double qlen(Quaternion q) Quaternion normq(Quaternion q) Quaternion slerp(Quaternion q, Quaternion r, double t) Quaternion qsandwich(Quaternion q, Quaternion r) Point3 qsandwichpt3(Quaternion q, Point3 p) Point3 qrotate(Point3 p, Point3 axis, double θ)
unhandled troff command .PB
/* RFrame */ void rframematrix(Matrix, RFrame) void rframematrix3(Matrix3, RFrame3) Point2 rframexform(Point2 p, RFrame rf) Point3 rframexform3(Point3 p, RFrame3 rf) Point2 invrframexform(Point2 p, RFrame rf) Point3 invrframexform3(Point3 p, RFrame3 rf)
unhandled troff command .PB
/* Fmt */ #pragma varargck type "v" Point2 #pragma varargck type "V" Point3 int vfmt(Fmt*) int Vfmt(Fmt*) void GEOMfmtinstall(void)
DESCRIPTION
This library provides routines to operate with homogeneous coordinates in 2D and 3D projective spaces by means of points, matrices, quaternions and frames of reference.
Besides their many mathematical properties and applications, the data structures and algorithms used here to represent these abstractions are specifically tailored to the world of computer graphics and simulators, and so it uses the conventions associated with these fields, such as the right-hand rule for coordinate systems and column vectors for matrix operations.
UTILS
These utility functions provide extra floating-point operations that are not available in the standard libc.
Description
Performs a linear interpolation by a factor of
Performs a barycentric interpolation of the values in
Constrains
Points
A point
Description
Constructor function for a Point2 point.
Constructor function for a Point2 vector.
Creates a new 2D point out of the sum of
Creates a new 2D point out of the substraction of
Creates a new 2D point from multiplying
Creates a new 2D point from dividing
Performs a linear interpolation between the 2D points
Performs a barycentric interpolation between the 2D points
Computes the dot product of vectors
Computes the length—magnitude—of vector
Normalizes the vector
Returns the geometric center of a collection of 2D points.
Returns a 3D point that represents the barycentric coordinates of the
2D point
Performs a comparison between an edge, defined by a directed line from
Returns 1 if the 2D point
Constructor function for a Point3 point.
Constructor function for a Point3 vector.
Creates a new 3D point out of the sum of
Creates a new 3D point out of the substraction of
Creates a new 3D point from multiplying
Creates a new 3D point from dividing
Performs a linear interpolation between the 3D points
Performs a barycentric interpolation between the 3D points
Computes the dot product of vectors
Computes the cross product of vectors
Computes the length—magnitude—of vector
Normalizes the vector
Returns the geometric center of a collection of 3D points.
Finds the intersection between the line defined by
Tests if the point
Tests if the point
Matrices
Description
Initializes
Sums the matrices
Substracts the matrix
Multiplies the matrices
Multiplies every element of
Transforms the matrix
Computes the determinant of
Computes the trace of
Computes the minor of
Computes the cofactor of
Transforms the matrix
Transforms the matrix
Transforms the point
Initializes
Sums the matrices
Substracts the matrix
Multiplies the matrices
Multiplies every element of
Transforms the matrix
Computes the determinant of
Computes the trace of
Computes the minor of
Computes the cofactor of
Transforms the matrix
Transforms the matrix
Transforms the point
Quaternions
Quaternions are an extension of the complex numbers conceived as a tool to analyze 3-dimensional points. They are most commonly used to orient and rotate objects in 3D space.
Description
Constructor function for a Quaternion.
Constructor function for a Quaternion that takes the imaginary part in
the form of a vector
Creates a new quaternion out of the sum of
Creates a new quaternion from the substraction of
Multiplies
Multiplies each of the components of
Divides each of the components of
Computes the dot-product of
Computes the inverse of
Computes
Normalizes
Performs a spherical linear interpolation between the quaternions
Returns the result of "sandwiching"
Same as
Returns the result of rotating the point
Frames of reference
A frame of reference (or rframe) in a
Description
Initializes
Initializes
Transforms the point
Transforms the point
Transforms the point
Transforms the point
EXAMPLES
Rotate a point p by θ, scale it 2x, then translate it by vector v:
Matrix R = {
cos(θ), -sin(θ), 0,
sin(θ), cos(θ), 0,
0, 0, 1,
}, S = {
2, 0, 0,
0, 2, 0,
0, 0, 1,
}, T = {
1, 0, v.x,
0, 1, v.y,
0, 0, 1,
};
mulm(S, R);
mulm(T, S);
p = xform(p, T); /* p' = T·S·R·p */
Given a space with two observers, A and B, and a point p relative to A, find its location relative to B:
pb = rframexform(invrframexform(p, A), B);
Now let’s say observer C is located relative to A; find the point’s location in C:
pc = rframexform(p, C);
Finally, to obtain its location relative to the space itself (its global position):
pg = invrframexform(p, A);
The following is a common example of an
RFrame worldrf;
/* from screen... */
Point2
toworld(Point p)
{
return rframexform(Pt2(p.x,p.y,1), worldrf);
}
/* ...to screen */
Point
fromworld(Point2 p)
{
p = invrframexform(p, worldrf);
return Pt(p.x,p.y);
}
void
main(void)
⋯
worldrf.p = Pt2(screen->r.min.x+Dx(screen->r)/2,
screen->r.min.y+Dy(screen->r)/2, 1);
worldrf.bx = Vec2(1, 0);
worldrf.by = Vec2(0,-1);
⋯
SOURCE
SEE
Philip J. Schneider, David H. Eberly,
“Geometric Tools for Computer Graphics”,
Morgan Kaufmann Publishers, 2003.
Jonathan Blow,
“Understanding Slerp, Then Not Using it”,
The Inner Product, April 2004.
https://www.3dgep.com/understanding-quaternions/
https://motion.cs.illinois.edu/RoboticSystems/CoordinateTransformations.html
BUGS
No care is taken to avoid numeric overflows.
HISTORY
Libgeometry first appeared in Plan 9 from Bell Labs. It was revamped for 9front in January of 2023.