Skip to content

Commit fe10155

Browse files
author
Saurav Agarwal
committed
Add torch map conversion
1 parent c84f57e commit fe10155

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <torch/torch.h>
2+
#include <iostream>
3+
4+
#include <iomanip>
5+
#include <iostream>
6+
#include <random>
7+
8+
#include <CoverageControl/constants.h>
9+
#include <CoverageControl/typedefs.h>
10+
#include <CoverageControl/bivariate_normal_distribution.h>
11+
#include <CoverageControl/world_idf.h>
12+
#include <CoverageControl/robot_model.h>
13+
#include <CoverageControl/coverage_system.h>
14+
15+
#include <CoverageControlTorch/type_conversions.h>
16+
17+
using namespace CoverageControl;
18+
using namespace CoverageControlTorch;
19+
20+
int main(int argc, char** argv) {
21+
Parameters params;
22+
23+
params.pTruncationBND = 10;
24+
std::unique_ptr <CoverageSystem> env;
25+
26+
std::vector <Point2> robot_positions;
27+
robot_positions.push_back(Point2(100, 10));
28+
robot_positions.push_back(Point2(100, 100));
29+
robot_positions.push_back(Point2(10, 100));
30+
31+
WorldIDF world_idf(params);
32+
world_idf.AddNormalDistribution(BivariateNormalDistribution(Point2(50, 50), 30));
33+
34+
env = std::make_unique<CoverageSystem> (params, world_idf, robot_positions);
35+
36+
auto env_map = env->GetWorldIDF();
37+
auto env_map_tensor = EigenToLibTorch(env_map);
38+
std::cout << env_map_tensor.dtype() << std::endl;
39+
40+
print(env_map_tensor[50][50]);
41+
42+
return 0;
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Type conversion between CoverageControl and Torch
3+
*
4+
* TODO:
5+
**/
6+
7+
#ifndef COVERAGECONTROLTORCH_TYPE_CONVERSION_H_
8+
#define COVERAGECONTROLTORCH_TYPE_CONVERSION_H_
9+
10+
#include <vector>
11+
#include <Eigen/Dense> // Eigen is used for maps
12+
13+
#include <CoverageControl/typedefs.h>
14+
15+
#include <torch/torch.h>
16+
17+
using namespace CoverageControl;
18+
namespace CoverageControlTorch {
19+
20+
torch::Tensor EigenToLibTorch(MapType const &M) {
21+
MapType M_copy = M;
22+
std::vector<int64_t> dims = {M_copy.rows(), M_copy.cols()};
23+
auto T = torch::from_blob(M_copy.data(), dims).clone();
24+
return T;
25+
}
26+
27+
} // namespace CoverageControlTorch
28+
29+
#endif // COVERAGECONTROLTORCH_TYPE_CONVERSION_H_

0 commit comments

Comments
 (0)