POV-Ray : Newsgroups : povray.general : supplying parameters dynamically Server Time
5 Nov 2024 07:20:22 EST (-0500)
  supplying parameters dynamically (Message 1 to 5 of 5)  
From: stevenvh
Subject: supplying parameters dynamically
Date: 4 Feb 2009 02:10:00
Message: <web.49893e4a14f123fac0721a1d0@news.povray.org>
In my code I want to generate a series of prisms dynamically, where both the
number of points and their coordinates are read from a file. I have no idea how
to do this, so any hints be greatly appreciated.
Regards,
Steven


Post a reply to this message

From: stbenge
Subject: Re: supplying parameters dynamically
Date: 4 Feb 2009 02:56:57
Message: <49894a49$1@news.povray.org>
stevenvh wrote:
> In my code I want to generate a series of prisms dynamically, where both the
> number of points and their coordinates are read from a file. I have no idea how
> to do this, so any hints be greatly appreciated.
> Regards,
> Steven

You could have an include file which contains an array:

#declare pn = pi*2/6;

#declare points=
array[7]{
  <sin(pn*0),cos(pn*0)>,
  <sin(pn*1),cos(pn*1)>,
  <sin(pn*2),cos(pn*2)>,
  <sin(pn*3),cos(pn*3)>,
  <sin(pn*4),cos(pn*4)>,
  <sin(pn*5),cos(pn*5)>,
  <sin(pn*0),cos(pn*0)>
}

Then you can #include the file into your scene and write something like 
this:

prism{
  -.5,.5,dimension_size(points,1)
  #declare V=0;
  #while(V<dimension_size(points,1))
   points[V]
   #declare V=V+1;
  #end
  pigment{rgb 1}
}

You'll probably want to generate the include file automatically. You 
should check into #fopen, #write and #fclose (3.2.2.3  File I/O 
Directives). You shouldn't have to #read a file to accomplish your goal. 
I hope this helps.

Sam


Post a reply to this message

From: Chris B
Subject: Re: supplying parameters dynamically
Date: 4 Feb 2009 06:38:10
Message: <49897e22$1@news.povray.org>
"stevenvh" <nomail@nomail> wrote in message 
news:web.49893e4a14f123fac0721a1d0@news.povray.org...
> In my code I want to generate a series of prisms dynamically, where both 
> the
> number of points and their coordinates are read from a file. I have no 
> idea how
> to do this, so any hints be greatly appreciated.
> Regards,
> Steven

Or, if you want to #read straight from an existing data file that is in csv 
format you can do that too. The only tricky bit with mixed data types (point 
counts and coordinates) is making sure you don't get out of step when trying 
to read the file. The end of the file needs to be quite tidy too (e.g. no 
trailing comma on the last line), otherwise the end-of-file may not be 
detected when it needs to be detected.

If you have a csv file containing:

7,
<-1  ,0>,
<-0.5,0>,
< 0  ,1>,
<-0.5,2>,
<-1  ,2>,
<-1.5,1>,
<-1  ,0>,
7,
< 1  ,0>,
< 0.5,0>,
< 0  ,1>,
< 0.5,2>,
< 1  ,2>,
< 1.5,1>,
< 1  ,0>

You can read this in and use it to draw prisms as follows:

camera {location <0,3,-2> look_at <0,1,1>}
light_source {<-10,20,50>, rgb 1}
// Randomisation seed is just for the color
#declare S = seed(3);

#fopen MyFile "datafile.csv" read
#while (defined(MyFile))
  #read(MyFile,PointCount)
  prism{
    0,1,PointCount
    #declare I=0;
    #while(I<PointCount)
      #undef ThisPoint
      #read(MyFile,ThisPoint)
      // Add this point into the prism:
      ThisPoint
      #declare I=I+1;
    #end
    pigment{rgb <rand(S),rand(S),rand(S)>}
  }
#end

This uses two nested #while loops. The first reads the file till there's no 
more values to read. Inside the first loop we read a value which needs to be 
a whole number of points, then the second #while loop reads each point (uv 
vector) in turn and uses it in the prism definition. When the first prism is 
finished the outer loop looks for the next point count, and so on, until the 
end of the file. You can add as many sets of points to the file as you need.

Regards,
Chris B.


Post a reply to this message

From: stevenvh
Subject: Re: supplying parameters dynamically
Date: 5 Feb 2009 01:45:00
Message: <web.498a8a84c849518bc0721a1d0@news.povray.org>
I am an idiot, that I couldn't think of something this obvious.
But to my defence I must say I'm more used to classic programming (mostly
Delphi), where it's not common that a program generates on-the-fly executable
code for itself. :-)
Thanks for the replies,
Steven


Post a reply to this message

From: Chris B
Subject: Re: supplying parameters dynamically
Date: 5 Feb 2009 04:20:44
Message: <498aaf6c$1@news.povray.org>
"stevenvh" <nomail@nomail> wrote in message 
news:web.498a8a84c849518bc0721a1d0@news.povray.org...
>I am an idiot, that I couldn't think of something this obvious.

Not at all. I've been doing this for years. When I started there's no way 
I'd have gotten straight to this solution.

Also there are alternative, more conventional approaches that separate out 
the language directives and the object definition as much as possible (e.g. 
using arrays and macros etc. to split the #while loop out from the prism 
definition). These can prove advantageous and tidier if you start to develop 
more complex ways of using your data. I just took the lazy route and went 
for an approach that seemed easy for me to explain :-).

Regards,
Chris B.


Post a reply to this message

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