OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
cgal_interp.cpp
Go to the documentation of this file.
1 #include "cgal_interp.h"
2 
3 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
4 #include <CGAL/Delaunay_triangulation_2.h>
5 
6 #include <CGAL/Interpolation_traits_2.h>
7 #include <CGAL/natural_neighbor_coordinates_2.h>
8 #include <CGAL/interpolation_functions.h>
9 
10 #include <iostream>
11 
12 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
13 typedef CGAL::Delaunay_triangulation_2<K> Delaunay_triangulation;
14 typedef CGAL::Interpolation_traits_2<K> Traits;
15 typedef K::FT Coord_type;
16 typedef K::Point_2 Point;
17 typedef std::map<Point, Coord_type, K::Less_xy_2> Coord_map;
18 typedef CGAL::Data_access<Coord_map> Value_access;
19 
20 typedef std::vector<std::pair<Point, Coord_type> > Point_coordinate_vector;
21 
22 static const int pool_size = 10;
23 static bool cpool[pool_size]={false,false,false,false,false,false,false,false,false,false};
24 static std::vector< std::vector<std::pair<Point, Coord_type> >> cov[pool_size];
25 static Coord_type *nrms[pool_size];
26 
27 int cgal_nnc( int nxy, float *x, float *y, int nxyq, float *xq, float *yq)
28 {
30 
31  int nnc_tag = -1;
32  for (int i=0 ; i<pool_size; i++) {
33  if (cpool[i] == false) {
34  cpool[i] = true;
35  nnc_tag = i;
36  //std::cout << "Assigning NNC tag: " << i << std::endl;
37  break;
38  }
39  }
40  if(nnc_tag == -1) {
41  std::cout << "Error: no NNC tags left\n";
42  return -1;
43  }
44 
45  for (int i=0 ; i<nxy ; i++){
46  K::Point_2 p(x[i],y[i]);
47  T.insert(p);
48  }
49 
50  nrms[nnc_tag] = new Coord_type[nxyq];
51 
52  // Generate interpolated values at xy gridpoints
53  for (int i=0 ; i<nxyq ; i++) {
54  // if (i % 100000 == 0) std::cout << i << std::endl;
55 
56  K::Point_2 p(xq[i], yq[i]);
57  std::vector<std::pair<Point, Coord_type> > coords;
58 
59  CGAL::Triple<std::back_insert_iterator<Point_coordinate_vector>,
60  K::FT, bool> nn_result =
61  CGAL::natural_neighbor_coordinates_2(T, p, std::back_inserter(coords));
62 
63  bool status = nn_result.third;
64  if (status)
65  nrms[nnc_tag][i] = nn_result.second;
66  else
67  nrms[nnc_tag][i] = -1;
68  cov[nnc_tag].push_back(coords);
69  }
70 
71  return nnc_tag;
72 }
73 
74 int cgal_interp2( int nxy, float *x, float *y, float *v, int nxyq, float *vq,
75  int nnc_tag)
76 {
77  Coord_map value_function;
78 
79  if(nnc_tag < 0 || nnc_tag >= pool_size) {
80  std::cout << "Error: bad NNC tag: " << nnc_tag << std::endl;
81  return -1;
82  }
83 
84  for (int i=0 ; i<nxy ; i++){
85  K::Point_2 p(x[i],y[i]);
86  value_function.insert(std::make_pair(p, v[i]));
87  }
88 
89  // Generate interpolated values at xy gridpoints
90  for (int i=0 ; i<nxyq ; i++) {
91  // K::Point_2 p(xq[i], yq[i]);
92  std::vector<std::pair<Point, Coord_type> > coords;
93 
95  coords = cov[nnc_tag][i];
96  norm = nrms[nnc_tag][i];
97 
98  if (coords.size() > 0) {
99  Coord_type res =
100  CGAL::linear_interpolation(coords.begin(), coords.end(), norm,
101  Value_access(value_function));
102  vq[i] = res;
103  } else {
104  vq[i] = -999;
105  }
106  }
107 
108  return EXIT_SUCCESS;
109 }
110 
111 int cgal_release_tag( int nnc_tag) {
112 
113  if(nnc_tag < 0 || nnc_tag >= pool_size) {
114  std::cout << "Error: bad NNC tag: " << nnc_tag << std::endl;
115  return -1;
116  }
117 
118  if (cpool[nnc_tag] == true) {
119  //std::cout << "Releasing NNC tag: " << nnc_tag << std::endl;
120  cpool[nnc_tag] = false;
121  delete[] nrms[nnc_tag];
122  cov[nnc_tag].clear();
123  } else {
124  std::cout << "Improper nnc tag: " << nnc_tag << std::endl;
125  return -1;
126  }
127 
128  return EXIT_SUCCESS;
129 }
int cgal_nnc(int nxy, float *x, float *y, int nxyq, float *xq, float *yq)
Definition: cgal_interp.cpp:27
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
int status
Definition: l1_czcs_hdf.c:32
CGAL::Data_access< Coord_map > Value_access
Definition: cgal_interp.cpp:18
std::map< Point, Coord_type, K::Less_xy_2 > Coord_map
Definition: cgal_interp.cpp:17
int cgal_interp2(int nxy, float *x, float *y, float *v, int nxyq, float *vq, int nnc_tag)
Definition: cgal_interp.cpp:74
CGAL::Exact_predicates_inexact_constructions_kernel K
Definition: cgal_interp.cpp:12
K::Point_2 Point
Definition: cgal_interp.cpp:16
CGAL::Delaunay_triangulation_2< K > Delaunay_triangulation
Definition: cgal_interp.cpp:13
int cgal_release_tag(int nnc_tag)
int i
Definition: decode_rs.h:71
K::FT Coord_type
Definition: cgal_interp.cpp:15
CGAL::Interpolation_traits_2< K > Traits
Definition: cgal_interp.cpp:14
std::vector< std::pair< Point, Coord_type > > Point_coordinate_vector
Definition: cgal_interp.cpp:20
float p[MODELMAX]
Definition: atrem_corl1.h:131