-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImuPositionPlugin.cc
150 lines (123 loc) · 4.25 KB
/
ImuPositionPlugin.cc
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
#include "ImuPositionPlugin.h"
#include <iostream>
#include <QFileDialog>
#include <QMessageBox>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <rbdl_wrapper.h>
#include <parser.hpp>
ImuPositionPlugin::ImuPositionPlugin() {
parentApp = NULL;
}
ImuPositionPlugin::~ImuPositionPlugin() {
}
void ImuPositionPlugin::init(ToolkitApp* app) {
//save reference to parent ToolkitApp
parentApp = app;
load_file_trigger = new QAction("Load Imu Position");
parentApp->addFileAction(load_file_trigger);
connect(load_file_trigger, SIGNAL(triggered(bool)), this, SLOT(action_load_data()));
QCommandLineOption imuposition_option( QStringList() << "imu" << "imuposition",
"Load ImuPosition files <file>",
"file"
);
parentApp->addCmdOption(imuposition_option, [this](QCommandLineParser& parser){
auto data_list = parser.values("imuposition");
for (int i=0; i<data_list.size(); i++) {
if (i < parentApp->getLoadedModels()->size() ) {
auto file = data_list[i];
ImuPositionModelExtension* ext;
try {
ext = loadImuPositionFile(file);
} catch (RigidBodyDynamics::Errors::RBDLError& e){
ToolkitApp::showExceptionDialog(e);
delete ext;
}
RBDLModelWrapper* rbdl_model = parentApp->getLoadedModels()->at(i);
rbdl_model->addExtension(ext);
model_file_map[rbdl_model] = file;
}
}
});
loadImuPositionSettings();
connect(parentApp, &ToolkitApp::reloaded_model, this, &ImuPositionPlugin::reload);
}
void ImuPositionPlugin::loadImuPositionSettings() {
parentApp->toolkit_settings.beginGroup("ImuPositionOptions");
QVariant val = parentApp->toolkit_settings.value("imu.color");
if (val.isNull()) {
imu_color = QColor::fromRgbF(1., 0., 1., 1.);
parentApp->toolkit_settings.setValue("imu.color", imu_color.rgba());
} else {
imu_color = QColor::fromRgba(val.toUInt());
}
parentApp->toolkit_settings.setType("imu.color", imu_color);
val = parentApp->toolkit_settings.value("imu.size");
if (val.isNull()) {
imu_size = 0.1;
parentApp->toolkit_settings.setValue("imu.size", imu_size);
} else {
imu_size = val.toFloat();
}
parentApp->toolkit_settings.setType("imu.size", imu_size);
parentApp->toolkit_settings.endGroup();
}
void ImuPositionPlugin::action_load_data() {
if (parentApp != NULL) {
QFileDialog file_dialog (parentApp, "Select Imu Position File");
file_dialog.setNameFilter(tr("Imu Position File (*.txt)"));
file_dialog.setFileMode(QFileDialog::ExistingFile);
ImuPositionModelExtension* ext;
if (file_dialog.exec()) {
QString filepath;
try {
filepath = file_dialog.selectedFiles().at(0);ext = loadImuPositionFile(filepath);
} catch (RigidBodyDynamics::Errors::RBDLError& e){
ToolkitApp::showExceptionDialog(e);delete ext;
}if (parentApp->getLoadedModels()->size() != 0) {
RBDLModelWrapper* rbdl_model = nullptr;
if (parentApp->getLoadedModels()->size() == 1) {
rbdl_model = parentApp->getLoadedModels()->at(0);
} else {
rbdl_model = parentApp->selectModel(nullptr);
}
if (rbdl_model != nullptr) {
rbdl_model->addExtension(ext);
} else {
delete ext;
}
model_file_map[rbdl_model] = file_dialog.selectedFiles().at(0);
}
}
} else {
//should never happen
throw RigidBodyDynamics::Errors::RBDLError("ImuPositionPlugin was not initialized correctly!");
}
}
void ImuPositionPlugin::reload(RBDLModelWrapper* model) {
for (auto it = model_file_map.begin(); it != model_file_map.end(); it++) {
if ( it->first == model ) {
auto ext = loadImuPositionFile(it->second);
model->addExtension(ext);
}
}
}
ImuPositionModelExtension* ImuPositionPlugin::loadImuPositionFile(QString path) {
ImuPositionModelExtension* extention = new ImuPositionModelExtension(imu_color, imu_size);
std::vector<std::string> names;
std::vector<float> row_values;
std::ifstream file(path.toStdString().c_str(), std::ios_base::in);
aria::csv::CsvParser parser(file);
parser.delimiter(' ');
for (auto& row : parser) {
if ( row.size() == 4 ) {
std::string segment = row[0];
Vector3d pos;
for ( int i=0 ; i<3 ; i++ ) {
pos(i) = QString::fromStdString(row[i+1]).toFloat();
}
extention->addImuPosition(segment, pos);
}
}
return extention;
}