Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How many numbers are there on a line?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Gooie




PostPosted: Sun Dec 23, 2007 2:59 pm   Post subject: How many numbers are there on a line?

I have been having a lot of trouble with some concepts when making something 3D. So I have another question I've been trying to wrap my head around. File I/O isn't my strong suit, but I'm getting better. How would I figure out how many numbers come after an 'f' in a line. I can't change the file, since it's output is from a 3D modeling program. (Ignore the lines with 'g' and 'v'. I've figured those out already.)

Here are some examples of a model.

Cube
code:

g
v -0.5 -0.5 -0.5
v 0.5 -0.5 -0.5
v 0.5 -0.5 0.5
v -0.5 -0.5 0.5
v -0.5 0.5 -0.5
v 0.5 0.5 -0.5
v 0.5 0.5 0.5
v -0.5 0.5 0.5
g
f 2 6 5 1
f 3 7 6 2
f 4 8 7 3
f 1 5 8 4
f 3 2 1 4
f 6 7 8 5


Sphere
code:

g
v 0 0 -1
v 0 0.360728621 -0.932670832
v 0.343073219 0.111471429 -0.932670832
v 0 0.67288357 -0.739748418
v 0.356822073 0.491123766 -0.79465431
v 0.639950156 0.20793274 -0.739748418
v 0 0.894427419 -0.447213173
v 0.343073279 0.784355044 -0.51680547
v 0.639950156 0.568661451 -0.51680553
v 0.850650847 0.276393682 -0.447213203
v 0.212030917 -0.291835517 -0.932670832
v 0.577350378 -0.187592328 -0.794654429
v 0.395510972 -0.544374168 -0.739748478
v 0.851981342 -0.0839028805 -0.516805649
v 0.738584518 -0.432902813 -0.516805649
v 0.525731206 -0.723606944 -0.447213262
v -0.212030917 -0.291835517 -0.932670832
v 0 -0.607062101 -0.794654369
v -0.395510972 -0.544374168 -0.739748478
v 0.183480024 -0.836209893 -0.516805649
v -0.183480024 -0.836209893 -0.516805649
v -0.525731206 -0.723606944 -0.447213262
v -0.343073219 0.111471429 -0.932670832
v -0.577350378 -0.187592328 -0.794654429
v -0.639950156 0.20793274 -0.739748418
v -0.738584518 -0.432902813 -0.516805649
v -0.851981342 -0.0839028805 -0.516805649
v -0.850650847 0.276393682 -0.447213203
v -0.356822073 0.491123766 -0.79465431
v -0.639950156 0.568661451 -0.51680553
v -0.343073279 0.784355044 -0.51680547
v -0.212030932 0.964718997 -0.156077206
v -0.577350199 0.794654608 -0.187592268
v -0.395510912 0.905102789 0.156077221
v -0.851980984 0.499768257 -0.156077206
v -0.738584161 0.655845463 0.156077206
v -0.525731206 0.723606944 0.447213262
v -0.983023465 0.0964612812 -0.156077221
v -0.934172392 -0.303531021 -0.187592298
v -0.983023465 -0.0964612812 0.156077221
v -0.738584161 -0.655845463 -0.156077206
v -0.851980984 -0.499768257 0.156077206
v -0.850650847 -0.276393682 0.447213203
v -0.395510912 -0.905102789 -0.156077221
v 0 -0.982246995 -0.187592283
v -0.212030932 -0.964718997 0.156077206
v 0.395510912 -0.905102789 -0.156077221
v 0.212030932 -0.964718997 0.156077206
v 0 -0.894427419 0.447213173
v 0.738584161 -0.655845463 -0.156077206
v 0.934172392 -0.303531021 -0.187592298
v 0.851980984 -0.499768257 0.156077206
v 0.983023465 0.0964612812 -0.156077221
v 0.983023465 -0.0964612812 0.156077221
v 0.850650847 -0.276393682 0.447213203
v 0.851980984 0.499768257 -0.156077206
v 0.577350199 0.794654608 -0.187592268
v 0.738584161 0.655845463 0.156077206
v 0.212030932 0.964718997 -0.156077206
v 0.395510912 0.905102789 0.156077221
v 0.525731206 0.723606944 0.447213262
v 0 0.982246995 0.187592283
v -0.183480024 0.836209893 0.516805649
v 0.183480024 0.836209893 0.516805649
v -0.934172392 0.303531021 0.187592313
v -0.851981342 0.0839028805 0.516805649
v -0.738584518 0.432902813 0.516805649
v -0.577350199 -0.794654608 0.187592268
v -0.343073279 -0.784355044 0.51680547
v -0.639950156 -0.568661451 0.51680553
v 0.577350199 -0.794654608 0.187592268
v 0.639950156 -0.568661451 0.51680553
v 0.343073279 -0.784355044 0.51680547
v 0.934172392 0.303531021 0.187592298
v 0.738584518 0.432902813 0.516805649
v 0.851981342 0.0839028805 0.516805649
v 0 0 1
v 0.212030917 0.291835517 0.932670832
v -0.212030917 0.291835517 0.932670832
v 0.395510972 0.544374168 0.739748478
v 0 0.607062101 0.794654369
v -0.395510972 0.544374168 0.739748478
v 0.343073219 -0.111471429 0.932670832
v 0.639950156 -0.20793274 0.739748418
v 0.577350378 0.187592328 0.794654429
v 0 -0.360728621 0.932670832
v 0 -0.67288357 0.739748418
v 0.356822073 -0.491123766 0.79465431
v -0.343073219 -0.111471429 0.932670832
v -0.639950156 -0.20793274 0.739748418
v -0.356822073 -0.491123766 0.79465431
v -0.577350378 0.187592328 0.794654429
g
f 3 2 1
f 5 4 2
f 6 5 3
f 8 7 4
f 9 8 5
f 10 9 6
f 3 5 2
f 5 8 4
f 6 9 5
f 11 3 1
f 12 6 3
f 13 12 11
f 14 10 6
f 15 14 12
f 16 15 13
f 11 12 3
f 12 14 6
f 13 15 12
f 17 11 1
f 18 13 11
f 19 18 17
f 20 16 13
f 21 20 18
f 22 21 19
f 17 18 11
f 18 20 13
f 19 21 18
f 23 17 1
f 24 19 17
f 25 24 23
f 26 22 19
f 27 26 24
f 28 27 25
f 23 24 17
f 24 26 19
f 25 27 24
f 2 23 1
f 29 25 23
f 4 29 2
f 30 28 25
f 31 30 29
f 7 31 4
f 2 29 23
f 29 30 25
f 4 31 29
f 32 31 7
f 33 30 31
f 34 33 32
f 35 28 30
f 36 35 33
f 37 36 34
f 32 33 31
f 33 35 30
f 34 36 33
f 38 27 28
f 39 26 27
f 40 39 38
f 41 22 26
f 42 41 39
f 43 42 40
f 38 39 27
f 39 41 26
f 40 42 39
f 44 21 22
f 45 20 21
f 46 45 44
f 47 16 20
f 48 47 45
f 49 48 46
f 44 45 21
f 45 47 20
f 46 48 45
f 50 15 16
f 51 14 15
f 52 51 50
f 53 10 14
f 54 53 51
f 55 54 52
f 50 51 15
f 51 53 14
f 52 54 51
f 56 9 10
f 57 8 9
f 58 57 56
f 59 7 8
f 60 59 57
f 61 60 58
f 56 57 9
f 57 59 8
f 58 60 57
f 59 32 7
f 62 34 32
f 60 62 59
f 63 37 34
f 64 63 62
f 61 64 60
f 59 62 32
f 62 63 34
f 60 64 62
f 35 38 28
f 65 40 38
f 36 65 35
f 66 43 40
f 67 66 65
f 37 67 36
f 35 65 38
f 65 66 40
f 36 67 65
f 41 44 22
f 68 46 44
f 42 68 41
f 69 49 46
f 70 69 68
f 43 70 42
f 41 68 44
f 68 69 46
f 42 70 68
f 47 50 16
f 71 52 50
f 48 71 47
f 72 55 52
f 73 72 71
f 49 73 48
f 47 71 50
f 71 72 52
f 48 73 71
f 53 56 10
f 74 58 56
f 54 74 53
f 75 61 58
f 76 75 74
f 55 76 54
f 53 74 56
f 74 75 58
f 54 76 74
f 79 78 77
f 81 80 78
f 82 81 79
f 64 61 80
f 63 64 81
f 37 63 82
f 79 81 78
f 81 64 80
f 82 63 81
f 78 83 77
f 85 84 83
f 80 85 78
f 76 55 84
f 75 76 85
f 61 75 80
f 78 85 83
f 85 76 84
f 80 75 85
f 83 86 77
f 88 87 86
f 84 88 83
f 73 49 87
f 72 73 88
f 55 72 84
f 83 88 86
f 88 73 87
f 84 72 88
f 86 89 77
f 91 90 89
f 87 91 86
f 70 43 90
f 69 70 91
f 49 69 87
f 86 91 89
f 91 70 90
f 87 69 91
f 89 79 77
f 92 82 79
f 90 92 89
f 67 37 82
f 66 67 92
f 43 66 90
f 89 92 79
f 92 67 82
f 90 66 92

