Files
OpenSpace/modules/space/rendering/renderableconstellationlines.cpp
Emma Broman c165098cdc Point cloud rewrite (RIP RenderableBillboardsCloud) (#2932)
* Start refactoring billboardscloud renderable

* Remove extra unit computation in geometry shader

* More refactor and fix size parameter not working without colormap (it never did)

* Remove redundant bool in shader

* Divide settings into property owners

* Refactor colormap

* More refactoring (utilize properties for visiblity)

* Remove redundant extra data structures for options

* Move data logic to the colormap settings structure

* Remove confusing correction size properties

* Separate polygon cloud into its own renderable

* Clean up some unused code

* Update some property descriptions

* Split some more code out into functions

* Remove odd and unused "linear filtering"

Should be reimplemented ina better way in a general color map overhaul

* Start refactoring some more properties

* Make color optional and remove redundant color assignments in assets

* Refactor color-parameter input and make range mapping work correctly

* Code cleanup (unused code and fix data types)

* Rename ScaleFactor to ScaleExponent

* Add a scale factor as well, as they're easier to use, and clarify documentation

* Fix faultily renamed scale factors

* Remove confusing multiplication for scale exponent

* Fix problem with points not appearing when _drawElements was set to true

* Move all colormapsettings into its own table in the asset files

* Move scaling/sizing into its own table as well

* Group sizing properties and remove min pixel size limit (was always zero)

* Add some docs and rename a parameter to avoid repetition

* Remove IsExact colormap and add an option to hide outliers

OBS! Still need to update the actual cmap files

* Add property to disable size mapping

* Increase pixel size of polyon texture - reduces jagged edges

* Remove unused points class

* Rename files and billboard class, to make it clearer that it is a point cloud

* Move size mapping into size settings

* Move color map properties out into its own component

* Move color map related computations into the new component

* Add outlier colors, to cover some specific color mapping issues

* Automatically fill parameter options if no colormap parameters were defined

* Move all color-related parameters to under one property owner

* Inlcude changes from matrix maintenance

* Add a checkbox to disable additive blending

* Remove a unused variable in shader

* DO color maping on the graphics card instead of CPU, using a texture

* WIP remove old "Outliers" and start introducing concept of no data colors instead

* Move speck loader to core and divide into a general data loader and a speck specific loader (In preparation for adding CSV support)

* More labelscomponent to core

* Fix a faulty header

* Handle NaN values

* Rename color map component property owner

It's not just the color map, but the settings for the entire mapping

* Hide point with nan value is show missing data is not selected

* Fix labels component documentation after move

* Add option to not provide any texture, and use a sphere generated form the shader

* Don't crash when loading unsupported data formats :)

* Add some TODO comments that should be addressed before merge

* Move renderables from DU module to base, and do some header cleanup

* Move colormapping component to core ad rename it to make it clear that it is not the color map itself

* Add above, below and nan color values to cmap loading

* Remove missing Data value specs in asset (and use belowRange in cmap instead)

* Make warning about wrong color number more verbose

* Add TODO comment about colormap, and actually used color in prev version, to localdwarfs

* WIP: Load CSV files

* Update some property visbility

* Check if chosen parameter options actually exist

* Fix fault column check

* Update example asset to cover more different cases

* Use new asset.resource loading

* Update asset versions (major update)

* Add parent documentation for RenderablePolygonCloud

* Add point cloud example with size scaling and add TODO comment

* Add example with textured points

* Don't blend textured points

* Fix problem with shaders being confused about texture units

* Bring back deepsky points color

* Start implementing more sophisticated datamapping for point data files

* Allow reading datamapping from input dictionary

* Load dataset in constructor instead

Solves problem with color map columns not loading for big datasets when loaded the first time

* Fix crash from reading column value that is interpreted as inf

* Inlcude information about data mapping when caching dataset

Updating the parameters now leads to an updated caching of the data file

* Adapt to tolower update

* Add option to specify columns to exclude in load (for CSV files)

* Start updating some docs

* Add a readonly property that shows the number of data points

* Rename example asset to shorter name

* Add option for not using cache when loading dataset

* Fix some errors in example asset

* Set scaleExponent from max position if not specified

That way the points are always visible, even when no exponent is given

* Restructure and add explanations in example asset

* Add option to not cache labels component data file

* Add example file with labels and units

* Compute fade distances in model coordinates instead of world space (issue #1746)

* Add possiblity to invert fading and add exmaple asset

* Make a separate propertyowner for fading settings

* Update docs

* Add example asset with custom data mapping

* Address some of my own review comments

* Read provided Parameter from asset

* Read ValueRange from asset

* Add example asset for more advanced color mapping settings

* Allow using the same column for two position coordinate axes

* Update src/rendering/colormappingcomponent.cpp

Co-authored-by: Alexander Bock <alexander.bock@liu.se>

* Update src/data/datamapping.cpp

* Use helper function for tolowercase

* Simplify some std::optional handling, and fix broken assignment from code review fix

* Update data file versions to get updated cmap files

* Include a progress bar to show progress of CSV data loading

* Update csv data-to-float parsing for clang

* Add a description about why `openspace.absPath` is used in the file instead of `asset.resource`

* Apply suggestions from code review

* Apply suggestions from code review

* Add some missing info in documentation

* Place size-related property descriptions next to each other

* Remove the explicitly set renderbin

* Fix overly complicated description

* Fix polygon texture not being used

* Add polygon cloud example

* only bind texture and activate texture unit when required

* Rename alpha to opacity in shader

* Fix value range not loading, and make first parameter in list the default

* Rename Pixel Size property

* Use base 10 for scale exponent instead of base e

* Update src/data/datamapping.cpp

* Update modules/base/shaders/billboardpoint_gs.glsl

* Remove some redundant asset settings (Home label)

* Review comments

* Add TODO comment for localdwarfs asset (Speck file reading problems)

* Oops, forgot part of the comment

* Apply suggestions from code review

Co-authored-by: Alexander Bock <alexander.bock@liu.se>

* More code review fixes

* Fix faulty isReady check when labels are included

* Apply suggestions from code review

Co-authored-by: Alexander Bock <alexander.bock@liu.se>

* Apply suggestions from code review

Co-authored-by: Alexander Bock <alexander.bock@liu.se>

* More code review updates

---------

Co-authored-by: Alexander Bock <alexander.bock@liu.se>
2023-12-19 13:20:07 +01:00

471 lines
17 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2023 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/space/rendering/renderableconstellationlines.h>
#include <openspace/documentation/documentation.h>
#include <openspace/engine/globals.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/glm.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/stringhelper.h>
#include <ghoul/opengl/openglstatecache.h>
#include <ghoul/opengl/programobject.h>
#include <scn/scn.h>
#include <array>
#include <filesystem>
#include <fstream>
#include <optional>
namespace {
constexpr std::string_view _loggerCat = "RenderableConstellationLines";
constexpr std::array<const char*, 4> UniformNames = {
"modelViewTransform", "projectionTransform", "opacity", "color"
};
constexpr openspace::properties::Property::PropertyInfo SpeckInfo = {
"File",
"Constellation Data File Path",
"The file that contains the data for the constellation lines",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo DrawElementsInfo = {
"DrawElements",
"Draw Elements",
"Enables/Disables the drawing of the constellations",
openspace::properties::Property::Visibility::NoviceUser
};
constexpr openspace::properties::Property::PropertyInfo UnitInfo = {
"Unit",
"Unit",
"The distance unit used for the constellation lines data",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo ColorsInfo = {
"Colors",
"Constellation Colors",
"The defined colors for the constellations to be rendered. There can be several "
"groups of constellaitons that can have distinct colors.",
openspace::properties::Property::Visibility::User
};
struct [[codegen::Dictionary(RenderableConstellationLines)]] Parameters {
// The path to the SPECK file that contains constellation lines data
std::filesystem::path file;
enum class [[codegen::map(openspace::DistanceUnit)]] Unit {
Meter [[codegen::key("m")]],
Kilometer [[codegen::key("Km")]],
Parsec [[codegen::key("pc")]],
Kiloparsec [[codegen::key("Kpc")]],
Megaparsec [[codegen::key("Mpc")]],
Gigaparsec [[codegen::key("Gpc")]],
Gigalightyear [[codegen::key("Gly")]]
};
// [[codegen::verbatim(UnitInfo.description)]]
std::optional<Unit> unit;
// [[codegen::verbatim(ColorsInfo.description)]]
std::optional<std::vector<glm::vec3>> colors;
};
#include "renderableconstellationlines_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation RenderableConstellationLines::Documentation() {
return codegen::doc<Parameters>("space_renderable_constellationlines");
}
RenderableConstellationLines::RenderableConstellationLines(
const ghoul::Dictionary& dictionary)
: RenderableConstellationsBase(dictionary)
, _drawElements(DrawElementsInfo, true)
, _speckFile(SpeckInfo)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
// Avoid reading files here, instead do it in multithreaded initialize()
_speckFile = absPath(p.file.string()).string();
_speckFile.onChange([this]() { loadData(); });
addProperty(_speckFile);
addProperty(_drawElements);
if (p.unit.has_value()) {
_constellationUnit = codegen::map<DistanceUnit>(*p.unit);
}
else {
_constellationUnit = DistanceUnit::Meter;
}
if (p.colors.has_value()) {
std::vector<glm::vec3> ops = *p.colors;
for (size_t i = 0; i < ops.size(); ++i) {
_constellationColorMap.insert({ static_cast<int>(i) + 1, ops[i] });
}
}
}
void RenderableConstellationLines::selectionPropertyHasChanged() {
using ConstellationKeyValuePair = std::pair<const int, ConstellationLine>;
// If no values are selected (the default), we want to show all constellations
if (!_selection.hasSelected()) {
for (ConstellationKeyValuePair& pair : _renderingConstellationsMap) {
pair.second.isEnabled = true;
}
if (_hasLabels) {
for (dataloader::Labelset::Entry& e : _labels->labelSet().entries) {
e.isEnabled = true;
}
}
}
else {
// Enable all constellations that are selected
for (ConstellationKeyValuePair& pair : _renderingConstellationsMap) {
bool isSelected = _selection.isSelected(pair.second.name);
pair.second.isEnabled = isSelected;
if (_hasLabels) {
for (dataloader::Labelset::Entry& e : _labels->labelSet().entries) {
if (constellationFullName(e.identifier) == pair.second.name) {
e.isEnabled = isSelected;
break;
}
}
}
}
}
}
bool RenderableConstellationLines::isReady() const {
bool isReady = _program && !_renderingConstellationsMap.empty();
// If we have labels, they also need to be loaded
if (_hasLabels) {
return isReady && RenderableConstellationsBase::isReady();
}
return isReady;
}
void RenderableConstellationLines::initialize() {
RenderableConstellationsBase::initialize();
loadData();
if (!_assetSelection.empty()) {
const std::vector<std::string> options = _selection.options();
std::set<std::string> selectedConstellations;
for (const std::string& s : _assetSelection) {
auto it = std::find(options.begin(), options.end(), s);
if (it == options.end()) {
// Test if the provided name was an identifier instead of the full name
it = std::find(
options.begin(),
options.end(),
constellationFullName(s)
);
if (it == options.end()) {
// The user has specified a constellation name that doesn't exist
LWARNING(fmt::format(
"Option '{}' not found in list of constellations", s
));
}
else {
selectedConstellations.insert(constellationFullName(s));
}
}
else {
selectedConstellations.insert(s);
}
}
_selection = selectedConstellations;
}
}
void RenderableConstellationLines::initializeGL() {
_program = global::renderEngine->buildRenderProgram(
"RenderableConstellationLines",
absPath("${MODULE_SPACE}/shaders/constellationlines_vs.glsl"),
absPath("${MODULE_SPACE}/shaders/constellationlines_fs.glsl")
);
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
createConstellations();
}
void RenderableConstellationLines::deinitializeGL() {
using ConstellationKeyValuePair = std::pair<const int, ConstellationLine>;
for (const ConstellationKeyValuePair& pair : _renderingConstellationsMap)
{
glDeleteVertexArrays(1, &pair.second.vaoArray);
glDeleteBuffers(1, &pair.second.vboArray);
}
if (_program) {
global::renderEngine->removeRenderProgram(_program.get());
_program = nullptr;
}
}
void RenderableConstellationLines::renderConstellations(const RenderData&,
const glm::dmat4& modelViewMatrix,
const glm::dmat4& projectionMatrix)
{
glEnablei(GL_BLEND, 0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDepthMask(false);
glEnable(GL_DEPTH_TEST);
_program->activate();
_program->setUniform(_uniformCache.modelViewTransform, modelViewMatrix);
_program->setUniform(_uniformCache.projectionTransform, projectionMatrix);
_program->setUniform(_uniformCache.opacity, opacity());
using ConstellationKeyValuePair = std::pair<const int, ConstellationLine>;
for (const ConstellationKeyValuePair& pair : _renderingConstellationsMap)
{
if (!pair.second.isEnabled) {
continue;
}
_program->setUniform(
_uniformCache.color,
_constellationColorMap[pair.second.colorIndex]
);
glBindVertexArray(pair.second.vaoArray);
glLineWidth(_lineWidth);
glDrawArrays(GL_LINE_STRIP, 0, pair.second.numV);
global::renderEngine->openglStateCache().resetLineState();
}
glBindVertexArray(0);
_program->deactivate();
// Restores GL State
global::renderEngine->openglStateCache().resetDepthState();
global::renderEngine->openglStateCache().resetBlendState();
}
void RenderableConstellationLines::render(const RenderData& data, RendererTasks& tasks) {
const glm::dmat4 modelViewTransform = calcModelViewTransform(data);
const glm::dmat4 projectionTransform = data.camera.projectionMatrix();
if (_drawElements) {
renderConstellations(data, modelViewTransform, projectionTransform);
}
RenderableConstellationsBase::render(data, tasks);
}
void RenderableConstellationLines::update(const UpdateData&) {
if (_program->isDirty()) {
_program->rebuildFromFile();
ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames);
}
}
bool RenderableConstellationLines::loadData() {
bool success = readSpeckFile();
if (!success) {
throw ghoul::RuntimeError("Error loading data");
}
return success;
}
bool RenderableConstellationLines::readSpeckFile() {
if (_speckFile.value().empty()) {
return false;
}
std::filesystem::path fileName = absPath(_speckFile);
LINFO(fmt::format("Loading Speck file {}", fileName));
std::ifstream file(fileName);
if (!file.good()) {
LERROR(fmt::format("Failed to open Speck file {}", fileName));
return false;
}
const float scale = static_cast<float>(toMeter(_constellationUnit));
double maxRadius = 0.0;
int lineIndex = 0;
// The beginning of the speck file has a header that either contains comments
// (signaled by a preceding '#') or information about the structure of the file
// (signaled by the keywords 'datavar', 'texturevar', and 'texture')
std::string line;
while (true) {
std::getline(file, line);
if (file.eof()) {
break;
}
// Guard against wrong line endings (copying files from Windows to Mac) causes
// lines to have a final \r
if (!line.empty() && line.back() == '\r') {
line = line.substr(0, line.length() - 1);
}
if (line.empty() || line[0] == '#') {
continue;
}
std::size_t found = line.find("mesh");
if (found == std::string::npos) {
continue;
}
else {
// mesh lines are structured as follows:
// mesh -c colorindex {
// colorindex is the index of the color for the mesh
std::stringstream str(line);
ConstellationLine constellationLine;
constellationLine.lineIndex = lineIndex;
std::string dummy;
str >> dummy; // mesh command
dummy.clear();
str >> dummy; // color index command
do {
if (dummy == "-c") {
str >> constellationLine.colorIndex; // color index
}
else {
std::string message = fmt::format("Unknown command '{}' found in "
"constellation file '{}'", dummy, fileName);
LWARNING(message);
}
dummy.clear();
str >> dummy;
} while (dummy != "{");
std::getline(file, line);
// Read the identifier
std::stringstream id(line);
std::string identifier;
id >> dummy; // id command
dummy.clear();
std::getline(id, identifier); // identifier
ghoul::trimWhitespace(identifier);
std::string name = constellationFullName(identifier);
if (!name.empty()) {
constellationLine.name = name;
}
// Read the number of vertices
std::getline(file, line);
std::stringstream dim(line);
dim >> constellationLine.numV;
// We can now read the vertices data:
for (int l = 0; l < constellationLine.numV; ++l) {
std::getline(file, line);
if (line.substr(0, 1) == "}") {
break;
}
// Try to read three values for the position
glm::vec3 pos;
auto reading = scn::scan(line, "{} {} {}", pos.x, pos.y, pos.z);
if (reading) {
pos *= scale;
constellationLine.vertices.push_back(pos.x);
constellationLine.vertices.push_back(pos.y);
constellationLine.vertices.push_back(pos.z);
}
else {
LERROR(fmt::format(
"Failed reading position on line {} of mesh {} in file: '{}'. "
"Stopped reading constellation data", l, lineIndex, fileName
));
}
// Check if new max radius
const double r = glm::length(glm::dvec3(pos));
maxRadius = std::max(maxRadius, r);
}
std::getline(file, line);
if (line.substr(0, 1) == "}") {
_renderingConstellationsMap.insert({ lineIndex++, constellationLine });
}
else {
return false;
}
}
}
setBoundingSphere(maxRadius);
return true;
}
void RenderableConstellationLines::createConstellations() {
LDEBUG("Creating constellations");
for (std::pair<const int, ConstellationLine>& p : _renderingConstellationsMap) {
GLuint vao;
glGenVertexArrays(1, &vao);
p.second.vaoArray = vao;
GLuint vbo;
glGenBuffers(1, &vbo);
p.second.vboArray = vbo;
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(
GL_ARRAY_BUFFER,
p.second.vertices.size() * sizeof(GLfloat),
p.second.vertices.data(),
GL_STATIC_DRAW
);
// in_position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
}
glBindVertexArray(0);
}
} // namespace openspace