ericlengyel / terathon-math-library Goto Github PK
View Code? Open in Web Editor NEWC++ math library for 2D/3D/4D vector, matrix, quaternion, and geometric algebra.
Home Page: https://projectivegeometricalgebra.org/
License: MIT License
C++ math library for 2D/3D/4D vector, matrix, quaternion, and geometric algebra.
Home Page: https://projectivegeometricalgebra.org/
License: MIT License
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
the TSMath.h header seems to be missing, it's being called from TSAlgebra.h , Thank you
Looks like TSBasic.h is missing (included in TSMatrix3D.cpp)
I think the formula in the documentation is wrong.
It should not be (a - a.b)b, but rather a - (a.b)b
Terathon-Math-Library/TSVector2D.h
Line 124 in ed1cbdf
The actual code seems fine, though:
Terathon-Math-Library/TSVector2D.h
Line 399 in ed1cbdf
I suggest adding topics such as geometric-algebra
, quaternion
in the About section
Any plans on adding unit tests to the library? I think it could prevent issues like these:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.