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

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

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 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

Spatial Data Structures

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

Spatial Data Structures

UNIVERSITY OF WATERLOO Faculty of Mathematics

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

Spatial Data Structures

Speeding Up Ray Tracing. Optimisations. Ray Tracing Acceleration

Spatial Data Structures

Module 8: Range-Searching in Dictionaries for Points

Algorithms for GIS:! Quadtrees

Ray Tracing Acceleration Data Structures

CMSC 754 Computational Geometry 1

Multidimensional Indexing The R Tree

Computer Graphics. Bing-Yu Chen National Taiwan University

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

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

Geometric Structures 2. Quadtree, k-d stromy

Spatial Data Structures

Advanced 3D-Data Structures

1 The range query problem

1. Meshes. D7013E Lecture 14

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

Computer Graphics II

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

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

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

CS210 Project 5 (Kd-Trees) Swami Iyer

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

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

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

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

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

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

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

Spatial data structures in 2D

Spatial Data Management

Lecture 11: Ray tracing (cont.)

Spatial Data Management

3D Representation and Solid Modeling

COMP 175: Computer Graphics April 11, 2018

Collision and Proximity Queries

Computer Graphics. - Rasterization - Philipp Slusallek

CS535 Fall Department of Computer Science Purdue University

Ray Tracing: Intersection

improving raytracing speed

Intersection Acceleration

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

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

Introduction to Algorithms

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

I/O-Algorithms Lars Arge Aarhus University

Computational Geometry

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

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

Simulation in Computer Graphics Space Subdivision. Matthias Teschner

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

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

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

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

Advanced Data Types and New Applications

Lecture 3 February 9, 2010

Line segment intersection. Family of intersection problems

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

Geometric Data Structures

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

Collision Detection based on Spatial Partitioning

Lecture 13: Binary Search Trees

Acceleration Data Structures

3D Modeling: Solid Models

CS2012 Programming Techniques II

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

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

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

Computational Geometry

Advanced Data Types and New Applications

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

CS S Lecture February 13, 2017

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

Speeding up your game

We assume uniform hashing (UH):

Orthogonal range searching. Orthogonal range search

Prof. Gill Barequet. Center for Graphics and Geometric Computing, Technion. Dept. of Computer Science The Technion Haifa, Israel

26 The closest pair problem

Quadtrees and Meshing

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

Organizing Spatial Data

ADAPTIVE GRIDS FOR GEOMETRIC OPERATIONS

More Hidden Surface Removal

Line Arrangement. Chapter 6

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Approximation Algorithms for Geometric Intersection Graphs

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Transcription:

Geometric search: Overview Geometric Algorithms Types of data. Points, lines, planes, polygons, circles,... This lecture. Sets of N objects. Geometric problems extend to higher dimensions.! Good algorithms also extend to higher dimensions.! Curse of dimensionality. Basic problems.! Range searching.! Nearest neighbor.! Finding intersections of geometric objects. Reference: Chapters 26-27, Algorithms in C, 2 nd Edition, Robert Sedgewick. Robert Sedgewick and Kevin Wayne Copyright 2005 http://www.princeton.edu/~cos226 2 1D Range Search 7.3 Range Searching 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 a 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 Robert Sedgewick and Kevin Wayne Copyright 2005 http://www.princeton.edu/~cos226 4

1D Range Search Implementations 2D Orthogonal Range Search 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. 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?! Range count: how many keys lie in a 2D range? Applications: networking, circuit design, databases. insert count range ordered array N log N R + log N Geometric interpretation.! Keys are point in the plane.! Find all in a given h-v rectangle? 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 5 6 2D Orhtogonal Range Search: Grid Implementation 2D Orthogonal Range Search: Grid Implementation Costs 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. Space-time tradeoff.! Space: M 2 + N.! Time: 1 + N / M 2 per grid cell examined on average. 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. [if are evenly distributed]! Initialize: O(N).! Insert: O(1).! Range: O(1) per point in range. RT LB LB 7 8

