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

Added alpha channel and Color functions #359

Merged
merged 4 commits into from
Mar 22, 2015
Merged
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
9 changes: 5 additions & 4 deletions apps/simpleFrames/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ int main(int argc, char* argv[])
myWorld.addEntity(&A);

SimpleFrame arrow(Frame::World(), "arrow");
arrow.addVisualizationShape(new ArrowShape(Eigen::Vector3d(0.1,-0.1, 0.0),
Eigen::Vector3d(0.1, 0.0, 0.0),
ArrowShape::Properties(0.002, 1.8),
Eigen::Vector3d(1.0, 0.5, 0.5)));
arrow.addVisualizationShape(
new ArrowShape(Eigen::Vector3d(0.1,-0.1, 0.0),
Eigen::Vector3d(0.1, 0.0, 0.0),
ArrowShape::Properties(0.002, 1.8),
Eigen::Vector4d(1.0, 0.5, 0.5, 1.0)));
myWorld.addEntity(&arrow);

// CAREFUL: For an Entity (or Frame) that gets added to the world to be
Expand Down
14 changes: 14 additions & 0 deletions apps/softBodies/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ int main(int argc, char* argv[])
DART_DATA_PATH"skel/softBodies.skel");
assert(myWorld != NULL);

for(size_t i=0; i<myWorld->getNumSkeletons(); ++i)
{
dart::dynamics::Skeleton* skel = myWorld->getSkeleton(i);
for(size_t j=0; j<skel->getNumBodyNodes(); ++j)
{
dart::dynamics::BodyNode* bn = skel->getBodyNode(j);
for(size_t k=0; k<bn->getNumVisualizationShapes(); ++k)
{
dart::dynamics::Shape* vs = bn->getVisualizationShape(k);
vs->setColor(dart::Color::Random());
}
}
}

// create a window and link it to the world
MyWindow window;
window.setWorld(myWorld);
Expand Down
7 changes: 4 additions & 3 deletions dart/dynamics/ArrowShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ArrowShape::Properties::Properties(double _radius, double _headRadiusScale,
ArrowShape::ArrowShape(const Eigen::Vector3d& _tail,
const Eigen::Vector3d& _head,
const Properties& _properties,
const Eigen::Vector3d& _color,
const Eigen::Vector4d& _color,
size_t _resolution)
: MeshShape(Eigen::Vector3d::Ones(), nullptr),
mTail(_tail),
Expand Down Expand Up @@ -96,15 +96,16 @@ void ArrowShape::setProperties(const Properties& _properties)
}

