Practical Applications of Interval Tree - data-structures

I have googled about this topic a bit and found this on http://www.geeksforgeeks.org/
Interval tree is mainly a geometric data structure and often used for windowing queries, for instance, to find all roads on a computerized map inside a rectangular viewport, or to find all visible elements inside a three-dimensional scene.
Now my question is actually in two parts:
How is interval tree used to find all roads on a computerized map?
What are some other example of practical applications of interval tree?
P.S: Brief explanations with reference to more reading materials on interval tree will be more than welcomed

In the windowing query, given a set of line segments and an axis-aligned rectangle, we have to find the intersections of the line segments with the rectangle. This can be solved by using Interval Trees in combination with Range Trees.
Range Trees are an efficient data structure for finding the set of points present within a Range/Rectangle. So they can be used to find all the line segments such that one of the end points of each line segment is present in the query Rectangle. These correspond to the blue line segments in the figure below.
Interval Trees can be used to find those segments that overlap with the window but whose endpoints are outside the window. These are the red segments in the figure.
Before solving this problem, imagine a more restricted version of the problem in which all line segments are horizontal or vertical. In this case any horizontal segment that intersects the rectangle should intersect the left (and right) vertical edge of the rectangle. If we think of the horizontal segments as intervals and the vertical edge of the rectangle as a point, the problem is to find all the intervals that contain the point. Thus we can solve this problem using interval trees. Similarly we can find all intersecting vertical line segments.
The general version of the problem where line segments aren't parallel to axis cannot be solved perfectly using interval trees. However we can use interval trees to eliminate the overwhelming majority of the line segments that don't overlap with the query rectangle. For each line segment in the input, we construct an axis-parallel rectangle whose diagonal is the line segment. We then construct (horizontal and vertical) interval trees using the sides of the rectangles. Given a query rectangle R, we can first find all rectangles that intersect R as before. The corresponding line segments have a high chance of intersecting with R and can be checked individually for actual intersection.

Maybe not directly answer your question but I think it might helpful:
Enclosing Interval Searching Problem:
Given a set S of n intervals and a query point, q, report all those intervals containing q.
Overlapping Interval Searching Problem:
Given a set S of n intervals and a query interval Q, report all those intervals in S overlapping Q.
Reference (also compare with other similar data structure like segement tree): http://www.iis.sinica.edu.tw/~dtlee/dtlee/CRCbook_chapter18.pdf

Related

Sweep line algorithm for circle intersection, represent circle into two halves

I am trying to develop sweep line algorithm for given list of circles. by checking different sources I am able to understand the algorithm .
here are the links that i am referring ,
http://www.stolerman.net/studies/cs623/cs623_winter_2012_sol2.pdf
https://www.cs.cmu.edu/~15451-f17/lectures/lec21-sweepline.pdf
As the solution in given links says that We need to represent the circle in two halves because every sweep line intersect a given circle at most two point at any given time.
I am not able to understand how can we represent any circle in two halves e.g., bottom half and top half . It will be great help if someone can explain the data structure to represent a circle into two halves and how the ordering will be defined for semicircles in binary search tree ?
The algorithm you linked to uses the fact that, if you have two opposite points on a circle, the segment between them uniquely defines a circle, and that segment is a diameter of the circle.
Hence, if you keep a list of all rightmost and leftmost points for all your circle, you will have a representation of all your circles.
More generally, you just need to make sure that the two points you are keeping for each circle make a diameter that is perpendicular to the sweep line you are using in the algorithm.
Depending on what the expected input for the algorithm is, you will need to translate that representation into those two points, but then, with a list of couple of points: [(p1, p2), ..., (p_{2n-1}, p_{2n})] where you have n circles.
Rather than storing a circle in the tree, you can store two half-circles. They result from splitting the circle so that the sweep-line meets a half-circle only once. Whether you indeed store two half circles, or two references to the same circle with a way to tell which half is a matter of ease of implementation.
Beware that two half-circles may intersect even if the segments that join the same endpoints do not intersect.

Approximate a curve with a limited number of line segments and arcs of circles

