source: XIOS/dev/branch_openmp/extern/remap/src/intersection_ym.cpp @ 1220

Last change on this file since 1220 was 1220, checked in by yushan, 4 years ago

test_remap_omp tested on ADA except two fields

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