Here is a the section of the code I need help with, it's not everything. it's in a class though. I doubt this is difficult, I'm probably missing something obvious.

Turing:

    procedure FetchModelVaribale

        /* Produces The Correct Number of Vertex Variables
         Based on previous procedures output. */

        new Vertex, VertexCount

        /* Creates a string variable for capturing the 'v'
         or 'g' in the file line. */

        var LineType : string

        /* This is a dummy variable used as a more flexible
         'for' loop. */

        var VertexCounter, FaceCounter : int := 1

        /* Opens the File Based on the input from previous
         procedures. */

        open : ModelFile, FilePath, get

        loop

            /* End the loop when there are no more lines to read.*/
            exit when eof (ModelFile)

            /*Gets a the first word in the line, and decides what
             method to use to extract the information*/

            get : ModelFile, LineType

            /* Scans The Lines based on the type of information
             that is there, defined by the first letter in the line. */

            if LineType (1) = "v" then /*In case of Vertex Line Type*/

                /* Each 'get' takes all the information from the file
                 until it hits a space, then it moves to the next 'get'.*/

                get : ModelFile, Vertex (VertexCounter).X
                get : ModelFile, Vertex (VertexCounter).Y
                get : ModelFile, Vertex (VertexCounter).Z

                /* Increase counter to change Vertex Subscript on
                 next pass. */

                VertexCounter += 1

            elsif LineType (1) = "f" then /*In case of Face Line Type*/
           
                /* Increase counter to change Face Subscript on
                 next pass. */

                FaceCounter += 1

            end if
        end loop
        VertexCounter := 1
        FaceCounter := 1
    end FetchModelVaribale
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Mon Dec 24, 2007 10:27 am   Post subject: RE:How many numbers are there on a line?

