[Closed] How to create customized math operators in MaxScript
Hi,
Is there a way to create a customized math operator in MaxScript? Please, look at the following C code example (it shows a customized ‘*’ operator for multiplying two vectors):
inline MYVECTOR operator *(MYVECTOR &V1, MYVECTOR &V2)
{
return MYVECTOR (V1.x*V2.x, V1.y*V2.y, V1.z*V2.z);
}
Is there some way to create this in MaxScript?
Thanks in advance
You can’t do your own operator overloading in MXS, as you can in C++. You have to write functions, or ‘methods’ in a struct defining your own ‘type’ to do the job. (The quotes are because it’s not like true Object Orientation) .
However, in this particular case, MXS has already overloaded ‘*’ on Point3’s for you, so it returns the results you would expect.
Thanks drdubosc.
So, unfortunately there is no way to write our own operator… It would be nice if we could do this:
MOut = M1 * M2 * M3 * M4 * M5
instead of this:
MOut = MultMyMatrices M1 M2
MOut = MultMyMatrices MOut M3
MOut = MultMyMatrices MOut M4
MOut = MultMyMatrices MOut M5
However, in this particular case, MXS has already overloaded ‘*’ on Point3’s for you, so it returns the results you would expect.
Yes, I agree, but that was just an example. Actually I would like to create my own 4×4 matrices and 4×1 column vectors.
However, in this particular case, MXS has already overloaded ‘*’ on Point3’s for you, so it returns the results you would expect.
I am probably wrong, but don’t many/most languages consider Vector multiplication a Dot product?
Also, MagicToy, don’t forget you can pass arrays into functions, so you could have:
MultMyMatrices #(M1, M2, M3, M4, M5)
and the function would say something like:
fn MultiMyMatrices arr =
(
local ret = arr[1]
for i = 2 to arr.count do
ret *= arr[i]
ret
)
But yeah, lack of operator overloading sucks. The more I use other languages and improve software design, when I go back to MXS, I get very frustrated.
Out of curiosity, what do you need explicit 4×4 matrices for, that can’t be handled by the MXS matrix3 type (which presumably bangs on a 0001 w column internally?)
You are right, I could use matrix3. Well… nothing in special, it’s just because I like to work with column vectors, so in this case the matrices should be transposes of matrix3… but I already abandoned this idea. Thanks!
I learned things the other way up as well, all that time ago …
Ok, perhaps not what you’d expect :)! Depends on your local convention, I guess. I don’t think C++ has a standard library for it, which would lay down the rules.