POV-Ray : Newsgroups : povray.general : array.length()? Server Time
30 Jul 2024 16:20:14 EDT (-0400)
  array.length()? (Message 5 to 14 of 14)  
<<< Previous 4 Messages Goto Initial 10 Messages
From: clipka
Subject: Re: array.length()?
Date: 24 Feb 2009 03:40:00
Message: <web.49a3b21678dc09aea5bc7c50@news.povray.org>
"[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> Is this the best I can hope for? Is there some other way to detect the
> populated length of an array? dimension_size is *useless*. Might it be that
> I am forced to form the array within the loop which uses it? That would
> suck...not very OO...but then SDL isn't very OO to start with, ironically..


Prepared to give yourself a big slap on the head?

Try this (*ta-daaah*):


      -------------------------
       #ifdef (myArray[i]) ...
      -------------------------


So a fail-safe code to loop through an array would look something like this:

#declare i=0;
#while (i < dimension_size(myArray,1))
  #ifdef (myArray[i])
    #debug concat("[", str(i,0,0), "]=", str(myArray[i],0,0), "\n")
    #declare i=i+1;
  #else
    #debug concat("[", str(i,0,0), "] undefined\n")
    #declare i=dimension_size(myArray,1); // force exiting of the loop
  #end
#end

(unfortunately, POV doesn't have a break statement for loops (its #break
statement is only of use in #switch statements), nor does it "short-circuit"
its & operator so #while (i < dimension_size(myArray,1) & defined(myArray[i]))
will not work properly, hence the comparatively ugly way of terminating the
loop)

Or, if your list may contain "holes":

#declare i=0;
#while (i < dimension_size(myArray,1))
  #ifdef (myArray[i])
    #debug concat("[", str(i,0,0), "]=", str(myArray[i],0,0), "\n")
  #else
    #debug concat("[", str(i,0,0), "] undefined\n")
  #end
  #declare i=i+1;
#end


Post a reply to this message

From: Chris B
Subject: Re: array.length()?
Date: 24 Feb 2009 03:44:08
Message: <49a3b358$1@news.povray.org>
"[GDS|Entropy]" <gds### [at] hotmailcom> wrote in message 
news:49a3abc7@news.povray.org...
> Because the array is declared outside of the macro which populates it, to 
> facilitate its passing to other macros as a variable, so always returns 
> whatever you defined its length to be, regardless if it has been populated 
> with that many values.
>
> Pov doesn't support redimensioning, does it?
>

Just to make sure I understand you correctly: You define an array that is, 
say 5000 elements long. You then call a macro to populate some elements of 
that array, but there are gaps. You then need a second macro to loop through 
the array, ignoring any unassigned elements?

If that's right, you just need to use #ifdef to see if an element of the 
array has been assigned before you use it.

If by 'redimensioning' you mean just scrunching up the gaps, then you can do 
that very simply in a loop using #ifdef on an array element. If it exists 
move it to the current 'write' point in the array. Plus you can count the 
actual number of elements uses as you go.

#local I = 0;
#declare NewSize = 0;
#while (I<dimension_size(MyArray,1))
  #ifdef(MyArray[I])
    #declare MyArray[NewSize] = MyArray[I];
    #declare NewSize = NewSize+1;
  #end
  #local I=I+1;
#end

Untested code Warning! I didn't run this, so it may contain syntax errors 
etc.

Regards,
Chris B.


Post a reply to this message

From: [GDS|Entropy]
Subject: Re: array.length()?
Date: 24 Feb 2009 05:39:22
Message: <49a3ce5a$1@news.povray.org>
Thanks guys!
That #ifdef took care of the issue. I should have remembered that, its part 
of C lol! :-D

Tim: I wasn't able to get your macro to work (though looking at it while I'm 
typing, it looks like the c=c+1 should come after the c2=c2+1? Am I right?). 
I'll play with it a bit later, but that is pretty close to the redim I was 
referring to.

I hadn't even considered an array with holes, which is something I should 
look out for, but I think in the context of this specific problem that 
wasn't happening.
At least I have the solution for that too now. :-D

I appreciate everyones help!
ian


Post a reply to this message

From: Bill Pragnell
Subject: Re: array.length()?
Date: 24 Feb 2009 06:15:00
Message: <web.49a3d5fe78dc09ae6dd25f0b0@news.povray.org>
"[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> Thanks guys!
> That #ifdef took care of the issue. I should have remembered that, its part
> of C lol! :-D

