creative software programming - hanyang

61
Computer Graphics 11 Curves Yoonsang Lee Spring 2019

Upload: others

Post on 02-Dec-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creative Software Programming - Hanyang

Computer Graphics

11 – Curves

Yoonsang Lee

Spring 2019

Page 2: Creative Software Programming - Hanyang

Topics Covered

• Intro: Motivation and Curve Representation

• Polynomial Curve

– Polynomial Interpolation

– More Discussion on Polynomials

• Hermite Curve

• Bezier Curve

• (Very short) Intro to Spline

Page 3: Creative Software Programming - Hanyang

Intro: Motivation and Curve

Representation

Page 4: Creative Software Programming - Hanyang

Motivation: Why Do We Need Curve?

• Smoothness

– no discontinuity

• In many application, we need smooth shape and

smooth movement.

Page 5: Creative Software Programming - Hanyang

Curve Representations

• Non-parametric

– Explicit : y = f(x)• ex) y = x2 + 2x - 2

• Pros) Easy to generate points

• Cons) Cannot express vertical lines!

– Implicit : f(x, y) = 0• ex) x2 + y2 – 22 = 0

• Pros) Easy to test if a point is inside or outside

• Cons) Inconvenient to generate points

Page 6: Creative Software Programming - Hanyang

Curve Representations

• Parametric : (x, y) = (f(t), g(t))

– ex) (x, y) = (2 cos(t), 2 sin(t))

– Each point on a curve is expressed as a

function of additional parameter t

– Pros) Easy to generate points

– The parameter t act as a “local coordinate”

for points on the curve

• For computer graphics, the parametric

representation is the most suitable.

Page 7: Creative Software Programming - Hanyang

Polynomial Curve

Page 8: Creative Software Programming - Hanyang

Polynomial Curve

• Polynomials are usually used to describe

curves in computer graphics

– Simple

– Efficient

– Easy to manipulate

– Historical reasons

• A polynomial of degree n:

01

1

1)( atatatatx n

n

n

n

Page 9: Creative Software Programming - Hanyang

Polynomial Interpolation

• One way to make a smooth curve is with

polynomial interpolation.

• Polynomial interpolation determines a specific

smooth polynomial curve passing though given

data points.

Page 10: Creative Software Programming - Hanyang

Polynomial Interpolation

• Linear interpolation with a polynomial of degree one

– Input: two nodes

– Output: Linear polynomial

01 atatx )(

If t0=0 and t1=1, then a0=x0 and a1=x1-x0

→ x(t) = (x1-x0)t + x0 = (1-t)x0 + tx1

How to find a0 and a1?

→position of a point

parameter of a curve

We can compute the value of a0 & a1 because we have 2 equations

(=2 data points) for 2 unknowns!

Page 11: Creative Software Programming - Hanyang

Polynomial Interpolation

• Quadratic interpolation with a polynomial of degree two

),( 00 xt

),( 11 xt

01

2

2)( atatatx

2

1

0

2

1

0

2

22

2

11

2

00

2021

2

22

1011

2

12

0001

2

02

1

1

1

x

x

x

a

a

a

tt

tt

tt

xatata

xatata

xatata

),( 22 xt

(we need 3 points to get the value of 3 unknowns)

Page 12: Creative Software Programming - Hanyang

Polynomial Interpolation

• Polynomial interpolation of degree n

),( 00 xt

),( 11 xt

01)( atatatx n

n

nnn

n

n

n

nn

nn

x

x

x

a

a

a

tt

tt

tt

1

0

1

0

1

1

1

1

0

1

0

1

1

1),( nn xt

• How to find the value of unknowns an, ..., a0?

• Several methods:

– Solving linear system, Lagrange’s, Newton’s method, ...

Page 13: Creative Software Programming - Hanyang

Problem of Higher Degree Polynomial

Interpolation

• Oscillations at the ends – Runge’s Phenomenon

– Nobody uses higher degree polynomial interpolation

now

Page 14: Creative Software Programming - Hanyang

[Practice] Polynomial Interpolation

https://www.benjoffe.com/code/demos/interpolate

• Drag points and observe changes of the curve.

• Increase polynomial degree and drag points.

Page 15: Creative Software Programming - Hanyang

Cubic Polynomials

• Cubic (degree of 3) polynomials

are commonly used in computer

graphics because...

• The lowest-degree polynomials

representing a 3D space curve..

• Unwanted wiggles of higher-

degree polynomials (Runge’s

Phenomenon)

dcbap

tttt

dtctbtatz

dtctbtaty

dtctbtatx

zzzz

yyyy

xxxx

23

23

23

23

)(

or

)(

)(

)(

Page 16: Creative Software Programming - Hanyang

• How to make ?

• using ?

• Answer → Spline: piecewise polynomial

• At this moment, let’s just think about a single piece of polynomial.

Then, how to make complex curves using

such a low degree polynomial?

Page 17: Creative Software Programming - Hanyang

Defining a Single Piece of Cubic Polynomial

• Goal: Defining a specific curve (finding a, b, c, d) as we want (using data points or conditions)

• 4 unknowns, so we need 4 equations (conditions or constraints). For example,

– 4 data points

– position and derivative of 2 end points

– ...

dcbap tttt 23)(

Page 18: Creative Software Programming - Hanyang

Formulation of a Single Piece of Polynomial

• A polynomial can be formulated in two ways:

• With coefficients and variable:

– coefficients: a, b, c, d

– variable: t

• With basis functions and points:

– basis functions: b0(t), b1(t), b2(t), b3(t)

– points: p0, p1, p2, p3

Page 19: Creative Software Programming - Hanyang

01 atatx )(

Trivial Example: Linear Polynomial

),( 00 xt

),( 11 xt

Page 20: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

0

Trivial Example: Linear Polynomial

• Formulation with coefficients and variable:

• Matrix formulation

basis matrix

Page 21: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

1

Trivial Example: Linear Polynomial

• Formulation with basis functions and points:

– regroup expression by p rather than t

– interpretation in matrix viewpointbasis functions

Page 22: Creative Software Programming - Hanyang

Meaning of Basis Functions

• Contribution of each point as t changes

b0(t)=1-tb1(t)=t

Page 23: Creative Software Programming - Hanyang

Quiz #1

• Go to https://www.slido.com/

• Join #cg-hyu

• Click “Polls”

• Submit your answer in the following format:

– Student ID: Your answer

– e.g. 2017123456: 4)

