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

Last change on this file since 815 was 688, checked in by mhnguyen, 9 years ago

Integrating remap library into XIOS

+) Change name of some files of remap library to be compatible with XIOS
+) Implement function to fill in automatically boundary longitude and latitude

Test
+) On Curie
+) test_remap correct

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