Blame view

legacy/rtsMath.h 5.01 KB
f1402849   dmayerich   renewed commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  /*These files contain basic structures used frequently in the program and in computer

  graphics in general.

  vector3D -	A 3D vector class that contains 3 double values and overloaded methods for

  			most vector operations such as euclidean and cross products.

  point3D	-	A 3D point structure that contains 3 values and overloads functions for

  			point-vector arithmetic.

  These two classes are designed to work together and different methods may require operands

  of both types.

  

  RGBA and RGB -	Color storage and associated methods, although they are not fully implemented yet

  

  -David Mayerich, 8/20/05*/

  

  #ifndef _RTS_MATH_H

  #define _RTS_MATH_H

  

  #define RTS_PI			3.14159

  #define TORADIANS(a)	(a)*RTS_PI/180.0

  #define TODEGREES(a)	(a)*180.0/RTS_PI

  

  #include "rtsConstants.h"

  #include <math.h>

  #include <iostream>

  using namespace std;

  

  class vector3D

  {

  public:

  	float x;

  	float y;

  	float z;

  	

  	vector3D();

  	vector3D(double newx, double newy, double newz);

  	vector3D operator+(vector3D param);

  	vector3D operator-(vector3D param);

  	double operator*(vector3D param);	//inner product

  	vector3D cross(vector3D param);

  	vector3D operator*(double param);

  	

  	int normalize();

  	double length();

  

  	void print();

  };

  

  

  class point3D

  {

  public:

  	float x;

  	float y;

  	float z;

  

  	point3D();

  	point3D(double newx, double newy, double newz);

  

  	//support for vector arithmetic

  	point3D operator+(vector3D param);

  	point3D operator-(vector3D param);

  

  	vector3D operator-(point3D param);

  

  	void print();

  };

  

  class matrix4x4

  {

  

  	/*stored as:

  	| 0  4  8  12 |

      |             |

      | 1  5  9  13 |

      |             |

      | 2  6  10 14 |

      |             |

      | 3  7  11 15 |

  	*/

  public:

  	float matrix[16];

  	matrix4x4();

  	matrix4x4(float m00, float m01, float m02, float m03,

  			  float m10, float m11, float m12, float m13,

  			  float m20, float m21, float m22, float m23,

  			  float m30, float m31, float m32, float m33);

  	matrix4x4(vector3D basis_x, vector3D basis_y, vector3D basis_z);

  	vector3D basis_x();

  	vector3D basis_y();

  	vector3D basis_z();

  	point3D operator*(point3D param);

  	vector3D operator*(vector3D param);

  	double operator()(unsigned int row, unsigned int col);

  	void gl_set_matrix(float* gl_matrix);

  	void gl_get_matrix(float* gl_matrix);

  	matrix4x4 submatrix(unsigned int start_col, unsigned int start_row, unsigned int end_col, unsigned int end_row);

  	matrix4x4 transpose();

  	void print();

  };

  

  

  class RGB

  {

  public:

  	float r;

  	float g;

  	float b;

  };

  

  class RGBA

  {

  public:

  	float r;

  	float g;

  	float b;

  	float a;

  

  	RGBA();

  	RGBA(double red, double green, double blue, double ambient);

  };

  

  class quaternion

  {

  public:

  	float w;

  	float x;

  	float y;

  	float z;

  

  	int normalize();

  	quaternion operator*(quaternion param);

  	double* toMatrix();

  

  	quaternion();

  	quaternion(double w, double x, double y, double z);

  

  };

  

  class plane3D

  {

  public:

  	point3D point;

  	vector3D normal;

  

  	plane3D();

  	plane3D(point3D point_on_plane, vector3D plane_normal);

  };

  

  class ray3D

  {

  public:

  	point3D point;

  	vector3D direction;

  

  	ray3D();

  	ray3D(point3D start_point, vector3D direction);

  	ray3D(point3D first_point, point3D second_point);

  

  	int intersect(plane3D plane, point3D& intersection_point);

  };

  

  class line3D

  {

  private:

  	point3D m_p0;

  	point3D m_p1;

  

  public:

  	line3D();

  	line3D(point3D p0, point3D p1);

  

  	point3D get_point(double pos);	//returns a point at pos = [0.0, 1.0]

  };

  

  

  //at this point, this template class is only really working with floating point numbers (and doubles)

  #define RTS_UNSIGNED_BYTE		1

  #define RTS_FLOAT				2

  #define RTS_DOUBLE				3

  

  typedef unsigned int rtsBitType;	

  

  class function2D

  {

  public:

  

  	function2D(float domain_x0, float domain_x1, float domain_y0, float domain_y1, int x_resolution, int y_resolution);

  	function2D();

  	~function2D();

  	function2D(const function2D &copy);		//copy constructor

  	function2D& operator=(const function2D& f);

  	

  	void CreateGaussian(float mean_x, float mean_y, float std_dev);

  	void CreateConstant(float value);

  	void CreateCircleMask(float center_x, float center_y, float radius);

  	void Scale(float min, float max);		//scales the function so that the range exists only in the specified bounds

  	void Clip(float min, float max);		//clips the function so that the range exists only in the specified bounds

  	void Abs();								//sets every sample point to the absolute value

  	float Integral();						//returns the sum of the function

  	float Average();

  	float operator ()(float x, float y);

  

  	function2D operator*(function2D param);

  	function2D operator*(float param);

  	function2D operator+(function2D param);

  	function2D operator+(float param);

  	function2D operator-(function2D param);

  	

  	float* getBits();

  	void setBits(float* bits);

  	void setBits(unsigned char* bits);

  	void setBits(double* bits);

  	void setBits(int* bits);

  

  

  private:

  	float m_x0;

  	float m_x1;

  	float m_y0;

  	float m_y1;

  	int m_x_resolution;

  	int m_y_resolution;

  	float* m_values;

  };

  

  

  

  

  

  

  

  

  

  #endif