POV-Ray : Newsgroups : povray.advanced-users : Some math aid required... Server Time
28 Dec 2024 14:21:39 EST (-0500)
  Some math aid required... (Message 1 to 10 of 24)  
Goto Latest 10 Messages Next 10 Messages >>>
From: Tim Nikias v2 0
Subject: Some math aid required...
Date: 10 Feb 2004 06:22:02
Message: <4028beda$1@news.povray.org>
so, I've got the following formula to calculate Bezier-Splines:

Point_1*pow(1-Mover,3)+
Point_2*3*pow(1-Mover,2)*Mover-
Point_3*3*pow(Mover,2)*(1-Mover)+
Point_4*pow(Mover,3)

(I guess that formula should be known around pro's, but I've posted it here
to make descriptions easier. Mover is the value that runs from 0 to 1 from
beginning to end of the spline-segment, where one segment is made of four
points: starting- and end-point, and two control-points).

Anyways, I need to calculate the direction/tangent of the spline at any
given position. I'm somehow stuck, probably because my head is still filled
with the last exam I just wrote. Any help, or links?

Thanks in advance,
Tim

-- 
"Tim Nikias v2.0"
Homepage: <http://www.nolights.de>
Email: tim.nikias (@) nolights.de


Post a reply to this message

From: Christopher James Huff
Subject: Re: Some math aid required...
Date: 10 Feb 2004 08:59:45
Message: <cjameshuff-9FB9E9.09000610022004@news.povray.org>
In article <4028beda$1@news.povray.org>,
 "Tim Nikias v2.0" <tim.nikias (@) nolights.de> wrote:

> so, I've got the following formula to calculate Bezier-Splines:
> 
> Point_1*pow(1-Mover,3)+
> Point_2*3*pow(1-Mover,2)*Mover-
> Point_3*3*pow(Mover,2)*(1-Mover)+
> Point_4*pow(Mover,3)

> Anyways, I need to calculate the direction/tangent of the spline at any
> given position. I'm somehow stuck, probably because my head is still filled
> with the last exam I just wrote. Any help, or links?

Okay...your function is this, P1..P4 being the spline values, t being 
the spline time parameter:

P1*(1 - t)^3 +
P2*3*(1 - t)^2*t -
P3*3*t^2*(1 - t) +
P4*t^3

The slope of the tangent is simply the derivative, the rate of change at 
the given point. Unless I've screwed up the math somewhere, that would 
be:

P1*3*(1 - t)^2*(-1) +
P2*3*(2*(1 - t)*(-1)*t + (1 - t)^2) -
P3*3*(2*t*(1 - t) - t^2) +
P4*3*t^2

simplified:

3*(P2 - P1 + (P1 - 2*P2 - P3)*2*t + (P2*3 + P3*3 + P4 - P1)*t^2)

(somebody want to check that?)

The tangent line at point p would be:
x*f'(p) - p*f'(p) + f(p)
where f() is the spline function, and f'() is its derivative.

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: <chr### [at] tagpovrayorg>
http://tag.povray.org/


Post a reply to this message

From: sascha
Subject: Re: Some math aid required...
Date: 10 Feb 2004 09:04:06
Message: <4028e4d6$1@news.povray.org>
Ok, first let's rewrite the bezier formula:

p(t) = p0 * B0(t) + p1 * B1(t) + p2 * B2(t) + p3 * B3(t)

where p0,p1,p2,p3 are the bezier control points and

B0(t) = (1-t)^3
B1(t) = 3 * t * (1-t)^2
B2(t) = 3 * t^2 * (1-t)
B3(t) = t^3

(Bernstein polynomials)

this is a cubic bezier spline.

The normals should be quadratic, just calculate

n0 = p1 - p0
n1 = p2 - p1
n2 = p3 - p2

and

B0(t) = (1-t)^2
B1(t) = 2 * t * (1-t)
B2(t) = t^2

