Skip to content

Commit

Permalink
Refactor: RpcOptim & RPC cpp constructor
Browse files Browse the repository at this point in the history
refactor: header correction

refactor: change names of func and str

CI: cut long line

refactor: Attribut in rpc cpp class + data test path

CI: API docu on getter
  • Loading branch information
August12vaux committed Dec 12, 2023
1 parent 7d8d18c commit fb2cb67
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 65 deletions.
2 changes: 0 additions & 2 deletions shareloc/bindings/GeoModelTemplate.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
coding: utf8
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
Expand Down
2 changes: 0 additions & 2 deletions shareloc/bindings/GeoModelTemplate.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
coding: utf8
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
Expand Down
30 changes: 27 additions & 3 deletions shareloc/bindings/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,47 @@ PYBIND11_MODULE(rpc_c, m) {
.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(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("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_numba", &compute_loc_inverse_derivates_numba, "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)
Expand Down
49 changes: 35 additions & 14 deletions shareloc/bindings/rpc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
coding: utf8
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
Expand All @@ -22,25 +20,26 @@ limitations under the License.
/**
Cpp copy of rpc.py
*/

#include <typeinfo>
#include "rpc.hpp"

//---- RPC methodes ----//

RPC::RPC(array<double, 20> num_col,
array<double, 20> den_col,
array<double, 20> num_row,
array<double, 20> den_row,
RPC::RPC(array<double, 20> num_col_input,
array<double, 20> den_col_input,
array<double, 20> num_row_input,
array<double, 20> den_row_input,
array<double, 10> norm_coeffs):GeoModelTemplate(){

num_col = num_col;
den_col = den_col;
num_row = num_row;
den_row = den_row;

offset_lon = norm_coeffs[0];
std::copy(num_col_input.begin(), num_col_input.end(), num_col.begin());
std::copy(den_col_input.begin(), den_col_input.end(), den_col.begin());
std::copy(num_row_input.begin(), num_row_input.end(), num_row.begin());
std::copy(den_row_input.begin(), den_row_input.end(), den_row.begin());

offset_lon = norm_coeffs[0];//offset_x
scale_lon = norm_coeffs[1];
offset_lat = norm_coeffs[2];
offset_lat = norm_coeffs[2];//offset_t
scale_lat = norm_coeffs[3];
offset_alt = norm_coeffs[4];
scale_alt = norm_coeffs[5];
Expand Down Expand Up @@ -135,6 +134,28 @@ vector<vector<double>> RPC::los_extrema(
return vect;
}

array<double, 20> RPC::get_num_col(){return num_col;}
array<double, 20> RPC::get_den_col(){return den_col;}
array<double, 20> RPC::get_num_row(){return num_row;}
array<double, 20> RPC::get_den_row(){return den_row;}

array<double, 20> RPC::get_num_lon(){return num_lon;}
array<double, 20> RPC::get_den_lon(){return den_lon;}
array<double, 20> RPC::get_num_lat(){return num_lat;}
array<double, 20> RPC::get_den_lat(){return den_lat;}

double RPC::get_offset_row(){return offset_row;}
double RPC::get_scale_row(){return scale_row;}
double RPC::get_offset_col(){return offset_col;}
double RPC::get_scale_col(){return scale_col;}
double RPC::get_offset_alt(){return offset_alt;}
double RPC::get_scale_alt(){return scale_alt;}
double RPC::get_offset_lon(){return offset_lon;}
double RPC::get_scale_lon(){return scale_lon;}
double RPC::get_offset_lat(){return offset_lat;}
double RPC::get_scale_lat(){return scale_lat;}


//---- Functions ----//

double polynomial_equation(
Expand Down Expand Up @@ -188,7 +209,7 @@ double derivative_polynomial_longitude(
tuple<vector<double>,
vector<double>,
vector<double>,
vector<double>> compute_loc_inverse_derivates_numba(
vector<double>> compute_loc_inverse_derivates_optimized(
vector<double> lon_norm,
vector<double> lat_norm,
vector<double> alt_norm,
Expand Down
53 changes: 47 additions & 6 deletions shareloc/bindings/rpc.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
coding: utf8
Copyright (c) 2023 Centre National d'Etudes Spatiales (CNES).
This file is part of shareloc
Expand Down Expand Up @@ -54,10 +52,10 @@ class RPC : public GeoModelTemplate{
array<double, 20> num_row;
array<double, 20> den_row;

array<double, 20> num_x;
array<double, 20> den_x;
array<double, 20> num_y;
array<double, 20> den_y;
array<double, 20> num_lon;
array<double, 20> den_lon;
array<double, 20> num_lat;
array<double, 20> den_lat;

array<double, 2> alt_minmax;

Expand Down Expand Up @@ -152,5 +150,48 @@ class RPC : public GeoModelTemplate{
double alt_max,
bool fill_nan=false,
int epsg=4326);


//-- getter --//

/**get_num_col*/
array<double, 20> get_num_col();
/**get_den_col*/
array<double, 20> get_den_col();
/**get_num_row*/
array<double, 20> get_num_row();
/**get_den_row*/
array<double, 20> get_den_row();

/**get_num_lon*/
array<double, 20> get_num_lon();
/**get_den_lon*/
array<double, 20> get_den_lon();
/**get_num_lat*/
array<double, 20> get_num_lat();
/**get_den_lat*/
array<double, 20> get_den_lat();

/**get_offset_row*/
double get_offset_row();
/**get_scale_row*/
double get_scale_row();
/**get_offset_col*/
double get_offset_col();
/**get_scale_col*/
double get_scale_col();
/**get_offset_alt*/
double get_offset_alt();
/**get_scale_alt*/
double get_scale_alt();
/**get_offset_lon*/
double get_offset_lon();
/**get_scale_lon*/
double get_scale_lon();
/**get_offset_lat*/
double get_offset_lat();
/**get_scale_lat*/
double get_scale_lat();
};


32 changes: 18 additions & 14 deletions shareloc/geomodels/rpc_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@ class RpcOptim(rpc_c.RPC, GeoModelTemplate):
def __init__(self, rpc_params):
GeoModelTemplate.__init__(self)

for key, value in rpc_params.items():
setattr(self, key, value)

norm_coeffs = [
self.offset_x,
self.scale_x, # longitude
self.offset_y,
self.scale_y, # latitude
self.offset_alt,
self.scale_alt,
self.offset_col,
self.scale_col,
self.offset_row,
self.scale_row,
rpc_params["offset_x"],
rpc_params["scale_x"], # longitude
rpc_params["offset_y"],
rpc_params["scale_y"], # latitude
rpc_params["offset_alt"],
rpc_params["scale_alt"],
rpc_params["offset_col"],
rpc_params["scale_col"],
rpc_params["offset_row"],
rpc_params["scale_row"],
]

rpc_c.RPC.__init__(self, self.num_col, self.den_col, self.num_row, self.den_row, norm_coeffs)
rpc_c.RPC.__init__(
self,
rpc_params["num_col"],
rpc_params["den_col"],
rpc_params["num_row"],
rpc_params["den_row"],
norm_coeffs,
)

@classmethod
def load(cls, geomodel_path):
Expand Down
76 changes: 52 additions & 24 deletions tests/geomodels/test_rpc_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,91 @@
Module to test RpcOptim class
"""


import os

import numpy as np

# Third party imports
import pytest

# Shareloc bindings
import rpc_c

# Shareloc imports
from shareloc.geomodels import GeoModel
from shareloc.geomodels.rpc_readers import rpc_reader

# Shareloc test imports
from ..helpers import data_path


# pylint: disable=duplicate-code
@pytest.mark.parametrize(
"geom_path",
[
"tests/data/rpc/PHR1B_P_201709281038045_SEN_PRG_FC_178608-001.geom",
"tests/data/rpc/PHR1B_P_201709281038393_SEN_PRG_FC_178609-001.tif",
"tests/data/rpc/PHRDIMAP_P1BP--2017030824934340CP.XML",
"tests/data/rpc/RPC_P1BP--2017092838284574CP.XML",
"tests/data/rpc/RPC_PHR1B_P_201709281038045_SEN_PRG_FC_178608-001.XML",
"rpc/PHR1B_P_201709281038045_SEN_PRG_FC_178608-001.geom",
"rpc/PHR1B_P_201709281038393_SEN_PRG_FC_178609-001.tif",
"rpc/PHRDIMAP_P1BP--2017030824934340CP.XML",
"rpc/RPC_P1BP--2017092838284574CP.XML",
"rpc/RPC_PHR1B_P_201709281038045_SEN_PRG_FC_178608-001.XML",
],
)
def test_construtor(geom_path):
"""
Test RpcOptim constructor
"""

rpc_optim = GeoModel(geom_path, "RpcOptim")
rpc_py = GeoModel(geom_path, "RPC")
file_path = os.path.join(data_path(), geom_path)

rpc_py = GeoModel(file_path, "RPC")

rpc_params = rpc_reader(file_path, topleftconvention=True)
norm_coeffs = [
rpc_params["offset_x"],
rpc_params["scale_x"], # longitude
rpc_params["offset_y"],
rpc_params["scale_y"], # latitude
rpc_params["offset_alt"],
rpc_params["scale_alt"],
rpc_params["offset_col"],
rpc_params["scale_col"],
rpc_params["offset_row"],
rpc_params["scale_row"],
]
rpc_cpp = rpc_c.RPC(
rpc_params["num_col"], rpc_params["den_col"], rpc_params["num_row"], rpc_params["den_row"], norm_coeffs
)

assert rpc_py.offset_x == rpc_optim.offset_x
assert rpc_py.scale_x == rpc_optim.scale_x
assert rpc_py.offset_y == rpc_optim.offset_y
assert rpc_py.scale_y == rpc_optim.scale_y
assert rpc_py.offset_alt == rpc_optim.offset_alt
assert rpc_py.scale_alt == rpc_optim.scale_alt
assert rpc_py.offset_col == rpc_optim.offset_col
assert rpc_py.scale_col == rpc_optim.scale_col
assert rpc_py.offset_row == rpc_optim.offset_row
assert rpc_py.scale_row == rpc_optim.scale_row
assert rpc_py.offset_x == rpc_cpp.get_offset_lon()
assert rpc_py.scale_x == rpc_cpp.get_scale_lon()
assert rpc_py.offset_y == rpc_cpp.get_offset_lat()
assert rpc_py.scale_y == rpc_cpp.get_scale_lat()
assert rpc_py.offset_alt == rpc_cpp.get_offset_alt()
assert rpc_py.scale_alt == rpc_cpp.get_scale_alt()
assert rpc_py.offset_col == rpc_cpp.get_offset_col()
assert rpc_py.scale_col == rpc_cpp.get_scale_col()
assert rpc_py.offset_row == rpc_cpp.get_offset_row()
assert rpc_py.scale_row == rpc_cpp.get_scale_row()

np.testing.assert_array_equal(rpc_py.num_col, rpc_optim.num_col)
np.testing.assert_array_equal(rpc_py.den_col, rpc_optim.den_col)
np.testing.assert_array_equal(rpc_py.num_row, rpc_optim.num_row)
np.testing.assert_array_equal(rpc_py.den_row, rpc_optim.den_row)
np.testing.assert_array_equal(rpc_py.num_col, rpc_cpp.get_num_col())
np.testing.assert_array_equal(rpc_py.den_col, rpc_cpp.get_den_col())
np.testing.assert_array_equal(rpc_py.num_row, rpc_cpp.get_num_row())
np.testing.assert_array_equal(rpc_py.den_row, rpc_cpp.get_den_row())


def test_method_rpc_cpp():
"""
Test call to method parent class rpc in cpp
"""

rpc = GeoModel("tests/data/rpc/PHR1B_P_201709281038045_SEN_PRG_FC_178608-001.geom", "RpcOptim")
file_path = os.path.join(data_path(), "rpc/PHR1B_P_201709281038045_SEN_PRG_FC_178608-001.geom")
rpc = GeoModel(file_path, "RpcOptim")

vector_double = [1.0, 1.0, 1.0]
double = 1.0
integer = 1
string = "peuimporte"
string = "string"

rpc.direct_loc_h(vector_double, vector_double, double, False)
rpc.direct_loc_grid_h(integer, integer, integer, integer, integer, integer, double)
Expand Down Expand Up @@ -119,7 +147,7 @@ def test_function_rpc_cpp():

rpc_c.derivative_polynomial_longitude(double, double, double, vector_double)

rpc_c.compute_loc_inverse_derivates_numba(
rpc_c.compute_loc_inverse_derivates_optimized(
vector_double,
vector_double,
vector_double,
Expand Down

0 comments on commit fb2cb67

Please sign in to comment.