POV-Ray : Newsgroups : povray.general : Automatically named identifiers Server Time
23 Nov 2024 22:32:07 EST (-0500)
  Automatically named identifiers (Message 1 to 10 of 13)  
Goto Latest 10 Messages Next 3 Messages >>>
From: Thomas Fester
Subject: Automatically named identifiers
Date: 4 Nov 2024 17:55:00
Message: <web.6729503a3c415faa6c6071e48a2a825f@news.povray.org>
Hello everybody: I have got a (seemingly) simple programing question: Suppose I
I want to declare a number of splines in a loop-process with one spline per
loop. The splines should have identifiers like SplineXA, SplineXB, SplineXC and
so on. Is there a way to automatically generate such names within the loops? At
the moment I am only able to explicitely generate every identifier on its own.
To make it more clear: Is it possible to have a loop for:

#declare SplineXA  = spline {
    cubic_spline
....
}

#declare SplineXB  = spline {
    cubic_spline
....
}

#declare SplineXC  = spline {
    cubic_spline
....
}

#declare SplineXD  = spline {
    cubic_spline
....
}

....

Best regards and thanks in advance!

Thomas


Post a reply to this message

From: Kenneth
Subject: Re: Automatically named identifiers
Date: 4 Nov 2024 21:50:00
Message: <web.672986e6523ee18491c33a706e066e29@news.povray.org>
Off the top of my head, I would say that it would be easier (less coding) to use
numbers-- like 1, 2, 3 etc-- rather than letters A, B, C. Using letters would
involve generating 'strings' (i.e., text) whereas numbers are much easier to
program with. My opinion ;-)

Even then, I am not sure that #declare statements themselves can be
'auto-generated' the way you mention (only because I don't remember if I have
ever tried that myself.) My own experience is with auto-creating the variable
*after* the = sign.

Others here may prove me wrong, of course!


Post a reply to this message

From: kurtz le pirate
Subject: Re: Automatically named identifiers
Date: 5 Nov 2024 05:24:42
Message: <6729f26a$1@news.povray.org>
On 04/11/2024 23:52, Thomas Fester wrote:
> Hello everybody: I have got a (seemingly) simple programing question: Suppose I
> I want to declare a number of splines in a loop-process with one spline per
> loop. The splines should have identifiers like SplineXA, SplineXB, SplineXC and
> so on. Is there a way to automatically generate such names within the loops? At
> the moment I am only able to explicitely generate every identifier on its own.

To my knowledge, it is not possible to generate variables on the fly.

One workaround may be to generate an “inc” file and then use this same.

For example :
-----------------------------------------------------------------------
...
...
// -----------------------
// --- file generation ---
// -----------------------
#declare alea = seed(2753);

// name of include file containing splines
#declare FileName = "Splines.inc";
// opening the file
#fopen FILE FileName write


// five splines in this example
#declare Nb = 5;
// i : spline index
#declare i = 0;
// loop for splines
#while ( i < Nb )
	// build declaration string for named splines
	#declare Name = concat("#declare SplineX",chr(i+65)," = spline 
{\n\tcubic_spline\n")
	// write it to the file
	#write (FILE, Name)
	// --- fake spline generation ---
	// random number of points in spline
	#declare SplinePoint = 2+int(12*rand(alea));
	// calculates increment to go from zero to one
	#declare inc = 1/(SplinePoint-1);
	// j : points in spline index
	#declare j = 0;
	// loop for points in each spline
	#while ( j < SplinePoint )
		// random 3d point
		#declare point = concat("< ",str(rand(alea)*20-10,0,2),", 
",str(rand(alea)*20-10,0,2),", ",str(rand(alea)*20-10,0,2),">")
		// 'time' value for the point
		#declare splineItem = concat("\t",str(inc*j,0,2),", ",point,"\n")
		// write to the file
		#write (FILE, splineItem)
		// increment points index
		#declare j = j + 1;
		// to the next point...
	#end
	// write end of spline declaration
	#write (FILE, "\t}\n\n")
	// increment spline index
	#declare i = i + 1;
	// to the next spline
#end
// close the include file
#fclose FILE
#debug "\nEnd of spline generation\n"
...
...
// ----------------------------
// --- use generated spline ---
// ----------------------------
#include FileName

// here you can use SplineXA, SplineXB, ...

-----------------------------------------------------------------------


Hope that help ;)


Post a reply to this message

