168 lines
3.5 KiB
C++
168 lines
3.5 KiB
C++
#include "drone_controller.hh"
|
|
#include "opengl_widget.hh"
|
|
|
|
#define TINYOBJLOADER_IMPLEMENTATION
|
|
#include "tiny_obj_loader.h"
|
|
|
|
#include <QJsonArray>
|
|
#include <QDebug>
|
|
#include <Qt3DRender/QMesh>
|
|
|
|
|
|
Waypoint::Waypoint(unsigned frame, QVector3D pos)
|
|
:frame(frame),
|
|
pos(pos) {}
|
|
|
|
|
|
Waypoint::Waypoint(const QJsonObject &json)
|
|
:Waypoint(json["frame"].toInt(),
|
|
QVector3D(json["position"]["lng_X"].toInt() / 100.0,
|
|
json["position"]["alt_Y"].toInt() / 100.0,
|
|
json["position"]["lat_Z"].toInt() / 100.0)) {}
|
|
|
|
|
|
bool Drone::mesh_initialized = false;
|
|
OpenGLMesh *Drone::mesh = nullptr;
|
|
|
|
Drone::Drone() {
|
|
if (!mesh_initialized) {
|
|
tinyobj::ObjReaderConfig reader_config;
|
|
reader_config.mtl_search_path = "../mdl/"; // Path to material files
|
|
tinyobj::ObjReader reader;
|
|
if (!reader.ParseFromFile("../mdl/dji600.obj", reader_config)) {
|
|
if (!reader.Error().empty()) {
|
|
qWarning() << "Erreur lors de la lecture de ../mdl/dji600.obj";
|
|
}
|
|
exit(1);
|
|
}
|
|
// if (!reader.Warning().empty()) {
|
|
// qWarning() << "TinyObjReader: " << reader.Warning();
|
|
// }
|
|
auto& attrib = reader.GetAttrib();
|
|
mesh = new OpenGLMesh(QVector<float>(attrib.vertices.begin(), attrib.vertices.end()));
|
|
mesh_initialized = true;
|
|
}
|
|
OpenGLWidget::instance->meshes.append(*mesh);
|
|
mesh_id = OpenGLWidget::instance->meshes.size() - 1;
|
|
}
|
|
|
|
|
|
Drone::Drone(const QJsonObject &json)
|
|
:Drone() {
|
|
QJsonArray ja = json["waypoints"].toArray();
|
|
waypoints.reserve(ja.size());
|
|
for (const QJsonValue &o : ja) {
|
|
waypoints.append(Waypoint(o.toObject()));
|
|
}
|
|
}
|
|
|
|
|
|
const QVector<Waypoint> Drone::getWaypoints() const {
|
|
return waypoints;
|
|
}
|
|
|
|
|
|
void Drone::setTo(int frame) {
|
|
int prev = -1, next = -1;
|
|
const Waypoint *prev_wp, *next_wp;
|
|
for (const Waypoint &wp : waypoints) { // TODO: this can be optimized
|
|
if (wp.frame < frame) {
|
|
prev = wp.frame;
|
|
prev_wp = ℘
|
|
} else {
|
|
next = wp.frame;
|
|
next_wp = ℘
|
|
break;
|
|
}
|
|
}
|
|
OpenGLMesh &mesh = OpenGLWidget::instance->meshes[mesh_id];
|
|
mesh.mat = QMatrix4x4();
|
|
if (next > -1 && prev == -1) {
|
|
mesh.mat.translate(next_wp->pos);
|
|
} else if (prev > -1 && next == -1) {
|
|
mesh.mat.translate(prev_wp->pos);
|
|
} else {
|
|
mesh.mat.translate(lerp(prev_wp->pos, next_wp->pos, (double) (frame-prev) / (next-prev)));
|
|
}
|
|
}
|
|
|
|
|
|
DroneController::DroneController(const QJsonObject &json)
|
|
:framerate(json["framerate"].toInt()) {
|
|
|
|
QJsonArray ja = json["drones"].toArray();
|
|
drones.reserve(ja.size());
|
|
for (const QJsonValue &o : ja) {
|
|
drones.append(Drone(o.toObject()));
|
|
}
|
|
for (const Drone &d : drones) {
|
|
for (const Waypoint &wp : d.getWaypoints()) {
|
|
if (wp.frame > duration) duration = wp.frame;
|
|
}
|
|
}
|
|
|
|
connect(&timer, &QTimer::timeout, this, &DroneController::step);
|
|
pause();
|
|
}
|
|
|
|
|
|
int DroneController::getDuration() const {
|
|
return duration;
|
|
}
|
|
|
|
|
|
void DroneController::step() {
|
|
// qDebug("frame %d (%d%%)", frame, (int) ((double) frame / duration * 100));
|
|
for (Drone d : drones) {
|
|
d.setTo(frame);
|
|
}
|
|
OpenGLWidget::instance->update();
|
|
emit frameChanged(frame);
|
|
if (frame == duration) {
|
|
pause();
|
|
} else {
|
|
frame++;
|
|
}
|
|
}
|
|
|
|
|
|
void DroneController::play() {
|
|
if (!paused) return;
|
|
paused = false;
|
|
timer.start(1000. / framerate);
|
|
qDebug() << "playing";
|
|
emit playing();
|
|
}
|
|
|
|
|
|
void DroneController::pause() {
|
|
if (paused) return;
|
|
paused = true;
|
|
timer.stop();
|
|
qDebug() << "pausing";
|
|
emit pausing();
|
|
}
|
|
|
|
|
|
void DroneController::suspend() {
|
|
if (!paused) {
|
|
pause();
|
|
paused = false;
|
|
}
|
|
}
|
|
|
|
|
|
void DroneController::resume() {
|
|
if (!paused) {
|
|
paused = true;
|
|
play();
|
|
}
|
|
}
|
|
|
|
|
|
void DroneController::seek(int frame) {
|
|
if (this->frame == frame) return;
|
|
this->frame = frame;
|
|
step();
|
|
}
|