Merge branch 'master' into issue/2093

# Conflicts:
#	modules/skybrowser/src/targetbrowserpair.cpp
This commit is contained in:
Ylva Selling
2022-10-12 09:14:41 -04:00
626 changed files with 8865 additions and 5169 deletions
+23 -36
View File
@@ -55,9 +55,6 @@ namespace {
};
struct [[codegen::Dictionary(Browser)]] Parameters {
// [[codegen::verbatim(DimensionsInfo.description)]]
std::optional<glm::vec2> dimensions;
// [[codegen::verbatim(UrlInfo.description)]]
std::optional<std::string> url;
@@ -66,7 +63,6 @@ namespace {
};
#include "browser_codegen.cpp"
} // namespace
namespace openspace {
@@ -80,29 +76,21 @@ void Browser::RenderHandler::setTexture(GLuint t) {
}
Browser::Browser(const ghoul::Dictionary& dictionary)
: _browserPixeldimensions(
: _browserDimensions(
DimensionsInfo,
glm::vec2(500.f),
global::windowDelegate->currentSubwindowSize(),
glm::vec2(10.f),
glm::vec2(3000.f)
)
, _url(UrlInfo)
, _reload(ReloadInfo)
{
if (dictionary.hasValue<std::string>(UrlInfo.identifier)) {
_url = dictionary.value<std::string>(UrlInfo.identifier);
}
// Handle target dimension property
const Parameters p = codegen::bake<Parameters>(dictionary);
_url = p.url.value_or(_url);
_browserPixeldimensions = p.dimensions.value_or(_browserPixeldimensions);
glm::vec2 windowDimensions = global::windowDelegate->currentSubwindowSize();
_browserPixeldimensions = windowDimensions;
_url.onChange([this]() { _isUrlDirty = true; });
_browserPixeldimensions.onChange([this]() { _isDimensionsDirty = true; });
_browserDimensions.onChange([this]() { _isDimensionsDirty = true; });
_reload.onChange([this]() { _shouldReload = true; });
// Create browser and render handler
@@ -121,9 +109,9 @@ Browser::Browser(const ghoul::Dictionary& dictionary)
Browser::~Browser() {}
bool Browser::initializeGL() {
void Browser::initializeGL() {
_texture = std::make_unique<ghoul::opengl::Texture>(
glm::uvec3(glm::ivec2(_browserPixeldimensions.value()), 1),
glm::uvec3(glm::ivec2(_browserDimensions.value()), 1),
GL_TEXTURE_2D
);
@@ -131,7 +119,6 @@ bool Browser::initializeGL() {
_browserInstance->initialize();
_browserInstance->loadUrl(_url);
return isReady();
}
void Browser::deinitializeGL() {
@@ -164,11 +151,11 @@ void Browser::update() {
_browserInstance->loadUrl(_url);
_isUrlDirty = false;
}
if (_isDimensionsDirty) {
if (_browserPixeldimensions.value().x > 0 &&
_browserPixeldimensions.value().y > 0)
{
_browserInstance->reshape(_browserPixeldimensions.value());
glm::vec2 dim = _browserDimensions;
if (dim.x > 0 && dim.y > 0) {
_browserInstance->reshape(dim);
_isDimensionsDirty = false;
}
}
@@ -184,12 +171,16 @@ bool Browser::isReady() const {
}
glm::vec2 Browser::browserPixelDimensions() const {
return _browserPixeldimensions;
return _browserDimensions;
}
// Updates the browser size to match the size of the texture
void Browser::updateBrowserSize() {
_browserPixeldimensions = _texture->dimensions();
_browserDimensions = _texture->dimensions();
}
void Browser::reload() {
_reload.set(true);
}
float Browser::browserRatio() const {
@@ -198,23 +189,19 @@ float Browser::browserRatio() const {
}
void Browser::setCallbackDimensions(const std::function<void(const glm::dvec2&)>& func) {
_browserPixeldimensions.onChange([&]() {
func(_browserPixeldimensions.value());
_browserDimensions.onChange([&]() {
func(_browserDimensions.value());
});
}
void Browser::executeJavascript(const std::string& script) const {
// Make sure that the browser has a main frame
const bool browserExists = _browserInstance && _browserInstance->getBrowser();
const bool frameIsLoaded = browserExists &&
_browserInstance->getBrowser()->GetMainFrame();
bool browserExists = _browserInstance && _browserInstance->getBrowser();
bool frameIsLoaded = browserExists && _browserInstance->getBrowser()->GetMainFrame();
if (frameIsLoaded) {
_browserInstance->getBrowser()->GetMainFrame()->ExecuteJavaScript(
script,
_browserInstance->getBrowser()->GetMainFrame()->GetURL(),
0
);
CefRefPtr<CefFrame> frame = _browserInstance->getBrowser()->GetMainFrame();
frame->ExecuteJavaScript(script, frame->GetURL(), 0);
}
}
@@ -68,6 +68,12 @@ namespace {
"The thickness of the line of the target. The larger number, the thicker line"
};
constexpr openspace::properties::Property::PropertyInfo VerticalFovInfo = {
"VerticalFov",
"Vertical Field Of View",
"The vertical field of view of the target."
};
struct [[codegen::Dictionary(RenderableSkyTarget)]] Parameters {
// [[codegen::verbatim(crossHairSizeInfo.description)]]
std::optional<float> crossHairSize;
@@ -77,6 +83,9 @@ namespace {
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> lineWidth;
// [[codegen::verbatim(VerticalFovInfo.description)]]
std::optional<double> verticalFov;
};
#include "renderableskytarget_codegen.cpp"
@@ -93,6 +102,7 @@ RenderableSkyTarget::RenderableSkyTarget(const ghoul::Dictionary& dictionary)
, _crossHairSize(crossHairSizeInfo, 2.f, 1.f, 10.f)
, _showRectangleThreshold(RectangleThresholdInfo, 5.f, 0.1f, 70.f)
, _lineWidth(LineWidthInfo, 13.f, 1.f, 100.f)
, _verticalFov(VerticalFovInfo, 10.0, 0.00000000001, 70.0)
, _borderColor(220, 220, 220)
{
// Handle target dimension property
@@ -104,7 +114,12 @@ RenderableSkyTarget::RenderableSkyTarget(const ghoul::Dictionary& dictionary)
_showRectangleThreshold = p.rectangleThreshold.value_or(_showRectangleThreshold);
addProperty(_showRectangleThreshold);
_lineWidth = p.lineWidth.value_or(_lineWidth);
addProperty(_lineWidth);
_verticalFov= p.verticalFov.value_or(_verticalFov);
_verticalFov.setReadOnly(true);
addProperty(_verticalFov);
}
void RenderableSkyTarget::bindTexture() {}
@@ -151,6 +166,7 @@ void RenderableSkyTarget::render(const RenderData& data, RendererTasks&) {
_shader->setUniform("ratio", _ratio);
_shader->setUniform("lineColor", color);
_shader->setUniform("fov", static_cast<float>(_verticalFov));
_shader->setUniform("borderRadius", static_cast<float>(_borderRadius));
glm::dvec3 objectPositionWorld = glm::dvec3(
glm::translate(
@@ -231,4 +247,8 @@ void RenderableSkyTarget::setVerticalFov(double fov) {
_verticalFov = fov;
}
void RenderableSkyTarget::setBorderRadius(double radius) {
_borderRadius = radius;
}
} // namespace openspace
@@ -116,9 +116,10 @@ ScreenSpaceSkyBrowser::ScreenSpaceSkyBrowser(const ghoul::Dictionary& dictionary
addProperty(_isHidden);
addProperty(_url);
addProperty(_browserPixeldimensions);
addProperty(_browserDimensions);
addProperty(_reload);
addProperty(_textureQuality);
addProperty(_verticalFov);
_textureQuality.onChange([this]() { _textureDimensionsIsDirty = true; });
@@ -198,9 +199,10 @@ void ScreenSpaceSkyBrowser::updateTextureResolution() {
float newResX = newResY * _ratio;
glm::vec2 newSize = glm::vec2(newResX , newResY) * _textureQuality.value();
_browserPixeldimensions = glm::ivec2(newSize);
_browserDimensions = glm::ivec2(newSize);
_texture->setDimensions(glm::ivec3(newSize, 1));
_objectSize = glm::ivec3(_texture->dimensions());
_radiusIsDirty = true;
}
void ScreenSpaceSkyBrowser::addDisplayCopy(const glm::vec3& raePosition, int nCopies) {
@@ -236,7 +238,9 @@ void ScreenSpaceSkyBrowser::addDisplayCopy(const glm::vec3& raePosition, int nCo
void ScreenSpaceSkyBrowser::removeDisplayCopy() {
if (!_displayCopies.empty()) {
removeProperty(_displayCopies.back().get());
removeProperty(_showDisplayCopies.back().get());
_displayCopies.pop_back();
_showDisplayCopies.pop_back();
}
}
@@ -322,16 +326,22 @@ void ScreenSpaceSkyBrowser::update() {
_isInitialized = false;
}
WwtCommunicator::update();
if (_radiusIsDirty && _isInitialized) {
setBorderRadius(_borderRadius);
_radiusIsDirty = false;
}
ScreenSpaceRenderable::update();
WwtCommunicator::update();
}
void ScreenSpaceSkyBrowser::setVerticalFovWithScroll(float scroll) {
double ScreenSpaceSkyBrowser::setVerticalFovWithScroll(float scroll) {
// Make scroll more sensitive the smaller the FOV
double x = _verticalFov;
double zoomFactor = atan(x / 50.0) + exp(x / 40.0) - 0.99999999999999999999999999999;
double zoom = scroll > 0.0 ? zoomFactor : -zoomFactor;
_verticalFov = std::clamp(_verticalFov + zoom, 0.0, 70.0);
return _verticalFov;
}
void ScreenSpaceSkyBrowser::bindTexture() {
+34 -57
View File
@@ -40,6 +40,22 @@
#include <functional>
#include <chrono>
namespace {
void aimTargetGalactic(std::string id, glm::dvec3 direction) {
glm::dvec3 positionCelestial = glm::normalize(direction) *
openspace::skybrowser::CelestialSphereRadius;
std::string script = fmt::format(
"openspace.setPropertyValueSingle('Scene.{}.Translation.Position', {});",
id, ghoul::to_string(positionCelestial)
);
openspace::global::scriptEngine->queueScript(
script,
openspace::scripting::ScriptEngine::RemoteScripting::Yes
);
}
} // namespace
namespace openspace {
TargetBrowserPair::TargetBrowserPair(SceneGraphNode* targetNode,
@@ -53,31 +69,10 @@ TargetBrowserPair::TargetBrowserPair(SceneGraphNode* targetNode,
_targetRenderable = dynamic_cast<RenderableSkyTarget*>(_targetNode->renderable());
}
TargetBrowserPair& TargetBrowserPair::operator=(TargetBrowserPair other) {
std::swap(_targetNode, other._targetNode);
std::swap(_browser, other._browser);
return *this;
}
void TargetBrowserPair::setImageOrder(int i, int order) {
_browser->setImageOrder(i, order);
}
void TargetBrowserPair::aimTargetGalactic(glm::dvec3 direction) {
std::string id = _targetNode->identifier();
glm::dvec3 positionCelestial = glm::normalize(direction) *
skybrowser::CelestialSphereRadius;
std::string script = fmt::format(
"openspace.setPropertyValueSingle('Scene.{}.Translation.Position', {});",
id, ghoul::to_string(positionCelestial)
);
openspace::global::scriptEngine->queueScript(
script,
scripting::ScriptEngine::RemoteScripting::Yes
);
}
void TargetBrowserPair::startFinetuningTarget() {
_startTargetPosition = _targetNode->worldPosition();
}
@@ -90,9 +85,13 @@ void TargetBrowserPair::fineTuneTarget(const glm::vec2& translation)
glm::dvec2 startRaDec = skybrowser::cartesianToSpherical(
skybrowser::galacticToEquatorial(glm::normalize(_startTargetPosition))
);
glm::dvec2 newRaDec = startRaDec + glm::dvec2(translation);
glm::dvec3 newCartesian = skybrowser::sphericalToCartesian(newRaDec);
aimTargetGalactic(skybrowser::equatorialToGalactic(newCartesian));
aimTargetGalactic(
_targetNode->identifier(),
skybrowser::equatorialToGalactic(newCartesian)
);
}
void TargetBrowserPair::synchronizeAim() {
@@ -108,11 +107,6 @@ void TargetBrowserPair::setEnabled(bool enable) {
_targetRenderable->property("Enabled")->set(enable);
}
void TargetBrowserPair::setOpacity(float opacity) {
_browser->property("Opacity")->set(opacity);
_targetRenderable->property("Opacity")->set(opacity);
}
bool TargetBrowserPair::isEnabled() const {
return _targetRenderable->isEnabled() || _browser->isEnabled();
}
@@ -157,10 +151,6 @@ std::string TargetBrowserPair::targetNodeId() const {
return _targetNode->identifier();
}
float TargetBrowserPair::browserRatio() const {
return _browser->browserRatio();
}
double TargetBrowserPair::verticalFov() const {
return _browser->verticalFov();
}
@@ -182,12 +172,13 @@ ghoul::Dictionary TargetBrowserPair::dataAsDictionary() const {
res.setValue("roll", targetRoll());
res.setValue("color", borderColor());
res.setValue("cartesianDirection", cartesian);
res.setValue("ratio", static_cast<double>(browserRatio()));
res.setValue("ratio", static_cast<double>(_browser->browserRatio()));
res.setValue("isFacingCamera", isFacingCamera());
res.setValue("isUsingRae", isUsingRadiusAzimuthElevation());
res.setValue("selectedImages", selectedImages());
res.setValue("scale", static_cast<double>(_browser->scale()));
res.setValue("opacities", _browser->opacities());
res.setValue("borderRadius", _browser->borderRadius());
std::vector<std::pair<std::string, glm::dvec3>> copies = displayCopies();
std::vector<std::pair<std::string, bool>> showCopies = _browser->showDisplayCopies();
@@ -240,19 +231,10 @@ void TargetBrowserPair::hideChromeInterface() {
void TargetBrowserPair::sendIdToBrowser() const {
_browser->setIdInBrowser();
}
void TargetBrowserPair::updateBrowserSize() {
_browser->updateBrowserSize();
}
std::vector<std::pair<std::string, glm::dvec3>> TargetBrowserPair::displayCopies() const {
return _browser->displayCopies();
}
bool TargetBrowserPair::isImageCollectionLoaded() {
return _browser->isImageCollectionLoaded();
}
void TargetBrowserPair::setVerticalFov(double vfov) {
_browser->setVerticalFov(vfov);
_targetRenderable->setVerticalFov(vfov);
@@ -260,6 +242,7 @@ void TargetBrowserPair::setVerticalFov(double vfov) {
void TargetBrowserPair::setEquatorialAim(const glm::dvec2& aim) {
aimTargetGalactic(
_targetNode->identifier(),
skybrowser::equatorialToGalactic(skybrowser::sphericalToCartesian(aim))
);
_browser->setEquatorialAim(aim);
@@ -270,13 +253,19 @@ void TargetBrowserPair::setBorderColor(const glm::ivec3& color) {
_browser->setBorderColor(color);
}
void TargetBrowserPair::setBorderRadius(double radius) {
_browser->setBorderRadius(radius);
_targetRenderable->setBorderRadius(radius);
}
void TargetBrowserPair::setBrowserRatio(float ratio) {
_browser->setRatio(ratio);
_targetRenderable->setRatio(ratio);
}
void TargetBrowserPair::setVerticalFovWithScroll(float scroll) {
_browser->setVerticalFovWithScroll(scroll);
double fov = _browser->setVerticalFovWithScroll(scroll);
_targetRenderable->setVerticalFov(fov);
}
void TargetBrowserPair::setImageCollectionIsLoaded(bool isLoaded) {
@@ -286,16 +275,16 @@ void TargetBrowserPair::setImageCollectionIsLoaded(bool isLoaded) {
void TargetBrowserPair::incrementallyAnimateToCoordinate() {
// Animate the target before the field of view starts to animate
if (_targetAnimation.isAnimating()) {
aimTargetGalactic(_targetAnimation.getNewValue());
aimTargetGalactic(_targetNode->identifier(), _targetAnimation.newValue());
}
else if (!_targetAnimation.isAnimating() && _targetIsAnimating) {
// Set the finished position
aimTargetGalactic(_targetAnimation.getNewValue());
aimTargetGalactic(_targetNode->identifier(), _targetAnimation.newValue());
_fovAnimation.start();
_targetIsAnimating = false;
}
if (_fovAnimation.isAnimating()) {
_browser->setVerticalFov(_fovAnimation.getNewValue());
_browser->setVerticalFov(_fovAnimation.newValue());
_targetRenderable->setVerticalFov(_browser->verticalFov());
}
}
@@ -369,20 +358,8 @@ bool TargetBrowserPair::isUsingRadiusAzimuthElevation() const {
return _browser->isUsingRaeCoords();
}
SceneGraphNode* TargetBrowserPair::targetNode() const {
return _targetNode;
}
ScreenSpaceSkyBrowser* TargetBrowserPair::browser() const {
return _browser;
}
bool operator==(const TargetBrowserPair& lhs, const TargetBrowserPair& rhs) {
return lhs._targetNode == rhs._targetNode && lhs._browser == rhs._browser;
}
bool operator!=(const TargetBrowserPair& lhs, const TargetBrowserPair& rhs) {
return !(lhs == rhs);
}
} // namespace openspace
+35 -32
View File
@@ -32,6 +32,20 @@
#include <glm/gtx/vector_angle.hpp>
#include <cmath>
namespace {
// Galactic coordinates are projected onto the celestial sphere
// Equatorial coordinates are unit length
// Conversion spherical <-> Cartesian
// Conversion matrix - J2000 equatorial <-> galactic
// https://arxiv.org/abs/1010.3773v1
constexpr glm::dmat3 ConversionMatrix = glm::dmat3(
-0.054875539390, 0.494109453633, -0.867666135681, // col 0
-0.873437104725, -0.444829594298, -0.198076389622, // col 1
-0.483834991775, 0.746982248696, 0.455983794523 // col 2
);
} // namespace
namespace openspace::skybrowser {
// Converts from spherical coordinates in the unit of degrees to cartesian coordianates
@@ -53,26 +67,26 @@ glm::dvec2 cartesianToSpherical(const glm::dvec3& coord) {
double ra = atan2(coord.y, coord.x);
double dec = atan2(coord.z, glm::sqrt((coord.x * coord.x) + (coord.y * coord.y)));
ra = ra > 0 ? ra : ra + glm::two_pi<double>();
ra = ra > 0.0 ? ra : ra + glm::two_pi<double>();
glm::dvec2 celestialCoords = glm::dvec2(ra, dec);
return glm::degrees(celestialCoords);
}
glm::dvec3 galacticToEquatorial(const glm::dvec3& coords) {
return glm::transpose(conversionMatrix) * glm::normalize(coords);
return glm::transpose(ConversionMatrix) * glm::normalize(coords);
}
glm::dvec3 equatorialToGalactic(const glm::dvec3& coords) {
// On the unit sphere
glm::dvec3 rGalactic = conversionMatrix * glm::normalize(coords);
glm::dvec3 rGalactic = ConversionMatrix * glm::normalize(coords);
return rGalactic;
}
glm::dvec3 localCameraToScreenSpace3d(const glm::dvec3& coords) {
// Ensure that if the coord is behind the camera,
// the converted coordinate will be there too
double zCoord = coords.z > 0 ? -ScreenSpaceZ : ScreenSpaceZ;
double zCoord = coords.z > 0.0 ? -ScreenSpaceZ : ScreenSpaceZ;
// Calculate screen space coords x and y
double tanX = coords.x / coords.z;
@@ -91,15 +105,15 @@ glm::dvec3 localCameraToGalactic(const glm::dvec3& coords) {
// Subtract camera position to get the view direction
glm::dvec3 galactic = glm::dvec3(camMat * coordsVec4) - camPos;
return glm::normalize(galactic) * skybrowser::CelestialSphereRadius;
return glm::normalize(galactic) * CelestialSphereRadius;
}
glm::dvec3 localCameraToEquatorial(const glm::dvec3& coords) {
// Calculate the galactic coordinate of the target direction
// projected onto the celestial sphere
glm::dvec3 camPos = global::navigationHandler->camera()->positionVec3();
glm::dvec3 galactic = camPos + skybrowser::localCameraToGalactic(coords);
return skybrowser::galacticToEquatorial(galactic);
glm::dvec3 galactic = camPos + localCameraToGalactic(coords);
return galacticToEquatorial(galactic);
}
glm::dvec3 equatorialToLocalCamera(const glm::dvec3& coords) {
@@ -117,8 +131,10 @@ glm::dvec3 galacticToLocalCamera(const glm::dvec3& coords) {
}
double targetRoll(const glm::dvec3& up, const glm::dvec3& forward) {
glm::dvec3 upJ2000 = skybrowser::galacticToEquatorial(up);
glm::dvec3 forwardJ2000 = skybrowser::galacticToEquatorial(forward);
constexpr glm::dvec3 NorthPole = glm::dvec3(0.0, 0.0, 1.0);
glm::dvec3 upJ2000 = galacticToEquatorial(up);
glm::dvec3 forwardJ2000 = galacticToEquatorial(forward);
glm::dvec3 crossUpNorth = glm::cross(upJ2000, NorthPole);
double dotNorthUp = glm::dot(NorthPole, upJ2000);
@@ -129,14 +145,15 @@ double targetRoll(const glm::dvec3& up, const glm::dvec3& forward) {
glm::dvec3 cameraDirectionEquatorial() {
// Get the view direction of the screen in cartesian J2000 coordinates
return galacticToEquatorial(cameraDirectionGalactic());
glm::dvec3 camDirGalactic = cameraDirectionGalactic();
return galacticToEquatorial(camDirGalactic);
}
glm::dvec3 cameraDirectionGalactic() {
// Get the view direction of the screen in galactic coordinates
glm::dvec3 camPos = global::navigationHandler->camera()->positionVec3();
glm::dvec3 view = global::navigationHandler->camera()->viewDirectionWorldSpace();
glm::dvec3 galCoord = camPos + (skybrowser::CelestialSphereRadius * view);
glm::dvec3 galCoord = camPos + CelestialSphereRadius * view;
return galCoord;
}
@@ -150,11 +167,10 @@ bool isCoordinateInView(const glm::dvec3& equatorial) {
// Check if image coordinate is within current FOV
glm::dvec3 localCamera = equatorialToLocalCamera(equatorial);
glm::dvec3 coordsScreen = localCameraToScreenSpace3d(localCamera);
double r = static_cast<float>(windowRatio());
bool isCoordInView = abs(coordsScreen.x) < r && abs(coordsScreen.y) < 1.f &&
coordsScreen.z < 0;
double r = windowRatio();
bool isCoordInView =
abs(coordsScreen.x) < r && abs(coordsScreen.y) < 1.f && coordsScreen.z < 0.f;
return isCoordInView;
}
@@ -200,31 +216,18 @@ glm::dmat4 incrementalAnimationMatrix(const glm::dvec3& start, const glm::dvec3&
}
double sizeFromFov(double fov, glm::dvec3 worldPosition) {
// Calculate the size with trigonometry
// /|
// /_| Adjacent is the horizontal line, opposite the vertical
// \ | Calculate for half the triangle first, then multiply with 2
// \|
double adjacent = glm::length(worldPosition);
double opposite = 2 * adjacent * glm::tan(glm::radians(fov * 0.5));
double opposite = 2.0 * adjacent * glm::tan(glm::radians(fov * 0.5));
return opposite;
}
template <>
float Animation<float>::getNewValue() {
if (!isAnimating()) {
return _goal;
}
else {
float percentage = static_cast<float>(percentageSpent());
float diff = static_cast<float>((_goal - _start) * ghoul::exponentialEaseOut(percentage));
return _start + diff;
}
}
template <>
double Animation<double>::getNewValue() {
double Animation<double>::newValue() const {
if (!isAnimating()) {
return _goal;
}
@@ -236,7 +239,7 @@ double Animation<double>::getNewValue() {
}
template <>
glm::dmat4 Animation<glm::dvec3>::getRotationMatrix() {
glm::dmat4 Animation<glm::dvec3>::rotationMatrix() {
if (!isAnimating()) {
return glm::dmat4(1.0);
}
@@ -254,7 +257,7 @@ glm::dmat4 Animation<glm::dvec3>::getRotationMatrix() {
}
template <>
glm::dvec3 Animation<glm::dvec3>::getNewValue() {
glm::dvec3 Animation<glm::dvec3>::newValue() const {
if (!isAnimating()) {
return _goal;
}
+165 -136
View File
@@ -32,15 +32,137 @@
namespace {
constexpr std::string_view _loggerCat = "WwtCommunicator";
// WWT messages
ghoul::Dictionary moveCameraMessage(const glm::dvec2& celestCoords, double fov,
double roll)
{
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "center_on_coordinates"s);
msg.setValue("ra", celestCoords.x);
msg.setValue("dec", celestCoords.y);
msg.setValue("fov", fov);
msg.setValue("roll", roll);
msg.setValue("instant", true);
return msg;
}
ghoul::Dictionary loadCollectionMessage(const std::string& url) {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "load_image_collection"s);
msg.setValue("url", url);
msg.setValue("loadChildFolders", true);
return msg;
}
ghoul::Dictionary setForegroundMessage(const std::string& name) {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "set_foreground_by_name"s);
msg.setValue("name", name);
return msg;
}
ghoul::Dictionary addImageMessage(const std::string& id, const std::string& url) {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_create"s);
msg.setValue("id", id);
msg.setValue("url", url);
msg.setValue("mode", "preloaded"s);
msg.setValue("goto", false);
return msg;
}
ghoul::Dictionary removeImageMessage(const std::string& imageId) {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_remove"s);
msg.setValue("id", imageId);
return msg;
}
ghoul::Dictionary setImageOpacityMessage(const std::string& imageId, double opacity) {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_set"s);
msg.setValue("id", imageId);
msg.setValue("setting", "opacity"s);
msg.setValue("value", opacity);
return msg;
}
ghoul::Dictionary setLayerOrderMessage(const std::string& id, int order) {
static int MessageCounter = 0;
// The lower the layer order, the more towards the back the image is placed
// 0 is the background
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_order"s);
msg.setValue("id", id);
msg.setValue("order", order);
msg.setValue("version", MessageCounter);
MessageCounter++;
return msg;
}
constexpr openspace::properties::Property::PropertyInfo VerticalFovInfo = {
"VerticalFov",
"Vertical Field Of View",
"The vertical field of view of the target."
};
struct [[codegen::Dictionary(WwtCommunicator)]] Parameters {
// [[codegen::verbatim(VerticalFovInfo.description)]]
std::optional<double> verticalFov;
};
#include "wwtcommunicator_codegen.cpp"
} // namespace
namespace openspace {
WwtCommunicator::WwtCommunicator(const ghoul::Dictionary& dictionary)
: Browser(dictionary)
{}
, _verticalFov(VerticalFovInfo, 10.0, 0.00000000001, 70.0)
{
// Handle target dimension property
const Parameters p = codegen::bake<Parameters>(dictionary);
_verticalFov = p.verticalFov.value_or(_verticalFov);
_verticalFov.setReadOnly(true);
}
WwtCommunicator::~WwtCommunicator() {}
void WwtCommunicator::update() {
// Cap how messages are passed
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::duration timeSinceLastUpdate = now - _lastUpdateTime;
if (timeSinceLastUpdate > TimeUpdateInterval) {
if (_equatorialAimIsDirty) {
updateAim();
_equatorialAimIsDirty = false;
}
if (_borderColorIsDirty) {
updateBorderColor();
_borderColorIsDirty = false;
}
_lastUpdateTime = std::chrono::system_clock::now();
}
if (_shouldReload) {
_isImageCollectionLoaded = false;
}
Browser::update();
}
void WwtCommunicator::selectImage(const std::string& url, int i) {
// Ensure there are no duplicates
@@ -66,7 +188,6 @@ void WwtCommunicator::addImageLayerToWwt(const std::string& url, int i) {
void WwtCommunicator::removeSelectedImage(int i) {
// Remove from selected list
auto it = findSelectedImage(i);
if (it != _selectedImages.end()) {
_selectedImages.erase(it);
sendMessageToWwt(removeImageMessage(std::to_string(i)));
@@ -74,26 +195,38 @@ void WwtCommunicator::removeSelectedImage(int i) {
}
void WwtCommunicator::sendMessageToWwt(const ghoul::Dictionary& msg) const {
std::string script = "sendMessageToWWT(" + ghoul::formatJson(msg) + ");";
executeJavascript(script);
std::string m = ghoul::formatJson(msg);
executeJavascript(fmt::format("sendMessageToWWT({});", m));
}
std::vector<int> WwtCommunicator::selectedImages() const {
std::vector<int> selectedImagesVector;
for (const std::pair<int, double>& image : _selectedImages) {
selectedImagesVector.push_back(image.first);
}
selectedImagesVector.resize(_selectedImages.size());
std::transform(
_selectedImages.cbegin(),
_selectedImages.cend(),
selectedImagesVector.begin(),
[](const std::pair<int, double>& image) { return image.first; }
);
return selectedImagesVector;
}
std::vector<double> WwtCommunicator::opacities() const {
std::vector<double> opacities;
for (const std::pair<int, double>& image : _selectedImages) {
opacities.push_back(image.second);
}
opacities.resize(_selectedImages.size());
std::transform(
_selectedImages.cbegin(),
_selectedImages.cend(),
opacities.begin(),
[](const std::pair<int, double>& image) { return image.second; }
);
return opacities;
}
double WwtCommunicator::borderRadius() const {
return _borderRadius;
}
void WwtCommunicator::setTargetRoll(double roll) {
_targetRoll = roll;
}
@@ -103,12 +236,6 @@ void WwtCommunicator::setVerticalFov(double vfov) {
_equatorialAimIsDirty = true;
}
void WwtCommunicator::setWebpageBorderColor(glm::ivec3 color) const {
std::string stringColor = fmt::format("{},{},{}", color.x, color.y, color.z);
std::string scr = "document.body.style.backgroundColor = 'rgb(" + stringColor + ")';";
executeJavascript(scr);
}
void WwtCommunicator::setEquatorialAim(glm::dvec2 equatorial) {
_equatorialAim = std::move(equatorial);
_equatorialAimIsDirty = true;
@@ -119,8 +246,18 @@ void WwtCommunicator::setBorderColor(glm::ivec3 color) {
_borderColorIsDirty = true;
}
void WwtCommunicator::setBorderRadius(double radius) {
_borderRadius = radius;
std::string scr = fmt::format("setBorderRadius({});", radius);
executeJavascript(scr);
}
void WwtCommunicator::updateBorderColor() const {
setWebpageBorderColor(_borderColor);
std::string script = fmt::format(
"setBackgroundColor('rgb({},{},{})');",
_borderColor.x, _borderColor.y, _borderColor.z
);
executeJavascript(script);
}
void WwtCommunicator::updateAim() const {
@@ -130,8 +267,9 @@ void WwtCommunicator::updateAim() const {
}
glm::dvec2 WwtCommunicator::fieldsOfView() const {
glm::dvec2 browserFov = glm::dvec2(verticalFov() * browserRatio(), verticalFov());
return browserFov;
const double vFov = verticalFov();
const double hFov = vFov * browserRatio();
return glm::dvec2(hFov, vFov);
}
bool WwtCommunicator::isImageCollectionLoaded() const {
@@ -139,10 +277,11 @@ bool WwtCommunicator::isImageCollectionLoaded() const {
}
std::deque<std::pair<int, double>>::iterator WwtCommunicator::findSelectedImage(int i) {
auto it = std::find_if(_selectedImages.begin(), _selectedImages.end(),
[i](std::pair<int, double>& pair) {
return (pair.first == i);
});
auto it = std::find_if(
_selectedImages.begin(),
_selectedImages.end(),
[i](const std::pair<int, double>& pair) { return pair.first == i; }
);
return it;
}
@@ -186,35 +325,13 @@ void WwtCommunicator::hideChromeInterface() const {
executeJavascript(script);
}
void WwtCommunicator::update() {
// Cap how messages are passed
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::duration timeSinceLastUpdate = now - _lastUpdateTime;
if (timeSinceLastUpdate > TimeUpdateInterval) {
if (_equatorialAimIsDirty) {
updateAim();
_equatorialAimIsDirty = false;
}
if (_borderColorIsDirty) {
updateBorderColor();
_borderColorIsDirty = false;
}
_lastUpdateTime = std::chrono::system_clock::now();
}
if (_shouldReload) {
_isImageCollectionLoaded = false;
}
Browser::update();
}
void WwtCommunicator::setImageCollectionIsLoaded(bool isLoaded) {
_isImageCollectionLoaded = isLoaded;
}
void WwtCommunicator::setIdInBrowser(const std::string& id) const {
// Send ID to it's browser
executeJavascript("setId('" + id + "')");
// Send ID to its browser
executeJavascript(fmt::format("setId('{}')", id));
}
glm::ivec3 WwtCommunicator::borderColor() const {
@@ -225,92 +342,4 @@ double WwtCommunicator::verticalFov() const {
return _verticalFov;
}
// WWT messages
ghoul::Dictionary WwtCommunicator::moveCameraMessage(const glm::dvec2& celestCoords,
double fov, double roll,
bool shouldMoveInstantly) const
{
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "center_on_coordinates"s);
msg.setValue("ra", celestCoords.x);
msg.setValue("dec", celestCoords.y);
msg.setValue("fov", fov);
msg.setValue("roll", roll);
msg.setValue("instant", shouldMoveInstantly);
return msg;
}
ghoul::Dictionary WwtCommunicator::loadCollectionMessage(const std::string& url) const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "load_image_collection"s);
msg.setValue("url", url);
msg.setValue("loadChildFolders", true);
return msg;
}
ghoul::Dictionary WwtCommunicator::setForegroundMessage(const std::string& name) const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "set_foreground_by_name"s);
msg.setValue("name", name);
return msg;
}
ghoul::Dictionary WwtCommunicator::addImageMessage(const std::string& id,
const std::string& url) const
{
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_create"s);
msg.setValue("id", id);
msg.setValue("url", url);
msg.setValue("mode", "preloaded"s);
msg.setValue("goto", false);
return msg;
}
ghoul::Dictionary WwtCommunicator::removeImageMessage(const std::string& imageId) const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_remove"s);
msg.setValue("id", imageId);
return msg;
}
ghoul::Dictionary WwtCommunicator::setImageOpacityMessage(const std::string& imageId,
double opacity) const
{
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_set"s);
msg.setValue("id", imageId);
msg.setValue("setting", "opacity"s);
msg.setValue("value", opacity);
return msg;
}
ghoul::Dictionary WwtCommunicator::setLayerOrderMessage(const std::string& id, int order)
{
// The lower the layer order, the more towards the back the image is placed
// 0 is the background
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_order"s);
msg.setValue("id", id);
msg.setValue("order", order);
msg.setValue("version", messageCounter);
messageCounter++;
return msg;
}
} // namespace openspace
+303 -293
View File
@@ -25,239 +25,316 @@
#include <modules/skybrowser/include/wwtdatahandler.h>
#include <modules/skybrowser/include/utility.h>
#include <modules/space/speckloader.h>
#include <openspace/util/httprequest.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/logging/logmanager.h>
#include <algorithm>
#include <filesystem>
#include <sys/types.h>
#include <sys/stat.h>
#include <string_view>
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-override"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#include <modules/skybrowser/ext/tinyxml2/tinyxml2.h>
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
namespace {
constexpr std::string_view _loggerCat = "WwtDataHandler";
constexpr std::string_view Thumbnail = "Thumbnail";
constexpr std::string_view Name = "Name";
constexpr std::string_view ImageSet = "ImageSet";
constexpr std::string_view Dec = "Dec";
constexpr std::string_view RA = "RA";
constexpr std::string_view Undefined = "";
constexpr std::string_view Folder = "Folder";
constexpr std::string_view Place = "Place";
constexpr std::string_view ThumbnailUrl = "ThumbnailUrl";
constexpr std::string_view Url = "Url";
constexpr std::string_view Credits = "Credits";
constexpr std::string_view CreditsUrl = "CreditsUrl";
constexpr std::string_view ZoomLevel = "ZoomLevel";
constexpr std::string_view DataSetType = "DataSetType";
constexpr std::string_view Sky = "Sky";
bool hasAttribute(const tinyxml2::XMLElement* element, std::string_view name) {
std::string n = std::string(name);
return element->FindAttribute(n.c_str());
}
std::string attribute(const tinyxml2::XMLElement* element, std::string_view name) {
if (hasAttribute(element, name)) {
std::string n = std::string(name);
return element->FindAttribute(n.c_str())->Value();
}
return std::string(Undefined);
}
// Parsing and downloading of wtml files
bool downloadFile(const std::string& url, const std::filesystem::path& destination) {
using namespace openspace;
HttpFileDownload wtmlRoot(url, destination, HttpFileDownload::Overwrite::Yes);
wtmlRoot.start(std::chrono::milliseconds(10000));
return wtmlRoot.wait();
}
bool directoryExists(const std::filesystem::path& path) {
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
}
const tinyxml2::XMLElement* directChildNode(const tinyxml2::XMLElement* node,
std::string_view name)
{
while (node && node->Name() != name) {
node = node->FirstChildElement();
}
return node;
}
const tinyxml2::XMLElement* childNode(const tinyxml2::XMLElement* node,
std::string_view name)
{
const tinyxml2::XMLElement* child = node->FirstChildElement();
// Traverse the children and look at all their first child to find ImageSet
while (child) {
const tinyxml2::XMLElement* imageSet = directChildNode(child, name);
if (imageSet) {
return imageSet;
}
child = child->NextSiblingElement();
}
return nullptr;
}
std::string childNodeContentFromImageSet(const tinyxml2::XMLElement* imageSet,
std::string_view elementName)
{
// Find the thumbnail image url
// The thumbnail is the last node so traverse backwards for speed
std::string n = std::string(elementName);
const tinyxml2::XMLElement* child = imageSet->FirstChildElement(n.c_str());
return child && child->GetText() ? child->GetText() : std::string(Undefined);
}
std::string urlFromPlace(const tinyxml2::XMLElement* place) {
// If the place has a thumbnail url, return it
if (hasAttribute(place, Thumbnail)) {
return attribute(place, Thumbnail);
}
// If the place doesn't have a thumbnail url data attribute,
// Load the image set it stores instead
const tinyxml2::XMLElement* imageSet = childNode(place, ImageSet);
// If there is an imageSet, collect thumbnail url, if it doesn't contain an
// ImageSet, it doesn't have an url
return imageSet ?
childNodeContentFromImageSet(imageSet, ThumbnailUrl) :
std::string(Undefined);
}
bool downloadWtmlFiles(const std::filesystem::path& directory, const std::string& url,
const std::string& fileName)
{
using namespace openspace;
// Download file from url
std::filesystem::path file = directory.string() + fileName + ".aspx";
const bool success = downloadFile(url, file);
if (!success) {
LINFO(fmt::format(
"Could not download file '{}' to directory {}", url, directory
));
return false;
}
// Parse file to XML
auto document = std::make_unique<tinyxml2::XMLDocument>();
document->LoadFile(file.string().c_str());
// Search XML file for folders with urls
const tinyxml2::XMLElement* root = document->RootElement();
const tinyxml2::XMLElement* element = root->FirstChildElement(Folder.data());
const bool folderExists = element != nullptr;
const bool folderContainNoUrls = folderExists && !hasAttribute(element, Url);
// If the file contains no folders, or there are folders but without urls,
// stop recursion
if (!folderExists || folderContainNoUrls) {
LINFO(fmt::format("Saving {}", url));
return true;
}
// Iterate through all the folders in the XML file
while (element && std::string(element->Value()) == Folder) {
// If folder contains urls, download and parse those urls
if (hasAttribute(element, Url) && hasAttribute(element, Name)) {
std::string urlAttr = attribute(element, Url);
std::string fileNameAttr = attribute(element, Name);
downloadWtmlFiles(directory, urlAttr, fileNameAttr);
}
element = element->NextSiblingElement();
}
return true;
}
std::optional<openspace::ImageData> loadImageFromNode(
const tinyxml2::XMLElement* node,
std::string collection)
{
using namespace openspace;
// Collect the image set of the node. The structure is different depending on if
// it is a Place or an ImageSet
std::string thumbnailUrl = std::string(Undefined);
const tinyxml2::XMLElement* imageSet = nullptr;
std::string type = node->Name();
if (type == ImageSet) {
thumbnailUrl = childNodeContentFromImageSet(node, ThumbnailUrl);
imageSet = node;
}
else if (type == Place) {
thumbnailUrl = urlFromPlace(node);
imageSet = childNode(node, ImageSet);
}
// Only collect the images that have a thumbnail image, that are sky images and
// that have an image
const bool hasThumbnailUrl = thumbnailUrl != Undefined;
const bool isSkyImage = attribute(node, DataSetType) == Sky;
const bool hasImageUrl = imageSet ? hasAttribute(imageSet, Url) : false;
if (!(hasThumbnailUrl && isSkyImage && hasImageUrl)) {
return std::nullopt;
}
// Collect name, image url and credits
std::string name = attribute(node, Name);
if (std::islower(name[0])) {
// convert string to upper case
name[0] = static_cast<char>(std::toupper(name[0]));
}
std::string imageUrl = attribute(imageSet, Url);
std::string credits = childNodeContentFromImageSet(imageSet, Credits);
std::string creditsUrl = childNodeContentFromImageSet(imageSet, CreditsUrl);
// Collect equatorial coordinates. All-sky surveys do not have these coordinates
bool hasCelestialCoords = hasAttribute(node, RA) && hasAttribute(node, Dec);
glm::dvec2 equatorialSpherical = glm::dvec2(0.0);
glm::dvec3 equatorialCartesian = glm::dvec3(0.0);
if (hasCelestialCoords) {
// The RA from WWT is in the unit hours:
// to convert to degrees, multiply with 360 (deg) /24 (h) = 15
double ra = 15.0 * std::stod(attribute(node, RA));
double dec = std::stod(attribute(node, Dec));
equatorialSpherical = glm::dvec2(ra, dec);
equatorialCartesian = skybrowser::sphericalToCartesian(equatorialSpherical);
}
// Collect field of view. The WWT definition of ZoomLevel is: VFOV = ZoomLevel / 6
float fov = 0.f;
if (hasAttribute(node, ZoomLevel)) {
fov = std::stof(attribute(node, ZoomLevel)) / 6.f;
}
return ImageData{
name,
thumbnailUrl,
imageUrl,
credits,
creditsUrl,
collection,
hasCelestialCoords,
fov,
equatorialSpherical,
equatorialCartesian
};
}
} //namespace
namespace openspace {
bool hasAttribute(const tinyxml2::XMLElement* element, const std::string_view& name) {
return element->FindAttribute(std::string(name).c_str());
}
std::string attribute(const tinyxml2::XMLElement* element, const std::string& name) {
if (hasAttribute(element, name)) {
return element->FindAttribute(name.c_str())->Value();
}
return wwt::Undefined;
}
// Parsing and downloading of wtml files
bool downloadFile(const std::string& url, const std::filesystem::path& fileDestination) {
// Get the web page and save to file
HttpFileDownload wtmlRoot(
url,
fileDestination,
HttpFileDownload::Overwrite::Yes
);
wtmlRoot.start(std::chrono::milliseconds(10000));
return wtmlRoot.wait();
}
bool directoryExists(const std::filesystem::path& path) {
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
}
std::string createSearchableString(std::string str) {
// Remove white spaces and all special characters
str.erase(
std::remove_if(
str.begin(), str.end(),
[](char c) {
const bool isNumberOrLetter = std::isdigit(c) || std::isalpha(c);
return !isNumberOrLetter;
}
),
str.end()
);
// Make the word lower case
std::transform(
str.begin(), str.end(),
str.begin(),
[](char c) { return static_cast<char>(std::tolower(c)); }
);
return str;
}
tinyxml2::XMLElement* getDirectChildNode(tinyxml2::XMLElement* node,
const std::string& name)
{
while (node && node->Name() != name) {
node = node->FirstChildElement();
}
return node;
}
tinyxml2::XMLElement* getChildNode(tinyxml2::XMLElement* node,
const std::string& name)
{
tinyxml2::XMLElement* child = node->FirstChildElement();
// Traverse the children and look at all their first child to find ImageSet
while (child) {
tinyxml2::XMLElement* imageSet = getDirectChildNode(child, name);
// Found
if (imageSet) {
return imageSet;
}
child = child->NextSiblingElement();
}
return nullptr;
}
std::string getChildNodeContentFromImageSet(tinyxml2::XMLElement* imageSet,
const std::string& elementName)
{
// Find the thumbnail image url
// The thumbnail is the last node so traverse backwards for speed
tinyxml2::XMLElement* imageSetChild =
imageSet->FirstChildElement(elementName.c_str());
if (imageSetChild && imageSetChild->GetText()) {
return imageSetChild->GetText();
}
else {
return wwt::Undefined;
}
}
std::string getUrlFromPlace(tinyxml2::XMLElement* place) {
// If the place has a thumbnail url, return it
if (hasAttribute(place, wwt::Thumbnail)) {
return attribute(place, wwt::Thumbnail);
}
// If the place doesn't have a thumbnail url data attribute,
// Load the image set it stores instead
tinyxml2::XMLElement* imageSet = getChildNode(place, wwt::ImageSet);
// If there is an imageSet, collect thumbnail url
if (imageSet) {
return getChildNodeContentFromImageSet(imageSet, wwt::ThumbnailUrl);
}
else {
// If it doesn't contain an ImageSet, it doesn't have an url
return wwt::Undefined;
}
}
void parseWtmlsFromDisc(std::vector<tinyxml2::XMLDocument*>& xmls,
const std::filesystem::path& directory)
{
for (const auto& entry : std::filesystem::directory_iterator(directory)) {
tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument();
std::string path = entry.path().string();
tinyxml2::XMLError successCode = document->LoadFile(path.c_str());
if (successCode == tinyxml2::XMLError::XML_SUCCESS) {
xmls.push_back(document);
}
}
}
bool downloadAndParseWtmlFilesFromUrl(std::vector<tinyxml2::XMLDocument*>& xmls,
const std::filesystem::path& directory,
const std::string& url, const std::string& fileName)
{
// Look for WWT image data folder, create folder if it doesn't exist
if (!directoryExists(directory)) {
std::string newDir = directory.string();
// Remove the '/' at the end
newDir.pop_back();
LINFO("Creating directory" + newDir);
std::filesystem::create_directory(newDir);
}
// Download file from url
std::filesystem::path file = directory.string() + fileName + ".aspx";
if (!downloadFile(url, file)) {
LINFO(
fmt::format("Couldn't download file '{}' to directory '{}'", url, directory)
);
return false;
}
// Parse file to XML
using namespace tinyxml2;
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
doc->LoadFile(file.string().c_str());
// Search XML file for folders with urls
XMLElement* root = doc->RootElement();
XMLElement* element = root->FirstChildElement(wwt::Folder.c_str());
const bool folderExists = element;
const bool folderContainNoUrls = folderExists && !hasAttribute(element, wwt::Url);
// If the file contains no folders, or there are folders but without urls,
// stop recursion
if (!folderExists || folderContainNoUrls) {
xmls.push_back(doc);
LINFO("Saving " + url);
return true;
}
// Iterate through all the folders in the XML file
while (element && std::string(element->Value()) == wwt::Folder) {
// If folder contains urls, download and parse those urls
if (hasAttribute(element, wwt::Url) && hasAttribute(element, wwt::Name)) {
std::string urlAttr = attribute(element, wwt::Url);
std::string fileNameAttr = attribute(element, wwt::Name);
downloadAndParseWtmlFilesFromUrl(xmls, directory, urlAttr, fileNameAttr);
}
element = element->NextSiblingElement();
}
return true;
}
WwtDataHandler::~WwtDataHandler() {
// Call destructor of all allocated xmls
_xmls.clear();
}
void WwtDataHandler::loadImages(const std::string& root,
const std::filesystem::path& directory)
{
// Collect the wtml files, either by reading from disc or from a url
if (directoryExists(directory) && !std::filesystem::is_empty(directory)) {
parseWtmlsFromDisc(_xmls, directory);
LINFO("Loading images from directory");
}
else {
downloadAndParseWtmlFilesFromUrl(_xmls, directory, root, "root");
LINFO("Loading images from url");
// Steps to download new images
// 1. Create the target directory if it doesn't already exist
// 2. If the 'root' has an associated hash file, download and compare it with the
// local file. If the hash has changed, nuke the folder
// 3. If the folder is empty, download files
// 1.
if (!directoryExists(directory)) {
LINFO(fmt::format("Creating directory {}", directory));
std::filesystem::create_directory(directory);
}
// Traverse through the collected wtml documents and collect the images
for (tinyxml2::XMLDocument* doc : _xmls) {
tinyxml2::XMLElement* rootNode = doc->FirstChildElement();
std::string collectionName = attribute(rootNode, wwt::Name);
saveImagesFromXml(rootNode, collectionName);
// Get the hash from the remote. If no such hash exists, the remoteHash will be empty
std::string remoteHash;
{
std::string remoteHashFile = root.substr(0, root.find_last_of('/')) + "/hash.md5";
bool success = downloadFile(remoteHashFile, directory / "hash.tmp");
// The hash download might fail if the provided 'root' does not have a hash
// in which case we assume that the underlying data has not changed
if (success) {
std::ifstream(directory / "hash.tmp") >> remoteHash;
std::filesystem::remove(directory / "hash.tmp");
}
}
// Load the local hash. If no such hash exists, the localHash will be empty
std::string localHash;
std::filesystem::path localHashFile = directory / "hash.md5";
if (std::filesystem::exists(localHashFile)) {
std::ifstream(localHashFile) >> localHash;
}
// Check if the hash has changed. This will be ignored if either the local of remote
// hash does not exist
if (!localHash.empty() && !remoteHash.empty() && localHash != remoteHash) {
LINFO(fmt::format(
"Local hash '{}' differs from remote hash '{}'. Cleaning directory",
localHash, remoteHash
));
std::filesystem::remove_all(directory);
std::filesystem::create_directory(directory);
}
// If there is no directory (either because it is the first start, or the previous
// contents were deleted because of a change in hash) we have to download the files
if (std::filesystem::is_empty(directory)) {
LINFO("Loading images from url");
downloadWtmlFiles(directory, root, "root");
std::ofstream(localHashFile) << remoteHash;
}
// Finally, we can load the files that are now on disk
LINFO("Loading images from directory");
for (const auto& entry : std::filesystem::directory_iterator(directory)) {
tinyxml2::XMLDocument document;
std::string path = entry.path().string();
tinyxml2::XMLError successCode = document.LoadFile(path.c_str());
if (successCode == tinyxml2::XMLError::XML_SUCCESS) {
tinyxml2::XMLElement* rootNode = document.FirstChildElement();
std::string collectionName = attribute(rootNode, Name);
saveImagesFromXml(rootNode, collectionName);
}
}
// Sort images in alphabetical order
std::sort(
_images.begin(),
_images.end(),
[](ImageData& a, ImageData& b) {
// If the first character in the names are lowercase, make it upper case
if (std::islower(a.name[0])) {
// convert string to upper case
a.name[0] = static_cast<char>(::toupper(a.name[0]));
}
if (std::islower(b.name[0])) {
b.name[0] = static_cast<char>(::toupper(b.name[0]));
}
return a.name < b.name;
}
[](ImageData& a, ImageData& b) { return a.name < b.name; }
);
LINFO(fmt::format("Loaded {} WorldWide Telescope images", _images.size()));
@@ -267,100 +344,33 @@ int WwtDataHandler::nLoadedImages() const {
return static_cast<int>(_images.size());
}
const ImageData& WwtDataHandler::getImage(int i) const {
const ImageData& WwtDataHandler::image(int i) const {
ghoul_assert(i < static_cast<int>(_images.size()), "Index outside of vector size");
return _images[i];
}
void WwtDataHandler::saveImageFromNode(tinyxml2::XMLElement* node, std::string collection)
{
// Collect the image set of the node. The structure is different depending on if
// it is a Place or an ImageSet
std::string thumbnailUrl = wwt::Undefined;
tinyxml2::XMLElement* imageSet = nullptr;
std::string type = std::string(node->Name());
if (type == wwt::ImageSet) {
thumbnailUrl = getChildNodeContentFromImageSet(node, wwt::ThumbnailUrl);
imageSet = node;
}
else if (type == wwt::Place) {
thumbnailUrl = getUrlFromPlace(node);
imageSet = getChildNode(node, wwt::ImageSet);
}
// Only collect the images that have a thumbnail image, that are sky images and
// that have an image
const bool hasThumbnailUrl = thumbnailUrl != wwt::Undefined;
const bool isSkyImage = attribute(node, wwt::DataSetType) == wwt::Sky;
const bool hasImageUrl = imageSet ? hasAttribute(imageSet, wwt::Url) : false;
if (!(hasThumbnailUrl && isSkyImage && hasImageUrl)) {
return;
}
// Collect name, image url and credits
std::string name = attribute(node, wwt::Name);
std::string imageUrl = attribute(imageSet, wwt::Url);
std::string credits = getChildNodeContentFromImageSet(imageSet, wwt::Credits);
std::string creditsUrl = getChildNodeContentFromImageSet(imageSet, wwt::CreditsUrl);
// Collect equatorial coordinates. All-sky surveys do not have this kind of
// coordinate
bool hasCelestialCoords = hasAttribute(node, wwt::RA) && hasAttribute(node, wwt::Dec);
glm::dvec2 equatorialSpherical = glm::dvec2(0.0);
glm::dvec3 equatorialCartesian = glm::vec3(0.0);
if (hasCelestialCoords) {
// The RA from WWT is in the unit hours:
// to convert to degrees, multiply with 360 (deg) /24 (h) = 15
double ra = 15.0 * std::stod(attribute(node, wwt::RA));
double dec = std::stod(attribute(node, wwt::Dec));
equatorialSpherical = glm::dvec2(ra, dec);
equatorialCartesian = skybrowser::sphericalToCartesian(equatorialSpherical);
}
// Collect field of view. The WWT definition of ZoomLevel is: VFOV = ZoomLevel / 6
float fov = 0.f;
if (hasAttribute(node, wwt::ZoomLevel)) {
fov = std::stof(attribute(node, wwt::ZoomLevel)) / 6.0f;
}
ImageData image = {
name,
thumbnailUrl,
imageUrl,
credits,
creditsUrl,
collection,
hasCelestialCoords,
fov,
equatorialSpherical,
equatorialCartesian
};
_images.push_back(image);
}
void WwtDataHandler::saveImagesFromXml(tinyxml2::XMLElement* root, std::string collection)
void WwtDataHandler::saveImagesFromXml(const tinyxml2::XMLElement* root,
std::string collection)
{
// Get direct child of node called Place
using namespace tinyxml2;
XMLElement* node = root->FirstChildElement();
const tinyxml2::XMLElement* node = root->FirstChildElement();
// Iterate through all siblings of node. If sibling is folder, open recursively.
// If sibling is image, save it.
while (node) {
const std::string name = node->Name();
// If node is an image or place, load it
if (name == wwt::ImageSet || name == wwt::Place) {
saveImageFromNode(node, collection);
if (name == ImageSet || name == Place) {
std::optional<ImageData> image = loadImageFromNode(node, collection);
if (image.has_value()) {
_images.push_back(std::move(*image));
}
}
// If node is another folder, open recursively
else if (name == wwt::Folder) {
std::string newCollectionName = collection + "/";
newCollectionName += attribute(node, wwt::Name);
else if (name == Folder) {
std::string nodeName = attribute(node, Name);
std::string newCollectionName = fmt::format("{}/{}", collection, nodeName);
saveImagesFromXml(node, newCollectionName);
}
node = node->NextSiblingElement();