From: Bald Eagle
Subject: Re: Automatically named identifiers
Date: 5 Nov 2024 08:05:00
Message: <web.672a17d8523ee1846563700825979125@news.povray.org>
"Thomas Fester" <tfe### [at] scivitde> wrote:
> Hello everybody: I have got a (seemingly) simple programing question: Suppose I
> I want to declare a number of splines in a loop-process with one spline per
> loop. The splines should have identifiers like SplineXA, SplineXB, SplineXC and
> so on. Is there a way to automatically generate such names within the loops?

You're going to have to use parse_string to make the variable name in each
iteration.
Concat "SplineX" with the char corresponding to the letter you want.

I'm hoping there are some examples if you search for them.

- BW


Post a reply to this message

From: Cousin Ricky
Subject: Re: Automatically named identifiers
Date: 5 Nov 2024 08:55:07
Message: <672a23bb$1@news.povray.org>
On 2024-11-04 18:52 (-4), Thomas Fester wrote:
> Hello everybody: I have got a (seemingly) simple programing question: Suppose I
> I want to declare a number of splines in a loop-process with one spline per
> loop. The splines should have identifiers like SplineXA, SplineXB, SplineXC and
> so on. Is there a way to automatically generate such names within the loops? At
> the moment I am only able to explicitely generate every identifier on its own.
> To make it more clear: Is it possible to have a loop for:

Would an array of splines serve your purpose?

#for (I, 0, 3)
  #declare SplineXs[I] = spline {
    cubic_spline
    ...
  }
#end


Post a reply to this message

From: Kenneth
Subject: Re: Automatically named identifiers
Date: 5 Nov 2024 12:15:00
Message: <web.672a5258523ee18491c33a706e066e29@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
>
> You're going to have to use parse_string to make the variable name in each
> iteration.
> Concat "SplineX" with the char corresponding to the letter you want.
>

My own initial (naive) approach to this would be to try and use the Parse_String
macro to the *left* of the = sign, in a #declare. (The macro -- in "strings.inc"
-- takes a string like "MY_VALUE" or "1.5", writes it to an external .tmp file
but without the double quotes, then immediately returns it to the scene.)

So a *simple* example of such a construct would seem to be...

#include "strings.inc"
#declare Parse_String("MY_VALUE")= 27;

I would naively expect this to be the equivalent of...
#declare MY_VALUE = 27;

but it doesn't work: ""Expected 'undeclared identifier', macro identifier found
instead."

I tried other iterations of this and they all fail.

Maybe there are other (more complex?) ways of using Parse_String to accomplish
what the OP wants-- *as* the #declared variable names-- but I don't have a clue
as to how.


Post a reply to this message

From: Leroy
Subject: Re: Automatically named identifiers
Date: 5 Nov 2024 16:20:00
Message: <web.672a8b41523ee184a196c5bf712fc00@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> "Bald Eagle" <cre### [at] netscapenet> wrote:
> >
> > You're going to have to use parse_string to make the variable name in each
> > iteration.
> > Concat "SplineX" with the char corresponding to the letter you want.
> >
>
> My own initial (naive) approach to this would be to try and use the Parse_String
> macro to the *left* of the = sign, in a #declare. (The macro -- in "strings.inc"
> -- takes a string like "MY_VALUE" or "1.5", writes it to an external .tmp file
> but without the double quotes, then immediately returns it to the scene.)
>
> So a *simple* example of such a construct would seem to be...
>
> #include "strings.inc"
> #declare Parse_String("MY_VALUE")= 27;
>
> I would naively expect this to be the equivalent of...
> #declare MY_VALUE = 27;
>
> but it doesn't work: ""Expected 'undeclared identifier', macro identifier found
> instead."
>
> I tried other iterations of this and they all fail.
>
> Maybe there are other (more complex?) ways of using Parse_String to accomplish
> what the OP wants-- *as* the #declared variable names-- but I don't have a clue
> as to how.

#include "strings.inc"
Parse_String("#declare I")=4;
#debug concat("I =",str(I,0,0),"\n")

works fine  on win3.7 version

Parse_String("#declare I=4;") also works ok
Have Fun!


Post a reply to this message

From: Kenneth
Subject: Re: Automatically named identifiers
Date: 6 Nov 2024 15:50:00
Message: <web.672bd56a523ee18491c33a706e066e29@news.povray.org>
"Leroy" <whe### [at] gmailcom> wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:

> >
> > Maybe there are other (more complex?) ways of using Parse_String to accomplish
> > what the OP wants-- *as* the #declared variable names-- but I don't have
> > a clue as to how.
>
> #include "strings.inc"
> Parse_String("#declare I")=4;
> #debug concat("I =",str(I,0,0),"\n")
>
> works fine  on win3.7 version
>
> Parse_String("#declare I=4;") also works ok

