spatialetl-providers-common-interpcgal 0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spatialetl_providers_common_interpcgal-0.1/CMakeLists.txt +50 -0
- spatialetl_providers_common_interpcgal-0.1/PKG-INFO +6 -0
- spatialetl_providers_common_interpcgal-0.1/pyproject.toml +44 -0
- spatialetl_providers_common_interpcgal-0.1/setup.py +23 -0
- spatialetl_providers_common_interpcgal-0.1/src/nninterpol.cpp +92 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.10.2)
|
|
2
|
+
project(interpcgal)
|
|
3
|
+
|
|
4
|
+
include(CheckIncludeFileCXX)
|
|
5
|
+
check_include_file_cxx("cstdint" HAVE_CSTDINT)
|
|
6
|
+
if(NOT HAVE_CSTDINT)
|
|
7
|
+
message(FATAL_ERROR "C++ compiler must support <cstdint>")
|
|
8
|
+
endif()
|
|
9
|
+
|
|
10
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
11
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
12
|
+
|
|
13
|
+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
|
14
|
+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
|
|
15
|
+
set(CMAKE_BUILD_TYPE Release)
|
|
16
|
+
|
|
17
|
+
# Try to import all Python components potentially needed by nanobind
|
|
18
|
+
find_package(Python 3.9
|
|
19
|
+
REQUIRED COMPONENTS Interpreter Development.Module
|
|
20
|
+
OPTIONAL_COMPONENTS Development.SABIModule)
|
|
21
|
+
# Import nanobind through CMake's find_package mechanism
|
|
22
|
+
find_package(nanobind CONFIG REQUIRED)
|
|
23
|
+
|
|
24
|
+
set(SOURCE_DIR "src/")
|
|
25
|
+
include_directories(${SOURCE_DIR})
|
|
26
|
+
|
|
27
|
+
# We are now ready to compile the actual extension module
|
|
28
|
+
nanobind_add_module(
|
|
29
|
+
# Name of the extension
|
|
30
|
+
nninterpol
|
|
31
|
+
|
|
32
|
+
# Target the stable ABI for Python 3.12+, which reduces
|
|
33
|
+
# the number of binary wheels that must be built. This
|
|
34
|
+
# does nothing on older Python versions
|
|
35
|
+
STABLE_ABI
|
|
36
|
+
|
|
37
|
+
# Source code goes here
|
|
38
|
+
${SOURCE_DIR}/nninterpol.cpp
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
# Install directive for scikit-build-core
|
|
42
|
+
install(TARGETS nninterpol LIBRARY DESTINATION spatialetl/operator/interpolator/interpcgal)
|
|
43
|
+
|
|
44
|
+
find_package(CGAL CONFIG REQUIRED)
|
|
45
|
+
target_link_libraries(nninterpol PRIVATE CGAL::CGAL)
|
|
46
|
+
|
|
47
|
+
target_compile_definitions(nninterpol PRIVATE
|
|
48
|
+
-D__STDC_LIMIT_MACROS
|
|
49
|
+
-D__STDC_CONSTANT_MACROS
|
|
50
|
+
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
# Copyright (c) 2024 [SNALE - French SAS Company - RCS 951 724 616]
|
|
3
|
+
#
|
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
# furnished to do so, subject to the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
# copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
# SOFTWARE.
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["scikit-build-core >=0.4.3", "nanobind >=1.3.2"]
|
|
23
|
+
build-backend = "scikit_build_core.build"
|
|
24
|
+
|
|
25
|
+
[project]
|
|
26
|
+
name = "spatialetl-providers-common-interpcgal"
|
|
27
|
+
version = "0.1"
|
|
28
|
+
description = "Natural Neighbor Interpolation using CGAL"
|
|
29
|
+
authors = [
|
|
30
|
+
{name = "Fabien RÉTIF", email = "fabien.retif@snale.net"},
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[tool.scikit-build]
|
|
34
|
+
# Protect the configuration against future changes in scikit-build-core
|
|
35
|
+
minimum-version = "0.4"
|
|
36
|
+
# Setuptools-style build caching in a local directory
|
|
37
|
+
build-dir = "build/{wheel_tag}"
|
|
38
|
+
# Build stable ABI wheels for CPython 3.12+
|
|
39
|
+
wheel.py-api = "cp312"
|
|
40
|
+
|
|
41
|
+
[tool.uv.sources]
|
|
42
|
+
# These names must match the names as defined in the pyproject.toml of the workspace items,
|
|
43
|
+
# *not* the workspace folder paths
|
|
44
|
+
spatialetl = {workspace = true}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
# Copyright (c) 2024 [SNALE - French SAS Company - RCS 951 724 616]
|
|
3
|
+
#
|
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
# furnished to do so, subject to the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
# copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
# SOFTWARE.
|
|
21
|
+
from setuptools import setup
|
|
22
|
+
|
|
23
|
+
setup()
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Natural Neighbor Interpolation following the CGAL User Manual:
|
|
2
|
+
// https://doc.cgal.org/latest/Interpolation/index.html
|
|
3
|
+
|
|
4
|
+
#include <nanobind/nanobind.h>
|
|
5
|
+
#include <nanobind/stl/string.h>
|
|
6
|
+
#include <nanobind/ndarray.h>
|
|
7
|
+
#include <nanobind/stl/vector.h>
|
|
8
|
+
|
|
9
|
+
namespace nb = nanobind;
|
|
10
|
+
|
|
11
|
+
#include <cstdint>
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
#include <ctime>
|
|
14
|
+
|
|
15
|
+
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
|
16
|
+
#include <CGAL/Delaunay_triangulation_2.h>
|
|
17
|
+
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
|
|
18
|
+
#include <CGAL/natural_neighbor_coordinates_2.h>
|
|
19
|
+
#include <CGAL/interpolation_functions.h>
|
|
20
|
+
|
|
21
|
+
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
|
|
22
|
+
typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned, K> Vb;
|
|
23
|
+
typedef CGAL::Triangulation_data_structure_2<Vb> Tds;
|
|
24
|
+
typedef CGAL::Delaunay_triangulation_2<K, Tds> Delaunay_triangulation;
|
|
25
|
+
typedef Delaunay_triangulation::Vertex_handle Vertex_handle;
|
|
26
|
+
typedef K::FT Coord_type;
|
|
27
|
+
typedef K::Point_2 Point;
|
|
28
|
+
typedef std::vector< std::pair<Point, Coord_type> > Coordinate_vector;
|
|
29
|
+
typedef std::map<Point, Coord_type, K::Less_xy_2> Point_value_map;
|
|
30
|
+
typedef std::vector<std::vector<double>> CVdouble2d;
|
|
31
|
+
using Vdouble = nb::ndarray<double, nb::ndim<1>, nb::device::cpu>;
|
|
32
|
+
using Vdouble2d = nb::ndarray<double, nb::ndim<2>, nb::device::cpu>;
|
|
33
|
+
|
|
34
|
+
template<typename T>
|
|
35
|
+
std::vector<std::vector<T>> make_2d_vector(std::size_t rows, std::size_t cols)
|
|
36
|
+
{
|
|
37
|
+
return std::vector<std::vector<T>>(rows, std::vector<T>(cols));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
Delaunay_triangulation triangulate(Vdouble2d candidates) {
|
|
41
|
+
|
|
42
|
+
int ntrainpoints = candidates.shape(0);
|
|
43
|
+
std::vector< std::pair<Point,unsigned> > points;
|
|
44
|
+
points.reserve(ntrainpoints);
|
|
45
|
+
|
|
46
|
+
Delaunay_triangulation T;
|
|
47
|
+
for(int i=0; i<ntrainpoints ; i++){
|
|
48
|
+
Point p(candidates(i,0), candidates(i,1));
|
|
49
|
+
points.push_back(std::make_pair(p,i));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
T.insert( points.begin(),points.end() );
|
|
53
|
+
return T;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
CVdouble2d nninterpol(Delaunay_triangulation T, Vdouble z, Vdouble2d xx, Vdouble2d yy, double fill_value) {
|
|
57
|
+
int dst_x_size = xx.shape(0);
|
|
58
|
+
int dst_y_size = xx.shape(1);
|
|
59
|
+
CVdouble2d raster = make_2d_vector<double>(dst_x_size, dst_y_size);
|
|
60
|
+
Point_value_map values;
|
|
61
|
+
|
|
62
|
+
for (Delaunay_triangulation::Vertex_handle v : T.finite_vertex_handles()) {
|
|
63
|
+
values.insert(std::make_pair(v->point(), z(v->info())));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Delaunay_triangulation::Face_handle fh;
|
|
67
|
+
for(int i=0; i<dst_x_size; i++) {
|
|
68
|
+
for(int j=0; j<dst_y_size; j++) {
|
|
69
|
+
Point p(xx(i,j), yy(i,j));
|
|
70
|
+
fh = T.locate(p, fh);
|
|
71
|
+
std::vector< std::pair< Point, Coord_type > > coords;
|
|
72
|
+
CGAL::Triple<std::back_insert_iterator<Coordinate_vector>, K::FT, bool> natneighbor = CGAL::natural_neighbor_coordinates_2(T, p, std::back_inserter(coords),fh);
|
|
73
|
+
|
|
74
|
+
if(!natneighbor.third)
|
|
75
|
+
{
|
|
76
|
+
raster[i][j] = fill_value;
|
|
77
|
+
}
|
|
78
|
+
else
|
|
79
|
+
{
|
|
80
|
+
raster[i][j] = CGAL::linear_interpolation(coords.begin(), coords.end(), natneighbor.second, CGAL::Data_access<Point_value_map>(values));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return raster;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
NB_MODULE(nninterpol, m) {
|
|
88
|
+
m.doc() = "Natural Neighbor Interpolation using CGAL";
|
|
89
|
+
nb::class_<Delaunay_triangulation>(m, "Delaunay_triangulation");
|
|
90
|
+
m.def("triangulate", &triangulate, "Triangulate the source grid using CGAL");
|
|
91
|
+
m.def("nninterpol", &nninterpol,nb::call_guard<nb::gil_scoped_release>(), "Linear Interpolation using CGAL");
|
|
92
|
+
}
|