source: XIOS/dev/dev_trunk_omp/extern/remap/src/intersection_ym.cpp @ 1602

Last change on this file since 1602 was 1602, checked in by yushan, 5 years ago

branch_openmp merged with trunk r1597

File size: 8.8 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#include <array>
11#include <cstdint>
12#include "earcut.hpp"
13#include <fstream>
14
15
16#define epsilon 1e-3  // epsilon distance ratio over side lenght for approximate small circle by great circle
17#define fusion_vertex 1e-13
18
19namespace sphereRemap {
20
21extern CRemapGrid srcGrid;
22#pragma omp threadprivate(srcGrid)
23
24extern CRemapGrid tgtGrid;
25#pragma omp threadprivate(tgtGrid)
26
27
28using namespace std;
29using namespace ClipperLib ;
30       
31
32double intersect_ym(Elt *a, Elt *b)
33{
34
35  using N = uint32_t;
36  using Point = array<double, 2>;
37  vector<Point> vect_points;
38  vector< vector<Point> > polyline;
39
40// transform small circle into piece of great circle if necessary
41
42  vector<Coord> srcPolygon ;
43  createGreatCirclePolygon(*b, srcGrid.pole, srcPolygon) ;
44//  b->area=polygonarea(&srcPolygon[0],srcPolygon.size()) ;
45  vector<Coord> dstPolygon ;
46  createGreatCirclePolygon(*a, tgtGrid.pole, dstPolygon) ;
47  a->area=polygonarea(&dstPolygon[0],dstPolygon.size()) ; // just for target
48
49// compute coordinates of the polygons into the gnomonique plane tangent to barycenter C of dst polygon
50// transform system coordinate : Z axis along OC
51  int na=dstPolygon.size() ;
52  Coord *a_gno   = new Coord[na];
53  int nb=srcPolygon.size() ;
54  Coord *b_gno   = new Coord[nb];
55
56  Coord OC=barycentre(a->vertex,a->n) ;
57  Coord Oz=OC ;
58  Coord Ox=crossprod(Coord(0,0,1),Oz) ;
59// choose Ox not too small to avoid rounding error
60  if (norm(Ox)< 0.1) Ox=crossprod(Coord(0,1,0),Oz) ;
61  Ox=Ox*(1./norm(Ox)) ;
62  Coord Oy=crossprod(Oz,Ox) ;
63  double cos_alpha;
64
65  /// vector<p2t::Point*> polyline;
66  for(int n=0; n<na;n++)
67  {
68    cos_alpha=scalarprod(OC,dstPolygon[n]) ;
69    a_gno[n].x=scalarprod(dstPolygon[n],Ox)/cos_alpha ;
70    a_gno[n].y=scalarprod(dstPolygon[n],Oy)/cos_alpha ;
71    a_gno[n].z=scalarprod(dstPolygon[n],Oz)/cos_alpha ; // must be equal to 1
72
73    vect_points.push_back( array<double, 2>() );
74    vect_points[n][0] = a_gno[n].x;
75    vect_points[n][1] = a_gno[n].y;
76
77  }
78
79  polyline.push_back(vect_points);
80  vector<N> indices_a_gno = mapbox::earcut<N>(polyline);
81 
82  double area_a_gno=0 ;
83  for(int i=0;i<indices_a_gno.size()/3;++i)
84    {
85      Coord x0 = Ox * polyline[0][indices_a_gno[3*i]][0] + Oy* polyline[0][indices_a_gno[3*i]][1] + Oz ;
86      Coord x1 = Ox * polyline[0][indices_a_gno[3*i+1]][0] + Oy* polyline[0][indices_a_gno[3*i+1]][1] + Oz ;
87      Coord x2 = Ox * polyline[0][indices_a_gno[3*i+2]][0] + Oy* polyline[0][indices_a_gno[3*i+2]][1] + Oz ;
88      area_a_gno+=triarea(x0 * (1./norm(x0)),x1* (1./norm(x1)), x2* (1./norm(x2))) ;
89    }
90
91  vect_points.clear();
92  polyline.clear();
93  indices_a_gno.clear();
94
95 
96
97  for(int n=0; n<nb;n++)
98  {
99    cos_alpha=scalarprod(OC,srcPolygon[n]) ;
100    b_gno[n].x=scalarprod(srcPolygon[n],Ox)/cos_alpha ;
101    b_gno[n].y=scalarprod(srcPolygon[n],Oy)/cos_alpha ;
102    b_gno[n].z=scalarprod(srcPolygon[n],Oz)/cos_alpha ; // must be equal to 1
103
104    vect_points.push_back( array<double, 2>() );
105    vect_points[n][0] = b_gno[n].x;
106    vect_points[n][1] = b_gno[n].y;
107  }
108
109
110  polyline.push_back(vect_points);
111  vector<N> indices_b_gno = mapbox::earcut<N>(polyline);
112
113  double area_b_gno=0 ;
114  for(int i=0;i<indices_b_gno.size()/3;++i)
115    {
116      Coord x0 = Ox * polyline[0][indices_b_gno[3*i]][0] + Oy* polyline[0][indices_b_gno[3*i]][1] + Oz ;
117      Coord x1 = Ox * polyline[0][indices_b_gno[3*i+1]][0] + Oy* polyline[0][indices_b_gno[3*i+1]][1] + Oz ;
118      Coord x2 = Ox * polyline[0][indices_b_gno[3*i+2]][0] + Oy* polyline[0][indices_b_gno[3*i+2]][1] + Oz ;
119      area_b_gno+=triarea(x0 * (1./norm(x0)),x1* (1./norm(x1)), x2* (1./norm(x2))) ;
120    }
121
122  vect_points.clear();
123  polyline.clear();
124  indices_b_gno.clear();
125
126
127// Compute intersections using clipper
128// 1) Compute offset and scale factor to rescale polygon
129
130  double xmin, xmax, ymin,ymax ;
131  xmin=xmax=a_gno[0].x ;
132  ymin=ymax=a_gno[0].y ;
133
134  for(int n=0; n<na;n++)
135  {
136    if (a_gno[n].x< xmin) xmin=a_gno[n].x ;
137    else if (a_gno[n].x > xmax) xmax=a_gno[n].x ;
138
139    if (a_gno[n].y< ymin) ymin=a_gno[n].y ;
140    else if (a_gno[n].y > ymax) ymax=a_gno[n].y ;
141  }
142
143  for(int n=0; n<nb;n++)
144  {
145    if (b_gno[n].x< xmin) xmin=b_gno[n].x ;
146    else if (b_gno[n].x > xmax) xmax=b_gno[n].x ;
147
148    if (b_gno[n].y< ymin) ymin=b_gno[n].y ;
149    else if (b_gno[n].y > ymax) ymax=b_gno[n].y ;
150  }
151
152  double xoffset=(xmin+xmax)*0.5 ;
153  double yoffset=(ymin+ymax)*0.5 ;
154  double xscale= 1e-4*0.5*hiRange/(xmax-xoffset) ;
155  double yscale= 1e-4*0.5*hiRange/(ymax-yoffset) ;
156// Problem with numerical precision if using larger scaling factor
157
158// 2) Compute intersection with clipper
159//    clipper use only long integer value for vertex => offset and rescale
160
161  Paths src(1), dst(1), intersection;
162
163  for(int n=0; n<na;n++)
164     src[0]<<IntPoint((a_gno[n].x-xoffset)*xscale,(a_gno[n].y-yoffset)*yscale) ;
165
166  for(int n=0; n<nb;n++)
167     dst[0]<<IntPoint((b_gno[n].x-xoffset)*xscale,(b_gno[n].y-yoffset)*yscale) ;
168
169  Clipper clip ;
170  clip.AddPaths(src, ptSubject, true);
171  clip.AddPaths(dst, ptClip, true);
172  clip.Execute(ctIntersection, intersection);
173 
174  double area=0 ;
175
176  for(int ni=0;ni<intersection.size(); ni++)
177  {
178    Coord* intersectPolygon=new Coord[intersection[ni].size()] ;
179    for(int n=0; n < intersection[ni].size(); n++)
180    {
181      intersectPolygon[n].x=intersection[ni][n].X/xscale+xoffset ;
182      intersectPolygon[n].y=intersection[ni][n].Y/yscale+yoffset ;
183    }
184   
185
186    int nv=0;
187
188    for(int n=0; n < intersection[ni].size(); n++)
189    {
190       double dx=intersectPolygon[n].x-intersectPolygon[(n+1)%intersection[ni].size()].x ;
191       double dy=intersectPolygon[n].y-intersectPolygon[(n+1)%intersection[ni].size()].y ;
192     
193       if (dx*dx+dy*dy>fusion_vertex*fusion_vertex)
194       {
195          intersectPolygon[nv]=intersectPolygon[n] ;
196
197          vect_points.push_back( array<double, 2>() );
198          vect_points[nv][0] = intersectPolygon[n].x;
199          vect_points[nv][1] = intersectPolygon[n].y;
200
201          nv++ ;
202       }
203     
204
205    }
206
207    polyline.push_back(vect_points);
208    vect_points.clear();
209
210    if (nv>2)
211    {
212 
213      vector<N> indices = mapbox::earcut<N>(polyline);
214
215      double area2=0 ;
216      for(int i=0;i<indices.size()/3;++i)
217      {
218          Coord x0 = Ox * polyline[0][indices[3*i]][0] + Oy* polyline[0][indices[3*i]][1] + Oz ;
219          Coord x1 = Ox * polyline[0][indices[3*i+1]][0] + Oy* polyline[0][indices[3*i+1]][1] + Oz ;
220          Coord x2 = Ox * polyline[0][indices[3*i+2]][0] + Oy* polyline[0][indices[3*i+2]][1] + Oz ;
221          area2+=triarea(x0 * (1./norm(x0)),x1* (1./norm(x1)), x2* (1./norm(x2))) ;
222      }
223
224      polyline.clear();
225
226      for(int n=0; n < nv; n++)
227      {
228        intersectPolygon[n] = Ox*intersectPolygon[n].x+Oy*intersectPolygon[n].y+Oz;
229        intersectPolygon[n] = intersectPolygon[n]*(1./norm(intersectPolygon[n])) ;
230      }
231
232
233//     assign intersection to source and destination polygons
234       Polyg *is = new Polyg;
235       is->x = exact_barycentre(intersectPolygon,nv);
236//       is->area = polygonarea(intersectPolygon,nv) ;
237       is->area = area2 ;
238
239//        if (is->area < 1e-12) cout<<"Small intersection : "<<is->area<<endl ;
240       if (is->area==0.) delete is ;
241       else
242       { 
243         is->id = b->id; /* intersection holds id of corresponding source element (see Elt class definition for details about id) */
244         is->src_id = b->src_id;
245         is->n = nv;
246         (a->is).push_back(is);
247         (b->is).push_back(is);
248         area=is->area ;
249       }
250    }
251    delete[] intersectPolygon ;
252  }
253
254  delete[] a_gno ;
255  delete[] b_gno ;
256  return area ;
257
258}
259
260
261
262void createGreatCirclePolygon(const Elt& element, const Coord& pole, vector<Coord>& coordinates)
263{
264  int nv = element.n;
265
266  double z,r ;
267  int north ;
268  int iterations ;
269
270  Coord xa,xb,xi,xc ;
271  Coord x1,x2,x ;
272
273  for(int i=0;i < nv ;i++)
274  {
275    north = (scalarprod(element.edge[i], pole) < 0) ? -1 : 1;
276    z=north*element.d[i] ;
277
278    if (z != 0.0)
279    {
280
281      xa=element.vertex[i] ;
282      xb=element.vertex[(i+1)%nv] ;
283      iterations=0 ;
284
285// compare max distance (at mid-point) between small circle and great circle
286// if greater the epsilon refine the small circle by dividing it recursively.
287
288      do
289      {
290        xc = pole * z ;
291        r=sqrt(1-z*z) ;
292        xi=(xa+xb)*0.5 ;
293        x1=xc+(xi-xc)*(r/norm(xi-xc)) ;
294        x2= xi*(1./norm(xi)) ;
295        ++iterations;
296        xb=x1 ;
297      } while(norm(x1-x2)/norm(xa-xb)>epsilon) ;
298
299      iterations = 1 << (iterations-1) ;
300
301// small circle divided in "iterations" great circle arc
302      Coord delta=(element.vertex[(i+1)%nv]-element.vertex[i])*(1./iterations);
303      x=xa ;
304      for(int j=0; j<iterations ; j++)
305      {
306        //xc+(x-xc)*r/norm(x-xc)
307        coordinates.push_back(xc+(x-xc)*(r/norm(x-xc))) ;
308        x=x+delta ;
309      }
310    }
311    else coordinates.push_back(element.vertex[i]) ;
312  }
313}
314
315}
Note: See TracBrowser for help on using the repository browser.