Aha! So a variable name CAN be auto-generated. Excellent. (These also work
successfully in v3.8 beta 1.) I didn't think to try your iterations earlier; I
was, uh, rushed for time yesterday :-[  (Well, that's my excuse anyway, ha)

Unfortunately, even these would not solve Warren's initial query of
auto-creating *multiple* #declares, at least not efficiently.

Here's a #for-loop example that might *seem* to be a way to do it. Even though
it works code-wise as SDL, the result is not what might be expected:

[I used simple vectors here instead of Warren's splines]
#declare My_Vector_Array=array[3] // 'bins' 0 to 2 inclusive
#declare My_Vector_Array[0]= <.1,.3,.6>;
#declare My_Vector_Array[1]= <.2,.4,.7>;
#declare My_Vector_Array[2]= <.3,.5,.8>;

#for(i,1,3)
Parse_String(concat("declare MY_VECTOR_",str(i,0,0)," = <",
vstr(3,My_Vector_Array[i-1],", ",0,1),">;"))
#end

This actually creates only ONE #declare, the final one:
#declare MY_VECTOR_3 = <.3,.5,.8>;

The Parse_String macro always #writes its result to a *single* file named
parse_string.tmp  (in Windows anyway); so in the for-loop, each result simply
gets over-written by the next one.

It would be an interesting exercise to try and re-code the macro itself, to see
if it could be made to deal with multiple entries...and to create multiple
written .tmp files as well.


Post a reply to this message

From: Thomas Fester
Subject: Re: Automatically named identifiers
Date: 6 Nov 2024 16:20:00
Message: <web.672bdc61523ee1846c6071e48a2a825f@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> "Leroy" <whe### [at] gmailcom> wrote:
> > "Kenneth" <kdw### [at] gmailcom> wrote:
>
> > >
> > > Maybe there are other (more complex?) ways of using Parse_String to accomplish
> > > what the OP wants-- *as* the #declared variable names-- but I don't have
> > > a clue as to how.
> >
> > #include "strings.inc"
> > Parse_String("#declare I")=4;
> > #debug concat("I =",str(I,0,0),"\n")
> >
> > works fine  on win3.7 version
> >
> > Parse_String("#declare I=4;") also works ok
>
> Aha! So a variable name CAN be auto-generated. Excellent. (These also work
> successfully in v3.8 beta 1.) I didn't think to try your iterations earlier; I
> was, uh, rushed for time yesterday :-[  (Well, that's my excuse anyway, ha)
>
> Unfortunately, even these would not solve Warren's initial query of
> auto-creating *multiple* #declares, at least not efficiently.
>
> Here's a #for-loop example that might *seem* to be a way to do it. Even though
> it works code-wise as SDL, the result is not what might be expected:
>
> [I used simple vectors here instead of Warren's splines]
> #declare My_Vector_Array=array[3] // 'bins' 0 to 2 inclusive
> #declare My_Vector_Array[0]= <.1,.3,.6>;
> #declare My_Vector_Array[1]= <.2,.4,.7>;
> #declare My_Vector_Array[2]= <.3,.5,.8>;
>
> #for(i,1,3)
> Parse_String(concat("declare MY_VECTOR_",str(i,0,0)," = <",
> vstr(3,My_Vector_Array[i-1],", ",0,1),">;"))
> #end
>
> This actually creates only ONE #declare, the final one:
> #declare MY_VECTOR_3 = <.3,.5,.8>;
>
> The Parse_String macro always #writes its result to a *single* file named
> parse_string.tmp  (in Windows anyway); so in the for-loop, each result simply
> gets over-written by the next one.
>
> It would be an interesting exercise to try and re-code the macro itself, to see
> if it could be made to deal with multiple entries...and to create multiple
> written .tmp files as well.

Wow - I'm thrilled so many answers! Thank you all!


Post a reply to this message

From: kurtz le pirate
Subject: Re: Automatically named identifiers
Date: 7 Nov 2024 04:31:53
Message: <672c8909$1@news.povray.org>
On 06/11/2024 21:45, Kenneth wrote:

> 
> It would be an interesting exercise to try and re-code the macro itself, to see
> if it could be made to deal with multiple entries...and to create multiple
> written .tmp files as well.
> 


Have you looked my method ?



-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

Goto Latest 10 Messages Next 3 Messages >>>

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