Coder Social home page Coder Social logo

ericlengyel / terathon-math-library Goto Github PK

View Code? Open in Web Editor NEW
851.0 19.0 63.0 446 KB

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

Home Page: https://projectivegeometricalgebra.org/

License: MIT License

C++ 100.00%
dual-quaternions geometric-algebra linear-algebra quaternions

terathon-math-library's Introduction

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 e23 + y e31 + z e12.
  • 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 x e1 + y e2 + z e3.
  • Line2D – A 2D line x e23 + y e31 + z e12.
  • Motor2D – A 2D motion operator Qx e1 + Qy e2 + Qz e3 + Qw 𝟙.
  • Flector2D - A 2D reflection operator Fx e23 + Fy e31 + Fz e12 + Fw 1.

3D rigid geometric algebra

  • FlatPoint3D – A 3D flat point x e1 + y e2 + z e3 + w e4.
  • Line3D – A 3D line lvx e41 + lvy e42 + lvz e43 + lmx e23 + lmy e31 + lmz e12.
  • Plane3D – A 3D plane x e234 + y e314 + z e124 + w e321.
  • Motor3D – A 3D motion operator Qvx e41 + Qvy e42 + Qvz e43 + Qvw 𝟙 + Qmx e23 + Qmy e31 + Qmz e12 + Qmw 1.
  • Flector3D - A 3D reflection operator Fpx e1 + Fpy e2 + Fpz e3 + Fpw e4 + Fgx e423 + Fgy e431 + Fgz e412 + Fgw e321.

2D conformal geometric algebra

  • RoundPoint2D – A 2D round point x e1 + y e2 + z e3 + w e4.
  • Dipole2D – A 2D dipole dgx e23 + dgy e31 + dgz e12 + dpx e41 + dpy e42 + dpz e43.
  • Circle2D – A 2D circle w e321 + x e423 + y e431 + z e412.

3D conformal geometric algebra

  • RoundPoint3D – A 3D round point x e1 + y e2 + z e3 + w e4 + u e5.
  • Dipole3D – A 3D dipole dvx e41 + dvy e42 + dvz e43 + dmx e23 + dmy e31 + dmz e12 + dpx e15 + dpy e25 + dpz e35 + dpw e45.
  • Circle3D – A 3D circle cgx e423 + cgy e431 + cgz e412 + cgw e321 + cvx e415 + cvy e425 + cvz e435 + cmx e235 + cmy e315 + cmz e125.
  • Sphere3D – A 3D sphere u e1234 + x e4235 + y e4315 + z e4125 + w e3215.

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 ⟇ ~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 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.

Licensing

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

terathon-math-library's People

Contributors

ericlengyel avatar talanc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

terathon-math-library's Issues

wrong formula in doc for Vector2D Reject

I think the formula in the documentation is wrong.

It should not be (a - a.b)b, but rather a - (a.b)b

//# Returns (<b>a</b>&#x202F;&minus;&#x202F;<b>a</b>&#x202F;&sdot;&#x202F;<b>b</b>)<b>b</b>, which is the rejection of $a$ from $b$ under the assumption that the magnitude of $b$ is one.

The actual code seems fine, though:

return (a - b * Dot(a, b));

TSMath.h is missing

the TSMath.h header seems to be missing, it's being called from TSAlgebra.h , Thank you

Unit tests

Any plans on adding unit tests to the library? I think it could prevent issues like these:

#5

add topic tags

I suggest adding topics such as geometric-algebra, quaternion in the About section

Terathon::Arctan() too imprecise

Hi,

We (Masterbrain Bytes/Cattle and Crops) have camera code that uses polar coordinates to handle input, and we also want the camera to move (and turn) with the vehicle the player is currently driving.
So we're using code like

float zrot = Arctan( vehicleTransform.m10, vehicleTransform.m00 );
azimuth += zrot;

This generally works, but we noticed that when just driving in a circle, at certain angles the camera stuttered.
When replacing Arctan() with atan2f() those problems went away.

This could've been it, but this issue somehow nerdsniped me so I started investigating and found out that Arctan(float x) has errors of up to 0.004459 (compared to atan(x) in double precision).

When looking up how a fast atan() can be implemented, I stumbled upon the idea of just doing a linear interpolation between values of a lookup table.
So I implemented it (with the existing lookup-table), just interpolating between table[i] and table[i+1], using b - i as the lerp factor, and to my big surprise this works really well - the maximal error seems to be below 0.000020, i.e. over 200 times lower.
In addition to that, it even seems to be about twice as fast, and it works with our camera code.

I tested this extensively both with values between +/- 10'000 (in steps of 0.00001) and values between +/- 1.5 (in steps of 0.00000001) and it indeed seems to work quite well.

Here's my test program (tested on both Linux and Windows), including both your and my Arctan(float) implementation: atantest-upd.cpp.txt (Github doesn't allow attaching .cpp files so I had to append .txt).
UPDATE: Replaced the file with an updated one with one more element in table; also added function with 130 element lookup table.
Feel free to use it (also in your commercial code).

Here's my test results from both Linux (on a AMD Ryzen 2700X) and Win10 (on an Intel Haswell i7-4771): atanresults.txt

Cheers,
Daniel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.