#ifndef D4GRID_H
#define D4GRID_H

#include <cassert>
using namespace std;

#include <d4tess.h>
#include <d4tessdraw.h>

typedef unsigned int uint;
typedef unsigned int const uintc;
typedef double real;
typedef double const realc;


//
//  Marching Tetrahedrons on 3D grid implemented.
//
//  Rushed code:  
//    d4grid::draw calls d4tessdraw::eval which uses 
//    d4marchdisp.
//
//  Client calls setf to evaluate function over grid.
class d4grid : public d4tess
{
  uint dimX;
  uint dimY;
  uint dimZ;

  void add( uintc i, uintc k, uintc m );

public:

  d4grid
  (
    uintc _dimX,
    uintc _dimY,
    uintc _dimZ,
    realc x0, realc x1,  // Ranges in the X dimension
    realc y0, realc y1,  // Ranges in the Y dimension
    realc z0, realc z1   // Ranges in the Z dimension
  );

  // Function assigning a value at a point.
  // F::operator(double & f, doube const x, realc y) expected.
  template< typename F >
  void setf(F const & f);

  // From the points linearly interpolate and draw.
  void draw(realc cut=0.0) const;

};


// --------------------------------------------------
// Implementation
//

template< typename F >
void d4grid::setf(F const & f )
{
  uint indx=0;

  uint i,k,m;
  for (m=0; m<(dimZ+1); ++m)
    for (k=0; k<(dimY+1); ++k)
      for (i=0; i<(dimX+1); ++i)
      {
        f(pt[indx].a,pt[indx].x,pt[indx].y,pt[indx].z);
        ++indx;
      }
}



  

#endif



