mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-24 04:58:59 -05:00
Cleanup (#600)
* General Code Cleanup * Add check for TABs to check_style_guide.py * Removing warnings
This commit is contained in:
+202
-155
@@ -1,176 +1,223 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2018 *
|
||||
* *
|
||||
* 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/volume/envelope.h>
|
||||
|
||||
namespace openspace {
|
||||
namespace volume {
|
||||
Envelope::Envelope() { }
|
||||
namespace openspace::volume {
|
||||
|
||||
Envelope::Envelope(std::vector<EnvelopePoint> vec) {
|
||||
_points = vec;
|
||||
}
|
||||
EnvelopePoint::EnvelopePoint(glm::vec3 c, float x, float y)
|
||||
: color(c)
|
||||
, colorHex(getHexadecimalFromVec3(c))
|
||||
, position(std::make_pair(x, y))
|
||||
{}
|
||||
|
||||
bool Envelope::operator!=(const Envelope& env) const {
|
||||
const double minDist = 0.0001;
|
||||
EnvelopePoint::EnvelopePoint(std::string c, float x, float y)
|
||||
: color(hexadecimalToRGBConversion(c))
|
||||
, colorHex(c)
|
||||
, position(std::make_pair(x, y))
|
||||
{}
|
||||
|
||||
if (_points.size() != env._points.size())
|
||||
return true;
|
||||
Envelope::Envelope() {}
|
||||
|
||||
auto iter = _points.begin();
|
||||
auto envIter = env._points.begin();
|
||||
for (; iter != _points.end(); ++iter, ++envIter) {
|
||||
if (abs(iter->position.first - envIter->position.first) > minDist ||
|
||||
abs(iter->position.second - envIter->position.second) > minDist ||
|
||||
iter->color != envIter->color)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Envelope::Envelope(std::vector<EnvelopePoint> vec) {
|
||||
_points = vec;
|
||||
}
|
||||
|
||||
bool Envelope::operator!=(const Envelope& env) const {
|
||||
const double minDist = 0.0001;
|
||||
|
||||
void Envelope::setPoints(std::vector<EnvelopePoint> vec) {
|
||||
this->_points = vec;
|
||||
}
|
||||
if (_points.size() != env._points.size()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<EnvelopePoint> Envelope::getPoints() {
|
||||
return _points;
|
||||
}
|
||||
|
||||
bool Envelope::isValueInEnvelope(float pos) const {
|
||||
if (!_points.empty()) {
|
||||
if (_points.front().position.first <= pos && _points.back().position.first >= pos)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Envelope::isEnvelopeValid() const {
|
||||
auto currentIter = _points.begin();
|
||||
auto nextIter = currentIter + 1;
|
||||
for (; nextIter != _points.end(); ++currentIter, ++nextIter) {
|
||||
if (currentIter->position.first > nextIter->position.first)
|
||||
return false;
|
||||
}
|
||||
auto iter = _points.begin();
|
||||
auto envIter = env._points.begin();
|
||||
for (; iter != _points.end(); ++iter, ++envIter) {
|
||||
if (abs(iter->position.first - envIter->position.first) > minDist ||
|
||||
abs(iter->position.second - envIter->position.second) > minDist ||
|
||||
iter->color != envIter->color)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
glm::vec3 Envelope::normalizeColor(glm::vec3 vec) const{
|
||||
|
||||
return glm::vec3{ vec.r / 255.f, vec.g / 255.f , vec.b / 255.f };
|
||||
}
|
||||
|
||||
glm::vec4 Envelope::getValueAtPosition(float pos) const {
|
||||
auto afterIter = _points.begin();
|
||||
glm::vec4 color{ 0.f, 0.f , 0.f , 0.f };
|
||||
while (afterIter->position.first < pos ) {
|
||||
if(afterIter == _points.end())
|
||||
return color;
|
||||
++afterIter;
|
||||
}
|
||||
if (afterIter->position.first == pos) {
|
||||
return glm::vec4{ afterIter->color, afterIter->position.second };
|
||||
}
|
||||
auto beforeIter = afterIter -1;
|
||||
void Envelope::setPoints(std::vector<EnvelopePoint> vec) {
|
||||
this->_points = vec;
|
||||
}
|
||||
|
||||
float dist = afterIter->position.first - beforeIter->position.first;
|
||||
float alpha;
|
||||
if(dist < 0.0001)
|
||||
color = { normalizeColor((beforeIter->color + afterIter->color) / 2.f),
|
||||
std::max(beforeIter->position.second, afterIter->position.second) };
|
||||
else
|
||||
color = { normalizeColor(beforeIter->color *(abs(pos - afterIter->position.first) / dist) + afterIter->color *(abs(pos - beforeIter->position.first) / dist)),
|
||||
beforeIter->position.second *(abs(pos - afterIter->position.first) / dist) + afterIter->position.second *(abs(pos - beforeIter->position.first) / dist) };
|
||||
std::vector<EnvelopePoint> Envelope::getPoints() {
|
||||
return _points;
|
||||
}
|
||||
|
||||
bool Envelope::isValueInEnvelope(float pos) const {
|
||||
if (!_points.empty()) {
|
||||
if (_points.front().position.first <= pos && _points.back().position.first >= pos)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Envelope::isEnvelopeValid() const {
|
||||
auto currentIter = _points.begin();
|
||||
auto nextIter = currentIter + 1;
|
||||
for (; nextIter != _points.end(); ++currentIter, ++nextIter) {
|
||||
if (currentIter->position.first > nextIter->position.first)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
glm::vec3 Envelope::normalizeColor(glm::vec3 vec) const{
|
||||
|
||||
return glm::vec3{ vec.r / 255.f, vec.g / 255.f , vec.b / 255.f };
|
||||
}
|
||||
|
||||
glm::vec4 Envelope::getValueAtPosition(float pos) const {
|
||||
auto afterIter = _points.begin();
|
||||
glm::vec4 color{ 0.f, 0.f , 0.f , 0.f };
|
||||
while (afterIter->position.first < pos ) {
|
||||
if(afterIter == _points.end())
|
||||
return color;
|
||||
}
|
||||
++afterIter;
|
||||
}
|
||||
if (afterIter->position.first == pos) {
|
||||
return glm::vec4{ afterIter->color, afterIter->position.second };
|
||||
}
|
||||
auto beforeIter = afterIter -1;
|
||||
|
||||
int EnvelopePoint::HexadecimalToDecimal(std::string hex) const {
|
||||
int hexLength = hex.length();
|
||||
double dec = 0;
|
||||
for (int i = 0; i < hexLength; ++i)
|
||||
{
|
||||
char b = hex[i];
|
||||
float dist = afterIter->position.first - beforeIter->position.first;
|
||||
float alpha;
|
||||
if(dist < 0.0001)
|
||||
color = { normalizeColor((beforeIter->color + afterIter->color) / 2.f),
|
||||
std::max(beforeIter->position.second, afterIter->position.second) };
|
||||
else
|
||||
color = {
|
||||
normalizeColor(
|
||||
beforeIter->color * (abs(pos - afterIter->position.first) / dist) +
|
||||
afterIter->color * (abs(pos - beforeIter->position.first) / dist)
|
||||
),
|
||||
beforeIter->position.second * (abs(pos - afterIter->position.first) / dist) +
|
||||
afterIter->position.second *
|
||||
(abs(pos - beforeIter->position.first) / dist)
|
||||
};
|
||||
|
||||
if (b >= 48 && b <= 57)
|
||||
b -= 48;
|
||||
else if (b >= 65 && b <= 70)
|
||||
b -= 55;
|
||||
else if (b >= 97 && b <= 102)
|
||||
b -= 87;
|
||||
dec += b * pow(16, ((hexLength - i) - 1));
|
||||
return color;
|
||||
}
|
||||
|
||||
int EnvelopePoint::HexadecimalToDecimal(std::string hex) const {
|
||||
int hexLength = hex.length();
|
||||
double dec = 0;
|
||||
for (int i = 0; i < hexLength; ++i)
|
||||
{
|
||||
char b = hex[i];
|
||||
|
||||
if (b >= 48 && b <= 57)
|
||||
b -= 48;
|
||||
else if (b >= 65 && b <= 70)
|
||||
b -= 55;
|
||||
else if (b >= 97 && b <= 102)
|
||||
b -= 87;
|
||||
dec += b * pow(16, ((hexLength - i) - 1));
|
||||
}
|
||||
return (int)dec;
|
||||
}
|
||||
|
||||
std::string EnvelopePoint::DecimalToHexadecimal(int dec) const {
|
||||
if (dec < 1) return "00";
|
||||
|
||||
int hex = dec;
|
||||
std::string hexStr = "";
|
||||
|
||||
while (dec > 0)
|
||||
{
|
||||
hex = dec % 16;
|
||||
|
||||
if (hex < 10)
|
||||
hexStr = hexStr.insert(0, std::string(1, (hex + 48)));
|
||||
else
|
||||
hexStr = hexStr.insert(0, std::string(1, (hex + 55)));
|
||||
dec /= 16;
|
||||
}
|
||||
return hexStr;
|
||||
}
|
||||
|
||||
glm::vec3 EnvelopePoint::hexadecimalToRGBConversion(std::string hex) const {
|
||||
float r = static_cast<float>(HexadecimalToDecimal(hex.substr(1, 2)));
|
||||
float g = static_cast<float>(HexadecimalToDecimal(hex.substr(3, 2)));
|
||||
float b = static_cast<float>(HexadecimalToDecimal(hex.substr(5, 2)));
|
||||
|
||||
return glm::vec3(r, g, b);
|
||||
}
|
||||
|
||||
std::string EnvelopePoint::getHexadecimalFromVec3(glm::vec3 vec) const {
|
||||
|
||||
std::string r = DecimalToHexadecimal(static_cast<int>(vec.r));
|
||||
std::string g = DecimalToHexadecimal(static_cast<int>(vec.g));
|
||||
std::string b = DecimalToHexadecimal(static_cast<int>(vec.b));
|
||||
|
||||
return ("#" + r + g + b);
|
||||
}
|
||||
|
||||
json Envelope::getJSONPoints() const {
|
||||
json j;
|
||||
for (int i = 0; i < _points.size(); i++) {
|
||||
j[i] = {
|
||||
{ "color", _points.at(i).colorHex },
|
||||
{ "position",{
|
||||
{ "x", _points.at(i).position.first },
|
||||
{ "y", _points.at(i).position.second },
|
||||
},
|
||||
}
|
||||
return (int)dec;
|
||||
}
|
||||
};
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
std::string EnvelopePoint::DecimalToHexadecimal(int dec) const {
|
||||
if (dec < 1) return "00";
|
||||
json Envelope::getJSONEnvelope() const {
|
||||
json j;
|
||||
j["points"] = getJSONPoints();
|
||||
return j;
|
||||
}
|
||||
|
||||
int hex = dec;
|
||||
std::string hexStr = "";
|
||||
void Envelope::setEnvelopeLuaTable(lua_State* state) const {
|
||||
for (auto iter = _points.begin(); iter != _points.end(); ++iter) {
|
||||
lua_newtable(state);
|
||||
lua_pushstring(state, iter->colorHex.c_str());
|
||||
lua_setfield(state, -2, "color");
|
||||
lua_newtable(state);
|
||||
lua_pushnumber(state, static_cast<lua_Number>(iter->position.first));
|
||||
lua_setfield(state, -2, "x");
|
||||
lua_pushnumber(state, static_cast<lua_Number>(iter->position.second));
|
||||
lua_setfield(state, -2, "y");
|
||||
lua_setfield(state, -2, "position");
|
||||
lua_setfield(
|
||||
state,
|
||||
-2,
|
||||
("[\"" + std::to_string(iter - _points.begin() + 1) + "\"]").c_str()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
while (dec > 0)
|
||||
{
|
||||
hex = dec % 16;
|
||||
|
||||
if (hex < 10)
|
||||
hexStr = hexStr.insert(0, std::string(1, (hex + 48)));
|
||||
else
|
||||
hexStr = hexStr.insert(0, std::string(1, (hex + 55)));
|
||||
dec /= 16;
|
||||
}
|
||||
return hexStr;
|
||||
}
|
||||
|
||||
glm::vec3 EnvelopePoint::hexadecimalToRGBConversion(std::string hex) const {
|
||||
float r = static_cast<float>(HexadecimalToDecimal(hex.substr(1, 2)));
|
||||
float g = static_cast<float>(HexadecimalToDecimal(hex.substr(3, 2)));
|
||||
float b = static_cast<float>(HexadecimalToDecimal(hex.substr(5, 2)));
|
||||
|
||||
return glm::vec3(r, g, b);
|
||||
}
|
||||
|
||||
std::string EnvelopePoint::getHexadecimalFromVec3(glm::vec3 vec) const {
|
||||
|
||||
std::string r = DecimalToHexadecimal(static_cast<int>(vec.r));
|
||||
std::string g = DecimalToHexadecimal(static_cast<int>(vec.g));
|
||||
std::string b = DecimalToHexadecimal(static_cast<int>(vec.b));
|
||||
|
||||
return ("#" + r + g + b);
|
||||
}
|
||||
|
||||
json Envelope::getJSONPoints() const {
|
||||
json j;
|
||||
for (int i = 0; i < _points.size(); i++) {
|
||||
j[i] = {
|
||||
{ "color", _points.at(i).colorHex },
|
||||
{ "position",{
|
||||
{ "x", _points.at(i).position.first },
|
||||
{ "y", _points.at(i).position.second },
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
json Envelope::getJSONEnvelope() const {
|
||||
json j;
|
||||
j["points"] = getJSONPoints();
|
||||
return j;
|
||||
}
|
||||
|
||||
void Envelope::setEnvelopeLuaTable(lua_State* state) const {
|
||||
for (auto iter = _points.begin(); iter != _points.end(); ++iter) {
|
||||
lua_newtable(state);
|
||||
lua_pushstring(state, iter->colorHex.c_str());
|
||||
lua_setfield(state, -2, "color");
|
||||
lua_newtable(state);
|
||||
lua_pushnumber(state, static_cast<lua_Number>(iter->position.first));
|
||||
lua_setfield(state, -2, "x");
|
||||
lua_pushnumber(state, static_cast<lua_Number>(iter->position.second));
|
||||
lua_setfield(state, -2, "y");
|
||||
lua_setfield(state, -2, "position");
|
||||
lua_setfield(state, -2, ("[\"" + std::to_string(iter - _points.begin() + 1) + "\"]").c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}//namespace volume
|
||||
}//namespace openspace
|
||||
} //namespace openspace::volume
|
||||
|
||||
Reference in New Issue
Block a user