浏览代码

Slimmed down math/math.h

Thomas Buck 11 年前
父节点
当前提交
b346206d92
共有 3 个文件被更改,包括 27 次插入68 次删除
  1. 12
    11
      include/math/math.h
  2. 3
    1
      src/OpenRaider.cpp
  3. 12
    56
      src/math/math.cpp

+ 12
- 11
include/math/math.h 查看文件

4
  * \brief Vector and Matrix math
4
  * \brief Vector and Matrix math
5
  *
5
  *
6
  * \author Mongoose
6
  * \author Mongoose
7
+ * \author xythobuz
7
  */
8
  */
8
 
9
 
9
 #include <math.h>
10
 #include <math.h>
11
 #ifndef _MATH_MATH_H
12
 #ifndef _MATH_MATH_H
12
 #define _MATH_MATH_H
13
 #define _MATH_MATH_H
13
 
14
 
14
-#define HEL_PI           ((float)M_PI) //!< pi
15
-#define HEL_2_PI         (HEL_PI * 2.0f) //!< pi*2
16
-#define HEL_PI_OVER_4    (HEL_PI / 4.0f) //!< pi/4
17
-#define HEL_PI_OVER_180  (HEL_PI / 180.0f) //!< pi/180
18
-#define HEL_180_OVER_PI  (180.0f / HEL_PI) //!< 180/pi
15
+#define OR_PI           ((float)M_PI) //!< pi
16
+#define OR_2_PI         (OR_PI * 2.0f) //!< pi*2
17
+#define OR_PI_OVER_4    (OR_PI / 4.0f) //!< pi/4
18
+#define OR_PI_OVER_180  (OR_PI / 180.0f) //!< pi/180
19
+#define OR_180_OVER_PI  (180.0f / OR_PI) //!< 180/pi
19
 
20
 
20
-#define HEL_RAD_TO_DEG(x) ((x) * HEL_180_OVER_PI) //!< Convert radians to degrees
21
-#define HEL_DEG_TO_RAD(x) ((x) * HEL_PI_OVER_180) //!< Convert degrees to radians
21
+#define OR_RAD_TO_DEG(x) ((x) * OR_180_OVER_PI) //!< Convert radians to degrees
22
+#define OR_DEG_TO_RAD(x) ((x) * OR_PI_OVER_180) //!< Convert degrees to radians
22
 
23
 
23
 typedef float vec_t;        //!< 1D Vector, aka float
24
 typedef float vec_t;        //!< 1D Vector, aka float
24
 typedef float vec2_t[2];    //!< 2D Vector
25
 typedef float vec2_t[2];    //!< 2D Vector
42
  * \param polygon polygon vertex array (0 to 2 are used)
43
  * \param polygon polygon vertex array (0 to 2 are used)
43
  * \returns 0 if there is no intersection
44
  * \returns 0 if there is no intersection
44
  */
45
  */
45
-int helIntersectionLineAndPolygon(vec3_t intersect, vec3_t p1, vec3_t p2, vec3_t *polygon);
46
+int intersectionLinePolygon(vec3_t intersect, vec3_t p1, vec3_t p2, vec3_t *polygon);
46
 
47
 
47
 /*!
48
 /*!
48
  * \brief Calculate the length of a line segment / the distance between two points
49
  * \brief Calculate the length of a line segment / the distance between two points
50
  * \param b Second point
51
  * \param b Second point
51
  * \returns distance/length
52
  * \returns distance/length
52
  */
53
  */
53
-vec_t helDist3v(vec3_t a, vec3_t b);
54
+vec_t distance(vec3_t a, vec3_t b);
54
 
55
 
55
 /*!
56
 /*!
56
  * \brief Calculates the midpoint between two points / of a line segment
57
  * \brief Calculates the midpoint between two points / of a line segment
58
  * \param b Second point
59
  * \param b Second point
59
  * \param mid Midpoint will be stored here
60
  * \param mid Midpoint will be stored here
60
  */
61
  */
61
-void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid);
62
+void midpoint(vec3_t a, vec3_t b, vec3_t mid);
62
 
63
 
63
 /*!
64
 /*!
64
  * \brief Calculates a pseudo-random number
65
  * \brief Calculates a pseudo-random number
66
  * \param to Upper bound
67
  * \param to Upper bound
67
  * \returns random number
68
  * \returns random number
68
  */
69
  */
69
-vec_t helRandomNum(vec_t from, vec_t to);
70
+vec_t randomNum(vec_t from, vec_t to);
70
 
71
 
71
 #endif
72
 #endif
72
 
73
 

+ 3
- 1
src/OpenRaider.cpp 查看文件

6
  */
6
  */
7
 
7
 
8
 #include <cstdio>
8
 #include <cstdio>
9
+#include <assert.h>
9
 
10
 
10
 #include "utils/strings.h"
11
 #include "utils/strings.h"
11
 #include "OpenRaider.h"
12
 #include "OpenRaider.h"
17
 }
18
 }
18
 
19
 
19
 int OpenRaider::loadConfig(const char *config) {
20
 int OpenRaider::loadConfig(const char *config) {
20
-    char *configFile = fullPath(config, 0);
21
+    assert(config != NULL);
21
 
22
 
23
+    char *configFile = fullPath(config, 0);
22
     printf("Trying to load \"%s\"...\n", configFile);
24
     printf("Trying to load \"%s\"...\n", configFile);
23
 
25
 
24
     return -1;
26
     return -1;

+ 12
- 56
src/math/math.cpp 查看文件

4
  * \brief Vector and Matrix math
4
  * \brief Vector and Matrix math
5
  *
5
  *
6
  * \author Mongoose
6
  * \author Mongoose
7
+ * \author xythobuz
7
  */
8
  */
8
 
9
 
9
 #include <stdlib.h>
10
 #include <stdlib.h>
22
     return false;
23
     return false;
23
 }
24
 }
