You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
8.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <cmath>
  3. #include <stdio.h>
  4. #include <cassert>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <stdarg.h>
  8. #define PRECISION 0.000001
  9. #define SQRT(X) sqrt((X)) //��ƽ����
  10. #define SQR(X) ((X)*(X)) //��ƽ��
  11. #define ABS(X) abs((X))
  12. #define EQ(X,Y,EPS) (ABS((X)-(Y))<EPS)//ͨ������ȥ�жϣ��������Ƿ�����
  13. #define EQ_ZERO(X,EPS) (ABS((X))<EPS)
  14. #define ARR_ZERO(A, N) memset((A), 0, sizeof(A[0])*(N))
  15. #define MIN(x,y) (x)<(y)?(x):(y)
  16. #define SWAP(type, x, y) {type tmp=(x); (x)=(y); (y) = tmp;}
  17. #define M_PI 3.141592
  18. using namespace std;
  19. class GVector3;
  20. class GVector;
  21. class GMatrix;
  22. class GPlane;
  23. class GQuater;
  24. typedef GVector3 GPoint3;
  25. class GVector3 {
  26. private:
  27. float v[3];
  28. public:
  29. GVector3(float x = 0.0f, float y = 0.0f, float z = 0.0f);
  30. GVector3(const GVector3& copy);
  31. GVector3& operator =(const GVector3& rhs);
  32. //�����Ӽ�
  33. GVector3 operator +(const GVector3& rhs) const; // u + v
  34. GVector3 operator -(const GVector3& rhs) const; // u - v
  35. //�����ͳ����˳�
  36. friend GVector3 operator *(const GVector3& lhs, const float& k); // u * k
  37. friend GVector3 operator *(const float& k, const GVector3& rhs); // k * u
  38. friend GVector3 operator /(const GVector3& lhs, const float& k); // u / k
  39. //��������ģ�ͷ���
  40. friend float norm(const GVector3& v); //��������С��ģ������
  41. GVector3& normalize(); //���������� �����
  42. //���������ڻ�
  43. float operator *(const GVector3& rhs) const;
  44. //����ͶӰ
  45. friend GVector3 proj(const GVector3& p, const GVector3& q);
  46. friend GVector3 perp(const GVector3& p, const GVector3& q);
  47. //�������ڻ� ����
  48. GVector3 operator^(const GVector3& rhs) const;
  49. //����������1
  50. GVector3& Set(const float& x, const float& y, const float& z);
  51. friend float distance(const GVector3& v, const GVector3 u);
  52. //����������2
  53. GVector3& operator +=(const GVector3& rhs);
  54. GVector3& operator -=(const GVector3& rhs);
  55. friend ostream& operator<<(ostream& os, const GVector3& v);
  56. //����������3
  57. GVector3& operator *=(const float& k);
  58. GVector3& operator /=(const float& k);
  59. GVector3& operator ^=(const GVector3& rhs);
  60. //����������4
  61. bool operator ==(const GVector3 rhs) const;
  62. bool operator !=(const GVector3 rhs) const;
  63. //����������5
  64. GVector3 operator+() const;
  65. GVector3 operator-() const;
  66. float& operator [](const int& idx); //�±긳ֵ
  67. const float operator [](const int& idx) const;//�±���ȡ����
  68. };
  69. class GVector {
  70. public:
  71. //GVector 1~2
  72. GVector(int dim = 3);
  73. GVector(int dim, double x, ...);
  74. GVector(const GVector3& copy);
  75. GVector(const GVector& copy);
  76. ~GVector();
  77. //GVector 3
  78. GVector& Set(double x, ...);
  79. GVector& Set(float *p);
  80. //GVector 4
  81. GVector& operator =(const GVector& rhs);
  82. GVector& operator +=(const GVector& rhs);
  83. GVector& operator -=(const GVector& rhs);
  84. GVector& operator +=(const float& k);
  85. GVector& operator -=(const float& k);
  86. GVector &operator *=(const float &k);
  87. GVector &operator /=(const float &k);
  88. bool operator ==(const GVector& rhs) const;
  89. bool operator !=(const GVector& rhs) const;
  90. GVector operator +() const;
  91. GVector operator -() const;
  92. GVector operator +(const GVector& rhs) const;
  93. GVector operator -(const GVector& rhs) const;
  94. float operator *(const GVector& rhs) const;
  95. GVector operator /(const float& k) const;
  96. float& operator [](const int& idx);
  97. const float& operator [](const int& idx) const;
  98. GVector& Nornalize();
  99. int GetDim() const;
  100. friend GVector operator *(const float& k, const GVector& rhs);
  101. friend GVector operator *(const GVector& lhs, const float& k);
  102. friend GVector operator *(const GMatrix& m, const GVector& v);
  103. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  104. friend float norm(const GVector& v);
  105. friend float distance(const GVector& v, const GVector& u);
  106. friend ostream& operator <<(ostream& os, const GVector& v);
  107. friend class GMatrix;
  108. private:
  109. int n;
  110. float* v;
  111. };
  112. class GMatrix {
  113. public:
  114. GMatrix(int row = 4, int col = 4, float *elem = NULL);
  115. GMatrix(const GMatrix& copy);
  116. ~GMatrix();
  117. GMatrix& operator =(const GMatrix& rhs);
  118. GMatrix& operator +=(const GMatrix& rhs);
  119. GMatrix& operator -=(const GMatrix& rhs);
  120. GMatrix& operator *=(const float& k);
  121. GMatrix& operator *=(const GMatrix& rhs);
  122. GMatrix& operator /=(const float& k);
  123. bool operator ==(const GMatrix& rhs) const;
  124. bool operator !=(const GMatrix& rhs) const;
  125. float* operator [](const int idx);
  126. const float* operator [](const int idx) const;
  127. GMatrix operator +() const;
  128. GMatrix operator -() const;
  129. GMatrix operator +(const GMatrix& rhs) const;
  130. GMatrix operator -(const GMatrix& rhs) const;
  131. GMatrix operator *(const GMatrix& rhs) const;
  132. GMatrix operator /(const float& k) const;
  133. GMatrix& SetInverse();
  134. GMatrix& SetTranspose();
  135. GMatrix& SetIdentity();
  136. GMatrix& SetZeros();
  137. GMatrix& SetRowVec(const int idx, const GVector& v);
  138. GMatrix& SetColVec(const int idx, const GVector& v);
  139. GVector GetRowVec(const int idx) const;
  140. GVector GetColVec(const int idx) const;
  141. GMatrix& ExchangeRows(const int idx0, const int idx1);
  142. GMatrix& ExchangeCols(const int idx0, const int idx1);
  143. int GetRowNum() const;
  144. int GetColNum() const;
  145. bool IsSquare() const;
  146. friend GMatrix operator *(const GMatrix& lhs, const float& k);
  147. friend GMatrix operator *(const float& k , const GMatrix& rhs);
  148. friend GVector operator *(const GMatrix& m, const GVector& v);
  149. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  150. friend ostream& operator <<(ostream& os, const GMatrix& m);
  151. friend GMatrix RowEchelonForm(const GMatrix& m);
  152. friend GMatrix ReduceRowEchelonForm(const GMatrix& m);
  153. friend float* from_arr(const GMatrix& m);
  154. friend int Rank(const GMatrix& m);//��ȡ��������
  155. friend int Nullity(const GMatrix& m);//��ȡ�������㻯��
  156. friend GMatrix MijMatrix(const GMatrix& m, int r, int c); //��������ʽ����
  157. friend float Mij(const GMatrix& m, int r, int c); //��������ʽ
  158. friend float Cij(const GMatrix& m, int r, int c); //������������ʽ����������ʽֵ
  159. friend float Det(const GMatrix& m);
  160. friend GMatrix Inverse(const GMatrix& m);
  161. friend GMatrix Transpose(const GMatrix& m);
  162. friend GMatrix Adjoint(const GMatrix& m);//����������
  163. private:
  164. int r;
  165. int c;
  166. float *m;
  167. };
  168. class GLine {
  169. private:
  170. // l(t) = p + t*v;
  171. GPoint3 p;//�˵�
  172. GVector3 v;//����
  173. public:
  174. GLine(const GPoint3& _p = GPoint3(0.0f, 0.0f, 0.0f), const GVector3& _v = GVector3(0.0f, 0.0f, 0.0f));
  175. GLine(const GLine& copy);
  176. GLine& operator =(const GLine& rhs);
  177. GPoint3 operator ()(const float t) const;
  178. GLine& SetPt(const GPoint3& _p);
  179. GLine& SetDir(const GVector3& _v);
  180. GVector3 GetPt() const;
  181. GVector3 GetDir() const;
  182. bool IsOnLine(const GPoint3& q);
  183. friend float dist(const GPoint3& q, const GLine& l);
  184. friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi);
  185. friend bool intersect_line_triangle(GPoint3& p, const GLine& l, const GPoint3& p1, const GPoint3& p2, const GPoint3& p3);
  186. };
  187. class GPlane {
  188. private:
  189. //ax+by+cz+d=0 a b c ��ʵ���Ƿ��ߣ�d��ƽ���ϵ�һ�����˷�����ȡ����
  190. GVector3 n;
  191. float d;
  192. public:
  193. GPlane(const GVector3& _n = GVector3(0.0f, 0.0f, 0.0f), const GPoint3& _p = GVector3(0.0f, 0.0f, 0.0f));
  194. GPlane(const GPoint3& p1, const GPoint3& p2, const GPoint3& p3);
  195. GPlane(const float& a, const float& b, const float&c, const float& d);
  196. GPlane(const GPlane& copy);
  197. GPlane& operator =(const GPlane& rhs);
  198. GVector3 GetNormal() const;
  199. friend float dist(const GPlane& pi, const GPoint3& p);
  200. friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi);
  201. friend bool intersect_line_triangle(GPoint3& p, const GLine& l, const GPoint3& p1, const GPoint3& p2, const GPoint3& p3);
  202. };
  203. class GQuater {
  204. private:
  205. float w, x, y, z;
  206. public:
  207. GQuater(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f);
  208. GQuater(const GQuater& copy);
  209. GQuater(GVector3 axis, float theta, bool radian = false);
  210. GQuater& operator=(const GQuater& rhs);
  211. GQuater& operator+=(const GQuater& rhs);
  212. GQuater& operator-=(const GQuater& rhs);
  213. GQuater& operator*=(const GQuater& rhs);
  214. GQuater& operator*=(const float& s);
  215. GQuater& operator/=(const float& s);
  216. GQuater operator+() const;
  217. GQuater operator-() const;
  218. GQuater operator+(const GQuater& rhs) const;
  219. GQuater operator-(const GQuater& rhs) const;
  220. GQuater operator*(const GQuater& rhs) const;
  221. GQuater operator*(const float& s) const;
  222. GQuater operator/(const float& s) const;
  223. GQuater& Set(const float w, const float x, const float y, const float z);
  224. GQuater& Set(float *q, bool invOrder = false);
  225. bool IsUnitQuater() const;
  226. bool IsIdentity() const;
  227. friend GQuater operator*(const float &s, const GQuater &rhs);
  228. friend ostream& operator<<(ostream &os, const GQuater &q);
  229. friend float norm(const GQuater &q);
  230. friend GQuater inv(const GQuater &q);
  231. GQuater& Normalize();
  232. GQuater& SetIdentitf();
  233. GQuater& SetConjugate();
  234. GQuater& SetInverse();
  235. };