BioinstSim  2
 All Classes Functions Variables
common.h
1 #ifndef COMMON_H_
2 #define COMMON_H_
3 
4 #include <list>
5 #include <vector>
6 using namespace std;
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12 
13 #include "simobject.h"
14 #include "arguments.h"
15 
16 typedef struct {
17  double x;
18  double y;
19 } TVector2d;
20 
21 extern double GetDistanceBetweenPositions(const TVector2d* pt_pos1, const TVector2d* pt_pos2);
22 extern double GetSquaredDistanceBetweenPositions(const TVector2d* pt_pos1, const TVector2d* pt_pos2);
23 
24 // Returns the squared length of a vector
25 #define Vec2dLengthSquared(vec) (vec.x*vec.x + vec.y*vec.y)
26 
27 // Returns the length of a vector
28 #define Vec2dLength(vec) sqrt(Vec2dLengthSquared(vec))
29 
30 
31 // Multiply a vector by a scalar
32 #define Vec2dMultiplyScalar(vec, scalar) \
33  { \
34  vec.x *= scalar; \
35  vec.y *= scalar; \
36  }
37 
38 #define Vec2dSub(result, A, B) \
39  { \
40  result.x = B.x - A.x; \
41  result.y = B.y - A.y; \
42  }
43 
44 // Adds two vectors:
45 #define Vec2dAdd(result, A, B) \
46  { \
47  result.x = B.x + A.x; \
48  result.y = B.y + A.y; \
49  }
50 
51 // Find the cos to the angle between two vectors
52 #define Vec2dCosAngle(vec1, vec2) \
53  ((vec1.x * vec2.x + vec1.y * vec2.y) / (Vec2dLength(vec1) * Vec2dLength(vec2)))
54 
55 // Find the angle between two vectors
56 #define Vec2dAngle(vec1, vec2) \
57  (acos(Vec2dCosAngle(vec1, vec2)))
58 
59 // Find the angle of one vector
60 #define Vec2dOwnAngle(vec) \
61  (atan2(vec.y, vec.x))
62 
63 #define Vec2dNormalize(vec) \
64  { \
65  double length___ = Vec2dLength(vec); \
66  vec.x /= length___; \
67  vec.y /= length___; \
68  }
69 
70 
71 // Returns the normalized angle in the range [0,2*M_PI)
72 #define NormalizeAngle(ang) \
73  (ang < 0.0 ? fmod(ang, 2.0*M_PI)+2.0*M_PI : fmod(ang, 2.0*M_PI))
74 
75 // Returns the normalized angle in the range [-PI,PI)
76 #define NormalizeAngleNegativePIPositivePI(ang) \
77  (NormalizeAngle(ang) > M_PI ? NormalizeAngle(ang) - 2.0*M_PI : NormalizeAngle(ang))
78 
79 // Rotate a vector:
80 #define Vec2dRotate(angle, vec) \
81  { \
82  double xt_ = vec.x; \
83  vec.x = cos(angle) * vec.x - sin(angle) * vec.y; \
84  vec.y = cos(angle) * vec.y + sin(angle) * xt_; \
85  }
86 
87 
88 
89 #define PI 3.14159265
90 #define EPSILON 1e-10
91 
92 #define ERRENDL fprintf(stderr, "\n");
93 #define PRINTPOS(label, vec) printf("%s, x: %2.6f, y: %2.6f\n", label, vec.x, vec.y);
94 #define PRINTVEC2(label, vec) printf("%s, x: %2.6f, y: %2.6f\n", label, vec.x, vec.y);
95 #define FILEANDLINE { fprintf(stderr, "In %s:%d: ", __FILE__, __LINE__); }
96 #define ERROR(s) { FILEANDLINE; fprintf(stderr, s); ERRENDL; }
97 #define ERROR1(s, p1) { FILEANDLINE; fprintf(stderr, s, p1); ERRENDL; }
98 #define ERROR2(s, p1, p2) { FILEANDLINE; fprintf(stderr, s, p1, p2); ERRENDL; }
99 #define ERROR3(s, p1, p2, p3) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3); ERRENDL; }
100 #define ERROR4(s, p1, p2, p3, p4) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4 ); ERRENDL; }
101 #define ERROR5(s, p1, p2, p3, p4, p5) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4, p5 ); ERRENDL; }
102 #define ERROR6(s, p1, p2, p3, p4, p5, p6) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4, p5, p6 ); ERRENDL; }
103 #define ERROR7(s, p1, p2, p3, p4, p5, p6, p7 ) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4, p5, p6, p7 ); ERRENDL; }
104 #define ERROR8(s, p1, p2, p3, p4, p5, p6, p7, p8 ) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4, p5, p6, p7, p8 ); ERRENDL; }
105 #define ERROR9(s, p1, p2, p3, p4, p5, p6, p7, p8, p9 ) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4, p5, p6, p7, p8, p9 ); ERRENDL; }
106 #define ERROR10(s, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 ) { FILEANDLINE; fprintf(stderr, s, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 ); ERRENDL; }
107 
108 // If debugging is defined then:
109 #ifdef DEBUG
110 
111 #include <stdio.h>
112 
113 #define DEBUGOUT(s) { fprintf(stderr, s); }
114 #define DEBUGOUT1(s, p1) { fprintf(stderr, s, p1); }
115 #define DEBUGOUT2(s, p1, p2) { fprintf(stderr, s, p1, p2); }
116 #define DEBUGOUT3(s, p1, p2, p3) { fprintf(stderr, s, p1, p2, p3); }
117 #define DEBUGOUT4(s, p1, p2, p3, p4) { fprintf(stderr, s, p1, p2, p3, p4 ); }
118 #else
119 // Otherwise simply define the macros as being empty:
120 #define DEBUGOUT(s)
121 #define DEBUGOUT1(s, p1)
122 #define DEBUGOUT2(s, p1, p2)
123 #define DEBUGOUT3(s, p1, p2, p3)
124 #define DEBUGOUT4(s, p1, p2, p3, p4)
125 #endif
126 
127 
128 #endif