Geometric Algorithms. Geometric search: overview. 1D Range Search. 1D Range Search Implementations

Similar documents
! Good algorithms also extend to higher dimensions. ! Curse of dimensionality. ! Range searching. ! Nearest neighbor.

Geometric Search. range search space partitioning trees intersection search. range search space partitioning trees intersection search.

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search kd trees line segment intersection interval search trees rectangle intersection

Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Announcements. Written Assignment2 is out, due March 8 Graded Programming Assignment2 next Tuesday

Spatial Data Structures

Spatial Data Structures

Algorithms for GIS:! Quadtrees

UNIVERSITY OF WATERLOO Faculty of Mathematics

Speeding Up Ray Tracing. Optimisations. Ray Tracing Acceleration

Spatial Data Structures

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

Spatial Data Structures

CMSC 754 Computational Geometry 1

Module 8: Range-Searching in Dictionaries for Points

Ray Tracing Acceleration Data Structures

Ray Tracing III. Wen-Chieh (Steve) Lin National Chiao-Tung University

Multidimensional Indexing The R Tree

Spatial Data Structures

Computer Graphics. Bing-Yu Chen National Taiwan University

Mesh Generation. Quadtrees. Geometric Algorithms. Lecture 9: Quadtrees

Lecture 25 of 41. Spatial Sorting: Binary Space Partitioning Quadtrees & Octrees

Geometric Structures 2. Quadtree, k-d stromy

CMSC 425: Lecture 10 Geometric Data Structures for Games: Index Structures Tuesday, Feb 26, 2013

Ray Tracing with Spatial Hierarchies. Jeff Mahovsky & Brian Wyvill CSC 305

Computer Graphics II

Lecture 11: Ray tracing (cont.)

Advanced 3D-Data Structures

CS535 Fall Department of Computer Science Purdue University

Collision and Proximity Queries

Spatial Data Structures and Speed-Up Techniques. Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Solid Modeling. Thomas Funkhouser Princeton University C0S 426, Fall Represent solid interiors of objects

Computer Graphics (CS 543) Lecture 13b Ray Tracing (Part 1) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

1. Meshes. D7013E Lecture 14

1 The range query problem

3D Representation and Solid Modeling

Accelerating Geometric Queries. Computer Graphics CMU /15-662, Fall 2016

Identifying those parts of a scene that are visible from a chosen viewing position, and only process (scan convert) those parts

Ray Tracing: Intersection

Collision Detection. These slides are mainly from Ming Lin s course notes at UNC Chapel Hill

CPSC / Sonny Chan - University of Calgary. Collision Detection II

Spatial Data Management

Computational Geometry

Spatial Data Management

Module 4: Index Structures Lecture 16: Voronoi Diagrams and Tries. The Lecture Contains: Voronoi diagrams. Tries. Index structures

improving raytracing speed

Intersection Acceleration

Indexing. Week 14, Spring Edited by M. Naci Akkøk, , Contains slides from 8-9. April 2002 by Hector Garcia-Molina, Vera Goebel

Accelerated Raytracing

January 10-12, NIT Surathkal Introduction to Graph and Geometric Algorithms

Scene Management. Video Game Technologies 11498: MSc in Computer Science and Engineering 11156: MSc in Game Design and Development

I/O-Algorithms Lars Arge Aarhus University

Principles of Data Management. Lecture #14 (Spatial Data Management)

Computer Graphics. - Rasterization - Philipp Slusallek

Speeding up your game

CS S Lecture February 13, 2017

Homework 1: Implicit Surfaces, Collision Detection, & Volumetric Data Structures. Loop Subdivision. Loop Subdivision. Questions/Comments?

Organizing Spatial Data

Acceleration Data Structures

Computer Graphics. - Ray-Tracing II - Hendrik Lensch. Computer Graphics WS07/08 Ray Tracing II

Computing Visibility. Backface Culling for General Visibility. One More Trick with Planes. BSP Trees Ray Casting Depth Buffering Quiz

Space-based Partitioning Data Structures and their Algorithms. Jon Leonard

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

COMP 175: Computer Graphics April 11, 2018

Ray Tracing. Cornell CS4620/5620 Fall 2012 Lecture Kavita Bala 1 (with previous instructors James/Marschner)

