View Full Version : How to convert a long string into vector array?
AtrusDni 05-30-2008, 12:35 AM Hey guys, so im reading data from a text file, and the positions of verticies is stored in one long line like this: (actually its longer, but for sake of clarity im showing only these)
-16.99 0.43 -34.11 -1.24 -11.19 -20.22 -0.39 9.69 -45.17 -9.45 1.01 2.56 -2.16 2.03 7.99 -3.03 20.11 2.02;
So every 3 values describes the XYZ position of a point on a triangle, and every 9 values describes 1 triangle face. So in this example there would be 2 triangle faces.
What would be the easiest way to convert this string into an array so all the elements look like this:
$positionArray[0] = <<-16.99, 0.43, -34.11, -1.24, -11.19, -20.22, -0.39, 9.69, -45.17>>;
$positionArray[1] = <<-9.45, 1.01, 2.56, -2.16, 2.03, 7.99, -3.03, 20.11, 2.02>>;
etc . . .
So basically every 9 values get put into their own slot in an array so i can query faces based on array id numbers?
|
|
dbsmith
05-30-2008, 02:31 AM
Well you could do it that way.
Or you could just put your numbers in one big array and have a formula that calculates which triangle or vertex you want eg
get vertex 7's Y value.
= (7 *3) +1
or get triangle 4's 3 vertices
= (4 * 9) + (...iterate from 0 to 8)
Or use 3 arrays, one for X values, one for Y and one for Z values.
Whatever suits u best, i dont think it really matters
AtrusDni
05-30-2008, 04:49 PM
thanks for the help dbsmith. Yeah, the way I was asking for was just off the top of my head, really anything thats easy and works is good by me. Ill give this a shot.
you can tokenize that string and put each number into a single array. then you have the choice of doing as dbsmith stated and using math when pulling numbers from the array or you can loop through the indicies of this array and make 3 new arrays. depending on what you are doing with these #s will determine which is more efficient.
you could even loop through the array and make a new array that contains the 9 #s per index, but it seems like you would break this down again later in your script so it may be redundant.
AtrusDni
05-31-2008, 01:58 AM
Yes thats what I want to do, im just not sure how to do it though. Im trying nested for loops but its not working right. I've decided that I want to store 9 values per array index. I will have to break them up again, but thats simple and that way each index is equal to 1 poly and its easier to set it up that way. Any ideas on how to do that?
Right now i have the long string already tokenized and stored in an array so each index contains a value.
I just got home but I threw this together real quick off the top of my head.
string $finalArray[]; //this will be your final array.
string $nextLine = "11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46"; //this should be your very long string of numbers each seperated by a blank space.
string $tempArray[]; //this is your current tokenized array.
tokenize $nextLine " " $tempArray;
ok so in the example above we already have the string of #s and tokenized them into the $tempArray. we also set up an empty array called $finalArray that will contain 9 #s per index.
for ($i = 0; $i < size($tempArray); $i += 9) {
string $poly = "";
for ($z = $i; $z <= ($i + 8); $z++) {
$poly += ( $tempArray[$z] + " " );
}
$finalArray[size($finalArray)] = $poly;
}
print $finalArray;
clear $finalArray;
we cycle through the indexs of the $tempArray but instead of doing it one at a time we jump 9 #s (hence the $i += 9).
I then nested another loop inside this one that starts at the same number as $i ($z = $i) with a condition that $z is less than or equal to $i + 8.
this will enable our string $poly to contain up to 9 values before the original loop continues to add the completed $poly string as a new index within the $final array.
hope this helps.
AtrusDni
05-31-2008, 05:00 AM
AH HA! Thats where I was getting stuck on! Thanks so much DOor! This helps a lot thank you.
CGTalk Moderation
05-31-2008, 05:00 AM
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.