source: XIOS/dev/branch_yushan_merged/extern/remap/src/intersection_ym.cpp @ 1155

Last change on this file since 1155 was 1155, checked in by yushan, 7 years ago

test_remap OK with openmp

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