the normal at t is

n(t) = n0 * B0(t) + n1 * B1(t) + n2 * B2(t)

I'm not sure if these are the true normals or just approximations, I'd have to check.
Anyway, you can always calculate the true normals by differenciating the Bernstein
polynominals B0(t),B1(t),B2(t),B3(t), so

dB0/dt = -3(1-t)^2
dB1/dt = -6t(1-t) + 3(1-t)^2
dB2/dt = -3t^2 + 6t(1-t)
dB3/dt = 3t^2

...

I hope this helps.

-Sascha


Tim Nikias v2.0 wrote:

> so, I've got the following formula to calculate Bezier-Splines:
> 
> Point_1*pow(1-Mover,3)+
> Point_2*3*pow(1-Mover,2)*Mover-
> Point_3*3*pow(Mover,2)*(1-Mover)+
> Point_4*pow(Mover,3)
> 
> (I guess that formula should be known around pro's, but I've posted it here
> to make descriptions easier. Mover is the value that runs from 0 to 1 from
> beginning to end of the spline-segment, where one segment is made of four
> points: starting- and end-point, and two control-points).
> 
> Anyways, I need to calculate the direction/tangent of the spline at any
> given position. I'm somehow stuck, probably because my head is still filled
> with the last exam I just wrote. Any help, or links?
> 
> Thanks in advance,
> Tim
>


Post a reply to this message

From: sascha
Subject: Re: Some math aid required...
Date: 10 Feb 2004 09:10:15
Message: <4028e647$1@news.povray.org>
Ah... someone was quicker :-)

Of course I meant "tangents", not "normals" in my posting...


Post a reply to this message

From: David Wallace
Subject: Re: Some math aid required...
Date: 10 Feb 2004 11:11:27
Message: <402902af@news.povray.org>
"sascha" <sas### [at] userssourceforgenet> wrote in message
news:4028e647$1@news.povray.org...
> Ah... someone was quicker :-)
>
> Of course I meant "tangents", not "normals" in my posting...

The 2nd derivative is the normal.


Post a reply to this message

From: Tim Nikias v2 0
Subject: Re: Some math aid required...
Date: 10 Feb 2004 12:08:53
Message: <40291025$1@news.povray.org>
> simplified:
>
> 3*(P2 - P1 + (P1 - 2*P2 - P3)*2*t + (P2*3 + P3*3 + P4 - P1)*t^2)

Simplified? ;-)

Uhm... Well, Sascha's approach worked quite well (now I just have to get it
work with more than one segment, but that's not the difficult part). To be
honest, I didn't really understand where you went with the derivative. Was a
long time ago that I had algebra in school, so I can't check if it's correct
or not, and since it looked so complicated, I took a first go with Sascha's
formula.

Still, enlighten me about this part:

> The tangent line at point p would be:
> x*f'(p) - p*f'(p) + f(p)
> where f() is the spline function, and f'() is its derivative.

So, instead of using t, you want me to put a point into a function? What's
x? I got a little confused here and am not really sure what you were trying
to tell me. Thanks for the effort though!

Regards,
Tim

-- 
"Tim Nikias v2.0"
Homepage: <http://www.nolights.de>
Email: tim.nikias (@) nolights.de


Post a reply to this message

From: Tim Nikias v2 0
Subject: Re: Some math aid required...
Date: 10 Feb 2004 12:10:56
Message: <402910a0@news.povray.org>
[some complicated Math stuff abd derivative, Bernstein polynomials etc]
SNIP
> I hope this helps.

I did. Though that part about diffenciating the Bernstein polynomials, that
was a little above my head. Still, the other approach works fine and seems
to return actual normals, not just approximations. At least looks like that.

Regards and thanks a lot,
Tim

-- 
"Tim Nikias v2.0"
Homepage: <http://www.nolights.de>
Email: tim.nikias (@) nolights.de


Post a reply to this message

