Im changing vecmath for my personal needs to make it more usable. The naming of methods is a bit counter intuitive so i have changed it but the idea is the same. I think these changes can be added to the official vecmath api if necessary as they only relate to usability changes.
Constructors:
Empty constructor:
Tuple<><>()
where Type is one of: b, s, i, l, c, f, d. Yes im having one for every primitive type for concistency.
Length i >= 1 and <= 4. I have a 1 length tuple because a tuple is mutable and a Tuple1i works much like an Integer class except that Integer is immutable and Tuple1i is not. Don’t know if this is useful or not.
Tuple components are public and accesed by the names x, y, z, w.
To copy data into a tuple the literal() method is used: new Tuple3f().literal(1.0f, 1.0f, 1.0f)
There are literal methods to feed each tuple with data that take as arguments (L is length and T is type of the tuple):
1 value of type T that will be copied to all tuple components
L values of type T one for each component
1 array of type T and an offset in that array from which L values will be copied
1 tuple of length L
1 tuple of length less than L and one pad value
Methods to copy tuple components into an array like arraycopy:
T[] toArray(T[] dest, int ofs)
T[] toArray(int ofs, int len, T[] dest, int ofs2)
Methods to treat a tuple like an array. Don’t know if this is useful or not but i want the permute method so i need these for that method:
double element(int i)
TupleLT assign(int i, T value)
int length();
Class type();
TupleLT permute(int i, int j)
The usal tuple math but modified so that a tuple reference to self is allways returned. This allows for expressions like:
t.scale(s).absolute().negate()
There are operations for: absolute(), negate(), scale(s), add(t), subtract(t), clone(), toString(), equals(t), equals(t,epsilon), hashCode()
Im considering extending tuples to implement Matrices. In this case there would be an adicional TupleT9 (3x3) and TupleT16 (4x4) for matrices which would support the basic math operations for tuples. Components would be refered to c0…c8 and c0…c15. When extended by a MatrixTL the related matrix operations are added.
Another interesting functionality i am considering adding is an apply method that would work with a class FunctionLT where L is the number of arguments and T is the type of the arguments and return value, for example Function0f, Function2d, etc.
An apply or similar function could be very useful.
Filtering a tuple. Every component c is replaced by f©:
TupleLT apply(Function1T f)
Combine two tuples, c0 = f(c0,t.c0),etc:
TupleLT combine(Function2T f, TupleLT t2)
Apply a 2 arg repeatidly and return f(c0, f(c1,…)) or left associative:
T rightAssociative(Function2T fun)
T leftAssociative(Function2T fun)
For example a linear combination:
t.combine(new MultiplyFunction2f(), t2).leftAssociative(new SumFunction2f())
Another altirnative put these methods in a utility class as static for extra speed. EMath stands for extra math:
T EMath.linearCombination(TupleLT t1, Tuple LT t2)
TupleLT EMath.linarInterpolation(TupleLT t1, Tuple LT t2, T alpha)
T EMath.clamp(TupleLT t, int min, max)
I haven’t given a good consideration on this yet but i was thinking of creating classe for TupleBufferLT that would implement NIO buffers of tuples.
PS: Notice this is mostly a usability rearrangement in hope that it become less confusing to recall and select what methods to use when working with vecmath. The point is not making clever algorithms but making vecmath method easier to remenber.