aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorccolin2021-01-02 01:16:06 +0100
committerccolin2021-01-02 01:16:06 +0100
commitbb0da810d945d98b25390c65ab3f34e695914fd6 (patch)
tree7ecbfe570db170b72b7e3096f78c3d60d0bf0acd
parent861d505606d612bc328534dba3257e9ef9a1c269 (diff)
move Drone and Waypoint declarations to their own files
-rw-r--r--projet.pro4
-rw-r--r--src/drone.cc73
-rw-r--r--src/drone.hh37
-rw-r--r--src/drone_controller.cc82
-rw-r--r--src/drone_controller.hh37
-rw-r--r--src/waypoint.cc13
-rw-r--r--src/waypoint.hh17
7 files changed, 145 insertions, 118 deletions
diff --git a/projet.pro b/projet.pro
index d44ae55..0ff7abb 100644
--- a/projet.pro
+++ b/projet.pro
@@ -13,6 +13,8 @@ SOURCES += src/opengl_widget.cc
SOURCES += src/drone_controller.cc
SOURCES += src/load_obj.cc
SOURCES += src/settings_pane.cc
+SOURCES += src/drone.cc
+SOURCES += src/waypoint.cc
HEADERS += src/main_window.hh
HEADERS += src/opengl_mesh.hh
@@ -20,3 +22,5 @@ HEADERS += src/opengl_widget.hh
HEADERS += src/drone_controller.hh
HEADERS += src/load_obj.hh
HEADERS += src/settings_pane.hh
+HEADERS += src/drone.cc
+HEADERS += src/waypoint.cc
diff --git a/src/drone.cc b/src/drone.cc
new file mode 100644
index 0000000..b7e4007
--- /dev/null
+++ b/src/drone.cc
@@ -0,0 +1,73 @@
+#include "drone.hh"
+
+#include "load_obj.hh"
+#include "opengl_widget.hh"
+
+#include <QJsonArray>
+
+
+bool Drone::mesh_initialized = false;
+OpenGLMesh *Drone::mesh = nullptr;
+
+Drone::Drone(int id)
+ :id(id) {
+ if (!mesh_initialized) {
+ QVector<GLfloat> verts = load_obj(":/mdl/dji600.obj", LOAD_OBJ_NORMALS | LOAD_OBJ_UVS);
+ QOpenGLTexture *texture = new QOpenGLTexture(QImage(":/img/dji600.jpg").mirrored());
+ mesh = new OpenGLMesh(verts, texture);
+ mesh_initialized = true;
+ }
+ OpenGLWidget::instance->meshes.append(*mesh);
+ mesh_id = OpenGLWidget::instance->meshes.size() - 1;
+}
+
+
+Drone::Drone(const QJsonObject &json)
+ :Drone(json["id"].toInt()) {
+ 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 = &wp;
+ } else {
+ next = wp.frame;
+ next_wp = &wp;
+ break;
+ }
+ }
+ OpenGLMesh &mesh = OpenGLWidget::instance->meshes[mesh_id];
+ mesh.mat = QMatrix4x4();
+ if (next > -1 && prev == -1) {
+ pos = next_wp->pos;
+ } else if (prev > -1 && next == -1) {
+ pos = prev_wp->pos;
+ } else {
+ pos = lerp(prev_wp->pos, next_wp->pos, (double) (frame-prev) / (next-prev));
+ }
+ mesh.mat.translate(pos);
+}
+
+
+QVector3D Drone::getPos() const {
+ return pos;
+}
+
+
+int Drone::getId() const {
+ return id;
+}
diff --git a/src/drone.hh b/src/drone.hh
new file mode 100644
index 0000000..6c43cc1
--- /dev/null
+++ b/src/drone.hh
@@ -0,0 +1,37 @@
+#ifndef DRONE_HH
+#define DRONE_HH
+
+#include "opengl_mesh.hh"
+#include "waypoint.hh"
+
+#include <QVector>
+#include <QVector3D>
+#include <QJsonObject>
+
+
+template <typename T>
+static T lerp(T a, T b, double factor) {
+ return a + (factor * (b - a));
+}
+
+
+class Drone {
+ static OpenGLMesh *mesh;
+ static bool mesh_initialized;
+
+ QVector<Waypoint> waypoints;
+ int mesh_id;
+ QVector3D pos;
+ int id;
+
+public:
+ Drone(int id);
+ Drone(const QJsonObject &json);
+ const QVector<Waypoint> getWaypoints() const;
+ void setTo(int frame);
+ QVector3D getPos() const;
+ int getId() const;
+};
+
+
+#endif
diff --git a/src/drone_controller.cc b/src/drone_controller.cc
index 6ef78ad..588ac23 100644
--- a/src/drone_controller.cc
+++ b/src/drone_controller.cc
@@ -1,89 +1,7 @@
#include "drone_controller.hh"
#include "opengl_widget.hh"
-#include "load_obj.hh"
#include <QJsonArray>
-#include <QDebug>
-#include <QFile>
-
-
-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(int id)
- :id(id) {
- if (!mesh_initialized) {
- QVector<GLfloat> verts = load_obj(":/mdl/dji600.obj", LOAD_OBJ_NORMALS | LOAD_OBJ_UVS);
- QOpenGLTexture *texture = new QOpenGLTexture(QImage(":/img/dji600.jpg").mirrored());
- mesh = new OpenGLMesh(verts, texture);
- mesh_initialized = true;
- }
- OpenGLWidget::instance->meshes.append(*mesh);
- mesh_id = OpenGLWidget::instance->meshes.size() - 1;
-}
-
-
-Drone::Drone(const QJsonObject &json)
- :Drone(json["id"].toInt()) {
- 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 = &wp;
- } else {
- next = wp.frame;
- next_wp = &wp;
- break;
- }
- }
- OpenGLMesh &mesh = OpenGLWidget::instance->meshes[mesh_id];
- mesh.mat = QMatrix4x4();
- if (next > -1 && prev == -1) {
- pos = next_wp->pos;
- } else if (prev > -1 && next == -1) {
- pos = prev_wp->pos;
- } else {
- pos = lerp(prev_wp->pos, next_wp->pos, (double) (frame-prev) / (next-prev));
- }
- mesh.mat.translate(pos);
-}
-
-
-QVector3D Drone::getPos() const {
- return pos;
-}
-
-
-int Drone::getId() const {
- return id;
-}
DroneController::DroneController(const QJsonObject &json)
diff --git a/src/drone_controller.hh b/src/drone_controller.hh
index 93930a1..08dac42 100644
--- a/src/drone_controller.hh
+++ b/src/drone_controller.hh
@@ -1,47 +1,12 @@
#ifndef DRONE_CONTROLLER_HH
#define DRONE_CONTROLLER_HH
-#include "opengl_mesh.hh"
+#include "drone.hh"
#include <QJsonObject>
-#include <QVector3D>
#include <QTimer>
-template <typename T>
-static T lerp(T a, T b, double factor) {
- return a + (factor * (b - a));
-}
-
-
-struct Waypoint {
- int frame;
- QVector3D pos;
-
- Waypoint(unsigned frame, QVector3D pos);
- Waypoint(const QJsonObject &json);
-};
-
-
-class Drone {
- static OpenGLMesh *mesh;
- static bool mesh_initialized;
-
- QVector<Waypoint> waypoints;
- int mesh_id;
- QVector3D pos;
- int id;
-
-public:
- Drone(int id);
- Drone(const QJsonObject &json);
- const QVector<Waypoint> getWaypoints() const;
- void setTo(int frame);
- QVector3D getPos() const;
- int getId() const;
-};
-
-
class DroneController : public QObject {
Q_OBJECT
diff --git a/src/waypoint.cc b/src/waypoint.cc
new file mode 100644
index 0000000..7acf632
--- /dev/null
+++ b/src/waypoint.cc
@@ -0,0 +1,13 @@
+#include "waypoint.hh"
+
+
+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)) {}
diff --git a/src/waypoint.hh b/src/waypoint.hh
new file mode 100644
index 0000000..4b455fd
--- /dev/null
+++ b/src/waypoint.hh
@@ -0,0 +1,17 @@
+#ifndef WAYPOINT_HH
+#define WAYPOINT_HH
+
+#include <QVector3D>
+#include <QJsonObject>
+
+
+struct Waypoint {
+ int frame;
+ QVector3D pos;
+
+ Waypoint(unsigned frame, QVector3D pos);
+ Waypoint(const QJsonObject &json);
+};
+
+
+#endif