rtsMath.h 5.01 KB
/*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