source: XIOS/trunk/extern/remap/src/intersection_ym.cpp @ 845

Last change on this file since 845 was 845, checked in by ymipsl, 8 years ago

Correction of area incoherency between supermesh and target cells due to approximation of small circle by piece of great circle.

YM

File size: 5.9 KB
Line 
1#include "intersection_ym.hpp"
2#include "elt.hpp"
3#include "clipper.hpp"
4#include "gridRemap.hpp"
5#include "triple.hpp"
6#include "polyg.hpp"
7#include <vector>
8#include <stdlib.h>
9#include <limits>
10
11#define epsilon 1e-3  // epsilon distance ratio over side lenght for approximate small circle by great circle
12#define fusion_vertex 1e-13
13
14namespace sphereRemap {
15
16using namespace std;
17using namespace ClipperLib ;
18
19double intersect_ym(Elt *a, Elt *b)
20{
21
22// transform small circle into piece of great circle if necessary
23
24  vector<Coord> srcPolygon ;
25  createGreatCirclePolygon(*b, srcGrid.pole, srcPolygon) ;
26//  b->area=polygonarea(&srcPolygon[0],srcPolygon.size()) ;
27  vector<Coord> dstPolygon ;
28  createGreatCirclePolygon(*a, tgtGrid.pole, dstPolygon) ;
29  a->area=polygonarea(&dstPolygon[0],dstPolygon.size()) ; // just for target
30
31// compute coordinates of the polygons into the gnomonique plane tangent to barycenter C of dst polygon
32// transform system coordinate : Z axis along OC
33  int na=dstPolygon.size() ;
34  Coord *a_gno   = new Coord[na];
35  int nb=srcPolygon.size() ;
36  Coord *b_gno   = new Coord[nb];
37
38  Coord OC=barycentre(a->vertex,a->n) ;
39  Coord Oz=OC ;
40  Coord Ox=crossprod(Coord(0,0,1),Oz) ;
41// choose Ox not too small to avoid rounding error
42  if (norm(Ox)< 0.1) Ox=crossprod(Coord(0,1,0),Oz) ;
43  Ox=Ox*(1./norm(Ox)) ;
44  Coord Oy=crossprod(Oz,Ox) ;
45  double cos_alpha;
46
47  for(int n=0; n<na;n++)
48  {
49    cos_alpha=scalarprod(OC,dstPolygon[n]) ;
50    a_gno[n].x=scalarprod(dstPolygon[n],Ox)/cos_alpha ;
51    a_gno[n].y=scalarprod(dstPolygon[n],Oy)/cos_alpha ;
52    a_gno[n].z=scalarprod(dstPolygon[n],Oz)/cos_alpha ; // must be equal to 1
53  }
54
55  for(int n=0; n<nb;n++)
56  {
57    cos_alpha=scalarprod(OC,srcPolygon[n]) ;
58    b_gno[n].x=scalarprod(srcPolygon[n],Ox)/cos_alpha ;
59    b_gno[n].y=scalarprod(srcPolygon[n],Oy)/cos_alpha ;
60    b_gno[n].z=scalarprod(srcPolygon[n],Oz)/cos_alpha ; // must be equal to 1
61  }
62
63
64
65// Compute intersections using clipper
66// 1) Compute offset and scale factor to rescale polygon
67
68  double xmin, xmax, ymin,ymax ;
69  xmin=xmax=a_gno[0].x ;
70  ymin=ymax=a_gno[0].y ;
71
72  for(int n=0; n<na;n++)
73  {
74    if (a_gno[n].x< xmin) xmin=a_gno[n].x ;
75    else if (a_gno[n].x > xmax) xmax=a_gno[n].x ;
76
77    if (a_gno[n].y< ymin) ymin=a_gno[n].y ;
78    else if (a_gno[n].y > ymax) ymax=a_gno[n].y ;
79  }
80
81  for(int n=0; n<nb;n++)
82  {
83    if (b_gno[n].x< xmin) xmin=b_gno[n].x ;
84    else if (b_gno[n].x > xmax) xmax=b_gno[n].x ;
85
86    if (b_gno[n].y< ymin) ymin=b_gno[n].y ;
87    else if (b_gno[n].y > ymax) ymax=b_gno[n].y ;
88  }
89
90  double xoffset=(xmin+xmax)*0.5 ;
91  double yoffset=(ymin+ymax)*0.5 ;
92  double xscale= 1e-4*0.5*hiRange/(xmax-xoffset) ;
93  double yscale= 1e-4*0.5*hiRange/(ymax-yoffset) ;
94// Problem with numerical precision if using larger scaling factor
95
96// 2) Compute intersection with clipper
97//    clipper use only long integer value for vertex => offset and rescale
98
99  Paths src(1), dst(1), intersection;
100
101  for(int n=0; n<na;n++)
102     src[0]<<IntPoint((a_gno[n].x-xoffset)*xscale,(a_gno[n].y-yoffset)*yscale) ;
103
104  for(int n=0; n<nb;n++)
105     dst[0]<<IntPoint((b_gno[n].x-xoffset)*xscale,(b_gno[n].y-yoffset)*yscale) ;
106
107  Clipper clip ;
108  clip.AddPaths(src, ptSubject, true);
109  clip.AddPaths(dst, ptClip, true);
110  clip.Execute(ctIntersection, intersection);
111
112  double area=0 ;
113
114  for(int ni=0;ni<intersection.size(); ni++)
115  {
116    // go back into real coordinate on the sphere
117    Coord* intersectPolygon=new Coord[intersection[ni].size()] ;
118    for(int n=0; n < intersection[ni].size(); n++)
119    {
120      double x=intersection[ni][n].X/xscale+xoffset ;
121      double y=intersection[ni][n].Y/yscale+yoffset ;
122
123      intersectPolygon[n]=Ox*x+Oy*y+Oz ;
124      intersectPolygon[n]=intersectPolygon[n]*(1./norm(intersectPolygon[n])) ;
125    }
126
127// remove redondants vertex
128    int nv=0 ;
129    for(int n=0; n < intersection[ni].size(); n++)
130    {
131      if (norm(intersectPolygon[n]-intersectPolygon[(n+1)%intersection[ni].size()])>fusion_vertex)
132      {
133        intersectPolygon[nv]=intersectPolygon[n] ;
134        nv++ ;
135      }
136    }
137
138
139    if (nv>2)
140    {
141//     assign intersection to source and destination polygons
142       Polyg *is = new Polyg;
143       is->x = exact_barycentre(intersectPolygon,nv);
144       is->area = polygonarea(intersectPolygon,nv) ;
145//        if (is->area < 1e-12) cout<<"Small intersection : "<<is->area<<endl ;
146       is->id = b->id; /* intersection holds id of corresponding source element (see Elt class definition for details about id) */
147       is->src_id = b->src_id;
148       is->n = nv;
149       (a->is).push_back(is);
150       (b->is).push_back(is);
151       area=is->area ;
152    }
153    delete[] intersectPolygon ;
154  }
155
156  delete[] a_gno ;
157  delete[] b_gno ;
158  return area ;
159}
160
161void createGreatCirclePolygon(const Elt& element, const Coord& pole, vector<Coord>& coordinates)
162{
163  int nv = element.n;
164
165  double z,r ;
166  int north ;
167  int iterations ;
168
169  Coord xa,xb,xi,xc ;
170  Coord x1,x2,x ;
171
172  for(int i=0;i < nv ;i++)
173  {
174    north = (scalarprod(element.edge[i], pole) < 0) ? -1 : 1;
175    z=north*element.d[i] ;
176
177    if (z != 0.0)
178    {
179
180      xa=element.vertex[i] ;
181      xb=element.vertex[(i+1)%nv] ;
182      iterations=0 ;
183
184// compare max distance (at mid-point) between small circle and great circle
185// if greater the epsilon refine the small circle by dividing it recursively.
186
187      do
188      {
189        xc = pole * z ;
190        r=sqrt(1-z*z) ;
191        xi=(xa+xb)*0.5 ;
192        x1=xc+(xi-xc)*(r/norm(xi-xc)) ;
193        x2= xi*(1./norm(xi)) ;
194        ++iterations;
195        xb=x1 ;
196      } while(norm(x1-x2)/norm(xa-xb)>epsilon) ;
197
198      iterations = 1 << (iterations-1) ;
199
200// small circle divided in "iterations" great circle arc
201      Coord delta=(element.vertex[(i+1)%nv]-element.vertex[i])*(1./iterations);
202      x=xa ;
203      for(int j=0; j<iterations ; j++)
204      {
205        //xc+(x-xc)*r/norm(x-xc)
206        coordinates.push_back(xc+(x-xc)*(r/norm(x-xc))) ;
207        x=x+delta ;
208      }
209    }
210    else coordinates.push_back(element.vertex[i]) ;
211  }
212}
213
214}
Note: See TracBrowser for help on using the repository browser.