[774] | 1 | #ifndef GFX_STD_INCLUDED // -*- C++ -*-
|
---|
| 2 | #define GFX_STD_INCLUDED
|
---|
| 3 |
|
---|
| 4 | /************************************************************************
|
---|
| 5 |
|
---|
| 6 | Standard base include file for all gfx-based programs. This defines
|
---|
| 7 | various common stuff that is used elsewhere.
|
---|
| 8 |
|
---|
| 9 | $Id: std.h,v 1.8 1997/06/25 14:12:31 garland Exp $
|
---|
| 10 |
|
---|
| 11 | ************************************************************************/
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | #include <stdlib.h>
|
---|
| 15 | #include <string.h>
|
---|
| 16 | #include <math.h>
|
---|
| 17 | #include <iostream>
|
---|
| 18 |
|
---|
| 19 |
|
---|
[1025] | 20 | namespace simplif
|
---|
[774] | 21 | {
|
---|
| 22 | //
|
---|
| 23 | // Define the real (ie. default floating point) type
|
---|
| 24 | //
|
---|
| 25 | #ifdef GFX_REAL_FLOAT
|
---|
| 26 | typedef float real;
|
---|
| 27 | #else
|
---|
| 28 | #define GFX_REAL_DOUBLE
|
---|
| 29 | typedef double real;
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | //
|
---|
| 33 | // Define the boolean type and true/false constants.
|
---|
| 34 | //
|
---|
| 35 | /*#ifndef GFX_NO_BOOL
|
---|
| 36 | typedef int bool;
|
---|
| 37 | const int false = 0;
|
---|
| 38 | const int true = 1;
|
---|
| 39 | #endif
|
---|
| 40 | */
|
---|
| 41 | // Define True/False as synonyms for true/false
|
---|
| 42 | #ifndef GFX_NO_BOOL_MACROS
|
---|
| 43 | # ifndef True
|
---|
| 44 | # define True true
|
---|
| 45 | # define False false
|
---|
| 46 | # endif
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
| 49 | // Handle platforms which don't define M_PI in <math.h>
|
---|
| 50 | #ifndef M_PI
|
---|
| 51 | #define M_PI 3.14159265358979323846
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 | //
|
---|
| 55 | // Define min/max.
|
---|
| 56 | // These are defined as inlined template functions because that's a more
|
---|
| 57 | // C++ish thing to do.
|
---|
| 58 | //
|
---|
| 59 | #ifndef GFX_NO_MINMAX
|
---|
| 60 | # ifndef min
|
---|
| 61 | template<class T>
|
---|
| 62 | inline T min(T a, T b) { return (a < b)?a:b; }
|
---|
| 63 |
|
---|
| 64 | template<class T>
|
---|
| 65 | inline T max(T a, T b) { return (a > b)?a:b; }
|
---|
| 66 | # endif
|
---|
| 67 | #endif
|
---|
| 68 |
|
---|
| 69 | //
|
---|
| 70 | // For the old school, we also have MIN/MAX defined as macros.
|
---|
| 71 | //
|
---|
| 72 | #ifndef MIN
|
---|
| 73 | #define MIN(a,b) (((a)>(b))?(b):(a))
|
---|
| 74 | #define MAX(a,b) (((a)>(b))?(a):(b))
|
---|
| 75 | #endif
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | #ifndef ABS
|
---|
| 79 | #define ABS(x) (((x)<0)?-(x):(x))
|
---|
| 80 | #endif
|
---|
| 81 |
|
---|
| 82 | #ifdef GFX_DEF_FMATH
|
---|
| 83 | #define fabsf(a) ((float)fabs((double)a))
|
---|
| 84 | #define cosf(a) ((float)cos(double)a)
|
---|
| 85 | #define sinf(a) ((float)sin(double)a)
|
---|
| 86 | #endif
|
---|
| 87 |
|
---|
| 88 | #ifndef FEQ_EPS
|
---|
| 89 | #define FEQ_EPS 1e-6
|
---|
| 90 | #define FEQ_EPS2 1e-12
|
---|
| 91 | #endif
|
---|
| 92 | inline bool FEQ(double a,double b,double eps=FEQ_EPS) { return fabs(a-b)<eps; }
|
---|
| 93 | inline bool FEQ(float a,float b,float eps=FEQ_EPS) { return fabsf(a-b)<eps; }
|
---|
| 94 |
|
---|
| 95 | #ifndef GFX_NO_AXIS_NAMES
|
---|
| 96 | enum Axis {X=0, Y=1, Z=2, W=3};
|
---|
| 97 | #endif
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | #define fatal_error(s) report_error(s,__FILE__,__LINE__)
|
---|
| 102 |
|
---|
| 103 | // #ifdef assert
|
---|
| 104 | // # undef assert
|
---|
| 105 | // #endif
|
---|
| 106 | // #define assert(i) (i)?((void)NULL):assert_failed(# i,__FILE__,__LINE__)
|
---|
| 107 |
|
---|
| 108 | #ifdef SAFETY
|
---|
| 109 | # define AssertBound(t) assert(t)
|
---|
| 110 | #else
|
---|
| 111 | # define AssertBound(t)
|
---|
| 112 | #endif
|
---|
| 113 |
|
---|
| 114 | //
|
---|
| 115 | // Define the report_error and assert_failed functions.
|
---|
| 116 | //
|
---|
| 117 | inline void report_error(char *msg,char *file,int line)
|
---|
| 118 | {
|
---|
| 119 | std::cerr << msg << " ("<<file<<":"<<line<<")"<<std::endl;
|
---|
| 120 | exit(1);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | inline void assert_failed(char *text,char *file,int line)
|
---|
| 124 | {
|
---|
| 125 | std::cerr << "Assertion failed: {" << text <<"} at ";
|
---|
| 126 | std::cerr << file << ":" << line << std::endl;
|
---|
| 127 | abort();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | inline void assertf(int test, char *msg,
|
---|
| 131 | char *file=__FILE__, int line=__LINE__)
|
---|
| 132 | {
|
---|
| 133 | if( !test )
|
---|
| 134 | {
|
---|
| 135 | std::cerr << "Assertion failed: " << msg << " at ";
|
---|
| 136 | std::cerr << file << ":" << line << std::endl;
|
---|
| 137 | abort();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | // GFX_STD_INCLUDED
|
---|
| 143 | #endif
|
---|