Transcript lecture3

강의 내용(세 번째)
• 오늘 강의 내용 (9월 24일)
–
–
–
–
주사변환(Scan conversion)
Bresenham’s Algorithm
다각형의 주사변환
채우기 (filling)
• 예습 : Reading Assignment(10월 1일)
– 계층적모델(p361-p373)
• 숙제
– 없음
7.8 주사 변환
• 래스터화 : 기하학적 개체의 명시로부터
프레임 버퍼에서 화소의 설정
• 가정
– 선분과 다각형만 고려함.
– 각 기본 요소는 화면에 나타날 수 있도록
이미 절단되어 있음.
– 모든 정점들은 2차원으로 투영되었고, 화면
좌표계에서 작업함.
Write_pixel(int ix, int iy, int value);
value : index mode에서 index, RGB mode에서는
32 bit 수 (화소값)
ix, iy : 정수값
그림 7.39 윈도우 좌표계에서의 선분
DDA(Digital Differential Analyzer) 알고리즘
그림 7.40 DDA 알고리즘에 의해 생성된 화소
DDA(Digital Differential Analyzer) 알고리즘
m
y2  y1 y

x2  x1 x
m이 1보다 큰 경우는 대칭성을 이용
0  m 1
y  mx
x  1이므로
y  m
m이 부동 소숫점이므로
반올림이 필요하다.
for (ix=x1, ix<=x2, ix++)
{
y=+m;
write_pixel(x, round(y), line_color);
}
그림 7.41 기울기가 큰 직선과 작은 직선에 의해 생성된 화소들
그림 7.42 수정된 DDA 알고리즘에 의해
생성된 화소
7.9 Bresenham 알고리즘
• DDA는 한번의 부동소수점 덧셈과 반올
림 연산이 필요하다. 이러한 부동솟점
연산을 피하기 위하여 제안된 알고리즘
그림 7.43 Bresenham 알고리즘에 대한 조건
그림 7.44 Bresenham 알고리즘의 결정 변수
결정변수 : d=a-b
d>=0 : 아래점이 선택
d>0 : 윗점이 선택
 2y