Clustering Space Partitioning Trees Grid implementation. Fast, simple solution for well-distributed. Problem. Clustering is a well-known phenomenon in geometric data. 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. Ex: USA map data.! Ray tracing.! 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.! Flight simulators.! N-body simulation.! Collision detection.! Astronomical databases.! Adaptive mesh generation. Grid kd tree Need data structure that gracefully adapts to data.! Accelerate rendering in Doom.! Hidden surface removal and shadow casting. Quadtree BSP tree 9 10 Quad Trees Curse of Dimensionality Quad tree. Recursively partition plane into 4 quadrants. Implementation: 4-way tree. public class QuadTree { Quad quad; Value value; QuadTree NW, NE, SW, SE; 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 2 100 centrants??? b a c d a h e d e f g f g h b c Good clustering performance is a primary reason to choose quad trees over grid methods. Raytracing with octrees http://graphics.cs.ucdavis.edu/~gregorsk/graphics/275.html 11 12

2D Trees kd Trees 2D tree. Recursively partition plane into 2 halfplanes. kd tree. Recursively partition k-dimensional space into 2 halfspaces. Implementation: BST, but alternate using x and y coordinates as key.! Search gives rectangle containing point.! Insert further subdivides the plane. Implementation: BST, but cycle through dimensions ala 2D trees. p level! i (mod k) p whose i th coordinate is less than p s whose i th coordinate is greater than p s left of p right of p even levels q Efficient, simple data structure for processing k-dimensional data.! Adapts well to clustered data.! Adapts well to high dimensional data. below q above q odd levels! Discovered by an undergrad in an algorithms class! 13 14 Summary Basis of many geometric algorithms: search in a planar subdivision. 7.4 Geometric Intersection 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 15 Robert Sedgewick and Kevin Wayne Copyright 2005 http://www.princeton.edu/~cos226

Geometric Intersection Orthogonal Segment Intersection: Sweep Line Algorithm Problem. Find all intersecting pairs among set of N geometric objects. Applications. CAD, games, movies, virtual reality. Simple version: 2D, all objects are horizontal or vertical line segments. Sweep vertical line from left to right.! Event times: x-coordinates of h-v line segments.! 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. Brute force. Test all $(N 2 ) pairs of line segments for intersection. Sweep line. Efficient solution extends to 3D and general objects. 17 insert y delete y range search 18 Orthogonal Segment Intersection: Sweep Line Algorithm Immutable H-V Segment ADT Sweep line: reduces 2D orthogonal segment intersection problem to 1D range searching! Running time of sweep line algorithm.! Put x-coordinates on a PQ (or sort). 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) Efficiency relies on judicious use of data structures. N = # line segments R = # intersections public final class SegmentHV implements Comparable<SegmentHV> { public final int x1, y1; public final int x2, y2; public SegmentHV(int x1, int y1, int x2, int y2) public boolean ishorizontal() { public boolean isvertical() { public int compareto(segmenthv b) { public String tostring() { compare by y-coordinate; break ties by x-coordinate (x1, y2) (x1, y1) (x2, y1) (x1, y1) horizontal segment vertical segment 19 20