CSG obj. oper3. obj1 obj2 obj3. obj5. obj4

Simulation in Computer Graphics Space Subdivision. Matthias Teschner

Spatial data structures in 2D

Lecture 17: Solid Modeling.... a cubit on the one side, and a cubit on the other side Exodus 26:13

Computational Geometry. Lecture 17

3D Modeling: Solid Models

Advanced Data Types and New Applications

CMPS 3130/6130 Computational Geometry Spring Voronoi Diagrams. Carola Wenk. Based on: Computational Geometry: Algorithms and Applications

Computational Geometry

9. Visible-Surface Detection Methods

INFOGR Computer Graphics. J. Bikker - April-July Lecture 11: Acceleration. Welcome!

More Hidden Surface Removal

Spatial Data Structures and Speed-Up Techniques. Ulf Assarsson Department of Computer Science and Engineering Chalmers University of Technology

On the data structures and algorithms for contact detection in granular media (DEM) V. Ogarko, May 2010, P2C course

Collision Detection based on Spatial Partitioning

Graphics 2009/2010, period 1. Lecture 8: ray tracing

CS210 Project 5 (Kd-Trees) Swami Iyer

MODELING AND HIERARCHY

Renderer Implementation: Basics and Clipping. Overview. Preliminaries. David Carr Virtual Environments, Fundamentals Spring 2005

1/60. Geometric Algorithms. Lecture 1: Introduction. Convex Hulls

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

CS 532: 3D Computer Vision 14 th Set of Notes

High Dimensional Indexing by Clustering

Advanced Data Types and New Applications

Visible Surface Detection Methods

6.854J / J Advanced Algorithms Fall 2008

Computational Geometry. Algorithm Design (10) Computational Geometry. Convex Hull. Areas in Computational Geometry

Lecture 3 February 9, 2010

Orthogonal Range Queries

Orthogonal range searching. Orthogonal range search

Lecture 2 - Acceleration Structures

Ranking. In an ordered set or a dictionary: Rank of key k is i iff k is the ith smallest key. Example: if s stores these keys 42, 55, 61, then:

Transcription:

Geometric search: overview Geometric Algorithms Types of data:, lines, planes, polygons, circles,... This lecture: sets of N objects. Range searching Quadtrees, 2D trees, kd trees Intersections of geometric objects Geometric problems extend to higher dimensions. Good algorithms also extend to higher dimensions. Basic problems. Range searching. Nearest neighbor. Finding intersections of geometric objects. Princeton University COS 226 Algorithms and Data Structures Spring 2004 Kevin Wayne http://www.princeton.edu/~cos226 2 1D Range Search 1D Range Search Implementations Extension to symbol-table ADT with comparable keys. Insert key-value pair. Search for key k. How many records have keys between k 1 and k 2? Iterate over all records with keys between k 1 and k 2. Application: database queries. Geometric intuition. Keys are point on the line. How many in a given interval? insert B B insert D B D insert A A B D insert I A B D I insert H A B D H I insert F A B D F H I insert P A B D F H I P count G to K 2 search G to K H I Range search: how many records have keys between k 1 and k 2? Ordered array. Slow insert, binary search for k 1 and k 2 to find range. Hash table. No reasonable algorithm (key order lost in hash). BST. In each node x, maintain number of nodes in tree rooted at x. Search for smallest element k 1 and largest element k 2. insert count range ordered array N log N R + log N hash table 1 N N BST log N log N R + log N N = # records R = # records that match nodes examined within interval not touched 3 4

2D Range Search 2D Range Search Grid Implementation Extension to symbol-table ADT with 2D keys. Insert a 2D key. Search for a 2D key. Range search: find all keys that lie in a 2D range? Applications: networking, circuit design, databases. Geometric interpretation. Keys are point in the plane. Find all in a given h-v rectangle? Grid implementation. (Sedgewick 3.18) Divide space into M-by-M grid of squares. Create linked list for each square. Use 2D array to directly access relevant square. Insert: insert (x, y) into corresponding grid square. Range search: examine only those grid squares that could have in the rectangle. RT 5 LB 6 2D Range Search Grid Implementation Costs Clustering Space-time tradeoff. Space: M 2 + N. Time: 1 + N / M 2 per grid cell examined on average. Grid implementation. Fast, simple solution for well-distributed. Problem. Clustering is a well-known phenomenon in geometric data. Choose grid square size to tune performance. Too small: wastes space. Too large: too many per grid square. rule of thumb: N by N grid. RT Time costs. Initialize: O(N) to initialize 2D array of lists. Insert: O(1). assumes are evenly distributed Range: O(1) per point in range. LB Ex: USA map data. 80,000, 20,000 grid squares. Half the grid squares are empty. Half the have 10 others in same grid square. Ten percent have 100 others in same grid square. Need data structure that gracefully adapts to data. 7 8

