vectorMath, crossProduct, scalarProduct

 From:  Michael Gibson
4951.4 In reply to 4951.1 
Hi Brian,

Your code was:
quote:

function crossProduct( v0, v1, startpt )
{
var vcx = (v0.y * v1.z) - (v0.z * v1.y);
var vcy = (v0.z * v1.x) - (v0.x * v1.z);
var vcz = (v0.x * v1.y) - (v0.y * v1.x);
var vcross_endpt = moi.vectorMath.createPoint( vcx, vcy, vcz );
var vcross = moi.vectorMath.makeVector( startpt, vcross_endpt);
return vcross;
}


So this is a kind of weird way to do cross product - normally the cross product takes in 2 vectors as input and returns one new vector as output which is perpendicular to both inputs, unless they were colinear in which case it will be zero magnitude vector (or almost zero magnitude due to the tiny bit of fuzz in floating point calculations).

The part that you may be getting stuck on is that to build a vector directly from x, y, z values you actually want to use createPoint() to do that, since you will actually hold a point or a vector in the exact same point object since they both just need to contain an x,y,z triplet value. Since both vectors and points have the exact same data in them, there's just one class for them since otherwise they would just have all the exact same properties and methods and everything.

So the return value from crossProduct should be sent back with createPoint() - you only use makeVector for building a vector that represents a displacement between a "from point" and a "to point".

So a more normal cross product function would look like this:

code:
function crossProduct( v0, v1 )
{
    var x = (v0.y * v1.z) - (v0.z * v1.y);
    var y = (v0.z * v1.x) - (v0.x * v1.z);
    var z = (v0.x * v1.y) - (v0.y * v1.x);

    return moi.vectorMath.createPoint( x, y, z );
}



And normally the result of a scalar product/dot product does not produce a vector as output, but instead generates a single number as the output. It's not the same thing as "scaling" a vector.

So normally the dot product would be like this:
code:
function dotProduct( v0, v1 )
{
    return (v0.x * v1.x) + (v0.y * v1.y) + (v0.z * v1.z);
}


If you want to scale up a vector to increase or decrease its magnitude you would do something like this:

code:
function scaleVector( v, factor )
{
    var x = v.x * factor;
    var y = v.y * factor;
    var z = v.z * factor;
    return moi.vectorMath.createPoint( x, y, z );
}



That one produces a new scaled vector as output from the function.

There is also a method .scale( ) that you can call on a point or vector to modify that vector by a scale factor.

To do that you would do something like this:

var vector = <get a vector somewhere...>
vector.scale( 5.0 );

That .scale method is on IMoiPoint - look up IMoiPoint in the moi.idl file to see what else is there - that's also where you will see the properties for x, y, z listed.

There's not a whole lot of stuff set up for scripting this stuff yet - in the future I want to expand the library of functions available to script for doing this kind of stuff. Right now you will need to make some various helper functions of your own though.

Hope this helps!

- Michael

EDITED: 21 Feb 2012 by MICHAEL GIBSON