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.

121 lines
3.9 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/Geometry>
  11. #include <Eigen/LU>
  12. #include <Eigen/SVD>
  13. /* this test covers the following files:
  14. Geometry/OrthoMethods.h
  15. */
  16. template<typename Scalar> void orthomethods_3()
  17. {
  18. typedef typename NumTraits<Scalar>::Real RealScalar;
  19. typedef Matrix<Scalar,3,3> Matrix3;
  20. typedef Matrix<Scalar,3,1> Vector3;
  21. typedef Matrix<Scalar,4,1> Vector4;
  22. Vector3 v0 = Vector3::Random(),
  23. v1 = Vector3::Random(),
  24. v2 = Vector3::Random();
  25. // cross product
  26. VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).dot(v1), Scalar(1));
  27. VERIFY_IS_MUCH_SMALLER_THAN(v1.dot(v1.cross(v2)), Scalar(1));
  28. VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).dot(v2), Scalar(1));
  29. VERIFY_IS_MUCH_SMALLER_THAN(v2.dot(v1.cross(v2)), Scalar(1));
  30. Matrix3 mat3;
  31. mat3 << v0.normalized(),
  32. (v0.cross(v1)).normalized(),
  33. (v0.cross(v1).cross(v0)).normalized();
  34. VERIFY(mat3.isUnitary());
  35. // colwise/rowwise cross product
  36. mat3.setRandom();
  37. Vector3 vec3 = Vector3::Random();
  38. Matrix3 mcross;
  39. int i = internal::random<int>(0,2);
  40. mcross = mat3.colwise().cross(vec3);
  41. VERIFY_IS_APPROX(mcross.col(i), mat3.col(i).cross(vec3));
  42. mcross = mat3.rowwise().cross(vec3);
  43. VERIFY_IS_APPROX(mcross.row(i), mat3.row(i).cross(vec3));
  44. // cross3
  45. Vector4 v40 = Vector4::Random(),
  46. v41 = Vector4::Random(),
  47. v42 = Vector4::Random();
  48. v40.w() = v41.w() = v42.w() = 0;
  49. v42.template head<3>() = v40.template head<3>().cross(v41.template head<3>());
  50. VERIFY_IS_APPROX(v40.cross3(v41), v42);
  51. // check mixed product
  52. typedef Matrix<RealScalar, 3, 1> RealVector3;
  53. RealVector3 rv1 = RealVector3::Random();
  54. VERIFY_IS_APPROX(v1.cross(rv1.template cast<Scalar>()), v1.cross(rv1));
  55. VERIFY_IS_APPROX(rv1.template cast<Scalar>().cross(v1), rv1.cross(v1));
  56. }
  57. template<typename Scalar, int Size> void orthomethods(int size=Size)
  58. {
  59. typedef typename NumTraits<Scalar>::Real RealScalar;
  60. typedef Matrix<Scalar,Size,1> VectorType;
  61. typedef Matrix<Scalar,3,Size> Matrix3N;
  62. typedef Matrix<Scalar,Size,3> MatrixN3;
  63. typedef Matrix<Scalar,3,1> Vector3;
  64. VectorType v0 = VectorType::Random(size);
  65. // unitOrthogonal
  66. VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().dot(v0), Scalar(1));
  67. VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), RealScalar(1));
  68. if (size>=3)
  69. {
  70. v0.template head<2>().setZero();
  71. v0.tail(size-2).setRandom();
  72. VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().dot(v0), Scalar(1));
  73. VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), RealScalar(1));
  74. }
  75. // colwise/rowwise cross product
  76. Vector3 vec3 = Vector3::Random();
  77. int i = internal::random<int>(0,size-1);
  78. Matrix3N mat3N(3,size), mcross3N(3,size);
  79. mat3N.setRandom();
  80. mcross3N = mat3N.colwise().cross(vec3);
  81. VERIFY_IS_APPROX(mcross3N.col(i), mat3N.col(i).cross(vec3));
  82. MatrixN3 matN3(size,3), mcrossN3(size,3);
  83. matN3.setRandom();
  84. mcrossN3 = matN3.rowwise().cross(vec3);
  85. VERIFY_IS_APPROX(mcrossN3.row(i), matN3.row(i).cross(vec3));
  86. }
  87. void test_geo_orthomethods()
  88. {
  89. for(int i = 0; i < g_repeat; i++) {
  90. CALL_SUBTEST_1( orthomethods_3<float>() );
  91. CALL_SUBTEST_2( orthomethods_3<double>() );
  92. CALL_SUBTEST_4( orthomethods_3<std::complex<double> >() );
  93. CALL_SUBTEST_1( (orthomethods<float,2>()) );
  94. CALL_SUBTEST_2( (orthomethods<double,2>()) );
  95. CALL_SUBTEST_1( (orthomethods<float,3>()) );
  96. CALL_SUBTEST_2( (orthomethods<double,3>()) );
  97. CALL_SUBTEST_3( (orthomethods<float,7>()) );
  98. CALL_SUBTEST_4( (orthomethods<std::complex<double>,8>()) );
  99. CALL_SUBTEST_5( (orthomethods<float,Dynamic>(36)) );
  100. CALL_SUBTEST_6( (orthomethods<double,Dynamic>(35)) );
  101. }
  102. }