https://github.com/EricLengyel/Terathon-Math-Library
C++ math library for 2D/3D/4D vector, matrix, quaternion, and geometric algebra.
https://github.com/EricLengyel/Terathon-Math-Library
dual-quaternions geometric-algebra linear-algebra quaternions
Last synced: about 1 year ago
JSON representation
C++ math library for 2D/3D/4D vector, matrix, quaternion, and geometric algebra.
- Host: GitHub
- URL: https://github.com/EricLengyel/Terathon-Math-Library
- Owner: EricLengyel
- License: mit
- Created: 2020-12-07T04:44:04.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-06-25T21:36:15.000Z (about 1 year ago)
- Last Synced: 2025-06-25T22:33:47.558Z (about 1 year ago)
- Topics: dual-quaternions, geometric-algebra, linear-algebra, quaternions
- Language: C++
- Homepage: https://projectivegeometricalgebra.org/
- Size: 509 KB
- Stars: 968
- Watchers: 25
- Forks: 76
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 *x* **e**23 + *y* **e**31 + *z* **e**12.
* **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 *x***i** + *y***j** + *z***k** + *w*.
* **DualNum** – A dual number *s* + *tε*.
2D rigid geometric algebra
* **FlatPoint2D** – A 2D flat point *x* **e**1 + *y* **e**2 + *z* **e**3.
* **Line2D** – A 2D line *x* **e**23 + *y* **e**31 + *z* **e**12.
* **Motor2D** – A 2D motion operator *Qx* **e**1 + *Qy* **e**2 + *Qz* **e**3 + *Qw* 𝟙.
* **Flector2D** - A 2D reflection operator *Fx* **e**23 + *Fy* **e**31 + *Fz* **e**12 + *Fw* **1**.
3D rigid geometric algebra
* **FlatPoint3D** – A 3D [flat point](https://rigidgeometricalgebra.org/wiki/index.php?title=Point) *x* **e**1 + *y* **e**2 + *z* **e**3 + *w* **e**4.
* **Line3D** – A 3D [line](https://rigidgeometricalgebra.org/wiki/index.php?title=Line) *lvx* **e**41 + *lvy* **e**42 + *lvz* **e**43 + *lmx* **e**23 + *lmy* **e**31 + *lmz* **e**12.
* **Plane3D** – A 3D [plane](https://rigidgeometricalgebra.org/wiki/index.php?title=Plane) *x* **e**234 + *y* **e**314 + *z* **e**124 + *w* **e**321.
* **Motor3D** – A 3D [motion operator](https://rigidgeometricalgebra.org/wiki/index.php?title=Motor) *Qvx* **e**41 + *Qvy* **e**42 + *Qvz* **e**43 + *Qvw* 𝟙 + *Qmx* **e**23 + *Qmy* **e**31 + *Qmz* **e**12 + *Qmw* **1**.
* **Flector3D** - A 3D [reflection operator](https://rigidgeometricalgebra.org/wiki/index.php?title=Flector) *Fpx* **e**1 + *Fpy* **e**2 + *Fpz* **e**3 + *Fpw* **e**4 + *Fgx* **e**423 + *Fgy* **e**431 + *Fgz* **e**412 + *Fgw* **e**321.
2D conformal geometric algebra
* **RoundPoint2D** – A 2D round point *x* **e**1 + *y* **e**2 + *z* **e**3 + *w* **e**4.
* **Dipole2D** – A 2D dipole *dgx* **e**23 + *dgy* **e**31 + *dgz* **e**12 + *dpx* **e**41 + *dpy* **e**42 + *dpz* **e**43.
* **Circle2D** – A 2D circle *w* **e**321 + *x* **e**423 + *y* **e**431 + *z* **e**412.
3D conformal geometric algebra
* **RoundPoint3D** – A 3D [round point](https://conformalgeometricalgebra.org/wiki/index.php?title=Round_point) *x* **e**1 + *y* **e**2 + *z* **e**3 + *w* **e**4 + *u* **e**5.
* **Dipole3D** – A 3D [dipole](https://conformalgeometricalgebra.org/wiki/index.php?title=Dipole) *dvx* **e**41 + *dvy* **e**42 + *dvz* **e**43 + *dmx* **e**23 + *dmy* **e**31 + *dmz* **e**12 + *dpx* **e**15 + *dpy* **e**25 + *dpz* **e**35 + *dpw* **e**45.
* **Circle3D** – A 3D [circle](https://conformalgeometricalgebra.org/wiki/index.php?title=Circle) *cgx* **e**423 + *cgy* **e**431 + *cgz* **e**412 + *cgw* **e**321 + *cvx* **e**415 + *cvy* **e**425 + *cvz* **e**435 + *cmx* **e**235 + *cmy* **e**315 + *cmz* **e**125.
* **Sphere3D** – A 3D [sphere](https://conformalgeometricalgebra.org/wiki/index.php?title=Sphere) *u* **e**1234 + *x* **e**4235 + *y* **e**4315 + *z* **e**4125 + *w* **e**3215.
## 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](https://terathon.com/gdc18_lengyel.pdf).
## 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 **Q** ⟇ *p* ⟇ **Q̰** 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](https://projectivegeometricalgebra.org) 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](https://c4engine.com/docs/Math/index.html).
## Mathematica Packages
Mathematica packages for rigid, conformal, and spacetime geometric algebras are available in the [Geometric Algebra](https://github.com/EricLengyel/Geometric-Algebra/) repository.
## Licensing
Separate proprietary licenses are available from Terathon Software. Please [send an email](mailto:service@terathon.com) with details about your particular use case if you are interested.