Add floating and double precision.

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
Gawaboumga
Posts: 11
Joined: Sun Jul 14, 2013 3:06 pm

Add floating and double precision.

Post by Gawaboumga »

Hi everyone,

I've made lots of changes to the motor to support the single/double precision. Total redesign of the classes (line/vector/matrix/quaternion/ ...). Someone should reread what I've done because the matrixOps returns strange results and vector2d/vector3d have errors (that I presume due to the precisions of the tests).

The topic is available here: https://sourceforge.net/p/irrlicht/patches/272/

Here is the list of changes:

--- aabbox3d ---

-Add equals() functions.
-Operator equality now use equals().
-Specialisation for getInterpolated().
-Specialisation for intersectsWithLine().
-Add of typedef aabbox3d aabbox3dl and typedef aabbox3d aabbox3dd.

--- dimension2d ---

-Specialisation for getInterpolated().
-Add of typedef dimension2d dimension2dl.

--- irrMath.h ---

-New const u32 ROUNDING_ERROR_U32 = 0 and const f64 HALF_PI64 = PI64 / 2.0.
/// ! \\ Now ROUNDING_ERROR_f32 and f64 are called ROUNDING_ERROR_F32 and F64.
-New min_ for 2/3 types (The output will be the first type).
-New max_ for 2/3 types (The output will be the first type).
-In clamp(), min_ and max_ direct inlining of code.
-Add equals() functions.
-Add iszero() funcitons.
-Add isnotzero() functions.
-Suppression of s32_min/s32_max and s32_clamp.
-New reciprocal_squareroot() with new algorithm for fast math.
-New floor_/ceil_/round_/fract_ and suppression of the floor32/ceil32/round32.

--- line2d ---

-Add equals() functions.
-Operator equality now use equals().
-Specialisation for intersectWith().
-Specialisation for getAngleWith().
-Specialisation for getClosestPoint().
-Add of typedef line2d line2dl and typedef line2d line2dd.

--- line3d ---

-Add equals() functions.
-Operator equality now use equals().
-Add of typedef line3d line3dl and typedef line3d line3dd.

--- CMatrix4 ---

/ This should be rewritten with template default /
-Add equals() functions.
-Operator equality now use equals().
-Specialisation for setRotationRadians().
-Specialisation for setInverseRotationRadians().
-Specialisation for setRotationDegrees().
-Specialisation for setInverseRotationDegrees().
-Specialisation for getRotationDegrees().
-Specialisation for setRotationAxisRadians().
/// ! \\ I messed up isIdentity_integer_base().
-Specialisation for buildProjectionMatrixPerspectiveFovRH().
-Specialisation for buildProjectionMatrixPerspectiveFovLH().
-Specialisation for buildProjectionMatrixPerspectiveFovInfinityLH().
-Specialisation for buildProjectionMatrixOrthoRH().
-Specialisation for buildProjectionMatrixOrthoLH().
-Specialisation for buildProjectionMatrixPerspectiveRH().
-Specialisation for buildProjectionMatrixPerspectiveLH().
-Specialisation for buildShadowMatrix().
-Specialisation for buildNDCToDCMatrix().
-Specialisation for buildTextureTransform().
-Specialisation for setTextureRotationCenter().
-Add of typedef CMatrix4 matrix4i, CMatrix4 matrix4l and typedef CMatrix4 matrix4d.

--- plane3d ---

-Add equals() functions.
-Operator equality now use equals().
-Specialisation for getIntersectionWithPlane().
-Add of typedef plane3d plane3dl and typedef plane3d plane3dd.

--- CQuaternion ---

/// ! \\ The class "quaternion" is recalled "CQuaternion" to be more logical with CMatrix4
/ This should be rewritten with template default /
-Add equals() functions.
-Operator equality now use equals().
-Specialisation for getMatrix().
-Specialisation for getMatrixCenter().
-Specialisation for getMatrix_transposed().
-Specialisation for set().
-Specialisation for slerp().
-Specialisation for fromAngleAxis().
-Specialisation for toAngleAxis().
-Specialisation for toEuler().
-Specialisation for rotationFromTo().
-Add of typedef CQuaternion quaternioni, typedef CQuaternion quaternionl,
typedef CQuaternion quaternion and typedef CQuaternion quaterniond.

--- rect ---

-Add equals() functions.
-Operator equality now use equals().
-Add operator <= and >=.
-Add of typedef rect rectu, typedef rect rectl and typedef rect rectd.
--- triangle3d ---
-Add equals() functions.
-Operator equality now use equals().
-Specialisation for isPointInsideFast().
-Specialisation for getIntersectionOfPlaneWithLine().
-Add of typedef triangle3d triangle3dl and typedef triangle3d triangle3dd.

--- vector2d ---

/ This should be rewritten with template default /
-Add equals() functions.
-Operator equality now use equals().
-Changes in the operatos > and <.
-Specialisation for rotateBy().
-Specialisation for getAngleTrig().
-Specialisation for getAngle().
-Specialisation for getAngleWith().
-Specialisation for getInterpolated().
-Specialisation for getInterpolated_quadratic().
-Specialisation for interpolate().
-Add of typedef vector2d vector2dl and typedef vector2d vector2dd.

--- vector3d ---

