int main() { Delaunay_triangulation dt; for(int y=0; y<3; ++y) for(int x=0; x<3; ++x) dt.insert(K::Point_2(x, y)); // coordinates computation K::Point_2 p(1.2, 0.7); // query point Point_coordinate_vector coords; CGAL::Triple<std::back_insert_iterator<Point_coordinate_vector>, K::FT, bool> result = CGAL::natural_neighbor_coordinates_2(dt, p, std::back_inserter(coords)); if(!result.third) { std::cout << "The coordinate computation was not successful." << std::endl; std::cout << "The point (" << p << ") lies outside the convex hull." << std::endl; } K::FT norm = result.second; std::cout << "Coordinate computation successful." << std::endl; std::cout << "Normalization factor: " << norm << std::endl; std::cout << "Coordinates for point: (" << p << ") are the following: " << std::endl; for(std::size_t i=0; i<coords.size(); ++i) std::cout << " Point: (" << coords[i].first << ") coeff: " << coords[i].second << std::endl; return EXIT_SUCCESS; }
int main() { Delaunay_triangulation T; Point_value_map value_function; Point_vector_map gradient_function; //parameters for spherical function: Coord_type a(0.25), bx(1.3), by(-0.7), c(0.2); for (int y=0; y<4; y++) { for (int x=0; x<4; x++) { K::Point_2 p(x,y); T.insert(p); value_function.insert(std::make_pair(p,a + bx* x+ by*y + c*(x*x+y*y))); } } sibson_gradient_fitting_nn_2(T, std::inserter(gradient_function, gradient_function.begin()), CGAL:: Data_access<Point_value_map>(value_function), Traits()); for(Point_vector_map::iterator it = gradient_function.begin(); it != gradient_function.end(); ++it) { std::cout << it->first << " " << it->second << std::endl; } // coordinate computation K::Point_2 p(1.6, 1.4); std::vector< std::pair< Point, Coord_type > > coords; Coord_type norm = CGAL::natural_neighbor_coordinates_2(T, p, std::back_inserter(coords)).second; //Sibson interpolant: version without sqrt: std::pair<Coord_type, bool> res = CGAL::sibson_c1_interpolation_square(coords.begin(), coords.end(), norm, p, CGAL::Data_access<Point_value_map>(value_function), CGAL::Data_access<Point_vector_map>(gradient_function), Traits()); if(res.second) std::cout << "Tested interpolation on " << p << " interpolation: " << res.first << " exact: " << a + bx*p.x() + by*p.y() + c*(p.x()*p.x()+p.y()*p.y()) << std::endl; else std::cout << "C^1 Interpolation not successful." << std::endl << " not all gradients are provided." << std::endl << " You may resort to linear interpolation." << std::endl; return EXIT_SUCCESS; }
int main() { Delaunay_triangulation T; std::map<Point, Coord_type, K::Less_xy_2> function_values; typedef CGAL::Data_access< std::map<Point, Coord_type, K::Less_xy_2 > > Value_access; Coord_type a(0.25), bx(1.3), by(-0.7); for (int y=0 ; y<3 ; y++) for (int x=0 ; x<3 ; x++){ K::Point_2 p(x,y); T.insert(p); function_values.insert(std::make_pair(p,a + bx* x+ by*y)); } //coordinate computation K::Point_2 p(1.3,0.34); std::vector< std::pair< Point, Coord_type > > coords; Coord_type norm = CGAL::natural_neighbor_coordinates_2 (T, p,std::back_inserter(coords)).second; Coord_type res = CGAL::linear_interpolation(coords.begin(), coords.end(), norm, Value_access(function_values)); std::cout << " Tested interpolation on " << p << " interpolation: " << res << " exact: " << a + bx* p.x()+ by* p.y()<< std::endl; std::cout << "done" << std::endl; ////////////////////////////////////////////////////////////////////// // own version return 0; }
tphysvar goft(const tphysvar logT, const tphysvar logrho, const cube gofttab) { // uses the log(T), because the G(T) is also stored using those values. typedef CGAL::Delaunay_triangulation_2<K> Delaunay_triangulation; typedef CGAL::Interpolation_traits_2<K> Traits; typedef K::FT Coord_type; typedef K::Point_2 Point; std::map<Point, Coord_type, K::Less_xy_2> function_values; typedef CGAL::Data_access< std::map<Point, Coord_type, K::Less_xy_2 > > Value_access; tgrid grid=gofttab.readgrid(); tphysvar tempgrid=grid[0]; tphysvar rhogrid=grid[1]; tphysvar goftvec=gofttab.readvar(0); // put the temperature and density grid from the gofttab into the CGAL data structures vector<Point> delaunaygrid; Point temporarygridpoint; tphysvar::const_iterator tempit=tempgrid.begin(); tphysvar::const_iterator rhoit=rhogrid.begin(); tphysvar::const_iterator goftit=goftvec.begin(); for (; tempit != tempgrid.end(); ++tempit) { temporarygridpoint=Point(*tempit,*rhoit); delaunaygrid.push_back(temporarygridpoint); function_values.insert(std::make_pair(temporarygridpoint,Coord_type(*goftit))); ++rhoit; ++goftit; } Value_access tempmap=Value_access(function_values); // compute the Delaunay triangulation Delaunay_triangulation DT; DT.insert(delaunaygrid.begin(),delaunaygrid.end()); tphysvar g; int ng=logT.size(); // the reservation of the size is necessary, otherwise the vector reallocates in the openmp threads with segfaults as consequence g.resize(ng); cout << "Doing G(T) interpolation: " << flush; // If MPI is available, we should divide the work between nodes, we can just use a geometric division by the commsize int commrank,commsize; #ifdef HAVEMPI MPI_Comm_rank(MPI_COMM_WORLD,&commrank); MPI_Comm_size(MPI_COMM_WORLD,&commsize); #else commrank = 0; commsize = 1; #endif int mpimin, mpimax; // MPE_Decomp1d decomposes a vector into more or less equal parts // The only restriction is that it takes the vector from 1:ng, rather than 0:ng-1 // Therefore, the mpimin and mpimax are decreases afterwards. MPE_Decomp1d(ng,commsize, commrank, &mpimin, &mpimax); mpimin--; mpimax--; boost::progress_display show_progress((mpimax-mpimin+1)/10); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) #endif for (int i=mpimin; i<mpimax; i++) { Point p(logT[i],logrho[i]); // a possible linear interpolation // see http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Interpolation/Chapter_main.html#Subsection_70.3.3 // make sure to use small interpolation file. If not, this takes hours! std::vector< std::pair< Point, Coord_type > > coords; Coord_type norm = CGAL::natural_neighbor_coordinates_2(DT, p,std::back_inserter(coords)).second; Coord_type res = CGAL::linear_interpolation(coords.begin(), coords.end(), norm, Value_access(function_values)); g[i]=res; // let's not do nearest neighbour // it is much faster than the linear interpolation // but the spacing in temperature is too broad too handle this /*Delaunay_triangulation::Vertex_handle v=DT.nearest_vertex(p); Point nearest=v->point(); std::pair<Coord_type,bool> funcval=tempmap(nearest); g[i]=funcval.first;*/ // This introduces a race condition between threads, but since it's only a counter, it doesn't really matter. if ((i-mpimin)%10 == 0) ++show_progress; } cout << " Done!" << endl << flush; return g; }
void clear() { data.clear(); T.clear(); }
float interpolNearest(const Point &p) { return data[T.nearest_vertex(p)->point()]; }