Just a note, there is such a thing as overcommenting.
Mazer




PostPosted: Mon Dec 24, 2007 10:49 am   Post subject: RE:How many numbers are there on a line?

For readability, yes. For full marks, often no.
Sean




PostPosted: Mon Dec 24, 2007 11:24 am   Post subject: Re: How many numbers are there on a line?

Gooie usually reduces his commenting on programs to a minimum, allowing proper use of documentation, but for this, he was just using it so others could understand what each did, and so he remembered which did what, I suspect.
Nick




PostPosted: Mon Dec 24, 2007 12:50 pm   Post subject: RE:How many numbers are there on a line?

the problem doesn't seem to be file I/O but rather string manipulation

try this:
code:
for i:1..length(LineType)
    if LineType(i) = ' ' then
        numCount+=1
    end if
end for
Gooie




PostPosted: Mon Dec 24, 2007 2:40 pm   Post subject: Re: RE:How many numbers are there on a line?

Clayton @ December 24th, 10:27 am wrote:
Just a note, there is such a thing as overcommenting.


Haha, ya I realize that now. I just was tired of not commenting at all, and not knowing what anything did, so I went a bit nuts, I need a happy medium. >.<

momop @ December 24th, 12:50 pm wrote:
the problem doesn't seem to be file I/O but rather string manipulation

try this:
code:

for i:1..length(LineType)
    if LineType(i) = ' ' then
        numCount+=1
    end if
end for


Nice! Thanks, I'll try that. Now, all I need is a proper container for the face values. Why doesn't Turing have flexible multidimensional arrays! Why! They would be so useful.
HeavenAgain




PostPosted: Mon Dec 24, 2007 2:52 pm   Post subject: RE:How many numbers are there on a line?

Quote:
Why doesn't Turing have flexible multidimensional arrays! Why! They would be so useful.
they do i think, look at the turing tut section
Nick




PostPosted: Mon Dec 24, 2007 2:55 pm   Post subject: RE:How many numbers are there on a line?

they have multidimensional arrays and flexible arrays just not both together :\
Sponsor
Sponsor
Sponsor
sponsor
Gooie




PostPosted: Mon Dec 24, 2007 3:11 pm   Post subject: Re: RE:How many numbers are there on a line?

momop @ December 24th, 12:50 pm wrote:
the problem doesn't seem to be file I/O but rather string manipulation

try this:
code:
for i:1..length(LineType)
    if LineType(i) = ' ' then
        numCount+=1
    end if
end for


Ok well, I tried putting that in, and it would work fine, except then LineType only captures the first word in the line. then in sequence the vector coordinates are captured. and I can't even add ": *" because then the rest of the program wouldn't work. I just need to know how to go back and reread the last line. Is there a way?
Nick




