View Full Version : How to create customized math operators in MaxScript
MagicToy 10-18-2008, 01:12 PM 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
|
|
drdubosc
10-18-2008, 01:35 PM
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.
MagicToy
10-18-2008, 02:16 PM
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 4x4 matrices and 4x1 column vectors.
RobGalanakis
10-18-2008, 04:19 PM
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.
drdubosc
10-18-2008, 04:47 PM
...Actually I would like to create my own 4x4 matrices and 4x1 column vectors.
Out of curiosity, what do you need explicit 4x4 matrices for, that can't be handled by the MXS matrix3 type (which presumably bangs on a 0001 w column internally?)
MagicToy
10-18-2008, 05:44 PM
Out of curiosity, what do you need explicit 4x4 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!
drdubosc
10-18-2008, 07:21 PM
....I like to work with column vectors, so in this case the matrices should be transposes of matrix3
I learned things the other way up as well, all that time ago ...
don't many/most languages consider Vector multiplication a Dot product?
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.
CGTalk Moderation
10-18-2008, 07:21 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.