Commit 2b6f52dc authored by Hina Shah's avatar Hina Shah
Browse files

FEAT: Adding color and opacity changing capabilities for imstkGeometry.

FEAT: Adding color for text
ENH: Enabling Depth peeling in vtkViewer for proper rendering of opacity.
ENH: Removing unwanted functions from imstkInteractorStyle
parent b5c311af
...@@ -31,6 +31,7 @@ Geometry::print() const ...@@ -31,6 +31,7 @@ Geometry::print() const
LOG(INFO) << "Position: " << "(" << m_position.x() << ", " << m_position.y() << ", " << m_position.z() << ")"; LOG(INFO) << "Position: " << "(" << m_position.x() << ", " << m_position.y() << ", " << m_position.z() << ")";
LOG(INFO) << "Orientation:\n" << m_orientation.toRotationMatrix(); LOG(INFO) << "Orientation:\n" << m_orientation.toRotationMatrix();
LOG(INFO) << "Scaling: " << m_scaling; LOG(INFO) << "Scaling: " << m_scaling;
LOG(INFO) << "Color: " << m_color;
} }
void void
...@@ -178,6 +179,11 @@ Geometry::resetConfiguration() ...@@ -178,6 +179,11 @@ Geometry::resetConfiguration()
m_configurationModified = true; m_configurationModified = true;
} }
void
Geometry::setColorProperty(double r, double g, double b, double a)
{
m_color.setValue(r, g, b, a);
}
const std::string const std::string
Geometry::getTypeName() const Geometry::getTypeName() const
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "g3log/g3log.hpp" #include "g3log/g3log.hpp"
#include "imstkMath.h" #include "imstkMath.h"
#include "imstkColor.h"
// Eigen // Eigen
#include <Eigen/Geometry> #include <Eigen/Geometry>
...@@ -134,31 +135,59 @@ public: ...@@ -134,31 +135,59 @@ public:
/// ///
const std::string getTypeName() const; const std::string getTypeName() const;
/// ///
/// \brief Returns true if one of position, orientation or scaling is modified /// \brief Returns true if one of position, orientation or scaling is modified
/// ///
bool isConfigurationModified() const { return m_configurationModified; } bool isConfigurationModified() const { return m_configurationModified; }
/// ///
/// \brief Sets the state of configuration modified to desired value /// \brief Sets the state of configuration modified to desired value
/// ///
void setConfigurationModified(const bool state) void setConfigurationModified(const bool state)
{ {
m_configurationModified = state; m_configurationModified = state;
} }
/// ///
/// \brief Reset the geometric configuration to identity /// \brief Reset the geometric configuration to identity
/// ///
void resetConfiguration(); void resetConfiguration();
/// ///
/// \brief /// \brief
/// ///
const AffineTransform3d& getEigenTransform() const const AffineTransform3d& getEigenTransform() const
{ {
return m_transform; return m_transform;
} }
///
/// \brief
///
void setColorProperty(double r, double g, double b, double a);
///
/// \brief
///
imstk::Color getGeometryColor()
{
return m_color;
}
///
/// \brief
///
void setWireFrameMode(bool wireFrameModeOn)
{
m_wireframeMode = wireFrameModeOn;
}
///
/// \brief
bool getWireFrameMode()
{
return m_wireframeMode;
}
protected: protected:
...@@ -170,10 +199,13 @@ protected: ...@@ -170,10 +199,13 @@ protected:
const Quatd& orientation = Quatd::Identity()) : const Quatd& orientation = Quatd::Identity()) :
m_type(type), m_type(type),
m_position(position), m_position(position),
m_orientation(orientation), m_transform() m_orientation(orientation),
{ m_transform(),
m_transform.setIdentity(); m_color(1.,1.,1.),
} m_wireframeMode(false)
{
m_transform.setIdentity();
}
Type m_type; ///> Geometry type Type m_type; ///> Geometry type
...@@ -181,9 +213,11 @@ protected: ...@@ -181,9 +213,11 @@ protected:
Quatd m_orientation; ///> orientation Quatd m_orientation; ///> orientation
double m_scaling = 1; ///> Scaling double m_scaling = 1; ///> Scaling
AffineTransform3d m_transform; ///> Compounded transform of the geometry AffineTransform3d m_transform; ///> Compounded transform of the geometry
imstk::Color m_color;
bool m_configurationModified = true; // true if one of position, orientation or scaling is modified bool m_configurationModified = true; // true if one of position, orientation or scaling is modified
bool m_wireframeMode;
}; };
} //imstk } //imstk
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "vtkPolyDataMapper.h" #include "vtkPolyDataMapper.h"
#include "vtkPolyDataNormals.h" #include "vtkPolyDataNormals.h"
#include "vtkTransform.h" #include "vtkTransform.h"
#include "vtkProperty.h"
namespace imstk namespace imstk
{ {
...@@ -106,6 +107,8 @@ RenderDelegate::setActorMapper(vtkAlgorithmOutput *source) ...@@ -106,6 +107,8 @@ RenderDelegate::setActorMapper(vtkAlgorithmOutput *source)
mapper->SetInputConnection(normalGen->GetOutputPort()); mapper->SetInputConnection(normalGen->GetOutputPort());
m_actor->SetMapper(mapper); m_actor->SetMapper(mapper);
this->setColorAndOpacity();
this->setWireFrameMode();
} }
vtkSmartPointer<vtkActor> vtkSmartPointer<vtkActor>
...@@ -121,6 +124,23 @@ RenderDelegate::update() ...@@ -121,6 +124,23 @@ RenderDelegate::update()
this->updateActorTransform(); this->updateActorTransform();
} }
void
RenderDelegate::setColorAndOpacity()
{
imstk::Color geomColor = this->getGeometry()->getGeometryColor();
this->m_actor->GetProperty()->SetColor(geomColor.r, geomColor.g, geomColor.b);
this->m_actor->GetProperty()->SetOpacity(geomColor.a);
}
void
RenderDelegate::setWireFrameMode()
{
if (this->getGeometry()->getWireFrameMode())
this->m_actor->GetProperty()->SetRepresentationToWireframe();
else
this->m_actor->GetProperty()->SetRepresentationToSurface();
}
void void
RenderDelegate::setVtkTrasnformFromEigen(const AffineTransform3d& t) RenderDelegate::setVtkTrasnformFromEigen(const AffineTransform3d& t)
{ {
......
...@@ -78,10 +78,20 @@ public: ...@@ -78,10 +78,20 @@ public:
/// ///
void updateActorTransform(); void updateActorTransform();
/// ///
/// \brief /// \brief
/// ///
void setVtkTrasnformFromEigen(const AffineTransform3d& t); void setVtkTrasnformFromEigen(const AffineTransform3d& t);
///
/// \brief
///
void setColorAndOpacity();
///
/// \brief
///
void setWireFrameMode();
protected: protected:
/// ///
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <vtkImageReader2.h> #include <vtkImageReader2.h>
#include <vtkTexture.h> #include <vtkTexture.h>
#include <vtkProperty.h> #include <vtkProperty.h>
#include <vtkPolyDataNormals.h>
#include "g3log/g3log.hpp" #include "g3log/g3log.hpp"
...@@ -69,6 +70,10 @@ SurfaceMeshRenderDelegate::SurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh ...@@ -69,6 +70,10 @@ SurfaceMeshRenderDelegate::SurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh
polydata->SetPoints(points); polydata->SetPoints(points);
polydata->SetPolys(triangles); polydata->SetPolys(triangles);
/* auto normalGen = vtkSmartPointer<vtkPolyDataNormals>::New();
normalGen->SetInputData(polydata);
normalGen->SplittingOff();*/
// Mapper // Mapper
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polydata); mapper->SetInputData(polydata);
...@@ -129,6 +134,9 @@ SurfaceMeshRenderDelegate::SurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh ...@@ -129,6 +134,9 @@ SurfaceMeshRenderDelegate::SurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh
// Actor // Actor
m_actor->SetMapper(mapper); m_actor->SetMapper(mapper);
this->setColorAndOpacity();
this->setWireFrameMode();
// Transform // Transform
this->updateActorTransform(); this->updateActorTransform();
} }
......
...@@ -51,10 +51,10 @@ public: ...@@ -51,10 +51,10 @@ public:
/// ///
SurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh>SurfaceMesh); SurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh>SurfaceMesh);
/// ///
/// \brief /// \brief
/// ///
void mapVertices(); void mapVertices();
/// ///
......
...@@ -115,6 +115,7 @@ Renderer::setup(Mode mode) ...@@ -115,6 +115,7 @@ Renderer::setup(Mode mode)
} }
m_vtkRenderer->SetActiveCamera(m_defaultVtkCamera); m_vtkRenderer->SetActiveCamera(m_defaultVtkCamera);
m_vtkRenderer->SetUseDepthPeeling(true);
} }
else if( mode == Mode::DEBUG && m_currentMode != Mode::DEBUG ) else if( mode == Mode::DEBUG && m_currentMode != Mode::DEBUG )
{ {
...@@ -131,6 +132,7 @@ Renderer::setup(Mode mode) ...@@ -131,6 +132,7 @@ Renderer::setup(Mode mode)
m_vtkRenderer->SetActiveCamera(m_defaultVtkCamera); m_vtkRenderer->SetActiveCamera(m_defaultVtkCamera);
m_vtkRenderer->ResetCamera(); m_vtkRenderer->ResetCamera();
m_vtkRenderer->SetUseDepthPeeling(true);
} }
else if ( mode == Mode::SIMULATION && m_currentMode != Mode::SIMULATION ) else if ( mode == Mode::SIMULATION && m_currentMode != Mode::SIMULATION )
{ {
...@@ -149,6 +151,10 @@ Renderer::setup(Mode mode) ...@@ -149,6 +151,10 @@ Renderer::setup(Mode mode)
m_vtkRenderer->SetActiveCamera(m_sceneVtkCamera); m_vtkRenderer->SetActiveCamera(m_sceneVtkCamera);
m_vtkRenderer->ResetCameraClippingRange(); m_vtkRenderer->ResetCameraClippingRange();
// Choose to use depth peeling (if supported) (initial value is 0 (false)):
m_vtkRenderer->SetUseDepthPeeling(true);
} }
m_currentMode = mode; m_currentMode = mode;
...@@ -167,7 +173,7 @@ Renderer::updateSceneCamera(std::shared_ptr<Camera> imstkCam) ...@@ -167,7 +173,7 @@ Renderer::updateSceneCamera(std::shared_ptr<Camera> imstkCam)
m_sceneVtkCamera->SetFocalPoint(f[0], f[1], f[2]); m_sceneVtkCamera->SetFocalPoint(f[0], f[1], f[2]);
m_sceneVtkCamera->SetViewUp(v[0], v[1], v[2]); m_sceneVtkCamera->SetViewUp(v[0], v[1], v[2]);
m_sceneVtkCamera->SetViewAngle(imstkCam->getViewAngle()); m_sceneVtkCamera->SetViewAngle(imstkCam->getViewAngle());
m_sceneVtkCamera->Zoom(imstkCam->getZoomFactor()); m_sceneVtkCamera->Zoom(imstkCam->getZoomFactor());
} }
void void
......
...@@ -51,236 +51,236 @@ namespace imstk ...@@ -51,236 +51,236 @@ namespace imstk
{ {
vtkStandardNewMacro(InteractorStyle); vtkStandardNewMacro(InteractorStyle);
//
bool //bool
InteractorStyle::parseLogFileAndCalculateMetrics(std::string filename, vtkSmartPointer<vtkPoints> outPoints, //InteractorStyle::parseLogFileAndCalculateMetrics(std::string filename, vtkSmartPointer<vtkPoints> outPoints,
vtkSmartPointer<vtkFloatArray> outVelocities, // vtkSmartPointer<vtkFloatArray> outVelocities,
vtkSmartPointer<vtkFloatArray> outAccelerations, // vtkSmartPointer<vtkFloatArray> outAccelerations,
vtkSmartPointer<vtkFloatArray> outJerks) // vtkSmartPointer<vtkFloatArray> outJerks)
{ //{
// open logger file // // open logger file
fstream fstr(filename.c_str(), fstream::in); // fstream fstr(filename.c_str(), fstream::in);
if (!fstr.is_open() || !fstr.good() || fstr.eof()) // if (!fstr.is_open() || !fstr.good() || fstr.eof())
{ // {
LOG(WARNING) << "ERROR opening " << filename ; // LOG(WARNING) << "ERROR opening " << filename ;
return false; // return false;
} // }
//
// parse logger file // // parse logger file
double min = MAX_D; // double min = MAX_D;
double max = MIN_D; // double max = MIN_D;
char line[1024]; // char line[1024];
while (fstr.good()) // while (fstr.good())
{ // {
fstr.getline(line, 1024); // fstr.getline(line, 1024);
if (line[0] == '\0') continue; // if (line[0] == '\0') continue;
//
char *ptr; // char *ptr;
double x = 0.0, y = 0.0, z = 0.0; // double x = 0.0, y = 0.0, z = 0.0;
int columnNumber = 0; // int columnNumber = 0;
bool reTokenise = false; // bool reTokenise = false;
//
if (strncmp(line, ",", 1) == 0) // if (strncmp(line, ",", 1) == 0)
{ // {
ptr = nullptr; // ptr = nullptr;
reTokenise = true; // reTokenise = true;
} // }
else // else
{ // {
ptr = strtok(line, ","); // ptr = strtok(line, ",");
} // }
//
// Read columns // // Read columns
while (columnNumber < 4) // while (columnNumber < 4)
{ // {
if (ptr != nullptr) // if (ptr != nullptr)
{ // {
if (columnNumber == 1) // if (columnNumber == 1)
{ // {
x = atof(ptr); // x = atof(ptr);
} // }
else if (columnNumber == 2) // else if (columnNumber == 2)
{ // {
y = atof(ptr); // y = atof(ptr);
} // }
else if (columnNumber == 3) // else if (columnNumber == 3)
{ // {
z = atof(ptr); // z = atof(ptr);
} // }
} // }
//
if (reTokenise == false) // if (reTokenise == false)
{ // {
ptr = strtok(NULL, ","); // ptr = strtok(NULL, ",");
} // }
else // else
{ // {
ptr = strtok(line, ","); // ptr = strtok(line, ",");
reTokenise = false; // reTokenise = false;
} // }
columnNumber++; // columnNumber++;
} // }
//
// Add points or velocities // // Add points or velocities
if (line[0] == 'P') // if (line[0] == 'P')
{ // {
outPoints->InsertNextPoint(x, y, z); // outPoints->InsertNextPoint(x, y, z);
} // }
else if (line[0] == 'V') // else if (line[0] == 'V')
{ // {
auto norm = Vec3d(x, y, z).norm(); // auto norm = Vec3d(x, y, z).norm();
outVelocities->InsertNextValue(norm); // outVelocities->InsertNextValue(norm);
} // }
} // }
//
auto numpts = outPoints->GetNumberOfPoints(); // auto numpts = outPoints->GetNumberOfPoints();
auto numdata = outVelocities->GetNumberOfValues(); // auto numdata = outVelocities->GetNumberOfValues();
if (numpts != numdata) // if (numpts != numdata)
{ // {
LOG(WARNING) << "ERROR reading " << filename << " : inconsistant number of points & velocity " // LOG(WARNING) << "ERROR reading " << filename << " : inconsistant number of points & velocity "
<< "(" << numpts << " & " << numdata << ")"; // << "(" << numpts << " & " << numdata << ")";
return false; // return false;
} // }
//
//Calculate accelerations and jerks for each point // //Calculate accelerations and jerks for each point
for (auto index = 0; index < numpts; index++) // for (auto index = 0; index < numpts; index++)
{ // {
//
if (index < 2 || index >= numpts - 2) // if (index < 2 || index >= numpts - 2)
{ // {
//Ignoring the boundary points // //Ignoring the boundary points
outJerks->InsertNextTuple3(0., 0., 0.); // outJerks->InsertNextTuple3(0., 0., 0.);
if (index == 0 || index == numpts - 1) // if (index == 0 || index == numpts - 1)
outAccelerations->InsertNextTuple3(0., 0., 0.); // outAccelerations->InsertNextTuple3(0., 0., 0.);
continue; // continue;
} // }
//
Vec3d x(0., 0., 0.); // Vec3d x(0., 0., 0.);
Vec3d x_min1(0., 0., 0.); // Vec3d x_min1(0., 0., 0.);
Vec3d x_min2(0., 0., 0.); // Vec3d x_min2(0., 0., 0.);
Vec3d x_plus1(0., 0., 0.); // Vec3d x_plus1(0., 0., 0.);
Vec3d x_plus2(0., 0., 0.); // Vec3d x_plus2(0., 0., 0.);
//
outPoints->GetPoint(index, x.data()); // outPoints->GetPoint(index, x.data());
//
//
auto newInd = index - 1; // auto newInd = index - 1;
outPoints->GetPoint(newInd, x_min1.data()); // outPoints->GetPoint(newInd, x_min1.data());
//
newInd = index - 2; // newInd = index - 2;
outPoints->GetPoint(newInd, x_min2.data()); // outPoints->GetPoint(newInd, x_min2.data());
//
newInd = index + 1; // newInd = index + 1;
outPoints->GetPoint(newInd, x_plus1.data()); // outPoints->GetPoint(newInd, x_plus1.data());
//
newInd = index + 2; // newInd = index + 2;
outPoints->GetPoint(newInd, x_plus2.data()); // outPoints->GetPoint(newInd, x_plus2.data());
//
//acceleration calculated as a second central derivative of position wrt time, h=1 // //acceleration calculated as a second central derivative of position wrt time, h=1
auto acceleration = x_plus1 - 2 * x + x_min1; // auto acceleration = x_plus1 - 2 * x + x_min1;
//jerk calculated as the third derivative of position wrt time, h=1