view include/Math.h @ 4:f4e384b966b9

About half done with Vectors
author Eris Caffee <discordia@eldalin.com>
date Tue, 30 Aug 2011 11:08:12 -0500
parents
children f6e6f3c8f7eb
line source
1 #ifndef MATH_H_
2 #define MATH_H_
4 #include <cmath>
5 #include <string>
7 namespace arda
8 {
10 ////////////////////////////////////////////////////////////////////////////////
11 // General notes:
12 //
13 // As much as possible is defined inline for performance.
14 // Using typedef'ed arrays for vectors and matrices necessitates a lot of
15 // redundant function names: scale2, scale3, scale4, for example.
16 // This is, however, perfectly in line with the implementation of OpenGL and
17 // allows the library to maintain optimal memory usage and performance. (I hope!)
18 // In any event, I think I could do far worse than to emulate the OpenGL
19 // working group - a group of people who have studied this for years and are
20 // experts in the field.
22 ////////////////////////////////////////////////////////////////////////////////
23 // Vectors
25 namespace Vector
26 {
28 typedef int Vector2i[2];
29 typedef float Vector2f[2];
30 typedef double Vector2d[2];
32 typedef int Vector3i[3];
33 typedef float Vector3f[3];
34 typedef double Vector3d[3];
36 typedef int Vector4i[4];
37 typedef float Vector4f[4];
38 typedef double Vector4d[4];
40 //////////////////////////////////////////////////////////////////////////
41 // vectostr*(Vector v)
42 // Returns a C string representation of the vector.
43 // This is one of the few non-inline Vector functions.
45 std::string & vectostr2(Vector2i v, std::string & s);
46 std::string & vectostr2(Vector2f v, std::string & s);
47 std::string & vectostr2(Vector2d v, std::string & s);
48 std::string & vectostr3(Vector3i v, std::string & s);
49 std::string & vectostr3(Vector3f v, std::string & s);
50 std::string & vectostr3(Vector3d v, std::string & s);
51 std::string & vectostr4(Vector4i v, std::string & s);
52 std::string & vectostr4(Vector4f v, std::string & s);
53 std::string & vectostr4(Vector4d v, std::string & s);
55 //////////////////////////////////////////////////////////////////////////
56 // scale*(Vector v, scalar s)
57 // Multiply a vector by a scalar.
58 // Returns v so that the result may be passed to other functions.
60 inline int * scale2(Vector2i v, const int s)
61 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
62 inline int * scale2(Vector2i v, const float s)
63 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
64 inline int * scale2(Vector2i v, const double s)
65 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
67 inline float * scale2(Vector2f v, const int s)
68 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
69 inline float * scale2(Vector2f v, const float s)
70 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
71 inline float * scale2(Vector2f v, const double s)
72 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
74 inline double * scale2(Vector2d v, const int s)
75 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
76 inline double * scale2(Vector2d v, const float s)
77 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
78 inline double * scale2(Vector2d v, const double s)
79 { v[0] = v[0] * s ; v[1] = v[1] * s; return v; }
82 inline int * scale3(Vector3i v, const int s)
83 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
84 inline int * scale3(Vector3i v, const float s)
85 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
86 inline int * scale3(Vector3i v, const double s)
87 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
89 inline float * scale3(Vector3f v, const int s)
90 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
91 inline float * scale3(Vector3f v, const float s)
92 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
93 inline float * scale3(Vector3f v, const double s)
94 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
96 inline double * scale3(Vector3d v, const int s)
97 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
98 inline double * scale3(Vector3d v, const float s)
99 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
100 inline double * scale3(Vector3d v, const double s)
101 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; }
104 inline int * scale4(Vector4i v, const int s)
105 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
106 inline int * scale4(Vector4i v, const float s)
107 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
108 inline int * scale4(Vector4i v, const double s)
109 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
111 inline float * scale4(Vector4f v, const int s)
112 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
113 inline float * scale4(Vector4f v, const float s)
114 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
115 inline float * scale4(Vector4f v, const double s)
116 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
118 inline double * scale4(Vector4d v, const int s)
119 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
120 inline double * scale4(Vector4d v, const float s)
121 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
122 inline double * scale4(Vector4d v, const double s)
123 { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; }
126 //////////////////////////////////////////////////////////////////////////
127 // dot*(Vector v1, Vector v2)
128 // The dot product of two vectors, which must be the same type
129 // Returns a long for int vectors, a double for floating point vectors.
131 inline long dot2(Vector2i v1, Vector2i v2)
132 { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1]; }
133 inline double dot2(Vector2f v1, Vector2f v2)
134 { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1]; }
135 inline double dot2(Vector2d v1, Vector2d v2)
136 { return v1[0]*v2[0] + v1[1]*v2[1]; }
138 inline long dot3(Vector3i v1, Vector3i v2)
139 { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1] + (long) v1[2]*v2[2]; }
140 inline double dot3(Vector3f v1, Vector3f v2)
141 { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1] + (double) v1[2]*v2[2]; }
142 inline double dot3(Vector3d v1, Vector3d v2)
143 { return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; }
145 inline long dot4(Vector4i v1, Vector4i v2)
146 { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1] + (long) v1[2]*v2[2] + (long) v1[3]*v2[3]; }
147 inline double dot4(Vector4f v1, Vector4f v2)
148 { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1] + (double) v1[2]*v2[2] + (double) v1[3]*v2[3]; }
149 inline double dot4(Vector4d v1, Vector4d v2)
150 { return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]; }
153 //////////////////////////////////////////////////////////////////////////
154 // length*(Vector v)
155 // Get the length of a vector.
156 // Returns a float for float for float vectors, and a double for
157 // int and double vectors.
159 inline double length2(Vector2i v)
160 { return sqrt((dot2(v, v))); }
161 inline float length2(Vector2f v)
162 { return sqrtf((dot2(v, v))); }
163 inline double length2(Vector2d v)
164 { return sqrt((dot2(v, v))); }
166 inline double length3(Vector3i v)
167 { return sqrt((dot3(v, v))); }
168 inline float length3(Vector3f v)
169 { return sqrtf((dot3(v, v))); }
170 inline double length3(Vector3d v)
171 { return sqrt((dot3(v, v))); }
173 inline double length4(Vector4i v)
174 { return sqrt((dot4(v, v))); }
175 inline float length4(Vector4f v)
176 { return sqrtf((dot4(v, v))); }
177 inline double length4(Vector4d v)
178 { return sqrt((dot4(v, v))); }
181 //////////////////////////////////////////////////////////////////////////
182 // add*(Vector v1, Vector v2, Vector vres)
183 // Add two vectors together.
184 // Returns vres so that the result may be used in further
185 // calculations.
187 inline int * add2(Vector2i v1, Vector2i v2, Vector2i vres)
188 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; }
189 inline float * add2(Vector2f v1, Vector2f v2, Vector2f vres)
190 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; }
191 inline double * add2(Vector2d v1, Vector2d v2, Vector2d vres)
192 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; }
194 inline int * add3(Vector3i v1, Vector3i v2, Vector3i vres)
195 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; }
196 inline float * add3(Vector3f v1, Vector3f v2, Vector3f vres)
197 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; }
198 inline double * add3(Vector3d v1, Vector3d v2, Vector3d vres)
199 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; }
201 inline int * add4(Vector4i v1, Vector4i v2, Vector4i vres)
202 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; vres[3] = v1[3] + v2[3]; return vres;}
203 inline float * add4(Vector4f v1, Vector4f v2, Vector4f vres)
204 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; vres[3] = v1[3] + v2[3]; return vres; }
205 inline double * add4(Vector4d v1, Vector4d v2, Vector4d vres)
206 { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; vres[3] = v1[3] + v2[3]; return vres; }
208 //////////////////////////////////////////////////////////////////////////
209 // normalize*(Vector v)
210 // Normalizes the vecor. Only defined for floating point vectors.
211 // Returns the vector.
213 inline float * normalize2(Vector2f v)
214 { float l = length2(v); v[0] /= l; v[1] /= l; return v; }
215 inline double * normalize2(Vector2d v)
216 { double l = length2(v); v[0] /= l; v[1] /= l; return v; }
218 inline float * normalize3(Vector3f v)
219 { float l = length3(v); v[0] /= l; v[1] /= l; v[2] /= l; return v; }
220 inline double * normalize3(Vector3d v)
221 { double l = length3(v); v[0] /= l; v[1] /= l; v[2] /= l; return v; }
223 inline float * normalize4(Vector4f v)
224 { float l = length4(v); v[0] /= l; v[1] /= l; v[2] /= l; v[3] /= l; return v; }
225 inline double * normalize4(Vector4d v)
226 { double l = length4(v); v[0] /= l; v[1] /= l; v[2] /= l; v[3] /= l; return v; }
227 }
229 } // namespace arda
231 #endif