%%START

\chapter {Hidden Line Removal using BSP Tree and Polygon Clipping}

\section {Algorithm}

Here, the BSP Tree routine and Polygon Clipping routine are used in succession
to perform hidden line removal. A C++ pseudo-code illustrating the 
algorithm is given below.

\begin{verbatim}
void HiddenLineRemoval ()
{
  OFFObject *obj;
  Point3d viewpoint;
  Poly2d subject,clip;
  Poly2d *insidelist,*outsidelist;
  Poly2d *subjectlist,*new_subjectlist;
  int i,j,k,l;

  ReadOFFObject (obj);
  Read (viewpioint);

  Sort_using_BSP (obj,viewpoint);

  Transform2d (obj,viewpoint);

  for (i = obj->nf-1; i >= 0; i--)
     {
      subject = obj->faces[i];

      subjectlist[0] = subject;
      subjectlist->n = 1;

      for (j = i+1; j <= obj->nf; j++)
         {
          clip = obj->faces[j];

          new_subjectlist->n = 0;
          for (k = 0; k < subjectlist->n; k++)
             {
              Clip2polygons(clip,subjectlist[k],insidelist,outsidelist);
              new_subjectlist += outsidelist;
              new_subjectlist->n += outsidelist->n;
             		// Append 'outsidelist' to 'new_subjectlist'
             }

          subjectlist = new_subjectlist;
         }

      Output (subjectlist);
             	// Print 'subjectlist' as output
     }
}
\end{verbatim}

The input here are the set of convex polygons in {\em 3D} space in {\em OFF} 
format and the view point, and the output is a set of non-overlapping 
polygons in {\em 2D} space which when
drawn in any order, give the desired hidden line removal effect with respect to
the given view point.

\section {Implementation}

The above algorithm was formulated and implemented during the duration of the
practical training. However, the BSP routine could not be incorporated
into the code due to lack of time. Thus, the implemented code requires the
output from the BSP routine as its input.

\section {Efficiency}

The algorithm as described above has complexity of $O(N^2)$, 
$N$ being the number of polygons in the input. The efficiency greatly depends
on the efficiency of the clipping algorithm which itself has complexity of
$O(M N)$ as described previously. But since a significant number of 
polygons in a typical input are disjoint 
(i.e. not all polygons in the list necessarily intersect each other), the
polygon clipping routine on an average has a complexity of much less than
$O(N^2)$ for sufficiently large $N$. Thus, the typical time complexity of the
algorithm would be $O(N^3)$.

\section {Scope for Improvement}

There is ample scope for improvement in the algorithm mentioned above. It is
almost a brute force implementation using the clipping routine to obtain hidden
line removal and there exist more efficient ways (of possibly $O(N logN)$ 
complexity) 
to obtain the same results (although implementation may be more complex).
Also, dynamic memory allocation (DMA) can be used instead of the present fixed
memory allocation to efficiently utilise the system resources.

%%END