From: Tim Nikias v2 0
Subject: Re: Some math aid required...
Date: 10 Feb 2004 12:28:22
Message: <402914b6@news.povray.org>
Ah, I take that back. It just approximates the normals, it doesn't properly
calculate them. I'll try to do something with that differenciating thingy
you mentioned at the end and return here later.

Regards,
Tim

-- 
"Tim Nikias v2.0"
Homepage: <http://www.nolights.de>
Email: tim.nikias (@) nolights.de


Post a reply to this message

From: sascha
Subject: Re: Some math aid required...
Date: 10 Feb 2004 12:34:42
Message: <40291632$1@news.povray.org>
You're right, it should give you the correct values for the tangents.

It follows from the deCasteljau algorithm (do a google for it) which can be used to
compute positions and tangents on a bezier curve.

regards,
-sascha

Tim Nikias v2.0 wrote:
> [some complicated Math stuff abd derivative, Bernstein polynomials etc]
> SNIP
> 
>>I hope this helps.
> 
> 
> I did. Though that part about diffenciating the Bernstein polynomials, that
> was a little above my head. Still, the other approach works fine and seems
> to return actual normals, not just approximations. At least looks like that.
> 
> Regards and thanks a lot,
> Tim
>


Post a reply to this message

From: sascha
Subject: Re: Some math aid required...
Date: 10 Feb 2004 12:44:41
Message: <40291889$1@news.povray.org>
No, it should work. Here's a part of a code I once wrote when playing with bezier
curves. What do you need the tangents for? Perhaps it's necessary to normalize them...

#default {
	finish { ambient 1 }
}

#declare curve_size = 0.05;
#declare point_size = 0.20;
#declare col = color rgb <1,1,1>;

camera {
	orthographic
	location <0,0,-30>
	look_at 0
}

#macro drawBezierCurve(p0,p1,p2,p3)
	#local s = 0;
	#while (s <= 1)
		#local p = bezier(p0,p1,p2,p3,s);
		#local n = dbezier(p0,p1,p2,p3,s);
		sphere {
			p,curve_size
			pigment { color col }
		}
		cylinder {
			p,p+vnormalize(n)*5,curve_size/2
			pigment { color col }
		}
		
		#local s = s + 1/50;
	#end
#end

#macro bezier(p0,p1,p2,p3,s)
	#local B0 = (1 - s) * (1 - s) * (1 - s);
	#local B1 = 3 * s * (1 - s) * (1 - s);
	#local B2 = 3 * s * s * (1 - s);
	#local B3 = s * s * s;
	p0 * B0 + p1 * B1 + p2 * B2 + p3 * B3
#end

#macro bezier2(p0,p1,p2,s)
	#local B0 = (1 - s) * (1 - s);
	#local B1 = 2 * s * (1 - s);
	#local B2 = s * s;
	p0 * B0 + p1 * B1 + p2 * B2
#end

#macro dbezier(p0,p1,p2,p3,s)
	#local n0 = p1 - p0;
	#local n1 = p2 - p1;
	#local n2 = p3 - p2;
	bezier2(n0,n1,n2,s)
#end


#macro drawPoint(p)
	sphere {
		p,point_size
		pigment { col }
	}
#end

#macro drawLine(a,b)
	cylinder {
		a,b,curve_size
		pigment { col }
	}
#end

#macro setColor(c)
	#declare col = color rgb c;
#end
	
#if (true)
	#declare A = <-10,-5>;
	#declare B = <-5,5>;
	#declare C = <5,5>;
	#declare D = <10,-5>;
	
	setColor(<1,0,0>)
	drawPoint(A)
	drawPoint(B)
	drawPoint(C)
	drawPoint(D)
	
	setColor(<0,0,1>)
	drawBezierCurve(A,B,C,D)
#end


Post a reply to this message

Goto Latest 10 Messages Next 10 Messages >>>

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