PageRenderTime 86ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/include/misc_lib.h

https://bitbucket.org/nahumfarchi/software-renderer
C Header | 447 lines | 379 code | 46 blank | 22 comment | 8 complexity | 3d7f7869812d2de678281ca4c45c3acc MD5 | raw file
  1. /******************************************************************************
  2. * Misc_lib.h - header file for the misc. library. *
  3. * This header is also the interface header to the world. *
  4. *******************************************************************************
  5. * (C) Gershon Elber, Technion, Israel Institute of Technology *
  6. *******************************************************************************
  7. * Written by Gershon Elber, Oct. 94. *
  8. ******************************************************************************/
  9. #ifndef MISC_LIB_H
  10. #define MISC_LIB_H
  11. #include <stdio.h>
  12. #ifdef __WINCE__
  13. # include <memory.h>
  14. #endif
  15. #include "irit_sm.h"
  16. #ifdef USE_VARARGS
  17. #include <varargs.h>
  18. #else
  19. #include <stdarg.h>
  20. #endif /* USE_VARARGS */
  21. typedef enum {
  22. MISC_ERR_MALLOC_FAILED,
  23. MISC_ERR_CONFIG_FILE_NO_FOUND,
  24. MISC_ERR_CONFIG_ERROR,
  25. MISC_ERR_UNKNOWN_CONFIG,
  26. MISC_ERR_UNDEFINE_ERR
  27. } MiscFatalErrorType;
  28. typedef enum {
  29. IC_NONE_TYPE = 0,
  30. IC_BOOLEAN_TYPE,
  31. IC_INTEGER_TYPE,
  32. IC_REAL_TYPE,
  33. IC_STRING_TYPE,
  34. IC_MULTI_STR_TYPE
  35. } IrtCfgDataType;
  36. typedef enum {
  37. IRIT_IMAGE_UNKNOWN_TYPE,
  38. IRIT_IMAGE_RLE_TYPE,
  39. IRIT_IMAGE_PPM3_TYPE,
  40. IRIT_IMAGE_PPM6_TYPE,
  41. IRIT_IMAGE_PNG_TYPE
  42. } IrtImgImageType;
  43. typedef struct IritConfigStruct {
  44. char *VarName;
  45. char *SomeInfo;
  46. VoidPtr VarData;
  47. IrtCfgDataType VarType;
  48. } IritConfigStruct;
  49. typedef struct IrtImgPixelStruct {
  50. IrtBType r, g, b;
  51. } IrtImgPixelStruct;
  52. typedef struct IrtImgRGBAPxlStruct {
  53. IrtBType r, g, b, a;
  54. } IrtImgRGBAPxlStruct;
  55. typedef float IrtImgRealClrType;
  56. typedef struct IrtImgRealPxlStruct {
  57. IrtImgRealClrType r, g, b, a;
  58. } IrtImgRealPxlStruct;
  59. typedef struct IritPriorQue {
  60. struct IritPriorQue *Right, *Left; /* Pointers to two sons of this node. */
  61. VoidPtr Data; /* Pointers to the data itself. */
  62. } IritPriorQue;
  63. typedef struct IritHashElementStruct {
  64. struct IritHashElementStruct *Pnext;
  65. VoidPtr Data;
  66. IrtRType Key;
  67. } IritHashElementStruct;
  68. typedef struct IritHashTableStruct {
  69. IrtRType MinKeyVal, MaxKeyVal, DKey, KeyEps;
  70. IritHashElementStruct **Vec;
  71. int VecSize;
  72. } IritHashTableStruct;
  73. typedef void (*IritFatalMsgFuncType)(char *Msg);
  74. typedef void (*IritWarningMsgFuncType)(char *Msg);
  75. typedef void (*IritInfoMsgFuncType)(char *Msg);
  76. typedef void (*MiscSetErrorFuncType)(MiscFatalErrorType, char *);
  77. typedef int (*IritPQCompFuncType)(VoidPtr, VoidPtr);/* Comparison functions. */
  78. typedef int (*IritHashCmpFuncType)(VoidPtr Data1, VoidPtr Data2);
  79. typedef void IritLevenEvalFuncType(IrtRType *CurPoint,
  80. IrtRType ModelParams[],
  81. IrtRType *YPointer,
  82. IrtRType YdParams[]);
  83. typedef void IritLevenNumerProtectionFuncType(IrtRType InternalModelParams[]);
  84. typedef int IritLevenIsModelValidFuncType(IrtRType InternalModelParams[]);
  85. #if defined(__cplusplus) || defined(c_plusplus)
  86. extern "C" {
  87. #endif
  88. /* Basic dynamic memory allocation routines: */
  89. #ifdef DEBUG_IRIT_MALLOC
  90. VoidPtr IritMalloc(unsigned Size, char *ObjType, char *FileName, int LineNum);
  91. #define IritMalloc(x) IritMalloc((x), #x, __FILE__, __LINE__)
  92. void IritFree(VoidPtr p);
  93. #define IritFree(x) { IritFree(x); x = NULL; }
  94. void IritTestAllDynMemory(void);
  95. void IritInitTestDynMemory(void);
  96. void IritInitTestDynMemory2(int DebugMalloc,
  97. IritIntPtrSizeType DebugSearchPtr,
  98. int DebugSearchPtrAbort);
  99. void IritCheckMarkDynMemory(IrtRType *Start);
  100. void IritDebugMallocSearchPtr(VoidPtr p, int Abort);
  101. #else
  102. #define IritMalloc(Size) malloc(Size)
  103. #define IritFree(Ptr) free(Ptr)
  104. #endif /* DEBUG_IRIT_MALLOC */
  105. VoidPtr IritRealloc(VoidPtr p, unsigned OldSize, unsigned NewSize);
  106. /* Prototype of the configuration routines: */
  107. int IritConfig(const char *PrgmName,
  108. const IritConfigStruct *SetUp,
  109. int NumVar);
  110. void IritConfigPrint(const IritConfigStruct *SetUp, int NumVar);
  111. int IritConfigSave(const char *FileName,
  112. const IritConfigStruct *SetUp,
  113. int NumVar);
  114. /* Get command line arguments. */
  115. #ifdef USE_VARARGS
  116. int GAGetArgs(int va_alist, ...);
  117. #else
  118. int GAGetArgs(int argc, ...);
  119. #endif /* USE_VARARGS */
  120. char *GAStringErrMsg(int Error, char *OutStr);
  121. void GAPrintErrMsg(int Error);
  122. char *GAStringHowTo(const char *CtrlStr, char *OutStr);
  123. void GAPrintHowTo(const char *CtrlStr);
  124. /* Homogeneous 4x4 matrix routines. */
  125. void MatGenUnitMat(IrtHmgnMatType Mat);
  126. int MatIsUnitMatrix(IrtHmgnMatType Mat, IrtRType Eps);
  127. void MatGenMatTrans(IrtRType Tx, IrtRType Ty, IrtRType Tz, IrtHmgnMatType Mat);
  128. void MatGenMatUnifScale(IrtRType Scale, IrtHmgnMatType Mat);
  129. void MatGenMatScale(IrtRType Sx, IrtRType Sy, IrtRType Sz, IrtHmgnMatType Mat);
  130. void MatGenMatRotX1(IrtRType Teta, IrtHmgnMatType Mat);
  131. void MatGenMatRotX(IrtRType CosTeta, IrtRType SinTeta, IrtHmgnMatType Mat);
  132. void MatGenMatRotY1(IrtRType Teta, IrtHmgnMatType Mat);
  133. void MatGenMatRotY(IrtRType CosTeta, IrtRType SinTeta, IrtHmgnMatType Mat);
  134. void MatGenMatRotZ1(IrtRType Teta, IrtHmgnMatType Mat);
  135. void MatGenMatRotZ(IrtRType CosTeta, IrtRType SinTeta, IrtHmgnMatType Mat);
  136. void MatMultTwo4by4(IrtHmgnMatType MatRes,
  137. IrtHmgnMatType Mat1,
  138. IrtHmgnMatType Mat2);
  139. void MatAddTwo4by4(IrtHmgnMatType MatRes,
  140. IrtHmgnMatType Mat1,
  141. IrtHmgnMatType Mat2);
  142. void MatSubTwo4by4(IrtHmgnMatType MatRes,
  143. IrtHmgnMatType Mat1,
  144. IrtHmgnMatType Mat2);
  145. void MatScale4by4(IrtHmgnMatType MatRes,
  146. IrtHmgnMatType Mat,
  147. const IrtRType *Scale);
  148. void MatMultVecby4by4(IrtVecType VecRes,
  149. const IrtVecType Vec,
  150. IrtHmgnMatType Mat);
  151. void MatMultPtby4by4(IrtPtType PtRes,
  152. const IrtPtType Pt,
  153. IrtHmgnMatType Mat);
  154. void MatMultWVecby4by4(IrtRType VRes[4],
  155. const IrtRType Vec[4],
  156. IrtHmgnMatType Mat);
  157. IrtRType MatDeterminantMatrix(IrtHmgnMatType Mat);
  158. int MatInverseMatrix(IrtHmgnMatType M, IrtHmgnMatType InvM);
  159. void MatTranspMatrix(IrtHmgnMatType M, IrtHmgnMatType TranspM);
  160. IrtRType MatScaleFactorMatrix(IrtHmgnMatType M);
  161. void MatRotateFactorMatrix(IrtHmgnMatType M, IrtHmgnMatType RotMat);
  162. void MatTranslateFactorMatrix(IrtHmgnMatType M, IrtVecType Trans);
  163. /* General matrix routines. */
  164. void MatGnrlCopy(IrtGnrlMatType Dst, IrtGnrlMatType Src, int n);
  165. void MatGnrlUnitMat(IrtGnrlMatType Mat, int n);
  166. int MatGnrlIsUnitMatrix(IrtGnrlMatType Mat, IrtRType Eps, int n);
  167. void MatGnrlMultTwoMat(IrtGnrlMatType MatRes,
  168. IrtGnrlMatType Mat1,
  169. IrtGnrlMatType Mat2,
  170. int n);
  171. void MatGnrlAddTwoMat(IrtGnrlMatType MatRes,
  172. IrtGnrlMatType Mat1,
  173. IrtGnrlMatType Mat2,
  174. int n);
  175. void MatGnrlSubTwoMat(IrtGnrlMatType MatRes,
  176. IrtGnrlMatType Mat1,
  177. IrtGnrlMatType Mat2,
  178. int n);
  179. void MatGnrlScaleMat(IrtGnrlMatType MatRes,
  180. IrtGnrlMatType Mat,
  181. IrtRType *Scale,
  182. int n);
  183. void MatGnrlMultVecbyMat(IrtVecGnrlType VecRes,
  184. IrtGnrlMatType Mat,
  185. IrtVecGnrlType Vec,
  186. int n);
  187. void MatGnrlMultVecbyMat2(IrtVecGnrlType VecRes,
  188. IrtVecGnrlType Vec,
  189. IrtGnrlMatType Mat,
  190. int n);
  191. int MatGnrlInverseMatrix(IrtGnrlMatType M,
  192. IrtGnrlMatType InvM,
  193. int n);
  194. void MatGnrlTranspMatrix(IrtGnrlMatType M,
  195. IrtGnrlMatType TranspM,
  196. int n);
  197. IrtRType MatGnrlDetMatrix(IrtGnrlMatType M, int n);
  198. int MatGnrlOrthogonalSubspace(IrtGnrlMatType M, int n);
  199. void MatGnrlPrintMatrix(IrtGnrlMatType M, int n, FILE *F);
  200. /* QR matrix factorization. */
  201. int IritQRFactorization(IrtRType *A,
  202. int n,
  203. int m,
  204. IrtRType *Q,
  205. IrtRType *R);
  206. int IritSolveUpperDiagMatrix(const IrtRType *A,
  207. int n,
  208. const IrtRType *b,
  209. IrtRType *x);
  210. int IritSolveLowerDiagMatrix(const IrtRType *A,
  211. int n,
  212. const IrtRType *b,
  213. IrtRType *x);
  214. int IritQRUnderdetermined(IrtRType *A,
  215. IrtRType *x,
  216. const IrtRType *b,
  217. int m,
  218. int n);
  219. /* Gauss Jordan matrix solver and Levenberg Marquardt local minimum finder. */
  220. int IritGaussJordan(IrtRType *A, IrtRType *B, unsigned N, unsigned M);
  221. IrtRType IritLevenMarMin(IrtRType **x,
  222. IrtRType y[],
  223. IrtRType Sigma[],
  224. unsigned NumberOfDataElements,
  225. IrtRType ModelParams[],
  226. IritLevenEvalFuncType *ShapeFunc,
  227. IritLevenNumerProtectionFuncType *ProtectionFunc,
  228. IritLevenIsModelValidFuncType *ModelValidatorFunc,
  229. unsigned NumberOfModelParams,
  230. IrtRType Tolerance);
  231. IrtRType IritLevenMarMinSig1(IrtRType **XVals,
  232. IrtRType YVals[],
  233. unsigned NumberOfDataElements,
  234. IrtRType ModelParams[],
  235. IritLevenEvalFuncType *ShapeFunc,
  236. IritLevenNumerProtectionFuncType *ProtectionFunc,
  237. IritLevenIsModelValidFuncType *ModelValidatorFunc,
  238. unsigned NumberOfMedelParams,
  239. IrtRType Tolerance);
  240. unsigned int IritLevenMarSetMaxIterations(unsigned NewVal);
  241. /* An implementation of a priority queue. */
  242. void IritPQInit(IritPriorQue **PQ);
  243. int IritPQEmpty(IritPriorQue *PQ);
  244. void IritPQCompFunc(IritPQCompFuncType NewCompFunc);
  245. VoidPtr IritPQFirst(IritPriorQue **PQ, int Delete);
  246. VoidPtr IritPQInsert(IritPriorQue **PQ, VoidPtr NewItem);
  247. VoidPtr IritPQDelete(IritPriorQue **PQ, VoidPtr NewItem);
  248. VoidPtr IritPQFind(IritPriorQue *PQ, VoidPtr OldItem);
  249. VoidPtr IritPQNext(IritPriorQue *PQ, VoidPtr CmpItem, VoidPtr BiggerThan);
  250. int IritPQSize(IritPriorQue *PQ);
  251. void IritPQPrint(IritPriorQue *PQ, void (*PrintFunc)(VoidPtr));
  252. void IritPQFree(IritPriorQue *PQ, int FreeItems);
  253. void IritPQFreeFunc(IritPriorQue *PQ, void (*FreeFunc)(VoidPtr));
  254. /* An implementation of a hashing table. */
  255. IritHashTableStruct *IritHashTableCreate(IrtRType MinKeyVal,
  256. IrtRType MaxKeyVal,
  257. IrtRType KeyEps,
  258. int VecSize);
  259. int IritHashTableInsert(IritHashTableStruct *IHT,
  260. VoidPtr Data,
  261. IritHashCmpFuncType HashCmpFunc,
  262. IrtRType Key,
  263. int RplcSame);
  264. VoidPtr IritHashTableFind(IritHashTableStruct *IHT,
  265. VoidPtr Data,
  266. IritHashCmpFuncType HashCmpFunc,
  267. IrtRType Key);
  268. int IritHashTableRemove(IritHashTableStruct *IHT,
  269. VoidPtr Data,
  270. IritHashCmpFuncType HashCmpFunc,
  271. IrtRType Key);
  272. void IritHashTableFree(IritHashTableStruct *IHT);
  273. /* Read/Write of images. */
  274. int IrtImgReadImageXAlign(int Alignment);
  275. IrtImgPixelStruct *IrtImgReadImage(const char *ImageFileName,
  276. int *MaxX,
  277. int *MaxY,
  278. int *Alpha);
  279. IrtImgPixelStruct *IrtImgReadImage2(const char *ImageFileName,
  280. int *MaxX,
  281. int *MaxY,
  282. int *Alpha);
  283. void IrtImgReadClrCache(void);
  284. IrtImgImageType IrtImgWriteSetType(const char *ImageType);
  285. int IrtImgWriteOpenFile(const char **argv,
  286. const char *FName,
  287. int Alpha,
  288. int XSize,
  289. int YSize);
  290. void IrtImgWritePutLine(IrtBType *Alpha, IrtImgPixelStruct *Pixels);
  291. void IrtImgWriteCloseFile(void);
  292. IrtImgPixelStruct *IrtImgFlipXYImage(IrtImgPixelStruct *Img,
  293. int MaxX,
  294. int MaxY,
  295. int Alpha);
  296. int IrtImgParsePTextureString(const char *PTexture,
  297. char *FName,
  298. IrtRType *Scale,
  299. int *Flip);
  300. /* Searching routines. */
  301. VoidPtr IritSearch2DInit(IrtRType XMin,
  302. IrtRType XMax,
  303. IrtRType YMin,
  304. IrtRType YMax,
  305. IrtRType Tol,
  306. int DataSize);
  307. void IritSearch2DFree(VoidPtr S2D);
  308. void IritSearch2DInsertElem(VoidPtr S2D,
  309. IrtRType XKey,
  310. IrtRType YKey,
  311. VoidPtr Data);
  312. int IritSearch2DFindElem(VoidPtr S2D,
  313. IrtRType XKey,
  314. IrtRType YKey,
  315. VoidPtr Data);
  316. /* Set cover related routines. */
  317. VoidPtr IritRLNew(void);
  318. void IritRLAdd(VoidPtr RLC, IrtRType l, IrtRType r, int attr);
  319. int *IritRLFindCyclicCover(VoidPtr RLC, IrtRType Tol);
  320. void IritRLDelete(VoidPtr RLC);
  321. int IritRLSetGaurdiansNumber(int g);
  322. /* XGeneral routine - compatibility between Unix and Win95/WinNT/OS2/etc. */
  323. char *IritStrdup(const char *s);
  324. char *IritStrUpper(char *s);
  325. void IritSleep(int MiliSeconds);
  326. void IritRandomInit(long Seed);
  327. IrtRType IritRandom(IrtRType Min, IrtRType Max);
  328. IrtRType IritCPUTime(int Reset);
  329. char *IritRealTimeDate(void);
  330. IrtRType IritApproxStrStrMatch(const char *Str1,
  331. const char *Str2,
  332. int IgnoreCase);
  333. #ifndef AMIGA
  334. void movmem(VoidPtr Src, VoidPtr Dest, int Len);
  335. #endif /* AMIGA */
  336. const char *searchpath(const char *Name);
  337. #ifdef STRICMP
  338. int strnicmp(const char *s1, const char *s2, int n);
  339. int stricmp(const char *s1, const char *s2);
  340. #else
  341. #if !(defined(__WINNT__) || defined(__WINCE__) || defined(OS2GCC) || defined(AMIGA) || defined(__CYGWIN__))
  342. # define strnicmp(s1, s2, n) strncasecmp((s1), (s2), (n))
  343. # define stricmp(s1, s2) strcasecmp((s1), (s2))
  344. #endif /* !(__WINNT__ || __WINCE__|| OS2GCC || AMIGA) */
  345. #ifdef __WINNT__
  346. # if _MSC_VER >= 1400 /* Visual 8, 2005 */
  347. # define strnicmp(s1, s2, n) _strnicmp((s1), (s2), (n))
  348. # define stricmp(s1, s2) _stricmp((s1), (s2))
  349. # endif /* _MSC_VER >= 1400 */
  350. #endif /* __WINNT__ */
  351. #ifdef __WINCE__
  352. # ifdef strnicmp
  353. # undef strnicmp
  354. # undef stricmp
  355. # endif /* strnicmp */
  356. # define strnicmp(s1, s2, n) _strnicmp((s1), (s2), (n))
  357. # define stricmp(s1, s2) _stricmp((s1), (s2))
  358. #endif /* __WINCE__ */
  359. #endif /* STRICMP */
  360. #ifdef __WINNT__
  361. # if _MSC_VER >= 1400 /* Visual 8, 2005 */
  362. # ifdef getcwd
  363. # undef getcwd
  364. # endif /* getcwd */
  365. # define getcwd(buf, maxlen) _getcwd(buf, maxlen)
  366. # define chdir(dir) _chdir(dir)
  367. # define setmode(fd, mode) _setmode(fd, mode)
  368. # define putenv(str) _putenv(str)
  369. # endif /* _MSC_VER >= 1400 */
  370. #endif /* __WINNT__ */
  371. const char *IritStrIStr(const char *s, const char *Pattern);
  372. #ifdef STRSTR
  373. char *strstr(const char *s, const char *Pattern);
  374. #endif /* STRSTR */
  375. #ifdef GETCWD
  376. char *getcwd(char *s, int Len);
  377. #endif /* GETCWD */
  378. /* Error handling. */
  379. MiscSetErrorFuncType MiscSetFatalErrorFunc(MiscSetErrorFuncType ErrorFunc);
  380. void MiscFatalError(MiscFatalErrorType ErrID, char *ErrDesc);
  381. const char *MiscDescribeError(MiscFatalErrorType ErrorNum);
  382. int IritLineHasCntrlChar(const char *Line);
  383. IritFatalMsgFuncType IritSetFatalErrorFunc(IritFatalMsgFuncType FatalMsgFunc);
  384. void IritFatalError(char *Msg);
  385. IritWarningMsgFuncType IritSetWarningMsgFunc(IritWarningMsgFuncType WrnMsgFunc);
  386. void IritWarningMsg(char *Msg);
  387. IritInfoMsgFuncType IritSetInfoMsgFunc(IritInfoMsgFuncType InfoMsgFunc);
  388. void IritInformationMsg(char *Msg);
  389. #ifdef USE_VARARGS
  390. void IritFatalErrorPrintf(const char *va_alist, ...);
  391. void IritWarningMsgPrintf(const char *va_alist, ...);
  392. void IritInformationMsgPrintf(const char *va_alist, ...);
  393. #else
  394. void IritFatalErrorPrintf(const char *Format, ...);
  395. void IritWarningMsgPrintf(const char *Format, ...);
  396. void IritInformationMsgPrintf(const char *Format, ...);
  397. #endif /* USE_VARARGS */
  398. #if defined(__cplusplus) || defined(c_plusplus)
  399. }
  400. #endif
  401. #endif /* MISC_LIB_H */