Ray Polygon Interception: Cyrus Beck Algorithm Mengxia Zhu Fall 2007 Polygon and Polyhedron Polygon and Polyhedron are fundamental objects in 2D or 3D graphics. In 2D, straight lines make a polygon, in 3D, an object if often modeled as a polygonal “mesh”: a collection of polygons that fit together. If the skin forms a closed surface, the mesh is called polyhedron. P Ray Polygon Intersection In order to analyze or render them, we usually need to solve the intersection problem. The general case of intersecting a line with a polygon and polyhedron is quite complex. We study convex forms with bounding lines or bounding planes first. Solve how a ray intersects a convex polygon or convex polyhedron is an essential technique in viewing 3D objects. Is a given point P inside or outside the object Where does a given ray first intersect the object Which part of a given line lies inside the object? Ray Casting Ray Tracing examples www.geocities.com/.../ coolhead1small2rot.png www-evasion.imag.fr/. ../ecs177.html Parametric Line Revisited P1 P̂ P0 t k Pˆ k P0 t P1 k t 1 k,t 0 •P0 is the starting point •P1 another point on the line •P linear interpolation between P0 and P1 •Affine combination, Convex combination Pˆ (1 t ) P0 t P1 P0 t ( P1 P0 ) 0 t 1 Dot Product Revisited V1 V2 V1 V2 V1 V2 cos Supposethat V1 0 and V2 0. Then, V1 V2 0 V1 V2 0 2 2 V1 V2 0 0 2 Point inside or outside? P1 n outward normal insdie half-space outside half-space P2 f n P3 W( ) n ( P3 f ) 0 P3 n ( P2 f ) 0 P2 n ( P1 f ) 0 P1 W ( ) Direction for Ray that Strikes Plane r n r nr 0 nr 0 nr 0 the ray is aimed “along with” the normal the ray is parallel to the plane the ray is aimed “counter to” the normal Cyrus-Beck Algorithm Developed by Cyrus and Beck in 1978 Clip line against convex polygon/polyhedron Completely described by a set of “bounding lines or planes” Find a value of t for intersection of line and clipping edges. Use simple comparison of t values to find actual clipping segment. Where a Ray intersects a line(2D) or plane(3D) A point normal form of edge or plane Outside P(t) Nl ( P Pl ) 0 Inside A parametric equation of line: P(t ) P0 t ( P1 P0 ) Find t such that PL N l [ P t Pl ] 0 c ( P1 P0 ) N l [ P0 tc Pl ] 0 N l ( P0 Pl ) N l ct 0 t N l ( Pl P0 ) / N l c NL Cyrus-Beck Algorithm (Cont’) Classify the relative position of the ray to intercepting edge or plane If Nl ( P1 P0 ) 0: ray parallel to the edge of line or plane If Nl ( P1 P0 ) 0 : ray exiting polygon If Nl ( P1 P0 ) 0 : ray entering polygon Entering n P0 P1 Exiting Each Bounding Line or Plane We should find: The hit time of the ray with the bounding line or bounding plane N l [ P t Pl ] 0 c ( P1 P0 ) N l [ P0 tc Pl ] 0 N l ( P0 Pl ) N l ct 0 t N l ( Pl P0 ) / N l c Whether the ray is entering or exiting the polygon or polyhedron. Cyrus-Beck Algorithm (Cont’) Compute PE with largest t: max (tin, 0) Compute PL with smallest t: min ( tout, 1) P1 Clip to these two points PL PL PE PE P0 Cyrus-Beck Algorithm (Cont’) Initialize the candidate interval to [0,1] For each bounding line, find the hit time of thit and determine whether it is an entering or exiting hit If t_hit is an entering hit, set t_in = max (tin, thit) If t_hit is an exiting hit, set t_out = min (tout, thit) If at any point tin becomes greater than t_out, the ray misses P entirely, and testing is terminated. If the candidate interval is not empty, then the segment between tin and tout lies inside P. Clipping Against Arbitrary Polygons Polygon is no longer bounded by a collection of infinite bounding lines in point normal form. We have to work with N finite segments P2 P1 Clipping Against Arbitrary Polygons Represent edge of P parametrically For example, segment P3P4 is represented as: P3 + e3*u, where e3=P4-P3, u ranges from 0 to 1. Ray intersection with above segment is represented as: R + c*t = P3 + e3*u Find all of the hits of the ray with all edges of P and place them in a list of the hit times. The smallest value of t in hit-list is the first hit Take the t values in pairs. The ray enters P at the first time in each pair and exits from P at the second time of each pair Exercises Draw an icosahedron by using the following drawing routine to use vertex arrays. Icosahedron approximate a sphere by using 20 polygons. After finish it, try to use recursive method to further divide each triangle to get more triangles to better approximate the sphere. #define x .525731112119133606 #define z .850650808352039932 void display (void) {static GLfloatv data[12][3] = { {-x,0.0,z},{x,0.0,z}, {-x,0.0,-z},{x,0.0,z},{0.0,z,x},{0.0,z,-x},{0.0,-z,x}, {0.0,-z,-x},{z,x,0.0},{-z,x,0.0},{z,-x,0.0},{-z,-x,0.0} }; static GLuinttindices[20][3] = { {0,4,1},{0,9,4},{9,5,4},{4,5,8},{4,8,1},{8,10,1},{8,3,10},{5,3,8},{5,2,3},{2,7,3},{7,10,3},{7, 6,10},{7,11,6},{11,0,6},{0,1,6},{6,1,10},{9,0,11},{9,11,2},{9,2,5}, {7,2,11} }; Exercises Implement a 2D/3D Cyrus-Beck Algorithm for convex polygon or polyhedron by prompt user to input a ray and a sequence of bounding lines/bounding plane. Output the enter point and exit point. Exercises for (i = 0; i < 20; i++) { glVertex3fv(&vdata[tindices[i][0]][0]); glVertex3fv(&vdata[tindices[i][1]][0]); glVertex3fv(&vdata[tindices[i][2]][0]); }