POV-Ray : Newsgroups : povray.general : Reading from a text file Server Time
1 Nov 2024 11:15:24 EDT (-0400)
  Reading from a text file (Message 1 to 10 of 14)  
Goto Latest 10 Messages Next 4 Messages >>>
From: Scott
Subject: Reading from a text file
Date: 5 Jan 2012 08:45:00
Message: <web.4f05a8fc1cabb177be8719c80@news.povray.org>
This should be easy, but I'm having some trouble.

I would like to read a series of vectors <x,y,z> from a text file and plot the
points as spheres.  To test things out, I made a text file with 2 vectors
separated by commas.

I get this error message.  Can anyone tell me what I am doing wrong?

//-------------------------------------------
#fopen MyFile "myData.txt" read
# local i = 1;
  #while (i<2)
  #read (MyFile,Vector[i]
Parse Error: Expected 'undeclared identifier', empty array found instead
//---------------------------------------------

Here is the POV-Ray code I used:

#declare maxArray = 2;
#declare Vector = array[maxArray];

#fopen MyFile "myData.txt" read
# local i = 1;
  #while (i<2)
        #read (MyFile,Vector[i])
        #local i = i + 1;
  #end

#declare Graphic = union {

sphere {
  Vector[i],    1  texture {
    pigment { color rgb <1,0,0> }
    finish { Substance }
  }
}
sphere {
  Vector[i],    1  texture {
    pigment { color rgb <1,1,0 > }
    finish { Substance }
  }
}
} // end of Graphic


Post a reply to this message

From: Jim Holsenback
Subject: Re: Reading from a text file
Date: 5 Jan 2012 09:39:46
Message: <4f05b632$1@news.povray.org>
On 01/05/2012 08:44 AM, Scott wrote:
> This should be easy, but I'm having some trouble.
>
> I would like to read a series of vectors<x,y,z>  from a text file and plot the
> points as spheres.  To test things out, I made a text file with 2 vectors
> separated by commas.
>
> I get this error message.  Can anyone tell me what I am doing wrong?
>
> //-------------------------------------------
> #fopen MyFile "myData.txt" read
> # local i = 1;
>    #while (i<2)
>    #read (MyFile,Vector[i]
> Parse Error: Expected 'undeclared identifier', empty array found instead
> //---------------------------------------------

have a look at the following if you've not done that already:
http://wiki.povray.org/content/Documentation:Reference_Section_2.3#Array_Identifiers

should be #local i = 0; ????


Post a reply to this message

From: Scott
Subject: Re: Reading from a text file
Date: 5 Jan 2012 09:50:01
Message: <web.4f05b8068e1c2e69be8719c80@news.povray.org>
Jim Holsenback <nom### [at] nomailcom> wrote:
> On 01/05/2012 08:44 AM, Scott wrote:
> > This should be easy, but I'm having some trouble.
> >
> > I would like to read a series of vectors<x,y,z>  from a text file and plot the
> > points as spheres.  To test things out, I made a text file with 2 vectors
> > separated by commas.
> >
> > I get this error message.  Can anyone tell me what I am doing wrong?
> >
> > //-------------------------------------------
> > #fopen MyFile "myData.txt" read
> > # local i = 1;
> >    #while (i<2)
> >    #read (MyFile,Vector[i]
> > Parse Error: Expected 'undeclared identifier', empty array found instead
> > //---------------------------------------------
>
> have a look at the following if you've not done that already:
> http://wiki.povray.org/content/Documentation:Reference_Section_2.3#Array_Identifiers
>
> should be #local i = 0; ????

Thanks.  However, I had previously checked out that section of the POV
reference.  I did initially have the #local i = 0, but that did not help.


Post a reply to this message

From: Thomas de Groot
Subject: Re: Reading from a text file
Date: 5 Jan 2012 10:18:50
Message: <4f05bf5a$1@news.povray.org>
You may also consider using:

#wile (defined(filename))
...
#end

Thomas


Post a reply to this message

From: Bill Pragnell
Subject: Re: Reading from a text file
Date: 5 Jan 2012 10:45:00
Message: <web.4f05c4c58e1c2e696dd25f0b0@news.povray.org>
The #read section in the docs says that a vector identifier used in this way
should be predeclared. You've declared the array, but not initialised the
element you're trying to read into. Try:

#fopen MyFile "myData.txt" read
#local i = 1;
#while (i<2)
  #local Vector[i] = <0,0,0>;
  #read (MyFile,Vector[i])
  #local i = i + 1;