d k 1  d k  
2(y  x)
What is d0
if d k  0
otherwise
?
이점: 부동 소수점 연산이 고정 소수점 연산으로 대치된다.
그림 7.45 a 와 b 의 증분
Design of Line and Circle
Algorithms
Basic Line and Circle Algorithms
1. Must compute integer coordinates of pixels which lie on or near a
line or circle.
2. Pixel level algorithms are invoked hundreds or thousands of times
when an image is created or modified.
3. Lines must create visually satisfactory images.
• Lines should appear straight
• Lines should terminate accurately
• Lines should have constant density
• Line density should be independent of line length and angle.
4. Line algorithm should always be defined.
Simple DDA Line Algorithm
{Based on the parametric equation of a line}
Procedure DDA(X1,Y1,X2,Y2 :Integer);
Var Length, I
:Integer;
X,Y,Xinc,Yinc
:Real;
Begin
Length := ABS(X2 - X1);
If ABS(Y2 - Y1) > Length Then
Length := ABS(Y2-Y1);
Xinc := (X2 - X1)/Length;
Yinc := (Y2 - Y1)/Length;
X := X1;
Y := Y1;
For I := 0 To Length Do
Begin
Plot(Round(X), Round(Y));
X := X + Xinc;
Y := Y + Yinc
End {For}
End; {DDA}
DDA creates good lines but it is too time consuming due to the
round function and long operations on real values.
DDA Example
Compute which pixels should be turned on to represent the line
from (6,9) to (11,12).
Length := Max of (ABS(11-6), ABS(12-9)) = 5
Xinc := 1
Yinc := 0.6
Values computed are:
(6,9), (7,9.6),
(8,10.2), (9,10.8),
(10,11.4), (11,12)
13
12
11
10
9
6
7
8
9
10
11
12
13
Fast Lines Using The Midpoint Method
•
Assumptions: Assume we wish to draw a line between points
(0,0) and (a,b) with slope m between 0 and 1 (i.e. line lies in
first octant).
+y
-x
+x
-y
•
The general formula for a line is y = mx + B where m is the
slope of the line and B is the y-intercept. From our
assumptions m = b/a and B = 0.
y = (b/a)x + 0 --> f(x,y) = bx - ay = 0 is an equation for the line.
Fast Lines (cont.)
For lines in the first octant, the next
pixel is to the right or to the right
and up.
T = (x i+ 1, y i+ 1)
(x i +1, y i + 1/2 + e)
e
(x i +1,y i+ 1/2)
Assume:
Distance between pixels centers = 1
P = (x ,y
i )i
S = (x i+ 1, y )i
Having turned on pixel P at (xi, yi), the next pixel is T at (xi+1, yi+1) or S at
(xi+1, yi). Choose the pixel closer to the line f(x, y) = bx - ay = 0.
The midpoint between pixels S and T is (xi + 1,yi + 1/2). Let e be the
difference between the midpoint and where the line actually crosses
between S and T. If e is positive the line crosses above the midpoint and
is closer to T. If e is negative, the line crosses below the midpoint and
is closer to S. To pick the correct point we only need to know the sign of
e.
Fast Lines - The Decision Variable
f(xi+1,yi+ 1/2 + e) = b(xi+1) - a(yi+ 1/2 + e) = b(xi + 1) - a(yi + 1/2) -ae
= f(xi + 1, yi + 1/2) - ae = 0
Let di = f(xi + 1, yi + 1/2) = ae; di is known as the decision variable.
Since a  0, di has the same sign as e.
Algorithm:
If di  0 Then
Choose T = (xi + 1, yi + 1) as next point
di+1
= f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1+1/2)
= b(xi +1+1) - a(yi +1+1/2) = f(xi + 1, yi + 1/2) + b - a
= di + b - a
Else
Choose S = (xi + 1, yi) as next point
di+1
= f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1/2)
= b(xi +1+1) - a(yi +1/2) = f(xi + 1, yi + 1/2) + b
= di + b
Fast Line Algorithm
The initial value for the decision variable, d0, may be calculated directly from
the formula at point (0,0).
d0 = f(0 + 1, 0 + 1/2) = b(1) - a(1/2) = b - a/2
Therefore, the algorithm for a line from (0,0) to (a,b) in the first octant is:
x := 0;
y := 0;
d := b - a/2;
For i := 0 to a do Begin
Plot(x,y);
Else Begin
x := x + 1;
d := d + b
End
End
If d >= 0 Then Begin
x := x + 1;
y := y + 1;
d := d + b - a
End
Note that the only non-integer value is a/2. If we then multiply by 2 to get
d' = 2d, we can do all integer arithmetic using only the operations +, -,
and left shift. The algorithm still works since we only care about the
sign, not the value of d.
Bresenham’s Line Algorithm
•
We can also generalize the algorithm to work for lines beginning
at points other than (0,0) by giving x and y the proper initial
values. This results in Bresenham's Line Algorithm.
Begin {Bresenham for lines with slope between 0 and 1}
a := ABS(xend - xstart);
b := ABS(yend - ystart);
d := 2*b - a;
For I := 0 to a Do Begin
Incr1 := 2*(b-a);
Plot(x,y);
Incr2 := 2*b;
x := x + 1;
If xstart > xend Then Begin
If d  0 Then Begin
x := xend;
y := y + 1;
y := yend
d := d + incr1
End
End
Else Begin
Else
x := xstart;
d := d + incr2
y := ystart
End {For Loop}
End;
End; {Bresenham}
Optimizations
•
Speed can be increased even more by detecting cycles in the
decision variable. These cycles correspond to a repeated
pattern of pixel choices.
•
The pattern is saved and if a cycle is detected it is repeated
without recalculating.
16
15
14
13
12
11
10
9
6
di=
7
8
2
9
-6
11
10
6
12
-2
13 14 15 16 17
10
2
-6
6 -2 10
Fast Circles
•
Consider only the first octant of a circle of radius r centered on the origin. We
begin by plotting point (r,0) and end when x < y.
2
2
x +y -r
•
2
=0
x=y
The decision at each step is whether to choose the pixel directly above the
current pixel or the pixel which is above and to the left (8-way stepping).
Assume Pi = (xi, yi)
is the current pixel.
Ti = (xi, yi +1)
is the pixel directly above
Si = (xi -1, yi +1)
is the pixel above and to the left.
Circle Drawing Algorithm
We only need to calculate the values on the border of the circle in
the first octant. The other values may be determined by
symmetry. Assume a circle of radius r with center at (0,0).
Procedure Circle_Points(x,y :Integer);
Begin
Plot(x,y);
Plot(y,x);
Plot(y,-x);
Plot(x,-y);
(-a,b)
Plot(-x,-y);
Plot(-y,-x);
(-a,-b)
Plot(-y,x);
Plot(-x,y)
End;
(-b,a)
(b,a)
(a,b)
(a,-b)
(-b,-a)
(b,-a)
7.10 다각형의 주사 변환
• 색깔로 채워진 다각형의 주사 변환
• 다각형 색깔 채우기의 경우, 벡터 시스
템은 불가능하였으나, 래스터 시스템은
매우 쉽게 구현이 가능하다.
7.10.1 z-buffer를 이용한 주사변환
• 정점들은 절단 전에 다각형으로 조립된다. 절단 후에
다각형이 남겨진다면, 직교 투영, 은면 제거, 음영법
등이 수행되어야 한다.
• Z-buffer 알고리즘은 세 가지 작업을 동시에 이룰 수
있다.
– 화면 좌표계의 주사선은 정규화 장치 좌표계에서 상수 y의
직선에 대응한다. (그림 7.47, 직교투영)
– 정규화 좌표계의 직선을 사용해서 깊이를 증분적으로 결정
하고, 화면 좌표계의 화소가 보이는 점인지의 여부를 결정한
다.(은면 제거)
– 다각형에 대한 정점 정보를 이용하여, 음영을 계산한다. 그
리고, 정점에 대한 음영을 보간 음영법을 이용하여 보이는
화소의 올바른 화소값을 계산한다(음영법)
그림 7.46 다각형의 두 가지 표현
정규화 장치 좌표계
화면 좌표계
그림 7.47 주사선
정규화장치 좌표계
화면 좌표계
7.10.2 채우기와 정렬
• 일정한 색으로 다각형을 채운다. 점이 다각형
내부에 있다면, 그것을 내부(채우기)색으로
색칠한다.
• 채우기 문제는 모든 화소를 다각형 내부에 있
는 것인가 그렇치 않은 것으로 분류하는 문제
이다.
• Filling
– 범람 채우기(flood fill)
– 주사선 채우기 (scan line fill)
– 홀수-짝수 채우기 (even-odd fill)
7.10.3 범람 채우기
• Bresenham 알고리즘을 이용하여, 다각
형의 변들을 프레임버퍼로 래스터화 함
으로써 채워지지 않는 다각형을 표시한
다. (그림 7.48)
• 다각형 내부에서 초기점(혹은 종자점)을
찾아, 그 이웃을 순환적으로 살펴본다.
이웃이 변의 점이 아니라면(색깔이 배경
색이라면) 전경색으로 칠한다.
flood_fill (int x, int y)
{
if (read_pixel(x,y) == WHITE)
{
write_pixel(x,y,BLACK);
flood_fill(x-1,y);
flood_fill(x+1,y);
flood_fill(x,y-1);
flood_fill(x,y+1);
}
}
그림 7.48 변에 의해 그려진 다각형
전경색
배경색
7.10.4 주사선 알고리즘
• 다각형의 내부를 정의하기 위해서 홀수-짝수 검사를
사용한다면, 주사선 상에서 다각형 내부에 있는 화소
의 그룹인 구간(span)을 형성한다.
• 구간들은 다각형과 주사선의 교차점 집합으로 결정된
다. 교차점 계산은 증분적으로 계산된다. (Edge
Coherence)
• 그림 7.50과 그림 7.51의 비교
• Y-x 알고리즘 : 적절히 선택된 자료구조가 알고리즘
을 빠르게 할 수 있다. 그림 7.52 참조
• Computer Graphics (Hearn & Baker), 3-11 Filled
Area primitives (P117-130 참조)
그림 7.49 구간이 표시된 다각형
그림 7.50 정점 리스트에 의해
생성된 다각형
그림 7.51 원하는 정점의 순서
그림 7.52 y-x 알고리즘에 대한 자료구조
그림 7.53 특이성
홀수-짝수 채우기 정의를 사용한다면,
옆의 두가지 경우를 다르게 계산하여
야 한다. 즉, 꼭지점의 경우를 계산할
때, (a)의 경우는 0번이나 2번으로 계
산하고, (b)의 경우에는 1번으로 계
산하여야 한다.