-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpython_api.cpp
More file actions
770 lines (691 loc) · 44.3 KB
/
Copy pathpython_api.cpp
File metadata and controls
770 lines (691 loc) · 44.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//
// Created by juancarlos on 18/11/20.
//
#include "dsr/api/dsr_api.h"
#include <dsr/api/dsr_signal_emitter.h>
#include <stdexcept>
#include <thread>
using namespace DSR;
#include "include/silent_output.h"
#include "include/signal_function_caster.h"
#include "include/custom_bind_map.h"
#include "include/custom_bool_cast.h"
#include "include/custom_vector_cast.h"
#include "include/GHistory.h"
#pragma push_macro("slots")
#undef slots
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/eigen.h>
#include <pybind11/stl_bind.h>
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#pragma pop_macro("slots")
#include <utility>
#include <memory>
#include <functional>
namespace py = pybind11;
using namespace py::literals;
//using namespace RoboCompDSRGetID;
enum ATT_ENUM: uint16_t {
STRING_PY,
BOOL_PY,
NPYU8,
NPYF,
NPYU64,
VECU8_PY,
VECF_PY,
VECU64_PY,
VEC2_PY,
VEC3_PY,
VEC4_PY,
VEC6_PY,
U64_PY,
DOUBLE_PY,
FLOAT_PY,
I32_PY,
U32_PY
};
using attribute_type = std::variant<std::string,
no_int_cast_bool,
py::array_t<uint8_t>,
py::array_t<float>,
py::array_t<uint64_t>,
std::vector<uint8_t>,
std::vector<float>,
std::vector<uint64_t>,
std::array<float, 2>,
std::array<float, 3>,
std::array<float, 4>,
std::array<float, 6>,
uint64_t,
double,
float,
int32_t,
uint32_t>;
template<std::size_t idx, typename T>
Value convert_variant_fn(const attribute_type & e)
{
//std::cout << "[PYTHON_VARIANT -> Value] " << attribute_type_TYPENAMES_UNION[e.index()] << std::endl;
Value vout;
if constexpr (std::is_same_v<T, py::array_t<uint8_t>>)
{
auto tmp = std::get<py::array_t<uint8_t>>(e);
const auto size = tmp.size();
py::buffer_info x = tmp.request();
vout.emplace<idx>(std::vector<uint8_t>{static_cast<uint8_t *>(x.ptr), static_cast<uint8_t *>(x.ptr) + size});
} else if constexpr (std::is_same_v<T, py::array_t<float>>)
{
auto tmp = std::get<py::array_t<float>>(e);
//std::cout << tmp.dtype() << " " << tmp.size() << py::array_t<float>::ensure(tmp) << std::endl;
const auto size = tmp.size();
py::buffer_info x = tmp.request();
vout.emplace<idx>(std::vector<float>{static_cast<float *>(x.ptr), static_cast<float *>(x.ptr) + size});
} else if constexpr (std::is_same_v<T, py::array_t<uint64_t>>)
{
auto tmp = std::get<py::array_t<uint64_t>>(e);
const auto size = tmp.size();
py::buffer_info x = tmp.request();
vout.emplace<idx>(std::vector<uint64_t>{static_cast<uint64_t *>(x.ptr), static_cast<uint64_t *>(x.ptr) + size});
} else if constexpr (std::is_same_v<T, no_int_cast_bool>)
{
auto tmp = std::get<no_int_cast_bool>(e)();
vout.emplace<idx>(tmp);
}
else
{
auto x = std::get<T>(e);
vout.emplace<idx>(x);
}
return vout;
};
Value convert_variant(const attribute_type & e)
{
typedef Value (*conver_fn) (const attribute_type &);
constexpr std::array<conver_fn, 17> cast = { convert_variant_fn<0, std::string>,
convert_variant_fn<4, no_int_cast_bool>,
convert_variant_fn<5, py::array_t<uint8_t>>,
convert_variant_fn<3, py::array_t<float>>,
convert_variant_fn<9, py::array_t<uint64_t>>,
convert_variant_fn<5, std::vector<uint8_t>>,
convert_variant_fn<3, std::vector<float>>,
convert_variant_fn<9, std::vector<uint64_t>>,
convert_variant_fn<10, std::array<float, 2>>,
convert_variant_fn<11, std::array<float, 3>>,
convert_variant_fn<12, std::array<float, 4>>,
convert_variant_fn<13, std::array<float, 6>>,
convert_variant_fn<7, uint64_t>,
convert_variant_fn<8, double>,
convert_variant_fn<2, float>,
convert_variant_fn<1, int32_t>,
convert_variant_fn<6, uint32_t>
};
const auto idx = e.index(); //idx_Value.at(e.index());
return cast[idx](e);
}
PYBIND11_MAKE_OPAQUE(std::map<std::pair<uint64_t, std::string>, Edge>)
PYBIND11_MAKE_OPAQUE(std::map<std::string, Attribute>)
PYBIND11_MODULE(pydsr, m) {
py::enum_<SyncMode>(m, "SyncMode")
.value("CRDT", SyncMode::CRDT)
.value("LWW", SyncMode::LWW)
.export_values();
py::bind_map<std::map<std::pair<uint64_t, std::string>, Edge>>(m, "MapStringEdge");
py::bind_dsr_map<std::map<std::string, Attribute>>(m, "MapStringAttribute");
m.doc() = "DSR Api for python";
uint64_t local_agent_id = -1;
//Disable messages from Qt.
qInstallMessageHandler([](QtMsgType type, const QMessageLogContext &context, const QString &msg) {
if (type == QtCriticalMsg || type == QtFatalMsg) {
fprintf(stderr, "%s", msg.toStdString().c_str());
}
});
//Disable cout
//m.attr("redirect_output") = py::capsule(new scoped_ostream_discard(),
// [](void *sor) { delete static_cast<scoped_ostream_discard *>(sor); });
auto sig = m.def_submodule("signals",
R""""(
Connect functions to DSR signals. The types of the signals are defined in the "signal_type enum.
In order to connect the signals you must annotate the types of function parameters to match those of the signals.
The function signatures are the following:
UPDATE_NODE: [[int, str], None]
UPDATE_NODE_ATTR: [[int, [str]], None]
UPDATE_EDGE: [[int, int, str], None]
UPDATE_EDGE_ATTR: [[int, int, [str]], None]
DELETE_EDGE: [[int, int, str], None]
DELETE_NODE: [[int], None]
DELETE_NODE_OBJ: [[pydsr.Node], None]
DELETE_EDGE_OBJ: [[pydsr.EDGE], None]
")"""");
enum signal_type
{
UPDATE_NODE,
UPDATE_NODE_ATTR,
UPDATE_EDGE,
UPDATE_EDGE_ATTR,
DELETE_EDGE,
DELETE_NODE,
DELETE_NODE_OBJ,
DELETE_EDGE_OBJ
};
py::enum_<signal_type>(sig, "signal_type")
.value("UPDATE_NODE", UPDATE_NODE)
.value("UPDATE_NODE_ATTR", UPDATE_NODE_ATTR)
.value("UPDATE_EDGE", UPDATE_EDGE)
.value("UPDATE_EDGE_ATTR", UPDATE_EDGE_ATTR)
.value("DELETE_EDGE", DELETE_EDGE)
.value("DELETE_NODE", DELETE_NODE)
.value("DELETE_EDGE_OBJ", DELETE_EDGE_OBJ)
.value("DELETE_NODE_OBJ", DELETE_NODE_OBJ)
.export_values();
sig.def("connect", [&](DSRGraph *G, signal_type type, callback_types fn_callback) {
auto runner = G->get_signal_runner();
if (!runner) {
throw std::runtime_error("Signal runner doesn't exist");
}
try {
runner->connect(fn_callback, std::to_string(type));
} catch (std::exception &e) {
std::cout << "Update Node Callback must be (int, str)\n " << std::endl;
throw e;
}
});
//DSR Attribute class
py::class_<Attribute>(m, "Attribute")
.def(py::init([&](attribute_type const& v, uint64_t t, uint32_t agent_id){
//Use another variant type to avoid problems with implicit conversions.
return Attribute(convert_variant(v), t, agent_id);
}),
"value"_a, "timestamp"_a, "agent_id"_a)
.def(py::init([&](attribute_type const& v , uint32_t agent_id) {
//Comprobar tipos en Value. Como se convien los arrays de numpy, las listas, los doubles, etc.
return Attribute(convert_variant(v), get_unix_timestamp(), agent_id);
}),"value"_a, "agent_id"_a)
.def(py::init([&](attribute_type const& v) {
//Comprobar tipos en Value. Como se convien los arrays de numpy, las listas, los doubles, etc.
return Attribute(convert_variant(v), get_unix_timestamp(), local_agent_id);
}),"value"_a)
.def("__repr__", [](Attribute const &self) {
std::stringstream out;
out << " < ";
out << self;
out << " >";
return out.str();
})
.def_property_readonly("agent_id", [](Attribute &self) { return self.agent_id(); },
"read the agent_id attribute. This property is readonly ans it is updated when a change is made in the value property.")
.def_property_readonly("timestamp", [](Attribute &self) { return self.timestamp(); },
"read the timestamp (ns) attribute. This property is readonly and it is updated when a change is made in the value property.")
.def_property("value", [](Attribute &self) -> attribute_type {
switch (self.selected()) {
// Basic types
case 0: return self.str();
case 1: return self.dec();
case 2: return self.fl();
case 6: return self.uint();
case 7: return self.uint64();
case 8: return self.dob();
case 4: return self.bl();
// Vectors types
case 3: // float_vec
return py::array_t<float>(
{(py::ssize_t)self.float_vec().size()}, // Shape
{sizeof(float)}, // Stride
self.float_vec().data(), // Pointer
py::cast(self) // Owner
);
case 5: // byte_vec
return py::array_t<uint8_t>(
{(py::ssize_t)self.byte_vec().size()},
{sizeof(uint8_t)},
self.byte_vec().data(),
py::cast(self)
);
case 9: // u64_vec
return py::array_t<uint64_t>(
{(py::ssize_t)self.u64_vec().size()},
{sizeof(uint64_t)},
self.u64_vec().data(),
py::cast(self)
);
case 10: // vec2
return py::array_t<float>(
{2}, {sizeof(float)},
self.vec2().data(),
py::cast(self)
);
case 11: // vec3
return py::array_t<float>(
{3}, {sizeof(float)},
self.vec3().data(),
py::cast(self)
);
case 12: // vec4
return py::array_t<float>(
{4}, {sizeof(float)},
self.vec4().data(),
py::cast(self)
);
case 13: // vec6
return py::array_t<float>(
{6}, {sizeof(float)},
self.vec6().data(),
py::cast(self)
);
default:
throw pybind11::type_error("Unreachable");
}
},
[&](Attribute &self, const attribute_type &val) {
auto excep = std::string("Attributes cannot change type. Selected type is " + std::string{DSR::TYPENAMES_UNION[self.selected()]} + " and used type is " + std::string{attribute_type_TYPENAMES_UNION[val.index()]});
try {
//std::cout << "[SET MAP VALUE] " << self.selected() << ", " << val.index() << std::endl;
switch (self.selected()) {
case 0:
self.str(std::get<std::string>(val));
break;
case 1:
if (val.index() == ATT_ENUM::U64_PY) self.dec(std::get<uint64_t>(val)); //This is intentional
else self.dec(std::get<int32_t>(val));
break;
case 2:
if (val.index() == ATT_ENUM::DOUBLE_PY) self.fl(std::get<double>(val)); //This is intentional
else self.fl(std::get<float>(val));
break;
case 3:
if (val.index() == ATT_ENUM::NPYF) {
auto tmp = std::get<py::array_t<float>>(val);
const auto size = tmp.size();
py::buffer_info x = tmp.request();
self.float_vec(std::vector<float>{static_cast<float *>(x.ptr), static_cast<float *>(x.ptr) + size});
}
else self.float_vec(std::get<std::vector<float>>(val));
break;
case 4:
self.bl(std::get<no_int_cast_bool>(val)());
break;
case 5:
if (val.index() == ATT_ENUM::NPYU8) {
auto tmp = std::get<py::array_t<uint8_t>>(val);
const auto size = tmp.size();
py::buffer_info x = tmp.request();
self.byte_vec(std::vector<uint8_t>{static_cast<uint8_t *>(x.ptr), static_cast<uint8_t *>(x.ptr) + size});
}
else self.byte_vec(std::get<std::vector<uint8_t>>(val));
break;
case 6:
if (val.index() == ATT_ENUM::U64_PY) self.uint(std::get<uint64_t>(val));
else self.uint(std::get<uint32_t>(val));
break;
case 7:
self.uint64(std::get<uint64_t>(val));
break;
case 8:
self.dob(std::get<double>(val));
break;
case 9:
if (val.index() == ATT_ENUM::NPYU64) {
auto tmp = std::get<py::array_t<uint64_t>>(val);
const auto size = tmp.size();
py::buffer_info x = tmp.request();
self.u64_vec(std::vector<uint64_t>{static_cast<uint64_t *>(x.ptr), static_cast<uint64_t *>(x.ptr) + size});
} else if (val.index() == ATT_ENUM::VECU8_PY) {
auto tmp = std::get<std::vector<uint8_t>>(val);
self.u64_vec(std::vector<uint64_t>{tmp.begin(), tmp.end()});
}
else self.u64_vec(std::get<std::vector<uint64_t>>(val));
break;
case 10:
if (val.index() == ATT_ENUM::NPYF) {
auto tmp = std::get<py::array_t<float>>(val);
if (tmp.size() == 2) {
py::buffer_info x = tmp.request();
self.vec2(std::array<float, 2>{static_cast<float *>(x.ptr)[0],
static_cast<float *>(x.ptr)[1]});
} else throw;
}else if (val.index() == ATT_ENUM::VECF_PY) {
auto tmp = std::get<std::vector<float>>(val);
if (tmp.size() == 2) {
self.vec2(std::array<float, 2>{tmp[0], tmp[1]});
} else throw pybind11::type_error(excep);
}
else self.vec2(std::get<std::array<float, 2>>(val));
break;
case 11:
if (val.index() == ATT_ENUM::NPYF) {
auto tmp = std::get<py::array_t<float>>(val);
if (tmp.size() == 3) {
py::buffer_info x = tmp.request();
self.vec3(std::array<float, 3>{static_cast<float *>(x.ptr)[0],
static_cast<float *>(x.ptr)[1],
static_cast<float *>(x.ptr)[2]});
} else throw pybind11::type_error(excep);
} else if (val.index() == ATT_ENUM::VECF_PY) {
auto tmp = std::get<std::vector<float>>(val);
if (tmp.size() == 3) {
self.vec3(std::array<float, 3>{tmp[0],
tmp[1],
tmp[2]});
} else throw pybind11::type_error(excep);
}
else self.vec3(std::get<std::array<float, 3>>(val));
break;
case 12:
if (val.index() == ATT_ENUM::NPYF) {
auto tmp = std::get<py::array_t<float>>(val);
if (tmp.size() == 4) {
py::buffer_info x = tmp.request();
self.vec4(std::array<float, 4>{static_cast<float *>(x.ptr)[0],
static_cast<float *>(x.ptr)[1],
static_cast<float *>(x.ptr)[2],
static_cast<float *>(x.ptr)[3]});
} else throw pybind11::type_error(excep);
}else if (val.index() == ATT_ENUM::VECF_PY) {
auto tmp = std::get<std::vector<float>>(val);
if (tmp.size() == 4) {
self.vec4(std::array<float, 4>{tmp[0],
tmp[1],
tmp[2],
tmp[3]});
} else throw pybind11::type_error(excep);
}
else self.vec4(std::get<std::array<float, 4>>(val));
break;
case 13:
if (val.index() == ATT_ENUM::NPYF) {
auto tmp = std::get<py::array_t<float>>(val);
if (tmp.size() == 6) {
py::buffer_info x = tmp.request();
self.vec6(std::array<float, 6>{static_cast<float *>(x.ptr)[0],
static_cast<float *>(x.ptr)[1],
static_cast<float *>(x.ptr)[2],
static_cast<float *>(x.ptr)[3],
static_cast<float *>(x.ptr)[4],
static_cast<float *>(x.ptr)[5]});
} else throw pybind11::type_error(excep);
} else if (val.index() == ATT_ENUM::VECF_PY) {
auto tmp = std::get<std::vector<float>>(val);
if (tmp.size() == 6) {
self.vec6(std::array<float, 6>{tmp[0],
tmp[1],
tmp[2],
tmp[3],
tmp[4],
tmp[5]});
} else throw pybind11::type_error(excep);
}
else self.vec6(std::get<std::array<float, 6>>(val));
break;
default:
throw /*std::runtime_error*/ pybind11::type_error(excep);
};
self.timestamp(get_unix_timestamp());
self.agent_id(local_agent_id);
} catch (...) {
throw /*std::runtime_error*/ pybind11::type_error(excep);
}
},
py::return_value_policy::reference, "read or assign a new value to the Attribute object.");
//DSR Edge class
py::class_<Edge>(m, "Edge")
.def(py::init<uint64_t, uint64_t, std::string, uint32_t>(),
"to"_a, "from"_a, "type"_a, "agent_id"_a)
.def("__repr__", [](Edge const &self) {
std::stringstream out;
out << "------------------------------------" << std::endl;
out << "Type: " << self.type() << " from: " << self.from() << " to: " << self.to() << std::endl;
for (auto& [k, v] : self.attrs())
out << " [" << k << "] -- " << v << std::endl;
out << "------------------------------------" << std::endl;
return out.str();
})
.def_property_readonly("type", [](Edge const &self) { return self.type(); },
"read the type of the edge. This property is readonly")
.def_property_readonly("origin", [](Edge const &self) { return self.from(); },
"read the origen node of the edge. This property is readonly")
.def_property_readonly("destination", [](Edge const &self) { return self.to(); },
"read the destination node of the edge. This property is readonly")
.def_property("agent_id", [](Edge const &self) { return self.agent_id(); },
[](Edge &self, uint32_t val) { self.agent_id(val); },
"read or assign a new value to the agent_id attribute.")
.def_property("attrs", [](Edge &self) -> std::map<std::string, Attribute> & { return self.attrs(); },
[](Edge &self, const std::map<std::string, Attribute> &at) { self.attrs(at); },
py::return_value_policy::reference, "read or write in the attribute map of the edge.");
//DSR Node class
py::class_<Node>(m, "Node")
.def(py::init([](uint32_t agent_id, const std::string &type,
const std::string &name = "") -> std::unique_ptr<Node> {
auto tmp = std::make_unique<Node>(agent_id, type);
tmp->name(name);
return tmp;
}), "agent_id"_a, "type"_a, "name"_a = "")
.def("__repr__", [](Node const &self) {
std::stringstream out;
out << "------------------------------------" << std::endl;
out << "Node: " << self.id() << std::endl;
out << " Type: " << self.type() << std::endl;
out << " Name: " << self.name() << std::endl;
out << " Agent id: " << self.agent_id() << std::endl;
out << " ATTRIBUTES: [\n";
for (auto& [key, val] : self.attrs())
out << " [" << key << "] -- " << val << std::endl;
out << " ]" << std::endl;
out << " EDGES: [" << std::endl;
for (auto& [key, val] : self.fano()) {
out << " Edge type: " << val.type() << " from: " << val.from() << " to: " << val.to()
<< std::endl;
for (auto& [k, v] : val.attrs())
out << " [" << k << "] -- " << v << std::endl;
}
out << " ]" << std::endl;
out << "------------------------------------" << std::endl;
return out.str();
})
.def_property_readonly("id", [](Node const &self) { return self.id(); },
"read the id of the node. This property is readonly and is generated by the idserver agent when the node is inserted")
.def_property_readonly("name", [](Node const &self) { return self.name(); },
"read the name of the node. This property is readonly. If the name is not provided to the constructor or the name already exist in G, the name is generated by the idserver agent with a combination of the type and the id when the node is inserted")
.def_property_readonly("type", [](Node const &self) { return self.type(); },
"read the type of the node. This property is readonly")
.def_property("agent_id", [](Node const &self) { return self.agent_id(); },
[](Node &self, uint32_t val) { self.agent_id(val); },
"read or assign a new value to the agent_id attribute.")
.def_property("attrs", [](Node &self) -> std::map<std::string, Attribute> & { return self.attrs(); },
[](Node &self, const std::map<std::string, Attribute> &at) { self.attrs(at); },
py::return_value_policy::reference, "read or write in the attribute map of the node.")
.def_property("edges",
[](Node &self) -> std::map<std::pair<uint64_t, std::string>, Edge> & { return self.fano(); },
[](Node &self, const std::map<std::pair<uint64_t, std::string>, Edge> &edges) {
return self.fano(edges);
},
py::return_value_policy::reference, "read or write in the edge map of the node.")
.def("get_edges", [](Node &self){
std::vector<Edge> edges;
edges.reserve(self.fano().size());
for (auto [_, edge] : self.fano()) {
edges.emplace_back(edge);
}
return edges;
});
//DSR DSRGraph class
py::class_<DSRGraph>(m, "DSRGraph")
.def(py::init([&](int root, const std::string &name, int id,
const std::string &dsr_input_file = "",
bool all_same_host = true, int8_t domain_id = 0,
SyncMode sync_mode = SyncMode::CRDT) -> std::unique_ptr<DSRGraph> {
local_agent_id = id;
GraphSettings settings;
settings.agent_id = id;
settings.graph_name = name;
settings.input_file = dsr_input_file;
settings.same_host = all_same_host;
settings.domain_id = domain_id;
settings.signal_mode = SignalMode::Queue;
settings.sync_mode = sync_mode;
auto g = std::make_unique<DSRGraph>(settings);
return g;
}), "root"_a, "name"_a, "id"_a, "dsr_input_file"_a = "",
"all_same_host"_a = true, "domain_id"_a=0, "sync_mode"_a = SyncMode::CRDT, py::call_guard<py::gil_scoped_release>())
.def("get_agent_id", &DSRGraph::get_agent_id, "get agent_id")
.def("get_agent_name", &DSRGraph::get_agent_name, "get agent_id")
.def("get_node", [](DSRGraph &self, uint64_t id) -> std::optional<Node> {
return self.get_node(id);
}, "id"_a, "return the node with the id passed as parameter. Returns None if the node does not exist.")
.def("get_node", [](DSRGraph &self, const std::string &name) -> std::optional<Node> {
return self.get_node(name);
}, "name"_a, "return the node with the name passed as parameter. Returns None if the node does not exist.")
.def("delete_node", static_cast<bool (DSRGraph::*)(uint64_t)>(&DSRGraph::delete_node), "id"_a,
"delete the node with the given id. Returns a bool with the result o the operation.")
.def("delete_node",
static_cast<bool (DSRGraph::*)(const std::basic_string<char> &)>(&DSRGraph::delete_node), "name"_a,
"delete the node with the given name. Returns a bool with the result o the operation.")
.def("insert_node", [](DSRGraph &g, Node &n) -> std::optional<uint64_t> {
return g.insert_node(n);
}, "node"_a,
"Insert in the graph the new node passed as parameter. Returns the id of the node or None if the Node alredy exist in the map.")
.def("update_node", &DSRGraph::update_node<DSR::Node&>, "node"_a, "Update the node in the graph. Returns a bool.")
.def("get_edge", [](DSRGraph &self, const std::string &from, const std::string &to,
const std::string &key) -> std::optional<Edge> {
return self.get_edge(from, to, key);
}, "from"_a, "to"_a, "type"_a,
"Return the edge with the parameters from, to, and type passed as parameter. If the edge does not exist it return None")
.def("get_edge",
[](DSRGraph &self, uint64_t from, uint64_t to, const std::string &key) -> std::optional<Edge> {
return self.get_edge(from, to, key);
}, "from"_a, "to"_a, "type"_a,
"Return the edge with the parameters from, to, and type passed as parameter. If the edge does not exist it return None")
.def("insert_or_assign_edge", &DSRGraph::insert_or_assign_edge<DSR::Edge&>, "edge"_a,
"Insert or updates and edge. returns a bool")
.def("delete_edge", static_cast<bool (DSRGraph::*)(uint64_t, uint64_t,
const std::basic_string<char> &)>(&DSRGraph::delete_edge),
"from"_a, "to"_a, "type"_a, "Removes the edge and returns a bool")
.def("delete_edge",
static_cast<bool (DSRGraph::*)(const std::basic_string<char> &, const std::basic_string<char> &,
const std::basic_string<char> &)>(&DSRGraph::delete_edge), "from"_a,
"to"_a, "type"_a, "Removes the edge and returns a bool")
.def("get_node_root", &DSRGraph::get_node_root, "Return the root node.")
.def("get_nodes_by_type", &DSRGraph::get_nodes_by_type, "type"_a, "Return all the nodes with a given type.")
.def("get_nodes", &DSRGraph::get_nodes, "Returns all nodes")
.def("get_name_from_id", &DSRGraph::get_name_from_id, "id"_a, "Return the name of a node given its id")
.def("get_id_from_name", &DSRGraph::get_id_from_name, "name"_a, "Return the id from a node given its name")
.def("get_edges", &DSRGraph::get_edges, "Return all the edges in the graph")
.def("get_edges_by_type", &DSRGraph::get_edges_by_type, "type"_a, "Return all the edges with a given type.")
.def("get_edges_to_id", &DSRGraph::get_edges_to_id, "id"_a, "Return all the edges that point to the node")
.def("write_to_json_file", &DSRGraph::write_to_json_file, "file"_a, "skip_atts"_a=std::vector<std::string>{}, "Return all the edges that point to the node");
//DSR RT_API class
auto rt_api = py::class_<RT_API>(m, "rt_api");
py::enum_<RT_API::TimeQuery>(rt_api, "time_query")
.value("nearest", RT_API::TimeQuery::Nearest)
.value("interpolated", RT_API::TimeQuery::Interpolated);
rt_api
.def(py::init([](DSRGraph &g) -> std::unique_ptr<RT_API> {
return g.get_rt_api();
}))
.def("insert_or_assign_edge_RT", [](RT_API &self, Node &n, uint64_t to,
const std::vector<float> &translation,
const std::vector<float> &rotation_euler,
std::optional<uint64_t> timestamp
) {
self.insert_or_assign_edge_RT(n, to, translation, rotation_euler, timestamp);
}, "node"_a, "to"_a, "trans"_a, "rot_euler"_a, "timestamp"_a=std::nullopt)
.def_static("get_edge_RT", &RT_API::get_edge_RT, "node"_a, "to"_a, "type_edge"_a="RT")
.def("get_RT_pose_from_parent", [](RT_API &self, Node &e, const std::string &type_edge) -> std::optional<Eigen::Matrix<double, 4, 4>> {
auto tmp = self.get_RT_pose_from_parent(e);
if (tmp.has_value()) {
Eigen::Matrix<double, 4, 4> Trans;
Trans.setIdentity(); // Set to Identity to make bottom row of Matrix 0,0,0,1
Trans.block<3, 3>(0, 0) = tmp.value().rotation();
Trans.block<3, 1>(0, 3) = tmp.value().translation();
return Trans;
} else {
return std::nullopt;
}
}, "node"_a, "type_edge"_a="RT")
.def("get_edge_RT_as_rtmat", [](RT_API &self, Edge &e, std::uint64_t t, RT_API::TimeQuery time_query) -> std::optional<Eigen::Matrix<double, 4, 4>> {
auto tmp = self.get_edge_RT_as_rtmat(e, t, time_query);
if (tmp.has_value()) {
Eigen::Matrix<double, 4, 4> Trans;
Trans.setIdentity(); // Set to Identity to make bottom row of Matrix 0,0,0,1
Trans.block<3, 3>(0, 0) = tmp.value().rotation();
Trans.block<3, 1>(0, 3) = tmp.value().translation();
return Trans;
} else {
return std::nullopt;
}
}, "edge"_a, "timestamp"_a=0, "time_query"_a=RT_API::TimeQuery::Nearest)
.def("get_translation", [](RT_API &self, std::uint64_t node_id, std::uint64_t to, std::uint64_t timestamp, RT_API::TimeQuery time_query)
{
return self.get_translation(node_id, to, timestamp, time_query);
}, "node_id"_a, "to"_a, "timestamp"_a=0, "time_query"_a=RT_API::TimeQuery::Nearest);
py::class_<InnerEigenAPI>(m, "inner_api")
.def(py::init([](DSRGraph &g) -> std::unique_ptr<InnerEigenAPI> {
return g.get_inner_eigen_api();
}), "graph"_a)
.def("transform", static_cast<std::optional<Eigen::Vector3d> (InnerEigenAPI::*)(const std::string &,
const std::string &,
std::uint64_t timestamp,
const std::string &type_edge,
RT_API::TimeQuery time_query)>(&InnerEigenAPI::transform),
"orig"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("transform", static_cast<std::optional<Eigen::Vector3d> (InnerEigenAPI::*)(const std::string &,
const Mat::Vector3d &,
const std::string &,
std::uint64_t timestamp,
const std::string &type_edge,
RT_API::TimeQuery time_query)>(&InnerEigenAPI::transform),
"orig"_a, "vector"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("transform_axis", static_cast<std::optional<Mat::Vector6d> (InnerEigenAPI::*)(const std::string &,
const std::string &,
std::uint64_t timestamp,
const std::string &type_edge,
RT_API::TimeQuery time_query)>(&InnerEigenAPI::transform_axis),
"orig"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("transform_axis",
static_cast<std::optional<Mat::Vector6d> (InnerEigenAPI::*)(const std::string &, const Mat::Vector6d &,
const std::string &,
std::uint64_t timestamp,
const std::string &type_edge,
RT_API::TimeQuery time_query)>(&InnerEigenAPI::transform_axis),
"orig"_a, "vector"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("get_transformation_matrix",
[](InnerEigenAPI &self, const std::string &dest,
const std::string &orig, std::uint64_t timestamp, const std::string &type_edge, RT_API::TimeQuery time_query) -> std::optional<Eigen::Matrix<double, 4, 4>> {
auto tmp = self.get_transformation_matrix(dest, orig, timestamp, type_edge, time_query);
if (tmp.has_value()) {
Eigen::Matrix<double, 4, 4> Trans;
Trans.setIdentity(); // Set to Identity to make bottom row of Matrix 0,0,0,1
Trans.block<3, 3>(0, 0) = tmp.value().rotation();
Trans.block<3, 1>(0, 3) = tmp.value().translation();
return Trans;
} else {
return std::nullopt;
}
}, "orig"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("get_rotation_matrix", &InnerEigenAPI::get_rotation_matrix, "orig"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("get_translation_vector", &InnerEigenAPI::get_translation_vector, "orig"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest)
.def("get_euler_xyz_angles", &InnerEigenAPI::get_euler_xyz_angles, "orig"_a, "dest"_a, "timestamp"_a=0, "type_edge"_a="RT", "time_query"_a=RT_API::TimeQuery::Nearest);
bind_ghistory(m);
/*
py::class_<CameraAPI>(m, "camera_api")
.def(py::init([](DSRGraph &g, Node &cam) -> std::unique_ptr<CameraAPI> {
return g.get_camera_api(cam);
}))
.def_property_readonly("id", [](CameraAPI& self) { return self.get_id();})
.def_property_readonly("focal", [](CameraAPI& self) { return self.get_focal();})
.def_property_readonly("focal_x", [](CameraAPI& self) { return self.get_focal_x();})
.def_property_readonly("focal_y", [](CameraAPI& self) { return self.get_focal_y();})
.def_property_readonly("height", [](CameraAPI& self) { return self.get_height();})
.def_property_readonly("width", [](CameraAPI& self) { return self.get_width();})
.def("get_roi_depth", &CameraAPI::get_roi_depth)
.def("project", &CameraAPI::project)
.def("reload_camera", &CameraAPI::reload_camera)
.def("get_rgb_image", &CameraAPI::get_rgb_image)
.def("get_depth_image",
static_cast<std::optional<std::vector<float>> (CameraAPI::*)()>(&CameraAPI::get_depth_image))
.def("get_depth_image",
static_cast<std::optional<std::reference_wrapper<const std::vector<uint8_t>>> (CameraAPI::*)() const>(&CameraAPI::get_depth_image))
.def("get_pointcloud", &CameraAPI::get_pointcloud)
.def("get_depth_as_gray_image", &CameraAPI::get_depth_as_gray_image)
;
*/
}