#include "particlemap.h"
#include "shadeop.h"


static BalancedParticleMap* gMap;

SHADEOP_TABLE(initparticlemap) = {
   { "void initparticlemap (string)", "", "" },
   { "" }
};

SHADEOP_TABLE(sampleparticlemap) = {
   { "float sampleparticlemap (point,float,float)", "", "" },
   { "" }
};

SHADEOP_INIT(particlemap_init) {
   // We don't need implicit initialization
   return 0;
}

SHADEOP(initparticlemap) {
   const char** filename = (const char**)argv[1];
   
   // Initialize the map
   if (!gMap) {
      gMap = LoadParticleMap((char*)*filename);
		printf("Particle map loaded\n");
		printf("%i particles found\n", gMap->storedParticles);
   }
   
   return 0;
}

SHADEOP(sampleparticlemap) {
   // Fetch our arguments
   float* pos = (float*)argv[1];
   float radius = *(float*)argv[2];
   float count = *(float*)argv[3];
   
   // Place our return value in argument 0
   if (gMap) {
	float testPos[3] = { 0.3, 0.1, -0.02 };
	//	float d = DensityEstimate(gMap, testPos, 0.2, 25);
	//	printf("d = %f\n", d);
      *(float*)argv[0] = DensityEstimate(gMap, pos, radius, count);   
   } else {
		printf("Could not find pmap\n");
      *(float*)argv[0] = 0.0f;
   }
   
   return 0;
}

SHADEOP_CLEANUP(particlemap_cleanup) {
   // Do nothing
}
