Skip to content

Commit

Permalink
Merge branch 'RpcOptim' into 'master'
Browse files Browse the repository at this point in the history
RpcOptim - shareloc optimisé avec bindings c++

See merge request 3d/shareloc!143
  • Loading branch information
dyoussef committed Dec 12, 2023
2 parents 528aa38 + fb2cb67 commit 46b4313
Show file tree
Hide file tree
Showing 15 changed files with 953 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ suggestion-mode=yes
# active Python interpreter and may run arbitrary code.
unsafe-load-any-extension=no

# List of cpp allowed extension
extension-pkg-whitelist= rpc_c

[MESSAGES CONTROL]

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ install: venv ## install the package in dev mode in virtualenv
[ "${CHECK_SHARELOC}" ] || ${VENV}/bin/python -m pip install -e .[dev,docs,notebook]
@test -f .git/hooks/pre-commit || echo "Install pre-commit"
@test -f .git/hooks/pre-commit || ${VENV}/bin/pre-commit install -t pre-commit
@test -f .git/hooks/pre-push || ${VENV}/bin/pre-commit install -t pre-push
@test -f .git/hooks/pre-push || ${VENV}/bin/pre-commit install -t pre-push
@echo "Shareloc ${VERSION} installed in dev mode in virtualenv ${VENV} with documentation"
@echo " virtualenv usage : source ${VENV}/bin/activate; python3 -c 'import shareloc'"

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pyproject.toml
[build-system]
requires = ["setuptools>=65.5", "wheel", "setuptools_scm[toml]>=6.2"]
requires = ["setuptools>=65.5", "wheel", "setuptools_scm[toml]>=6.2","pybind11"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
Expand Down Expand Up @@ -30,4 +30,4 @@ disable_error_code = 'attr-defined'
module = [
'setuptools',
]
ignore_missing_imports = true
ignore_missing_imports = true
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ setup_requires =
setuptools>=65.5
setuptools_scm[toml]>=6.2 # Following https://pypi.org/project/setuptools-scm/
wheel
pybind11

# shareloc packages dependencies
install_requires =
Expand All @@ -61,7 +62,8 @@ install_requires =
rasterio
xarray
numba

pybind11

package_dir =
. = shareloc
packages = find:
Expand All @@ -86,6 +88,7 @@ dev =
pytest-sugar
tox
mypy
pybind11

# [doc] mode dependencies
docs =
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
Main part is in setup.cfg file.
"""

from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools import setup

# Main setup with setup.cfg file.
setup(use_scm_version=True)
extensions = [Pybind11Extension("rpc_c", ["shareloc/bindings/bind.cpp"])]

setup(use_scm_version=True, cmdclass={"build_ext": build_ext}, ext_modules=extensions)
54 changes: 54 additions & 0 deletions shareloc/bindings/GeoModelTemplate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
(see https://github.com/CNES/shareloc).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
Cpp copy of GeoModelTemplate.py
*/


GeoModelTemplate::GeoModelTemplate() {
epsg = 0;
}
GeoModelTemplate::~GeoModelTemplate() {
}

vector<vector<double>> GeoModelTemplate::direct_loc_h(
vector<double> row,
vector<double> col,
double alt,
bool fill_nan){
vector<vector<double>> vect;
return vect;
}

vector<vector<double>> GeoModelTemplate::direct_loc_dtm(
vector<double> row,
vector<double> col,
string dtm){
vector<vector<double>> vect;
return vect;
}

tuple<vector<double>,vector<double>,vector<double>> GeoModelTemplate::inverse_loc(
vector<double> lon,
vector<double> lat,
double alt){
tuple<vector<double>,vector<double>,vector<double>> res;
return res;
}
71 changes: 71 additions & 0 deletions shareloc/bindings/GeoModelTemplate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
(see https://github.com/CNES/shareloc).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
Cpp copy of GeoModelTemplate.py
Abstract class GeoModelTemplate.
Child class: RPC
*/

#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <array>
#include <map>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::tuple;
using std::array;
using std::map;

/**
Abstract class GeoModelTemplate:
Child: RPC
*/
class GeoModelTemplate {

private:
string type;
int epsg;
public:
/**Constructor*/
GeoModelTemplate();
/**Destructor*/
~GeoModelTemplate();
/**direct_loc_h*/
vector<vector<double>> direct_loc_h(
vector<double> row,
vector<double> col,
double alt,
bool fill_nan=false);
/**direct_loc_dtm*/
vector<vector<double>> direct_loc_dtm(
vector<double> row,
vector<double> col,
string dtm);
/**inverse_loc*/
tuple<vector<double>,vector<double>,vector<double>> inverse_loc(
vector<double> lon,
vector<double> lat,
double alt);
};
88 changes: 88 additions & 0 deletions shareloc/bindings/bind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
coding: utf8
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
(see https://github.com/CNES/shareloc).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
Thes purpose of this module is only to "bind" cpp code.
It gives to the compiler the instructions to compile the usefull cpp code into an .so file
which is callable in a python code as a python module.
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "rpc.cpp"

namespace py = pybind11;


PYBIND11_MODULE(rpc_c, m) {

py::class_<GeoModelTemplate>(m, "GeoModelTemplate")
.def(py::init<>())
.def("direct_loc_h", &GeoModelTemplate::direct_loc_h)
.def("direct_loc_dtm", &GeoModelTemplate::direct_loc_dtm)
.def("inverse_loc", &GeoModelTemplate::inverse_loc);

py::class_<RPC,GeoModelTemplate>(m, "RPC")
.def(py::init<array<double, 20>,
array<double, 20>,
array<double, 20>,
array<double, 20>,
array<double, 10>>())
.def("direct_loc_h", &RPC::direct_loc_h)
.def("direct_loc_grid_h", &RPC::direct_loc_grid_h)
.def("direct_loc_dtm", &RPC::direct_loc_dtm)
.def("filter_coordinates", &RPC::filter_coordinates)
.def("compute_loc_inverse_derivates", &RPC::compute_loc_inverse_derivates)
.def("direct_loc_inverse_iterative", &RPC::direct_loc_inverse_iterative)
.def("get_alt_min_max", &RPC::get_alt_min_max)
.def("los_extrema", &RPC::los_extrema)
.def("get_num_col", &RPC::get_num_col)
.def("get_den_col", &RPC::get_den_col)
.def("get_num_row", &RPC::get_num_row)
.def("get_den_row", &RPC::get_den_row)
.def("get_num_lon", &RPC::get_num_lon)
.def("get_den_lon", &RPC::get_den_lon)
.def("get_num_lat", &RPC::get_num_lat)
.def("get_den_lat", &RPC::get_den_lat)
.def("get_offset_row", &RPC::get_offset_row)
.def("get_scale_row", &RPC::get_scale_row)
.def("get_offset_col", &RPC::get_offset_col)
.def("get_scale_col", &RPC::get_scale_col)
.def("get_offset_alt", &RPC::get_offset_alt)
.def("get_scale_alt", &RPC::get_scale_alt)
.def("get_offset_lon", &RPC::get_offset_lon)
.def("get_scale_lon", &RPC::get_scale_lon)
.def("get_offset_lat", &RPC::get_offset_lat)
.def("get_scale_lat", &RPC::get_scale_lat);

//m.doc() = "Pybind hello world"; // optional module docstring
m.def("polynomial_equation", &polynomial_equation, "TODO: doc");
m.def("compute_rational_function_polynomial", &compute_rational_function_polynomial,
"TODO: doc");
m.def("derivative_polynomial_latitude", &derivative_polynomial_latitude, "TODO: doc");
m.def("derivative_polynomial_longitude", &derivative_polynomial_longitude, "TODO: doc");
m.def("compute_loc_inverse_derivates_optimized",
&compute_loc_inverse_derivates_optimized,
"TODO: doc");
}

//c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes)
//bind.cpp -o pbrpc$(python3-config --extension-suffix)
Loading

0 comments on commit 46b4313

Please sign in to comment.