Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GCC 11 compatibility issue #658

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 101 additions & 129 deletions include/pangolin/gl/colour.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,151 +28,123 @@
#pragma once

#include <cmath>

#include <limits>
#include <stdexcept>

namespace pangolin
{
namespace pangolin {

/// Represent OpenGL floating point colour: Red, Green and Blue with alpha.
struct Colour
{
inline static Colour White() {
return Colour(1.0f,1.0f,1.0f,1.0f);
}
inline static Colour Black() {
return Colour(0.0f,0.0f,0.0f,1.0f);
}
inline static Colour Red() {
return Colour(1.0f,0.0f,0.0f,1.0f);
}
inline static Colour Green() {
return Colour(0.0f,1.0f,0.0f,1.0f);
}
inline static Colour Blue() {
return Colour(0.0f,0.0f,1.0f,1.0f);
}
inline static Colour Unspecified() {
return Colour(
std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN()
);
}

/// Default constructs white.
inline Colour()
: red(1.0f), green(1.0f), blue(1.0f), alpha(1.0f)
{
}

/// Construct from component values
inline Colour(const float red, const float green, const float blue, const float alpha = 1.0f)
: red(red), green(green), blue(blue), alpha(alpha)
{
}

/// Construct from rgba array.
inline Colour(const float rgba[4])
{
r = rgba[0];
g = rgba[1];
b = rgba[2];
a = rgba[3];
}

/// Return pointer to OpenGL compatible RGBA array.
inline float* Get()
{
return c;
}

/// Return this colour with alpha adjusted.
inline Colour WithAlpha(const float alpha)
{
return Colour(r,g,b,alpha);
}

/// Construct from HSV Colour
/// @param hue Colour hue in range [0,1]
/// @param sat Saturation in range [0,1]
/// @param val Value / Brightness in range [0,1].
static inline Colour Hsv(const float hue, const float sat = 1.0f, const float val = 1.0f, const float alpha = 1.0f)
{
const float h = 6.0f * hue;
const int i = (int)floor(h);
const float f = (i%2 == 0) ? 1-(h-i) : h-i;
const float m = val * (1-sat);
const float n = val * (1-sat*f);

switch(i)
{
case 0: return Colour(val,n,m,alpha);
case 1: return Colour(n,val,m,alpha);
case 2: return Colour(m,val,n,alpha);
case 3: return Colour(m,n,val,alpha);
case 4: return Colour(n,m,val,alpha);
case 5: return Colour(val,m,n,alpha);
default:
throw std::runtime_error("Found extra colour in rainbow.");
}
}

union {
struct {
float red;
float green;
float blue;
float alpha;
};
struct {
float r;
float g;
float b;
float a;
};
float c[4];
struct Colour {
inline static Colour White() { return Colour(1.0f, 1.0f, 1.0f, 1.0f); }
inline static Colour Black() { return Colour(0.0f, 0.0f, 0.0f, 1.0f); }
inline static Colour Red() { return Colour(1.0f, 0.0f, 0.0f, 1.0f); }
inline static Colour Green() { return Colour(0.0f, 1.0f, 0.0f, 1.0f); }
inline static Colour Blue() { return Colour(0.0f, 0.0f, 1.0f, 1.0f); }
inline static Colour Unspecified() {
return Colour(std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::quiet_NaN());
}

/// Default constructs white.
inline Colour() : red(1.0f), green(1.0f), blue(1.0f), alpha(1.0f) {}

/// Construct from component values
inline Colour(const float red, const float green, const float blue,
const float alpha = 1.0f)
: red(red), green(green), blue(blue), alpha(alpha) {}

/// Construct from rgba array.
inline Colour(const float rgba[4]) {
r = rgba[0];
g = rgba[1];
b = rgba[2];
a = rgba[3];
}

/// Return pointer to OpenGL compatible RGBA array.
inline float *Get() { return c; }

/// Return this colour with alpha adjusted.
inline Colour WithAlpha(const float alpha) { return Colour(r, g, b, alpha); }

/// Construct from HSV Colour
/// @param hue Colour hue in range [0,1]
/// @param sat Saturation in range [0,1]
/// @param val Value / Brightness in range [0,1].
static inline Colour Hsv(const float hue, const float sat = 1.0f,
const float val = 1.0f, const float alpha = 1.0f) {
const float h = 6.0f * hue;
const int i = (int)floor(h);
const float f = (i % 2 == 0) ? 1 - (h - i) : h - i;
const float m = val * (1 - sat);
const float n = val * (1 - sat * f);

switch (i) {
case 0:
return Colour(val, n, m, alpha);
case 1:
return Colour(n, val, m, alpha);
case 2:
return Colour(m, val, n, alpha);
case 3:
return Colour(m, n, val, alpha);
case 4:
return Colour(n, m, val, alpha);
case 5:
return Colour(val, m, n, alpha);
default:
throw std::runtime_error("Found extra colour in rainbow.");
}
}

union {
struct {
float red;
float green;
float blue;
float alpha;
};

struct {
float r;
float g;
float b;
float a;
};
float c[4];
};
};

/// A ColourWheel is like a continuous colour palate that can be sampled.
/// In the future, different ColourWheels will be supported, but this one
/// is based on sampling hues in HSV colourspace. An indefinite number of
/// unique colours are sampled using the golden angle.
class ColourWheel
{
class ColourWheel {
public:
/// Construct ColourWheel with Saturation, Value and Alpha constant.
inline ColourWheel(float saturation = 0.5f, float value = 1.0f, float alpha = 1.0f)
: unique_colours(0), sat(saturation), val(value), alpha(alpha)
{

}
/// Construct ColourWheel with Saturation, Value and Alpha constant.
inline ColourWheel(float saturation = 0.5f, float value = 1.0f,
float alpha = 1.0f)
: unique_colours(0), sat(saturation), val(value), alpha(alpha) {}

/// Use Golden ratio (/angle) to pick well spaced colours.
inline Colour GetColourBin(int i) const
{
float hue = i * 0.5f * (3.0f - sqrt(5.0f));
hue -= (int)hue;
return Colour::Hsv(hue,sat,val,alpha);
}
/// Use Golden ratio (/angle) to pick well spaced colours.
inline Colour GetColourBin(int i) const {
float hue = i * 0.5f * (3.0f - sqrt(5.0f));
hue -= (int)hue;
return Colour::Hsv(hue, sat, val, alpha);
}

/// Return next unique colour from ColourWheel.
inline Colour GetUniqueColour()
{
return GetColourBin(unique_colours++);
}
/// Return next unique colour from ColourWheel.
inline Colour GetUniqueColour() { return GetColourBin(unique_colours++); }

/// Reset colour wheel counter to initial state
inline void Reset() {
unique_colours = 0;
}
/// Reset colour wheel counter to initial state
inline void Reset() { unique_colours = 0; }

protected:
int unique_colours;
float sat;
float val;
float alpha;
int unique_colours;
float sat;
float val;
float alpha;
};

}
} // namespace pangolin
18 changes: 9 additions & 9 deletions include/pangolin/utils/picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ class value {
private:
template <typename T> value(const T*); // intentionally defined to block implicit conversion of pointer to bool
template <typename Iter> static void _indent(Iter os, int indent);
template <typename Iter> void _serialize(Iter os, int indent) const;
std::string _serialize(int indent) const;
template <typename Iter> void serialize_(Iter os, int indent) const;
std::string serialize_(int indent) const;
};

typedef value::array array;
Expand Down Expand Up @@ -561,11 +561,11 @@ template <typename Iter> void serialize_str(const std::string& s, Iter oi) {
}

template <typename Iter> void value::serialize(Iter oi, bool prettify) const {
return _serialize(oi, prettify ? 0 : -1);
return serialize_(oi, prettify ? 0 : -1);
}

inline std::string value::serialize(bool prettify) const {
return _serialize(prettify ? 0 : -1);
return serialize_(prettify ? 0 : -1);
}

template <typename Iter> void value::_indent(Iter oi, int indent) {
Expand All @@ -575,7 +575,7 @@ template <typename Iter> void value::_indent(Iter oi, int indent) {
}
}

template <typename Iter> void value::_serialize(Iter oi, int indent) const {
template <typename Iter> void value::serialize_(Iter oi, int indent) const {
switch (type_) {
case string_type:
serialize_str(*u_.string_, oi);
Expand All @@ -594,7 +594,7 @@ template <typename Iter> void value::_serialize(Iter oi, int indent) const {
if (indent != -1) {
_indent(oi, indent);
}
i->_serialize(oi, indent);
i->serialize_(oi, indent);
}
if (indent != -1) {
--indent;
Expand Down Expand Up @@ -624,7 +624,7 @@ template <typename Iter> void value::_serialize(Iter oi, int indent) const {
if (indent != -1) {
*oi++ = ' ';
}
i->second._serialize(oi, indent);
i->second.serialize_(oi, indent);
}
if (indent != -1) {
--indent;
Expand All @@ -644,9 +644,9 @@ template <typename Iter> void value::_serialize(Iter oi, int indent) const {
}
}

inline std::string value::_serialize(int indent) const {
inline std::string value::serialize_(int indent) const {
std::string s;
_serialize(std::back_inserter(s), indent);
serialize_(std::back_inserter(s), indent);
return s;
}

Expand Down