//==============================================================================
void ArrowShape::setColor(const Eigen::Vector3d& _color)
void ArrowShape::setRGBA(const Eigen::Vector4d& _color)
{
mColor = _color;
for(size_t i=0; i<mMesh->mNumMeshes; ++i)
{
aiMesh* mesh = mMesh->mMeshes[i];
for(size_t j=0; j<mesh->mNumVertices; ++j)
{
mesh->mColors[0][j] = aiColor4D(_color.x(), _color.y(), _color.z(), 1.0f);
mesh->mColors[0][j] = aiColor4D(_color[0], _color[1],
_color[2], _color[3]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions dart/dynamics/ArrowShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ArrowShape : public MeshShape
/// properties.
ArrowShape(const Eigen::Vector3d& _tail, const Eigen::Vector3d& _head,
const Properties& _properties = Properties(),
const Eigen::Vector3d& _color=Eigen::Vector3d(0.5,0.5,1.0),
const Eigen::Vector4d& _color=Eigen::Vector4d(0.5,0.5,1.0,1.0),
size_t _resolution=10);

/// Set the positions of the tail and head of the arrow without changing any
Expand All @@ -90,7 +90,7 @@ class ArrowShape : public MeshShape
void setProperties(const Properties& _properties);

/// Set the color of this arrow
void setColor(const Eigen::Vector3d& _color) override;
void setRGBA(const Eigen::Vector4d& _color) override;

/// Get the properties of this arrow
const Properties& getProperties() const;
Expand Down
27 changes: 24 additions & 3 deletions dart/dynamics/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Shape::Shape(ShapeType _type)
: mBoundingBoxDim(0, 0, 0),
mVolume(0.0),
mID(mCounter++),
mColor(0.5, 0.5, 1.0),
mColor(0.5, 0.5, 1.0, 1.0),
mTransform(Eigen::Isometry3d::Identity()),
mType(_type)
{
Expand All @@ -56,10 +56,31 @@ Shape::~Shape() {
}

void Shape::setColor(const Eigen::Vector3d& _color) {
mColor = _color;
setRGB(_color);
}

const Eigen::Vector3d& Shape::getColor() const {
void Shape::setColor(const Eigen::Vector4d &_color) {
setRGBA(_color);
}

void Shape::setRGB(const Eigen::Vector3d& _rgb) {
mColor << _rgb, mColor[3];
setRGBA(mColor);
}

void Shape::setRGBA(const Eigen::Vector4d& _rgba) {
mColor = _rgba;
}

Eigen::Vector3d Shape::getColor() const {
return getRGB();
}

Eigen::Vector3d Shape::getRGB() const {
return Eigen::Vector3d(mColor[0], mColor[1], mColor[2]);
}

const Eigen::Vector4d& Shape::getRGBA() const {
return mColor;
}

Expand Down
24 changes: 20 additions & 4 deletions dart/dynamics/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,27 @@ class Shape : public virtual common::Subject
/// \brief Destructor
virtual ~Shape();

/// \brief Set color.
virtual void setColor(const Eigen::Vector3d& _color);
/// \brief Set RGB color components (leave alpha alone). Identical to
/// setRGB(const Eigen::Vector3d&)
void setColor(const Eigen::Vector3d& _color);

/// \brief Set RGBA color components
void setColor(const Eigen::Vector4d& _color);

/// \brief Set RGB color components (leave alpha alone)
void setRGB(const Eigen::Vector3d& _rgb);

/// \brief Set RGBA color components
virtual void setRGBA(const Eigen::Vector4d& _rgba);

/// \brief Get color.
const Eigen::Vector3d& getColor() const;
Eigen::Vector3d getColor() const;

/// \brief Get RGB color components
Eigen::Vector3d getRGB() const;

/// \brief Get RGBA color components
const Eigen::Vector4d& getRGBA() const;

/// \brief Get dimensions of bounding box.
/// The dimension will be automatically determined by the sub-classes
Expand Down Expand Up @@ -141,7 +157,7 @@ class Shape : public virtual common::Subject
int mID;

/// \brief Color for the primitive.
Eigen::Vector3d mColor;
Eigen::Vector4d mColor;

/// \brief Local geometric transformation of the Shape w.r.t. parent frame.
Eigen::Isometry3d mTransform;
Expand Down
81 changes: 81 additions & 0 deletions dart/math/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,87 @@ inline int castUIntToInt(size_t _x)
}

} // namespace math

namespace Color
{

inline Eigen::Vector4d Red(double alpha)
{
return Eigen::Vector4d(0.9, 0.1, 0.1, alpha);
}

inline Eigen::Vector3d Red()
{
return Eigen::Vector3d(0.9, 0.1, 0.1);
}

inline Eigen::Vector4d Green(double alpha)
{
return Eigen::Vector4d(0.1, 0.9, 0.1, alpha);
}

inline Eigen::Vector3d Green()
{
return Eigen::Vector3d(0.1, 0.9, 0.1);
}

inline Eigen::Vector4d Blue(double alpha)
{
return Eigen::Vector4d(0.1, 0.1, 0.9, alpha);
}

inline Eigen::Vector3d Blue()
{
return Eigen::Vector3d(0.1, 0.1, 0.9);
}

inline Eigen::Vector4d White(double alpha)
{
return Eigen::Vector4d(1.0, 1.0, 1.0, alpha);
}

inline Eigen::Vector3d White()
{
return Eigen::Vector3d(1.0, 1.0, 1.0);
}

inline Eigen::Vector4d Black(double alpha)
{
return Eigen::Vector4d(0.05, 0.05, 0.05, alpha);
}

inline Eigen::Vector3d Black()
{
return Eigen::Vector3d(0.05, 0.05, 0.05);
}

inline Eigen::Vector4d Gray(double alpha)
{
return Eigen::Vector4d(0.6, 0.6, 0.6, alpha);
}

inline Eigen::Vector3d Gray()
{
return Eigen::Vector3d(0.6, 0.6, 0.6);
}

inline Eigen::Vector4d Random(double alpha)
{
return Eigen::Vector4d(math::random(0.0, 1.0),
math::random(0.0, 1.0),
math::random(0.0, 1.0),
alpha);
}

inline Eigen::Vector3d Random()
{
return Eigen::Vector3d(math::random(0.0, 1.0),
math::random(0.0, 1.0),
math::random(0.0, 1.0));
}

} // namespace Color

} // namespace dart

#endif // DART_MATH_HELPERS_H_