Upload
willa-cummings
View
247
Download
0
Tags:
Embed Size (px)
Citation preview
Unit – IV 2D Viewing
.
2of30
Contents
Windowing Concepts• The viewing pipeline• viewing coordinate reference frame,• window to view-port coordinate transformation, • viewing functions Line Clipping
– Introduction – Cohen-Sutherland Clipping Algorithm– Cyrus-beck line clipping algorithms
Polygon Clipping– Sutherland-Hodgman Area Clipping Algorithm
3of30
Where we require clipping?
An architect may have a graphics program to draw an entire building but be interested in only the swimming pool.
4of30
Viewing in 2D - Viewport
Window in world coordinates.
45
250
Viewport inDevice coords
250 x 250Pixels.
Display Device
5of30
The computer is used in design applications because it can easily and accurately create, store and modify very complex drawings. When drawings are too complex, however, they become difficult to read. In such situations it is useful to display only those portions of the drawing that are of immediate interest. Sometimes, it is desirable to enlarge these portions to take full advantage of the available display surface.
The method for selecting and enlarging portions of a drawing is called windowing.
The technique for not showing the part of the drawing which one is not interested is called a clipping.
6of30
Windowing I
A scene is made up of a collection of objects specified in world coordinates
World Coordinates
7of30
Window
A world coordinate area selected for display is called a window.
wymax
wymin
wxmin wxmax
Window
World Coordinates
8of30
View port
An area on a display device to which a window is mapped is called a view port.
Vymax
Vymin
Vxmin Vxmax
Device Coordinates
View port
9of30
VIDEOCON
Display Screen
View Port
10of30
The window defines what is to be viewed. And the view port defines where it is to be displayed.
Window and view ports are rectangles in standard positions, with rectangle edges parallel to the coordinate axes.
11of30
Window to viewport mappingor
windowing transformation.
The mapping of a part of a world coordinate scene to device coordinates is referred to as a viewing transformation. 2-d viewing transformation is also referred as window to view port or windowing transformation.
Y wmax
Y wmin
X wminX wmax
window
Y vmax
Y vmin
X vmin X vmax
View port
12of30
The Viewing Pipeline
Window
• A world-coordinate area selected for display.
defines what is to be viewed
Viewport
• An area on a display device to which a window is mapped.
defines where it is to be displayed
Viewing transformation
• The mapping of a part of a world-coordinate scene to device coordinates.
A window could be a rectangle to have any orientation.
13of30
14of30
15of30
2D Viewing Pipeline
To make the viewing process independent of the requirements of any output device, graphics systems convert object descriptions to normalized coordinates range from 0 to 1
16of30
Window to viewing coordinate transformation
First, we construct the scene in world coordinates using the output primitives and attributes. Next, to obtain a particular orientation for the window, we can set up a 2-dimenstional viewing coordinate system in the world coordinate plane and define a window in the viewing coordinate system. The viewing coordinate reference frame is used to provide a method for setting up arbitrary orientation of rectangular windows. Once the viewing reference frame is established, we can transform descriptions in world coordinates to viewing coordinates( in the range from 0 to 1) and map viewing coordinate description of the scene to normalized coordinates. At the final step, all parts of the picture that lie outside the view port are clipped and the contents of the view port are transferred to device coordinates.
17of30
Cntd..
By changing the position of the view port, we can view objects at different positions on the display area of an output device.
Also, by varying the size of view port, we can change the size and proportions of displayed objects.
We achieve zooming effects by successively mapping different sized windows on a fixed size view port. As the windows are made smaller, we zoom in on some part of a scene to view details that are not shown with larger windows. Similarly, more overview is obtained by zooming out from a section of a scene with successively larger windows.
18of30
The Viewing Pipeline
Mwc,vc = R. T
19of30
20of30
21of30
(0,0)
(100,50)
(0,25)
(50,50)
So Sx = 50/100 = ½And Sy = 25/50 = ½
XWmin,YWmin
XWmax,YWmax
XVmin,YVmin
XVmax,YVmax
XVmax-XVmin
XWmax-XWmin
Sx=
YVmax-YVmin
YWmax-YWmin
Sy=
22of30
Window to View port coordinate Transformation
23of30
24of30
Window-to-Viewport Coordinate Transformation
minmax
min
minmax
min
xwxw
xwxw
xvxv
xvxv
minmax
min
minmax
min
ywyw
ywyw
yvyv
yvyv
sxxwxwxvxv )( minmin
syywywyvyv )( minmin minmax
minmax
ywyw
yvyvsy
minmax
minmax
xwxw
xvxvsx
To map a point in the window at world position (xw, yw ) in the associated view port (xv, yv ) we use the concept of relative distance.
25of30
Workstation transformation
Mapping selected parts of a scene in normalized coordinates to different video monitors with workstation transformations
26of30
Clipping Operations
Clipping • Identify those portions of a picture that are either
inside or outside of a specified region of space.
Clip window • The region against which an object is to be
clipped.
• The shape of clip window
Applications of clipping
World-coordinate clipping
27of30
Clipping Operations
Viewport clipping • It can reduce calculations by allowing
concatenation of viewing and geometric transformation matrices.
Types of clipping • Point clipping • Line clipping • Area (Polygon) clipping • Curve clipping • Text clipping
Point clipping (Rectangular clip window)
28of30
Point clipping
Point clipping: Remove points outside window. – A point is either entirely inside the region or notAssuming that the clip window is a rectangle in standard
position, we save a point p=(x,y) for display if the following inequalities are satisfied:
Where the edges of the clip window (xwmin, xwmax, ywmin, ywmax) can be either world coordinate window boundaries or viewport boundaries.
If any one of these four inequalities is not satisfied, the point is clipped (not saved for display)
maxmin
maxmin
yyy
xxx
29of30
(xmin , ymin )
(xmax , ymax )
x = xmin x = xmax
y = ymin
y = ymax
(x1, y1)
cliprectangle
maxmin
maxmin
yyy
xxx
For a point For a point ((x,yx,y) ) to be inside the clip rectangleto be inside the clip rectangle::
Point Clipping
30of30
Line Clipping
Basic calculations:– Is an endpoint inside or outside the clipping window?– Find the point of intersection, if any, between a line
segment and an edge of the clipping window.
Both endpoints inside:
trivial accept
One inside: find
intersection and clip
Both outside: either
clip or reject
31of30
Line Clipping
For the image below consider which lines and points should be kept and which ones should be clipped
wymax
wymin
wxmin wxmax
Window
P1
P2
P3
P6
P5P7
P10
P9
P4
P8
32of30
Point Clipping
Easy - a point (x,y) is not clipped if:
wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
otherwise it is clipped
wymax
wymin
wxmin wxmax
Window
P1
P2
P5
P7
P10
P9
P4
P8
Clipped
Points Within the Window are Not Clipped
Clipped
Clipped
Clipped
33of30
Line Clipping
Harder - examine the end-points of each line to see if they are in the window or not
Situation Solution Example
Both end-points inside the window
Don’t clip
One end-point inside the window, one outside
Must clip
Both end-points outside the window
Must clip
34of30
Cohen-Sutherland Clipping Algorithm
An efficient line clipping algorithm
The key advantage of the algorithm is that it vastly reduces the number of line intersections that must be calculated
Dr. Ivan E. Sutherland co-developed the Cohen-Sutherland clipping algorithm. Sutherland is a graphics giant and includes amongst his achievements the invention of the head mounted display.
35of30
cliprectangle
Cases for clipping linesCases for clipping lines
Line Clipping
36of30
Cases for clipping linesCases for clipping lines
A
B
A
B
cliprectangle
Line Clipping
37of30
Cases for clipping linesCases for clipping lines
D
A
BC
D'
A
BC
D'
cliprectangle
Line Clipping
38of30
Cases for clipping linesCases for clipping lines
D
E
F
A
BC
D'
A
BC
D'
cliprectangle
Line Clipping
39of30
Cases for clipping linesCases for clipping lines
D
E
F
A
BC
D'
G
H
G'
H'A
BC
D'
G'
H'
cliprectangle
Line Clipping
40of30
Cases for clipping linesCases for clipping lines
D
E
F
A
BC
D'
G
H
G'
H'
I
J
I'
J'
A
BC
D'
G'
H'
cliprectangle
Line Clipping
41of30
Cohen-Sutherland: World Division
World space is divided into regions based on the window boundaries
– Each region has a unique four bit region code– Region codes indicate the position of the
regions with respect to the window
1001 1000 1010
00010000
Window0010
0101 0100 0110
42of30
1. End-points pairs are checked for trivial acceptance or rejection using outcode;
2. If not trivially accepted or rejected, divide the line segment into two at a clip edge;
3. Iteratively clipped by test trivial-acceptance or trivial-rejection, and divided into two segments until completely inside or trivial-rejection.
A
B
C
D
E
F
G
H
I1001
0001
0101 0100
0000
1000 1010
0010
0110
Steps for Cohen-Sutherland alg.
43of30
Cohen-Sutherland: Labelling
Every end-point is labelled with the appropriate region code
wymax
wymin
wxmin wxmax
Window
P3 [0001]P6 [0000]
P5 [0000]
P7 [0001]
P10 [0100]
P9 [0000]
P4 [1000]
P8 [0010]
P12 [0010]
P11 [1010]
P13 [0101] P14 [0110]
44of30
Cohen-Sutherland: Lines In The Window
Lines completely contained within the window boundaries have region code [0000] for both end-points so are not clipped
wymax
wymin
wxmin wxmax
Window
P3 [0001]P6 [0000]
P5 [0000]
P7 [0001]
P10 [0100]
P9 [0000]
P4 [1000]
P8 [0010]
P12 [0010]
P11 [1010]
P13 [0101] P14 [0110]
45of30
Cohen-Sutherland: Lines Outside The Window
Any lines with a common set bit in the region codes of both end-points can be clipped
– The AND operation can efficiently check this
wymax
wymin
wxmin wxmax
Window
P3 [0001]P6 [0000]
P5 [0000]
P7 [0001]
P10 [0100]
P9 [0000]
P4 [1000]
P8 [0010]
P12 [0010]
P11 [1010]
P13 [0101] P14 [0110]
46of30
Cohen-Sutherland: Other Lines
Lines that cannot be identified as completely inside or outside the window may or may not cross the window interior
These lines are processed as follows:– Compare an end-point outside the window to a
boundary (choose any order in which to consider boundaries e.g. left, right, bottom, top) and determine how much can be discarded
– If the remainder of the line is entirely inside or outside the window, retain it or clip it respectively
47of30
Cohen-Sutherland: Other Lines (cont…)
– Otherwise, compare the remainder of the line against the other window boundaries
– Continue until the line is either discarded or a segment inside the window is found
We can use the region codes to determine which window boundaries should be considered for intersection
– To check if a line crosses a particular boundary we compare the appropriate bits in the region codes of its end-points
– If one of these is a 1 and the other is a 0 then the line crosses the boundary
48of30
Cohen-Sutherland Examples
Consider the line P9 to P10 below
– Start at P10
– From the region codes of the two end-points we know the line doesn’t cross the left or right boundary
– Calculate the intersection of the line with the bottom boundary to generate point P10’
– The line P9 to P10’ is completely inside the window so is retained
wymax
wymin
wxmin wxmax
Window
P10 [0100]
P9 [0000]
P10’ [0000]
P9 [0000]
49of30
Cohen-Sutherland Examples (cont…)
Consider the line P3 to P4 below
– Start at P4
– From the region codes of the two end-points we know the line crosses the left boundary so calculate the intersection point to generate P4’
– The line P3 to P4’ is completely outside the window so is clipped
wymax
wymin
wxmin wxmax
WindowP4’ [1001]
P3 [0001]
P4 [1000]
P3 [0001]
50of30
Cohen-Sutherland Examples (cont…)
Consider the line P7 to P8 below
– Start at P7
– From the two region codes of the two end-points we know the line crosses the left boundary so calculate the intersection point to generate P7’
wymax
wymin
wxmin wxmax
Window
P7’ [0001]
P7 [0001] P8 [0010]P8’ [0010]
51of30
Cohen-Sutherland Examples (cont…)
Consider the line P7’ to P8
– Start at P8
– Calculate the intersection with the right boundary to generate P8’
– P7’ to P8’ is inside the window so is retained
wymax
wymin
wxmin wxmax
Window
P7’ [0001]
P7 [0001] P8 [0010]P8’ [0010]
52of30
Cohen-Sutherland Worked Example
wymax
wymin
wxmin wxmax
Window
53of30
Calculating Line Intersections
Intersection points with the window boundaries are calculated using the line-equation parameters
– Consider a line with the end-points (x1, y1) and (x2, y2)
– The y-coordinate of an intersection with a vertical window boundary can be calculated using:
y = y1 + m (xboundary - x1)
where xboundary can be set to either wxmin or wxmax
54of30
Calculating Line Intersections (cont…)
– The x-coordinate of an intersection with a horizontal window boundary can be calculated using:
x = x1 + (yboundary - y1) / m
where yboundary can be set to either wymin or wymax
– m is the slope of the line in question and can be calculated as m = (y2 - y1) / (x2 - x1)
55of30
Initial ConditionInitial Condition
Sutherland-Hodgeman Algo.
Clip Against Right Clipping Clip Against Right Clipping BoundaryBoundary
Clip Against Top Clipping Clip Against Top Clipping BoundaryBoundary
The Clipped PolygonThe Clipped PolygonClip Against Bottom Clipping Clip Against Bottom Clipping BoundaryBoundary
Clip Against Left Clipping Clip Against Left Clipping BoundaryBoundary
56of30
Polygon ClippingPolygon Clipping
v1 v2v1’
out in
Output: v1’ and v2
v1
v2
in in
Output: v2
v1v2 v1’
in out
Output: v1’
v1
v2
out out
Output: none
outside inside
Sutherland-Hodgeman Polygon Clipping against a rectangle (or a window)
For each stage, the boundary clipper divides the plane into an inside half-planeAnd outside half-plane
Inside half-plane contains the clipping window; Outside half-plane doesn’t.
Given an edge going from vertex V[i] to vertex V[i+1], during the transversal, there are four cases (assuming the V[i] has been checked in the previous edge):
57of30
Case 1Case 1
Inside Outside
Polygonbeingclipped
Clipboundary
s
p:output
4 Cases of Polygon Clipping
Case 2Case 2
Inside Outside
s
p
i:output
Case 3Case 3
Inside Outside
s
p
(no output)
Case 4Case 4
Inside Outside
s
i:first output
p:second output
58of30
Cohen-Sutherland – polygons
s
p
Inside Outside
s
p
Inside Outside
s
pInside Outside
s
p
Inside Outside
1st Output
i
Output No Output
i
2nd Output
Output
59of30
Area Clipping
Similarly to lines, areas must be clipped to a window boundary
Consideration must be taken as to which portions of the area must be clipped
60of30
Sutherland-Hodgman Area Clipping Algorithm
A technique for clipping areas developed by Sutherland & Hodgman
Put simply the polygon is clipped by comparing it against each boundary in turn
Original Area Clip Left Clip Right Clip Bottom Clip Top
Sutherland turns up again. This time with Gary Hodgman with whom he worked at the first ever graphics company Evans & Sutherland
61of30
Sutherland-Hodgman Area Clipping Algorithm (cont…)
To clip an area against an individual boundary:– Consider each vertex in turn against the
boundary– Vertices inside the boundary are saved for
clipping against the next boundary– Vertices outside the boundary are clipped– If we proceed from a point inside the boundary
to one outside, the intersection of the line with the boundary is saved
– If we cross from the outside to the inside intersection point and the vertex are saved
62of30
Sutherland-Hodgman Example
Each example shows the point being processed (P) and the previous point (S)
Saved points define area clipped to the boundary in question
S
P
Save Point P
S
P
Save Point I
I
P
S
No Points Saved
S
P
Save Points I & P
I
63of30
Other Area Clipping Concerns
Clipping concave areas can be a little more tricky as often superfluous lines must be removed
Clipping curves requires more work– For circles we must find the two intersection
points on the window boundary
Window WindowWindow Window
64of30
Summary
Objects within a scene must be clipped to display the scene in a window
Because there are can be so many objects clipping must be extremely efficient
The Cohen-Sutherland algorithm can be used for line clipping
The Sutherland-Hodgman algorithm can be used for area clipping
65of30
66of30
Cohen-Sutherland Clipping Algorithm VI
Let’s consider the lines remaining below
wymax
wymin
wxmin wxmax
Window
P3 [0001]P6 [0000]
P5 [0000]
P7 [0001]
P10 [0100]
P9 [0000]
P4 [1000]
P8 [0010]
P12 [0010]
P11 [1010]
P13 [0101] P14 [0110]
67of30
Cohen-Sutherland Clipping Algorithm
Easy - a point (x,y) is not clipped if:
wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
otherwise it is clipped
wymax
wymin
wxmin wxmax
Window
P1
P2
P5
P7
P10
P9
P4
P8
Clipped
Points Within the Window are Not Clipped
Clipped
Clipped
Clipped
68of30
Clipping
Point clipping is easy:– For point (x,y) the point is not clipped if
wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
wymax
wymin
wxmin wxmax
Before Clipping
Window
P1
P2
P3
P6
P5P7
P10
P9
P4
P8
69of30
Cohen-Sutherland – polygons
s
p
Inside Outside
s
p
Inside Outside
s
pInside Outside
s
p
Inside Outside
1st Output
i
Output No Output
i
2nd Output
Output
Polygon Clipping
.
71of30 Ex: Simple Convex
72of30
Ex: Multiple Components
73of30 Nonconvex with Many Edges
74of30
Polygons : Before Clipping
75of30
Polygons: Clip on Top
Top ClipBoundary
76of30
Polygons: Clip on Right
Right ClipBoundary
77of30
Polygons: Clip on Bottom
Bottom ClipBoundary
78of30
Polygons: Clip on Left
Left ClipBoundary
79of30
Polygons: Clipped Polygon
80of30
Begin Animated Recap
81of30
Polygons: Before Clipping
82of30
Polygons: Clip on Top
Top ClipBoundary
83of30
Polygons: Clip on Top
Top ClipBoundary
84of30
Polygons: Clip on Right
Right ClipBoundary
85of30
Polygons: Clip on Right
Right ClipBoundary
86of30
Polygons: Clip on Bottom
Bottom ClipBoundary
87of30
Polygons: Clip on Bottom
Bottom ClipBoundary
88of30
Polygons: Clip on left
Left ClipBoundary
89of30
Polygons: Clip on left
Left ClipBoundary
90of30
Polygons: Fully Clipped
91of30
Polygons: Clip on Left
Left ClipBoundary
92of30
Polygons: Clipped Polygon
93of30
End Animated Recap
Hodgeman-Sutherland
95of30 Polygons: Case 1Polygons: Case 1
INOUT
Si output
P
Case 1: S out; P in
Action: Output i and P
97of30 Polygons: Case 2Polygons: Case 2
(no output)
P
S
INOUT
Case 2: S & P both in
Action: output p
99of30 Polygons: Case 3Polygons: Case 3
P: second output
i: first output
S
INOUT
Case 3: S IN; P OUT
Action: Output i
101of30 Polygons: Case 4Polygons: Case 4
Polygonbeing clipped
P: output Clip boundary
S
INOUT
Case 4: S & P both out
Action: None
103of30
Case 2: S & P both IN
Action: Output P
Case 3: S IN; P OUT
Action: Output i
(S already output
under Case 1)
Case 4: S & P both OUT
Action: None
Case 1: S OUT; P IN
Action: Output i and P
104of30 Hodgeman-Sutherland
v1 v2
v3
v4
v5 v6
v7v8
105of30
Hodgeman-Sutherland
v1 v2
v3
v4
v5 v6
v7v8
106of30
Hodgeman-Sutherland: Top
2
v3
v5 v6
v7v8
34
1
107of30
Hodgeman-Sutherland: Top
)( 87654321 vvvvvvvv
)( 875 643321 vvvvv
108of30 Hodgeman-Sutherland: Bottom
2
v3
v5 v6
v7v8
34
1
109of30 Hodgeman-Sutherland: Bottom
2
v3
v5 v6
65
34
1
110of30 Hodgeman-Sutherland: Bottom
)( 875 643321 vvvvv
)( 665433215 vvv
111of30
Hodgeman-Sutherland: Right
2
v3
v5 v6
65
34
1
112of30
)( 665433215 vvv
)( 665433215 vvv
This case is trivial: do nothing
Hodgeman-Sutherland: Right
113of30
Hodgeman-Sutherland: Left
2
v3
v5 v6
65
34
1
114of30
Hodgeman-Sutherland: Left
2
v3
7
v6
39
8
6
10
115of30
Hodgeman-Sutherland: Left
)( 665433215 vvv
)( 5610933287 vv
116of30
Hodgeman-Sutherland: Done
117of30
Begin Animated Recap
118of30
v1 v2
v3
v4
v5 v6
v7v8
1 2 3 4 5 6 7 8( )v v v v v v v v
119of30 ( )1)(
v1 v2
v3
v5 v6
v7v8
v4
2 3)v
341
3) 4 5)v 6)v 7)v 8)v Done
Top21
121of30 ( ) 15( ) 2) 3)v 3) 4) 5)v 6)v 6)
v3
v5 v6
v7v8
2341
5 6
Done
Bottom
122of30 7( )
v3
v5 v6
327 8( )v ( )
2341
37 28 3( )v 32 37 8 9 10( )v 3 62 37 8 9 10( )v v 3 5 62 3 45 61( )v v v
5
7 8 2( )
6
37 8 92 3( )v
7
89
10
Nothing needs to be clipped against Right
123of30
v3
v5 v6
234
67
89
10
3 5 62 3 45 61( )v v v
124of30
End Animated Recap
125of30
Hodgeman-Sutherland: Done
126of30 Hodgeman-Sutherland Problem
Extra Lines
127of30
Correct Result
How to Fix Up?
128of30 Distinguish Between Case 2Distinguish Between Case 2
Si output
P
IN OUT
129of30
And, Case 4And, Case 4
P: second output
i: first output
S
IN OUT
130of30
Distingish Between Case 2Distingish Between Case 2
SP
is output
IN OUT
131of30
And, Case 4And, Case 4
P: second output S
is output first
IN OUT
132of30
Extended H-S Algorithm
Tag the different new vertices
Call the one generated on transition from IN to OUT
Call the one generated on transition from OUT to IN
Starting from OUT, connect as soon as is generated
and
133of30
Example of Extended Algo
IN OUT
134of30
Result
IN OUT
135of30
Extended H-S Algorithm
Connect and as soon as the . is generated
Generate next polygon beginning with next , i.e.,
( ,....)
Another Polygon Clipping Algorithm
Weiler-Atherton Clipping
137of30
Another approach to polygon clipping
No extra clipping outside window
Works for arbitrary shapes
Avoids degenerate polygons
Weiler-Atherton Polygon Clipping
138of30
Weiler-Atherton Clipping
13
11
12 6
5
414
3
2
79
810
1
Clip Polygon (ep)
Subject Polygon (sp)
139of30 Gives “Right” Answer
140of30
Start at first (inside) vertexTraverse polygon until hitting a window boundary
Output intersection point iTurn rightFollow window boundary until next intersection
Weiler-Atherton Clipping
141of30
Output second intersection
Turn right, again, and follow subject polygon until closed
Continue on subject polygon from first intersection point.
Repeat processing until complete
Weiler-Atherton Clipping
142of30
Approximate description
Can be improved
Weiler-Atherton Clipping
143of30 Generalizations of W-A
Can be extended to complex situations,
arbitrary windows
Stability issues can arise for such cases
General Boolean Operations
via Boundary Representations
(B-reps)
General Clipping
with abitrary polygons
146of30 General Boolean Operations
A
B
147of30
A
B
BA in
148of30
B
BA in
149of30 BA in
A
B
150of30 BA in
151of30
A
B
A
AB in
152of30
A
AB in
153of30 AB in
154of30
BA out
A
B
155of30
A
B
BA out
156of30
BA out
157of30
A
B
AB out
158of30
AB out
159of30 BA on
B
on
160of30
BA on
161of30
AB on
A
B
on
162of30
AB on
163of30 ((anti-)shared )
anti-sharedshared
B A
164of30
A
B
BA
165of30
A B
n BAo
BAin
n AB i
166of30
A
B
BA
167of30 BA
n BAo BAout
t AB ou
168of30
A
B
BA
169of30 BAout n ABi BA
The End
Polygon Clipping
Lect
ure
Set
5