Class design, kinda math specific.

Started by
13 comments, last by GameDev.net 11 years ago

Its just, are you sure you need the same functionality more than once? Why not just have a function and assign the return value?

Ah, good point. The difference is in the utility of the class. If I have only the member function then an equation such as the following becomes impossible:


Vector3f negVec = -Normalize( other );

You have to write:


Vector3f otherNormalized = other;
otherNormalized.Normalize();
Vector3f negVec = -otherNormalized;

The point here is to avoid easy errors but also to prevent the C like verbosity required without the helper.

Writing "equations" be they based on float or a vector should not be "too" different given that the basic math operations are all the same. Results are completely different and an equation in one type of math is completely different in another but the idea is that: "a = 1+2 == 3" are writable in both cases, though in the case of vectors the result is actually {3, 3, 3}... This is a bit different than the "normalize" function but basically extending the concepts of floats to higher dimensions. Figuring out equations in 1D often expands to 3D, why should the math required be typed in differently?

Advertisement

I'm actually pretty unsure of the speed/clearness/etc of some of the decisions on my math library (in Java) but after going around 3 or so designs, my latest iteration does this:

public static Vector3f normalize( Vector3f vec, Vector3f dest )
{
 
//normalize vec, store in dest.
 
return dest;
}

Instead of writing 2 or 3 similar methods, I wrote only one that can be wired to have different results. If you don't want your vec to be changed, send a different one via the "dest" parameter. If you don't mind your vec changed, call the method with vec on both parameters.

You can call it with an anonymous object if you want since it returns the result. (ie, norm = normalize( vec, new Vector3f() ) ).

I don't think its the most performant implementation. It probably can't be used freely with some operations (matrix multiplication of the top of my head) but it does what I wanted to in a single method instead of using more.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

I'm actually pretty unsure of the speed/clearness/etc of some of the decisions on my math library (in Java) but after going around 3 or so designs, my latest iteration does this:


public static Vector3f normalize( Vector3f vec, Vector3f dest )
{
 
//normalize vec, store in dest.
 
return dest;
}

Instead of writing 2 or 3 similar methods, I wrote only one that can be wired to have different results. If you don't want your vec to be changed, send a different one via the "dest" parameter. If you don't mind your vec changed, call the method with vec on both parameters.

You can call it with an anonymous object if you want since it returns the result. (ie, norm = normalize( vec, new Vector3f() ) ).

I don't think its the most performant implementation. It probably can't be used freely with some operations (matrix multiplication of the top of my head) but it does what I wanted to in a single method instead of using more.

err... You sure about that code? :) Anyway, without operator overloads, Java is a really bad language to compare to in this area. Without overloads, Java doesn't allow a lot of the reasons for this subject matter. I.e. writing a simple equation which can be applied to multiple dimensions: a+b, be they floats, vector 2 or 3 etc.

What do you mean about if I'm sure? I don't remember C++ syntax (probably missing a :: or something like that) The complete method is this one:

public static DataArrayFloat normalize3f ( DataArrayFloat vec, DataArrayFloat dest )
{
float[] vecArray = vec.getArray(),
destArray = dest.getArray();
 
float norm = 1.0f / (float)(Math.sqrt(vecArray[0] * vecArray[0] + vecArray[1] * vecArray[1] + vecArray[2] * vecArray[2]));
 
destArray[0] = vecArray[0] * norm;
destArray[1] = vecArray[1] * norm;
destArray[2] = vecArray[2] * norm;
 
return dest;
}
As I implemted it, you can have a normalize method for arbitrary vector length by doing a for loop instead.

Anyway, why specifically do you need operator overloading for a normalize function? I reckon addition and substraction operators come in handy (see up there, no power ^ operator either heh) but I don't see its use for normalization.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

What do you mean about if I'm sure? I don't remember C++ syntax (probably missing a :: or something like that) The complete method is this one:

Sorry, it was meant as a joke at the time. :) Guess it didn't come across correctly....


public static DataArrayFloat normalize3f ( DataArrayFloat vec, DataArrayFloat dest )
{
return dest;
}
As I implemted it, you can have a normalize method for arbitrary vector length by doing a for loop instead.

Anyway, why specifically do you need operator overloading for a normalize function? I reckon addition and substraction operators come in handy (see up there, no power ^ operator either heh) but I don't see its use for normalization.

In the context of normalize as a single function, operators are not required as you say. I was just meaning that when placed inside the equational context where the operators are used, C++ normalize has different usages in different cases. The simplified solution we talked about just means 2 versions, one is the real version and the other is basically just syntactic sugar for use when placed into a larger equation as context.

I hope this makes a bit of sense. I can't think of a really good way to describe the intention here. It's almost the old C manner of doing math which is similar to the method of doing it using Java. This discussion is very much intended to the math overloads of operators and how a normalize function fits into that context. Hence Java is not really a good example when trying to make the comparisons and use/cases which I posted.

This topic is closed to new replies.

Advertisement