Commit c1b710e9 authored by Andrew Wilson's avatar Andrew Wilson
Browse files

ENH: Color operators and functions

parent cf8dd4b6
......@@ -41,10 +41,10 @@ main()
// Setup N separate string simulations with varying bend stiffnesses
const unsigned int numStrings = 8;
const unsigned int numVerts = 30;
const double stringSpacing = 2.0; // How far each string is apart
const double stringLength = 10.0; // Total length of string
const Vec3d startColor = Vec3d(1.0, 0.0, 0.0); // Color of first string
const Vec3d endColor = Vec3d(0.0, 1.0, 0.0); // Color of last string
const double stringSpacing = 2.0; // How far each string is apart
const double stringLength = 10.0; // Total length of string
const Color startColor = Color::Red; // Color of first string
const Color endColor = Color::Green; // Color of last string
struct PbdSim
{
std::shared_ptr<LineMesh> geometry;
......@@ -91,11 +91,11 @@ main()
sims[i].params = std::make_shared<PBDModelConfig>();
sims[i].params->enableConstraint(PbdConstraint::Type::Distance, 0.001);
sims[i].params->enableConstraint(PbdConstraint::Type::Bend, static_cast<double>(i) * 0.1 / numStrings + 0.001);
sims[i].params->m_fixedNodeIds = { 0 }; // Fix the first node in each string
sims[i].params->m_fixedNodeIds = { 0 }; // Fix the first node in each string
sims[i].params->m_uniformMassValue = 5.0;
sims[i].params->m_gravity = Vec3d(0, -9.8, 0);
sims[i].params->m_dt = 0.0005;
sims[i].params->m_maxIter = 5;
sims[i].params->m_dt = 0.0005;
sims[i].params->m_maxIter = 5;
// Set the parameters
sims[i].model->configure(sims[i].params);
......@@ -105,9 +105,7 @@ main()
sims[i].visualModel = std::make_shared<VisualModel>(sims[i].geometry);
std::shared_ptr<RenderMaterial> material = std::make_shared<RenderMaterial>();
material->setDisplayMode(RenderMaterial::DisplayMode::WIREFRAME);
const double t = static_cast<double>(i) / (numStrings - 1);
Vec3d color = (endColor - startColor) * t + startColor;
material->setDebugColor(Color(color.x(), color.y(), color.z()));
material->setDebugColor(Color::lerpRgb(startColor, endColor, static_cast<double>(i) / (numStrings - 1)));
material->setLineWidth(2.0f);
sims[i].visualModel->setRenderMaterial(material);
sims[i].object->addVisualModel(sims[i].visualModel);
......
......@@ -52,20 +52,17 @@ Color::Color()
Color::Color(double r, double g, double b, double a)
{
bool redGood = isColorRangeCorrect(r);
bool greenGood = isColorRangeCorrect(g);
bool blueGood = isColorRangeCorrect(b);
bool alphaGood = isColorRangeCorrect(a);
if (!redGood || !greenGood || !blueGood || !alphaGood)
{
LOG(WARNING) << "Can not set Color: value outside of [0.0, 1.0] range.";
return;
}
setValue(r, g, b, a);
}
rgba[0] = r;
rgba[1] = g;
rgba[2] = b;
rgba[3] = a;
Color::Color(double* rgba)
{
setValue(rgba[0], rgba[1], rgba[2], rgba[3]);
}
Color::Color(Color color, double a)
{
setValue(color.r, color.g, color.b, a);
}
Color&
......@@ -125,14 +122,12 @@ Color::lighten(double p_darkFactor)
void
Color::setValue(double p_red, double p_green, double p_blue, double p_alpha)
{
bool redGood = isColorRangeCorrect(p_red);
bool greenGood = isColorRangeCorrect(p_green);
bool blueGood = isColorRangeCorrect(p_blue);
bool alphaGood = isColorRangeCorrect(p_alpha);
if (!redGood || !greenGood || !blueGood || !alphaGood)
if (!isColorRangeCorrect(p_red)
|| !isColorRangeCorrect(p_green)
|| !isColorRangeCorrect(p_blue)
|| !isColorRangeCorrect(p_alpha))
{
LOG(WARNING) << "Can not set Color: value outside of [0.0, 1.0] range.";
return;
}
rgba[0] = p_red;
......@@ -155,4 +150,148 @@ Color::getValue() const
{
return rgba;
}
Color
Color::lerpRgba(const Color& start, const Color& end, const double t)
{
return start + (end - start) * t;
}
Color
Color::lerpRgb(const Color& start, const Color& end, const double t)
{
return Color(start + (end - start) * t, 1.0);
}
Color
operator*(const Color& color_lhs, const Color& color_rhs)
{
Color results;
results.r = color_lhs.r * color_rhs.r;
results.g = color_lhs.g * color_rhs.g;
results.b = color_lhs.b * color_rhs.b;
results.a = color_lhs.a * color_rhs.a;
return results;
}
Color
operator*(const Color& color_lhs, const double intensity_rhs)
{
Color results;
results.r = color_lhs.r * intensity_rhs;
results.g = color_lhs.g * intensity_rhs;
results.b = color_lhs.b * intensity_rhs;
results.a = color_lhs.a * intensity_rhs;
return results;
}
Color
operator*=(const Color& color_lhs, const Color& color_rhs)
{
Color results;
results.r = color_lhs.r * color_rhs.r;
results.g = color_lhs.g * color_rhs.g;
results.b = color_lhs.b * color_rhs.b;
results.a = color_lhs.a * color_rhs.a;
return results;
}
Color
operator*=(const Color& color_lhs, const double intensity_rhs)
{
Color results;
results.r = color_lhs.r * intensity_rhs;
results.g = color_lhs.g * intensity_rhs;
results.b = color_lhs.b * intensity_rhs;
results.a = color_lhs.a * intensity_rhs;
return results;
}
Color
operator+(const Color& color_lhs, const Color& color_rhs)
{
Color results;
results.r = color_lhs.r + color_rhs.r;
results.g = color_lhs.g + color_rhs.g;
results.b = color_lhs.b + color_rhs.b;
results.a = color_lhs.a + color_rhs.a;
return results;
}
Color
operator+(const Color& color_lhs, const double intensity_rhs)
{
Color results;
results.r = color_lhs.r + intensity_rhs;
results.g = color_lhs.g + intensity_rhs;
results.b = color_lhs.b + intensity_rhs;
results.a = color_lhs.a + intensity_rhs;
return results;
}
Color
operator+=(const Color& color_lhs, const Color& color_rhs)
{
Color results;
results.r = color_lhs.r + color_rhs.r;
results.g = color_lhs.g + color_rhs.g;
results.b = color_lhs.b + color_rhs.b;
results.a = color_lhs.a + color_rhs.a;
return results;
}
Color
operator+=(const Color& color_lhs, const double intensity_rhs)
{
Color results;
results.r = color_lhs.r + intensity_rhs;
results.g = color_lhs.g + intensity_rhs;
results.b = color_lhs.b + intensity_rhs;
results.a = color_lhs.a + intensity_rhs;
return results;
}
Color
operator-(const Color& color_lhs, const Color& color_rhs)
{
Color results;
results.r = color_lhs.r - color_rhs.r;
results.g = color_lhs.g - color_rhs.g;
results.b = color_lhs.b - color_rhs.b;
results.a = color_lhs.a - color_rhs.a;
return results;
}
Color
operator-(const Color& color_lhs, const double intensity_rhs)
{
Color results;
results.r = color_lhs.r - intensity_rhs;
results.g = color_lhs.g - intensity_rhs;
results.b = color_lhs.b - intensity_rhs;
results.a = color_lhs.a - intensity_rhs;
return results;
}
Color
operator-=(const Color& color_lhs, const Color& color_rhs)
{
Color results;
results.r = color_lhs.r - color_rhs.r;
results.g = color_lhs.g - color_rhs.g;
results.b = color_lhs.b - color_rhs.b;
results.a = color_lhs.a - color_rhs.a;
return results;
}
Color
operator-=(const Color& color_lhs, const double intensity_rhs)
{
Color results;
results.r = color_lhs.r - intensity_rhs;
results.g = color_lhs.g - intensity_rhs;
results.b = color_lhs.b - intensity_rhs;
results.a = color_lhs.a - intensity_rhs;
return results;
}
}
......@@ -51,6 +51,12 @@ struct Color
///
Color();
Color(double r, double g, double b, double a = 1.0);
Color(double* rgba);
///
/// \brief Constructor overwrites the alpha component
///
Color(Color color, double a);
///
/// \brief Equality operator
......@@ -92,6 +98,12 @@ struct Color
///
const double* getValue() const;
///
/// \brief interpolate between two colors by ratio t
///
static Color lerpRgba(const Color& start, const Color& end, const double t);
static Color lerpRgb(const Color& start, const Color& end, const double t);
/// Various commonly used colors
static Color White;
static Color Black;
......@@ -105,4 +117,28 @@ struct Color
static Color Yellow;
};
#pragma warning(default : 4201)
///
/// \brief Multiply operators
///
Color operator*(const Color& color_lhs, const Color& color_rhs);
Color operator*(const Color& color_lhs, const double intensity_rhs);
Color operator*=(const Color& color_lhs, const Color& color_rhs);
Color operator*=(const Color& color_lhs, const double intensity_rhs);
///
/// \brief Add operators
///
Color operator+(const Color& color_lhs, const Color& color_rhs);
Color operator+(const Color& intensity_lhs, const double intensity_rhs);
Color operator+=(const Color& color_lhs, const Color& color_rhs);
Color operator+=(const Color& intensity_lhs, const double intensity_rhs);
///
/// \brief Subtract operators
///
Color operator-(const Color& color_lhs, const Color& color_rhs);
Color operator-(const Color& color_rhs, const double intensity_lhs);
Color operator-=(const Color& color_lhs, const Color& color_rhs);
Color operator-=(const Color& color_rhs, const double intensity_lhs);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment