Explorar el Código

Documented MatMath

Added notice of changes to greatest.h
Removed unnecessary install_name_tool call in makefile
Thomas Buck hace 11 años
padre
commit
639d95badf
Se han modificado 5 ficheros con 89 adiciones y 110 borrados
  1. 1
    0
      ChangeLog
  2. 0
    3
      Makefile
  3. 60
    88
      include/MatMath.h
  4. 24
    18
      src/MatMath.cpp
  5. 4
    1
      test/greatest.h

+ 1
- 0
ChangeLog Ver fichero

@@ -6,6 +6,7 @@
6 6
  OpenRaider (0.1.2) xythobuz <xythobuz@xythobuz.de>
7 7
 
8 8
 	[ 20140111 ]
9
+	* Documented MatMath, fixed bug in helRandomNum()
9 10
 	* Only one way of conversion between Deg and Rad in MatMath
10 11
 	* Use the same style of include guards in all headers
11 12
 	* Added memory test to SkeletalModel. Adding to OpenGLMesh causes

+ 0
- 3
Makefile Ver fichero

@@ -365,9 +365,6 @@ Sound.test:
365 365
 	$(CC) $(TEST_FLAGS) \
366 366
 		-DUSING_OPENAL $(AUDIO_LIBS) $(AUDIO_DEFS) \
367 367
 		src/Sound.cpp test/Sound.cpp -o $(BUILD_TEST_DIR)/Sound.test
368
-ifeq ($(UNAME),Darwin)
369
-	install_name_tool -change libalut.0.1.0.dylib /opt/local/lib/libalut.0.1.0.dylib $(BUILD_TEST_DIR)/Sound.test
370
-endif
371 368
 
372 369
 #################################################################
373 370
 

+ 60
- 88
include/MatMath.h Ver fichero

@@ -1,109 +1,81 @@
1
-/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
2
-/*================================================================
1
+/*!
3 2
  *
4
- * Project : Freyja
5
- * Author  : Terry 'Mongoose' Hendrix II
6
- * Website : http://www.westga.edu/~stu7440/
7
- * Email   : stu7440@westga.edu
8
- * Object  :
9
- * License : No use w/o permission (C) 2002 Mongoose
10
- * Comments:
3
+ * \file include/MatMath.h
4
+ * \brief Vector and Matrix math
11 5
  *
12
- *
13
- *           This file was generated using Mongoose's C++
14
- *           template generator script.  <stu7440@westga.edu>
15
- *
16
- *-- History ------------------------------------------------
17
- *
18
- * 2002.05.11:
19
- * Mongoose - Created
20
- ================================================================*/
21
-
6
+ * \author Mongoose
7
+ */
22 8
 
23 9
 #ifndef _MATMATH_H
24 10
 #define _MATMATH_H
25 11
 
26
-#define HEL_PI           3.14159265358979323846  /* pi */
27
-#define HEL_PI_OVER_2    1.57079632679489661923  /* pi/2 */
28
-#define HEL_2_PI         6.28318530717958647692  /* pi*2 */
29
-#define HEL_PI_OVER_4    0.78539816339744830962  /* pi/4 */
30
-#define HEL_PI_OVER_180  0.017453292519943295    /* pi/180 */
31
-#define HEL_180_OVER_PI  57.295779513082323      /* 180/pi */
32
-
33
-#define HEL_RAD_TO_DEG(x) ((x) * HEL_180_OVER_PI)
34
-#define HEL_DEG_TO_RAD(x) ((x) * HEL_PI_OVER_180)
12
+#define HEL_PI           3.14159265358979323846 //!< pi
13
+#define HEL_PI_OVER_2    1.57079632679489661923 //!< pi/2
14
+#define HEL_2_PI         6.28318530717958647692 //!< pi*2
15
+#define HEL_PI_OVER_4    0.78539816339744830962 //!< pi/4
16
+#define HEL_PI_OVER_180  0.017453292519943295   //!< pi/180
17
+#define HEL_180_OVER_PI  57.295779513082323     //!< 180/pi
35 18
 
36
-typedef float vec_t;
37
-typedef float vec2_t[2];
38
-typedef float vec3_t[3];
39
-typedef float vec4_t[4];
40
-typedef vec_t matrix_t[16];  /* Used as _Column_major_ in every class now! */
19
+#define HEL_RAD_TO_DEG(x) ((x) * HEL_180_OVER_PI) //!< Convert radians to degrees
20
+#define HEL_DEG_TO_RAD(x) ((x) * HEL_PI_OVER_180) //!< Convert degrees to radians
41 21
 
22
+typedef float vec_t;        //!< 1D Vector, aka float
23
+typedef float vec2_t[2];    //!< 2D Vector
24
+typedef float vec3_t[3];    //!< 3D Vector
25
+typedef float vec4_t[4];    //!< 4D Vector
26
+typedef vec_t matrix_t[16]; //!< Used as _Column_major_ in every class now!
42 27
 
43
-int helIntersectionLineAndPolygon(vec3_t intersect,
44
-											 vec3_t p1, vec3_t p2,
45
-											 unsigned int vertexCount, vec3_t *ploygon);
46
-/*------------------------------------------------------
47
- * Pre  : Given P1 and P2 of line segment and
48
- *        Vertex count and ploygon with count vertices
49
- *
50
- *        Only supports triangles and coplanar quads
51
- *
52
- * Post : Returns intersect point or 0 if none
53
- *
54
- *-- History ------------------------------------------
55
- *
56
- * 2003.05.30:
57
- * Mongoose - Created
58
- ------------------------------------------------------*/
28
+/*!
29
+ * \brief Calculate Intersection of a line and a polygon
30
+ * \param intersect Where the intersection is stored, if it exists
31
+ * \param p1 First point of line segment
32
+ * \param p2 Second point of line segment
33
+ * \param vertexCount number of vertices in polygon
34
+ * \param polygon polygon vertex array
35
+ * \returns 0 if there is no intersection
36
+ */
37
+int helIntersectionLineAndPolygon(vec3_t intersect, vec3_t p1, vec3_t p2, unsigned int vertexCount, vec3_t *polygon);
59 38
 
39
+/*!
40
+ * \brief Calculate the distance from a sphere to a plane
41
+ * \param center Center of sphere
42
+ * \param radius Radius of sphere
43
+ * \param plane Plane
44
+ * \returns distance
45
+ */
60 46
 vec_t helDistToSphereFromPlane3v(vec3_t center,	vec_t radius, vec4_t plane);
61
-/*------------------------------------------------------
62
- * Pre  : Given center and radius of sphere and a plane
63
- * Post : Returns distance from sphere to plane
64
- *
65
- *-- History ------------------------------------------
66
- *
67
- * 1999.06.14:
68
- * Mongoose - Created, from mtk3d
69
- ------------------------------------------------------*/
70 47
 
48
+/*!
49
+ * \brief Calculate the distance from a box to a plane
50
+ * \param min Minimum Point of a bounding box
51
+ * \param max Maximum Point of a bounding box
52
+ * \param plane Plane
53
+ * \returns distance
54
+ */
71 55
 vec_t helDistToBboxFromPlane3v(vec3_t min, vec3_t max, vec4_t plane);
72
-/*------------------------------------------------------
73
- * Pre  : Given min and max points of a bounding box
74
- *        and a plane
75
- *
76
- * Post : Returns distance from box to plane
77
- *
78
- *-- History ------------------------------------------
79
- *
80
- * 1999.06.14:
81
- * Mongoose - Created, from mtk3d
82
- ------------------------------------------------------*/
83 56
 
57
+/*!
58
+ * \brief Calculate the length of a line segment / the distance between two points
59
+ * \param a First point
60
+ * \param b Second point
61
+ * \returns distance/length
62
+ */
84 63
 vec_t helDist3v(vec3_t a, vec3_t b);
85
-/*------------------------------------------------------
86
- * Pre  : Given point A and B
87
- * Post : Returns length of line segment
88
- *
89
- *-- History ------------------------------------------
90
- *
91
- * 1999.06.14:
92
- * Mongoose - Created, from mtk3d
93
- ------------------------------------------------------*/
94 64
 
65
+/*!
66
+ * \brief Calculates the midpoint between two points / of a line segment
67
+ * \param a First point
68
+ * \param b Second point
69
+ * \param mid Midpoint will be stored here
70
+ */
95 71
 void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid);
96
-/*------------------------------------------------------
97
- * Pre  : Given point A and B
98
- * Post : Returns midpoint of line segment
99
- *
100
- *-- History ------------------------------------------
101
- *
102
- * 1999.06.14:
103
- * Mongoose - Created, from mtk3d
104
- ------------------------------------------------------*/
105
-
106 72
 
73
+/*!
74
+ * \brief Calculates a pseudo-random number
75
+ * \param from Lower bound of resulting number
76
+ * \param to Upper bound
77
+ * \returns random number
78
+ */
107 79
 vec_t helRandomNum(vec_t from, vec_t to);
108 80
 
109 81
 #endif

+ 24
- 18
src/MatMath.cpp Ver fichero

@@ -1,4 +1,10 @@
1
-/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
1
+/*!
2
+ *
3
+ * \file src/MatMath.cpp
4
+ * \brief Vector and Matrix math
5
+ *
6
+ * \author Mongoose
7
+ */
2 8
 
3 9
 #include <stdlib.h>
4 10
 #include <math.h>
@@ -122,7 +128,7 @@ int helIntersectionOfAbstractSphereAndLine(vec3_t center, vec_t radius,
122 128
 
123 129
 int helIntersectionLineAndPolygon(vec3_t intersect,
124 130
 											 vec3_t p1, vec3_t p2,
125
-											 unsigned int vertexCount, vec3_t *ploygon)
131
+											 unsigned int vertexCount, vec3_t *polygon)
126 132
 {
127 133
 	//	vec3_t normal, a, b;
128 134
 	Vector3d a, b, normal, pA, pB;
@@ -134,22 +140,22 @@ int helIntersectionLineAndPolygon(vec3_t intersect,
134 140
 	pB = Vector3d(p2);
135 141
 
136 142
 	// Find normal
137
-	//mtkVectorSubtract(ploygon[1], ploygon[0], a);
138
-	a = Vector3d(ploygon[1]) - Vector3d(ploygon[0]);
139
-	//mtkVectorSubtract(ploygon[2], ploygon[0], b);
140
-	b = Vector3d(ploygon[2]) - Vector3d(ploygon[0]);
143
+	//mtkVectorSubtract(polygon[1], polygon[0], a);
144
+	a = Vector3d(polygon[1]) - Vector3d(polygon[0]);
145
+	//mtkVectorSubtract(polygon[2], polygon[0], b);
146
+	b = Vector3d(polygon[2]) - Vector3d(polygon[0]);
141 147
 	normal = Vector3d::cross(a, b);
142 148
 	//mtkVectorCrossProduct(a, b, normal);
143 149
 	normal.normalize();
144 150
 	//mtkVectorNormalize(normal, normal);
145 151
 
146 152
 	// find D
147
-	//d = (normal[0] * ploygon[0][0] -
148
-	//	  normal[1] * ploygon[0][1] -
149
-	//	  normal[2] * ploygon[0][2]);
150
-	d = (normal.mVec[0] * ploygon[0][0] -
151
-		  normal.mVec[1] * ploygon[0][1] -
152
-		  normal.mVec[2] * ploygon[0][2]);
153
+	//d = (normal[0] * polygon[0][0] -
154
+	//	  normal[1] * polygon[0][1] -
155
+	//	  normal[2] * polygon[0][2]);
156
+	d = (normal.mVec[0] * polygon[0][0] -
157
+		  normal.mVec[1] * polygon[0][1] -
158
+		  normal.mVec[2] * polygon[0][2]);
153 159
 
154 160
 	// line segment parallel to plane?
155 161
 	//mtkVectorSubtract(p2, p1, a); // cache p2 - p1 => a
@@ -182,11 +188,11 @@ int helIntersectionLineAndPolygon(vec3_t intersect,
182 188
 
183 189
 	// See if the intercept is bound by polygon by winding number
184 190
 #ifdef WINDING_NUMBERS_TRIANGLE
185
-	mtkVectorSubtract(ploygon[0], intersect, a);
191
+	mtkVectorSubtract(polygon[0], intersect, a);
186 192
 	mtkVectorNormalize(a, a);
187
-	mtkVectorSubtract(ploygon[1], intersect, b);
193
+	mtkVectorSubtract(polygon[1], intersect, b);
188 194
 	mtkVectorNormalize(b, b);
189
-	mtkVectorSubtract(ploygon[2], intersect, c);
195
+	mtkVectorSubtract(polygon[2], intersect, c);
190 196
 	mtkVectorNormalize(c, c);
191 197
 
192 198
 	t0 = mtkVectorDotProduct(a, b);
@@ -198,9 +204,9 @@ int helIntersectionLineAndPolygon(vec3_t intersect,
198 204
 	if (total - 360 < 0.0)
199 205
 		return 0;
200 206
 #else // assume convex polygons here for sure
201
-	//mtkVectorSubtract(intersect, ploygon[0], a);
207
+	//mtkVectorSubtract(intersect, polygon[0], a);
202 208
 	//theta = mtkVectorDotProduct(a, normal);
203
-	theta = Vector3d::dot(b - Vector3d(ploygon[0]), normal); // b = intersect
209
+	theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
204 210
 
205 211
 	if (theta >= 90.0) // Yeah I know
206 212
 		return 0;
@@ -285,6 +291,6 @@ vec_t helNorm2v(vec2_t v)
285 291
 
286 292
 vec_t helRandomNum(vec_t from, vec_t to)
287 293
 {
288
-	return from + (to*rand()/(RAND_MAX+1.0));
294
+	return from + ((to - from) * rand() / (RAND_MAX + 1.0));
289 295
 }
290 296
 

+ 4
- 1
test/greatest.h Ver fichero

@@ -1,6 +1,9 @@
1 1
 /*
2 2
  * Copyright (c) 2011 Scott Vokes <vokes.s@gmail.com>
3 3
  *
4
+ * Modified 2014 by Thomas Buck <xythobuz@xythobuz.de>
5
+ * --> C++ support
6
+ *
4 7
  * Permission to use, copy, modify, and/or distribute this software for any
5 8
  * purpose with or without fee is hereby granted, provided that the above
6 9
  * copyright notice and this permission notice appear in all copies.
@@ -18,7 +21,7 @@
18 21
 #define GREATEST_H
19 22
 
20 23
 #ifdef __cplusplus
21
-#define __STDC_VERSION__ 19901L
24
+#define __STDC_VERSION__ 19901L // C++ compilers support var args
22 25
 extern "C"
23 26
 {
24 27
 #endif

Loading…
Cancelar
Guardar