POV-Ray : Newsgroups : povray.advanced-users : Create spline Server Time
23 Dec 2024 18:34:55 EST (-0500)
  Create spline (Message 1 to 10 of 10)  
From: twister
Subject: Create spline
Date: 2 Mar 2009 08:40:03
Message: <web.49abe083a6290068c9ee64130@news.povray.org>
Hi all,

Is there any possibility to write a #macro that returns spline by value? I have
made such macro but I have to pass a parameter to the macro that will be a
place for spline. I excpect something like that:

#declare new_spline = createSpline(myArray);

Now I have to do it like that:

#declare new_spline = 0;
createSpline(myArr, new_spline)
//now I have new_spline initialized

Thanks in advance,
twister


Post a reply to this message

From: Warp
Subject: Re: Create spline
Date: 2 Mar 2009 11:09:46
Message: <49ac04ca@news.povray.org>
twister <twi### [at] o2pl> wrote:
> Is there any possibility to write a #macro that returns spline by value? I have
> made such macro but I have to pass a parameter to the macro that will be a
> place for spline. I excpect something like that:

> #declare new_spline = createSpline(myArray);

> Now I have to do it like that:

> #declare new_spline = 0;
> createSpline(myArr, new_spline)
> //now I have new_spline initialized

  I can't really understand the problem you are having. Care to give an
example of a macro implementation which doesn't work as you want?

-- 
                                                          - Warp


Post a reply to this message

From: Kenneth
Subject: Re: Create spline
Date: 2 Mar 2009 18:05:00
Message: <web.49ac65a5db4d2f97f50167bc0@news.povray.org>
"twister" <twi### [at] o2pl> wrote:
> Hi all,
>
> Is there any possibility to write a #macro that returns spline by value? I have
> made such macro but I have to pass a parameter to the macro that will be a
> place for spline. I excpect something like that:
>
> #declare new_spline = createSpline(myArray);
>
> Now I have to do it like that:
>
> #declare new_spline = 0;
> createSpline(myArr, new_spline)
> //now I have new_spline initialized
>

Could it perhaps be a problem with how you are initially setting up or loading
values into myArray? An array[] starts at location 0, not 1.  Just a thought.

Ken W.


Post a reply to this message

From: Charles C
Subject: Re: Create spline
Date: 3 Mar 2009 02:30:00
Message: <web.49acda93db4d2f97cac4259f0@news.povray.org>
"twister" <twi### [at] o2pl> wrote:
> Hi all,
>
> Is there any possibility to write a #macro that returns spline by value? I have
> made such macro but I have to pass a parameter to the macro that will be a
> place for spline. I excpect something like that:
>
> #declare new_spline = createSpline(myArray);
>
> Now I have to do it like that:
>
> #declare new_spline = 0;
> createSpline(myArr, new_spline)
> //now I have new_spline initialized
>
> Thanks in advance,
> twister


I think for whatever reason, splines have to be global in order to return
correctly from inside a macro.  For instance, in my array -> spline macro below,
if you try to return the local spline, POV-Ray will crash:



Charles

//Usage:
//#local MyNewSpline = Spline_From_Array(MyArray,MyArray_Length);
// The_Array should be an array of vectors 4D vectors in the form <x,y,z,t>
// where t is the position value along the spline to be created.
#macro Spline_From_Array(The_Array,Array_Length)
  //POV-Ray crashes if this gets changed to #local:
  #local ATempSpline = spline{
   cubic_spline
   #local Ctr = 0;
   #while (Ctr < Array_Length)
      #local Tempfloat = The_Array[Ctr].t; //avoids error on next line.
      Tempfloat,  <The_Array[Ctr].x, The_Array[Ctr].y, The_Array[Ctr].z>,
     #local Ctr = Ctr+1;
   #end //end while
  }
  //The returned spline must be global or POV-Ray will crash:
  #declare ATempSpline_G = ATempSpline; //create a global variable
  //ATempSpline //Don't return this spline or POV-Ray will crash!
  ATempSpline_G //Return this spline
#end //end #macro Spline_From_Array(The_Array,Array_Length)


Post a reply to this message

From: Charles C
Subject: Re: Create spline
Date: 3 Mar 2009 02:35:01
Message: <web.49acdd97db4d2f97cac4259f0@news.povray.org>
Please disregard the comment in the macro:
 //POV-Ray crashes if this gets changed to #local:
It's a remnant from before I modified the macro.
Charles


Post a reply to this message

From: twister
Subject: Re: Create spline
Date: 3 Mar 2009 04:25:01
Message: <web.49acf63adb4d2f97c9ee64130@news.povray.org>
I have created such a #macro:

#macro createSpline(arr, arrSize, name)
  #local length = 0;
  #local i = 0;
  #while(i < arrSize - 1)
    #local length = length + vlength(arr[i+1] - arr[i]);
    #local i = i + 1;
  #end
  #local overallLength = length;
  #local length = 0;
  #local i = 0;
  #local sp = spline {
    natural_spline
    0.00, arr[0],
    #while(i < arrSize - 1)
      #local length = length + vlength(arr[i+1] - arr[i]);
      length/overallLength, arr[i+1],
      #local i = i + 1;
    #end
    1.00, arr[arrSize - 1]
  }
  #declare name = sp;
#end

So I can use it in this way:
#declare new_spline = 0;
createSpline(myArr, 5, new_spline)
//here I have new_spline filled with spline object

But my intention is to create a #macro that returns spline by value, not by
parameter as above. I could then use my #macro in this way:

#declare new_spline = createSpline(myArr, 5);
//where myArr is an array with points defining a curve

Question 2: Is there any way (other than using #ifdef command) to check the size
of an array?

Best regards,
twister


Post a reply to this message

From: Chris B
Subject: Re: Create spline
Date: 3 Mar 2009 06:27:12
Message: <49ad1410@news.povray.org>
"twister" <twi### [at] o2pl> wrote in message 
news:web.49acf63adb4d2f97c9ee64130@news.povray.org...
>I have created such a #macro:
> ...
>
> So I can use it in this way:
> #declare new_spline = 0;
> createSpline(myArr, 5, new_spline)
> //here I have new_spline filled with spline object
>
> But my intention is to create a #macro that returns spline by value, not 
> by
> parameter as above. I could then use my #macro in this way:
>
> #declare new_spline = createSpline(myArr, 5);
> //where myArr is an array with points defining a curve
>

If you look at Charles'  example (following his sig), you'll see that this 
is what it does. Essentially in your example, where you #declare name = sp; 
you can simply specify   sp (no quotes, brackets, semicolon etc.). 
Alternatively you can just remove the '#local sp =' from in front of the 
spline definition and the spline will be returned by the macro.


> Question 2: Is there any way (other than using #ifdef command) to check 
> the size
> of an array?
>

dimension_size(arr,1)


Regards,
Chris B.


Post a reply to this message

From: clipka
Subject: Re: Create spline
Date: 3 Mar 2009 08:00:00
Message: <web.49ad28c5db4d2f97bdc576310@news.povray.org>
"twister" <twi### [at] o2pl> wrote:
> But my intention is to create a #macro that returns spline by value, not by
> parameter as above. I could then use my #macro in this way:
>
> #declare new_spline = createSpline(myArr, 5);
> //where myArr is an array with points defining a curve

Well, the normal procedure for return-by-value from a macro is this:

#macro MyMacro(foo, bar)
  #declare myReturnValue = ...<something complicated>...
  myReturnValue
#end

I don't see why this shouldn't work for splines, too.


> Question 2: Is there any way (other than using #ifdef command) to check the
> size of an array?

Due to recent discussions, it may not be quite clear what you mean by "size"...

dimension_size(myArray,1) gives you the "official" size of a 1-dimensional
array.

#ifdef(myArray[i]) tests wheter there has actually been a value stored at index
i.

Aside from this, there is nothing. Remember, POV arrays are *not* dynamically
sized; they just might have some elements set to an undefined value (some
"null" value so to speak).


Post a reply to this message

From: twister
Subject: Re: Create spline
Date: 5 Mar 2009 09:45:00
Message: <web.49afe47cdb4d2f97c9ee64130@news.povray.org>
Thanks to all of you. create_spline macro works fine as I wanted to work.
Due to array size I meant number of elements in the array and for now
dimension_size is enough for me.

Best regards,
twister


Post a reply to this message

From: clipka
Subject: Re: Create spline
Date: 5 Mar 2009 10:35:00
Message: <web.49aff023db4d2f97f567c3de0@news.povray.org>
"twister" <twi### [at] o2pl> wrote:
> Thanks to all of you. create_spline macro works fine as I wanted to work.
> Due to array size I meant number of elements in the array and for now
> dimension_size is enough for me.

To efficiently check the "populated size" of an array, you could use #ifdef in a
binary search manner:

#local iLastKnownPopulated = -1;
#local iFirstKnownUnpopulated = dimension_size(MyArray,1);
#while (iLastKnownPopulated + 1 < iFirstKnownUnpopulated)
  #local iMid = int((iLastKnownPopulated + iFirstKnownUnpopulated)/2);
  #ifdef(MyArray[iCenter])
    #local iLastKnownPopulated = iMid;
  #else
    #local iFirstKnownUnpopulated = iMid;
  #end
#end
#local PopulatedArraySize = iFirstKnownUnpopulated;

(untested draft)


Post a reply to this message

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