Add new API functions and load images in a cleaner way from module instead of lua

This commit is contained in:
Ylva Selling
2021-05-12 12:14:18 +02:00
parent 810319ace7
commit d8d8cbf2fd
7 changed files with 138 additions and 97 deletions

View File

@@ -2,6 +2,7 @@
#define __OPENSPACE_MODULE_SKYBROWSER___SCREENSPACESKYBROWSER___H__
#include <modules/webbrowser/include/screenspacebrowser.h>
#include <modules/skybrowser/include/wwtdatahandler.h>
#include <openspace/properties/stringproperty.h>
namespace openspace {
@@ -24,9 +25,9 @@ namespace openspace {
ghoul::Dictionary createMessageForLoadingWWTImgColl(const std::string& url) const;
ghoul::Dictionary createMessageForSettingForegroundWWT(const std::string& name) const;
ghoul::Dictionary createMessageForSettingForegroundOpacityWWT(double val) const;
ghoul::Dictionary createMessageForAddingImageLayerWWT(const std::string& url);
ghoul::Dictionary createMessageForRemovingImageLayerWWT(int id) const;
ghoul::Dictionary createMessageForSettingOpacityLayerWWT(int id, double opacity) const;
ghoul::Dictionary createMessageForAddingImageLayerWWT(ImageData& url);
ghoul::Dictionary createMessageForRemovingImageLayerWWT(const std::string& id) const;
ghoul::Dictionary createMessageForSettingOpacityLayerWWT(const ImageData& id, double opacity) const;
bool sendMessageToWWT(const ghoul::Dictionary& msg);
void sendMouseEvent(CefStructBase<CefMouseEventTraits> event, int x, int y) const;
@@ -66,7 +67,7 @@ namespace openspace {
// For capping the calls to change the zoom from scrolling
constexpr static const std::chrono::milliseconds TimeUpdateInterval{ 10 };
std::chrono::system_clock::time_point _lastUpdateTime;
int imageId = 0;
int _imageId{ 0 };
};
}

View File

@@ -54,8 +54,10 @@ namespace openspace::speck {
namespace openspace {
struct ImageData {
static constexpr int NO_ID = -1;
std::string name;
std::string thumbnailUrl;
std::string imageUrl;
std::string credits;
std::string creditsUrl;
glm::dvec2 celestCoords;
@@ -63,6 +65,7 @@ namespace openspace {
float zoomLevel;
bool hasCoords;
glm::dvec3 position3d;
int id{ NO_ID };
};
struct ImageCollection {
@@ -80,20 +83,30 @@ namespace openspace {
bool downloadFile(std::string& url, std::string& fileDestination);
void loadWTMLCollectionsFromURL(std::string url, std::string fileName);
void loadWTMLCollectionsFromDirectory(std::string directory);
int loadAllImagesFromXMLs();
int loadImagesFromLoadedXMLs();
const std::vector<ImageCollection>& getAllImageCollectionUrls() const;
const std::vector<ImageData>& getLoadedImages() const;
std::vector<ImageData>& getLoadedImages();
void loadSpeckData(speck::Dataset& dataset);
private:
void loadImagesFromXML(tinyxml2::XMLElement* node, std::string collectionName);
int loadImage(tinyxml2::XMLElement* imageSet, std::string collectionName);
void setImageDataValues(tinyxml2::XMLElement* node, std::string credits, std::string creditsUrl, std::string thumbnail, std::string collectionName, ImageData& img);
void loadImagesFromXML(tinyxml2::XMLElement* node,
std::string collectionName);
int loadImageFromXmlNode(tinyxml2::XMLElement* imageSet,
std::string collectionName);
void setImageDataValues(tinyxml2::XMLElement* node,
std::string credits,
std::string creditsUrl,
std::string thumbnail,
std::string collectionName,
std::string imageUrl,
ImageData& img);
std::string getChildNodeContentFromImageSet(tinyxml2::XMLElement* imageSet, std::string elementName);
std::string getChildNodeContentFromImageSet(tinyxml2::XMLElement* imageSet,
std::string elementName);
std::string getURLFromPlace(tinyxml2::XMLElement* place);
tinyxml2::XMLElement* getDirectChildNode(tinyxml2::XMLElement* node, std::string name);
tinyxml2::XMLElement* getDirectChildNode(tinyxml2::XMLElement* node,
std::string name);
tinyxml2::XMLElement* getChildNode(tinyxml2::XMLElement* node, std::string name);
std::vector<ImageData> images;
@@ -101,6 +114,7 @@ namespace openspace {
std::vector<tinyxml2::XMLDocument*> xmls;
// 3D position data loaded from speck files
std::unordered_map<std::string, glm::dvec3> _3dPositions;
int nImagesWith3dPositions = 0;
};
}

View File

@@ -97,8 +97,8 @@ namespace openspace {
"input. An input string should be the name of the system host star"
},
{
"follow",
&skybrowser::luascriptfunctions::followCamera,
"loadImagesToWWT",
&skybrowser::luascriptfunctions::loadImagesToWWT,
{},
"string or list of strings",
"Add one or multiple exoplanet systems to the scene, as specified by the "
@@ -458,6 +458,36 @@ void SkyBrowserModule::setSelectedBrowser(int i) {
int SkyBrowserModule::getSelectedBrowserIndex() {
return selectedBrowser;
}
int SkyBrowserModule::loadImages(const std::string& root, int readingMode) {
// Load speck files for 3D positions
std::filesystem::path globularClusters = absPath("${BASE}/sync/http/digitaluniverse_globularclusters_speck/2/gc.speck");
std::filesystem::path openClusters = absPath("${BASE}/sync/http/digitaluniverse_openclusters_speck/2/oc.speck");
speck::Dataset speckGlobularClusters = speck::loadSpeckFile(globularClusters);
speck::Dataset speckOpenClusters = speck::loadSpeckFile(openClusters);
dataHandler->loadSpeckData(speckGlobularClusters);
dataHandler->loadSpeckData(speckOpenClusters);
int nLoadedImages;
// Read from disc
if (readingMode == FROM_DIRECTORY) {
LINFO("Loading images from directory");
dataHandler->loadWTMLCollectionsFromDirectory(absPath("${MODULE_SKYBROWSER}/WWTimagedata/"));
}
// Reading from url
else if (readingMode == FROM_URL) {
LINFO("Loading images from url");
dataHandler->loadWTMLCollectionsFromURL(root, "root");
}
nLoadedImages = dataHandler->loadImagesFromLoadedXMLs();
LINFO("Loaded " + std::to_string(nLoadedImages) + " WorldWide Telescope images.");
return nLoadedImages;
}
/*
std::vector<documentation::Documentation> SkyBrowserModule::documentations() const {
return {

View File

@@ -48,6 +48,8 @@ class WWTDataHandler;
class SkyBrowserModule : public OpenSpaceModule {
public:
constexpr static const char* Name = "SkyBrowser";
constexpr static const int FROM_DIRECTORY = 0;
constexpr static const int FROM_URL = 1;
SkyBrowserModule();
virtual ~SkyBrowserModule() = default;
@@ -60,6 +62,7 @@ public:
void setSelectedBrowser(ScreenSpaceRenderable* ptr);
void setSelectedBrowser(int i);
int getSelectedBrowserIndex();
int loadImages(const std::string& root, int readingMode = FROM_URL);
scripting::LuaLibrary luaLibrary() const override;
//std::vector<documentation::Documentation> documentations() const override;

View File

@@ -37,40 +37,39 @@ namespace openspace::skybrowser::luascriptfunctions {
ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::selectImage");
const int i = ghoul::lua::value<int>(L, 1);
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
ScreenSpaceSkyBrowser* browser = module->getSkyBrowsers()[module->getSelectedBrowserIndex()];
ScreenSpaceSkyBrowser* selectedBrowser = module->getSkyBrowsers()[module->getSelectedBrowserIndex()];
ScreenSpaceSkyTarget* selectedTarget = selectedBrowser->getSkyTarget();
const ImageData& resultImage = module->getWWTDataHandler()->getLoadedImages()[i];
// Load image collection, if it isn't loaded already
// TODO: Update or remove with new WWT API
const std::vector<ImageCollection>& collections = module->getWWTDataHandler()->getAllImageCollectionUrls();
auto it = std::find_if(collections.begin(), collections.end(), [&](const ImageCollection& coll) {
return coll.name == resultImage.collection;
});
if (!it->loaded) {
browser->sendMessageToWWT(browser->createMessageForLoadingWWTImgColl(it->url));
}
LINFO("Loading image " + resultImage.name);
browser->sendMessageToWWT(browser->createMessageForSettingForegroundWWT(resultImage.name));
browser->sendMessageToWWT(browser->createMessageForSettingForegroundOpacityWWT(100));
// Only move target if the image has coordinates
ImageData& resultImage = module->getWWTDataHandler()->getLoadedImages()[i];
// Load image, if the image has not been loaded yet
if (resultImage.id == ImageData::NO_ID) {
LINFO("Loading image " + resultImage.name);
ghoul::Dictionary msg = selectedBrowser->createMessageForAddingImageLayerWWT(resultImage);
selectedBrowser->sendMessageToWWT(msg);
selectedBrowser->sendMessageToWWT(selectedBrowser->createMessageForSettingOpacityLayerWWT(resultImage, 1.0));
}
// If the image has coordinates, move the target
if (resultImage.hasCoords) {
// Animate the target to the image coord position
// In WWT, the definition of ZoomLevel is: VFOV = ZoomLevel / 6
if (browser->getSkyTarget()) {
browser->getSkyTarget()->unlock();
browser->getSkyTarget()->startAnimation(resultImage.celestCoords, resultImage.zoomLevel / 6);
if (selectedTarget) {
selectedTarget->unlock();
selectedTarget->startAnimation(resultImage.celestCoords, resultImage.zoomLevel / 6);
glm::dvec3 imgCoordsOnScreen = J2000SphericalToScreenSpace(resultImage.celestCoords);
glm::vec2 windowRatio = global::windowDelegate->currentWindowSize();
float r = windowRatio.x / windowRatio.y;
// Check if image coordinate is within current FOV
if (!(abs(imgCoordsOnScreen.x) < r && abs(imgCoordsOnScreen.y) < 1.f && imgCoordsOnScreen.z < 0)
|| imgCoordsOnScreen.z > 0) {
bool coordIsWithinView = (abs(imgCoordsOnScreen.x) < r && abs(imgCoordsOnScreen.y) < 1.f && imgCoordsOnScreen.z < 0);
bool coordIsBehindCamera = imgCoordsOnScreen.z > 0;
// If the coordinate is not in view, rotate camera
if (!coordIsWithinView || coordIsBehindCamera) {
module->startRotation(resultImage.celestCoords);
}
}
}
return 0;
}
@@ -131,20 +130,16 @@ namespace openspace::skybrowser::luascriptfunctions {
return 0;
}
int followCamera(lua_State* L) {
int loadImagesToWWT(lua_State* L) {
// Load images from url
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::followCamera");
LINFO("Loading images from url");
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
std::string root = "https://raw.githubusercontent.com/WorldWideTelescope/wwt-web-client/master/assets/webclient-explore-root.wtml";
std::string hubble = "http://www.worldwidetelescope.org/wwtweb/catalog.aspx?W=hubble";
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::loadImagesToWWT");
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
module->getWWTDataHandler()->loadWTMLCollectionsFromURL(root, "root");
LINFO(std::to_string( module->getWWTDataHandler()->loadAllImagesFromXMLs()));
for (ScreenSpaceSkyBrowser* browser : module->getSkyBrowsers()) {
browser->sendMessageToWWT(browser->createMessageForLoadingWWTImgColl(root));
}
// Load the collections here because here we know that the browser can execute javascript
std::string root = "https://raw.githubusercontent.com/WorldWideTelescope/wwt-web-client/master/assets/webclient-explore-root.wtml";
for (ScreenSpaceSkyBrowser* browser : module->getSkyBrowsers()) {
browser->sendMessageToWWT(browser->createMessageForLoadingWWTImgColl(root));
}
return 1;
}
@@ -152,20 +147,7 @@ namespace openspace::skybrowser::luascriptfunctions {
int moveBrowser(lua_State* L) {
// Load images from local directory
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::moveBrowser");
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
module->getWWTDataHandler()->loadWTMLCollectionsFromDirectory(absPath("${MODULE_SKYBROWSER}/WWTimagedata/"));
std::string noOfLoadedImgs = std::to_string(module->getWWTDataHandler()->loadAllImagesFromXMLs());
LINFO("Loaded " + noOfLoadedImgs + " WorldWide Telescope images.");
ScreenSpaceSkyBrowser* browser = dynamic_cast<ScreenSpaceSkyBrowser*>(global::renderEngine->screenSpaceRenderable("SkyBrowser1"));
// Load all image collection urls
//const std::vector<std::string>& imageUrls = module->getWWTDataHandler()->getAllImageCollectionUrls();
//for (const std::string url : imageUrls) {
// browser->sendMessageToWWT(browser->createMessageForLoadingWWTImgColl(url));
//}
std::string root = "https://raw.githubusercontent.com/WorldWideTelescope/wwt-web-client/master/assets/webclient-explore-root.wtml";
std::string hubble = "http://www.worldwidetelescope.org/wwtweb/catalog.aspx?W=hubble";
browser->sendMessageToWWT(browser->createMessageForLoadingWWTImgColl(hubble));
return 1;
}
@@ -173,22 +155,15 @@ namespace openspace::skybrowser::luascriptfunctions {
// Send image list to GUI
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::getListOfImages");
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
// Load speck files for 3D positions
std::filesystem::path globularClusters = absPath("${BASE}/sync/http/digitaluniverse_globularclusters_speck/2/gc.speck");
std::filesystem::path openClusters = absPath("${BASE}/sync/http/digitaluniverse_openclusters_speck/2/oc.speck");
speck::Dataset speckGlobularClusters = speck::loadSpeckFile(globularClusters);
speck::Dataset speckOpenClusters = speck::loadSpeckFile(openClusters);
std::string root = "https://raw.githubusercontent.com/WorldWideTelescope/wwt-web-client/master/assets/webclient-explore-root.wtml";
std::string hubble = "http://www.worldwidetelescope.org/wwtweb/catalog.aspx?W=hubble";
module->getWWTDataHandler()->loadSpeckData(speckGlobularClusters);
module->getWWTDataHandler()->loadSpeckData(speckOpenClusters);
module->loadImages(root, SkyBrowserModule::FROM_DIRECTORY);
// If no data has been loaded yet, load it!
// If no data has been loaded yet, download the data from the web!
if (module->getWWTDataHandler()->getLoadedImages().size() == 0) {
// Read from disc
//moveBrowser(L);
// Read from URL
followCamera(L);
module->loadImages(root, SkyBrowserModule::FROM_URL);
}
// Create Lua table to send to the GUI

View File

@@ -2,6 +2,7 @@
#include <modules/skybrowser/include/screenspaceskytarget.h>
#include <modules/skybrowser/include/utility.h>
#include <modules/skybrowser/skybrowsermodule.h>
#include <modules/skybrowser/include/wwtdatahandler.h>
#include <modules/webbrowser/include/webkeyboardhandler.h>
#include <modules/webbrowser/include/browserinstance.h>
#include <modules/webbrowser/include/screenspacebrowser.h>
@@ -139,6 +140,7 @@ namespace openspace {
}
bool ScreenSpaceSkyBrowser::initializeGL() {
global::moduleEngine->module<SkyBrowserModule>()->addRenderable(this);
setConnectedTarget();
if (_skyTarget) {
@@ -240,7 +242,6 @@ namespace openspace {
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForLoadingWWTImgColl(const std::string& url) const {
// https://docs.worldwidetelescope.org/data-guide/1/data-file-formats/collections/sample-blank-collection.wtml
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "load_image_collection"s);
@@ -259,32 +260,33 @@ namespace openspace {
return msg;
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForAddingImageLayerWWT(const std::string& url) {
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForAddingImageLayerWWT(ImageData& image) {
std::string idString = std::to_string(_imageId);
using namespace std::string_literals;
ghoul::Dictionary msg;
image.id = _imageId;
msg.setValue("event", "image_layer_create"s);
msg.setValue("id", std::to_string(imageId));
msg.setValue("name", url);
// Update ID to ensure that all ID's are unique
imageId++;
msg.setValue("id", idString);
msg.setValue("url", image.imageUrl);
_imageId++;
return msg;
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForRemovingImageLayerWWT(int id) const {
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForRemovingImageLayerWWT(const std::string& id) const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_remove"s);
msg.setValue("id", std::to_string(id));
msg.setValue("id", id);
return msg;
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForSettingOpacityLayerWWT(int id, double opacity) const {
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForSettingOpacityLayerWWT(const ImageData& image, double opacity) const {
std::string idString = std::to_string(image.id);
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_set"s);
msg.setValue("id", std::to_string(id));
msg.setValue("id", idString);
msg.setValue("setting", "opacity"s);
msg.setValue("value", opacity);

View File

@@ -94,7 +94,7 @@ namespace openspace {
}
int WWTDataHandler::loadAllImagesFromXMLs() {
int WWTDataHandler::loadImagesFromLoadedXMLs() {
for (tinyxml2::XMLDocument* doc : xmls) {
tinyxml2::XMLElement* root = doc->FirstChildElement();
std::string collectionName = root->FindAttribute("Name") ? root->FindAttribute("Name")->Value() : "";
@@ -112,6 +112,8 @@ namespace openspace {
}
return a.name < b.name;
});
LINFO(std::to_string(nImagesWith3dPositions) + " 3D positions were matched in the speck files!");
return images.size();
}
@@ -129,7 +131,7 @@ namespace openspace {
while (ptr) {
// If node is an image or place, load it
if (std::string(ptr->Name()) == "ImageSet" || std::string(ptr->Name()) == "Place") {
loadImage(ptr, collectionName);
loadImageFromXmlNode(ptr, collectionName);
}
// If node is another folder, open recursively
else if (std::string(ptr->Name()) == "Folder") {
@@ -145,38 +147,44 @@ namespace openspace {
}
int WWTDataHandler::loadImage(tinyxml2::XMLElement* node, std::string collectionName) {
int WWTDataHandler::loadImageFromXmlNode(tinyxml2::XMLElement* node, std::string collectionName) {
// Only load "Sky" type images
if (std::string(node->FindAttribute("DataSetType")->Value()) != "Sky")
return -1;
std::string url;
std::string thumbnailUrl;
std::string credits;
std::string creditsUrl;
std::string imageUrl;
tinyxml2::XMLElement* imageSet = nullptr;
// Get url. The thumbnail can be located either in the Place or the ImageSet
if (std::string(node->Name()) == "ImageSet") {
url = getChildNodeContentFromImageSet(node, "ThumbnailUrl");
thumbnailUrl = getChildNodeContentFromImageSet(node, "ThumbnailUrl");
imageSet = node;
}
else if (std::string(node->Name()) == "Place") {
url = getURLFromPlace(node);
thumbnailUrl = getURLFromPlace(node);
imageSet = getChildNode(node, "ImageSet");
}
else {
return -1;
}
// Only load images that have a thumbnail
if (url == "" || !imageSet) {
// Only load images that have a thumbnail image url
if (thumbnailUrl == "" || !imageSet) {
return -1;
}
// The credits are always children nodes of ImageSet
// Only load images that contain a image url
if (!imageSet->FindAttribute("Url")) {
return -1;
}
// The credits and image url are always children nodes of ImageSet
credits = getChildNodeContentFromImageSet(imageSet, "Credits");
creditsUrl = getChildNodeContentFromImageSet(imageSet, "CreditsUrl");
imageUrl = imageSet->FindAttribute("Url")->Value();
ImageData image{};
setImageDataValues(node, credits, creditsUrl, url, collectionName, image);
setImageDataValues(node, credits, creditsUrl, thumbnailUrl, collectionName, imageUrl, image);
images.push_back(image);
// Return index of image in vector
@@ -223,7 +231,13 @@ namespace openspace {
return imageSet;
}
void WWTDataHandler::setImageDataValues(tinyxml2::XMLElement* node, std::string credits, std::string creditsUrl, std::string thumbnail, std::string collectionName, ImageData& img) {
void WWTDataHandler::setImageDataValues(tinyxml2::XMLElement* node,
std::string credits,
std::string creditsUrl,
std::string thumbnail,
std::string collectionName,
std::string imageUrl,
ImageData& img) {
// Get attributes for the image
img.name = node->FindAttribute("Name") ? node->FindAttribute("Name")->Value() : "";
img.hasCoords = node->FindAttribute("RA") && node->FindAttribute("Dec");
@@ -237,14 +251,16 @@ namespace openspace {
img.zoomLevel = node->FindAttribute("ZoomLevel") ? std::stof(node->FindAttribute("ZoomLevel")->Value()) : 0.f;
img.credits = credits;
img.creditsUrl = creditsUrl;
img.imageUrl = imageUrl;
// Look for 3D position in the data loaded from speck files
auto it = _3dPositions.find(img.name);
if (it != _3dPositions.end()) {
img.position3d = it->second;
nImagesWith3dPositions++;
}
}
const std::vector<ImageData>& WWTDataHandler::getLoadedImages() const {
std::vector<ImageData>& WWTDataHandler::getLoadedImages() {
return images;
}