Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

  1. #ifdef _CVI_
  2. #  include <ansi_c.h>
  3. # else _CVI_
  4. #  include <stdlib.h>
  5. #  include <stdio.h>
  6. #  include <string.h>
  7. #endif _CVI_
  8.  
  9. #include "H3D.h"
  10.  
  11. #define H3DMAX 500
  12. H3D *h3[H3DMAX];
  13. //int Printf(char *format, ...);
  14.  
  15. int H3DClear(int h3d) {
  16.   if (!h3[h3d]) return -1;
  17.   memset(h3[h3d]->data, 0,h3[h3d]->size );
  18.   h3[h3d]->min=0;
  19.   h3[h3d]->max=0;
  20.   h3[h3d]->nentries=0;
  21.   return  0;
  22.  
  23. }
  24.  
  25. int H3DPrint(int h3d) {
  26.   if (!h3[h3d]) return -1;
  27. //Printf("PrintH3D nx=%d minx=%f stepx=%f ny=%d miny=%f stepy=%f size=%d\n", h3[h3d]->nx, h3[h3d]->minx, h3[h3d]->stepx, h3[h3d]->ny, h3[h3d]->miny, h3[h3d]->stepy, h3[h3d]->size ) ;
  28.   return  0;
  29.  
  30. }
  31.  
  32. int H3DExist(int h) {
  33.   if (h3[h]) return 1;
  34.   else return 0;
  35. }
  36.  
  37.  
  38. int H3DCalculateBin(int h, int axis, double value) {
  39.   int nx=1,xmin=0,dx=0;
  40.   int bin;
  41.   switch (axis) {
  42.     case 0:
  43.       nx = H3DGetNbinsX(h);
  44.       xmin= H3DGetMinX(h);
  45.       dx = H3DGetStepX(h);
  46.       break;
  47.     case 1:
  48.       nx = H3DGetNbinsY(h);
  49.       xmin= H3DGetMinY(h);
  50.       dx = H3DGetStepY(h);
  51.       break;
  52.     case 2:
  53.       nx = H3DGetNbinsZ(h);
  54.       xmin= H3DGetMinZ(h);
  55.       dx = H3DGetStepZ(h);
  56.       break;
  57.  
  58.   }
  59.   if (dx==0) return -1;
  60.   if (value<xmin) return -1;
  61.   bin = (int)((value-xmin)/dx);
  62.   if (bin>=nx) return -1;
  63.   else return bin;
  64. }
  65.  
  66. int H3DFill(int h3d,double x, double y, double z, double val) {
  67.  
  68.   int idx, ix,iy, iz;
  69.   if (!h3[h3d]) return -1;
  70.  
  71.   ix = H3DCalculateBin(h3d,0,x);
  72.   if (ix<0) return ix;
  73.  
  74.   iy = H3DCalculateBin(h3d,1,y);
  75.   if (iy<0) return iy;
  76.  
  77.   iz = H3DCalculateBin(h3d,2,z);
  78.   if (iz<0) return iz;
  79.  
  80.  
  81.   H3DFillBin(h3d, ix, iy, iz, val);
  82.   return 0;
  83. }
  84.  
  85. int H3DGetBin(int h3d,int x, int y, int z) {
  86.   return x+h3[h3d]->nx *(y+z*h3[h3d]->ny);
  87. }
  88.  
  89. int H3DFillBin(int h3d,int x, int y, int z, double val) {
  90.  
  91.   int idx;
  92.   if (!h3[h3d]) {
  93. //Printf("FillH3D error h3d is not initialized\n");
  94.     return -1;
  95.   }
  96.  
  97.   idx = H3DGetBin(h3d,x,y,z);
  98.   h3[h3d]->data[idx]+=val;
  99. //Printf("%d %d data %f %f\n",x,y,val, h3[h3d]->data[idx]);
  100.   if (h3[h3d]->data[idx]>h3[h3d]->max) h3[h3d]->max= h3[h3d]->data[idx];
  101.   if (h3[h3d]->data[idx]<h3[h3d]->min) h3[h3d]->min= h3[h3d]->data[idx];
  102.   h3[h3d]->nentries++;
  103.   return 0;
  104. }
  105.  
  106. double H3DGetBinContent(int h3d,int atx, int aty, int atz) {
  107.  
  108.   int idx;
  109.   if (!h3[h3d]) return 0;
  110.   if (h3[h3d]->nx <= atx)  return 0;
  111.   if (h3[h3d]->ny <= aty)  return 0;
  112.   if (h3[h3d]->nz <= atz)  return 0;
  113.   idx = H3DGetBin(h3d,atx,aty,atz);
  114.   if (idx*sizeof(double) < h3[h3d]->size ) return h3[h3d]->data[idx];
  115.  
  116.  
  117.   return 0;
  118. }
  119.  
  120.  
  121. int H3DInit(int h3d,char *name, char *title,int nx, double minx, double stepx, int ny, double miny, double stepy, int nz, double minz, double stepz) {
  122.  
  123.   if (h3[h3d]) {
  124.  
  125.     free(h3[h3d]->data);
  126.     free(h3[h3d]);
  127.     h3[h3d] = NULL;
  128.   }
  129.   //printf("InitH3D hID=%d\n",h3d);
  130.   h3[h3d] = (H3D *) malloc(sizeof(H3D));
  131. //h2  =h3d;
  132.  
  133.   H3DSetTitle(h3d,title);
  134.   H3DSetName(h3d,name);
  135.   h3[h3d]->id=H3D_ID;
  136.   h3[h3d]->nx = nx;
  137.   h3[h3d]->ny = ny;
  138.   h3[h3d]->nz = nz;
  139.  
  140.   h3[h3d]->minx = minx;
  141.   h3[h3d]->miny = miny;
  142.   h3[h3d]->minz = minz;
  143.  
  144.   h3[h3d]->stepx = stepx;
  145.   h3[h3d]->stepy = stepy;
  146.   h3[h3d]->stepz = stepz;
  147.  
  148.   h3[h3d]->size = h3[h3d]->nx*h3[h3d]->ny*h3[h3d]->nz*sizeof(double);
  149.   h3[h3d]->data = (double *) malloc(h3[h3d]->size);
  150.   h3[h3d]->len=sizeof(H3D)-sizeof(double *)+h3[h3d]->size;
  151.   H3DClear(h3d);
  152.   H3DPrint(h3d);
  153. //Printf("InitH3D 0x%x\n", h3d );
  154.   return  0;
  155.  
  156. }
  157.  
  158.  
  159. double H3DGetXBinCenter(int h3d,int xbin) {
  160.   return h3[h3d]->minx+xbin*h3[h3d]->stepx;
  161. }
  162.  
  163. double H3DGetYBinCenter(int h3d,int ybin) {
  164.   return h3[h3d]->miny+ybin*h3[h3d]->stepy;
  165. }
  166.  
  167. double H3DGetZBinCenter(int h3d,int zbin) {
  168.   return h3[h3d]->minz+zbin*h3[h3d]->stepz;
  169. }
  170.  
  171.  
  172. int H3DWrite2File(int h3d,FILE *fp) {
  173.  
  174.   if (!fp) return -1;
  175. //printf("H3D sizeof(H3D)=%lu len-datasize=%d len=%lu datasize=%d\t",sizeof(H3D)-sizeof(double *),h3[h3d]->len-h3[h3d]->size,h3[h3d]->len,h3[h3d]->size);
  176. //printf("H3D sz=%d %d\n",sizeof(double),sizeof(double *));
  177.   fwrite (h3[h3d], 1, sizeof(H3D)-sizeof(double *), fp);
  178.   fwrite (h3[h3d]->data, 1, h3[h3d]->size, fp);
  179.   return  0;
  180. }
  181.  
  182. int H3DWrite(int h3d,const char *fname,const char *opt) {
  183.   FILE *fp=fopen(fname,opt);
  184.   H3DWrite2File(h3d,fp);
  185.   fclose(fp);
  186.   return  0;
  187. }
  188.  
  189.  
  190.  
  191.  
  192. int H3DSetTitle(int h3d,char *title) {
  193.   sprintf(h3[h3d]->title,"%s",title);
  194.   return 0;
  195. }
  196.  
  197.  
  198. int H3DSetTitleX(int h3d,char *title) {
  199.   sprintf(h3[h3d]->titlex,"%s",title);
  200.   return 0;
  201. }
  202.  
  203.  
  204. int H3DSetTitleY(int h3d,char *title) {
  205.   sprintf(h3[h3d]->titley,"%s",title);
  206.   return 0;
  207. }
  208.  
  209. int H3DSetTitleZ(int h3d,char *title) {
  210.   sprintf(h3[h3d]->titlez,"%s",title);
  211.   return 0;
  212. }
  213.  
  214.  
  215. int H3DSetName(int h3d,char *title) {
  216.   sprintf(h3[h3d]->name,"%s",title);
  217.   return 0;
  218. }
  219.  
  220. int H3DGetNbinsY(int h) {
  221.   if (h3[h]) return h3[h]->ny;
  222.   else return 0;
  223. }
  224.  
  225. int H3DGetNbinsX(int h) {
  226.   if (h3[h]) return h3[h]->nx;
  227.   else return 0;
  228. }
  229.  
  230. int H3DGetNbinsZ(int h) {
  231.   if (h3[h]) return h3[h]->nz;
  232.   else return 0;
  233. }
  234.  
  235.  
  236.  
  237. double H3DGetMinX(int h) {
  238.   if (h3[h]) return h3[h]->minx;
  239.   else return 0;
  240. }
  241.  
  242. double H3DGetMinY(int h) {
  243.   if (h3[h]) return h3[h]->miny;
  244.   else return 0;
  245. }
  246.  
  247. double H3DGetMinZ(int h) {
  248.   if (h3[h]) return h3[h]->minz;
  249.   else return 0;
  250. }
  251.  
  252.  
  253.  
  254.  
  255. double H3DGetStepX(int h) {
  256.   if (h3[h]) return h3[h]->stepx;
  257.   else return 0;
  258. }
  259.  
  260. double H3DGetStepY(int h) {
  261.   if (h3[h]) return h3[h]->stepy;
  262.   else return 0;
  263. }
  264.  
  265. double H3DGetStepZ(int h) {
  266.   if (h3[h]) return h3[h]->stepz;
  267.   else return 0;
  268. }
  269.  
  270.  
  271.  
  272.  
  273. double H3DGetMin(int h) {
  274.   if (h3[h]) return h3[h]->min;
  275.   else return 0;
  276. }
  277.  
  278. double H3DGetMax(int h) {
  279.   if (h3[h]) return h3[h]->max;
  280.   else return 0;
  281. }
  282.  
  283. double * H3DGetSliceXYData(int h, int slice) {
  284.   if (h3[h]) return (h3[h]->data+slice*h3[h]->nx*h3[h]->ny);
  285.   else return NULL;
  286. }
  287.  
  288. double * H3DGetData(int h) {
  289.   if (h3[h]) return h3[h]->data;
  290.   else return NULL;
  291. }
  292.  
  293.