'How to use Python libraries and functions in a C++ project
I have two classes in a header in my C++ project, namely, Point_CCS_xy
and class random_Point_CCS_xy_generator
.
MWE
#include<iostream>
#include<fstream>
#include<functional>
#include<algorithm>
#include<deque>
#include<queue>
#include<set>
#include<list>
#include<limits>
#include<string>
#include<memory>
#include <iomanip>
#include <vector>
#include <random>
using namespace std;
class random_Point_CCS_xy_generator;
class Point_CCS_xy{
private:
long double x_coordinate_ {0.0};
long double y_coordinate_ {0.0};
public:
Point_CCS_xy () = default;
Point_CCS_xy(long double x, long double y);
~Point_CCS_xy() = default;
// copy and move C-Tor and assignment plus some other setter getters come here
vector<Point_CCS_xy> Random_Point_CCS_xy(long double Min, long double Max, size_t n){
// uses function from class random_Point_CCS_xy_generator
}
};
What I want to do is :
1. Generate random points an return a vector of points ---> I already do that through this function : std::uniform_real_distribution<double> distribution_;
and store them in a vector of points say vector<Point_CCS_xy> some_new_points(pnt2.Random_Point_CCS_xy(-5000, 5000, 50));
For instance, a vector of points
of size 10:
some_new_points = [
[226.249243, 415.339357],
[94.324159, 15.358249],
[475.149262, 161.560577],
[28.652470, 288.493022],
[-425.899313, -170.149945],
[83.743814, -469.027833],
[428.593479, -480.247566],
[279.371955, -163.955121],
[-157.474035, -85.180488],
[407.313194, 151.480287]
]
2. Convert the vector some_new_points
into an array using numpy
A = np.asarray([
[226.249243, 415.339357],
[94.324159, 15.358249],
[475.149262, 161.560577],
[28.652470, 288.493022],
[-425.899313, -170.149945],
[83.743814, -469.027833],
[428.593479, -480.247566],
[279.371955, -163.955121],
[-157.474035, -85.180488],
[407.313194, 151.480287]
])
3. Using scipy
, like:
from scipy import spatial
import numpy as np
import time
A = np.asarray(
[
[226.249243, 415.339357],
[94.324159, 15.358249],
[475.149262, 161.560577],
[28.652470, 288.493022],
[-425.899313, -170.149945],
[83.743814, -469.027833],
[428.593479, -480.247566],
[279.371955, -163.955121],
[-157.474035, -85.180488],
[407.313194, 151.480287]
]
)
pt = [20.45, 300.25] # <-- the point to find
start = time.time()
distance, index = spatial.KDTree(A).query(pt)
print("distance: ", distance)
print("index: ", index)
print("A[index] = ", A[index])
4. Then, for instance using the returned index, from the original points vector some_new_points
, I retrieve the point of interest.
Not that, I have already created a KD-Tree in my C++ program, but it does not have so many useful properties of that of scipy.spatial
I am newbie in programming and what I am currently doing seems to be primitive as I manually do the above steps.
My question is if I can directly make implement the functions in my .h header in the c++ Project without having to go through all those steps manually (from CLion to PyCharm). I would like to do the vector conversion and accessing KD-Tree of the scipy in my .h header file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|