\documentclass[11pt]{article}
\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{epsfig}
\usepackage{psfig}
\usepackage[usenames,dvipsnames]{pstricks}
\usepackage{epsfig}
\usepackage{pst-grad} % For gradients
\usepackage{pst-plot} % For axes

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf 6.851: Advanced Data Structures } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{Scribe: #4}{Lecture #1}}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}[theorem]{Assumption}

% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in

\parindent 0in
\parskip 1.5ex
%\renewcommand{\baselinestretch}{1.25}

\begin{document}

\lecture{6 --- February 23, 2010}{Spring 2010}{Dr.\ Andr\'e Schulz}{Reid Kleckner, Skye Wanderman-Milne}

\section{Overview}

In the last lecture we introduced ray shooting, where we determine which is
the first object in a set intersected by a given ray. We overviewed how to
solve this problem if our objects are simple polygons. This lecture explores
ray shooting more generally, beginning with data structures designed to
perform halfspace and simplex range queries such as partition trees, and
continuing with an explanation of how to use these data structures to perform
ray shooting.

\section{Partition Trees}
\paragraph{Problem.}
Given a pointset $S = \{p_1, p_2, \ldots, p_n\}$, we would like to perform two
sorts of queries:
\begin{enumerate}
\item {\em Halfspace Range Queries:} find properties relating to the subset of
  $S$ on one side of a line $h_q$ (e.g., how many points are above $h_q$?).
\item {\em Simplex Range Queries:} find properties relating to the subset of
  $S$ inside a simplex $t_q$ (e.g., how many points lie inside $t_q$?). In two
  dimensions, a simplex is a triangle, and we will use two-dimensional
  examples for the remainder of these notes.
\end{enumerate}

\paragraph{Idea.}
Partition $S$ into $r$ disjoint subsets $S_1, S_2, \ldots, S_r$. Each subset
$S_i$ is associated with a triangle $t_i$ that contains the points in that
subset (the triangles need not be disjoint). We call this partition $\Psi =
\{(S_1, t_1), (S_2, t_2), \ldots, (S_r, t_r)\}$. When finding the points in
the halfplane defined by a query line $h_q$, we can easily accept or discard
as necessary the points lying in a triangle that do not intersect $h_q$. We
then recurse on the remaining subsets in triangles intersecting $h_q$.


\begin{center}
% Generated with LaTeXDraw 2.0.2
% Tue Feb 23 21:07:43 EST 2010
\scalebox{1} % Change this value to rescale the drawing.
{
\begin{pspicture}(0,-2.6316721)(7.561875,2.3694677)
\pstriangle[linewidth=0.02,dimen=outer,fillstyle=solid](2.4032786,0.76376826)(1.2848753,1.0856996)
\rput{8.078382}(0.1766223,-0.6202093){\pstriangle[linewidth=0.02,dimen=outer,fillstyle=hlines*,hatchwidth=0.0,hatchangle=0.0](4.479845,0.34716254)(1.2848753,1.1866949)}
\rput{29.038815}(-0.06789627,-0.9497924){\pstriangle[linewidth=0.02,dimen=outer,fillstyle=hlines*,hatchwidth=0.0,hatchangle=10.0](1.7997766,-1.1803916)(1.4795533,1.1488217)}
\pstriangle[linewidth=0.02,dimen=outer,fillstyle=solid](5.245579,-1.344509)(1.4406178,1.1614461)
\rput{-21.924234}(0.92588514,1.083383){\pstriangle[linewidth=0.02,dimen=outer,fillstyle=solid](3.2595747,-2.384915)(1.5610266,1.0730752)}
\psline[linewidth=0.04cm](0.0,-1.9505322)(6.66,2.0494678)
\psline[linewidth=0.04cm,arrowsize=0.05291667cm 2.0,arrowlength=1.4,arrowinset=0.4]{->}(5.92,1.6094679)(5.48,2.3494678)
\usefont{T1}{ptm}{m}{n}
\rput(7.0714064,1.9344678){$h_q$}
\usefont{T1}{ptm}{m}{n}
\rput(4.804531,-2.0055323){discard}
\usefont{T1}{ptm}{m}{n}
\rput(1.5139062,1.7544678){accept}
\usefont{T1}{ptm}{m}{n}
\rput(3.69375,-0.3655322){recurse}
\end{pspicture} 
}
\end{center}

The {\it crossing number} of $\Psi$ is the maximum number of triangles that
can be intersected by some line.

We call $\Psi$ {\it fine} if $|S_i| \le \frac{2n}{r} \:\forall\: S_i$, meaning
every subset of $S$ has no more than twice the average number of points per
subset (i.e., the subsets are fairly equally distributed).

Ji\v{r}\'{i} Matou\v{s}ek has proved that for every $r$ such that $1 \le r \le
n$, there exists a fine partition $\Psi$ with size $r$ and crossing number
$O(\sqrt{r})$, and that $\Psi$ can be computed in $O(n^{1+\epsilon})$ time for
$\epsilon > 0$ \cite{matousek92a}. If we partition $S$ in this manner, a query
line $h_q$ cannot intersect more than $O(\sqrt{r})$ triangles. Thus we will need
to recurse on at most $O(\sqrt{r})$ subsets.

\paragraph{Performance.}
Parition trees yield the following performance for a halfspace range query:
\begin{itemize}
\item Query time: $O(n^{\frac{1}{2}+\epsilon})$
\item Storage: $O(n)$
\item Preprocessing: $O(n^{1+\epsilon})$
\end{itemize}
The derivation is left as a homework exercise.

For a simplex range query, we know that the query triangle $t_q$ intersects no
more than three times the crossing number of our partition. Thus, the query
time depends on $t_q$, but will never be worse than the bounds given for a
halfspace range query.

\begin{center}
% Generated with LaTeXDraw 2.0.2
% Tue Feb 23 21:20:09 EST 2010
\scalebox{1} % Change this value to rescale the drawing.
{
\begin{pspicture}(0,-2.56)(5.58,2.56)
\definecolor{color337}{rgb}{0.7490196078431373,0.7490196078431373,0.7490196078431373}
\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](1.41,0.08)(1.7,1.38)
\rput{4.3786483}(0.053013127,-0.20670652){\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](2.73,-0.1)(1.7,1.38)}
\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](1.09,-1.02)(1.7,1.38)
\rput{-36.967518}(0.8032909,1.4630469){\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](2.59,-1.16)(1.7,1.38)}
\rput{41.043407}(0.6426537,-2.5368724){\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](3.71,-1.1)(1.7,1.38)}
\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](3.97,-0.16)(1.7,1.38)
\pstriangle[linewidth=0.06,dimen=outer](2.8,-0.86)(1.8,1.7)
\psline[linewidth=0.04cm,linestyle=dashed,dash=0.16cm 0.16cm](4.56,-2.54)(1.86,2.54)
\psline[linewidth=0.04cm,linestyle=dashed,dash=0.16cm 0.16cm](1.06,-2.48)(3.64,2.38)
\psline[linewidth=0.04cm,linestyle=dashed,dash=0.16cm 0.16cm](5.56,-0.84)(0.0,-0.84)
\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](2.85,1.0)(1.7,1.38)
\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](3.21,-2.22)(1.7,1.38)
\rput{33.976875}(-0.5019267,-1.1370859){\pstriangle[linewidth=0.02,linecolor=color337,dimen=outer](1.61,-2.08)(1.7,1.38)}
\usefont{T1}{ptm}{m}{n}
\rput(3.2314062,-0.6){$t_q$}
\end{pspicture} 
}
\end{center}