Just an extra thought - couldn't you globally #declare your array size when you
populate it, then use this value in the second macro? Unless I've misunderstood
your first post...

Bill


Post a reply to this message

From: Christian Froeschlin
Subject: Re: array.length()?
Date: 24 Feb 2009 14:15:21
Message: <49a44749$1@news.povray.org>
Tim Attwood wrote:

> #macro resize_array(ar)
 >   ...
>   #declare ar = new_ar;
> #end
 >
 > resize_array(MyArray)

Interesting. I didn't know macro parameters got by-reference
semantics when an identifier was passed instead of an RVALUE.
I think I'd prefer to assign the value using #local, though,
to make it clear no global variable "ar" is involved.


Post a reply to this message

From: [GDS|Entropy]
Subject: Re: array.length()?
Date: 24 Feb 2009 16:46:26
Message: <49a46ab2$1@news.povray.org>
I had a version which did this, but I could only summon it once, as any 
subsequent incarnations redefined the contents upon each call.

ian

"Bill Pragnell" <bil### [at] hotmailcom> wrote in message 
news:web.49a3d5fe78dc09ae6dd25f0b0@news.povray.org...
> "[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
>> Thanks guys!
>> That #ifdef took care of the issue. I should have remembered that, its 
>> part
>> of C lol! :-D
>
> Just an extra thought - couldn't you globally #declare your array size 
> when you
> populate it, then use this value in the second macro? Unless I've 
> misunderstood
> your first post...
>
> Bill
>
>
>


Post a reply to this message

From: Bill Pragnell
Subject: Re: array.length()?
Date: 25 Feb 2009 04:20:00
Message: <web.49a50c3678dc09ae6dd25f0b0@news.povray.org>
> "Bill Pragnell" <bil### [at] hotmailcom> wrote in message
> > Just an extra thought - couldn't you globally #declare your array size
> > when you
> > populate it, then use this value in the second macro?

"[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> I had a version which did this, but I could only summon it once, as any
> subsequent incarnations redefined the contents upon each call.

I'm not sure I understand the problem... based on the first paragraph of your
original post, if you keep track of the number of array elements in the first
macro and #declare an array_size variable (not #local), then this number will
be available for all subsequent code, just like the array, and you will know how
many elements to loop through. If you call the same macro again, it will simply
redefine this counter to reflect the new array contents... is this not what you
want to do?

I've got the vague feeling that I'm missing something, as this seems too easy a
solution compared with other replies! :-)

Bill


Post a reply to this message

From: clipka
Subject: Re: array.length()?
Date: 25 Feb 2009 06:05:00
Message: <web.49a5259a78dc09aebdc576310@news.povray.org>
"Bill Pragnell" <bil### [at] hotmailcom> wrote:
> > > Just an extra thought - couldn't you globally #declare your array size
> > > when you populate it, then use this value in the second macro?
>
> "[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> > I had a version which did this, but I could only summon it once, as any
> > subsequent incarnations redefined the contents upon each call.
>
> I'm not sure I understand the problem... based on the first paragraph of your
> original post, if you keep track of the number of array elements in the first
> macro and #declare an array_size variable (not #local), then this number will
> be available for all subsequent code, just like the array, and you will know how
> many elements to loop through. If you call the same macro again, it will simply
> redefine this counter to reflect the new array contents... is this not what you
> want to do?
>
> I've got the vague feeling that I'm missing something, as this seems too easy a
> solution compared with other replies! :-)

Souns to me that he wants to use the same pair of macros over and over again and
therefore a single "global variable" to hold the array size wouldn't be enough.

I think testing for #ifdef(myArray[i]) is quite an elegant solution for the
problem: You just need the array, nothing more. "Global thinking" may be easier
for a quick hack, but is usually not so elegant in the long run. It's often an
obstacle when you want to turn code into a re-usable macro.


Post a reply to this message

From: Bill Pragnell
Subject: Re: array.length()?
Date: 25 Feb 2009 08:30:00
Message: <web.49a5473978dc09ae6dd25f0b0@news.povray.org>
"clipka" <nomail@nomail> wrote:
> I think testing for #ifdef(myArray[i]) is quite an elegant solution for the
> problem: You just need the array, nothing more. "Global thinking" may be easier
> for a quick hack, but is usually not so elegant in the long run. It's often an
> obstacle when you want to turn code into a re-usable macro.

