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.

1546 lines
25 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
  1. #include "GMath.h"
  2. GVector3::GVector3(float x, float y, float z)
  3. {
  4. v[0] = x;
  5. v[1] = y;
  6. v[2] = z;
  7. }
  8. GVector3::GVector3(const GVector3 & copy)
  9. {
  10. v[0] = copy.v[0];
  11. v[1] = copy.v[1];
  12. v[2] = copy.v[2];
  13. }
  14. GVector3 & GVector3::operator=(const GVector3 & rhs)
  15. {
  16. v[0] = rhs.v[0];
  17. v[1] = rhs.v[1];
  18. v[2] = rhs.v[2];
  19. return *this;
  20. }
  21. GVector3 GVector3::operator+(const GVector3 & rhs) const
  22. {
  23. GVector3 ret;
  24. ret.v[0] = v[0] + rhs.v[0];
  25. ret.v[1] = v[1] + rhs.v[1];
  26. ret.v[2] = v[2] + rhs.v[2];
  27. return ret;
  28. }
  29. GVector3 GVector3::operator-(const GVector3 & rhs) const
  30. {
  31. GVector3 ret;
  32. ret.v[0] = v[0] - rhs.v[0];
  33. ret.v[1] = v[1] - rhs.v[1];
  34. ret.v[2] = v[2] - rhs.v[2];
  35. return ret;
  36. }
  37. GVector3 operator*(const GVector3 & lhs, const float & k)
  38. {
  39. GVector3 ret;
  40. ret.v[0] = lhs.v[0] * k;
  41. ret.v[1] = lhs.v[1] * k;
  42. ret.v[2] = lhs.v[2] * k;
  43. return ret;
  44. }
  45. GVector3 operator*(const float & k, const GVector3& rhs)
  46. {
  47. GVector3 ret;
  48. ret.v[0] = rhs.v[0] * k;
  49. ret.v[1] = rhs.v[1] * k;
  50. ret.v[2] = rhs.v[2] * k;
  51. return ret;
  52. }
  53. GVector3 operator/(const GVector3 & lhs, const float & k)
  54. {
  55. GVector3 ret;
  56. assert(k != 0);
  57. ret.v[0] = lhs.v[0] / k;
  58. ret.v[1] = lhs.v[1] / k;
  59. ret.v[2] = lhs.v[2] / k;
  60. return ret;
  61. }
  62. float norm(const GVector3 & v)
  63. {
  64. return SQRT(v.v[0] * v.v[0] + v.v[1] * v.v[1] + v.v[2] * v.v[2]);
  65. }
  66. GVector3 & GVector3::normalize()
  67. {
  68. float len = norm(*this);
  69. if (len > PRECISION) {
  70. v[0] /= len;
  71. v[1] /= len;
  72. v[2] /= len;
  73. }
  74. return *this;
  75. }
  76. float GVector3::operator*(const GVector3 & rhs) const
  77. {
  78. float ret;
  79. ret = v[0] * rhs.v[0] + v[1] * rhs.v[1] +v[2] * rhs.v[2];
  80. return ret;
  81. }
  82. GVector3 proj(const GVector3 & p, const GVector3 & q)
  83. {
  84. return (p*q) / SQR(norm(q)) * q;
  85. }
  86. GVector3 perp(const GVector3 & p, const GVector3 & q)
  87. {
  88. return p - proj(p,q);
  89. }
  90. GVector3 GVector3::operator^(const GVector3 & rhs) const
  91. {
  92. GVector3 ret;
  93. ret.v[0] = v[1] * rhs.v[2] - v[2] * rhs.v[1];
  94. ret.v[1] = v[2] * rhs.v[0] - v[0] * rhs.v[2];
  95. ret.v[2] = v[0] * rhs.v[1] - v[1] * rhs.v[0];
  96. return ret;
  97. }
  98. GVector3 & GVector3::Set(const float & x, const float & y, const float & z)
  99. {
  100. v[0] = x;
  101. v[1] = y;
  102. v[2] = z;
  103. return *this;
  104. }
  105. float distance(const GVector3 & v, const GVector3 u)
  106. {
  107. float ret = 0.0;
  108. float a = v.v[0] - u.v[0];
  109. float b = v.v[1] - u.v[1];
  110. float c = v.v[2] - u.v[2];
  111. ret = SQRT(SQR(a) + SQR(b) + SQR(c));
  112. return ret;
  113. }
  114. GVector3 & GVector3::operator+=(const GVector3 & rhs)
  115. {
  116. v[0] += rhs.v[0];
  117. v[1] += rhs.v[1];
  118. v[2] += rhs.v[2];
  119. return *this;
  120. }
  121. GVector3 & GVector3::operator-=(const GVector3 & rhs)
  122. {
  123. v[0] -= rhs.v[0];
  124. v[1] -= rhs.v[1];
  125. v[2] -= rhs.v[2];
  126. return *this;
  127. }
  128. ostream & operator<<(ostream & os, const GVector3 & v)
  129. {
  130. os << "[" << setw(2) << v.v[0] << ", " << setw(2) << v.v[1] << ", " << setw(2) << v.v[2] << "]";
  131. return os;
  132. }
  133. GVector3 & GVector3::operator*=(const float & k)
  134. {
  135. v[0] *= k;
  136. v[1] *= k;
  137. v[2] *= k;
  138. return *this;
  139. }
  140. GVector3 & GVector3::operator/=(const float & k)
  141. {
  142. v[0] /= k;
  143. v[1] /= k;
  144. v[2] /= k;
  145. return *this;
  146. }
  147. GVector3 & GVector3::operator^=(const GVector3 & rhs)
  148. {
  149. v[0] = v[1] * rhs.v[2] - v[2] * rhs.v[1];
  150. v[1] = v[2] * rhs.v[0] - v[0] * rhs.v[2];
  151. v[2] = v[0] * rhs.v[1] - v[1] * rhs.v[0];
  152. return *this;
  153. }
  154. bool GVector3::operator==(const GVector3 rhs) const
  155. {
  156. return !((*this) != rhs);
  157. }
  158. bool GVector3::operator!=(const GVector3 rhs) const
  159. {
  160. return (!EQ(v[0], rhs.v[0], PRECISION) || !EQ(v[1], rhs.v[1], PRECISION) || !EQ(v[2], rhs.v[2], PRECISION));
  161. }
  162. GVector3 GVector3::operator+() const
  163. {
  164. return *this;
  165. }
  166. GVector3 GVector3::operator-() const
  167. {
  168. return *this * -1;
  169. }
  170. float & GVector3::operator[](const int & idx)
  171. {
  172. return v[idx];
  173. }
  174. const float GVector3::operator[](const int & idx) const
  175. {
  176. return v[idx];
  177. }
  178. //----GVector----//
  179. GVector::GVector(int dim)
  180. {
  181. n = dim;
  182. v = new float[n];
  183. ARR_ZERO(v, n);
  184. }
  185. GVector::GVector(int dim, double x, ...)
  186. {
  187. n = dim;
  188. v = new float[n];
  189. va_list ap;
  190. va_start(ap, dim);
  191. for (int i = 0; i < n; i++) {
  192. v[i] = (float)va_arg(ap, double);
  193. }
  194. va_end(ap);
  195. }
  196. GVector::GVector(const GVector3 & copy)
  197. {
  198. n = 3;
  199. v = new float[3];
  200. v[0] = copy[0];
  201. v[1] = copy[1];
  202. v[2] = copy[2];
  203. }
  204. GVector::GVector(const GVector & copy)
  205. {
  206. n = copy.n;
  207. v = new float[n];
  208. memcpy(v, copy.v, n * sizeof(float));
  209. }
  210. GVector::GVector(int dim, const GVector3& copy)
  211. {
  212. n = dim;
  213. v = new float[n];
  214. for (int i = 0; i < n; i++) {
  215. if (i < 3) {
  216. v[i] = copy[i];
  217. }
  218. else {
  219. v[i] = 0;
  220. }
  221. }
  222. }
  223. GVector::~GVector()
  224. {
  225. if (v) {
  226. delete[] v;
  227. }
  228. v = NULL;
  229. }
  230. GVector & GVector::Set(double x, ...)
  231. {
  232. v[0] = (float)x;
  233. va_list ap;
  234. va_start(ap, x);
  235. for (int i = 1; i < n; i++) {
  236. v[i] = (float)va_arg(ap, double);
  237. }
  238. va_end(ap);
  239. return *this;
  240. }
  241. GVector & GVector::Set(float * p)
  242. {
  243. memcpy(v, p, sizeof(float) * n);
  244. return *this;
  245. }
  246. GVector & GVector::operator=(const GVector & rhs)
  247. {
  248. if (v) {
  249. delete[] v;
  250. }
  251. n = rhs.n;
  252. v = new float[n];
  253. memcpy(v, rhs.v, n * sizeof(float));
  254. return *this;
  255. }
  256. GVector & GVector::operator+=(const GVector & rhs)
  257. {
  258. assert(n = rhs.n);
  259. for (int i = 0; i < n; i++) {
  260. v[i] += rhs.v[i];
  261. }
  262. return *this;
  263. }
  264. GVector & GVector::operator-=(const GVector & rhs)
  265. {
  266. assert(n = rhs.n);
  267. for (int i = 0; i < n; i++) {
  268. v[i] -= rhs.v[i];
  269. }
  270. return *this;
  271. }
  272. GVector & GVector::operator+=(const float & k)
  273. {
  274. for (int i = 0; i < n; i++) {
  275. v[i] += k;
  276. }
  277. return *this;
  278. }
  279. GVector & GVector::operator-=(const float & k)
  280. {
  281. for (int i = 0; i < n; i++) {
  282. v[i] -= k;
  283. }
  284. return *this;
  285. }
  286. GVector & GVector::operator*=(const float & k)
  287. {
  288. for (int i = 0; i < n; i++) {
  289. v[i] *= k;
  290. }
  291. return *this;
  292. }
  293. GVector & GVector::operator/=(const float & k)
  294. {
  295. for (int i = 0; i < n; i++) {
  296. v[i] /= k;
  297. }
  298. return *this;
  299. }
  300. bool GVector::operator==(const GVector & rhs) const
  301. {
  302. return ((*this) == rhs);
  303. }
  304. bool GVector::operator!=(const GVector & rhs) const
  305. {
  306. assert(n == rhs.n);
  307. for (int i = 0; i < n; i++) {
  308. if (!EQ(v[i], rhs.v[i], PRECISION)) {
  309. return true;
  310. }
  311. }
  312. return false;
  313. }
  314. GVector GVector::operator+() const
  315. {
  316. return *this;
  317. }
  318. GVector GVector::operator-() const
  319. {
  320. return *this * -1;
  321. }
  322. GVector GVector::operator+(const GVector & rhs) const
  323. {
  324. assert(n == rhs.n);
  325. GVector ret = GVector(n);
  326. for (int i = 0; i < n; i++) {
  327. ret.v[i] = v[i] + rhs.v[i];
  328. }
  329. return ret;
  330. }
  331. GVector GVector::operator-(const GVector & rhs) const
  332. {
  333. assert(n == rhs.n);
  334. GVector ret = GVector(n);
  335. for (int i = 0; i < n; i++) {
  336. ret.v[i] = v[i] - rhs.v[i];
  337. }
  338. return ret;
  339. }
  340. float GVector::operator*(const GVector & rhs) const
  341. {
  342. assert(n == rhs.n);
  343. float ret = 0;
  344. for (int i = 0; i < n; i++) {
  345. ret += v[i] * rhs.v[i];
  346. }
  347. return ret;
  348. }
  349. GVector GVector::operator/(const float & k) const
  350. {
  351. GVector ret = GVector(n);
  352. for (int i = 0; i < n; i++) {
  353. ret.v[i] = v[i]/k;
  354. }
  355. return ret;
  356. }
  357. float & GVector::operator[](const int & idx)
  358. {
  359. assert(idx >= 0 && idx <= n);
  360. return v[idx];
  361. }
  362. const float & GVector::operator[](const int & idx) const
  363. {
  364. assert(idx >= 0 && idx <= n);
  365. return v[idx];
  366. }
  367. GVector & GVector::Nornalize()
  368. {
  369. float m = norm(*this);
  370. for (int i = 0; i < n; i++) {
  371. v[i] /= m;
  372. }
  373. return *this;
  374. }
  375. int GVector::GetDim() const
  376. {
  377. return n;
  378. }
  379. GVector operator*(const float & k, const GVector & rhs)
  380. {
  381. GVector ret(rhs.n);
  382. for (int i = 0; i < ret.n; i++) {
  383. ret.v[i] = rhs[i] * k;
  384. }
  385. return ret;
  386. }
  387. GVector operator*(const GVector & lhs, const float & k)
  388. {
  389. GVector ret(lhs.n);
  390. for (int i = 0; i < ret.n; i++) {
  391. ret.v[i] = lhs[i] * k;
  392. }
  393. return ret;
  394. }
  395. float norm(const GVector & v)
  396. {
  397. float ret = 0;
  398. for (int i = 0; i < v.n; i++) {
  399. ret += SQR(v.v[i]);
  400. }
  401. ret = SQRT(ret);
  402. return ret;
  403. }
  404. float distance(const GVector & v, const GVector & u)
  405. {
  406. return norm(v - u);
  407. }
  408. ostream & operator<<(ostream & os, const GVector & v)
  409. {
  410. os << "[ ";
  411. for (int i = 0; i < v.n; i++) {
  412. os << setw(5) << v.v[i];
  413. if (i != v.n - 1) {
  414. os << ", ";
  415. }
  416. }
  417. os << " ]" << endl;
  418. return os;
  419. }
  420. GMatrix::GMatrix(int row, int col, float * elem)
  421. {
  422. r = row;
  423. c = col;
  424. m = new float[r*c];
  425. if (elem) {
  426. memcpy(m, elem, sizeof(float)*r*c);
  427. }
  428. else {
  429. ARR_ZERO(m, r*c);
  430. }
  431. }
  432. GMatrix::GMatrix(const GMatrix & copy)
  433. {
  434. r = copy.r;
  435. c = copy.c;
  436. m = new float[r*c];
  437. memcpy(m, copy.m, sizeof(float)*r*c);
  438. }
  439. GMatrix::~GMatrix()
  440. {
  441. if (m) {
  442. delete[] m;
  443. }
  444. m = NULL;
  445. }
  446. GMatrix & GMatrix::operator=(const GMatrix & rhs)
  447. {
  448. if (m) {
  449. delete[] m;
  450. }
  451. r = rhs.r;
  452. c = rhs.c;
  453. m = new float[r*c];
  454. memcpy(m, rhs.m, sizeof(float)*r*c);
  455. return *this;
  456. }
  457. GMatrix & GMatrix::operator+=(const GMatrix & rhs)
  458. {
  459. assert(r == rhs.r && c == rhs.c);
  460. for (int i = 0; i < r*c; i++) {
  461. m[i] += rhs.m[i];
  462. }
  463. return *this;
  464. }
  465. GMatrix & GMatrix::operator-=(const GMatrix & rhs)
  466. {
  467. assert(r == rhs.r && c == rhs.c);
  468. for (int i = 0; i < r*c; i++) {
  469. m[i] -= rhs.m[i];
  470. }
  471. return *this;
  472. }
  473. GMatrix & GMatrix::operator*=(const float & k)
  474. {
  475. for (int i = 0; i < r*c; i++) {
  476. m[i] *= k;
  477. }
  478. return *this;
  479. }
  480. GMatrix & GMatrix::operator*=(const GMatrix & rhs)
  481. {
  482. assert(c == rhs.r);
  483. c = rhs.c;
  484. float* p = new float[r*c];
  485. ARR_ZERO(p, r*c)
  486. ;
  487. for (int i = 0; i < r; i++) {
  488. for (int j = 0; j < c; j++) {
  489. for (int k = 0; k < rhs.r; k++) {
  490. p[i*c + j] += m[i*rhs.r + k] * rhs.m[k*c + j];
  491. }
  492. }
  493. }
  494. delete[] m;
  495. m = p;
  496. return *this;
  497. }
  498. GMatrix & GMatrix::operator/=(const float & k)
  499. {
  500. assert(k != 0);
  501. for (int i = 0; i < r*c; i++) {
  502. m[i] /= k;
  503. }
  504. return *this;
  505. }
  506. GMatrix GMatrix::operator+() const
  507. {
  508. return *this;
  509. }
  510. GMatrix GMatrix::operator-() const
  511. {
  512. return *this*-1;
  513. }
  514. GMatrix GMatrix::operator+(const GMatrix & rhs) const
  515. {
  516. assert(r == rhs.r && c == rhs.c);
  517. GMatrix ret(*this);
  518. ret += rhs;
  519. return ret;
  520. }
  521. GMatrix GMatrix::operator-(const GMatrix & rhs) const
  522. {
  523. assert(r == rhs.r && c == rhs.c);
  524. GMatrix ret(*this);
  525. ret -= rhs;
  526. return ret;
  527. }
  528. GMatrix GMatrix::operator*(const GMatrix & rhs) const
  529. {
  530. assert(c == rhs.r);
  531. GMatrix ret(*this);
  532. ret *= rhs;
  533. return ret;
  534. }
  535. GMatrix GMatrix::operator/(const float & k) const
  536. {
  537. GMatrix ret(*this);
  538. ret /= k;
  539. return ret;
  540. }
  541. GMatrix operator*(const GMatrix & lhs, const float & k)
  542. {
  543. GMatrix ret(lhs);
  544. ret *= k;
  545. return ret;
  546. }
  547. GMatrix operator*(const float& k, const GMatrix & rhs)
  548. {
  549. GMatrix ret(rhs);
  550. ret *= k;
  551. return ret;
  552. }
  553. GVector operator*(const GMatrix & m, const GVector & v)
  554. {
  555. assert(m.c == v.n);
  556. GVector ret(m.r);
  557. for (int i = 0; i < m.r; i++) {
  558. for (int j = 0; j < m.c; j++) {
  559. ret.v[i] += m.m[i*m.c + j] * v.v[j];
  560. }
  561. }
  562. return ret;
  563. }
  564. GMatrix operator*(const GVector & v, const GMatrix & m)
  565. {
  566. assert(m.r == 1);
  567. GMatrix ret(v.n, m.c);
  568. for (int i = 0; i < v.n; i++) {
  569. for (int j = 0; j < m.c; j++) {
  570. ret.m[i*m.c + j] = v.v[i] * m.m[j];
  571. }
  572. }
  573. return ret;
  574. }
  575. bool GMatrix::operator==(const GMatrix & rhs) const
  576. {
  577. if (r != rhs.r || c != rhs.c) {
  578. return false;
  579. }
  580. for (int i = 0; i < r*c; i++) {
  581. if (abs(m[i] - rhs.m[i]) > PRECISION) {
  582. return false;
  583. }
  584. }
  585. return true;
  586. }
  587. bool GMatrix::operator!=(const GMatrix & rhs) const
  588. {
  589. if (r != rhs.r || c != rhs.c) {
  590. return true;
  591. }
  592. for (int i = 0; i < r*c; i++) {
  593. if (abs(m[i] - rhs.m[i]) > PRECISION) {
  594. return true;
  595. }
  596. }
  597. return false;
  598. }
  599. float * GMatrix::operator[](const int idx)
  600. {
  601. assert(idx >= 0 && idx < r);
  602. return &m[idx*c];
  603. }
  604. const float * GMatrix::operator[](const int idx) const
  605. {
  606. assert(idx >= 0 && idx < r);
  607. return &m[idx*c];
  608. }
  609. GMatrix & GMatrix::SetInverse()
  610. {
  611. //ֻ�з�����������
  612. assert(r == c);
  613. //���������о������������󣬷���������������������Ҳ��������ʽ����Ϊ0
  614. float det = Det(*this);
  615. assert(det != 0);
  616. //������������
  617. int exc = c * 2;
  618. GMatrix exM(r, exc);
  619. int i, j = 0;
  620. for (i = 0; i < exM.GetRowNum(); i++) {
  621. for (j = 0; j < exM.GetColNum(); j++) {
  622. if (j < c) {
  623. exM[i][j] = m[i*c + j];
  624. }
  625. else {
  626. if (j - c == i) {
  627. exM[i][j] = 1.0f;
  628. }
  629. }
  630. }
  631. }
  632. exM = ReduceRowEchelonForm(exM);
  633. for (i = 0; i < exM.GetRowNum(); i++) {
  634. for (j = 0; j < exM.GetColNum(); j++) {
  635. if (j >= c) {
  636. m[i*c + (j - c)] = exM[i][j];
  637. }
  638. }
  639. }
  640. return *this;
  641. }
  642. GMatrix & GMatrix::SetTranspose()
  643. {
  644. int i, j;
  645. if (r == c) { //Spuare matrix
  646. for (i = 0; i < r; i++) {
  647. for (j = i+1; j < c; j++) {
  648. SWAP(float, m[i*c + j], m[j* r + i])
  649. }
  650. }
  651. }
  652. else {
  653. float *p = new float[r*c];
  654. memcpy(p, m, sizeof(float)*r*c);
  655. SWAP(int, r, c);
  656. for (i = 0; i < r; i++) {
  657. for (j = 0; j < c; j++) {
  658. m[i*c + j] = p[j*r + i];
  659. }
  660. }
  661. delete[] p;
  662. p = NULL;
  663. }
  664. return *this;
  665. }
  666. GMatrix & GMatrix::SetIdentity()
  667. {
  668. int min = MIN(r, c);
  669. SetZeros();
  670. for (int i = 0; i < min; i++) {
  671. m[i*c + i] = 1.0f;
  672. }
  673. return *this;
  674. }
  675. GMatrix & GMatrix::SetZeros()
  676. {
  677. memset(m, 0, sizeof(float)*r*c);
  678. return *this;
  679. }
  680. ostream & operator<<(ostream & os, const GMatrix & m)
  681. {
  682. float r = 0.0f;
  683. for (int i = 0; i < m.r; i++) {
  684. os << "|";
  685. for (int j = 0; j < m.c; j++) {
  686. r = EQ_ZERO(m.m[i*m.c + j], PRECISION) ? 0 : m.m[i*m.c + j];
  687. os << setw(6) << r << " ";
  688. }
  689. os << " |" << endl;
  690. }
  691. return os;
  692. }
  693. GMatrix & GMatrix::SetRowVec(const int idx, const GVector & v)
  694. {
  695. assert(idx < r && v.n == c);
  696. for (int i = 0; i < c; i++) {
  697. m[idx*c + i] = v.v[i];
  698. }
  699. return *this;
  700. }
  701. GMatrix & GMatrix::SetColVec(const int idx, const GVector & v)
  702. {
  703. assert(idx < c && v.n == r);
  704. for (int i = 0; i < r; i++) {
  705. m[i*c + idx] = v.v[i];
  706. }
  707. return *this;
  708. }
  709. GVector GMatrix::GetRowVec(const int idx) const
  710. {
  711. assert(idx < r);
  712. GVector ret(c);
  713. for (int i = 0; i < c; i++) {
  714. ret.v[i] = m[idx*c + i];
  715. }
  716. return ret;
  717. }
  718. GVector GMatrix::GetColVec(const int idx) const
  719. {
  720. assert(idx < c);
  721. GVector ret(r);
  722. for (int i = 0; i < r; i++) {
  723. ret.v[i] = m[i*c + idx];
  724. }
  725. return ret;
  726. }
  727. GMatrix & GMatrix::ExchangeRows(const int idx0, const int idx1)
  728. {
  729. GVector tmp(c);
  730. tmp = GetRowVec(idx0);
  731. SetRowVec(idx0, GetRowVec(idx1));
  732. SetRowVec(idx1, tmp);
  733. return *this;
  734. }
  735. GMatrix & GMatrix::ExchangeCols(const int idx0, const int idx1)
  736. {
  737. GVector tmp(r);
  738. tmp = GetColVec(idx0);
  739. SetColVec(idx0, GetColVec(idx1));
  740. SetColVec(idx1, tmp);
  741. return *this;
  742. }
  743. int GMatrix::GetRowNum() const
  744. {
  745. return r;
  746. }
  747. int GMatrix::GetColNum() const
  748. {
  749. return c;
  750. }
  751. bool GMatrix::IsSquare() const
  752. {
  753. return (r == c) ? true : false;
  754. }
  755. GMatrix RowEchelonForm(const GMatrix & m)
  756. {
  757. int i, j, k;
  758. int r = m.GetRowNum();
  759. int c = m.GetColNum();
  760. int n = MIN(r, c);
  761. GMatrix t(m);
  762. int shift = 0;
  763. for (i = 0; i < n; i++) {
  764. float max = ABS(t[i][i + shift]);
  765. int privot_idx = i;//�к�
  766. //�ҳ� ����ֵ�����ĵ��� �� ��������
  767. for (j = i+1; j < n; j++) {
  768. if (max < ABS(t[j][i + shift])) {
  769. max = ABS(t[j][i + shift]);
  770. privot_idx = j;
  771. }
  772. }
  773. //��������ֵ��0�Ļ�����ôֱ��������һ��
  774. if (EQ_ZERO(max, PRECISION)) {
  775. shift++;
  776. i--; //�ǵ�Ҫ������һ��
  777. continue;
  778. }
  779. //��������ֵ�����в��������У��Ǿ�Ҫ����
  780. if (i != privot_idx) {
  781. t.ExchangeRows(i, privot_idx);
  782. }
  783. //ȡ������ֵ
  784. float s = t[i][i + shift];//������Ϊ��һ���Ѿ�������ֵ�н�������������
  785. //������1
  786. for (j = i + shift; j < c; j++) {
  787. t[i][j] = t[i][j] / s;
  788. }
  789. //�ѵ�ǰ�У�����1���µ�ֵ������0
  790. for (j = i + 1; j < r; j++) {
  791. s = t[j][i + shift];
  792. for (k = i + shift; k < c; k++) {
  793. t[j][k] = t[j][k] - s * t[i][k];
  794. }
  795. }
  796. }
  797. return t;
  798. }
  799. GMatrix ReduceRowEchelonForm(const GMatrix & m)
  800. {
  801. int i, j, k;
  802. int r = m.GetRowNum();
  803. int c = m.GetColNum();
  804. int n = MIN(r, c);
  805. GMatrix t(m);
  806. int shift = 0;
  807. for (i = 0; i < n; i++) {
  808. float max = ABS(t[i][i + shift]);
  809. int privot_idx = i;//�к�
  810. //�ҳ� ����ֵ�����ĵ��� �� ��������
  811. for (j = i + 1; j < n; j++) {
  812. if (max < ABS(t[j][i + shift])) {
  813. max = ABS(t[j][i + shift]);
  814. privot_idx = j;
  815. }
  816. }
  817. //��������ֵ��0�Ļ�����ôֱ��������һ��
  818. if (EQ_ZERO(max, PRECISION)) {
  819. shift++;
  820. i--; //�ǵ�Ҫ������һ��
  821. continue;
  822. }
  823. //��������ֵ�����в��������У��Ǿ�Ҫ����
  824. if (i != privot_idx) {
  825. t.ExchangeRows(i, privot_idx);
  826. }
  827. //ȡ������ֵ
  828. float s = t[i][i + shift];//������Ϊ��һ���Ѿ�������ֵ�н�������������
  829. //������1
  830. for (j = i + shift; j < c; j++) {
  831. t[i][j] = t[i][j] / s;
  832. }
  833. //�ѵ�ǰ�У�����1����ֵ������0
  834. for (j = 0; j < r; j++) {
  835. if (i == j)
  836. continue;
  837. s = t[j][i + shift];
  838. for (k = i + shift; k < c; k++) {
  839. t[j][k] = t[j][k] - s * t[i][k];
  840. }
  841. }
  842. }
  843. return t;
  844. }
  845. float * from_arr(const GMatrix & m)
  846. {
  847. return m.m;
  848. }
  849. int Rank(const GMatrix & m)
  850. {
  851. int i, r, rank = 0;
  852. r = m.GetRowNum();
  853. GMatrix t = RowEchelonForm(m);
  854. for (i = 0; i < r; i++) {
  855. GVector rVec = t.GetRowVec(i);
  856. if (!EQ_ZERO(norm(rVec), PRECISION)) {
  857. rank++;
  858. }
  859. }
  860. return rank;
  861. }
  862. int Nullity(const GMatrix & m)
  863. {
  864. int rank = Rank(m);
  865. return m.GetColNum() - rank;
  866. }
  867. GMatrix MijMatrix(const GMatrix & m, int rom, int col)
  868. {
  869. int i,j = 0;
  870. int r = m.GetRowNum();
  871. int c = m.GetColNum();
  872. assert(r == c);//����
  873. assert(rom < r && col < c);
  874. //����ʽ������������
  875. int nR = r - 1;
  876. int nC = c - 1;
  877. GMatrix ret(nR, nC);
  878. nR = 0;
  879. for (i = 0; i < r; i++) {
  880. nC = 0;
  881. if (i == rom) {
  882. continue;
  883. }
  884. for (j = 0; j < c; j++) {
  885. if (j == col) {
  886. continue;
  887. }
  888. ret[nR][nC] = m[i][j];
  889. nC++;
  890. }
  891. nR++;
  892. }
  893. return ret;
  894. }
  895. float Mij(const GMatrix & m, int r, int c)
  896. {
  897. GMatrix M = MijMatrix(m, r, c);
  898. return Det(M);
  899. }
  900. float Cij(const GMatrix & m, int r, int c)
  901. {
  902. GMatrix M = MijMatrix(m, r, c);
  903. int t = pow(-1, r + c);
  904. return t*Det(M);
  905. }
  906. float Det(const GMatrix & m)
  907. {
  908. int i = 0;
  909. int r = m.GetRowNum();
  910. int c = m.GetColNum();
  911. assert(r == c);//������������ʽ
  912. float ret = 0.0f;
  913. if (r == 1) {
  914. ret = m[0][0];
  915. }
  916. else {
  917. for (i = 0; i < c; i++) {
  918. GMatrix t = MijMatrix(m, 0, i);
  919. ret += pow(-1, 0+ i) * m[0][i] * Det(t);
  920. }
  921. }
  922. return ret;
  923. }
  924. GMatrix Inverse(const GMatrix & m)
  925. {
  926. GMatrix ret(m);
  927. ret.SetInverse();
  928. return ret;
  929. }
  930. GMatrix Transpose(const GMatrix & m)
  931. {
  932. GMatrix ret(m);
  933. ret.SetTranspose();
  934. return ret;
  935. }
  936. GMatrix Adjoint(const GMatrix & m)
  937. {
  938. GMatrix tM = Transpose(m);
  939. GMatrix ret(tM.r, tM.c);
  940. int i, j = 0;
  941. for (i = 0; i < tM.r; i++) {
  942. for (j = 0; j < tM.c; j++) {
  943. ret[i][j] = Cij(tM, i, j);
  944. }
  945. }
  946. return ret;
  947. }
  948. GLine::GLine(const GPoint3 & _p, const GVector3 & _v)
  949. {
  950. p = _p;
  951. v = _v;
  952. }
  953. GLine::GLine(const GLine & copy)
  954. {
  955. p = copy.p;
  956. v = copy.v;
  957. }
  958. GLine & GLine::operator=(const GLine & rhs)
  959. {
  960. p = rhs.p;
  961. v = rhs.v;
  962. return *this;
  963. }
  964. GPoint3 GLine::operator()(const float t) const
  965. {
  966. return p + v*t;
  967. }
  968. GLine & GLine::SetPt(const GPoint3 & _p)
  969. {
  970. p = _p;
  971. return *this;
  972. }
  973. GLine & GLine::SetDir(const GVector3 & _v)
  974. {
  975. v = _v;
  976. return *this;
  977. }
  978. GVector3 GLine::GetPt() const
  979. {
  980. return p;
  981. }
  982. GVector3 GLine::GetDir() const
  983. {
  984. return v;
  985. }
  986. bool GLine::IsOnLine(const GPoint3 & q)
  987. {
  988. return EQ_ZERO(dist(q, *this), PRECISION);
  989. }
  990. float dist(const GPoint3 & q, const GLine & l)
  991. {
  992. GVector3 u = q - l.p;
  993. GVector3 p = proj(u, l.v);
  994. return norm(u - p);
  995. }
  996. GPlane::GPlane(const GVector3 & _n, const GPoint3 & _p)
  997. {
  998. n = _n;
  999. d = -n * _p;
  1000. }
  1001. GPlane::GPlane(const GPoint3 & p1, const GPoint3 & p2, const GPoint3 & p3)
  1002. {
  1003. n = (p2 - p1) ^ (p3 - p1);
  1004. d = -n * p1; //��ƽ���ϵ�һ�����˷�����ȡ����
  1005. }
  1006. GPlane::GPlane(const float & a, const float & b, const float & c, const float & d)
  1007. {
  1008. n = GVector3(a, b, c);
  1009. this->d = d;
  1010. }
  1011. GPlane::GPlane(const GPlane & copy)
  1012. {
  1013. n = copy.n;
  1014. d = copy.d;
  1015. }
  1016. GPlane& GPlane::operator=(const GPlane & rhs)
  1017. {
  1018. n = rhs.n;
  1019. d = rhs.d;
  1020. return *this;
  1021. }
  1022. GVector3 GPlane::GetNormal() const
  1023. {
  1024. return n;
  1025. }
  1026. float dist(const GPlane & pi, const GPoint3 & p)
  1027. {
  1028. float D;
  1029. D = (p*pi.n + pi.d) / norm(pi.n);
  1030. return D;
  1031. }
  1032. bool intersect_line_plane(GPoint3 & p, const GLine & l, const GPlane & pi)
  1033. {
  1034. //����ֱ�ߵķ�����ƽ���ķ��ߴ�ֱ����ֱ�ߺ�ƽ��ƽ�У�û�н���
  1035. if (EQ_ZERO((l.v*pi.n), PRECISION)) {
  1036. return false;
  1037. }
  1038. float t = -(l.p*pi.n + pi.d) / (l.v * pi.n);
  1039. p = l(t);
  1040. return true;
  1041. }
  1042. bool intersect_line_triangle(GPoint3 & p, const GLine & l, const GPoint3 & p1, const GPoint3 & p2, const GPoint3 & p3)
  1043. {
  1044. GVector3 e1, e2, u, v, w;
  1045. float a, b, t, det;
  1046. e1 = p2 - p1;
  1047. e2 = p3 - p1;
  1048. u = l.v ^ e2;
  1049. det = e1 * u;
  1050. if (EQ_ZERO(det, PRECISION)) {
  1051. return false;
  1052. }
  1053. w = l.p - p1;
  1054. a = w * u / det;
  1055. if (a < 0.0f || a > 1.0f) {
  1056. return false;
  1057. }
  1058. v = w ^ e1;
  1059. b = l.v * v / det;
  1060. if (b < 0.0f || b > 1.0f) {
  1061. return false;
  1062. }
  1063. t = e2 * v / det;
  1064. p = l(t);
  1065. return true;
  1066. }
  1067. GQuater::GQuater(float w, float x, float y, float z)
  1068. {
  1069. this->w = w;
  1070. this->x = x;
  1071. this->y = y;
  1072. this->z = z;
  1073. }
  1074. GQuater::GQuater(const GQuater & copy)
  1075. {
  1076. w = copy.w;
  1077. x = copy.x;
  1078. y = copy.y;
  1079. z = copy.z;
  1080. }
  1081. GQuater::GQuater(GVector3 axis, float theta, bool radian)
  1082. {
  1083. //q=(cosX, sinX(a,b,c))
  1084. float rad, sn, cs;
  1085. axis.normalize();
  1086. if (!radian) {
  1087. rad = theta * M_PI / 360; //��Ϊ��ת����2��X��������������360
  1088. }
  1089. sn = sin(rad);
  1090. cs = cos(rad);
  1091. sn = (abs(sn) < PRECISION) ? 0.0f : sn;
  1092. cs = (abs(cs) < PRECISION) ? 0.0f : cs;
  1093. w = cs;
  1094. x = sn * axis[0];
  1095. y = sn * axis[1];
  1096. z = sn * axis[2];
  1097. }
  1098. GQuater & GQuater::operator=(const GQuater & rhs)
  1099. {
  1100. w = rhs.w;
  1101. x = rhs.x;
  1102. y = rhs.y;
  1103. z = rhs.z;
  1104. return *this;
  1105. }
  1106. GQuater & GQuater::operator+=(const GQuater & rhs)
  1107. {
  1108. w += rhs.w;
  1109. x += rhs.x;
  1110. y += rhs.y;
  1111. z += rhs.z;
  1112. return *this;
  1113. }
  1114. GQuater & GQuater::operator-=(const GQuater & rhs)
  1115. {
  1116. w -= rhs.w;
  1117. x -= rhs.x;
  1118. y -= rhs.y;
  1119. z -= rhs.z;
  1120. return *this;
  1121. }
  1122. GQuater & GQuater::operator*=(const GQuater & rhs)
  1123. {
  1124. float nw, nx, ny, nz = 0;
  1125. nw = w * rhs.w - x*rhs.x - y * rhs.y - z * rhs.z;
  1126. nx = w * rhs.x + rhs.w*x + y * rhs.z - z * rhs.y;
  1127. ny = w * rhs.y + rhs.w*y + z * rhs.x - x * rhs.z;
  1128. nz = w * rhs.z + rhs.w*z + x * rhs.y - y * rhs.x;
  1129. w = nw;
  1130. x = nx;
  1131. y = ny;
  1132. z = nz;
  1133. return *this;
  1134. }
  1135. GQuater & GQuater::operator*=(const float & s)
  1136. {
  1137. w *= s;
  1138. x *= s;
  1139. y *= s;
  1140. z *= s;
  1141. return *this;
  1142. }
  1143. GQuater & GQuater::operator/=(const float & s)
  1144. {
  1145. w /= s;
  1146. x /= s;
  1147. y /= s;
  1148. z /= s;
  1149. return *this;
  1150. }
  1151. GQuater GQuater::operator+() const
  1152. {
  1153. return *this;
  1154. }
  1155. GQuater GQuater::operator-() const
  1156. {
  1157. //return *this*-1.0f;
  1158. return GQuater();
  1159. }
  1160. GQuater GQuater::operator+(const GQuater & rhs) const
  1161. {
  1162. GQuater ret(*this);
  1163. ret += rhs;
  1164. return ret;
  1165. }
  1166. GQuater GQuater::operator-(const GQuater & rhs) const
  1167. {
  1168. GQuater ret(*this);
  1169. ret -= rhs;
  1170. return ret;
  1171. }
  1172. GQuater GQuater::operator*(const GQuater & rhs) const
  1173. {
  1174. GQuater ret(*this);
  1175. ret *= rhs;
  1176. return ret;
  1177. }
  1178. GQuater GQuater::operator*(const float & s) const
  1179. {
  1180. GQuater ret(*this);
  1181. ret *= s;
  1182. return ret;
  1183. }
  1184. GQuater GQuater::operator/(const float & s) const
  1185. {
  1186. GQuater ret(*this);
  1187. ret /= s;
  1188. return ret;
  1189. }
  1190. GQuater & GQuater::Set(const float w, const float x, const float y, const float z)
  1191. {
  1192. this->w = w;
  1193. this->x = x;
  1194. this->y = y;
  1195. this->z = z;
  1196. return *this;
  1197. }
  1198. GQuater & GQuater::Set(float * q, bool invOrder)
  1199. {
  1200. if (invOrder) {
  1201. w = q[1];
  1202. x = q[2];
  1203. y = q[3];
  1204. z = q[0];
  1205. } else {
  1206. w = q[0];
  1207. x = q[1];
  1208. y = q[2];
  1209. z = q[3];
  1210. }
  1211. return *this;
  1212. }
  1213. bool GQuater::IsUnitQuater() const
  1214. {
  1215. float len = SQR(w) + SQR(x) + SQR(y) + SQR(z);
  1216. return EQ(len, 1.0f, PRECISION) ? true : false;
  1217. }
  1218. bool GQuater::IsIdentity() const
  1219. {
  1220. return EQ(w,1.0f,PRECISION) && EQ(x, 0.0f, PRECISION) && EQ(y, 0.0f, PRECISION) && EQ(z, 0.0f, PRECISION);
  1221. }
  1222. GQuater & GQuater::Normalize()
  1223. {
  1224. float len = SQR(w) + SQR(x) + SQR(y) + SQR(z);
  1225. w /= len;
  1226. x /= len;
  1227. y /= len;
  1228. z /= len;
  1229. return *this;
  1230. }
  1231. GQuater & GQuater::SetIdentitf()
  1232. {
  1233. w /= 1.0f;
  1234. x /= 0.0f;
  1235. y /= 0.0f;
  1236. z /= 0.0f;
  1237. return *this;
  1238. }
  1239. GQuater & GQuater::SetConjugate()
  1240. {
  1241. x *= -1.0f;
  1242. y *= -1.0f;
  1243. z *= -1.0f;
  1244. return *this;
  1245. }
  1246. GQuater & GQuater::SetInverse()
  1247. {
  1248. if (!IsUnitQuater()) {//�������ǵ�λ��Ԫ��Ҫ�ȵ�λ��
  1249. float len = SQR(w) + SQR(x) + SQR(y) + SQR(z);
  1250. *this /= len;
  1251. }
  1252. SetConjugate();
  1253. return *this;
  1254. }
  1255. GQuater operator*(const float & s, const GQuater & rhs)
  1256. {
  1257. GQuater ret(rhs);
  1258. ret *= s;
  1259. return ret;
  1260. }
  1261. ostream & operator<<(ostream & os, const GQuater & q)
  1262. {
  1263. os << "(" << setw(2) << q.w << ", " << setw(2) << q.x << ", " << setw(2) << q.y << ", " << setw(2) << q.z << ")";
  1264. return os;
  1265. }
  1266. float norm(const GQuater & q)
  1267. {
  1268. float norm = SQRT(SQR(q.w) + SQR(q.x) + SQR(q.y) + SQR(q.z));
  1269. return norm;
  1270. }
  1271. GQuater inv(const GQuater & q)
  1272. {
  1273. GQuater ret(q);
  1274. if (!ret.IsUnitQuater()) {//�������ǵ�λ��Ԫ��Ҫ�ȵ�λ��
  1275. float t = SQR(ret.w) + SQR(ret.x) + SQR(ret.y) + SQR(ret.z);
  1276. ret /= t;
  1277. }
  1278. ret.Set(ret.w, -ret.x, -ret.y, -ret.z);
  1279. return ret;
  1280. }