?? isob.c
字號:
#undef _DEBUG#include <stdio.h>#include <stdlib.h>/* ------------------ ASSERT ------------------------ */#ifndef ASSERT_DEFINED#define ASSERT_DEFINED#ifdef _DEBUG#ifdef CPP#define ASSERT_EXTERN extern "C"#else#define ASSERT_EXTERN#endifASSERT_EXTERN void _Assert(char *file, unsigned linenumber);#define ASSERT(f) if(f){}else{_Assert(__FILE__,__LINE__);}#define ASSERTFLAG(f) ASSERT((f)==0||(f)==1)#else#define ASSERT(f)#define ASSERTFLAG(f)#endif#endif#ifndef MALLOC_DEFINED#define MALLOC_DEFINED#ifndef pp_OSX#include <malloc.h>#endif#define memGarbage 0xA3typedef int mallocflag;typedef char bbyte;#define debugByte 0xE1#ifdef _DEBUG#define sizeofDebugByte 1#else#define sizeofDebugByte 0#endif#ifdef _DEBUG#define NewMemory(f,g) __NewMemory((f),(g),__FILE__,__LINE__)#define ResizeMemory(f,g) __ResizeMemory((f),(g),__FILE__,__LINE__)#else#define NewMemory(f,g) _NewMemory((f),(g))#define ResizeMemory(f,g) _ResizeMemory((f),(g))#endif#ifdef _DEBUGvoid _CheckMemory(void);void _CheckMemoryOn(void);void _CheckMemoryOff(void);#define CheckMemory _CheckMemory()#define CheckMemoryOn _CheckMemoryOn()#define CheckMemoryOff _CheckMemoryOff()char *_strcpy(char *s1, const char *s2);char *_strcat(char *s1, const char *s2);#define STRCPY(f,g) _strcpy((f),(g))#define STRCAT(f,g) _strcat((f),(g))#else#define CheckMemory#define CheckMemoryOn#define CheckMemoryOff#define STRCPY(f,g) strcpy((f),(g))#define STRCAT(f,g) strcat((f),(g))#endif#ifdef _DEBUGtypedef struct BLOCKINFO { struct BLOCKINFO *pbiNext; bbyte *pb; size_t size; mallocflag fref; char filename[256]; int linenumber;} blockinfo;mallocflag CreateBlockInfo(bbyte *pbNew, size_t sizeNew);void FreeBlockInfo(bbyte *pb);void UpdateBlockInfo(bbyte *pbOld, bbyte *pbNew, size_t sizeNew);size_t sizeofBlock(bbyte *pv);void PrintMemoryInfo(void);mallocflag __ResizeMemory(void **ppv, size_t sizeNew,char *file,int linenumber);mallocflag __NewMemory(void **ppv, size_t size,char *file,int linenumber);#endifmallocflag _ResizeMemory(void **ppv, size_t sizeNew);mallocflag _NewMemory(void **ppv, size_t size);void FreeMemory(void *pv);mallocflag ValidPointer(void *pv, size_t size);#endif#define FREEMEMORY(f) if((f)!=NULL){FreeMemory((f));(f)=NULL;}#ifndef DEF_ISOTEST2#define DEF_ISOTEST2#define INCPOINTS 100000#if defined(WIN32)#include <windows.h>#pragma warning (disable:4244) /* disable bogus conversion warnings */#pragma warning (disable:4305) /* disable bogus conversion warnings */#endif#ifdef pp_DRAWISO#include <GL/gl.h>#include <GL/glu.h>#endif#ifdef IN_ISOBOX#define SV_EXTERN#else#define SV_EXTERN extern#endif/* iso-surface definitions */typedef struct { float level; float *color; int dataflag; int nvertices, ntriangles, nnorm; /* actual number */ int maxvertices, maxtriangles, maxnorm; /* space reserved */ int colorindex; int normtype, defined, plottype; int *triangles; int *closestnodes; unsigned short *triangles2; unsigned char *triangles1; unsigned short *vertices, *tvertices; int *sortedlist,*rank; float xmin, ymin, zmin, xyzmaxdiff; float *xvert, *yvert, *zvert, *tvert; float tmin, tmax; float *xnorm, *ynorm, *znorm; short *norm, *vertexnorm;} isosurface;/* Seb. Henkel, FSG Changed for compilation on HPUX */#ifdef HPUX_NOAPPEND#define pp_noappend#endif/* End of Change */#ifndef pp_DRAWISO#ifndef pp_noappend#define CCisosurface2file iso2file_#define CCisosurfacet2file isot2file_#define CCisoheader isoheader_#define CCtisoheader tisoheader_#else#define CCisosurface2file iso2file#define CCisosurfacet2file isot2file#define CCtisoheader tisoheader#define CCisoheader isoheader#endifSV_EXTERN void CCisoheader(char *isofile, char *isolonglabel, char *isoshortlabel, char *isounits, float *levels, int *nlevels, int *error);SV_EXTERN void CCtisoheader(char *isofile, char *isolonglabel, char *isoshortlabel, char *isounits, float *levels, int *nlevels, int *error);SV_EXTERN void isoout(FILE *isostream,float t, int timeindex, isosurface *surface, int *error);#endif/* unsigned short *vertices, unsigned short *tvertices, int nvertices, // int *trilist, int ntrilist, // int *error);//SV_EXTERN void isoout(FILE *isostream, float t, int timeindex, // unsigned short *vertices, int nvertices, // int *trilist, int ntrilist, // int *error); */SV_EXTERN int CompressIsosurface(isosurface *surface, int reduce_triangles, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax);SV_EXTERN int UpdateIsosurface(isosurface *isodata, float *xvert, float *yvert, float *zvert, float *tvert, int *closestnodes, int nvert, int *trilist, int ntrilist); SV_EXTERN void DrawIsosurface(isosurface *isodata);SV_EXTERN void freesurface(isosurface *surfacedata);SV_EXTERN void InitIsosurface(isosurface *surfacedata, float level, float *color, int colorindex);SV_EXTERN int ResizeSurface(isosurface *surfacedata, int incvert, int inctrilist, int incnorm);SV_EXTERN void GetIsobox(float *x, float *y, float *z, float *vals, float *tvals, int *nodeindexes, float level, float *xvert, float *yvert, float *zvert, float *tvert, int *closestsnodes, int *nvert, int *trilist, int *ntrilist);SV_EXTERN int GetIsosurface(isosurface *surface, float *data, float *tdata, int *iblank, float level, float *xplt, int nx, float *yplt, int ny, float *zplt, int nz, int isooffset );SV_EXTERN void isosurface2file(char *isofile, float *t, float *data, int *iblank, float *level, int *nlevels, float *xplt, int *nx, float *yplt, int *ny, float *zplt, int *nz,int *isooffset, int *reduce_triangles, int *error);SV_EXTERN void ReduceToUnit(float v[3]);SV_EXTERN void calcNormal(float *v1, float *v2, float *v3, float *out);SV_EXTERN void calcNormal2(unsigned short *v1, unsigned short *v2, unsigned short *v3, float *out, float *area);SV_EXTERN int GetNormalSurface(isosurface *surfacedata);#endifvoid _Assert(char *filename, unsigned linenumber){ fflush(NULL); fprintf(stderr, "\nAssertion failed: %s, line %u\n",filename, linenumber); fflush(stderr); abort();}#include <stdlib.h>#include <stdio.h>#include <string.h>#ifdef _DEBUGstatic int checkmemoryflag=1;#endif/* ------------------ _NewMemory ------------------------ */mallocflag _NewMemory(void **ppv, size_t size){/* int temp; */ void **ppb=(void **)ppv;#ifdef _DEBUG char *c;#endif ASSERT(ppv != NULL && size != 0);/* temp=size+sizeofDebugByte; */ *ppb = (void *)malloc(size+sizeofDebugByte);#ifdef _DEBUG { CheckMemory; if(*ppb != NULL){ if(sizeofDebugByte!=0){ c = (char *)(*ppb) + size; *c=(char)debugByte; } memset(*ppb, memGarbage, size); if(!CreateBlockInfo(*ppb, size)){ free(*ppb); *ppb=NULL; } } ASSERT(*ppb !=NULL); }#endif return (*ppb != NULL);}/* ------------------ FreeMemory ------------------------ */void FreeMemory(void *pv){#ifdef _DEBUG int len_memory;#endif ASSERT(pv != NULL);#ifdef _DEBUG { CheckMemory; len_memory=sizeofBlock(pv); memset(pv, memGarbage, len_memory); FreeBlockInfo(pv); }#endif free(pv);}/* ------------------ ResizeMemory ------------------------ */mallocflag _ResizeMemory(void **ppv, size_t sizeNew){ bbyte **ppb = (bbyte **)ppv; bbyte *pbNew;#ifdef _DEBUG char *c; size_t sizeOld;#endif ASSERT(ppb != NULL && sizeNew != 0);#ifdef _DEBUG { CheckMemory; sizeOld = sizeofBlock(*ppb); if(sizeNew<sizeOld){ memset((*ppb)+sizeNew,memGarbage,sizeOld-sizeNew); } else if (sizeNew > sizeOld){ void *pbForceNew; if(_NewMemory((void **)&pbForceNew, sizeNew)){ memcpy(pbForceNew, *ppb, sizeOld); FreeMemory(*ppb); *ppb = pbForceNew; } } }#endif pbNew = (bbyte *)realloc(*ppb, sizeNew+sizeofDebugByte); if(pbNew != NULL){#ifdef _DEBUG { if(sizeofDebugByte!=0){ c = pbNew + sizeNew; *c=(char)debugByte; } UpdateBlockInfo(*ppb, pbNew, sizeNew); if(sizeNew>sizeOld){ memset(pbNew+sizeOld,memGarbage,sizeNew-sizeOld); } }#endif *ppb = pbNew; } return (pbNew != NULL);}#ifdef _DEBUG/* ------------------ pointer comparison defines ------------------------ */#define fPtrLess(pLeft, pRight) ((pLeft) < (pRight))#define fPtrGrtr(pLeft, pRight) ((pLeft) > (pRight))#define fPtrEqual(pLeft, pRight) ((pLeft) == (pRight))#define fPtrLessEq(pLeft, pRight) ((pLeft) <= (pRight))#define fPtrGrtrEq(pLeft, pRight) ((pLeft) >= (pRight))static blockinfo *GetBlockInfo(bbyte *pb);mallocflag __NewMemory(void **ppv, size_t size, char *file, int linenumber){ void **ppb=(void **)ppv; blockinfo *pbi; int return_code; return_code=_NewMemory(ppb,size); pbi=GetBlockInfo((bbyte *)*ppb); pbi->linenumber=linenumber; if(strlen(file)<256){ strcpy(pbi->filename,file); } else{ strncpy(pbi->filename,file,255); strcat(pbi->filename,(char)0); } return return_code;}/* ------------------ ResizeMemory ------------------------ */mallocflag __ResizeMemory(void **ppv, size_t size, char *file, int linenumber){ void **ppb=(void **)ppv; blockinfo *pbi; int return_code; return_code=_ResizeMemory(ppb,size); pbi=GetBlockInfo((bbyte *)*ppb); pbi->linenumber=linenumber; if(strlen(file)<256){ strcpy(pbi->filename,file); } else{ strncpy(pbi->filename,file,255); strcat(pbi->filename,(char)0); } return return_code;}static blockinfo *pbiHead = NULL;/* ------------------ GetBlockInfo ------------------------ */static blockinfo *GetBlockInfo(bbyte *pb){ blockinfo *pbi; for (pbi = pbiHead; pbi != NULL; pbi = pbi->pbiNext) { bbyte *pbStart = pbi->pb; bbyte *pbEnd = pbi->pb + pbi->size - 1; if(fPtrGrtrEq(pb, pbStart) && fPtrLessEq(pb, pbEnd)) break; } ASSERT(pbi != NULL); return (pbi);}/* ------------------ PrintMemoryInfo ------------------------ */void PrintMemoryInfo(void){ blockinfo *pbi; int n=0,size=0; for (pbi = pbiHead; pbi != NULL; pbi = pbi->pbiNext) { n++; size += pbi->size;/* printf("Block %i allocated in file=%s, linenumber=%i\n",n,pbi->filename,pbi->linenumber);*/ } printf("nblocks=%i sizeblocks=%i\n",n,size);}/* ------------------ GetBlockInfo_nofail ------------------------ */static blockinfo *GetBlockInfo_nofail(bbyte *pb){ blockinfo *pbi; for (pbi = pbiHead; pbi != NULL; pbi = pbi->pbiNext) { bbyte *pbStart = pbi->pb; bbyte *pbEnd = pbi->pb + pbi->size - 1; if(fPtrGrtrEq(pb, pbStart) && fPtrLessEq(pb, pbEnd)) break; } return (pbi);}/* ------------------ _CheckMemoryOn ------------------------ */void _CheckMemoryOn(void){ checkmemoryflag=1;}/* ------------------ _CheckMemoryOff ------------------------ */void _CheckMemoryOff(void){ checkmemoryflag=0;}/* ------------------ _CheckMemory ------------------------ */void _CheckMemory(void){ blockinfo *pbi; if(checkmemoryflag==0)return; for (pbi = pbiHead; pbi != NULL; pbi = pbi->pbiNext) { if(sizeofDebugByte!=0)ASSERT((char)*(pbi->pb+pbi->size)==(char)debugByte); } return;}/* ------------------ CreateBlockInfo ------------------------ */mallocflag CreateBlockInfo(bbyte *pbNew, size_t sizeNew){ blockinfo *pbi; ASSERT(pbNew != NULL && sizeNew != 0); pbi = (blockinfo *)malloc(sizeof(blockinfo)); if( pbi != NULL){ pbi->pb = pbNew; pbi->size = sizeNew; pbi->pbiNext = pbiHead; pbiHead = pbi; } return (mallocflag)(pbi != NULL);}/* ------------------ FreeBlockIfno ------------------------ */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -