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

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

Remapper : manage some floating rounding error leading to some NAN

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 ;
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    {
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       if (is->area==0.) delete is ;
147       else
148       {
149         is->id = b->id; /* intersection holds id of corresponding source element (see Elt class definition for details about id) */
150         is->src_id = b->src_id;
151         is->n = nv;
152         (a->is).push_back(is);
153         (b->is).push_back(is);
154         area=is->area ;
155       }
156    }
157    delete[] intersectPolygon ;
158  }
159
160  delete[] a_gno ;
161  delete[] b_gno ;
162  return area ;
163}
164
165void createGreatCirclePolygon(const Elt& element, const Coord& pole, vector<Coord>& coordinates)
166{
167  int nv = element.n;
168
169  double z,r ;
170  int north ;
171  int iterations ;
172
173  Coord xa,xb,xi,xc ;
174  Coord x1,x2,x ;
175
176  for(int i=0;i < nv ;i++)
177  {
178    north = (scalarprod(element.edge[i], pole) < 0) ? -1 : 1;
179    z=north*element.d[i] ;
180
181    if (z != 0.0)
182    {
183
184      xa=element.vertex[i] ;
185      xb=element.vertex[(i+1)%nv] ;
186      iterations=0 ;
187
188// compare max distance (at mid-point) between small circle and great circle
189// if greater the epsilon refine the small circle by dividing it recursively.
190
191      do
192      {
193        xc = pole * z ;
194        r=sqrt(1-z*z) ;
195        xi=(xa+xb)*0.5 ;
196        x1=xc+(xi-xc)*(r/norm(xi-xc)) ;
197        x2= xi*(1./norm(xi)) ;
198        ++iterations;
199        xb=x1 ;
200      } while(norm(x1-x2)/norm(xa-xb)>epsilon) ;
201
202      iterations = 1 << (iterations-1) ;
203
204// small circle divided in "iterations" great circle arc
205      Coord delta=(element.vertex[(i+1)%nv]-element.vertex[i])*(1./iterations);
206      x=xa ;
207      for(int j=0; j<iterations ; j++)
208      {
209        //xc+(x-xc)*r/norm(x-xc)
210        coordinates.push_back(xc+(x-xc)*(r/norm(x-xc))) ;
211        x=x+delta ;
212      }
213    }
214    else coordinates.push_back(element.vertex[i]) ;
215  }
216}
217
218}
Note: See TracBrowser for help on using the repository browser.