POV-Ray : Newsgroups : povray.general : Is it possible to convert a text string to a Macro Identifier ??? Server Time
31 Oct 2024 14:10:03 EDT (-0400)
  Is it possible to convert a text string to a Macro Identifier ??? (Message 1 to 8 of 8)  
From: Mike E 
Subject: Is it possible to convert a text string to a Macro Identifier ???
Date: 15 Aug 2005 09:45:00
Message: <web.43009badb594920d0a6eb5d0@news.povray.org>
I am trying to read a text file and call macros according to the names in
the file WITHOUT using a look up table with #if #else or #switch etc.
eg:

assume the text file is: "ROTATE_LEFT","ROTATE_RIGHT",..

#macro ROTATE_LEFT()
blah blah
#end

#macro ROTATE_RIGHT()
blah blah
#end

#read (MyFile,NAME)

NAME() // call the macro ROTATE_LEFT()

- I know this is illegal but this demonstrates what I would like to do, is
there some way I can convert the text string directly to a macro
identifier?


Post a reply to this message

From: Warp
Subject: Re: Is it possible to convert a text string to a Macro Identifier ???
Date: 15 Aug 2005 10:47:43
Message: <4300ab0f@news.povray.org>
You can create a pov file in your macro (using #fopen, #write, etc) and
then #include that file.

-- 

                                                          - Warp


Post a reply to this message

From: Jim Charter
Subject: Re: Is it possible to convert a text string to a Macro Identifier ???
Date: 15 Aug 2005 11:05:29
Message: <4300af39$1@news.povray.org>
Mike E. wrote:
> I am trying to read a text file and call macros according to the names in
> the file WITHOUT using a look up table with #if #else or #switch etc.
> eg:
> 
> assume the text file is: "ROTATE_LEFT","ROTATE_RIGHT",..
> 
> #macro ROTATE_LEFT()
> blah blah
> #end
> 
> #macro ROTATE_RIGHT()
> blah blah
> #end
> 
> #read (MyFile,NAME)
> 
> NAME() // call the macro ROTATE_LEFT()
> 
> - I know this is illegal but this demonstrates what I would like to do, is
> there some way I can convert the text string directly to a macro
> identifier?
> 
> 
There is a macro, Parse_String(String) in the strings.inc file that can 
sometimes help to get the behavior you want.  I am not sure if it is 
relevant to your particular need here but in general it offers a way to 
have a string that you have composed dynamically somehow, appear to the 
parser just as if you had typed it in the source file.  My language here 
  may lack technical exactitude but for instance here is a snippet of 
code I am currently using:

#local I=0;#while(I<Grain)

         #local SplineDef =
	#concat ( "#declare Length_Spline_", str(I,0,0), " = " );				
         Parse_String(SplineDef) 

         spline { ....

#local I=I+1;#end


Post a reply to this message

From: Jim Charter
Subject: Re: Is it possible to convert a text string to a Macro Identifier???
Date: 15 Aug 2005 11:59:53
Message: <4300bbf9$1@news.povray.org>
This seems to work:

#include "strings.inc"
#include "colors.inc"


#macro Lights ()
         light_source { <100,1000,-1000>, White}
#end

#macro Camera ()
         camera { location <0,1,-16>  look_at <0,0,0>}
#end

#macro Action ()
         sphere { 0, .5 pigment { Red } }
#end

#fopen MyFile "FILEIO5.TXT" write 

#write (MyFile,"\"Lights\",\"Camera\",\"Action\"")
#fclose MyFile

#fopen MyFile "FILEIO5.TXT" read

#read (MyFile,Name1,Name2,Name3)

Parse_String (Name1) ()
Parse_String (Name2) ()
Parse_String (Name3) ()

#fclose MyFile


Post a reply to this message

From: Mike E 
Subject: Re: Is it possible to convert a text string to a Macro Identifier???
Date: 15 Aug 2005 21:40:01
Message: <web.430142f1b67b12baedf9e5ca0@news.povray.org>
Jim Charter <jrc### [at] msncom> wrote:
> This seems to work:
>
> #include "strings.inc"
> #include "colors.inc"
>
>
> #macro Lights ()
>          light_source { <100,1000,-1000>, White}
> #end
>
> #macro Camera ()
>          camera { location <0,1,-16>  look_at <0,0,0>}
> #end
>
> #macro Action ()
>          sphere { 0, .5 pigment { Red } }
> #end
>
> #fopen MyFile "FILEIO5.TXT" write
>
> #write (MyFile,""Lights","Camera","Action"")
> #fclose MyFile
>
> #fopen MyFile "FILEIO5.TXT" read
>
> #read (MyFile,Name1,Name2,Name3)
>
> Parse_String (Name1) ()
> Parse_String (Name2) ()
> Parse_String (Name3) ()
>
> #fclose MyFile



Absolutely awesome response guys! The Parse_String command does exactly what
I'm after. I wish I'd asked this question a year ago. My POV code will
never be the same again.

Thanks Much


Post a reply to this message

From: Slime
Subject: Re: Is it possible to convert a text string to a Macro Identifier???
Date: 15 Aug 2005 22:14:47
Message: <43014c17$1@news.povray.org>
> Absolutely awesome response guys! The Parse_String command does exactly
what
> I'm after. I wish I'd asked this question a year ago. My POV code will
> never be the same again.

Parse_String, at least in this case, is sort of a hack that solves an
obscure problem (caused by an input file that I'm assuming you have no
control over). Don't rely on it all the time because there are generally
better ways to do things.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Mike E 
Subject: Re: Is it possible to convert a text string to a Macro Identifier???
Date: 16 Aug 2005 07:05:02
Message: <web.4301c5b4b67b12bad0a6eb5d0@news.povray.org>
"Slime" <fak### [at] emailaddress> wrote:


> Parse_String, at least in this case, is sort of a hack that solves an
> obscure problem (caused by an input file that I'm assuming you have no
> control over). Don't rely on it all the time because there are generally
> better ways to do things.
>
>  - Slime
>  [ http://www.slimeland.com/ ]


Hmm. Interesting response Mr Slime. It is an input file that I have some
control over from another pgm. But I find the notion of generating commands
which are dependent on external files (or even just text strings) very
useful in many cases,  not just this one - whether I have control over them
or not.
This is mainly to avoid long winded code using things like strcmp() or
switch statements to generate an action based on a large no of variable
data input.
Would you care to expand on your ideas about better ways to do things?

M.E.


Post a reply to this message

From: PM 2Ring
Subject: Re: Is it possible to convert a text string to a Macro Identifier???
Date: 20 Aug 2005 17:00:00
Message: <web.43079833b67b12baf405c6350@news.povray.org>
"Mike E." <nomail@nomail> wrote:
> "Slime" <fak### [at] emailaddress> wrote:
>
>
> > Parse_String, at least in this case, is sort of a hack that solves an
> > obscure problem (caused by an input file that I'm assuming you have no
> > control over). Don't rely on it all the time because there are generally
> > better ways to do things.
> >
> >  - Slime
> >  [ http://www.slimeland.com/ ]
>
>
> Hmm. Interesting response Mr Slime. It is an input file that I have some
> control over from another pgm. But I find the notion of generating commands
> which are dependent on external files (or even just text strings) very
> useful in many cases,  not just this one - whether I have control over them
> or not.
> This is mainly to avoid long winded code using things like strcmp() or
> switch statements to generate an action based on a large no of variable
> data input.
> Would you care to expand on your ideas about better ways to do things?
>
> M.E.

Have a look at the source for Parse_String():

#macro Parse_String(String)
    #fopen FOut "parse_string.tmp" write
    #write(FOut,String)
    #fclose FOut
    #include "parse_string.tmp"
#end

As you can see, lots of Parse_String() calls in a scene will have lots of
overhead with all that file handling. If you want to do this sort of thing,
its best if you can avoid all that file opening & closing.

For example:

// Persistence of Vision Ray Tracer Scene Description File
// File: Boxes.pov
// Vers: 3.5
// Desc: Show T_Stone textures on numbered superellipsoids in ZX plane
// Date: 24 Apr 2004
// Auth: PM 2Ring
//

global_settings {
  assumed_gamma  1.0
}

#include "colors.inc"
// T_Stone1 - T_Stone44
#include "stones.inc"

camera {
  location  <0.0, 0, -380>
  direction 10*z
  look_at   0
  right     x*image_width/image_height up y
}

light_source { <0, 500, -2000> *2  White }

#declare MI=6;
#declare MJ=7;
#declare MK=44;

//Let's build our own include file,
//since we can't manipulate identifiers directly
#declare FileName = "mydata.inc";
#debug concat("\n\nBuilding include file, ", FileName, "...\n")

#fopen MyFile FileName write
#write (MyFile, "#declare Tex = array[", str(MK,0,0), "]{\n")

#declare K=1;
#while (K<=MK)
  #write (MyFile, concat("  T_Stone", str(K,0,0), ",\n"))
  #declare K=K+1;
#end

#write (MyFile, "}")
#fclose MyFile

#include FileName
#debug "File I/O complete.\n"

#declare Scale = <3, 2, 1>;
#macro Make_SBlock(Str)
union{
  difference{
    superellipsoid {
      <0.4, 0.4>
      scale Scale
    }

    superellipsoid {
      <0.1, 0.1>
      scale <2.5, 1.5, .5>
      translate -1.25*z
    }
  }

  text {
    ttf "timrom.ttf" Str .975, 0
    scale 3
    translate <-1.25, -1.125, -.975>
  }
}
#end

#declare Boxes =
union{
  #declare K=0;
  #declare I=0;
  #while (I<MI)
    #declare J=0;
    #while (J<MJ)
      object{Make_SBlock(str(K+1,2,0))
        translate <2*J-MJ+1, 2*I-MI+1, 0> * (Scale * 1.15 * <1, 1.2, 1>)
        texture{Tex[K] scale Scale}
      }
      #declare J=J+1;
      #declare K=mod(K+1, MK);
    #end
    #declare I=I+1;
  #end
}

object{
  Boxes
}

background{rgb .5}

//-------------------------------------------------------------


Post a reply to this message

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