View Full Version : I'm coming crazy...help me

KurtCob

11-23-2000, 11:33 PM

Hello guys,

I want to draw curves (like cubic splines)..can someone help me on doing this ? I have tried to search on the net, in books, and I can't find anything.. I NEED some help.

Any suggestions/solutions will be welcome.

Thank you for the moment

Best regards

Kurt

did you try this link ? http://nehe.gamedev.net/tutorials/lesson29.asp

Hope it will help.

If you find other links, would you mail it to me please ???

Thx

Kilam Malik

11-24-2000, 12:41 AM

Depends on how you want to do it. You can either calculate your own cubic splines or use the bezier/nurbs from opengl.

For the latter one, search for functions like

glMap

glEval

glBeginCurve

glBeginSurface

in your documentation / books (e.g. superbible page 533).

If you need especcialy cubic splines you have to calculate them by yourself.

KurtCob

11-24-2000, 04:00 AM

Ok Ok....I want to use my own cubic spline (nurbs, don't produce the result taht I want), how can I calculate this ??? Thank you for the moment.

Best regards

Kurt

Kilam Malik

11-24-2000, 06:04 AM

I did a cubic spline a few years ago. I can look for the sources (FORTRAN ;-), but first I need to know if they will fit your needs:

I give the spline a few control points where the spline has to go through. I need extra points at the ends to define how it behaves there. It's 2D! Is that what you're looking for?

KurtCob

11-24-2000, 06:39 AM

Yeh!! (It is 2D curves, that I want to generate)

Probably it is the same, may be we can make a try, please if you can send this by mail, diogojacobs@cb.conex.net.

Thank you for the moment

Best regards

Kurt

KurtCob

11-26-2000, 09:33 AM

Hello Kilam,

Thank you for your code, but what I was trying to find is the math formula to do the interpolation, your code has comments in another language that I dont know. Can someone give me some formula, or tutorial, or link in the net, that shows how to make the cubic spline interpolation, to help me...it is really important for me..

Thank you anyway.

Best regards

Kurt

Here is a formula for you from the book Texturing and Modelling, a Procedural approach by Kenton Musgrave and David Ebert. I don't have to book with me, so you may want to look this up sometime to check my values.

Make this 4x4 matrix:

M =

-0.5, 1.5, -1.5, 0.5

1.0, -2.5, 2.0, -0.5

-0.5, 1.0, 0.5, 0.0

0.0 1.0 0.0, 0.0

Now, make a vector of 4 control points from your data. This is typically called a knot. For instance, if you wanted to interpolate the data at point 5.6 and you had an array of floats, this vector would contain array[4], array[5], array[6], array[7] (two data values in each direction from your desired number).

Now, multiply the 4x4 matrix times this 4x1 vector. The resulting vector has the coefficients of the cubic equation that you will use to interpolate.

For instance, call your resulting vector c.

interpolated value = c[0] * x^3 + c[1] * x^2 + c[2] * x + c[3].

Finally, note that x must be a number between 0.0 and 1.0 that indicates how far between the inner two numbers in your knot it should interpolate to. So for the example above, x would be 0.6, not 5.6.

Let me know if this is what you needed.

-- Zeno

KurtCob

11-26-2000, 12:55 PM

Hello,

But what is the meaning of the 4X4 matrix, what I want to do is something like, I click for point in the screen lets say :

X = 27.8335 , Y = 220.4566

X = 32.9985 , Y = 178.0280

X = 119.6554 , Y = 128.7191

X = 288.3780 , Y = 124.1322

and I want to draw a cubic spline with these points, witch will be the formula ???

Thank you in advice

Best regards

Kurt

onyXMaster

11-27-2000, 05:03 AM

Spline curves ( surfaces, etc. ) are represented by polynomials. One of the most common of representation is s[n] = i[n][0]*t^3 + i[n][1]*t^2 + i[n][1]*t + i[n][1], which makes cubic spline. n here is your dimension, i is the control matrix and t is a parametric scale going from 0 ( spline start ) to 1 ( spline end ).

You need a 2d curve, so n is 0 or 1. So you need a 4x2 control matrix. Final expression will look like:

x = a*t^3 + b*t^2 + c*t + d;

y = e*t^3 + f*t^2 + g*t + h;

where x and y are resulting pixel coords for parameter t.

To draw the spline, just calculate x and y for each value between 0 and 1 as t using some small step ( 1*10^-4 or so ). To offset start and end of your spline you can either use generic adding if you draw one spline, ore integrate basic offset into matrix as d and h members.

Now about control matrix which is

| a b c d |

| e f g h |

The values in this matrix ( especially left three columns ) control how spline maps along it's way - these numbers represent control vectors. (d;h) is the starting point, (c;g) is the first control vector, (b;f) is the second and (a;e) is the last. To control them you need to make a bit of math by yourself http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by onyXMaster (edited 11-27-2000).]

KurtCob

12-04-2000, 11:53 PM

Thank you guys for all your help, but it is not yet clear for me, (I'm sorry) can someone explain me in a better way.

Thank you for the moment

Best regards

Kurt

Succinct

12-05-2000, 03:59 AM

are you trying to do, say, bezier curves? Like the ones in MS Paint?

well, if not, most documentation i've seen online involving bezier curves has also either hyperlinked or directly explained other types of splines.

here's what to look for: Berstein Polynomials/functions.

it's real easy, 11th grade algebra.

alright, here's what i remember off the top of my head (i never really memorized it, cuz i only looked at it for a litte while, and i figured, "i have the book if i need it" http://www.opengl.org/discussion_boards/ubb/wink.gif )

you take 4 points (called control points).

Points 0 and 3 are the endpoints.

Points 1 and 2 form tangent vectors w/ the endpoints, basically meaning that whereever point 1 is, the curve will initially follow a path similar to the line from point 0 to 1.

then, you get a normalizer function, u, ranging from 0..1, and plug and chug (using vector arithmitic):

given

b(i,u) = berstein function i of u

C[i] = control point i

b(0,u) = u^3

b(1,u) = 3u^2(1 - u)

b(2,u) = 3u(1 - u)^2

b(3,u) = (1 - u)^3

Point p(u) = Sum(i = 0, i < 4)( C[i]*b(i,u) )

or

p(u) = C[0]*b(0,u) + C[1]*b(1,u) + C[2]*b(2,u) + C[3]*b(3,u);

or even more plugging:

p(u) = C[0]*u^3 + C[1]*3*u^2*(1-u) + C[2]*3*u*(1-u)^2 + C[3]*(1 - u)^3

and there are a hundred different ways of expressing it, optimizing the math, etc.

hrmm.

kinda sloppy...

could someone try and check my math?

like i said, it's off the top of my head...

i'll consult my book tonight, though, and have another post for you tomorrow w/ the 100% correct values. (alan watts: "3D Computer Graphics", 2nd ed.)

hope i'm clear enough

[This message has been edited by Succinct (edited 12-05-2000).]

KurtCob

12-05-2000, 05:45 AM

Ok thank you again,

Just one more question,

X = 27.8335 , Y = 220.4566 => this

X = 32.9985 , Y = 178.0280

X = 119.6554 , Y = 128.7191

X = 288.3780 , Y = 124.1322 => and this are the end points.

Witch will be the math formula to made the cubic spline interpolation ?

Thank you for the moment

Best regards

Kurt

hemicube

12-05-2000, 06:28 PM

struct POINT

{

float x, y;

POINT(float u, float v)

{

x = u;

y = v;

}

};

void DrawCubicSpline(POINT cv[4], float steps = 42.0f)

{

for (float t = 0.0f; t < 1.0f; t += 1.0f / steps)

{

float t2 = t * t;

float t3 = t2 * t;

// Screen coords

int sx = cv[0].x * t3 + cv[1].x * t2 + cv[2].x * t + cv[3].x;

int sy = cv[0].y * t3 + cv[1].y * t2 + cv[2].y * t + cv[3].y;

DrawPixel(sx, sy);

/* If you want a solid spline just draw lines between sample points

I figured I would leave you something todo http://www.opengl.org/discussion_boards/ubb/wink.gif

*/

}

}

void main()

{

POINT cvs[4];

/* Using you sample points

X = 27.8335 , Y = 220.4566

X = 32.9985 , Y = 178.0280

X = 119.6554 , Y = 128.7191

X = 288.3780 , Y = 124.1322

*/

cvs[0] = POINT(27.8335f, 220.4566f);

cvs[1] = POINT(32.9985f, 178.0280f);

cvs[2] = POINT(119.6554f, 128.7191f);

cvs[3] = POINT(288.3780f, 124.1322f);

DrawCubicSpline(cvs);

}

Kilam Malik

12-05-2000, 10:26 PM

Hemicube, you are using the 4 points (where the spline should go through) as coefficients of the spline function. But you have to use coefficients here. They must be calculated from the 4 (or more) control points.

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.