POV-Ray : Newsgroups : povray.general : Reading from a text file Server Time
29 Jul 2024 14:20:36 EDT (-0400)
  Reading from a text file (Message 5 to 14 of 14)  
<<< Previous 4 Messages Goto Initial 10 Messages
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

From: Bill Pragnell
Subject: Re: Reading from a text file
Date: 6 Jan 2012 06:15:00
Message: <web.4f06d7338e1c2e696dd25f0b0@news.povray.org>
Thomas de Groot <tenDOTlnDOTretniATtoorgedDOTt> wrote:
> It seems to me that arrays are unduly complicated in use here.

Indeed. Although an array would be useful if the positions were to be used
repeatedly. I've had to use #read and arrays like this before.

Bill


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Reading from a text file
Date: 6 Jan 2012 09:20:25
Message: <4f070329@news.povray.org>
Thomas de Groot wrote:
> 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

but for this you need a way to specify that a vector
should be read instead of a scalar, either by introducing
typed variables or requiring additional parameters.


Post a reply to this message

From: Thomas de Groot
Subject: Re: Reading from a text file
Date: 6 Jan 2012 10:19:13
Message: <4f0710f1$1@news.povray.org>
On 6-1-2012 15:20, Christian Froeschlin wrote:
> Thomas de Groot wrote:
>> 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
>
> but for this you need a way to specify that a vector
> should be read instead of a scalar, either by introducing
> typed variables or requiring additional parameters.
I cannot pretend to know exactly what you mean :-) but in my simple 
vision and use of this, I write and then read a vector (or a float or 
whatever, e.g. text) according to my needs. You only have to take care 
that you use a vector as such or a text as such and not make a 
hotchpotch of things. #read reads and interprets what is between the 
commas: if this is a vector, it reads a vector, etc. doesn't it?

Thomas


Post a reply to this message

From: Scott
Subject: Re: Reading from a text file
Date: 9 Jan 2012 12:40:01
Message: <web.4f0b25c68e1c2e69be8719c80@news.povray.org>
Just wanted to thank Alain and Thomas for those additional comments.

It is very helpful to get this feedback.  It is a great resource.

Thanks so much for taking the time to contribute to this discussion.

scott


Post a reply to this message

<<< Previous 4 Messages Goto Initial 10 Messages

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