Skip to content
Snippets Groups Projects
Commit 3a3648da authored by Alexis Girault's avatar Alexis Girault
Browse files

WIP: Add Light and Color

Light still a WIP
parent 4f9a8cbe
No related branches found
No related tags found
No related merge requests found
...@@ -5,12 +5,13 @@ include(imstkAddLibrary) ...@@ -5,12 +5,13 @@ include(imstkAddLibrary)
imstk_add_library( Core imstk_add_library( Core
H_FILES H_FILES
imstkMath.h imstkMath.h
imstkColor.h
imstkModule.h imstkModule.h
CPP_FILES CPP_FILES
imstkColor.cpp
imstkModule.cpp imstkModule.cpp
LIBRARIES LIBRARIES
Utilities Utilities
Eigen
) )
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkColor.h"
#include <g3log/g3log.hpp>
namespace imstk {
bool isColorRangeCorrect( double c )
{
return (c >= 0 && c <= 1.0);
}
Color Color::White(1.0, 1.0, 1.0, 1.0);
Color Color::Black(0.0, 0.0, 0.0, 1.0);
Color Color::DarkGray(0.8, 0.8, 0.8, 1.0);
Color Color::LightGray(0.3, 0.3, 0.3, 1.0);
Color Color::Blue(0.0, 0.0, 1.0, 1.0);
Color Color::Green(0.0, 1.0, 0.0, 1.0);
Color Color::Red(1.0, 0.0, 0.0, 1.0);
Color Color::Yellow(1.0, 1.0, 0.0, 1.0);
Color Color::Pink(1.0, 0.0, 1.0, 1.0);
Color::Color()
{
rgba[0] = 0.8f;
rgba[1] = 0.8f;
rgba[2] = 0.8f;
rgba[3] = 1.0f;
}
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;
}
rgba[0] = r;
rgba[1] = g;
rgba[2] = b;
rgba[3] = a;
}
Color &
Color::operator=(const Color &p_color )
{
rgba[0] = p_color.rgba[0];
rgba[1] = p_color.rgba[1];
rgba[2] = p_color.rgba[2];
rgba[3] = p_color.rgba[3];
return *this;
}
double
Color::operator()( int p_i ) const
{
if ( p_i < 0 || p_i > 3 )
{
return -1;
}
return rgba[p_i];
}
std::ostream& operator<<(std::ostream& os, const Color& c)
{
os << c.r << ' ' << c.g << ' ' << c.b << ' ' << c.a;
return os;
}
void
Color::darken( double p_darkFactor )
{
rgba[0] = ( rgba[1] - rgba[1] * ( p_darkFactor ) );
rgba[1] = ( rgba[2] - rgba[2] * ( p_darkFactor ) );
rgba[2] = ( rgba[3] - rgba[3] * ( p_darkFactor ) );
rgba[0] = ( rgba[0] < 0 ? 0 : rgba[0] );
rgba[1] = ( rgba[1] < 0 ? 0 : rgba[1] );
rgba[2] = ( rgba[2] < 0 ? 0 : rgba[2] );
}
void
Color::lighten( double p_darkFactor )
{
rgba[0] = rgba[1] + rgba[1] * ( p_darkFactor );
rgba[1] = rgba[2] + rgba[2] * ( p_darkFactor );
rgba[2] = rgba[3] + rgba[3] * ( p_darkFactor );
rgba[0] = ( rgba[0] > 1.0 ? 1.0 : rgba[0] );
rgba[1] = ( rgba[1] < 1.0 ? 1.0 : rgba[1] );
rgba[2] = ( rgba[2] < 1.0 ? 1.0 : rgba[2] );
}
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)
{
LOG(WARNING) << "Can not set Color: value outside of [0.0, 1.0] range.";
return;
}
rgba[0] = p_red;
rgba[1] = p_green;
rgba[2] = p_blue;
rgba[3] = p_alpha;
}
void
Color::getValue(double color[4])
{
color[0] = rgba[0];
color[1] = rgba[1];
color[2] = rgba[2];
color[3] = rgba[3];
}
const double *
Color::getValue() const
{
return rgba;
}
}
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkColor_h
#define imstkColor_h
#include <iostream>
namespace imstk {
struct Color
{
union
{
double rgba[4];
struct
{
double r;
double g;
double b;
double a;
};
};
Color();
Color(double r, double g, double b, double a = 1.0);
Color &operator=(const Color &p_color);
friend std::ostream& operator<<(std::ostream& os, const Color& c);
/// \brief returns the color value given with the index
double operator()(int p_i) const;
/// \brief Dark ratio. the valu is between 0 and 1.0
void darken(double p_darkFactor);
/// \brief lighten the color
void lighten(double p_darkFactor);
/// \brief set RGB color
void setValue(double p_red, double p_green, double p_blue, double p_alpha = 1.0);
/// \brief get RGB color
void getValue(double color[4]);
/// \brief get RGB color
const double *getValue() const;
static Color White;
static Color Black;
static Color DarkGray;
static Color LightGray;
static Color Blue;
static Color Green;
static Color Red;
static Color Pink;
static Color Yellow;
};
}
#endif // ifndef imstkColor_h
...@@ -5,9 +5,11 @@ include(imstkAddLibrary) ...@@ -5,9 +5,11 @@ include(imstkAddLibrary)
imstk_add_library( Scene imstk_add_library( Scene
H_FILES H_FILES
imstkScene.h imstkScene.h
imstkLight.h
imstkSceneObject.h imstkSceneObject.h
CPP_FILES CPP_FILES
imstkScene.cpp imstkScene.cpp
imstkLight.cpp
imstkSceneObject.cpp imstkSceneObject.cpp
LIBRARIES LIBRARIES
Core Core
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkLight.h"
#include <g3log/g3log.hpp>
namespace imstk {
const Vec3d
Light::getPosition() const
{
double p[3];
m_vtkLight->GetPosition(p);
return Vec3d(p[0], p[1], p[2]);
}
void
Light::setPosition(const Vec3d& p)
{
m_vtkLight->SetPosition(p[0], p[1], p[2]);
}
void
Light::setPosition(const double& x,
const double& y,
const double& z)
{
m_vtkLight->SetPosition(x, y, z);
}
const Color
Light::getColor() const
{
Color c;
m_vtkLight->GetDiffuseColor(c.rgba);
return c;
}
void
Light::setColor(const Color& c)
{
m_vtkLight->SetColor(c(0), c(1), c(2));
}
const std::string&
Light::getName() const
{
return m_name;
}
void
Light::setName(std::string name)
{
m_name = name;
}
}
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkLight_h
#define imstkLight_h
#include <string>
#include <vtkSmartPointer.h>
#include <vtkLight.h>
#include "imstkMath.h"
#include "imstkColor.h"
namespace imstk {
class Light
{
public:
Light(std::string name) :
m_name(name)
{}
~Light() = default;
const Vec3d getPosition() const;
void setPosition(const Vec3d& p);
void setPosition(const double& x,
const double& y,
const double& z);
const Color getColor() const;
void setColor(const Color& c);
const std::string& getName() const;
void setName(std::string name);
protected:
vtkSmartPointer<vtkLight> m_vtkLight = vtkSmartPointer<vtkLight>::New();
std::string m_name;
};
}
#endif // ifndef imstkLight_h
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "imstkPlane.h" #include "imstkPlane.h"
#include "imstkSphere.h" #include "imstkSphere.h"
#include "imstkCube.h" #include "imstkCube.h"
#include "imstkLight.h"
#include "imstkMath.h" #include "imstkMath.h"
#include "imstkSimulationManager.h" #include "imstkSimulationManager.h"
...@@ -50,6 +51,10 @@ int main() ...@@ -50,6 +51,10 @@ int main()
auto sphereObj = std::make_shared<imstk::VisualObject>("VisualSphere"); auto sphereObj = std::make_shared<imstk::VisualObject>("VisualSphere");
sphereObj->setVisualGeometry(sphereGeom); sphereObj->setVisualGeometry(sphereGeom);
auto light = std::make_shared<imstk::Light>("MainLight");
light->setPosition(imstk::Vec3d(1,2,3));
light->setColor(imstk::Color::Green);
auto sceneTest = sdk->createNewScene("SceneTest"); auto sceneTest = sdk->createNewScene("SceneTest");
sceneTest->setLoopDelay(1000); sceneTest->setLoopDelay(1000);
sceneTest->addSceneObject(planeObj); sceneTest->addSceneObject(planeObj);
...@@ -195,4 +200,4 @@ void testGeometryMaps() ...@@ -195,4 +200,4 @@ void testGeometryMaps()
geometryMapTest->addSceneObject(sphereObj); geometryMapTest->addSceneObject(sphereObj);
sdk->startSimulation("geometryMapTest"); sdk->startSimulation("geometryMapTest");
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment