POV-Ray : Newsgroups : povray.advanced-users : struggle with splines Server Time
1 Nov 2024 09:20:34 EDT (-0400)
  struggle with splines (Message 1 to 10 of 10)  
From: Chrisir
Subject: struggle with splines
Date: 3 Oct 2006 17:20:00
Message: <web.4522d3275cb1df97a9bd07600@news.povray.org>
Hello!

I am just struggling with splines here.
I have one spline then should go from P1
to P2 (exactly), being modified by M1, M2 and M3.

How can I make positively sure that my spline starts exactly
always and definetly in P1 and ends exactly
always and definetly in P2?

I build the thing for various reasons as a macro.
I start MySphere1, which uses MSpline.

Please help.

Thanks!

Yours sincerely,

Chris




#macro MSpline(P1,P2,M1,M2,M3)

// defines Spline

  #declare MSpline1 = spline {
    cubic_spline
    -1,M1
     0,P1
     0.5,M2
     1,P2
     2,M3
  }
#end  // end of macro

#macro MySphere1(P1,c1,P2,c2,M1,M2,M3) // c1 and c2 is for color.

        // uses spline MSpline

        #declare SphereRadius = 0.02;
        #declare ctr = 0;

        MSpline(P1,P2,M1,M2,M3);

        #while (ctr < 1)
        sphere {
            MSpline1(ctr), SphereRadius
            pigment { rgb c1 }
          }
          #declare ctr = ctr + 0.01;
        #end   // end of while

#end  // end of macro


Post a reply to this message

From: Chris B
Subject: Re: struggle with splines
Date: 3 Oct 2006 18:20:06
Message: <4522e216$1@news.povray.org>
"Chrisir" <nomail@nomail> wrote in message 
news:web.4522d3275cb1df97a9bd07600@news.povray.org...
>
>
> Hello!
>
> I am just struggling with splines here.
> I have one spline then should go from P1
> to P2 (exactly), being modified by M1, M2 and M3.
>
> How can I make positively sure that my spline starts exactly
> always and definetly in P1 and ends exactly
> always and definetly in P2?
>
> I build the thing for various reasons as a macro.
> I start MySphere1, which uses MSpline.
>
> Please help.
>
> Thanks!
>
> Yours sincerely,
>
> Chris
>
>
>
>
> #macro MSpline(P1,P2,M1,M2,M3)
>
> // defines Spline
>
>  #declare MSpline1 = spline {
>    cubic_spline
>    -1,M1
>     0,P1
>     0.5,M2
>     1,P2
>     2,M3
>  }
> #end  // end of macro
>
> #macro MySphere1(P1,c1,P2,c2,M1,M2,M3) // c1 and c2 is for color.
>
>        // uses spline MSpline
>
>        #declare SphereRadius = 0.02;
>        #declare ctr = 0;
>
>        MSpline(P1,P2,M1,M2,M3);
>
>        #while (ctr < 1)
>        sphere {
>            MSpline1(ctr), SphereRadius
>            pigment { rgb c1 }
>          }
>          #declare ctr = ctr + 0.01;
>        #end   // end of while
>
> #end  // end of macro
>
>

If you specify "#while (ctr <= 1)", then I reckon it should end at P2, 
rather than one sphere before it gets to P2.

Regards,
Chris B.


Post a reply to this message

From: Charles C
Subject: Re: struggle with splines
Date: 3 Oct 2006 18:40:00
Message: <web.4522e654d128f9362869ae640@news.povray.org>
Also if you want to verify it you can put in a test, eg something like:

#if(  MSpline(0) = P1 )
 #debug " All's well! "
#else
 #debug " Dohh! "
#end

Charles


Post a reply to this message

From: Random Pete
Subject: Re: struggle with splines
Date: 4 Oct 2006 00:10:00
Message: <web.45233402d128f9369cf609e10@news.povray.org>
"Chrisir" <nomail@nomail> wrote:
> Hello!
>
> I am just struggling with splines here.
> I have one spline then should go from P1
> to P2 (exactly), being modified by M1, M2 and M3.

You might find it useful to write the code like this:


#macro MSpline(P1,P2,M1,M2,M3)
  spline {
    cubic_spline
    -1,M1
     0,P1
     0.5,M2
     1,P2
     2,M3
  }
#end  // end of macro

#macro ObjSpline(Obj,Size,c1,c2,Spline) // c1 and c2 is for color.
        #local ctr = 0;

        #while (ctr <= 1)
                object {
                        Obj
                        scale Size
                        translate Spline(ctr)
                        pigment { rgb c1*(1-ctr) + c2*(2-ctr) }
                }
                #local ctr = ctr + 0.01;
        #end   // end of while
#end  // end of macro

#declare MSpline1 = MSpline(coords...);
ObjSpline(sphere{0,1},0.05,rgb<1,0,0>,rgb<0,1,0>,MSpline1)



....This way the ObjSpline macro can be used to distribute *any* object along
the spline's path. You could add additional parameters such as the number of
objects to distribute along the spline, and you could also make the function
reorient the object to point in the direction of the spline.


Post a reply to this message

From: Chrisir
Subject: Re: struggle with splines
Date: 4 Oct 2006 07:50:00
Message: <web.45239ee9d128f936a9bd07600@news.povray.org>
Guys,

thank you so much for your highly appreciated contributions.

Question: Do I have my maths right
when I have a spline with -1,0,0.5,1,2


> #macro MSpline(P1,P2,M1,M2,M3)
>   spline {
>     cubic_spline
>     -1,M1
>      0,P1
>      0.5,M2
>      1,P2
>      2,M3
>   }
> #end  // end of macro

going from -1 to 2
and my while is from 0 to 1?

#local ctr = 0;
#while (ctr <= 1)
 [......]
 #local ctr = ctr + 0.01;
#end   // end of while

I tried other figures (instead of -1,0,0.5,1,2), but I can not figure it
out.

Thanks!

Greetings!

Chris


Post a reply to this message

From: Warp
Subject: Re: struggle with splines
Date: 4 Oct 2006 10:02:35
Message: <4523befa@news.povray.org>
Chris B <c_b### [at] btconnectcomnospam> wrote:
> >          #declare ctr = ctr + 0.01;

> If you specify "#while (ctr <= 1)", then I reckon it should end at P2, 
> rather than one sphere before it gets to P2.

  Only in theory. In practice the last point may still be skipped.
This is because 0.01 cannot be represented in an exact way in binary
floating point format, and adding it repeatedly will cause the error
to grow up so large that the end result will be significantly erroneous.

  (A number like 0.025 *can* be represented with exact accuracy with
binary floating point but 0.01 can't. It might not be immediately obvious
why, if you don't know how binary floating point works, but it's just
like that. It's the same as 1/3 not having an exact representation in
decimal.)

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: struggle with splines
Date: 4 Oct 2006 10:07:34
Message: <4523c026@news.povray.org>
Chrisir <nomail@nomail> wrote:
> How can I make positively sure that my spline starts exactly
> always and definetly in P1 and ends exactly
> always and definetly in P2?

>         #declare ctr = 0;

>         #while (ctr < 1)
            ...
>           #declare ctr = ctr + 0.01;
>         #end   // end of while

  Basic programming "trick":

  You shouldn't use such a floating point number directly as your loop
counter. 0.01 cannot be represented accurately in binary floating point
and thus the sum you are accumulating in 'ctr' will become more and more
inaccurate, and there's a high chance that the loop will not be executed
the exact amount of times you want.
  Instead, you should do it like this:

#declare Index = 0;
#while(Index < 100)
  #declare ctr = Index/99;

  ... (the loop body, which uses 'ctr') ...

  #declare Index = Index+1;
#end

  This makes sure that the loop will be executed exactly 10 times and
that 'ctr' will get values between exactly 0 and 1.

-- 
                                                          - Warp


Post a reply to this message

From: Alain
Subject: Re: struggle with splines
Date: 4 Oct 2006 21:49:55
Message: <452464c3$1@news.povray.org>
Warp nous apporta ses lumieres en ce 04/10/2006 10:02:
> Chris B <c_b### [at] btconnectcomnospam> wrote:
>>>          #declare ctr = ctr + 0.01;
> 
>> If you specify "#while (ctr <= 1)", then I reckon it should end at P2, 
>> rather than one sphere before it gets to P2.
> 
>   Only in theory. In practice the last point may still be skipped.
> This is because 0.01 cannot be represented in an exact way in binary
> floating point format, and adding it repeatedly will cause the error
> to grow up so large that the end result will be significantly erroneous.
> 
>   (A number like 0.025 *can* be represented with exact accuracy with
> binary floating point but 0.01 can't. It might not be immediately obvious
> why, if you don't know how binary floating point works, but it's just
> like that. It's the same as 1/3 not having an exact representation in
> decimal.)
> 
You can circumvent the problem by multiplying everything by 100, and dividing by 
100 as you use the values to get the actual positions. It may slightly increase 
the parsing time, but you are now adding units and avoiding the rounding errors.

-- 
Alain
-------------------------------------------------
WARNING: the crumsumpten of alcohol may Mack you tink you kan tpye reel gode


Post a reply to this message

From: Warp
Subject: Re: struggle with splines
Date: 5 Oct 2006 10:25:13
Message: <452515c9@news.povray.org>
Alain <ele### [at] netscapenet> wrote:
> You can circumvent the problem by multiplying everything by 100, and dividing by 
> 100 as you use the values to get the actual positions. It may slightly increase 
> the parsing time, but you are now adding units and avoiding the rounding errors.

  The standard easy way to avoid these errors is not to use floating point
as loop counters. Just use integer loop counters and then create a floating
point variable from it, scaling it to the desired range. As I demonstrated
in another post.

-- 
                                                          - Warp


Post a reply to this message

From: Chrisir
Subject: Re: struggle with splines
Date: 5 Oct 2006 15:30:01
Message: <web.452559f0d128f936800c89c60@news.povray.org>
Well guys,

you definetly made my day.
Thanks so much!

What I did now, was to take the
sophisticated code from Random Pete
(I wrote
pigment { rgb (c1*(1-Value) + c2*(Value)) }
instead of
pigment { rgb c1*(1-ctr) + c2*(2-ctr)
the 2- bit seems to be wrong).

I then used the
Basic programming "trick" from
Warp which boils down to
#declare Index = 0;
#while(Index < 100)
  #declare ctr = Index/99;

For reasons of completeness
here the whole scene. To do
more splines just add more
    // calling of macro
    MyLine1 [.........]
with different values.

Thanks you all so much!!!

Greetings, Chrisir



// Persistence of Vision Ray Tracer Scene Description File
// File Path:
// File Name: temp05_10_2006_20_23_51.pov
// Vers: 3.6
// Desc: Scene
// Date: 05.10.2006

#version 3.6;

#declare Photons=off;
#declare Radiosity=off;

// ==== Standard POV-Ray Includes ====
// #include "colors.inc"      // Standard Color definitions
// #include "textures.inc"    // Standard Texture definitions
// #include "functions.inc"  // internal functions
// #include "glass.inc"      // Glass

// ----------------------------------------

// Light
light_source {
  <500,500,-500>       // light's position
  color rgb <1, 1, 1>  // light's color
}

light_source {
  <700,700,-500>       // light's position
  color rgb <1, 1, 1>  // light's color
}

light_source {
  <450, 650, 50>       // light's position
  color rgb<1, 1, 1> } // light's color

// ----------------------------------------

// camera
camera {
    location <0, 0, -144>
    look_at <0, 22, 0>
  }

// ----------------------------------------

// macros
#macro MSpline(P1,P2,M1,M2,M3)
  spline {
    cubic_spline
    -1,M1
     0,P1
     0.5,M2
     1,P2
     2,M3
  }
#end  // end of macro

#macro ObjSpline(Obj,Size,c1,c2,Spline)

        // Leads an Object Obj of a Size over a Spline
        // and changes its color from c1 to c2.

        #declare index = 0;

        #while (index < 100)
                // or you can use: 100/99; clock/99; index/99;
                #declare Value = index / 99;
                object {
                        Obj
                        scale Size
                        translate Spline(Value)
                        pigment { rgb (c1*(1-Value) + c2*(Value)) }
                }
                #declare index = index + 1;
        #end   // end of while
#end  // end of macro

#macro MyLine1(P1,c1,P2,c2,M1,M2,M3)

    // Connects two points P1 and P2 with a spline
    // which is modified ("M") by the points M1, M2
    // and M3.
    // Point P1 has the color c1,
    // Point P2 has the color c2.

    #local MSpline1 = MSpline( P1,P2,M1,M2,M3 );
    ObjSpline( sphere{0,1},0.5, rgb c1, rgb c2, MSpline1 )
#end  // end of macro

// ----------------------------------------

// calling of macro
MyLine1 ( <39,13,1>, <1,0,0>,       // First point and its color red
          <4,96,40>, <0,1,0>,       // Second point and its color green
          <35.18321,24.38798,-15.29158>,     // Modifiers
          <-2.2529,44.63106,36.97918>,
          <16.93481,-3.899466,50.60338>
        )

// ----------------------------------------
// end of scene.
// ----------------------------------------


Post a reply to this message

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