source: XIOS/trunk/extern/remap/src/triple.hpp @ 2088

Last change on this file since 2088 was 2088, checked in by ymipsl, 3 years ago

bug fix in remapper
YM

File size: 2.8 KB
Line 
1#ifndef  __TRIPLE_H__
2#define __TRIPLE_H__
3
4#include <cmath>
5#include <iostream>
6
7namespace sphereRemap {
8
9#define EPS 1e-15
10
11/* coordinate on sphere */
12class Coord
13{
14public:
15        Coord() : x(0), y(0), z(0) {};
16        Coord(double x, double y, double z) : x(x), y(y), z(z) {};
17
18        bool operator==(const Coord& rhs) const
19        {
20                return x == rhs.x && y == rhs.y && z == rhs.z;
21        }
22
23        void rot(const Coord &u, double theta);
24
25        double x, y, z;
26};
27
28class Vector
29{
30public:
31        Vector(double u = 0, double v = 0, double w = 0) : u(u), v(v), w(w) {};
32
33        /* vector from origin to point on sphere */
34        Vector(const Coord& to) : u(to.x), v(to.y), w(to.z) {};
35
36        Vector& operator+=(const Vector& rhs)
37        {
38           u += rhs.u;
39           v += rhs.v;
40           w += rhs.w;
41           return *this ;
42        }
43
44        double u, v, w;
45};
46
47extern const Coord ORIGIN;
48
49std::ostream& operator<<(std::ostream& os, const Coord& c);
50std::ostream& operator<<(std::ostream& os, const Vector& c);
51
52inline double lengthSquared(Vector& vec)
53{
54        return vec.u*vec.u + vec.v*vec.v + vec.w*vec.w;
55}
56       
57inline Coord operator+(const Coord &lhs, const Coord &rhs)
58{
59        return Coord(lhs.x + rhs.x, 
60                     lhs.y + rhs.y, 
61                     lhs.z + rhs.z);
62}
63
64inline Coord operator-(const Coord &lhs, const Coord &rhs)
65{
66        return Coord(lhs.x - rhs.x, 
67                     lhs.y - rhs.y, 
68                     lhs.z - rhs.z);
69}
70
71/** vector scalar multiplication */
72inline Coord operator*(const Coord &lhs, double rhs)
73{
74        return Coord(lhs.x * rhs, 
75                     lhs.y * rhs, 
76                     lhs.z * rhs);
77}
78
79inline double squaredist(const Coord &x, const Coord &y)
80{
81        return (y.x-x.x)*(y.x-x.x)
82             + (y.y-x.y)*(y.y-x.y) 
83             + (y.z-x.z)*(y.z-x.z);
84}
85
86inline double eucldist(const Coord &x, const Coord &y)
87{
88        return sqrt(squaredist(x, y));
89}
90
91
92double norm(const Coord &x);
93
94Coord proj(const Coord &x);
95
96double scalarprod(const Coord &a, const Coord &b);
97
98Coord crossprod(const Coord &a, const Coord &b);
99
100double arcdist(const Coord &x, const Coord &y);
101
102double ds(const Coord &x, const Coord &y);
103
104double dp(const Coord &x, const Coord &y, const Coord &pole);
105
106//double angle(const Vector&, const Vector&)
107
108void lonlat(const Coord &a, double &lon, double &lat);
109
110Coord xyz(double lon, double lat);
111
112/** Computes the midpoint on spherical arc between two points on the sphere */
113Coord midpoint(const Coord &a, const Coord &b);
114
115/** Computes the midpoint on *small circle* between two points on the sphere */
116Coord midpointSC(const Coord &a, const Coord &b);
117
118void rot(const Coord &u, const double th, Coord &x);
119
120void rotsg(const Coord &a, Coord &b, const Coord &c,
121           const Coord &d, Coord &x);
122
123double angle(const Coord &a, const Coord &b, const Coord &pole);
124
125// return oriented vector angle in range [-pi..pi], pole must be orthogonal to a and b
126double vectAngle(const Coord &a, const Coord &b, const Coord &pole) ;
127
128void print_Coord(Coord &p);
129
130}
131
132#endif
Note: See TracBrowser for help on using the repository browser.