POV-Ray : Newsgroups : povray.advanced-users : Array looped-initialization syntax Server Time
29 Mar 2024 12:02:18 EDT (-0400)
  Array looped-initialization syntax (Message 1 to 3 of 3)  
From: Bald Eagle
Subject: Array looped-initialization syntax
Date: 16 Aug 2016 12:50:01
Message: <web.57b343347d9aaf09b488d9aa0@news.povray.org>
I'm having some difficulty embedding a loop into an array declaration.
Is this not allowed?

Do I have to declare the array and then initialize it afterward with #declare
ExpandedArray [LoopVariable] = SomeValue;  ?


I'm also curious about how folks who code this type of thing handle the
requirement for a comma, since the LAST element of the array doesn't have one.
Is there internal POV-Ray Black Magic that handles this?

#####################################################################


#declare EightHedron = array [6] {
<1, 0, 0>, <-1, 0, 0>, <0, 1, 0>, <0, -1, 0>, <0, 0, 1>, <0, 0, -1>
}

#declare BasisArray = array [8][3] {
{EightHedron [0], EightHedron [4], EightHedron [2]},
{EightHedron [2], EightHedron [4], EightHedron [1]},
{EightHedron [1], EightHedron [1], EightHedron [4]},
{EightHedron [3], EightHedron [4], EightHedron [0]},
{EightHedron [0], EightHedron [2], EightHedron [5]},
{EightHedron [2], EightHedron [1], EightHedron [5]},
{EightHedron [1], EightHedron [3], EightHedron [5]},
{EightHedron [2], EightHedron [0], EightHedron [5]}
} //end BasisArray

#declare Octahedron = union {
#for (Side, 0, 7)
    triangle {
    #for (Vertex, 0, 2)
        BasisArray[Side][Vertex]
    #end
    }
#end // end for Side
pigment {Red}
}

//   **** THIS IS WHERE THE PARSER WANTS A { before the #, but issues an
"insufficient number of initializers" when it's placed before the #for

#declare BasisNumber = dimension_size (BasisArray, 1);
#declare ExpandedArray = array [4 * BasisNumber][3] {
    #for (BasisTriangle, 0, BasisNumber-1)
        {BasisArray[BasisTriangle][0],
(BasisArray[BasisTriangle][0]+BasisArray[BasisTriangle][1])/2,
(BasisArray[BasisTriangle][0]+BasisArray[BasisTriangle][2])/2},
        {BasisArray[BasisTriangle][1],
(BasisArray[BasisTriangle][1]+BasisArray[BasisTriangle][0])/2,
(BasisArray[BasisTriangle][1]+BasisArray[BasisTriangle][2])/2},
        {BasisArray[BasisTriangle][2],
(BasisArray[BasisTriangle][2]+BasisArray[BasisTriangle][0])/2,
(BasisArray[BasisTriangle][2]+BasisArray[BasisTriangle][1])/2},
        {(BasisArray[BasisTriangle][0]+BasisArray[BasisTriangle][1])/2,
(BasisArray[BasisTriangle][1]+BasisArray[BasisTriangle][2])/2,
(BasisArray[BasisTriangle][2]+BasisArray[BasisTriangle][0])/2}
    #end // end for BasisTriangle
}
#declare ExpandedNumber = dimension_size (ExpandedArray, 1);

#declare ExpandedOctahedron = union {
#for (Side, 0, ExpandedNumber-1)
    triangle {
    #for (Vertex, 0, 2)
        ExpandedArray[Side][Vertex]
    #end
    }
#end // end for Side
pigment {Red}
}


Post a reply to this message

From: Le Forgeron
Subject: Re: Array looped-initialization syntax
Date: 16 Aug 2016 12:56:52
Message: <57b345d4$1@news.povray.org>
Le 16/08/2016 à 18:45, Bald Eagle a écrit :
> 
> I'm also curious about how folks who code this type of thing handle the
> requirement for a comma, since the LAST element of the array doesn't have one.
> Is there internal POV-Ray Black Magic that handles this?

The parser of comma is very lenient. You can omit the comma, as long as the next value
is not interpreted as a continuation of an expression.

For instance
"A , A" can be written "A A", but if A looks like "-B", or "+C" you are in troubles
and doomed.

And remember #for takes 4 parameters.
1. Variable name
2. Start value
3. End value
4. Step between each value


Post a reply to this message

From: clipka
Subject: Re: Array looped-initialization syntax
Date: 16 Aug 2016 13:25:32
Message: <57b34c8c@news.povray.org>
Am 16.08.2016 um 18:45 schrieb Bald Eagle:
> I'm having some difficulty embedding a loop into an array declaration.
> Is this not allowed?

No; POV-Ray does not support any other directives (the things that begin
with a "#") inside a "#declare" statement.

> Do I have to declare the array and then initialize it afterward with #declare
> ExpandedArray [LoopVariable] = SomeValue;  ?

Yup.

> I'm also curious about how folks who code this type of thing handle the
> requirement for a comma, since the LAST element of the array doesn't have one.
> Is there internal POV-Ray Black Magic that handles this?

POV-Ray does allow a trailing comma after the last element. But
obviously that's of no help to you.


Post a reply to this message

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