Can I "templatize" MathDefs?

Everything about the development of Urho3D.

Can I "templatize" MathDefs?

PostPosted by TheComet » 29 Feb 2016, 11:46

I'm getting these errors when using Urho3D's math functions because there exist no overloads of Clamp() (or any of the other math functions) that use double.
Code: Select all
error: call of overloaded ‘Clamp(double&, double&, double&)’ is ambiguous


I took a look at MathDefs.h and I feel like there are two big improvements that can be made.


1) Use templates

This:
Code: Select all
inline float Clamp(float value, float min, float max)
{
    if (value < min)
        return min;
    else if (value > max)
        return max;
    else
        return value;
}


should be this:
Code: Select all
template <class T>
inline T Clamp(T value, T min, T max)
{
    if (value < min)
        return min;
    else if (value > max)
        return max;
    return value;
}



2) We should be using std::numeric_limits instead of trying to define our own epsilon, our own infinity etc.

This:
Code: Select all
inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }


would be much better as:
Code: Select all
template <class T>
bool Equals(T lhs, T rhs) { return lhs + std::numeric_limits<T>::epsilon >= rhs && lhs - std::numeric_limits<T>::epsilon <= rhs; }


that way these defines can be eliminated:
Code: Select all
static const int M_MIN_INT = 0x80000000;
static const int M_MAX_INT = 0x7fffffff;
static const unsigned M_MIN_UNSIGNED = 0x00000000;
static const unsigned M_MAX_UNSIGNED = 0xffffffff;

static const float M_EPSILON = 0.000001f;
static const float M_LARGE_EPSILON = 0.00005f;
static const float M_LARGE_VALUE = 100000000.0f;
static const float M_INFINITY = (float)HUGE_VAL;


Can I apply these changes and make a PR, or are there objections?
I'm a non-binary non-cis sexually fluid cephalopod identifying genderqueer mocha frappé latte
User avatar
TheComet
Active user
Active user
 
Posts: 122
Joined: 29 Jan 2014, 14:07
Location: Germany

Re: Can I "templatize" MathDefs?

PostPosted by cadaver » 29 Feb 2016, 13:43

Scripting doesn't generally support templates, so if you ensure scripting and platform and compiler compatibility there shouldn't be a problem.

Urho should still compile on VS2008, though it's becoming fairly ancient, but naturally it's better if this PR doesn't become the straw that breaks the camel's back.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: Can I "templatize" MathDefs?

PostPosted by TheComet » 29 Feb 2016, 15:46

I created a PR for the changes I mentioned above. The exact changes are detailed in the message there: https://github.com/urho3d/Urho3D/pull/1220

Please let me know if you disagree with anything.

Shall I go ahead and also templatize the other math classes?
I'm a non-binary non-cis sexually fluid cephalopod identifying genderqueer mocha frappé latte
User avatar
TheComet
Active user
Active user
 
Posts: 122
Joined: 29 Jan 2014, 14:07
Location: Germany

Re: Can I "templatize" MathDefs?

PostPosted by TheComet » 29 Feb 2016, 19:40

I realized it's probably not a good idea to have std::numeric_limits spread everywhere, so I created a template M_LIMITS that wraps std::numeric_limits in MathDefs.h. Using it looks like this:

Code: Select all
float infinity = M_LIMITS<float>::Infinity;
double largeEpsilon = M_LIMITS<double>::LargeEpsilon;
int maxInt = M_LIMITS<signed>::Max;


This also makes the AngelScript bindings much better.
I'm a non-binary non-cis sexually fluid cephalopod identifying genderqueer mocha frappé latte
User avatar
TheComet
Active user
Active user
 
Posts: 122
Joined: 29 Jan 2014, 14:07
Location: Germany

Re: Can I "templatize" MathDefs?

PostPosted by cadaver » 02 Mar 2016, 09:11

Thanks, will look at the PR. I don't recommend templatizing the math classes overall, as the engine is still bound to support only a specific kind of them (e.g. float vectors / matrices), as well as data that is being fed to shaders, Variant to/from serialization, and possibly other things I forgot.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: Can I "templatize" MathDefs?

PostPosted by TheComet » 02 Mar 2016, 10:38

Yeah, I noticed how heavily dependent everything is on floats. Would it be worth considering the introduction of a URHO3D_FLOAT that's typedef'd to double/float depending on the build configuration?
I'm a non-binary non-cis sexually fluid cephalopod identifying genderqueer mocha frappé latte
User avatar
TheComet
Active user
Active user
 
Posts: 122
Joined: 29 Jan 2014, 14:07
Location: Germany

Re: Can I "templatize" MathDefs?

PostPosted by cadaver » 02 Mar 2016, 11:40

Not convinced that's a good idea, because usually the reason to do that would be large world support, on the other hand there is a lot of data that is fine being float (double would be a waste of memory), and doubles would need conversion before feeding to the GPU, which would complicate the code.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: Can I "templatize" MathDefs?

PostPosted by rku » 16 Jul 2016, 07:12

For my own purposes i turned Vector3 into a template class because i needed Vector3 with 64bit integers that seamlessly integrate with rest of engine parts. It certainly isnt the best or complete implementation but it does what i need. I was wondering if there was interest to have it in engine. Check it out at https://github.com/rokups/Urho3D/commit ... 03dcc81a85
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Can I "templatize" MathDefs?

PostPosted by rku » 09 Aug 2016, 16:00

Shameless bump.

I fixed link in my previous post. It getting broken was probably why noone replied here ;) So please read my previous post if you havent.

All in all i would love to see all vector classes turned into templates. Now we have IntVector2 and Vector2. However Vector2 has more utility functions than IntVector2 but those functions are valid for both int and float types. I doubt copying them does any good to the codebase. And we all could benefit for vector classes of other data types. So what do you people think?
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Can I "templatize" MathDefs?

PostPosted by Stinkfist » 11 Aug 2016, 16:14

Templatized vector and matrix classes would be tricky for script bindings.
User avatar
Stinkfist
Have some posts
Have some posts
 
Posts: 29
Joined: 01 Apr 2014, 22:59
Location: Finland

Next

Return to Developer Talk

Who is online

Users browsing this forum: No registered users and 0 guests

cron