Skip to content

Latest commit

History

90 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Terathon Math Library

This is a C++ math library containing classes for vectors, matrices, quaternions, and elements of projective geometric algebra. The specific classes are the following:

  • Vector2D – A 2D vector (x, y) that extends to four dimensions as (x, y, 0, 0).
  • Vector3D – A 3D vector (x, y, z) that extends to four dimensions as (x, y, z, 0).
  • Vector4D – A 4D vector (x, y, z, w).
  • Point2D – A 2D point (x, y) that extends to four dimensions as (x, y, 0, 1).
  • Point3D – A 3D point (x, y, z) that extends to four dimensions as (x, y, z, 1).
  • Bivector3D – A 3D bivector xe23 + ye31 + ze12.
  • Matrix2D – A 2×2 matrix.
  • Matrix3D – A 3×3 matrix.
  • Matrix4D – A 4×4 matrix.
  • Transform2D – A 3×3 matrix with fourth row always (0, 0, 1).
  • Transform3D – A 4×4 matrix with fourth row always (0, 0, 0, 1).
  • Quaternion – A conventional quaternion xi + yj + zk + w.
  • DualNum – A dual number s + .

2D rigid geometric algebra

  • FlatPoint2D – A 2D flat point xe1 + ye2 + ze3.
  • Line2D – A 2D line xe23 + ye31 + ze12.
  • Motor2D – A 2D motion operator Qxe1 + Qye2 + Qze3 + Qw 𝟙.
  • Flector2D - A 2D reflection operator Fxe23 + Fye31 + Fze12 + Fw1.

3D rigid geometric algebra

  • FlatPoint3D – A 3D flat pointxe1 + ye2 + ze3 + we4.
  • Line3D – A 3D linelvxe41 + lvye42 + lvze43 + lmxe23 + lmye31 + lmze12.
  • Plane3D – A 3D planexe234 + ye314 + ze124 + we321.
  • Motor3D – A 3D motion operatorQvxe41 + Qvye42 + Qvze43 + Qvw 𝟙 + Qmxe23 + Qmye31 + Qmze12 + Qmw1.
  • Flector3D - A 3D reflection operatorFpxe1 + Fpye2 + Fpze3 + Fpwe4 + Fgxe423 + Fgye431 + Fgze412 + Fgwe321.

2D conformal geometric algebra

  • RoundPoint2D – A 2D round point xe1 + ye2 + ze3 + we4.
  • Dipole2D – A 2D dipole dgxe23 + dgye31 + dgze12 + dpxe41 + dpye42 + dpze43.
  • Circle2D – A 2D circle we321 + xe423 + ye431 + ze412.

3D conformal geometric algebra

  • RoundPoint3D – A 3D round pointxe1 + ye2 + ze3 + we4 + ue5.
  • Dipole3D – A 3D dipoledvxe41 + dvye42 + dvze43 + dmxe23 + dmye31 + dmze12 + dpxe15 + dpye25 + dpze35 + dpwe45.
  • Circle3D – A 3D circlecgxe423 + cgye431 + cgze412 + cgwe321 + cvxe415 + cvye425 + cvze435 + cmxe235 + cmye315 + cmze125.
  • Sphere3D – A 3D sphereue1234 + xe4235 + ye4315 + ze4125 + we3215.

Component Swizzling

Vector components can be swizzled using shading-language syntax. As an example, the following expressions are all valid for a Vector3D object v:

  • v.x – The x component of v.
  • v.xy – A 2D vector having the x and y components of v.
  • v.yzx – A 3D vector having the components of v in the order (y, z, x).

Support for repeated components in a swizzle can be enabled by defining TERATHON_SWIZZLE_REPEAT. This is disabled by default because the large number of additional swizzling possibilities increases compile times substantially. Swizzles with repeated components are always const so that it's not possible to assign to them.

Rows, columns, and submatrices can be extracted from matrix objects using a similar syntax. As an example, the following expressions are all valid for a Matrix3D object m:

  • m.m12 – The (1,2) entry of m.
  • m.row0 – The first row of m.
  • m.col1 – The second column of m.
  • m.matrix2D – The upper-left 2×2 submatrix of m.
  • m.transpose – The transpose of m.

All of the above are generally free operations, with no copying, when their results are consumed by an expression. For more information, see Eric Lengyel's 2018 GDC talk Linear Algebra Upgraded.

Geometric Algebra

The ^ operator is overloaded for cases in which the wedge or antiwedge product can be applied between vectors, bivectors, flat points, lines, planes, round points, dipoles, circles, and spheres. (Note that ^ has lower precedence than just about everything else, so parentheses will be necessary.)

The library does not provide operators that directly calculate the geometric product and antiproduct because they would tend to generate inefficient code and produce intermediate results having unnecessary types when something like the sandwich product Qp appears in an expression. Instead, there are Transform() functions that take some object p for the first parameter and the motor Q with which to transform it for the second parameter.

See Eric Lengyel's Projective Geometric Algebra website for more information about operations among these types.

API Documentation

There is API documentation embedded in the header files. The formatted equivalent can be found in the C4 Engine documentation.

Mathematica Packages

Mathematica packages for rigid, conformal, and spacetime geometric algebras are available in the Geometric Algebra repository.

Licensing

Separate proprietary licenses are available from Terathon Software. Please send an email with details about your particular use case if you are interested.

About

C++ math library for 2D/3D/4D vector, matrix, quaternion, and geometric algebra.

Topics

Resources

Stars

1.0k stars

Watchers

26 watching

Forks

Contributors

Languages