Scan Conversion
-
Upload
khangminh22 -
Category
Documents
-
view
1 -
download
0
Transcript of Scan Conversion
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Scan Conversion CS123
1 of 44 Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Line Drawing
Draw a line on a raster screen between two points
Why is this a difficult problem?
What is βdrawingβ on a raster display?
What is a βlineβ in raster world?
Efficiency and appearance are both important
Problem Statement
Given two points P and Q in XY plane, both with integer coordinates, determine which pixels on raster screen should be on in order to draw a unit-width line segment starting at P and ending at Q
2 of 44
Scan Converting Lines
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Final step of rasterisation (process of taking geometric shapes and converting them into an array of pixels stored in the framebuffer to be displayed)
Takes place after clipping occurs
All graphics packages do this at the end of the rendering pipeline
Takes triangles and maps them to pixels on the screen
Also takes into account other properties like lighting and shading, but weβll focus first on algorithms for line scan conversion
3 of 44
What is Scan Conversion?
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Special cases: Horizontal Line:
Draw pixel P and increment x coordinate value by 1 to get next pixel.
Vertical Line:
Draw pixel P and increment y coordinate value by 1 to get next pixel.
Diagonal Line:
Draw pixel P and increment both x and y coordinate by 1 to get next pixel.
What should we do in general case?
Increment x coordinate by 1 and choose point closest to line.
But how do we measure βclosestβ?
4 of 44
Finding the next pixel:
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Why can we use vertical distance as a measure of which point is closer?
β¦ because vertical distance is proportional to actual distance
Similar triangles show that true distances to line (in blue) are directly proportional to vertical distances to line (in black) for each point
Therefore, point with smaller vertical distance to line is closest to line
5 of 44
Vertical Distance
(π₯1, π¦1)
(π₯2, π¦2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Basic Algorithm: Find equation of line that connects two points P and Q
Starting with leftmost point , increment π₯π by 1 to calculate π¦π = π β π₯π + B where π = slope, B = y intercept
Draw pixel at (π₯π , Round(π¦π)) where Round (π¦π) = .5 + π¦π
Incremental Algorithm:
Each iteration requires a floating-point multiplication Modify algorithm to use deltas
(π¦π+1 β π¦π) = π β (π₯π+1 β π₯π) + B
π¦π+1 = π¦π + π β (π₯π+1 β π₯π)
If π₯ = π₯π+1 β π₯π = 1, then π¦π+1 = π¦π + π
At each step, we make incremental calculations based on preceding step to find next y value
6 of 44
Strategy 1 β Incremental Algorithm (1/3)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam 7 of 44
Strategy 1 β Incremental Algorithm (2/3)
(π₯π + 1, π ππ’ππ π¦π + π )
(π₯π , π¦π)
(π₯π , π ππ’ππ π¦π )
(π₯π + 1, π¦π + π)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam 8 of 44
Sample Code and Problems (3/3)
void Line(int x0, int y0, int x1, int y1) {
int x, y;
float dy = y1 β y0;
float dx = x1 β x0;
float m = dy / dx;
y = y0;
for (x = x0; x < x1; ++x) {
WritePixel( x, Round(y) );
y = y + m;
}
}
Rounding takes time
Since slope is fractional, need special case for vertical lines (dx = 0)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Assume that lineβs slope is shallow and positive (0 < slope < 1); other slopes can be handled by suitable reflections about principle axes
Call lower left endpoint (π₯0, π¦0) and upper right endpoint (π₯1, π¦1)
Assume that we have just selected pixel π at (π₯π, π¦π)
Next, we must choose between pixel to right (E pixel), or one right and one up (NE pixel)
Let Q be intersection point of line being scan-converted and vertical line π₯ = π₯π +1
9 of 44
Strategy 2 β Midpoint Line Algorithm (1/3)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam 10 of 44
Strategy 2 β Midpoint Line Algorithm (2/3)
Previous pixel
E pixel
NE pixel
Midpoint M Q
π = (π₯π, π¦π)
π₯ = π₯π + 1 (dashed)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Line passes between E and NE
Point that is closer to intersection point π must be chosen
Observe on which side of line midpoint π lies: E is closer to line if midpoint π lies
above line, i.e., line crosses bottom half
NE is closer to line if midpoint π lies below line, i.e., line crosses top half
Error (vertical distance between chosen pixel and actual line) is always β€ .5
11 of 44
Strategy 2- Midpoint Line Algorithm (3/3)
For line shown, algorithm chooses NE as next pixel.
Now, need to find a way to calculate on which side of line midpoint lies
E pixel
NE pixel
π
π
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Line equation as function: π π₯ = π¦ = ππ₯ + π΅ =ππ¦
ππ₯π₯ + π΅
Line equation as implicit function: π π₯, π¦ = ππ₯ + ππ¦ + π = 0 Avoids infinite slopes, provides symmetry between x and y
So from above, π¦ β ππ₯ = ππ¦ β π₯ + π΅ β ππ₯
ππ¦ β π₯ β π¦ β ππ₯ + π΅ β ππ₯ = 0 β΄ π = ππ¦, π = βππ₯, π = π΅ β ππ₯
Properties (proof by case analysis): π π₯π, π¦π = 0 when any point π is on line
π π₯π, π¦π < 0 when any point π is above line
π π₯π, π¦π > 0 when any point π is below line
Our decision will be based on value of function at midpoint π at (π₯π + 1, π¦π + .5)
12 of 44
General Line Equation
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Decision Variable π :
We only need sign of f(π₯π + 1, π¦π + .5) to see where the line lies, and then pick nearest pixel.
π = f(π₯π + 1, π¦π + .5)
if π > 0 choose pixel NE
if π < 0 choose pixel E
if π = 0 choose either one consistently
How do we incrementally update π?
On basis of picking E or NE, figure out location of π for the next pixel, and corresponding value π for next grid line.
We can derive π for the next pixel based on our current decision.
13 of 44
Decision Variable
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Increment M by one in x direction:
ππππ = π π₯π + 1 + π π¦π + .5 + π
ππππ€ = π(π₯π + 2, π¦π + .5)
= π π₯π + 2 + π π¦π + .5 + π
ππππ€ β ππππ is the incremental difference E ππππ€ = ππππ + π β E = π = ππ¦ (2 slides back)
We can compute value of decision variable at next step incrementally without computing F(M) directly ππππ€ = ππππ + E = ππππ + ππ¦
E can be thought of as correction or update factor to take ππππ to ππππ€
It is referred to as forward difference
14 of 44
Incrementing Decision Variable if E was chosen:
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Increment M by one in both x and y directions:
ππππ€ = π π₯π + 2, π¦π + 1.5
= π π₯π + 2 + π π¦π + 1.5 + π
NE = ππππ€ β ππππ
ππππ€ = ππππ + π + π β NE = π + π = ππ₯ β ππ¦
Thus, incrementally,
ππππ€ = ππππ + NE = ππππ + ππ₯ β ππ¦
15 of 44
If NE was chosen:
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
At each step, algorithm chooses between 2 pixels based on sign of decision variable calculated in previous iteration.
It then updates decision variable by adding either E or NE to old value depending on choice of pixel. Simple additions only!
First pixel is first endpoint (π₯0, π¦0), so we can directly calculate initial value of d for choosing between E and NE.
16 of 44
Summary (1/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
First midpoint for first π = ππ π‘πππ‘ is at (π₯0 + 1, π¦0 + .5)
f (π₯0 + 1, π¦0 + .5)
= π π₯0 + 1 + π π¦0 + .5 + π
= ππ₯0 + ππ¦0 + π +π
2+ c
= f π₯0, π¦0 + π +π
2
But π₯0, π¦0 is point on line, so f π₯0, π¦0 = 0
Therefore, ππ π‘πππ‘ = π +π
2= ππ¦ β
ππ₯
2
use ππ π‘πππ‘to choose second pixel, etc.
To eliminate fraction in ππ π‘πππ‘: redefine f by multiplying it by 2; π π₯, π¦ = 2 ππ₯ + ππ¦ + π
This multiplies each constant and decision variable by 2, but does not change sign
Note: this is identical to βBresenhamβs algorithmβ, though derived by different means. That wonβt be true for circle and ellipse scan conversion.
17 of 44
Summary (2/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
void MidpointLine(int x0, int y0, int x1, int y1) { int dx = (x1 - x0), dy = (y1 - y0); int d = 2 * dy - dx; int incrE = 2 * dy; int incrNE = 2 * (dy - dx); int x = x0, y = y0; WritePixel(x, y); while (x < x1) { if (d <= 0) d = d + incrE; // East Case else { d = d + incrNE; ++y; } // Northeast Case ++x; WritePixel(x, y); } }
18 of 44
Example Code
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Version 1: really bad For π₯ from βπ π‘π π :
π¦ = π 2 β π₯2;
WritePixel(round(π₯), round(π¦));
WritePixel(round(π₯), round(βπ¦));
Version 2: slightly less bad For π₯ from 0 to 360:
WritePixel(round(π cos (π₯)), round(π sin (π₯)));
19 of 44
Scan Converting Circles
(17, 0)
(0, 17)
(17, 0)
(0, 17)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam 20 of 44
Version 3 β Use Symmetry
R
(x0 + a, y0 + b)
(x-x0)2 + (y-y0)
2 = R2
(x0, y0)
Symmetry:
If π₯0 + π, π¦0 + π is on circle centered at π₯0, π¦0 :
Then π₯0 Β± π, π¦0 Β± π and π₯0 Β± π, π¦0 Β± π are also on the circle
Hence there is 8-way symmetry
Reduce the problem to finding the pixels for 1/8 of the circle.
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Scan top right 1/8 of circle of radius π
Circle starts at π₯0, π¦0 + π
Letβs use another incremental algorithm with decision variable evaluated at midpoint
21 of 44
Using the Symmetry
(x0, y0)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
x = x0, y = y0 + R; WritePixel(x, y); for (x = x + 1; (x β x0) > (y β y0); x++) { if (decision_var < 0) { // move east update decision variable } else { // move south east update decision variable y--; } WritePixel(x, y); }
Note: can replace all occurrences of π₯0, π¦0with 0, shifting coordinates by βπ₯0, βπ¦0
22 of 44
The incremental algorithm β a sketch
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Decision variable
negative if we move E, positive if we move SE (or vice versa).
Follow line strategy: Use implicit equation of circle
π π₯, π¦ = π₯2 + π¦2 β π 2 = 0
π π₯, π¦ is zero on circle, negative inside, positive outside
If we are at pixel π₯, π¦ examine π₯ + 1, π¦ and π₯ + 1, π¦ β 1
Compute f at the midpoint.
23 of 44
What we need for the Incremental Algorithm
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Evaluate π π₯, π¦ = π₯2 + π¦2 β π 2 at the point:
π₯ + 1, π¦ β1
2
We are asking: βIs π π =
π π₯ + 1, π¦ β1
2= (π₯ + 1)2+(π¦ β
1
2)2βπ 2
positive or negative?β (it is zero on circle)
24 of 44
The Decision Variable
If negative, midpoint inside circle, choose E vertical distance to the circle is less at (π₯ + 1, π¦) than at
π₯ + 1, π¦ β 1
If positive, opposite is true, choose SE
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Decision based on vertical distance
Ok for lines, since d and dvert are proportional
For circles, not true:
π π₯ + 1, π¦ , πΆπππ = π₯ + 1 2 + π¦2 β π
π π₯ + 1, π¦ β 1 , πΆπππ = π₯ + 1 2 + (π¦ β 1)2β π
Which d is closer to zero? (i.e., which value below is closest to R?):
π₯ + 1 2 + π¦2 or π₯ + 1 2 + (π¦ β 1)2
25 of 44
The right decision variable?
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
We could ask instead: βIs π₯ + 1 2 + π¦2 or π₯ + 1 2 + (π¦ β 1)2 closer to π 2?β
The two values in equation above differ by:
π₯ + 1 2 + π¦2 β π₯ + 1 2 + π¦ β 1 2 = 2π¦ β 1
26 of 44
Alternate Phrasing (1/3)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
The second value, which is always less, is closer if its difference from R2 is less than:
1
2(2π¦ β 1)
i.e., if π 2 β π₯ + 1 2 + π¦ β 1 2 <1
2(2π¦ β 1)
then 0 < π¦ β1
2+ π₯ + 1 2 + π¦ β 1 2 β R2
0 < π₯ + 1 2 + π¦2 β 2π¦ + 1 + π¦ β1
2β π 2
0 < π₯ + 1 2 + π¦2 β π¦ +1
2β π 2
0 < π₯ + 1 2 + (π¦ β1
2)2+
1
4β π 2
27 of 44
Alternate Phrasing (2/3)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
The radial distance decision is whether
π1 = π₯ + 1 2 + π¦ β1
2
2
+1
4β π 2
is positive or negative.
The vertical distance decision is whether
π2 = π₯ + 1 2 + π¦ β1
2
2β π 2
is positive or negative; π1and π2 are ΒΌ apart.
The integer π1 is positive only if π2 + ΒΌ is positive (except special case where π2 = 0: remember youβre using integers).
28 of 44
Alternate Phrasing (3/3)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
How can we compute the value of
π π₯, π¦ = π₯ + 1 2 + π¦ β1
2
2β π 2
at successive points? (vertical distance approach)
Answer: Note that π π₯ + 1, π¦ β π π₯, π¦
= ΞπΈ π₯, π¦ = 2π₯ + 3
and that π π₯ + 1, π¦ β 1 β π(π₯, π¦)
= ΞππΈ π₯, π¦ = 2π₯ β 2π¦ + 5
29 of 44
Incremental Computation Revisited (1/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
If we move E, update d = f(M) by adding 2π₯ + 3
If we move SE, update d by adding 2π₯ β 2π¦ + 5
Forward differences of a 1st degree polynomial are constants and those of a 2nd degree polynomial are 1st degree polynomials
this βfirst order forward difference,β like a partial derivative, is one degree lower
30 of 44
Incremental Computation (2/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
The function ΞE π₯, π¦ = 2π₯ + 3 is linear, hence amenable to incremental computation:
ΞE π₯ + 1, π¦ β ΞE π₯, π¦ = 2 ΞE π₯ + 1, π¦ β 1 β ΞE π₯, π¦ = 2
Similarly ΞSE π₯ + 1, π¦ β ΞSE π₯, π¦ = 2
ΞSE π₯ + 1, π¦ β 1 β ΞSE π₯, π¦ = 4
31 of 44
Second Differences (1/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
For any step, can compute new ΞE π₯, π¦ from old ΞE π₯, π¦ by adding appropriate second constant increment β update delta terms as we move. This is also true of ΞSE π₯, π¦ .
Having drawn pixel π, π , decide location of new pixel at π + 1, π or
(π + 1, π β 1), using previously computed Ξ(π, π)
Having drawn new pixel, must update Ξ(π, π) for next iteration; need to find
either Ξ(π + 1, π) ππ Ξ(π + 1, π β 1) depending on pixel choice
Must add ΞπΈ(π, π) or ΞππΈ(π, π) to Ξ(π, π)
So weβ¦ Look at π to decide which to draw next, update π₯ and π¦
Update d using ΞπΈ(π, π) ππ ΞππΈ(π, π)
Update each of ΞπΈ(π, π) πππ ΞππΈ(π, π) for future use
Draw pixel
32 of 44
Second Differences (2/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
MidpointEighthCircle(R) { /* 1/8th of a circle w/ radius R */ int x = 0, y = R; int deltaE = 2 * x + 3; int deltaSE = 2 * (x - y) + 5; float decision = (x + 1) * (x + 1) + (y - 0.5) * (y - 0.5) β R*R; WritePixel(x, y); while ( y > x ) { if (decision > 0) { // Move East x++; WritePixel(x, y); decision += deltaE; deltaE += 2; deltaSE += 2; // Update delta } else { // Move SouthEast y--; x++; WritePixel(x, y); decision += deltaSE; deltaE += 2; deltaSE += 4; // Update delta } } }
33 of 44
Midpoint Eighth Circle Algorithm
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Uses floats!
1 test, 3 or 4 additions per pixel
Initialization can be improved
Multiply everything by 4: No Floats!
Makes the components even, but sign of decision variable remains same
Questions
Are we getting all pixels whose distance from the circle is less than Β½?
Why is y > x the right criterion?
What if it were an ellipse?
34 of 44
Analysis
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Aligned Ellipses
Non-integer primitives
General conics
Patterned primitives
35 of 44
Other Scan Conversion Problems
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Equation is π₯2
π2 +π¦2
π2 = 1 i.e., π2π₯2 + π2π¦2 = π2π2
Computation of ΞπΈ and ΞππΈ is similar
Only 4-fold symmetry
When do we stop stepping horizontally and switch to vertical?
36 of 44
Aligned Ellipses
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
When absolute value of slope of ellipse is more than 1:
How do you check this? At a point π₯, π¦ for which π π₯, π¦ = 0, a vector perpendicular to the curve is π»π π₯, π¦ , which is
[ππ
ππ₯π₯, π¦ ,
ππ
ππ¦π₯, π¦ ]
This vector points more right than up when ππ
ππ₯π₯, π¦ β
ππ
ππ¦π₯, π¦ > 0
37 of 44
Direction Changing Criterion (1/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
In our case, ππ
ππ₯π₯, π¦ = 2π2π₯ and
ππ
ππ¦π₯, π¦ = 2π2π¦
so we check for 2π2π₯ β 2π2π¦ > 0
π2π₯ β π2π¦ > 0
This, too, can be computed incrementally
38 of 44
Direction Changing Criterion (2/2)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Now in ENE octant, not ESE octant
This problem is an artifact of aliasing, remember filter?
39 of 44
Problems with aligned ellipses
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam 40 of 44
Patterned line from P to Q is not same as patterned line from Q to P.
Patterns can be cosmetic or geometric
Cosmetic: Texture applied after transformations
Geometric: Pattern subject to transformations
Cosmetic patterned line
Geometric patterned line
Patterned Lines
P Q
P Q
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam 41 of 44
Geometric vs. Cosmetic
+
Geometric (Perspectivized/Filtered)
Cosmetic (Real-World Contact Paper)
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Non-Integer Primitives
Initialization is harder Endpoints are hard, too
making Line (π, π) and Line (π, π ) join properly is a good test
Symmetry is lost
General Conics
Very hard β the octant-changing test is tougher, the difference computations are tougher, etc. Do it only if you have to
42 of 44
Non-Integer Primitives and General Conics
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
What's the difference between these two solutions? Under which circumstances is the right one "better"?
43 of 44
Generic Polygons
Balsa Demo
Scan Conversion - 10/16/12
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Andries van Dam
Rapid Prototyping is becoming cheaper and more prevalent
3D printers lay down layer after layer to produce solid objects
We have an RP lab across the street in Barus and Holley
http://www.youtube.com/watch?v=ZboxMsSz5Aw
http://www.youtube.com/watch?v=Ah1067HXNdM&NR=1
44 of 44
Scan Converting Arbitrary Solids
Scan Conversion - 10/16/12