PDA

View Full Version : COFFEE | Getting midpoint using arrays, howto?


tcastudios
06-22-2006, 12:12 PM
I need some help understanding using arrays.
What I'd like to do is using the selected points in a PointSelectionTag.
Add those points positions and divide by the amount of points to get a mid position.

I come as far as the code below, I get the indexes and corresponding positions,but cannot get the grips of using an array to store and calculate them.
Given that the result allready is an array, as it looks, I might be on the wrong track, maybe.....

I -assume- I'll have to use an array for this, if not is there a better way?

Cheers
Lennart

main(doc,op)
{

var link = op#ID_USERDATA:1; // A object in a linkfield
if(link->IsInstanceOf(Tpointselection) !=FALSE) // If it is a PointSelectionTag run
{
println("YES"); // It´s a pointselectionTag
}
else
{
println("NO");
return;
}
// Get the selected points , index and position
var pointsel = link->GetSelection(); // get whats stored in the Tag
var count = link->GetObject()->GetPointCount(); // get the amount of points in the object
var i=0;
var found=0;
for (i=0; i< count; i++)
{
if(pointsel->IsSelected(i))
{
found++;
println(link->GetObject()->GetPoint(i)); // Prints the selected points position(s) as an array
println(i); // print the indexes of the points
println(found); // prints the amount of selected points

/*
Here I need to add all selected points positions and divide by found to get the avarage position
*/
}
}

}

Srek
06-22-2006, 01:06 PM
Just add all vectors upp and then divide the length of the resulting vecotr by the number of vectors you added up
Cheers
Björn

tcastudios
06-22-2006, 02:29 PM
Thanks Srek, but what I fail with is to use those specific pointpositions to add and divide.
When I try to use various add functions in the realms of ,
for (i = i+ < found ; i++ ) for example, I get not compatible values....

So I'm still at loss alittle.....I have the selected points, I have their positions...but can't find out how to add them.......

Cheers
Lennart

Srek
06-22-2006, 02:49 PM
You have to add up the components seperately, then get the length of the resulting vector, divide it by the count of vectors and multiply the result with the normal of the added up vector.
Cheers
Björn

JDP
06-22-2006, 07:49 PM
Hi Lennart, in this instance you do not need an array. The operators you are looking for are += and /= . An example: instead of saying i = i + 10 you can write i += 10. For your particular problem we're using vectors so just initialise a vector variable (lets call it pos) before you go into your loop for finding selections. When you find one that is selected use += to add it to your vector variable. Also before the loop initialise a variable for how many points you find (lets call it found)that are selected and use ++ to add 1 to it every time you do. After you exit the loop just divide pos by found (pos /= found), it will divide each component of the vector pos with found's value. Here's an example to clear up any ambiguities.



var tag = op->GetFirstTag();
while(tag && tag->GetType()!= Tpointselection)
tag = tag->GetNext();
var count = op->GetPointCount();
var selection = tag->GetSelection();
var pos = vector(0.0); // initialise vector variable
var found = 0;
var i;
for(i = 0; i<count; i++)
{
if(sel->IsSelected(i))
{
pos += op->GetPoint(i);
found++;
}
}
pos /= found;
println(pos);


This will give you the average position of the points in object space. If you want it in global space then get the objects global matrix and use the GetMulP function, so the script would now become something like:



var gMat = op->GetMg();
var tag = op->GetFirstTag();

while(tag && tag->GetType()!= Tpointselection)
tag = tag->GetNext();

var count = op->GetPointCount();
var selection = tag->GetSelection();
var pos = vector(0.0); // initialise vector variable
var found = 0;
var i;

for(i = 0; i<count; i++)
{
if(sel->IsSelected(i))
{
pos += gMat->GetMulP(op->GetPoint(i));
found++;
}
}

pos /= found;
println(pos);


Regards
JDP

tcastudios
06-22-2006, 10:20 PM
Absolutely fantastic, JDP!
I was messing around with the += and /= (Thanks to Samir) but right now I can see I totaly missed to initialize the positions as vectors. Darn, I'm old and and examples like this makes it possible to get the context of what is stated in the SDK, what relates to what in reality.

Cheers
Lennart

JDP
06-22-2006, 11:38 PM
can see I totaly missed to initialize the positions as vectors.


Actually I've discovered that you don't have to tell the variable it's a vector, as long as you initialise it with a value, e.g. 0,once the first += occurs it just changes it to a vector. I was thinking in C++ where you declare a variable by stating what type it is and you can't just jump between types willy nilly, this is one of COFFEE's idiosyncrasies.

regards
JDP

CGTalk Moderation
06-22-2006, 11:38 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.