PostPosted: Mon Dec 24, 2007 3:23 pm   Post subject: Re: RE:How many numbers are there on a line?

Gooie @ Mon Dec 24, 2007 3:11 pm wrote:
momop @ December 24th, 12:50 pm wrote:
the problem doesn't seem to be file I/O but rather string manipulation

try this:
code:
for i:1..length(LineType)
    if LineType(i) = ' ' then
        numCount+=1
    end if
end for


Ok well, I tried putting that in, and it would work fine, except then LineType only captures the first word in the line. then in sequence the vector coordinates are captured. and I can't even add ": *" because then the rest of the program wouldn't work. I just need to know how to go back and reread the last line. Is there a way?


you can put ":*"
I read over you're program but without the rest it was a little hard to make it run however I did find a couple things to comment on
1.
Quote:
get : ModelFile, Vertex (VertexCounter).X

should be
code:
get : ModelFile, Vertex (VertexCounter)->X
(assuming Vertex is a record or class)
2. when you create Vertex origanally you declare it as a single class/record but later you call it as though it is an array of a pointer to a class or record
so instead of [code]var Vertex:^ [insert class/record name here]
%it should be
var Vertex:array 1.. [insert array upper here] of ^[insert class/record name here][/code
then when you create a pointer it should look like this:
[code]new Vertex([array number here ex. 1])[/code]

and lastly to answer where and how to put ":*" it should look like this
[code] get:ModelFile, LineType:*[/code]

I don't know what Vertex(VertexCounter)-> X/Y/Z are for but they should also have a :* if the are reading the line
Clayton




PostPosted: Mon Dec 24, 2007 3:36 pm   Post subject: Re: RE:How many numbers are there on a line?

HeavenAgain @ Mon Dec 24, 2007 2:52 pm wrote:
Quote:
Why doesn't Turing have flexible multidimensional arrays! Why! They would be so useful.
they do i think, look at the turing tut section


They do, just not as you think they would. Multi-dimensional flexible arrays are actually a flexible array of static arrays. You can resize the first dimension all you want, just not the first.
Gooie




PostPosted: Mon Dec 24, 2007 4:22 pm   Post subject: Re: How many numbers are there on a line?

Hm... All of this help has been much appreciated. But, honestly, I don't think I have stated my question clearly enough.

I have to gather two types of information, vertexes and faces.

First I need to figure out the line is face or vertex.

If it's a vector it begin with a "v". Followed by three real values (X, Y, Z), each separated by a space.

code:

v -0.5 0.324 -0.324e-122


I need to store each value in a flexible array of a record that has three real values it corresponding. To the information in the file.

Turing:

    var Vertex : flexible array 1 .. 0 of
        record
            X : real
            Y : real
            Z : real
        end record


The size of the array is figured out by a previous procedure.

Turing:

        loop
            exit when eof (ModelFile)
            get : ModelFile, CurrentFileWord
            if CurrentFileWord = "v" then
                VertexCount_ += 1
            elsif CurrentFileWord = "f" then
                FaceCount_ += 1
            end if
        end loop


If it's a face it begin with an "f". Followed by three or four integer values each separated by a space.

code:

f 1 2 3


code:

f 1 2 3 4


They are the numbers of vertexes to connect to make a face.

There is a procedure earlier to find how many faces there are. Variable being "FaceCount"

I don't know how I will store theses yet, or how I will collect them. If I get the whole line I can't go back and read the values. If I just get the first word to find if it's a face or vertex, then I have no idea how many values will follow. How can I collect these values? and what sort of variable should I use?
Nick




PostPosted: Mon Dec 24, 2007 6:13 pm   Post subject: RE:How many numbers are there on a line?

well the best way in my opinion is to do something like this

1. get current line
- to do this imply read the whole line when with CurrentFileWord

2. ignore the first letter, then declare the following numbers (seperated by spaces) to Vertex X,Y,Z

to do number two find out where the number starts and where it ends then convert it from a string to a real (use strreal and strrealok)
Gooie




PostPosted: Mon Dec 24, 2007 7:07 pm   Post subject: Re: RE:How many numbers are there on a line?

momop @ December 24th, 6:13 pm wrote:
well the best way in my opinion is to do something like this

1. get current line
- to do this imply read the whole line when with CurrentFileWord

2. ignore the first letter, then declare the following numbers (seperated by spaces) to Vertex X,Y,Z

to do number two find out where the number starts and where it ends then convert it from a string to a real (use strreal and strrealok)


Oh man! Thanks! I didn't know about 'strtrealok'! That pretty much solved all my problems, thanks. For a container, i think I'll use a flexible array of a record, very similar to flexible multidimensional array. Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: