Ticket #97: elt.hpp

File elt.hpp, 3.8 KB (added by ssenesi, 8 years ago)
Line 
1#ifndef __ELT_H__
2#define __ELT_H__
3#include <list>
4#include "triple.hpp"
5
6#define NMAX 10 /**< maximum number of vertices for polygons */
7
8#define NOT_FOUND -1
9
10namespace sphereRemap {
11
12using namespace std;
13
14Coord barycentre(const Coord *x, int n);
15
16/** Two great or small circles (or mixed) have two or zero intersections.
17    The coordinates of the intersections are stored in `pt` and `nb` holds the number of intersections (0 or 2).
18    */
19struct Ipt
20{
21        int nb;
22        Coord pt[2];
23};
24
25struct Sgm // edge
26{
27        Coord n; // normal
28        Coord xt[2]; // endpoints
29        double d; // (see Elt)
30};
31
32struct GloId
33{
34        int rank;
35        int ind; /* local id */
36  long globalId ;
37
38        bool operator<(const GloId& other) const {
39                return (rank == other.rank) ? (ind < other.ind) : (rank < other.rank);
40        }
41};
42
43struct Polyg
44{ 
45        /* Note:  for the target grid elements the id (rank and local id) depends on the order of the target grid elements as read from the nc-file whereas for source grid elements it depends on the SS-tree (i.e. super mesh distribution, not the order in the nc-file) */
46        struct GloId id;
47        struct GloId src_id;
48        int n; /* number of vertices */
49        double area;
50        Coord x; /* barycentre */
51};
52
53struct Elt : Polyg
54{
55        Elt() {}
56        Elt(const double *bounds_lon, const double *bounds_lat, int max_num_vert)
57        {
58                int k = 0;
59                vertex[k++] = xyz(bounds_lon[0], bounds_lat[0]);
60                for (int i = 1; i < max_num_vert; i++)
61                {
62                        vertex[k] = xyz(bounds_lon[i], bounds_lat[i]);
63                        /* netCDF convention: if first vertex repeats element is finished (at least three vertices == triagle) */
64                        if (k >= 3 && squaredist(vertex[k], vertex[0]) < EPS*EPS) 
65                                break;
66                        /* eliminate zero edges: move to next vertex only if it is different */
67                        if (squaredist(vertex[k], vertex[k-1]) > EPS*EPS)
68                                k++;
69                }
70                n = k;
71                x = barycentre(vertex, n);
72        }
73
74        Elt& operator=(const Elt& rhs)
75        {
76                id    = rhs.id;
77                src_id    = rhs.src_id;
78                n    = rhs.n;
79                area = rhs.area;
80                x    = rhs.x;
81                val  = rhs.val;
82                grad = rhs.grad;
83                is   = rhs.is;
84
85                for(int i = 0; i < NMAX; i++)
86                {
87                        neighbour[i] = rhs.neighbour[i];
88                        d[i]         = rhs.d[i];
89                        edge[i]      = rhs.edge[i];
90                        vertex[i]    = rhs.vertex[i];
91                        gradNeigh[i] = rhs.gradNeigh[i];
92                }
93                return *this;
94        }
95
96        void delete_intersections()
97        {
98                for (list<Polyg*>::iterator it = this->is.begin(); it != this->is.end(); it++)
99                {
100                        Polyg* poly = *it;
101                        delete poly;
102                }
103        }
104
105        int neighbour[NMAX];
106        double d[NMAX]; /**< distance of centre of small circle to origin, zero if great circle */
107        double val;     /**< value (sample if src element, interpolated if dest element) */
108        Coord vertex[NMAX];
109        Coord edge[NMAX]; /**< edge normals: if great circle tangential to sphere, if small circle parallel to pole */
110        Coord grad;    /**< gradient of the reconstructed linear function over this element */
111        Coord gradNeigh[NMAX]; /**< for weight computation: gradients for val=1 at individual neighbours */
112        struct GloId neighId[NMAX]; /**< weight computation needs to know global IDs for all sources with "link" */
113        std::list<Polyg*> is; /**< intersections */
114};
115
116static double normals(Elt &elt, const Coord &pole)
117{
118        double nmin = 17.;
119        for (int i = 0; i < elt.n; i++)  // supposed oriented
120        {
121                int j = (i+1) % elt.n;
122                elt.edge[i] = crossprod(elt.vertex[j], elt.vertex[i]); 
123                Coord t = elt.vertex[j] - elt.vertex[i];
124                /* polygonal grid || vertices not on same latitude */
125                if (pole == ORIGIN || fabs(scalarprod(t, pole)) > EPS)  // great circle
126                {
127                        double n = norm(elt.edge[i]);
128                        //assert(n > 0);
129                        if (n < nmin) nmin = n;
130                        elt.edge[i] = proj(elt.edge[i]);
131                        elt.d[i] = 0.0;
132                }
133                else /* lan lot grid && vertices on same latitude => small circle */
134                {
135                        int north = (scalarprod(elt.edge[i], pole) < 0) ? -1 : 1;
136                        elt.edge[i] = pole * north;
137                        elt.d[i] = scalarprod(elt.vertex[i], elt.edge[i]);
138                }
139        }
140        return nmin;
141}
142
143}
144
145#endif