• Note that you must submit all quiz answers in the

above format to be checked for “attendance”.

Page 24: Creative Software Programming - Hanyang

Hermite Curve

Page 25: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

5

Hermite curve

• Less trivial example

• Form of curve: piecewise cubic

• Constraints: endpoints and tangents (derivatives)

Charles Hermite(1822-1901)

Page 26: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

6

Hermite curve

• Solve constraints to find coefficients

Page 27: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

7

Hermite curve

• Solve constraints

to find coefficients

calculate A-1

Page 28: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

8

Hermite curve

• Matrix form is much simpler

– coefficients = rows

– basis functions = columns

A-1

Page 29: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 2

9

Coefficients = rows

Page 30: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

0

Basis functions = columns

Page 31: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

1

Hermite curve

• Hermite basis functions

Page 32: Creative Software Programming - Hanyang

[Practice] Hermite Curve Online Demo

• Change the position of end points and their

derivatives by dragging

https://codepen.io/liorda/pen/KrvBwr

Page 33: Creative Software Programming - Hanyang

Quiz #2

• Go to https://www.slido.com/

• Join #cg-hyu

• Click “Polls”

• Submit your answer in the following format:

– Student ID: Your answer

– e.g. 2017123456: 4)

• Note that you must submit all quiz answers in the

above format to be checked for “attendance”.

Page 34: Creative Software Programming - Hanyang

Bezier Curve

Page 35: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

5

Recall: Hermite curve

• Constraints: endpoints and tangents (derivatives)

Page 36: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

6

Hermite to Bézier

• Mixture of points and vectors is awkward

• Specify tangents as differences of points

Page 37: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

7

Hermite to Bézier

• Mixture of points and vectors is awkward

• Specify tangents as differences of points

Page 38: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

8

Hermite to Bézier

• Mixture of points and vectors is awkward

• Specify tangents as differences of points

Page 39: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 3

9

Hermite to Bézier

• Mixture of points and vectors is awkward

• Specify tangents as differences of points

– note derivative is defined as 3 times offset t

Pierre Bézier (1910-1999)widely published research on this curve while working at Renault

Page 40: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 4

0

Hermite to Bézier

Page 41: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 4

1

Hermite to Bézier

Page 42: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 4

2

Hermite to Bézier

Hermite matrix

Page 43: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 4

3

Hermite to Bézier

control points

Page 44: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 4

4

Bézier matrix

– note that these are the Bernstein polynomials

C(n,k) tk (1 – t)n – k

and that defines Bézier curves for any degree

use notation ‘p’ instead of ‘q’

Page 45: Creative Software Programming - Hanyang

Bezier Curve

