|
|
#pragma once
#include <cmath>
#include <stdio.h>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <stdarg.h>
#define PRECISION 0.000001
#define SQRT(X) sqrt((X)) //��ƽ����
#define SQR(X) ((X)*(X)) //��ƽ��
#define ABS(X) abs((X))
#define EQ(X,Y,EPS) (ABS((X)-(Y))<EPS)//ͨ������ȥ�жϣ��������Ƿ�����
#define EQ_ZERO(X,EPS) (ABS((X))<EPS)
#define ARR_ZERO(A, N) memset((A), 0, sizeof(A[0])*(N))
#define MIN(x,y) (x)<(y)?(x):(y)
#define SWAP(type, x, y) {type tmp=(x); (x)=(y); (y) = tmp;}
#define M_PI 3.141592
using namespace std;
class GVector3; class GVector; class GMatrix; class GPlane; class GQuater; typedef GVector3 GPoint3;
class GVector3 { private: float v[3]; public: GVector3(float x = 0.0f, float y = 0.0f, float z = 0.0f); GVector3(const GVector3& copy); GVector3& operator =(const GVector3& rhs);
//�����Ӽ�
GVector3 operator +(const GVector3& rhs) const; // u + v
GVector3 operator -(const GVector3& rhs) const; // u - v
//�����ͳ����˳�
friend GVector3 operator *(const GVector3& lhs, const float& k); // u * k
friend GVector3 operator *(const float& k, const GVector3& rhs); // k * u
friend GVector3 operator /(const GVector3& lhs, const float& k); // u / k
//��������ģ�ͷ���
friend float norm(const GVector3& v); //��������С��ģ������
GVector3& normalize(); //���������� �����
//���������ڻ�
float operator *(const GVector3& rhs) const;
//����ͶӰ
friend GVector3 proj(const GVector3& p, const GVector3& q); friend GVector3 perp(const GVector3& p, const GVector3& q);
//�������ڻ� ����
GVector3 operator^(const GVector3& rhs) const;
//����������1
GVector3& Set(const float& x, const float& y, const float& z); friend float distance(const GVector3& v, const GVector3 u);
//����������2
GVector3& operator +=(const GVector3& rhs); GVector3& operator -=(const GVector3& rhs); friend ostream& operator<<(ostream& os, const GVector3& v);
//����������3
GVector3& operator *=(const float& k); GVector3& operator /=(const float& k); GVector3& operator ^=(const GVector3& rhs);
//����������4
bool operator ==(const GVector3 rhs) const; bool operator !=(const GVector3 rhs) const;
//����������5
GVector3 operator+() const; GVector3 operator-() const; float& operator [](const int& idx); //�±긳ֵ
const float operator [](const int& idx) const;//�±���ȡ����
};
class GVector { public: //GVector 1~2
GVector(int dim = 3); GVector(int dim, double x, ...); GVector(const GVector3& copy); GVector(const GVector& copy); ~GVector(); //GVector 3
GVector& Set(double x, ...); GVector& Set(float *p);
//GVector 4
GVector& operator =(const GVector& rhs); GVector& operator +=(const GVector& rhs); GVector& operator -=(const GVector& rhs); GVector& operator +=(const float& k); GVector& operator -=(const float& k);
GVector &operator *=(const float &k); GVector &operator /=(const float &k);
bool operator ==(const GVector& rhs) const; bool operator !=(const GVector& rhs) const;
GVector operator +() const; GVector operator -() const;
GVector operator +(const GVector& rhs) const; GVector operator -(const GVector& rhs) const; float operator *(const GVector& rhs) const; GVector operator /(const float& k) const;
float& operator [](const int& idx); const float& operator [](const int& idx) const;
GVector& Nornalize();
int GetDim() const;
friend GVector operator *(const float& k, const GVector& rhs); friend GVector operator *(const GVector& lhs, const float& k); friend GVector operator *(const GMatrix& m, const GVector& v); friend GMatrix operator *(const GVector& v, const GMatrix& m); friend float norm(const GVector& v); friend float distance(const GVector& v, const GVector& u); friend ostream& operator <<(ostream& os, const GVector& v);
friend class GMatrix; private: int n; float* v; };
class GMatrix { public: GMatrix(int row = 4, int col = 4, float *elem = NULL); GMatrix(const GMatrix& copy); ~GMatrix();
GMatrix& operator =(const GMatrix& rhs);
GMatrix& operator +=(const GMatrix& rhs); GMatrix& operator -=(const GMatrix& rhs); GMatrix& operator *=(const float& k); GMatrix& operator *=(const GMatrix& rhs); GMatrix& operator /=(const float& k);
bool operator ==(const GMatrix& rhs) const; bool operator !=(const GMatrix& rhs) const;
float* operator [](const int idx); const float* operator [](const int idx) const;
GMatrix operator +() const; GMatrix operator -() const;
GMatrix operator +(const GMatrix& rhs) const; GMatrix operator -(const GMatrix& rhs) const; GMatrix operator *(const GMatrix& rhs) const; GMatrix operator /(const float& k) const;
GMatrix& SetInverse(); GMatrix& SetTranspose(); GMatrix& SetIdentity(); GMatrix& SetZeros();
GMatrix& SetRowVec(const int idx, const GVector& v); GMatrix& SetColVec(const int idx, const GVector& v);
GVector GetRowVec(const int idx) const; GVector GetColVec(const int idx) const;
GMatrix& ExchangeRows(const int idx0, const int idx1); GMatrix& ExchangeCols(const int idx0, const int idx1);
int GetRowNum() const; int GetColNum() const;
bool IsSquare() const;
friend GMatrix operator *(const GMatrix& lhs, const float& k); friend GMatrix operator *(const float& k , const GMatrix& rhs);
friend GVector operator *(const GMatrix& m, const GVector& v); friend GMatrix operator *(const GVector& v, const GMatrix& m); friend ostream& operator <<(ostream& os, const GMatrix& m);
friend GMatrix RowEchelonForm(const GMatrix& m); friend GMatrix ReduceRowEchelonForm(const GMatrix& m);
friend float* from_arr(const GMatrix& m);
friend int Rank(const GMatrix& m);//��ȡ��������
friend int Nullity(const GMatrix& m);//��ȡ�������㻯��
friend GMatrix MijMatrix(const GMatrix& m, int r, int c); //��������ʽ����
friend float Mij(const GMatrix& m, int r, int c); //��������ʽ
friend float Cij(const GMatrix& m, int r, int c); //������������ʽ����������ʽֵ
friend float Det(const GMatrix& m); friend GMatrix Inverse(const GMatrix& m); friend GMatrix Transpose(const GMatrix& m); friend GMatrix Adjoint(const GMatrix& m);//����������
private: int r; int c; float *m; };
class GLine { private: // l(t) = p + t*v;
GPoint3 p;//�˵�
GVector3 v;//����
public: GLine(const GPoint3& _p = GPoint3(0.0f, 0.0f, 0.0f), const GVector3& _v = GVector3(0.0f, 0.0f, 0.0f)); GLine(const GLine& copy);
GLine& operator =(const GLine& rhs); GPoint3 operator ()(const float t) const;
GLine& SetPt(const GPoint3& _p); GLine& SetDir(const GVector3& _v);
GVector3 GetPt() const; GVector3 GetDir() const;
bool IsOnLine(const GPoint3& q); friend float dist(const GPoint3& q, const GLine& l);
friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi); friend bool intersect_line_triangle(GPoint3& p, const GLine& l, const GPoint3& p1, const GPoint3& p2, const GPoint3& p3); };
class GPlane { private: //ax+by+cz+d=0 a b c ��ʵ���Ƿ��ߣ�d��ƽ���ϵ�һ�����˷�����ȡ����
GVector3 n; float d; public: GPlane(const GVector3& _n = GVector3(0.0f, 0.0f, 0.0f), const GPoint3& _p = GVector3(0.0f, 0.0f, 0.0f)); GPlane(const GPoint3& p1, const GPoint3& p2, const GPoint3& p3); GPlane(const float& a, const float& b, const float&c, const float& d); GPlane(const GPlane& copy);
GPlane& operator =(const GPlane& rhs); GVector3 GetNormal() const;
friend float dist(const GPlane& pi, const GPoint3& p);
friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi);
friend bool intersect_line_triangle(GPoint3& p, const GLine& l, const GPoint3& p1, const GPoint3& p2, const GPoint3& p3); };
class GQuater { private: float w, x, y, z; public: GQuater(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f); GQuater(const GQuater& copy); GQuater(GVector3 axis, float theta, bool radian = false); GQuater& operator=(const GQuater& rhs);
GQuater& operator+=(const GQuater& rhs); GQuater& operator-=(const GQuater& rhs); GQuater& operator*=(const GQuater& rhs);
GQuater& operator*=(const float& s); GQuater& operator/=(const float& s); GQuater operator+() const; GQuater operator-() const;
GQuater operator+(const GQuater& rhs) const; GQuater operator-(const GQuater& rhs) const; GQuater operator*(const GQuater& rhs) const; GQuater operator*(const float& s) const; GQuater operator/(const float& s) const;
GQuater& Set(const float w, const float x, const float y, const float z); GQuater& Set(float *q, bool invOrder = false);
bool IsUnitQuater() const; bool IsIdentity() const;
friend GQuater operator*(const float &s, const GQuater &rhs); friend ostream& operator<<(ostream &os, const GQuater &q); friend float norm(const GQuater &q); friend GQuater inv(const GQuater &q);
GQuater& Normalize(); GQuater& SetIdentitf(); GQuater& SetConjugate(); GQuater& SetInverse(); };
|