Space Partitioning Trees Quad Trees Space partitioning tree. Use a tree to represent the recursive hierarchical subdivision of d-dimensional space. BSP tree. Recursively divide space into two regions. Quadtree. Recursively divide plane into four quadrants. Octree. Recursively divide 3D space into eight octants. kd tree. Recursively divide k-dimensional space into two half-spaces. Applications. Ray tracing. Flight simulators. N-body simulation. Collision detection. Astronomical databases. Adaptive mesh generation. Accelerate rendering in Doom. Hidden surface removal and shadow casting. Grid Quadtree kd tree BSP tree Quad tree. Recursively partition plane into 4 quadrants. Implementation: 4-way tree. a e f g c b h d Good clustering performance is a primary reason to choose quad trees over grid methods. a b c public class Quadtree { Quad quad; Object value; Quadtree NW, NE, SW, SE; } d e f g h 9 10 Curse of Dimensionality 2D Trees Range search / nearest neighbor in k dimensions? Main application. Multi-dimensional databases. 3D space. Octrees: recursively divide 3D space into 8 octants. 100D space. Centrees: recursively divide into 100 centrants??? 2D tree. Recursively partition plane into 2 halfplanes. Implementation: BST, but alternate using x and y coordinates as key. Search gives rectangle containing point. Insert further subdivides the plane. p left of p right of p even levels p Raytracing with octrees http://graphics.cs.ucdavis.edu/~gregorsk/graphics/275.html left of p right of p odd levels 11 12

kd Trees Geometric Intersection kd tree. Recursively partition k-dimensional space into 2 halfspaces. Implementation: BST, but cycle through dimensions ala 2D trees. Problem: find all intersecting pairs among set of N geometric objects. Applications: CAD, games, movies, virtual reality. p Simple version: 2D, all objects are horizontal or vertical line segments. whose i th coordinate is less than p s level i (mod k) whose i th coordinate is greater than p s Efficient, simple data structure for processing k-dimensional data. Adapts well to high dimensional data. Adapts well to clustered data. Discovered by an undergrad in an algorithms class! Brute force: test all (N 2 ) pairs of line segments for intersection. Sweep line: efficient solution extends to 3D and general objects. 13 14 Orthogonal Segment Intersection: Sweep Line Algorithm Orthogonal Segment Intersection: Sweep Line Algorithm Use horizontal sweep line moving from left to right. Sweep line: sort segments by x-coordinate and process in this order. Left endpoint of h-segment: insert y coordinate into ST. Right endpoint of h-segment: remove y coordinate from ST. v-segment: range search for interval of y end. Sweep line reduces 2D orthogonal segment intersection problem to 1D range searching! Running time of sweep line algorithm. Sort by x-coordinate. O(N log N) Insert y-coordinate into ST. O(N log N) Delete y-coordinate from ST. O(N log N) Range search. O(R + N log N) N = # line segments R = # intersections Efficiency relies on judicious use of data structures. insert y delete y range search 15 16

Line Segment Intersection: General Version Line Segment Intersection: General Version Use horizontal sweep line moving from left to right. Maintain order of segments that intersect sweep line by y-coordinate. Intersections can only occur between adjacent segments. Add/delete line segment one new pair of adjacent segments. Intersection two new pairs of adjacent segments. Efficient implementation of sweep line algorithm. Maintain PQ of important x-coordinates end and intersections. Maintain ST of segments intersecting sweep line, sorted by y. O(R log N + N log N). A A B B C C A A B D A B C A C B order of segments A C D C A D insert segment delete segment intersection A A B D A B C A C B order of segments A C D C A D insert segment delete segment intersection 17 18 Algorithms and Moore's Law Algorithms and Moore's Law VLSI database problem: Find all intersections among h-v rectangles. Application: microprocessor design. Early 1970s: microprocessor design became a geometric problem Very Large Scale Integration (VLSI). Computer-Aided Design (CAD). Design-rule checking. Moore s Law: processing power doubles every 18 months. 197x: need to check N rectangles. 197(x+1.5): need to check 2N rectangles on a 2x-faster computer. Quadratic algorithm: compare each rectangle against all others. 197x: takes M days. 197(x+1.5): takes (4M)/2 = 2M days. (!!) Need O(N log N) CAD algorithms to sustain Moore s Law. 19 20