Oh yes, I'm with you there. It's definitely a dirty trick, I'm just curious to
know why it mightn't work.


Post a reply to this message

From: [GDS|Entropy]
Subject: Re: array.length()?
Date: 25 Feb 2009 22:27:34
Message: <49a60c26$1@news.povray.org>
Well at this point there are no longer any issues with the multiple 
summoning of arrays as defined in previous macros, as the following example 
shows:

//      randVectorMM Macro by [GDS|Entropy]
//------
#macro randVectorMM(Array,ctr,RsA,Min,Max,Method,Object)
 #local vectorA = min_extent(Object);
 #local vectorB = max_extent(Object);
 #local i=0;
 #while (i<ctr)
  #switch (Method)
   #case (0)
    #declare Array[i] = <RRand(RsA, Min+vectorA.x, Max+vectorB.x),RRand(RsA, 
Min+vectorA.y, Max+vectorB.y),RRand(RsA, Min+vectorA.z, Max+vectorB.z)>;
   #break
   #case (1)
    #declare Array[i] = <sin(RRand(RsA, Min+vectorA.x, 
Max+vectorB.x)),RRand(RsA, Min+vectorA.y, Max+vectorB.y),tan(RRand(RsA, 
Min+vectorA.z, Max+vectorB.z))>;
   #break
   #case (2)
    #declare Array[i] = <sin(RRand(RsA, Min+vectorA.x, 
Max+vectorB.x)),atanh(RRand(RsA, Min+vectorA.y, 
Max+vectorB.y)),tan(RRand(RsA, Min+vectorA.z, Max+vectorB.z))>;
   #break
   #case (3)
    #declare Array[i] = <asin(RRand(RsA, Min+vectorA.x, 
Max+vectorB.x)),atanh(RRand(RsA, Min+vectorA.y, 
Max+vectorB.y)),tan(RRand(RsA, Min+vectorA.z, Max+vectorB.z))>;
   #break
   #case (4)
    #declare Array[i] = <acosh(RRand(RsA, Min+vectorA.x, 
Max+vectorB.x)),asinh(RRand(RsA, Min+vectorA.y, 
Max+vectorB.y)),tan(RRand(RsA, Min+vectorA.z, Max+vectorB.z))>;
   #break
   #case (5)
    #declare Array[i] = <cos(i+RRand(RsA, Min+vectorA.x, 
Max+vectorB.x)),i+RRand(RsA, Min+vectorA.y, Max+vectorB.y),sin(i+RRand(RsA, 
Min+vectorA.z, Max+vectorB.z))>;
   #break
  #end
 #set i=i+1;
 #end
#end

//surrounding macro excised before and after the following snippet
#declare sRandVectArray = array [snowCounter];
randVectorMM(sRandVectArray,snowCounter,seed(3141592654),-sFudgeFactor,sFudgeFactor,sMethod,testObject)
//------

I could go so far as to define get/set macros for everything, but in this 
case, I think that would be going a little far. :p

SDL gets easier and easier for me as I continue to learn tricks that make it 
behave like other C based languages.

There was an example file by Jean-Charles Marteau regarding Object Oriented 
POV code that I have, and can't seem to locate the download location for 
that did a pretty good job of this.

I can post the zip to p.b.i if anyone is interested.

ian

"Bill Pragnell" <bil### [at] hotmailcom> wrote in message 
news:web.49a50c3678dc09ae6dd25f0b0@news.povray.org...
>> "Bill Pragnell" <bil### [at] hotmailcom> wrote in message
>> > Just an extra thought - couldn't you globally #declare your array size
>> > when you
>> > populate it, then use this value in the second macro?
>
> "[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
>> I had a version which did this, but I could only summon it once, as any
>> subsequent incarnations redefined the contents upon each call.
>
> I'm not sure I understand the problem... based on the first paragraph of 
> your
> original post, if you keep track of the number of array elements in the 
> first
> macro and #declare an array_size variable (not #local), then this number 
> will
> be available for all subsequent code, just like the array, and you will 
> know how
> many elements to loop through. If you call the same macro again, it will 
> simply
> redefine this counter to reflect the new array contents... is this not 
> what you
> want to do?
>
> I've got the vague feeling that I'm missing something, as this seems too 
> easy a
> solution compared with other replies! :-)
>
> Bill
>
>
>
>


Post a reply to this message

<<< Previous 4 Messages Goto Initial 10 Messages

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