#ifndef D4FAN_H
#define D4FAN_H

#include <set>
#include <map>
#include <vector>
#include <iosfwd>
using namespace std;

#include <d4tess.h>
#include <simplexface.h>

typedef unsigned int uint;
typedef unsigned int const uintc;




//  Brief:  build a 3D fan from a point outside
//          the convex mesh.
//          Assumed tess's virtual tetrahedron's base
//          is orientated and viewable to point _w in eval.
class d4fan
{
  d4tess & tess;
public:

  // Tetrahedron face to tetrahedron.
  map<simplexface,uint> stot; 

  // Boundary or linked faces
  set< simplexface > dead;

  // Constructor
  d4fan( d4tess & _tess );

  // Build the 3D fan from the point _w to the 
  //   surface of tess.  Assume tess.cp on surface.
  void eval(uintc _w);

  ostream & print(ostream & os) const;

private:

  // The central point where all the points
  //   of the fan converge to.
  uint w;           

  // These are associated with partially constructed
  //   tetrahedrons which have been defined but not
  //   fully linked with their neighbours.
  //   The map stot connects the simplex with the tetrahedron.
  vector<simplexface> process;


  // Build a spike from the current surface to point w.
  void addspike();

  // Move on surface to neighbour.  ktni is the pointer
  //   of the tet siting on surface(call a fan tet) to its 
  //   neighbouring fan tet.  kt is the central fan tet.  
  // If the move can see point w, ktni and kt are possibly 
  //   constructed and they are linked.
  bool const link(uint & ktni, uintc kt);

  // The current surface face.
  simplexface cp;
};

ostream & operator << (ostream & os, d4fan const & x);

//  About:  
//
//  The class is intended to be constructed once and 
//  reused.  Because the STL containers are heavy and
//  preallocate their memory for efficiency puting this
//  class inside a loop and constructing it unnecessarily
//  allocates and deallocates memory.  Having this class
//  defined outside the loop solves this.
//
//  


#endif