#end

Also note that arrays are zero-based in index, and your code will only attempt
to read 1 vector from the file. You should set i = 0 instead, unless I have
misread your intentions!

Bill


Post a reply to this message

From: Thomas de Groot
Subject: Re: Reading from a text file
Date: 5 Jan 2012 10:52:22
Message: <4f05c736$1@news.povray.org>
It seems to me that arrays are unduly complicated in use here. 
Personally, I would prefer the following:

#fopen MyFile "myData.txt" read
#while (defined(MyFile))
   #read (MyFile,Vector)
#end

see: 3.2.2.3.3 The #read Directive

Thomas


Post a reply to this message

From: Scott
Subject: Re: Reading from a text file
Date: 5 Jan 2012 14:15:01
Message: <web.4f05f5b48e1c2e69be8719c80@news.povray.org>
Thank you both Thomas and Bill for your comments.

I came up with 2 versions that work.  One defines an array for the points, and
the other seems to work just as well without it.

Here are the 2 versions.

1)  No array

#fopen MyFile "Set_of_PointsXYZ.txt" read

#local i = 0;
#while (defined(MyFile))
     #read (MyFile,Vector)
      sphere { Vector,    0.1
      texture {
      pigment{ rgb <1,0,0>}
      finish { Substance }}}
      #local i = i + 1;
  #end

2) Using an array

#declare Vector = array[100000];
#fopen MyFile  "Set_of_PointsXYZ.txt"   read
#local i = 0;
#while (defined(MyFile))
    #local Vector [i]  =  <0,0,0>;
    #read (MyFile,Vector[i])
        sphere { Vector[i],    0.1
          texture {
          pigment{ rgb <1,0,0>}
          finish { Substance }}}

Again, thanks so much for your assistance.


Post a reply to this message

From: Charles C
Subject: Re: Reading from a text file
Date: 5 Jan 2012 15:35:01
Message: <web.4f06090f8e1c2e69cac4259f0@news.povray.org>
It looks like for your no-array option, you don't need "i" either.
Charles


Post a reply to this message

From: Alain
Subject: Re: Reading from a text file
Date: 5 Jan 2012 16:26:42
Message: <4f061592@news.povray.org>

> Thank you both Thomas and Bill for your comments.
>
> I came up with 2 versions that work.  One defines an array for the points, and
> the other seems to work just as well without it.
>
> Here are the 2 versions.
>
> 1)  No array
>
> #fopen MyFile "Set_of_PointsXYZ.txt" read
>
> #local i = 0;
> #while (defined(MyFile))
>       #read (MyFile,Vector)
>        sphere { Vector,    0.1
>        texture {
>        pigment{ rgb<1,0,0>}
>        finish { Substance }}}
>        #local i = i + 1;
>    #end
>
> 2) Using an array
>
> #declare Vector = array[100000];
> #fopen MyFile  "Set_of_PointsXYZ.txt"   read
> #local i = 0;
> #while (defined(MyFile))
>      #local Vector [i]  =<0,0,0>;
>      #read (MyFile,Vector[i])
>          sphere { Vector[i],    0.1
>            texture {
>            pigment{ rgb<1,0,0>}
>            finish { Substance }}}
>
> Again, thanks so much for your assistance.
>
>

Unless you plan having each spheres painted with different textures, you 
should use an union and aply the texture to the union as a whole.

Using your non-array solution, it gives this:
#fopen MyFile "Set_of_PointsXYZ.txt" read

#declare DotedSphere=
union{
  #while (defined(MyFile))
      #read (MyFile,Vector)
       sphere { Vector,    0.1}
   #end
  texture{pigment{rgb<1,0,0>}finish{Substance}}
}

Faster to parse as it uses less statements and takes less place in 
memory. If you have 100000 points or more, the memory requirement can 
become an isue.
It may also be faster to render.
You can #declare the union and use it in more than one place.



Alain


Post a reply to this message

From: Thomas de Groot
Subject: Re: Reading from a text file
Date: 6 Jan 2012 02:55:16
Message: <4f06a8e4$1@news.povray.org>
In the no array version, there is no need for i. Defined takes care of 
it all. What Alain says is important. It is a technique that I use for 
all of my scenes with a write/read code. And you can make the union 
and/or the while loop as complex as you want of course.

Thomas


Post a reply to this message

Goto Latest 10 Messages Next 4 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.