Some more cleanup

This commit is contained in:
Alexander Bock
2022-02-03 23:38:42 +01:00
parent 98f8ccc25e
commit 7cdfc4fa7e
6 changed files with 224 additions and 238 deletions
@@ -30,13 +30,6 @@
#include <openspace/engine/globals.h>
#include <openspace/documentation/documentation.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/misc/csvreader.h>
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
using namespace std::chrono;
namespace {
constexpr const std::array<const char*, 8> UniformNames = {
@@ -47,7 +40,7 @@ namespace {
constexpr openspace::properties::Property::PropertyInfo ColorInfo = {
"Color",
"Color",
"The color used to represent the lines"
"The color used to represent the lines."
};
constexpr openspace::properties::Property::PropertyInfo OpacityInfo = {
@@ -57,22 +50,39 @@ namespace {
};
constexpr openspace::properties::Property::PropertyInfo LatitudeThresholdInfo = {
"latitudeThreshold",
"LatitudeThreshold",
"Latitude Threshold",
"Minimum and maximum latitude for all aircraft."
};
constexpr openspace::properties::Property::PropertyInfo LongitudeThresholdInfo = {
"longitudeThreshold",
"LongitudeThreshold",
"Longitude Threshold",
"Minimum and maximum longitude for all aircraft."
};
constexpr openspace::properties::Property::PropertyInfo LineWidthInfo = {
"lineWidth",
"LineWidth",
"Line Width",
"Controls the line width of the bounding box."
};
struct [[codegen::Dictionary(RenderableAirTrafficBound)]] Parameters {
// [[codegen::verbatim(ColorInfo.Description)]]
std::optional<glm::vec3> color [[codegen::color()]];
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> lineWidth [[codegen::greaterequal(1.f)]];
// [[codegen::verbatim(LatitudeThresholdInfo.description)]]
std::optional<glm::vec2> latitudeThreshold
[[codegen::inrange(glm::vec2(-90.f), glm::vec2(90.f))]];
// [[codegen::verbatim(LongitudeThresholdInfo.description)]]
std::optional<glm::vec2> longitudeThreshold
[[codegen::inrange(glm::vec2(-180.f), glm::vec2(180.f))]];
};
#include "renderableairtrafficbound_codegen.cpp"
} // namespace
namespace openspace {
@@ -81,19 +91,12 @@ glm::vec2 RenderableAirTrafficBound::_lat;
glm::vec2 RenderableAirTrafficBound::_lon;
documentation::Documentation RenderableAirTrafficBound::Documentation() {
using namespace documentation;
return {
"Renderable Air Traffic Bound",
"RenderableAirTrafficBound",
{
}
};
return codegen::doc<Parameters>("airtraffic_renderableairtrafficbound");
}
RenderableAirTrafficBound::RenderableAirTrafficBound(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _color(ColorInfo, glm::vec3(1.f, 0.f, 0.f), glm::vec3(0.f), glm::vec3(1.f))
, _opacity(OpacityInfo, 1.f, 0.f, 1.f)
, _lineWidth(LineWidthInfo, 2.f, 1.f, 5.f)
, _latitudeThreshold(
LatitudeThresholdInfo,
@@ -108,10 +111,18 @@ RenderableAirTrafficBound::RenderableAirTrafficBound(const ghoul::Dictionary& di
glm::vec2(180.f)
)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
_color = p.color.value_or(_color);
addProperty(_color);
addProperty(_opacity);
_lineWidth = p.lineWidth.value_or(_lineWidth);
addProperty(_lineWidth);
_latitudeThreshold = p.latitudeThreshold.value_or(_latitudeThreshold);
addProperty(_latitudeThreshold);
_longitudeThreshold = p.longitudeThreshold.value_or(_longitudeThreshold);
addProperty(_longitudeThreshold);
onEnabledChange([&](bool enabled) {
@@ -132,7 +143,6 @@ RenderableAirTrafficBound::RenderableAirTrafficBound(const ghoul::Dictionary& di
};
void RenderableAirTrafficBound::initializeGL() {
glGenVertexArrays(1, &_vertexArray);
glGenBuffers(1, &_vertexBuffer);
@@ -153,7 +163,9 @@ void RenderableAirTrafficBound::initializeGL() {
void RenderableAirTrafficBound::deinitializeGL() {
glDeleteBuffers(1, &_vertexBuffer);
_vertexBuffer = 0;
glDeleteVertexArrays(1, &_vertexArray);
_vertexArray = 0;
global::renderEngine->removeRenderProgram(_shader.get());
_shader = nullptr;
@@ -249,4 +261,3 @@ void RenderableAirTrafficBound::updateBuffers() {
}
} // namespace openspace
@@ -67,7 +67,6 @@ public:
private:
properties::FloatProperty _lineWidth;
properties::Vec3Property _color;
properties::FloatProperty _opacity;
properties::Vec2Property _latitudeThreshold;
properties::Vec2Property _longitudeThreshold;
@@ -25,23 +25,13 @@
#include <modules/airtraffic/rendering/renderableairtraffichistorical.h>
#include <modules/airtraffic/rendering/renderableairtrafficbound.h>
#include <openspace/documentation/documentation.h>
#include <openspace/query/query.h>
#include <openspace/util/updatestructures.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/engine/globals.h>
#include <openspace/documentation/documentation.h>
#include <ghoul/filesystem/filesystem.h>
#include <iostream>
#include <ghoul/misc/csvreader.h>
#include <fstream>
#include <string>
#include <time.h>
#include <charconv>
#include <mutex>
std::mutex mutexLock;
using namespace std::chrono;
namespace ghoul::filesystem { class File; }
@@ -56,40 +46,129 @@ namespace {
"time", "cameraPosition", "modelTransform", "clipping"
};
constexpr openspace::properties::Property::PropertyInfo OpacityInfo = {
"Opacity",
"Opacity",
"The opacity of the lines used to represent aircraft."
};
constexpr openspace::properties::Property::PropertyInfo RenderedFlightsInfo = {
"RenderedFlights",
"Rendered Flights",
"The rendered number of flights."
};
} // namespace
namespace openspace {
RenderableAirTrafficHistorical::Date::Date(double timeNow) {
std::time_t date = static_cast<time_t>(timeNow + 365.25 * 24 * 60 * 60 * 30);
tm* tempTime = gmtime(&date);
year = tempTime->tm_year + 1900;
month = tempTime->tm_mon + 1;
day = tempTime->tm_mday;
}
RenderableAirTrafficHistorical::Date RenderableAirTrafficHistorical::Date::getTomorrow() {
const int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
// Just blindly add a day with no checks.
Date tomorrow;
tomorrow.year = year;
tomorrow.month = month;
tomorrow.day = day + 1;
// Allow Feb 29 in leap year if needed.
if (tomorrow.month == 2 && tomorrow.day == 29) {
if (tomorrow.year % 400 == 0) {
return tomorrow;
}
if ((tomorrow.year % 4 == 0) && (tomorrow.year % 100 != 0)) {
return tomorrow;
}
}
// Catch rolling into new month.
if (tomorrow.day > days[tomorrow.month - 1]) {
tomorrow.day = 1;
tomorrow.month++;
// Catch rolling into new year.
if (tomorrow.month == 13) {
tomorrow.month = 1;
tomorrow.year++;
}
}
return tomorrow;
}
RenderableAirTrafficHistorical::Date RenderableAirTrafficHistorical::Date::getYesterday()
{
const int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
// Just blindly add a day with no checks
Date yesterday;
yesterday.year = year;
yesterday.month = month;
yesterday.day = day - 1;
// Catch rolling into new month
if (yesterday.day == 0) {
yesterday.month--;
yesterday.day = days[yesterday.month];
// Allow Feb 29 in leap year if needed
if (yesterday.month == 2) {
if (yesterday.year % 400 == 0) {
yesterday.day = 29;
return yesterday;
}
if ((yesterday.year % 4 == 0) && (yesterday.year % 100 != 0)) {
yesterday.day = 29;
return yesterday;
}
}
// Catch rolling into new year
if (yesterday.month == 0) {
yesterday.month = 12;
yesterday.year--;
}
}
return yesterday;
}
bool RenderableAirTrafficHistorical::Date::operator==(const Date& d) const {
return (year == d.year && month == d.month && day == d.day);
}
bool RenderableAirTrafficHistorical::Date::operator!=(const Date& d) const {
return !(*this == d);
}
RenderableAirTrafficHistorical::Date&
RenderableAirTrafficHistorical::Date::operator=(const Date& d)
{
year = d.year;
month = d.month;
day = d.day;
return *this;
}
documentation::Documentation RenderableAirTrafficHistorical::Documentation() {
using namespace documentation;
return {
"Renderable Air Traffic Historical",
"RenderableAirTrafficHistorical",
{
}
"airtraffic_renderableairtraffichistorical",
{}
};
}
RenderableAirTrafficHistorical::RenderableAirTrafficHistorical(
const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _opacity(OpacityInfo, 0.4f, 0.f, 1.f)
, _nRenderedFlights(RenderedFlightsInfo, 0, 0, 10000)
{
addProperty(_opacity);
_nRenderedFlights.setReadOnly(true);
addProperty(_nRenderedFlights);
@@ -121,15 +200,22 @@ void RenderableAirTrafficHistorical::initializeGL() {
void RenderableAirTrafficHistorical::deinitializeGL() {
glDeleteBuffers(1, &_bufferA.vertexBuffer);
_bufferA.vertexBuffer = 0;
glDeleteVertexArrays(1, &_bufferA.vertexArray);
_bufferA.vertexArray = 0;
glDeleteBuffers(1, &_bufferB.vertexBuffer);
_bufferB.vertexBuffer = 0;
glDeleteVertexArrays(1, &_bufferB.vertexArray);
_bufferB.vertexArray = 0;
glDeleteBuffers(1, &_bufferC.vertexBuffer);
_bufferC.vertexBuffer = 0;
glDeleteVertexArrays(1, &_bufferC.vertexArray);
_bufferC.vertexArray = 0;
glDeleteBuffers(1, &_ssbo);
_ssbo = 0;
global::renderEngine->removeRenderProgram(_shader.get());
_shader = nullptr;
@@ -146,7 +232,7 @@ void RenderableAirTrafficHistorical::render(const RenderData& data, RendererTask
double timeNow = data.time.j2000Seconds();
Date inDate = Date(timeNow);
bool reverseTime = _lastUpdate > timeNow;
const bool reverseTime = _lastUpdate > timeNow;
// SSBO
glBindBuffer(GL_SHADER_STORAGE_BUFFER, _ssbo);
@@ -200,7 +286,9 @@ void RenderableAirTrafficHistorical::render(const RenderData& data, RendererTask
else if ((inDate != _currentDate && !_isDataLoading && !reverseTime) ||
(inDate != _nextDate && !_isDataLoading && reverseTime))
{
_bufferA.date = Date(); _bufferB.date = Date(); _bufferC.date = Date();
_bufferA.date = Date();
_bufferB.date = Date();
_bufferC.date = Date();
if (!reverseTime) {
_currentDate = inDate;
@@ -221,9 +309,12 @@ void RenderableAirTrafficHistorical::render(const RenderData& data, RendererTask
}
// Check if new data finished loading. Update buffers ONLY if finished
if (_future.valid() && _future.wait_for(seconds(0)) == std::future_status::ready) {
if (_future.valid() &&
_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
_future.get();
// As the asynchronous stage is finished, send to glBindBuffer on the main thread and context
// As the asynchronous stage is finished, send to glBindBuffer on the main thread
// and context
sendToGLBuffer(
_bufferA.date == _nextNextDate ?
_bufferA :
@@ -289,7 +380,7 @@ void RenderableAirTrafficHistorical::render(const RenderData& data, RendererTask
glDepthFunc(GL_ALWAYS);
// Draw the two buffers with dates that are either current or next
if (_bufferA.date ==_currentDate || _bufferA.date == _nextDate) {
if (_bufferA.date == _currentDate || _bufferA.date == _nextDate) {
// Draw A
glBindVertexArray(_bufferA.vertexArray);
glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(_bufferA.vertexBufferData.size()));
@@ -329,19 +420,25 @@ void RenderableAirTrafficHistorical::updateBuffersReverse(const Date& date, bool
if (_bufferA.date != _nextDate && _bufferA.date != _nextNextDate) {
// Fill A
fillBuffer(_bufferA);
if (!async) sendToGLBuffer(_bufferA);
if (!async) {
sendToGLBuffer(_bufferA);
}
_bufferA.date = date;
}
else if (_bufferB.date != _nextDate && _bufferB.date != _nextNextDate) {
// Fill B
fillBuffer(_bufferB);
if (!async) sendToGLBuffer(_bufferB);
if (!async) {
sendToGLBuffer(_bufferB);
}
_bufferB.date = date;
}
else {
// Fill C
fillBuffer(_bufferC);
if (!async) sendToGLBuffer(_bufferC);
if (!async) {
sendToGLBuffer(_bufferC);
}
_bufferC.date = date;
}
@@ -363,19 +460,25 @@ void RenderableAirTrafficHistorical::updateBuffers(const Date& date, bool async)
if (_bufferA.date != _currentDate && _bufferA.date != _nextDate) {
// Fill A
fillBuffer(_bufferA);
if(!async) sendToGLBuffer(_bufferA);
if (!async) {
sendToGLBuffer(_bufferA);
}
_bufferA.date = date;
}
else if (_bufferB.date != _currentDate && _bufferB.date != _nextDate) {
// Fill B
fillBuffer(_bufferB);
if (!async) sendToGLBuffer(_bufferB);
if (!async) {
sendToGLBuffer(_bufferB);
}
_bufferB.date = date;
}
else {
// Fill C
fillBuffer(_bufferC);
if (!async) sendToGLBuffer(_bufferC);
if (!async) {
sendToGLBuffer(_bufferC);
}
_bufferC.date = date;
}
@@ -460,7 +563,7 @@ void RenderableAirTrafficHistorical::sendToGLBuffer(Buffer& buffer) {
2,
GL_INT,
sizeof(AircraftVBOLayout),
reinterpret_cast<GLvoid*>(2 * sizeof(GL_FLOAT))
reinterpret_cast<GLvoid*>(2 * sizeof(float))
);
glBindVertexArray(0);
@@ -27,16 +27,10 @@
#include <openspace/rendering/renderable.h>
#include <openspace/util/time.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/scalar/intproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/properties/vector/vec2property.h>
#include <openspace/properties/optionproperty.h>
#include <ghoul/misc/csvreader.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/opengl/uniformcache.h>
#include <future>
#include <iomanip>
namespace ghoul::filesystem { class File; }
@@ -49,112 +43,6 @@ namespace openspace {
namespace documentation { struct Documentation; }
// Defines a date with only year, month, and day.
// Also defines operator==, operator!= and operator=
struct Date {
int year = 0;
int month = 0;
int day = 0;
Date() = default;
Date(double timeNow) {
std::time_t date = static_cast<time_t>(timeNow + 365.25 * 24 * 60 * 60 * 30);
tm* tempTime = gmtime(&date);
year = tempTime->tm_year + 1900;
month = tempTime->tm_mon + 1;
day = tempTime->tm_mday;
}
Date getTomorrow() {
const int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
// Just blindly add a day with no checks.
Date tomorrow;
tomorrow.year = year;
tomorrow.month = month;
tomorrow.day = day + 1;
// Allow Feb 29 in leap year if needed.
if (tomorrow.month == 2 && tomorrow.day == 29) {
if (tomorrow.year % 400 == 0)
return tomorrow;
if ((tomorrow.year % 4 == 0) && (tomorrow.year % 100 != 0))
return tomorrow;
}
// Catch rolling into new month.
if (tomorrow.day > days[tomorrow.month - 1]) {
tomorrow.day = 1;
tomorrow.month++;
// Catch rolling into new year.
if (tomorrow.month == 13) {
tomorrow.month = 1;
tomorrow.year++;
}
}
return tomorrow;
}
Date getYesterday() {
const int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
// Just blindly add a day with no checks
Date yesterday;
yesterday.year = year;
yesterday.month = month;
yesterday.day = day - 1;
// Catch rolling into new month
if (yesterday.day == 0) {
yesterday.month--;
yesterday.day = days[yesterday.month];
// Allow Feb 29 in leap year if needed
if (yesterday.month == 2) {
if (yesterday.year % 400 == 0) {
yesterday.day = 29;
return yesterday;
}
if ((yesterday.year % 4 == 0) && (yesterday.year % 100 != 0)) {
yesterday.day = 29;
return yesterday;
}
}
// Catch rolling into new year
if (yesterday.month == 0) {
yesterday.month = 12;
yesterday.year--;
}
}
return yesterday;
}
bool operator==(const Date& d) const {
return (year == d.year && month == d.month && day == d.day);
}
bool operator!=(const Date& d) const {
return !(*this == d);
}
Date& operator=(const Date& d) {
year = d.year;
month = d.month;
day = d.day;
return *this;
}
};
class RenderableAirTrafficHistorical : public Renderable {
public:
explicit RenderableAirTrafficHistorical(const ghoul::Dictionary& dictionary);
@@ -166,15 +54,34 @@ public:
bool isReady() const override;
void render(const RenderData& data, RendererTasks& rendererTask) override;
bool fetchData(const Date& date);
void updateBuffers(const Date& date, bool async = false);
void updateBuffersReverse(const Date& date, bool async = false);
static documentation::Documentation Documentation();
private:
// Defines a date with only year, month, and day.
// Also defines operator==, operator!= and operator=
struct Date {
int year = 0;
int month = 0;
int day = 0;
Date() = default;
Date(double timeNow);
Date& operator=(const Date& d);
Date getTomorrow();
Date getYesterday();
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
};
bool fetchData(const Date& date);
void updateBuffers(const Date& date, bool async = false);
void updateBuffersReverse(const Date& date, bool async = false);
// Stores some number outside the latitude and longitude
// interval used for validation
static const int _THRESHOLD = -9999;
@@ -202,7 +109,6 @@ private:
void sendToGLBuffer(Buffer& buffer);
// GUI properties
properties::FloatProperty _opacity;
properties::IntProperty _nRenderedFlights;
// Initializing backend storage for
@@ -25,21 +25,12 @@
#include <modules/airtraffic/rendering/renderableairtrafficlive.h>
#include <modules/airtraffic/rendering/renderableairtrafficbound.h>
#include <openspace/engine/downloadmanager.h>
#include <openspace/util/updatestructures.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/engine/globals.h>
#include <openspace/documentation/documentation.h>
#include <openspace/util/httprequest.h>
#include <ghoul/filesystem/filesystem.h>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <future>
#include <sstream>
using namespace std::chrono;
namespace ghoul::filesystem { class File; }
@@ -50,8 +41,8 @@ namespace ghoul::opengl {
namespace {
constexpr const std::array<const char*, 8> UniformNames = {
"modelViewProjection", "trailSize", "resolution", "lineWidth", "color",
"opacity", "latitudeThreshold", "longitudeThreshold"
"modelViewProjection", "trailSize", "resolution", "lineWidth", "color", "opacity",
"latitudeThreshold", "longitudeThreshold"
};
constexpr openspace::properties::Property::PropertyInfo URLPathInfo = {
@@ -84,54 +75,36 @@ namespace {
"The number of aircraft in traffic right now. This value is not affected by "
"filtering."
};
struct [[codegen::Dictionary(RenderableAirTrafficLive)]] Parameters {
// [[codegen::verbatim(ColorInfo.description)]]
std::optional<glm::vec3> color [[codegen::color()]];
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> lineWidth [[codegen::greaterequal(1.f)]];
};
#include "renderableairtrafficlive_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation RenderableAirTrafficLive::Documentation() {
using namespace documentation;
return {
"Renderable Air Traffic",
"renderableairtraffic",
{
{
URLPathInfo.identifier,
new StringVerifier,
Optional::No,
URLPathInfo.description
},
{
LineWidthInfo.identifier,
new DoubleVerifier,
Optional::Yes,
LineWidthInfo.description
},
{
ColorInfo.identifier,
new Vector3Verifier<double>,
Optional::Yes,
ColorInfo.description
},
{
OpacityInfo.identifier,
new DoubleVerifier,
Optional::Yes,
OpacityInfo.description
},
}
};
return codegen::doc<Parameters>("airtraffic_renderableairtrafficlive");
}
RenderableAirTrafficLive::RenderableAirTrafficLive(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _lineWidth(LineWidthInfo, 14.14f, 1.f, 30.f) // default, min, max
, _lineWidth(LineWidthInfo, 14.14f, 1.f, 30.f)
, _color(ColorInfo, glm::vec3(1.f, 0.f, 0.f), glm::vec3(0.f), glm::vec3(1.f))
, _opacity(OpacityInfo, 1.f, 0.f, 1.f)
, _nRenderedAircraft(RenderedAircraftsInfo, 0, 0, 10000)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
_lineWidth = p.lineWidth.value_or(_lineWidth);
addProperty(_lineWidth);
_color = p.color.value_or(_color);
addProperty(_color);
addProperty(_opacity);
_nRenderedAircraft.setReadOnly(true);
addProperty(_nRenderedAircraft);
@@ -143,7 +116,6 @@ void RenderableAirTrafficLive::initializeGL() {
glGenVertexArrays(1, &_vertexArray);
glGenBuffers(1, &_vertexBuffer);
// Setup shaders
_shader = global::renderEngine->buildRenderProgram(
"AirTrafficLiveProgram",
absPath("${MODULE_AIRTRAFFIC}/shaders/airtrafficlive_vs.glsl"),
@@ -191,7 +163,9 @@ void RenderableAirTrafficLive::render(const RenderData& data, RendererTasks&) {
}
// Check if new data finished loading. Update buffers ONLY if finished
if (_future.valid() && _future.wait_for(seconds(0)) == std::future_status::ready) {
if (_future.valid() &&
_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
_data = _future.get();
updateBuffers();
LINFOC(
@@ -28,15 +28,12 @@
#include <openspace/rendering/renderable.h>
#include <openspace/json.h>
#include <openspace/properties/optionproperty.h>
#include <ghoul/opengl/uniformcache.h>
#include <openspace/properties/scalar/intproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/properties/vector/vec2property.h>
#include <openspace/util/httprequest.h>
#include <openspace/util/time.h>
#include <ghoul/opengl/uniformcache.h>
#include <future>
#include <list>
namespace ghoul::filesystem { class File; }
@@ -57,25 +54,21 @@ public:
void initializeGL() override;
void deinitializeGL() override;
nlohmann::json fetchData();
bool isReady() const override;
void render(const RenderData& data, RendererTasks& rendererTask) override;
void updateBuffers();
static documentation::Documentation Documentation();
private:
nlohmann::json fetchData();
nlohmann::json parseData(std::string_view data);
void updateBuffers();
static const int _TRAILSIZE = 10;
static const int _THRESHOLD = -9999;
properties::FloatProperty _lineWidth;
properties::Vec3Property _color;
properties::FloatProperty _opacity;
properties::IntProperty _nRenderedAircraft;
struct AircraftVBOLayout {