VLSI Database Problem Interval Search Trees Move a vertical "sweep line" from left to right. Sweep line: sort rectangles by x-coordinate and process in this order. Maintain data structure of intervals intersecting sweep line. Key operation: given a new interval, does it intersect an existing one? (7, 10) (17, 19) (5, 11) (15, 18) (4, 8) (21, 23) Support following operations. Insert an interval (lo, hi). Delete the interval (lo, hi). Search for an interval that overlaps (lo, hi). Non-degeneracy assumption. No rectangles share same x-coordinate. 21 22 Interval Search Trees Finding an Overlapping Interval Interval tree implementation with BST. (7, 10) (5, 11) BST nodes contain interval. BST sorted on lo endpoint. Additional info: store max endpoint in subtree rooted at node. (15, 18) (17, 19) (4, 8) (21, 23) (17, 19) 23 (5, 11) 18 (21, 23) 23 Search for an interval that overlaps I = (lo, hi). x = root; while (x!= null) { if (x.interval.overlaps(lo, hi)) return x.interval; if (x.left == null) x = x.right; else if (x.left.max < lo) x = x.right; else x = x.left; } return null; (17, 19) 23 (5, 11) 18 (21, 23) 23 (4, 8) 8 (15, 18) 18 max in subtree (4, 8) 8 (15, 18) 18 key interval (7, 10) 10 (7, 10) 10 24 25

Finding an Overlapping Interval Finding an Overlapping Interval Search for an interval that overlaps I = (lo, hi). Search for an interval that overlaps I = (lo, hi). x = root; while (x!= null) { if (x.interval.overlaps(lo, hi)) return x.interval; if (x.left == null) x = x.right; else if (x.left.max < lo) x = x.right; else x = x.left; } return null; x = root; while (x!= null) { if (x.interval.overlaps(lo, hi)) return x.interval; if (x.left == null) x = x.right; else if (x.left.max < lo) x = x.right; else x = x.left; } return null; Case 1 (right). If search goes right, then there exists an overlap in right subtree or no overlap in either. Case 2 (left). If search goes left, then there exists an overlap in left subtree or no overlap in either. Proof. Suppose no overlap in right. (x.left == null) no overlap in left. max < lo no overlap with I in left (x.left.max < lo) no overlap in left. x.left (lo, hi) Proof. Suppose no overlap in left. (x.left.max >= lo) no interval (a, b) in right subtree overlaps (lo, hi). max lo and no overlap with I (lo, hi) (z, max) (a, b) (z, max) 26 a z hi since tree sorted by left endpoint 27 VLSI Database Problem VLSI Database Problem: Sweep Line Algorithm Move a vertical "sweep line" from left to right. Sweep line: sort rectangles by x-coordinates and process in this order, stopping on left and right end. Store set of rectangles that intersect the sweep line in an interval search tree (using y-interval of rectangle). Left side: interval search for y-interval of rectangle, insert y-interval. Right side: delete y-interval. Sweep line reduces 2D orthogonal rectangle intersection problem to 1D interval searching! Running time of sweep line algorithm. Sort by x-coordinate. O(N log N) Insert y-interval into ST. O(N log N) Delete y-interval from ST. O(N log N) Interval search. O(R + N log N) N = # line segments R = # intersections Efficiency relies on judicious extension of BST. 28 29

Summary Basis of many geometric algorithms: search in a planar subdivision. grid 2D tree Voronoi diagram intersecting lines basis N h-v lines N N N lines representation 2D array of N lists N-node BST N-node multilist ~N-node BST cells ~N squares N rectangles N polygons ~N triangles search cost 1 log N log N log N extend to kd? too many cells easy cells too complicated use (k-1)d hyperplane 30