Convert members to properties and initialize with Dictionary

This commit is contained in:
Ylva Selling
2024-07-11 19:03:11 +02:00
parent 9df44eeb6d
commit a3abcc7468
5 changed files with 234 additions and 110 deletions

View File

@@ -32,6 +32,9 @@
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/properties/vector/dvec2property.h>
#include <openspace/properties/list/stringlistproperty.h>
#include <openspace/properties/list/doublelistproperty.h>
namespace openspace {
using SelectedImageDeque = std::deque<std::pair<std::string, double>>;
@@ -49,6 +52,8 @@ public:
void update() override;
float opacity() const noexcept override;
void setAsPaired();
// Images in WorldWide Telescope
void selectImage(const std::string& imageUrl);
void removeSelectedImage(const std::string& imageUrl);
@@ -74,22 +79,17 @@ public:
void hideChromeInterface();
void setBorderColor(glm::ivec3 color);
void setBorderRadius(double radius);
double setVerticalFovWithScroll(float scroll);
void reload();
glm::dvec2 fineTuneVector(const glm::dvec2& drag);
bool shouldUpdateWhileTargetAnimates() const;
// Spacecraft pointing
void setPointSpaceCraft(bool shouldPoint);
bool isPointingSpacecraft() const;
// Initialization process
bool isInitialized() const;
void setIdInBrowser() const;
void loadImageCollection(const std::string& collection);
bool isImageCollectionLoaded() const;
void setImageCollectionIsLoaded(bool isLoaded);
void loadImageCollection(const std::string& collection);
double setVerticalFovWithScroll(float scroll);
void setIdInBrowser() const;
bool isInitialized() const;
void setIsInitialized(bool isInitialized);
// Display copies
@@ -106,22 +106,21 @@ private:
SelectedImageDeque::iterator findSelectedImage(const std::string& imageUrl);
SelectedImageDeque _selectedImages;
int _borderRadiusTimer = -1;
static constexpr int RadiusTimeOut = 25;
double _borderRadius = 0.0;
properties::StringListProperty _selectedImagesUrls;
properties::DoubleListProperty _selectedImagesOpacities;
properties::DoubleProperty _borderRadius;
properties::DoubleProperty _roll;
properties::DVec2Property _equatorialAim;
properties::FloatProperty _textureQuality;
float _lastTextureQuality = 1.f;
properties::BoolProperty _isHidden;
properties::BoolProperty _isPointingSpacecraft;
properties::BoolProperty _updateDuringTargetAnimation;
properties::DoubleProperty _verticalFov;
std::vector<std::unique_ptr<properties::Vec3Property>> _displayCopies;
std::vector<std::unique_ptr<properties::BoolProperty>> _showDisplayCopies;
glm::ivec3 _wwtBorderColor = glm::ivec3(70);
glm::dvec2 _equatorialAim = glm::dvec2(0.0);
double _targetRoll = 0.0;
int _borderRadiusTimer = -1;
static constexpr int RadiusTimeOut = 25;
float _lastTextureQuality = 1.f;
// Flags
bool _borderColorIsDirty = false;

View File

@@ -29,6 +29,9 @@
#include <modules/skybrowser/include/utility.h>
#include <openspace/json.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec3property.h>
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/stringproperty.h>
namespace ghoul { class Dictionary; }
@@ -42,7 +45,7 @@ class ScreenSpaceRenderable;
class TargetBrowserPair : public properties::PropertyOwner {
public:
TargetBrowserPair(SceneGraphNode* target, ScreenSpaceSkyBrowser* browser);
TargetBrowserPair(const ghoul::Dictionary& dictionary);
// Target & Browser
void initialize();
@@ -107,6 +110,11 @@ public:
private:
properties::FloatProperty _lineWidth;
properties::Vec3Property _color;
properties::BoolProperty _isPointingSpacecraft;
properties::BoolProperty _updateDuringTargetAnimation;
properties::StringProperty _browserId;
properties::StringProperty _targetId;
// Target and browser
RenderableSkyTarget* _targetRenderable = nullptr;

View File

@@ -285,14 +285,14 @@ void SkyBrowserModule::addTargetBrowserPair(const std::string& targetId,
return;
}
SceneGraphNode* target = global::renderEngine->scene()->sceneGraphNode(targetId);
ScreenSpaceSkyBrowser* browser = dynamic_cast<ScreenSpaceSkyBrowser*>(
global::renderEngine->screenSpaceRenderable(browserId)
);
ghoul::Dictionary init;
init.setValue("Target", std::string(targetId));
init.setValue("Browser", std::string(browserId));
std::unique_ptr<TargetBrowserPair> pair = std::make_unique<TargetBrowserPair>(init);
// Ensure pair has both target and browser
if (browser && target) {
_targetsBrowsers.push_back(std::make_unique<TargetBrowserPair>(target, browser));
if (pair) {
_targetsBrowsers.push_back(std::move(pair));
}
_uniqueIdentifierCounter++;
addPropertySubOwner(_targetsBrowsers.back().get());

View File

@@ -27,15 +27,13 @@
#include <modules/skybrowser/skybrowsermodule.h>
#include <modules/skybrowser/include/utility.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/windowdelegate.h>
#include <openspace/engine/moduleengine.h>
#include <openspace/engine/windowdelegate.h>
#include <openspace/rendering/renderengine.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/dictionaryjsonformatter.h>
#include <ghoul/opengl/texture.h>
#include <optional>
#include <glm/gtx/color_space.hpp>
#include <random>
namespace {
constexpr openspace::properties::Property::PropertyInfo TextureQualityInfo = {
@@ -72,22 +70,6 @@ namespace {
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo PointSpacecraftInfo = {
"PointSpacecraft",
"Point Spacecraft",
"If checked, spacecrafts will point towards the coordinate of an image upon "
"selection.",
openspace::properties::Property::Visibility::User
};
constexpr openspace::properties::Property::PropertyInfo UpdateDuringAnimationInfo = {
"UpdateDuringTargetAnimation",
"Update During Target Animation",
"If checked, the sky browser display copy will update its coordinates while "
"the target is animating.",
openspace::properties::Property::Visibility::User
};
constexpr openspace::properties::Property::PropertyInfo VerticalFovInfo = {
"VerticalFov",
"Vertical Field Of View",
@@ -95,6 +77,42 @@ namespace {
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo SelectedImagesUrlsInfo = {
"SelectedImagesUrls",
"Selected Images Urls",
"Urls of the images that have been selected for this Sky Browser.",
openspace::properties::Property::Visibility::User
};
constexpr openspace::properties::Property::PropertyInfo SelectedImagesOpacitiesInfo = {
"SelectedImagesOpacities",
"Selected Images Opacities",
"Opacities of the images that have been selected for this Sky Browser.",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo BorderRadiusInfo = {
"BorderRadius",
"Border Radius",
"The border radius of this Sky Browser.",
openspace::properties::Property::Visibility::NoviceUser
};
constexpr openspace::properties::Property::PropertyInfo RollInfo = {
"Roll",
"Roll",
"The roll of the sky browser view.",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo EquatorialAimInfo = {
"EquatrialAim",
"Equatorial Aim",
"The aim of the Sky Browser, given in equatorial coordinates Right Ascension (Ra)"
" and declination (dec).",
openspace::properties::Property::Visibility::AdvancedUser
};
// This `ScreenSpaceRenderable` is used to display a screen space window showing the
// integrated World Wide Telescope view. The view will be dynamically updated when
// interacting with the view or with images in the SkyBrowser panel.
@@ -109,31 +127,25 @@ namespace {
// [[codegen::verbatim(IsHiddenInfo.description)]]
std::optional<bool> isHidden;
// [[codegen::verbatim(PointSpacecraftInfo.description)]]
std::optional<bool> pointSpacecraft;
// [[codegen::verbatim(UpdateDuringAnimationInfo.description)]]
std::optional<bool> updateDuringTargetAnimation;
// [[codegen::verbatim(VerticalFovInfo.description)]]
std::optional<double> verticalFov;
// [[codegen::verbatim(SelectedImagesUrlsInfo.description)]]
std::optional<std::vector<std::string>> selectedImagesUrls;
// [[codegen::verbatim(SelectedImagesOpacitiesInfo.description)]]
std::optional<std::vector<double>> selectedImagesOpacities;
// [[codegen::verbatim(BorderRadiusInfo.description)]]
std::optional<double> borderRadius;
// [[codegen::verbatim(RollInfo.description)]]
std::optional<double> roll;
// [[codegen::verbatim(EquatorialAimInfo.description)]]
std::optional<glm::dvec2> equatorialAim;
};
#include "screenspaceskybrowser_codegen.cpp"
glm::ivec3 randomBorderColor() {
// Generate a random border color with sufficient lightness and a n
std::random_device rd;
// Hue is in the unit degrees [0, 360]
std::uniform_real_distribution<float> hue(0.f, 360.f);
// Value in saturation are in the unit percent [0,1]
constexpr float Value = 0.9f; // Brightness
constexpr float Saturation = 0.5f;
const glm::vec3 hsvColor = glm::vec3(hue(rd), Saturation, Value);
const glm::ivec3 rgbColor = glm::ivec3(glm::rgbColor(hsvColor) * 255.f);
return rgbColor;
}
} // namespace
namespace openspace {
@@ -145,11 +157,15 @@ documentation::Documentation ScreenSpaceSkyBrowser::Documentation() {
ScreenSpaceSkyBrowser::ScreenSpaceSkyBrowser(const ghoul::Dictionary& dictionary)
: ScreenSpaceRenderable(dictionary)
, _wwtCommunicator(dictionary)
, _selectedImagesUrls(SelectedImagesUrlsInfo)
, _selectedImagesOpacities(SelectedImagesOpacitiesInfo)
, _borderRadius(BorderRadiusInfo)
, _roll(RollInfo)
, _equatorialAim(EquatorialAimInfo)
, _verticalFov(VerticalFovInfo, 10.0, 0.00000000001, 70.0)
, _textureQuality(TextureQualityInfo, 1.f, 0.25f, 1.f)
, _isHidden(IsHiddenInfo, true)
, _isPointingSpacecraft(PointSpacecraftInfo, false)
, _updateDuringTargetAnimation(UpdateDuringAnimationInfo, false)
{
_identifier = makeUniqueIdentifier(_identifier);
@@ -158,23 +174,21 @@ ScreenSpaceSkyBrowser::ScreenSpaceSkyBrowser(const ghoul::Dictionary& dictionary
_textureQuality = p.textureQuality.value_or(_textureQuality);
_isHidden = p.isHidden.value_or(_isHidden);
_isPointingSpacecraft = p.pointSpacecraft.value_or(_isPointingSpacecraft);
_updateDuringTargetAnimation = p.updateDuringTargetAnimation.value_or(
_updateDuringTargetAnimation
);
// Handle target dimension property
_verticalFov = p.verticalFov.value_or(_verticalFov);
_verticalFov.setReadOnly(true);
_equatorialAim = p.equatorialAim.value_or(_equatorialAim);
_roll = p.roll.value_or(_roll);
_selectedImagesOpacities = p.selectedImagesOpacities.value_or(_selectedImagesOpacities);
_selectedImagesUrls = p.selectedImagesUrls.value_or(_selectedImagesUrls);
_borderRadius = p.borderRadius.value_or(_borderRadius);
addProperty(_isHidden);
addProperty(_textureQuality);
addProperty(_verticalFov);
addProperty(_isPointingSpacecraft);
addProperty(_updateDuringTargetAnimation);
if (global::windowDelegate->isMaster()) {
_wwtBorderColor = randomBorderColor();
}
addProperty(_equatorialAim);
addProperty(_roll);
addProperty(_selectedImagesOpacities);
addProperty(_selectedImagesUrls);
addProperty(_borderRadius);
_useRadiusAzimuthElevation.onChange(
[this]() {
@@ -201,8 +215,10 @@ ScreenSpaceSkyBrowser::ScreenSpaceSkyBrowser(const ghoul::Dictionary& dictionary
_textureQuality.onChange([this]() {
updateTextureResolution();
});
_lastUpdateTime = std::chrono::system_clock::now();
// Set the size of the renderable to the browser dimensions
_objectSize = glm::ivec3(_wwtCommunicator.browserDimensions(), 1);
}
@@ -238,14 +254,6 @@ bool ScreenSpaceSkyBrowser::isInitialized() const {
return _isInitialized;
}
bool ScreenSpaceSkyBrowser::isPointingSpacecraft() const {
return _isPointingSpacecraft;
}
bool ScreenSpaceSkyBrowser::shouldUpdateWhileTargetAnimates() const {
return _updateDuringTargetAnimation;
}
void ScreenSpaceSkyBrowser::setIdInBrowser() const {
int currentNode = global::windowDelegate->currentNode();
_wwtCommunicator.setIdInBrowser(std::format("{}_{}", identifier(), currentNode));
@@ -255,10 +263,6 @@ void ScreenSpaceSkyBrowser::setIsInitialized(bool isInitialized) {
_isInitialized = isInitialized;
}
void ScreenSpaceSkyBrowser::setPointSpaceCraft(bool shouldPoint) {
_isPointingSpacecraft = shouldPoint;
}
void ScreenSpaceSkyBrowser::updateTextureResolution() {
// Can't divide by zero
if (_lastTextureQuality < glm::epsilon<float>()) {
@@ -320,7 +324,6 @@ std::vector<std::string> ScreenSpaceSkyBrowser::selectedImages() const {
void ScreenSpaceSkyBrowser::setBorderColor(glm::ivec3 color) {
_wwtBorderColor = std::move(color);
_borderColorIsDirty = true;
_borderColor = glm::vec3(color) / 255.f;
}
@@ -441,11 +444,11 @@ void ScreenSpaceSkyBrowser::update() {
if (timeSinceLastUpdate > TimeUpdateInterval) {
if (_equatorialAimIsDirty) {
_wwtCommunicator.setAim(_equatorialAim, _verticalFov, _targetRoll);
_wwtCommunicator.setAim(_equatorialAim, _verticalFov, _roll);
_equatorialAimIsDirty = false;
}
if (_borderColorIsDirty) {
_wwtCommunicator.setBorderColor(_wwtBorderColor);
_wwtCommunicator.setBorderColor(glm::ivec3(_borderColor.value() * 255.f));
_borderColorIsDirty = false;
}
_lastUpdateTime = std::chrono::system_clock::now();
@@ -487,8 +490,21 @@ float ScreenSpaceSkyBrowser::opacity() const noexcept {
return _opacity;
}
// This Sky Browser is now controlled by a TargetBrowserPair
// Therefore we are disabling UI interaction with the properties from the UI
void ScreenSpaceSkyBrowser::setAsPaired() {
// These properties should only be set from the UI, scripting or Target Browser Pair
_verticalFov.setReadOnly(true);
_borderColor.setReadOnly(true);
_roll.setReadOnly(true);
_equatorialAim.setReadOnly(true);
_borderRadius.setReadOnly(true);
_selectedImagesOpacities.setReadOnly(true);
_selectedImagesUrls.setReadOnly(true);
}
glm::ivec3 ScreenSpaceSkyBrowser::borderColor() const {
return _wwtBorderColor;
return glm::ivec3(_borderColor.value() * 255.f);
}
void ScreenSpaceSkyBrowser::removeSelectedImage(const std::string& imageUrl) {
@@ -539,7 +555,7 @@ double ScreenSpaceSkyBrowser::borderRadius() const {
}
void ScreenSpaceSkyBrowser::setTargetRoll(double roll) {
_targetRoll = roll;
_roll = roll;
}
void ScreenSpaceSkyBrowser::setVerticalFov(double vfov) {

View File

@@ -31,14 +31,19 @@
#include <modules/skybrowser/skybrowsermodule.h>
#include <openspace/camera/camera.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/windowdelegate.h>
#include <openspace/engine/moduleengine.h>
#include <openspace/navigation/navigationhandler.h>
#include <openspace/rendering/screenspacerenderable.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/scene/scene.h>
#include <openspace/scripting/scriptengine.h>
#include <ghoul/misc/assert.h>
#include <glm/gtc/constants.hpp>
#include <functional>
#include <chrono>
#include <glm/gtx/color_space.hpp>
#include <random>
using nlohmann::json;
@@ -60,6 +65,20 @@ namespace {
);
}
glm::ivec3 randomBorderColor() {
// Generate a random border color with sufficient lightness and a n
std::random_device rd;
// Hue is in the unit degrees [0, 360]
std::uniform_real_distribution<float> hue(0.f, 360.f);
// Value in saturation are in the unit percent [0,1]
constexpr float Value = 0.9f; // Brightness
constexpr float Saturation = 0.5f;
const glm::vec3 hsvColor = glm::vec3(hue(rd), Saturation, Value);
const glm::ivec3 rgbColor = glm::ivec3(glm::rgbColor(hsvColor) * 255.f);
return rgbColor;
}
constexpr openspace::properties::Property::PropertyInfo LineWidthInfo = {
"LineWidth",
"Line Width",
@@ -67,31 +86,114 @@ namespace {
openspace::properties::Property::Visibility::NoviceUser
};
constexpr openspace::properties::Property::PropertyInfo ColorInfo = {
"Color",
"Color",
"The color of the border of the sky browser and the line of the target.",
openspace::properties::Property::Visibility::NoviceUser
};
constexpr openspace::properties::Property::PropertyInfo PointSpacecraftInfo = {
"PointSpacecraft",
"Point Spacecraft",
"If checked, spacecrafts will point towards the coordinate of an image upon "
"selection.",
openspace::properties::Property::Visibility::User
};
constexpr openspace::properties::Property::PropertyInfo UpdateDuringAnimationInfo = {
"UpdateDuringTargetAnimation",
"Update During Target Animation",
"If checked, the sky browser display copy will update its coordinates while "
"the target is animating.",
openspace::properties::Property::Visibility::User
};
constexpr openspace::properties::Property::PropertyInfo BrowserInfo = {
"Browser",
"Sky Browser",
"The identifier of the sky browser of this pair.",
openspace::properties::Property::Visibility::User
};
constexpr openspace::properties::Property::PropertyInfo TargetInfo = {
"Target",
"Sky Target",
"The identifier of the sky target of this pair.",
openspace::properties::Property::Visibility::User
};
struct [[codegen::Dictionary(TargetBrowserPair)]] Parameters {
// [[codegen::verbatim(LineWidthInfo.description)]]
std::optional<float> lineWidth;
// [[codegen::verbatim(ColorInfo.description)]]
std::optional<float> color;
// [[codegen::verbatim(PointSpacecraftInfo.description)]]
std::optional<bool> pointSpacecraft;
// [[codegen::verbatim(UpdateDuringAnimationInfo.description)]]
std::optional<bool> updateDuringTargetAnimation;
// [[codegen::verbatim(BrowserInfo.description)]]
std::string browser;
// [[codegen::verbatim(TargetInfo.description)]]
std::string target;
};
#include "targetbrowserpair_codegen.cpp"
} // namespace
namespace openspace {
TargetBrowserPair::TargetBrowserPair(SceneGraphNode* targetNode,
ScreenSpaceSkyBrowser* browser)
: properties::PropertyOwner({ "Guacamole" })
, _browser(browser)
, _targetNode(targetNode)
, _lineWidth(LineWidthInfo)
TargetBrowserPair::TargetBrowserPair(const ghoul::Dictionary& dictionary)
: properties::PropertyOwner({ "TargetBrowserPair" })
, _targetId(TargetInfo)
, _browserId(BrowserInfo)
, _lineWidth(LineWidthInfo)
, _color(ColorInfo)
, _isPointingSpacecraft(PointSpacecraftInfo, false)
, _updateDuringTargetAnimation(UpdateDuringAnimationInfo, false)
{
ghoul_assert(browser, "Sky browser is null pointer");
ghoul_assert(targetNode, "Sky target is null pointer");
const Parameters p = codegen::bake<Parameters>(dictionary);
_targetNode = global::renderEngine->scene()->sceneGraphNode(p.target);
_browser = dynamic_cast<ScreenSpaceSkyBrowser*>(
global::renderEngine->screenSpaceRenderable(p.browser)
);
ghoul_assert(_browser, "Sky browser is null pointer");
ghoul_assert(_targetNode, "Sky target is null pointer");
setIdentifier(std::format("{}Pair", _browser->identifier()));
_targetRenderable = dynamic_cast<RenderableSkyTarget*>(_targetNode->renderable());
_isPointingSpacecraft = p.pointSpacecraft.value_or(_isPointingSpacecraft);
_updateDuringTargetAnimation = p.updateDuringTargetAnimation.value_or(
_updateDuringTargetAnimation
);
addProperty(_isPointingSpacecraft);
addProperty(_updateDuringTargetAnimation);
addProperty(_lineWidth);
addProperty(_color);
_lineWidth.onChange([this]() {
_targetRenderable->property("LineWidth")->set(_lineWidth.value());
});
LINFO(uri());
_color.onChange([this]() {
_browser->setBorderColor(glm::ivec3(_color.value() * 255.f));
_targetRenderable->setColor(glm::ivec3(_color.value() * 255.f));
});
if (global::windowDelegate->isMaster()) {
_color = glm::vec3(randomBorderColor()) / 255.f;
}
_browser->setAsPaired();
}
void TargetBrowserPair::setImageOrder(const std::string& imageUrl, int order) {
@@ -99,7 +201,6 @@ void TargetBrowserPair::setImageOrder(const std::string& imageUrl, int order) {
}
void TargetBrowserPair::startFinetuningTarget() {
_startTargetPosition = _targetNode->worldPosition();
}
@@ -117,7 +218,7 @@ void TargetBrowserPair::fineTuneTarget(const glm::vec2& translation) {
void TargetBrowserPair::synchronizeAim() {
const bool shouldUpdate =
_browser->shouldUpdateWhileTargetAnimates() ||
_updateDuringTargetAnimation ||
!_targetAnimation.isAnimating();
if (shouldUpdate && _browser->isInitialized()) {
_browser->setEquatorialAim(targetDirectionEquatorial());
@@ -176,7 +277,7 @@ std::string TargetBrowserPair::targetNodeId() const {
}
bool TargetBrowserPair::pointSpaceCraft() const {
return _browser->isPointingSpacecraft();
return _isPointingSpacecraft;
}
double TargetBrowserPair::verticalFov() const {
@@ -326,7 +427,7 @@ void TargetBrowserPair::applyRoll() {
}
void TargetBrowserPair::setPointSpaceCraft(bool shouldPoint) {
_browser->setPointSpaceCraft(shouldPoint);
_isPointingSpacecraft = shouldPoint;
}
void TargetBrowserPair::incrementallyAnimateToCoordinate() {