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.

243 lines
6.8 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
  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. using namespace std;
  18. class GVector3;
  19. class GVector;
  20. class GMatrix;
  21. class GPlane;
  22. typedef GVector3 GPoint3;
  23. class GVector3 {
  24. private:
  25. float v[3];
  26. public:
  27. GVector3(float x = 0.0f, float y = 0.0f, float z = 0.0f);
  28. GVector3(const GVector3& copy);
  29. GVector3& operator =(const GVector3& rhs);
  30. //�����Ӽ�
  31. GVector3 operator +(const GVector3& rhs) const; // u + v
  32. GVector3 operator -(const GVector3& rhs) const; // u - v
  33. //�����ͳ����˳�
  34. friend GVector3 operator *(const GVector3& lhs, const float& k); // u * k
  35. friend GVector3 operator *(const float& k, const GVector3& rhs); // k * u
  36. friend GVector3 operator /(const GVector3& lhs, const float& k); // u / k
  37. //��������ģ�ͷ���
  38. friend float norm(const GVector3& v); //��������С��ģ������
  39. GVector3& normalize(); //���������� �����
  40. //���������ڻ�
  41. float operator *(const GVector3& rhs) const;
  42. //����ͶӰ
  43. friend GVector3 proj(const GVector3& p, const GVector3& q);
  44. friend GVector3 perp(const GVector3& p, const GVector3& q);
  45. //�������ڻ� ����
  46. GVector3 operator^(const GVector3& rhs) const;
  47. //����������1
  48. GVector3& Set(const float& x, const float& y, const float& z);
  49. friend float distance(const GVector3& v, const GVector3 u);
  50. //����������2
  51. GVector3& operator +=(const GVector3& rhs);
  52. GVector3& operator -=(const GVector3& rhs);
  53. friend ostream& operator<<(ostream& os, const GVector3& v);
  54. //����������3
  55. GVector3& operator *=(const float& k);
  56. GVector3& operator /=(const float& k);
  57. GVector3& operator ^=(const GVector3& rhs);
  58. //����������4
  59. bool operator ==(const GVector3 rhs) const;
  60. bool operator !=(const GVector3 rhs) const;
  61. //����������5
  62. GVector3 operator+() const;
  63. GVector3 operator-() const;
  64. float& operator [](const int& idx); //�±긳ֵ
  65. const float operator [](const int& idx) const;//�±���ȡ����
  66. };
  67. class GVector {
  68. public:
  69. //GVector 1~2
  70. GVector(int dim = 3);
  71. GVector(int dim, double x, ...);
  72. GVector(const GVector3& copy);
  73. GVector(const GVector& copy);
  74. ~GVector();
  75. //GVector 3
  76. GVector& Set(double x, ...);
  77. GVector& Set(float *p);
  78. //GVector 4
  79. GVector& operator =(const GVector& rhs);
  80. GVector& operator +=(const GVector& rhs);
  81. GVector& operator -=(const GVector& rhs);
  82. GVector& operator +=(const float& k);
  83. GVector& operator -=(const float& k);
  84. GVector &operator *=(const float &k);
  85. GVector &operator /=(const float &k);
  86. bool operator ==(const GVector& rhs) const;
  87. bool operator !=(const GVector& rhs) const;
  88. GVector operator +() const;
  89. GVector operator -() const;
  90. GVector operator +(const GVector& rhs) const;
  91. GVector operator -(const GVector& rhs) const;
  92. float operator *(const GVector& rhs) const;
  93. GVector operator /(const float& k) const;
  94. float& operator [](const int& idx);
  95. const float& operator [](const int& idx) const;
  96. GVector& Nornalize();
  97. int GetDim() const;
  98. friend GVector operator *(const float& k, const GVector& rhs);
  99. friend GVector operator *(const GVector& lhs, const float& k);
  100. friend GVector operator *(const GMatrix& m, const GVector& v);
  101. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  102. friend float norm(const GVector& v);
  103. friend float distance(const GVector& v, const GVector& u);
  104. friend ostream& operator <<(ostream& os, const GVector& v);
  105. friend class GMatrix;
  106. private:
  107. int n;
  108. float* v;
  109. };
  110. class GMatrix {
  111. public:
  112. GMatrix(int row = 4, int col = 4, float *elem = NULL);
  113. GMatrix(const GMatrix& copy);
  114. ~GMatrix();
  115. GMatrix& operator =(const GMatrix& rhs);
  116. GMatrix& operator +=(const GMatrix& rhs);
  117. GMatrix& operator -=(const GMatrix& rhs);
  118. GMatrix& operator *=(const float& k);
  119. GMatrix& operator *=(const GMatrix& rhs);
  120. GMatrix& operator /=(const float& k);
  121. bool operator ==(const GMatrix& rhs) const;
  122. bool operator !=(const GMatrix& rhs) const;
  123. float* operator [](const int idx);
  124. const float* operator [](const int idx) const;
  125. GMatrix operator +() const;
  126. GMatrix operator -() const;
  127. GMatrix operator +(const GMatrix& rhs) const;
  128. GMatrix operator -(const GMatrix& rhs) const;
  129. GMatrix operator *(const GMatrix& rhs) const;
  130. GMatrix operator /(const float& k) const;
  131. GMatrix& SetTranspose();
  132. GMatrix& SetIdentity();
  133. GMatrix& SetZeros();
  134. GMatrix& SetRowVec(const int idx, const GVector& v);
  135. GMatrix& SetColVec(const int idx, const GVector& v);
  136. GVector GetRowVec(const int idx) const;
  137. GVector GetColVec(const int idx) const;
  138. GMatrix& ExchangeRows(const int idx0, const int idx1);
  139. GMatrix& ExchangeCols(const int idx0, const int idx1);
  140. int GetRowNum() const;
  141. int GetColNum() const;
  142. bool IsSquare() const;
  143. friend GMatrix operator *(const GMatrix& lhs, const float& k);
  144. friend GMatrix operator *(const float& k , const GMatrix& rhs);
  145. friend GVector operator *(const GMatrix& m, const GVector& v);
  146. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  147. friend ostream& operator <<(ostream& os, const GMatrix& m);
  148. friend GMatrix RowEchelonForm(const GMatrix& m);
  149. friend GMatrix ReduceRowEchelonForm(const GMatrix& m);
  150. friend float* from_arr(const GMatrix& m);
  151. friend int Rank(const GMatrix& m);//��ȡ��������
  152. friend int Nullity(const GMatrix& m);//��ȡ�������㻯��
  153. friend GMatrix Mij(const GMatrix& m, int r, int c); //��������ʽ����
  154. friend float Det(const GMatrix& m);
  155. private:
  156. int r;
  157. int c;
  158. float *m;
  159. };
  160. class GLine {
  161. private:
  162. // l(t) = p + t*v;
  163. GPoint3 p;//�˵�
  164. GVector3 v;//����
  165. public:
  166. GLine(const GPoint3& _p = GPoint3(0.0f, 0.0f, 0.0f), const GVector3& _v = GVector3(0.0f, 0.0f, 0.0f));
  167. GLine(const GLine& copy);
  168. GLine& operator =(const GLine& rhs);
  169. GPoint3 operator ()(const float t) const;
  170. GLine& SetPt(const GPoint3& _p);
  171. GLine& SetDir(const GVector3& _v);
  172. GVector3 GetPt() const;
  173. GVector3 GetDir() const;
  174. bool IsOnLine(const GPoint3& q);
  175. friend float dist(const GPoint3& q, const GLine& l);
  176. };
  177. class GPlane {
  178. private:
  179. //ax+by+cz+d=0 a b c ��ʵ���Ƿ��ߣ�d��ƽ���ϵ�һ�����˷�����ȡ����
  180. GVector3 n;
  181. float d;
  182. public:
  183. GPlane(const GVector3& _n = GVector3(0.0f, 0.0f, 0.0f), const GPoint3& _p = GVector3(0.0f, 0.0f, 0.0f));
  184. GPlane(const GPoint3& p1, const GPoint3& p2, const GPoint3& p3);
  185. GPlane(const float& a, const float& b, const float&c, const float& d);
  186. GPlane(const GPlane& copy);
  187. GPlane& operator =(const GPlane& rhs);
  188. GVector3 GetNormal() const;
  189. };