\section{Multi-level Partition Trees}
\subsection{First attempt}
\paragraph{Problem.}
Given a set of line segments, we would like to find the segments stabbed by a
query line $h_q$; i.e., all the segments with one endpoint in the halfplane
above $h_q$, $h^+$, and the other in halfplane below $h_q$, $h^-$.

\begin{center}
% Generated with LaTeXDraw 2.0.2
% Tue Feb 23 21:28:21 EST 2010
\scalebox{1} % Change this value to rescale the drawing.
{
\begin{pspicture}(0,-2.05)(8.061875,2.05)
\psline[linewidth=0.04cm](0.0,-1.32)(7.12,1.44)
\psline[linewidth=0.04cm,arrowsize=0.05291667cm 2.0,arrowlength=1.4,arrowinset=0.4]{<->}(5.38,1.58)(5.98,0.2)
\usefont{T1}{ptm}{m}{n}
P\rput(5.931406,1.445){$h^+$}
\usefont{T1}{ptm}{m}{n}
\rput(6.331406,0.425){$h^-$}
\usefont{T1}{ptm}{m}{n}
\rput(7.5714064,1.525){$h_q$}
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(4.2,0.84)(5.46,0.06)
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(3.08,0.5)(2.8,-0.78)
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(4.2,-1.16)(6.28,-0.44)
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(1.36,0.8)(0.88,-0.58)
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(1.36,-1.78)(0.6,-0.88)
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(2.9,2.04)(4.18,1.8)
\psline[linewidth=0.02cm,dotsize=0.07055555cm 2.0]{*-*}(2.32,-1.76)(3.88,-2.04)
\end{pspicture} 
}
\end{center}