/ This should be rewritten with template default /
-Add equals() functions.
-Operator equality now use equals().
-Changes in the operatos > and <.
-Specialisation for rotateXZBy().
-Specialisation for rotateXYBy().
-Specialisation for rotateYZBy().
-Specialisation for getInterpolated().
-Specialisation for getInterpolated_quadratic().
-Specialisation for interpolate().
-Specialisation for getHorizontalAngle().
-Specialisation for getSphericalCoordinateAngles().
-Specialisation for rotationToDirection().
-Add of typedef vector3d vector3dl and typedef vector3d vector3dd.

Yes I know that some typedef <s32> or <s64> are pretty useless.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Re: Add floating and double precision.

Post by roxaz »

that would be nice. ofc if it is optional, like in ogre.
Gawaboumga
Posts: 11
Joined: Sun Jul 14, 2013 3:06 pm

Re: Add floating and double precision.

Post by Gawaboumga »

I just added the double functions. I didn't change anything in the engine in itself which is still using f32. I presume that if someone use what I've done, the choice will be optional. Everybody doesn't get a 64 bits OS.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Re: Add floating and double precision.

Post by roxaz »

oh its double precision only on outter layer and graphics api gets floats still?
Gawaboumga
Posts: 11
Joined: Sun Jul 14, 2013 3:06 pm

Re: Add floating and double precision.

Post by Gawaboumga »

Exactly. Yes this add is a bit useless ... But now this is more logical in the inner layer. Indeed, floating functions only use floating functions and there isn't anymore mixes between floating and double. Examples:

inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixPerspectiveFovLH(
f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{ const f64 h = reciprocal(tan(fieldOfViewRadians*0.5));

is now:

inline CMatrix4<f32>& CMatrix4<f32>::buildProjectionMatrixPerspectiveFovLH(
f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{ const f32 h = reciprocal(tanf(fieldOfViewRadians*0.5f));

and:

inline CMatrix4<f64>& CMatrix4<f64>::buildProjectionMatrixPerspectiveFovLH(
f64 fieldOfViewRadians, f64 aspectRatio, f64 zNear, f64 zFar)
{ const f64 h = reciprocal(tan(fieldOfViewRadians*0.5));
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Add floating and double precision.

Post by hybrid »

This is definitely not good! Having float and double separated or mixed is not a question of style, but of precision requirements. If you calculate too much of the intermediate results as float, you get a huge precision loss. That's why we made some things double even though the end results will be reduced to float.
AReichl
Posts: 270
Joined: Wed Jul 13, 2011 2:34 pm

Re: Add floating and double precision.

Post by AReichl »

> That's why we made some things double even though the end results will be reduced to float.

Right. This could be done with Specialisation. A Template where you expect a <float> would have internally a 'double' temporary, a <double> template internally a 'long double' until there is no bigger type. Maybe in C++11 there is a mechanism to get the "next biggest type" (e.g. float -> double).

Some time ago i thought it a good idea if Irrlicht could be build with 'double' support (like OGRE), but meanwhile i am not so sure any more. I read an article about precision problems and the result was "if you use double you have NOT understood the problem".
Meaning: if you really need precision (e.g. for a space simulation game), you should switch to int or long. Of course the underlying engine would be totally different. Other engines don't even have templates - they work with float and that's it. I think for now it is best to CONCENTRATE on float and make the funktions fast and reliable (see the thread about rounding errors). In the future then we can think about a "virtual scenemanager" on top of the normal one working internally with higher precision.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Re: Add floating and double precision.

Post by roxaz »

also bear in mind that to really use power of doubles they would have to be fed directly to opengl (yes, it can work with doubles too!). directx on the other hand cant. so if you wish to use directx you would have in the end still reduce to doubles. in that case it does not matter at all if double support is built into engine and used for EVERYTHING or if you have custom pos object composed of either doubles, 64 bit integers or even fat and slow big integer implementation and you reduce it to floats for rendering. initially i thought engine modifications included deep enough mods that bring doubles to opengl. w/o that such modification is probably pointless

edit:
im prototyping something with planets too. float seems to be really more than enough sufficient for rendering local space. that is space where close objects would move and interact. for other stuff like say planets i resorted to secondary scene manager that renders stuff that is on background (like planets/stars), scale them down by a PLANETARY_SCALE_CONSTANT and move reposition camera of that scene manager slower by factor of PLANETARY_SCALE_CONSTANT. end result - planets are small spheres but they move so slow that it seems they are far far away. no doubles required :)
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Add floating and double precision.

Post by Granyte »

I have been working for some times on a space sim with seamless transition to planet surface my solution so far has been to use double in the physic engine and make the camera as the (0,0,0) point in irrlicht and i translate everything around to match this so no precision issue anymore
AReichl
Posts: 270
Joined: Wed Jul 13, 2011 2:34 pm

Re: Add floating and double precision.

Post by AReichl »

> ... make the camera as the (0,0,0) point in irrlicht and i translate everything around to match this so no precision issue anymore.

THAT once was a question by me, but i got no sufficient answer - HOW do you do that?
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Add floating and double precision.

Post by Granyte »

I keep track of how much the camera move each frame then reset it to (0,0,0) then when i read all the position from the physic engine i translate them by how much the camera moved
Post Reply