24
 
25
 
25
-
26
-inline vec_t square(vec_t a)
27
-{
28
-    return a * a;
29
-}
30
-
31
-
32
-int helIntersectionLineAndPolygon(vec3_t intersect,
33
-        vec3_t p1, vec3_t p2,
34
-        vec3_t *polygon)
35
-{
26
+int intersectionLinePolygon(vec3_t intersect,
27
+        vec3_t p1, vec3_t p2, vec3_t *polygon) {
36
     assert(polygon != NULL);
28
     assert(polygon != NULL);
37
 
29
 
38
-    //  vec3_t normal, a, b;
30
+    // vec3_t normal, a, b;
39
     Vector3d a, b, normal, pA, pB;
31
     Vector3d a, b, normal, pA, pB;
40
     vec_t d, denominator, mu;
32
     vec_t d, denominator, mu;
41
 
33
 
50
     normal.normalize();
42
     normal.normalize();
51
 
43
 
52
     // find D
44
     // find D
53
-    //d = (normal[0] * polygon[0][0] -
54
-    //    normal[1] * polygon[0][1] -
55
-    //    normal[2] * polygon[0][2]);
56
     d = (normal.mVec[0] * polygon[0][0] -
45
     d = (normal.mVec[0] * polygon[0][0] -
57
-            normal.mVec[1] * polygon[0][1] -
58
-            normal.mVec[2] * polygon[0][2]);
46
+         normal.mVec[1] * polygon[0][1] -
47
+         normal.mVec[2] * polygon[0][2]);
59
 
48
 
60
     // line segment parallel to plane?
49
     // line segment parallel to plane?
61
     a = pB - pA;
50
     a = pB - pA;
62
 
51
 
63
-    //denominator = (normal[0] * a[0] +
64
-    //                  normal[1] * a[1] +
65
-    //                  normal[2] * a[2]);
66
     denominator = Vector3d::dot(normal, a);
52
     denominator = Vector3d::dot(normal, a);
67
 
53
 
68
     if (denominator > 0.0)
54
     if (denominator > 0.0)
69
         return 0;
55
         return 0;
70
 
56
 
71
     // Line segment contains intercept point?
57
     // Line segment contains intercept point?
72
-    //mu = - ((d + normal[0] * p1[0] + normal[1] * p1[1] + normal[2] * p1[2]) /
73
-    //        denominator);
74
     mu = -((d + Vector3d::dot(normal, pA)) / denominator);
58
     mu = -((d + Vector3d::dot(normal, pA)) / denominator);
75
 
59
 
76
     if (mu < 0.0 || mu > 1.0)
60
     if (mu < 0.0 || mu > 1.0)
77
         return 0;
61
         return 0;
78
 
62
 
79
-    //intersect[0] = p1[0] + mu * a[0];
80
-    //intersect[1] = p1[1] + mu * a[1];
81
-    //intersect[2] = p1[2] + mu * a[2];
82
     b = pA + (a * mu);
63
     b = pA + (a * mu);
83
     intersect[0] = b.mVec[0];
64
     intersect[0] = b.mVec[0];
84
     intersect[1] = b.mVec[1];
65
     intersect[1] = b.mVec[1];
85
     intersect[2] = b.mVec[2];
66
     intersect[2] = b.mVec[2];
86
 
67
 
87
-
88
     // See if the intercept is bound by polygon by winding number
68
     // See if the intercept is bound by polygon by winding number
89
     // assume convex polygons here for sure
69
     // assume convex polygons here for sure
90
     double theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
70
     double theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
95
     return 1;
75
     return 1;
96
 }
76
 }
97
 
77
 
98
-
99
-vec_t helDist3v(vec3_t a, vec3_t b)
100
-{
101
-    return (sqrtf( ((b[0] - a[0]) * (b[0] - a[0])) +
102
-                ((b[1] - a[1]) * (b[1] - a[1])) +
103
-                ((b[2] - a[2]) * (b[2] - a[2]))));
78
+vec_t distance(vec3_t a, vec3_t b) {
79
+    return sqrtf(((b[0] - a[0]) * (b[0] - a[0])) +
80
+                 ((b[1] - a[1]) * (b[1] - a[1])) +
81
+                 ((b[2] - a[2]) * (b[2] - a[2])));
104
 }
82
 }
105
 
83
 
106
-
107
-void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid)
108
-{
84
+void midpoint(vec3_t a, vec3_t b, vec3_t mid) {
109
     mid[0] = (a[0] + b[0]) / 2.0f;
85
     mid[0] = (a[0] + b[0]) / 2.0f;
110
     mid[1] = (a[1] + b[1]) / 2.0f;
86
     mid[1] = (a[1] + b[1]) / 2.0f;
111
     mid[2] = (a[2] + b[2]) / 2.0f;
87
     mid[2] = (a[2] + b[2]) / 2.0f;
112
 }
88
 }
113
 
89
 
114
-
115
-vec_t helNorm4v(vec4_t v)
116
-{
117
-    return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]));
118
-}
119
-
120
-
121
-vec_t helNorm3v(vec3_t v)
122
-{
123
-    return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]));
124
-}
125
-
126
-
127
-vec_t helNorm2v(vec2_t v)
128
-{
129
-    return (sqrtf(v[0]*v[0] + v[1]*v[1]));
130
-}
131
-
132
-
133
-vec_t helRandomNum(vec_t from, vec_t to)
134
-{
90
+vec_t randomNum(vec_t from, vec_t to) {
135
     return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
91
     return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
136
 }
92
 }
137
 
93
 

正在加载...
取消
保存