Sweep Line Event Sweep Line Algorithm: Initialize Events public class Event implements Comparable<Event> { int time; SegmentHV segment; public Event(int time, SegmentHV segment) { this.time = time; this.segment = segment; public int compareto(event b) { return a.time - b.time; // initialize events MinPQ<Event> pq = new MinPQ<Event>(); for (int i = 0; i < N; i++) { if (segments[i].isvertical()) { Event e = new Event(segments[i].x1, segments[i]); pq.insert(e); else if (segments[i].ishorizontal()) { Event e1 = new Event(segments[i].x1, segments[i]); Event e2 = new Event(segments[i].x2, segments[i]); pq.insert(e1); pq.insert(e2); 21 22 Sweep Line Algorithm: Simulate the Sweep Line General Line Segment Intersection // simulate the sweep line int INF = Integer.MAX_VALUE; RangeSearch<SegmentHV> st = new RangeSearch<SegmentHV>(); while (!pq.isempty()) { Event e = pq.delmin(); int sweep = e.time; SegmentHV segment = e.segment; 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. if (segment.isvertical()) { SegmentHV seg1, seg2; seg1 = new SegmentHV(-INF, segment.y1, -INF, segment.y1); seg2 = new SegmentHV(+INF, segment.y2, +INF, segment.y2); Iterable<SegmentHV> list = st.range(seg1, seg2); for (SegmentHV seg : list) System.out.println(segment + " intersects " + seg); A B else if (sweep == segment.x1) st.add(segment); else if (sweep == segment.x2) st.remove(segment); A AB C ABC D ACB ACBD ACD CAD CA A insert segment delete segment intersection order of segments 23 24

Line Segment Intersection: Implementation 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). VLSI Rules Checking Implementation issues.! Degeneracy.! Floating point precision.! Use PQ since intersection events aren't known ahead of time. 25 Robert Sedgewick and Kevin Wayne Copyright 2005 http://www.princeton.edu/~cos226 Algorithms and Moore's Law Algorithms and Moore's Law Rectangle intersection. Find all intersections among h-v rectangles. Application. VLSI rules checking in microprocessor design. "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. Early 1970s: microprocessor design became a geometric problem.! Very Large Scale Integration (VLSI).! Computer-Aided Design (CAD).! Design-rule checking. 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. 27 28

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, stopping on left and right end.! Maintain set of intervals intersecting sweep line.! Key operation: given a new interval, does it intersect one in the set? (7, 10) (5, 11) (4, 8) (15, 18) (17, 19) (20, 22) Support following operations.! Insert an interval (lo, hi).! Delete the interval (lo, hi).! Search for an interval that intersects (lo, hi). Non-degeneracy assumption. No intervals have the same x-coordinate. 29 30 Interval Search Trees Finding an Intersecting Interval (7, 10) (20, 22) Search for an interval that intersects (lo, hi). (5, 11) (4, 8) (15, 18) Interval tree implementation with BST.! Each BST node stores one interval.! BST nodes sorted on lo endpoint.! Additional info: store and maintain max endpoint in subtree rooted at node. (4, 8) 8 (17, 19) (17, 19) (5, 11) 18 (20, 22) 22 (15, 18) 18 22 Node x = root; while (x!= null) { if (x.interval.intersects(lo, hi)) return x.interval; else if (x.left == null) x = x.right; else if (x.left.max < lo) x = x.right; else x = x.left; return null; Case 1. If search goes right, no overlap in left.! (x.left == null) % trivial.! (x.left.max < lo) % for any interval (a, b) in left subtree of x, we have b " max < lo. defn of max reason for going right max max in subtree (a, b) (lo, hi) (7, 10) 10 left subtree of x 32 33

Finding an Intersecting Interval Interval Search Tree: Analysis Search for an interval that intersects (lo, hi). Implementation. Use a balanced BST to guarantee performance. Node x = root; while (x!= null) { if (x.interval.intersects(lo, hi)) return x.interval; else if (x.left == null) x = x.right; else if (x.left.max < lo) x = x.right; else x = x.left; return null; Case 2. If search goes left, then either (i) there is an intersection in left subtree or (ii) no intersections in either subtree. Pf. Suppose no intersection in left. Then for any interval (a, b) in right subtree, a! c > hi % no intersection in right. intervals sorted by left endpoint no intersection in left subtree max (lo, hi) (c, max) (a, b) Operation insert interval delete interval find an interval that intersects (lo, hi) find all intervals that intersect (lo, hi) N = # intervals R = # intersections can maintain auxiliary information using log N extra work per op Worst case log N log N log N R log N left subtree of x right subtree of x 34 35 VLSI Database Sweep Line Algorithm: Review 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.! 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 log N) N = # line segments R = # intersections Efficiency relies on judicious extension of BST. 36 37