\paragraph{Idea.}
We split the endpoints of the line segments into two sets, $S_l$ and $S_r$,
such that each line segment has an endpoint in each set. Using a partition
tree, we find all segments with an endpoint in $h^+$. Then using a
second-level partition tree, we check if the other endpoint of each segment
lies in $h^-$.

\paragraph{Performance.}
\begin{itemize}
\item Query time: $O(n^{\frac{1}{2}+\epsilon})$
\item Storage: $O(n\log n)$
\item Preprocessing: $O(n^{1+\epsilon})$
\end{itemize}

\subsection{Decreasing query time via increased storage}
\paragraph{Geometric duality transform.}
If we have a point $p = (p_x, p_y)$ in the primal, we transform it into a line
$p^*$ in the dual with $y = p_xx - p_y$. A line in the primal is likewise
transformed into a point in the dual. Thus, if we consider a halfplane range
query over pointset $S$ with query line $h_q$ in the dual, we obtain a query
point $h_q^*$ and a set $|S^*|$ of lines, which form $O(n^2)$ cells (regions
enclosed by a subset of the lines). The points in $S$ above $h_q$ correspond
to the lines in $S^*$ below $h_q^*$ (imagine sweeping $h_q$, which corresponds
to moving $h_q^*$ down vertically). These lines will be the same regardless of
where $h_q^*$ is in a given cell.

\begin{center}
% Generated with LaTeXDraw 2.0.2
% Tue Feb 23 21:42:12 EST 2010
\scalebox{1} % Change this value to rescale the drawing.
{
\begin{pspicture}(0,-2.2035)(10.828,2.2235)
\definecolor{color364c}{rgb}{0.8352941176470589,0.8352941176470589,0.8352941176470589}
\definecolor{color364}{rgb}{0.996078431372549,0.996078431372549,0.996078431372549}
\pspolygon[linewidth=0.044,linecolor=color364,fillstyle=vlines*,hatchwidth=0.04,hatchangle=111.5.0,hatchcolor=color364c](0.014298013,-1.9642375)(4.303702,-0.27076393)(4.303702,1.4535)(0.014298013,0.729925)(0.0,-1.9488422)
\psdots[dotsize=0.064](0.29800007,-0.9665)
\psline[linewidth=0.044cm](0.028596027,-1.9642375)(4.318,-0.27076393)
\psline[linewidth=0.044cm,arrowsize=0.05291667cm 2.0,arrowlength=1.4,arrowinset=0.4]{->}(3.418,-0.6265)(3.2380002,-0.1265)
\psdots[dotsize=0.064](2.138,-1.0065)
\psdots[dotsize=0.064](2.598,0.5935)
\usefont{T1}{ptm}{m}{n}
\rput(4.5294065,-0.4815){$h_q$}
\psdots[dotsize=0.064](3.398,-1.8465)
\psdots[dotsize=0.064](1.2980001,0.1135)
\psline[linewidth=0.02cm](7.178,1.1335)(10.658,-0.1265)
\psline[linewidth=0.02cm](5.678,-0.3665)(9.318,1.3935)
\psline[linewidth=0.02cm](10.818,0.2935)(8.258,-1.2865)
\psline[linewidth=0.02cm](5.8980002,-0.7665)(10.598001,-0.5865)
\psline[linewidth=0.02cm](7.118,-1.2065)(5.818,0.8935)
\psline[linewidth=0.02cm](5.978,-0.5265)(10.418,0.7135)
\psline[linewidth=0.02cm](6.998,0.8335)(9.158,-1.4865)
\psline[linewidth=0.02cm](7.098,-2.0065)(9.038,1.5535)
\psline[linewidth=0.02cm](5.8980002,-1.5465)(9.938,1.1535)
\psline[linewidth=0.04cm,linestyle=dotted,dotsep=0.16cm,dotsize=0.07055555cm 2.0,arrowsize=0.05291667cm 2.0,arrowlength=1.4,arrowinset=0.4]{*->}(8.918,-0.2865)(8.918,-1.6465)
\usefont{T1}{ptm}{m}{n}
\rput(9.239407,-0.1815){$h_q^*$}
\psdots[dotsize=0.074](1.478,-1.8065)
\psdots[dotsize=0.074](3.898,-1.0865)
\psdots[dotsize=0.074](3.558,0.7135)
\psdots[dotsize=0.074](0.6380001,-2.1465)
\usefont{T1}{ptm}{m}{it}
\rput(2.3314376,2.0185){primal}
\usefont{T1}{ptm}{m}{it}
\rput(8.338156,2.0185){dual}
\end{pspicture} 
}
\end{center}

