POV-Ray : Newsgroups : povray.general : Pass array to macro. Server Time
14 Oct 2024 04:08:54 EDT (-0400)
  Pass array to macro. (Message 1 to 5 of 5)  
From: kurtz le pirate
Subject: Pass array to macro.
Date: 12 Apr 2020 05:12:36
Message: <5e92db84$1@news.povray.org>
Hello,

With this code :

...
#declare ListOfLines = array[nbLines][2];
...
#macro drawLine(thisLine,lRadius,lColor)
   cylinder {
     thisLine[0], thisLine[1], lRadius
     pigment { color lColor }
     finish { specular 1 brilliance 2 }
     }
#end
...
#declare lineIndex=0;
#while(lineIndex<nbLines)
   drawLine(ListOfLines[lineIndex],0.05, GreenYellow) <<< error here !
   #declare lineIndex= lineIndex+1;
#end

The array "ListOfLines" contains start and end points of lines.

ListOfLines[i][0]=<x1,y1,z1>;
ListOfLines[i][2]=<x2,y2,z2>;
and so on


Running this code raise en error :
   Parse Error: Expected '[', ,  found instead
for the line highlighted above


The macro drawLine can't understand that "thisLine" parameter is an 
array of two elements.
So it seems that i can't pass an array as a parameter to a macro ?

Is my prognosis correct and is there any solution (other than split 
array's item into two variables).


Thanks



-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: jr
Subject: Re: Pass array to macro.
Date: 12 Apr 2020 06:15:01
Message: <web.5e92e8fa9cacd3d7827e2b3e0@news.povray.org>
hi,

kurtz le pirate <kur### [at] gmailcom> wrote:
> Hello,
>
> With this code :
>
> ...
> Running this code raise en error :
>    Parse Error: Expected '[', ,  found instead
> for the line highlighted above
>
>
> The macro drawLine can't understand that "thisLine" parameter is an
> array of two elements.
> So it seems that i can't pass an array as a parameter to a macro ?

"Although it is permissible to reference an entire array as a whole, you may not
reference just one dimension of a multi-dimensional array."
<http://wiki.povray.org/content/Reference:Array>

> Is my prognosis correct and is there any solution (other than split
> array's item into two variables).

you could declare an array of arrays, ie
#declare ListOfLines = array [N] {
  array [2] {point, point},
  ...
  array [2] {point, point}
};


regards, jr.


Post a reply to this message

From: Bald Eagle
Subject: Re: Pass array to macro.
Date: 12 Apr 2020 08:15:00
Message: <web.5e9306259cacd3d7fb0b41570@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:

> > Running this code raise en error :
> >    Parse Error: Expected '[', ,  found instead
> > for the line highlighted above

Correct and expected behaviour.

> > The macro drawLine can't understand that "thisLine" parameter is an
> > array of two elements.

It does, which is the problem.  It understands that thisLine is an array of the
dimensions that you specified, and you're supplying too few inices for it to
locate the element[s] that you want.

> > So it seems that i can't pass an array as a parameter to a macro ?

You totally can, I do it all the time.


> > Is my prognosis correct and is there any solution (other than split
> > array's item into two variables).

Hard to understand exactly what you mean, but:
All you do is pass the FULL array into the macro and then define the way you
address the parts of the array that you want to access.

If you only want to access the second dimension, then just hold the first one
constant, but you do have to put that into your ArrayName [index1][index2]
designation.

> you could declare an array of arrays, ie
> #declare ListOfLines = array [N] {
>   array [2] {point, point},
>   ...
>   array [2] {point, point}
> };

This I think would also be a way to avoid the error, and might be better, as you
can then have a parent array that is composed of sub-arrays of _different data
types_.    :)


Post a reply to this message

From: kurtz le pirate
Subject: Re: Pass array to macro.
Date: 14 Apr 2020 06:04:28
Message: <5e958aac$1@news.povray.org>
On 12/04/2020 14:14, Bald Eagle wrote:
> 
> Hard to understand exactly what you mean, but:
> All you do is pass the FULL array into the macro and then define the way you
> address the parts of the array that you want to access.

My ListOfLines = array[nbLines][2] is just and array of N lines and for 
each line, the start point "array[i][0]" and the end point "array[i][1]".

In my mind, passing array[i], allowed me to pass the two parameters 
present in the second dimension.

> If you only want to access the second dimension, then just hold the first one
> constant, but you do have to put that into your ArrayName [index1][index2]
> designation.
>  
>> you could declare an array of arrays, ie
>> #declare ListOfLines = array [N] {
>>    array [2] {point, point},
>>    ...
>>    array [2] {point, point}
>> };

"array of arrays" is not the same as array[][] ?

> This I think would also be a way to avoid the error, and might be better, as you
> can then have a parent array that is composed of sub-arrays of _different data
> types_.:)

For now, the simplest way is to pass two parameters to my macro.
drawLine(ListOfLines[lineIndex][0],ListOfLines[lineIndex][1],0.05, 
GreenYellow)

with :
#macro drawLine(thisLineStart, thisLineEnd, lRadius, lColor)


No more complications. Thankss

-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: Bald Eagle
Subject: Re: Pass array to macro.
Date: 14 Apr 2020 06:40:01
Message: <web.5e95927a9cacd3d7fb0b41570@news.povray.org>
kurtz le pirate <kur### [at] gmailcom> wrote:

> In my mind, passing array[i], allowed me to pass the two parameters
> present in the second dimension.

That's a common misconception - which is why "you can't do that" is in the docs.
 There's only so much code "under the hood", and the code that might allow you
to reliably do that - isn't.


> "array of arrays" is not the same as array[][] ?

It "is", but the point is about content, not form.
Array [][] is just a generic 2D array.
But when you fill the array with data, then it becomes an array of scalars, or
vectors, or ... arrays.



> For now, the simplest way is to pass two parameters to my macro.
> drawLine(ListOfLines[lineIndex][0],ListOfLines[lineIndex][1],0.05,
> GreenYellow)
>
> with :
> #macro drawLine(thisLineStart, thisLineEnd, lRadius, lColor)
>
>
> No more complications. Thankss


Correct - OR you could just hard-code the constant parameter in "drawline" if it
doesn't need to be variable.

Glad you got it working.   :)


Post a reply to this message

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