triple.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef __TRIPLE_H__
  2. #define __TRIPLE_H__
  3. #include <cmath>
  4. #include <iostream>
  5. namespace sphereRemap {
  6. #define EPS 1e-15
  7. /* coordinate on sphere */
  8. class Coord
  9. {
  10. public:
  11. Coord() : x(0), y(0), z(0) {};
  12. Coord(double x, double y, double z) : x(x), y(y), z(z) {};
  13. bool operator==(const Coord& rhs) const
  14. {
  15. return x == rhs.x && y == rhs.y && z == rhs.z;
  16. }
  17. void rot(const Coord &u, double theta);
  18. double x, y, z;
  19. };
  20. class Vector
  21. {
  22. public:
  23. Vector(double u = 0, double v = 0, double w = 0) : u(u), v(v), w(w) {};
  24. /* vector from origin to point on sphere */
  25. Vector(const Coord& to) : u(to.x), v(to.y), w(to.z) {};
  26. Vector& operator+=(const Vector& rhs)
  27. {
  28. u += rhs.u;
  29. v += rhs.v;
  30. w += rhs.w;
  31. }
  32. double u, v, w;
  33. };
  34. extern const Coord ORIGIN;
  35. std::ostream& operator<<(std::ostream& os, const Coord& c);
  36. std::ostream& operator<<(std::ostream& os, const Vector& c);
  37. inline double lengthSquared(Vector& vec)
  38. {
  39. return vec.u*vec.u + vec.v*vec.v + vec.w*vec.w;
  40. }
  41. inline Coord operator+(const Coord &lhs, const Coord &rhs)
  42. {
  43. return Coord(lhs.x + rhs.x,
  44. lhs.y + rhs.y,
  45. lhs.z + rhs.z);
  46. }
  47. inline Coord operator-(const Coord &lhs, const Coord &rhs)
  48. {
  49. return Coord(lhs.x - rhs.x,
  50. lhs.y - rhs.y,
  51. lhs.z - rhs.z);
  52. }
  53. /** vector scalar multiplication */
  54. inline Coord operator*(const Coord &lhs, double rhs)
  55. {
  56. return Coord(lhs.x * rhs,
  57. lhs.y * rhs,
  58. lhs.z * rhs);
  59. }
  60. inline double squaredist(const Coord &x, const Coord &y)
  61. {
  62. return (y.x-x.x)*(y.x-x.x)
  63. + (y.y-x.y)*(y.y-x.y)
  64. + (y.z-x.z)*(y.z-x.z);
  65. }
  66. inline double eucldist(const Coord &x, const Coord &y)
  67. {
  68. return sqrt(squaredist(x, y));
  69. }
  70. double norm(const Coord &x);
  71. Coord proj(const Coord &x);
  72. double scalarprod(const Coord &a, const Coord &b);
  73. Coord crossprod(const Coord &a, const Coord &b);
  74. double arcdist(const Coord &x, const Coord &y);
  75. double ds(const Coord &x, const Coord &y);
  76. double dp(const Coord &x, const Coord &y, const Coord &pole);
  77. //double angle(const Vector&, const Vector&)
  78. void lonlat(const Coord &a, double &lon, double &lat);
  79. Coord xyz(double lon, double lat);
  80. /** Computes the midpoint on spherical arc between two points on the sphere */
  81. Coord midpoint(const Coord &a, const Coord &b);
  82. /** Computes the midpoint on *small circle* between two points on the sphere */
  83. Coord midpointSC(const Coord &a, const Coord &b);
  84. void rot(const Coord &u, const double th, Coord &x);
  85. void rotsg(const Coord &a, Coord &b, const Coord &c,
  86. const Coord &d, Coord &x);
  87. double angle(const Coord &a, const Coord &b, const Coord &pole);
  88. // return oriented vector angle in range [-pi..pi], pole must be orthogonal to a and b
  89. double vectAngle(const Coord &a, const Coord &b, const Coord &pole) ;
  90. void print_Coord(Coord &p);
  91. }
  92. #endif