Is there any algorithm that would allow to approximate a path on the x-y plane (i.e. an ordered suite of points defined by x and y) with a limited number of line segments and arcs of circles (constant curvature)? The resulting curve needs to be C1 (continuity of slope).
The maximum number or segments and arcs could be a parameter. An additional interesting constraint would be to prevent two consecutive circles of arcs without an intermediate line segment joining them.
I do not see any way to do this, and I do not think that there exists a method for it, but any hint towards this objective is welcome.
Example:
Sample file available here
Consider this path. It looks like a line, but is actually an ordered suite of very close points. There is no noise and the order of the sequence of points is well known.
I would like to approximate this curve with a minimum number of succession of line segments and circular arcs (let's say 10 line segments and 10 circular arcs) and a C1 continuity. The number of segments/arcs is not an objective itself but I need any parameter which would allow to reduce/increase this number to attain a certain simplicity of the parametrization, at the cost of accuracy loss.
Solution:
Here is my solution, based on Spektre's answer. Red curve is original data. Black lines are segments and blue curves are circle arcs. Green crosses are arc centers with radii shown and blue ones are points where segments potentially join.
Detect line segments, based on slope max deviation and segment minimal length as parameters. The slope of the new segment step is compared with the average step of the existing segment. I would prefer an optimization-based method, but I do not think that it exists for disjoint segments with unknown number, position and length.
Join segments with tangent arcs. To close the system, the radius is chosen such that the segments extremities are the least possible moved. A minimum radius constraint has been added for my purposes. I believe that there will be some special cases to treat in the inflexion points are far away when (e.g. lines are nearly parallel) and interact with neigboring segments.
so you got a point cloud ... for such Usually points close together are considered connected so:
you need to add info about what points are close to which ones
points close only to 2 neighbors signaling interior of curve/line. Only one neighbor means endpoint of curve/lines and more then 2 means intersection or too close almost or parallel lines/curves. No neighbors means either noise or just a dot.
group path segments together
This is called connected component analysis. So you need to form polylines from your neighbor info table.
detect linear path chunks
these have the same slope among neighboring segments so you can join them to single line.
fit the rest with curves
Here related QAs:
Finding holes in 2d point sets?
Algorithms: Ellipse matching
How approximation search works see the sublinks there are quite a bit of examples of fitting
Trace a shape into a polygon of max n sides
[Edit1] simple line detection from #3 on your data
I used 5.0 deg angle change as threshold for lines and also minimal size fo detected line as 50 samples (too lazy to compute length assuming constant point density). The result looks like this:
dots are detected line endpoints, green lines are the detected lines and white "lines" are the curves so I do not see any problem with this approach for now.
Now the problem is with the points left (curves) I think there should be also geometric approach for this as it is just circular arcs so something like this
Formula to draw arcs ending in straight lines, Y as a function of X, starting slope, ending slope, starting point and arc radius?
And this might help too:
Circular approximation of polygon (or its part)
the C1 requirement demands the you must have alternating straights and arcs. Also realize if you permit a sufficient number of segments you can trivially fit every pair of points with a straight and use a tiny arc to satisfy slope continuity.
I'd suggest this algorithm,
1 best fit with a set of (specified N) straight segments. (surely there are well developed algorithms for that.)
2 consider the straight segments fixed and at each joint place an arc. Treating each joint individually i think you have a tractable problem to find the optimum arc center/radius to satisfy continuity and improve the fit.
3 now that you are pretty close attempt to consider all arc centers and radii (segments being defined by tangency) as a global optimization problem. This of course blows up if N is large.
A typical constraint when approximating a given curve by some other curve is to bound the approximate curve to an epsilon-hose within the original curve (in terms if Minkowski sum with a disk of fixed radius epsilon).
For G1- or C2-continuous approximation (which people from CNC/CAD like) with biarcs (and a straight-line segment could be seen as an arc with infinite radius) former colleagues of mine developed an algorithm that gives solutions like this [click to enlarge]:
The above picture is taken from the project website: https://www.cosy.sbg.ac.at/~held/projects/apx/apx.html
The algorithm is fast, that is, it runs in O(n log n) time and is based on the generalized Voronoi diagram. However, it does not give an approximation with the exact minimum number of elements. If you look for the theoretical optimum I would refer to a paper by Drysdale et al., Approximation of an Open Polygonal Curve with
a Minimum Number of Circular Arcs and Biarcs, CGTA, 2008.

Number of areas overlapped by exactly 3 circles

I have a task where i have any number of circles. All I know about one is its centre and radius. Now I need to find the number of areas which are overlapped by exactly 3 circles. I tried to solve it knowing that circles overlap when distance between their centres is shorter than sum of radiuses, but it got me nowhere.
A sweep-line algorithm should do the job.
Read about sweep-line algorithms in general here, about one particular (very important) algorithm here. The overall structure of an algorithm for this problem would be similar to that of Bentley–Ottmann algorithm. Below are some details (not a full description but rather a sketch; a full description
Take all the leftmost (min X) and rightmost (max X) points on each circle. Sort all these points by their X coordinate. Put them to a priority queue.
Run a vertical sweep line along the X axis. The line contains a collection of Y coordinates of points where it intersects with the circles at the current X coordinate, sorted by Y.
Once the sweep line hits a leftmost circle point, add it to the the collection twice — once for the upper semicircle and once for the lower semicircle. Once the sweep line hits a rightmost circle point, remove the corresponding points from the collection. Keep track of the number of times each interval between two successive points is covered by circles. Keep track of identity of these areas. Whenever two points that belong to different circles appear next to each other, calculate their intersection points, and insert them to the point queue. Every time the sweep line hits an intersection point, swap the corresponding points in the collection and adjust area identities and overlap counts.
It's easy to visualise the algorithm by drawing some circles on a piece of paper, coloring each area differently, and slowly moving a ruler across the drawing, noting how it intersects with circles and areas.
Also google "line sweep algorithm" "circles".

Find points that contained by set of retangle

Input:
A set of rectangles (overlapping rectangles too) and a set of point.
Coordinates are integer type.
Rectangle 's sides parallel to axis
Output:
All points inside any rectangles given
What is the efficient algorithm and data structure should I use ? Thanks.
You can use a sweep line algorithm: Sort the points by X coordinate. Introduce events when rectangles enter or leave the sweep line (the X coordinates of their left and right border). The rectangles currently intersecting the sweepline are a set of intervals when projected onto the sweep line, so they can be maintained using an interval tree or segment tree (the latter only after Y coordinate compression, but you can do that as a preprocessing step).
With that setup, for every point you just need to check whether it intersects one of the intervals maintained by your data structure.
Runtime: O((n+m) log (n+m))
2d segment tree (example here) is effective data structure to check if points are inside of any rectangle
The best idea I can come up with would be to check for every point (x,y) whether it is contained in any rectangle (l,t,w,h), yielding a runtime bound of O(nm) where n is the number of points and m is the number of rectangles.

Find out how to flood fill a polygon with the smallest number of vector lines

Say I have a vector polygon with holes. I need to flood fill it by drawing connected segments. Of course, since there are holes, I can't fill it using a single continous polyline: I'll need to interrupt my path sometimes, then move to an area which was skipped and start another polyline there.
My goal is to find a set of polylines needed to fill the whole polygon. Better if I can find the smallest set (that is, the way I can fill the polygon with the minimum number of interruptions).
Bonus question: how could I do that for partial density fills? Say, I don't want to fill at 100% density but I want a 50% (this will require that fill lines, supposing they're parallel each other and have a single-unit width, are put at a distance of two units).
I couldn't find a similar question here, although there are many related to flood-fill algorithms.
Any ideas or pointers?
Update: this picture from Wikipedia shows a good hypotetical flood path. I believe I could do that using a bitmap. However I've got a vector polygon. Should I rasterize it?
I'm assuming here that the distance between lines is 1 unit.
A crude implementation, with no guarantee to find the minimum number of polyline, is:
Start with an empty set of polylines.
Determine minx and maxx of the polygon.
Loop x from xmin to xmax, with a step of 1. Line L is the vertical line at x.
Intersect vertical line L with your polygon (quick algorithm, easy to find). That will give you a set of segments: {(x,y1)-(x,y2)}.
For all polylines, and all segments, merge segment + end of polylines (see note 1 below). When you merge a segment and a polyline, append a small stretch at the end of the polyline (to joint it to the segment), and the segment itself. For all segments that you can't merge using that, add a new polyline in the global set.
At the end, try to merge again polylines if possible (ends close together).
Optimal algorithm for merging new segments to existing polylines should be easy to find (hashing on y), or a brute force algorithm may suffice:
number of new segments per line scan should not be too high if your polygons do not have zillions of holes,
number of global polylines at every step should not be too large,
you compare only with the end segment of each polylines, not the whole of it.
Added note (1): To cover the case where your polygon has nearly-vertical edges, the merge process should not look only at y-delta, but allow a merge if any two y range overlaps (that means end of polyline y-range overlap segment y-range).

Resources