• Bernstein basis functions

• Cubic Bezier curve: Cubic polynomial in

Bernstein bases

iinn

i tti

ntB

)1()(

33

22

12

03

3332

321

310

30

13131 pppp

ppppp

tttttt

tBtBtBtBt

)()()(

)()()()()(

33

3

123

2

23

1

33

0

)(

)1(3)(

)1(3)(

)1()(

ttB

tttB

tttB

ttB

Page 46: Creative Software Programming - Hanyang

© 2008 Steve Marschner • Cornell CS4620 Fall 2008 • Lecture 18 4

6

Bézier basis

b3,1(t)

b3,0(t)

b3,2(t)

b3,3(t)

p2

Page 47: Creative Software Programming - Hanyang

de Casteljau’sAlgorithm

• Another method to compute Bezier curve

Paul de Casteljau (1930-)first developed the ‘Bezier’ curve using this algorithm in 1959 while working at Citroën, but was not able to publish them due to company policy

Page 48: Creative Software Programming - Hanyang
Page 49: Creative Software Programming - Hanyang

t

1-t t

1-t

t

1-t

Page 50: Creative Software Programming - Hanyang

t1-t

t

1-t

Page 51: Creative Software Programming - Hanyang

t1-t

Page 52: Creative Software Programming - Hanyang

de Casteljau’sAlgorithm

https://people.eecs.berkeley.edu/~sequin/CS284/LECT06/L3.htm

Page 53: Creative Software Programming - Hanyang

de Casteljau’sAlgorithm

• Nice recursive algorithm to compute a point on a Bezier curve

• Additionally, it subdivide a Bezier curve into two segments

• You can draw a curve with a sufficient number of subdivided control points

– "Subdivision" method for displaying curves

Page 54: Creative Software Programming - Hanyang

[Practice] de Casteljau’sAlgorithm

• Move red points

• Also check the subdivision demo

http://www.malinc.se/m/DeCasteljauAndBezier.php

Page 55: Creative Software Programming - Hanyang

Displaying Curves

• Need to generate a list of line segments to draw

– What we can compute is a set of points on a curve

– Connecting them with line segments would be good approximation for the curve

• Brute-force

– Evaluate p(t) for incrementally spaced values of t

• Finite difference

– The same idea, but much more efficient

– See http://www.drdobbs.com/forward-difference-calculation-of-bezier/184403417

• Subdivision

– Use de Casteljau’s algorithm

Page 56: Creative Software Programming - Hanyang

Properties of Bezier Curve

• Intuitively controlled by control points

• Contained in the convex hull of control points

• End point interpolation

Convex hull: Minimal-sized convex polygon containing all points

Page 57: Creative Software Programming - Hanyang

Quiz #3

• Go to https://www.slido.com/

• Join #cg-hyu

• Click “Polls”

• Submit your answer in the following format:

– Student ID: Your answer

– e.g. 2017123456: 4)

• Note that you must submit all quiz answers in the

above format to be checked for “attendance”.

Page 58: Creative Software Programming - Hanyang

Bezier Spline

• A combination of piecewise Bezier curves, Bezier spline, is very widely used. For example,

• To draw shapes in graphic tools such as Adobe Illustrator

• To define animation paths in 3D authoring tools such as Blender and Maya

• TrueType fonts use quadratic Bezier spline, PostScript fonts use cubic Bezier spline

Page 59: Creative Software Programming - Hanyang

[Practice] Bezier Spline

• How to “smooth” the spline?

http://math.hws.edu/graphicsbook/demos/c2/cubic-bezier.html

Page 60: Creative Software Programming - Hanyang

Spline

• Spline: piecewise polynomial

• Three issues:

– How to connect these pieces continuously?

– How easy is it to "control" the shape of a spline?

– Does a spline have to pass through specific points?

• For details, see 11-reference-splines.pdf

Page 61: Creative Software Programming - Hanyang

Next Time

• Lab in this week:

– Lab assignment 11

• Next lecture:

– 12 - More Lighting, Texture

• Acknowledgement: Some materials come from the lecture slides of

– Prof. Jehee Lee, SNU, http://mrl.snu.ac.kr/courses/CourseGraphics/index_2017spring.html

– Prof. Taesoo Kwon, Hanyang Univ., http://calab.hanyang.ac.kr/cgi-bin/cg.cgi

– Prof. Steve Marschner, Cornell Univ., http://www.cs.cornell.edu/courses/cs4620/2014fa/index.shtml

– Prof. William H. Hsu, Kansas State Univ. http://slideplayer.com/slide/4635444/