\paragraph{Locus approach.}
We precompute all cells in the dual, and locate the correct cell given a
query.
\begin{enumerate}
\item Storage: $O(n^2)$, corresponding to the number of cells.
\item Query time: $O(\log n)$ for finding the correct cell (via trapezoidal
  maps).
\end{enumerate}

\subsection{Other results}
\begin{itemize}
\item A data structure with $O(\sqrt{n}2^{O(\log^*n)})$ query time
  \cite{matousek92b}.
\item In dimension $d$, a data structure with $O(n^{1-\frac{1}{d}+\epsilon})$
  query time and $O(n^{1+\epsilon})$ storage \cite{matousek92b}.
\item Query time lower bound: $\Omega(\frac{n}{m^{\frac{1}{d}} \log n})$ (where m is storage) \cite{chazelle89}.
\item Halfspace reporting in two dimensions with $O(\log n)$ query time and $O(n)$ storage \cite{chazelle81}.
\item Simplex reporting in $O(m^{1+\epsilon})$ space and $O(\frac{n}{m^{\frac{1}{d}}} \log^2 n)$ query time \cite{chazelle92}.
\end{itemize}

\section{Ray Shooting}

The ray shooting problem can be thought of in two ways:

\begin{enumerate}
\item Decision: Given a point $q$, direction $d$, and distance $\lambda$, does
the line segment of length $\lambda$ from $d$ along $d$ intersect anything?
\item Optimization: Given a point $q$ and direction $d$, what is the smallest
$\lambda$ such that the decision problem can be answered positively?
\end{enumerate}

We notice that the decision version of the ray shooting problem is monotone,
meaning that after reaching some minimal value $\lambda$, the answer to the
decision problem will be positive for all larger values of $\lambda$.

Given a monotone decision algorithm, we can construct a new algorithm that
solves the optimization problem by symbolically evaluating the original decision
algorithm.  At the start of the execution, we say $\lambda$ has values ranging
from $(-\infty, +\infty)$.  Whenever $\lambda$ is used in a polynomial
expression as an input to a branch, we can solve the polynomial for the critical
points at which the branch goes one way or the other, and evaluate both under
each set.

Obviously, this approach is slow, since the total work increases by a factor of
two for every branch executed.  We can improve its performance by ``batching
up'' polynomials.  If the polynomials have at most $p$ terms, then our
optimization algorithm runs in $O(p) + O(\log{p} T_D)$, where $T_D$ is the time
to run the decision algorithm.

To answer a weaker version of the ray shooting decision problem, we consider
shooting a ray into a set of hyperplanes.  Since $\lambda$ is given in the
decision problem, the problem is to check if there are any hyperplanes between
the two ends of the line segment formed by $q$, the ray starting point, and $q +
d \lambda$, the end of the ray.  To solve this problem, we can use geometric
duality to transform all of the hyperplanes into points and line segment ends
into hyperplanes.  The two line segment ends will form a wedge out of two
hyperplanes.  If and only if the line segment intersects any hyperplanes in the
primal, there will be points between the two hyperplanes in the dual.


\bibliographystyle{alpha}

\begin{thebibliography}{77}

\bibitem{matousek92a}
J. Matou\v{s}eck,
\emph{Efficient partition trees},
Discrete \& Computational Geometry,
1992.

\bibitem{matousek92b}
J. Matou\v{s}eck,
\emph{Range searching with efficient hierarchical cuttings},
Symposium on Computational Geometry,
1992.

\bibitem{chazelle86}
B. Chazelle,
\emph{Lower bounds on the complexity of multidimensional searching},
27th Annual Symposium on Foundations of Computer Science,
27-29 October 1986.

\bibitem{chazelle87}
B. Chazelle, H. Edelsbrunner,
\emph{Linear space data structures for two types of range search},
Discrete and Computational Geometry,
113-126,
1987.

\bibitem{chazelle92}
B. Chazelle, M. Sharir, E. Welzl,
\emph{Quasi-optimal upper bounds for simplex range searching and new zone
theorems},
Algorithmica,
1992.

\end{thebibliography}


\end{document}
