Commit ee32f81b authored by Alexis Girault's avatar Alexis Girault
Browse files

Merge branch 'geometry-sprint' into 'refactoring'

Geometry sprint



See merge request !80
parents 94c7b9c2 14d8ae9e
......@@ -3,10 +3,8 @@
#-----------------------------------------------------------------------------
include(imstkAddLibrary)
imstk_add_library( Core
H_FILES
imstkModule.h
CPP_FILES
imstkModule.cpp
DEPENDS
g3log
)
#-----------------------------------------------------------------------------
......
/*=========================================================================
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 << "R = " << c.r << '\n'
<< "G = " << c.g << '\n'
<< "B = " << c.b << '\n'
<< "\u03B1 = " << 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
/*=========================================================================
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 "imstkLogUtility.h"
namespace imstk {
stdSink::FG_Color
stdSink::GetColor(const LEVELS level) const
{
if (level.value == WARNING.value) return YELLOW;
if (level.value == DEBUG.value) return GREEN;
if (level.value == FATAL.value) return RED;
return WHITE;
}
void
stdSink::ReceiveLogMessage(g3::LogMessageMover logEntry)
{
auto level = logEntry.get()._level;
auto color = GetColor(level);
std::cout << "\033[" << color << "m"
<< logEntry.get().message()
<< "\033[m" << std::endl;
}
void
LogUtility::createLogger(std::string name, std::string path)
{
m_g3logWorker = g3::LogWorker::createLogWorker();
m_fileSinkHandle = m_g3logWorker->addDefaultLogger(name, path);
m_stdSinkHandle = m_g3logWorker->addSink(
std2::make_unique<stdSink>(), &stdSink::ReceiveLogMessage);
g3::initializeLogging(m_g3logWorker.get());
}
}
\ No newline at end of file
......@@ -34,37 +34,12 @@ struct stdSink {
// http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
enum FG_Color { YELLOW = 33, RED = 31, GREEN = 32, WHITE = 97 };
FG_Color GetColor(const LEVELS level) const
{
if (level.value == WARNING.value) return YELLOW;
if (level.value == DEBUG.value) return GREEN;
if (level.value == FATAL.value) return RED;
return WHITE;
}
void ReceiveLogMessage(g3::LogMessageMover logEntry)
{
auto level = logEntry.get()._level;
auto color = GetColor(level);
std::cout << "\033[" << color << "m"
<< logEntry.get().message()
<< "\033[m" << std::endl;
}
FG_Color GetColor(const LEVELS level) const;
void ReceiveLogMessage(g3::LogMessageMover logEntry);
};
struct LogUtility {
void createLogger(std::string name, std::string path)
{
m_g3logWorker = g3::LogWorker::createLogWorker();
m_fileSinkHandle = m_g3logWorker->addDefaultLogger(name, path);
m_stdSinkHandle = m_g3logWorker->addSink(
std2::make_unique<stdSink>(), &stdSink::ReceiveLogMessage);
g3::initializeLogging(m_g3logWorker.get());
}
void createLogger(std::string name, std::string path);
std::unique_ptr<g3::LogWorker> m_g3logWorker;
std::unique_ptr<g3::SinkHandle<g3::FileSink> >m_fileSinkHandle;
......
/*=========================================================================
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 imstkMath_h
#define imstkMath_h
#include <vector>
#include <Eigen/Geometry>
namespace imstk {
// 2D vector
using Vec2f = Eigen::Vector2f;
using Vec2d = Eigen::Vector2d;
// 3D vector
using Vec3f = Eigen::Vector3f;
using Vec3d = Eigen::Vector3d;
// 4D vector
using Vec4f = Eigen::Vector4f;
using Vec4d = Eigen::Vector4d;
// Dynamic size vector
using VecNf = Eigen::VectorXf;
using VecNd = Eigen::VectorXd;
// Quaternion
using Quatf = Eigen::Quaternionf;
using Quatd = Eigen::Quaterniond;
// 3x3 Matrix
using Mat3f = Eigen::Matrix3f;
using Mat3d = Eigen::Matrix3d;
// 4x4 Matrix
using Mat4f = Eigen::Matrix4f;
using Mat4d = Eigen::Matrix4d;
// Rigid transform (translation and rotation)
using RigidTransform3f = Eigen::Isometry3f;
using RigidTransform3d = Eigen::Isometry3d;
// Affine transform (translation, rotation, scaling and shearing)
using AffineTransform3f = Eigen::Affine3f;
using AffineTransform3d = Eigen::Affine3d;
// Handy cartesian vectors in 3d
#define UP_VECTOR Vec3d(0.0, 1.0, 0.0)
#define DOWN_VECTOR Vec3d(0, -1, 0)
#define RIGHT_VECTOR Vec3d(1, 0, 0)
#define LEFT_VECTOR Vec3d(-1, 0, 0)
#define FORWARD_VECTOR Vec3d(0, 0, -1)
#define BACKWARD_VECTOR Vec3d(0, 0, 1)
#define WORLD_ORIGIN Vec3d::Zero()
// Some commonly used math constants
const double PI = 3.14159265358979323846;
const double PI_2 = 1.57079632679489661923;
const double PI_4 = 0.785398163397448309616;
const double INV_1_PI = 0.318309886183790671538;
const double INV_2_PI = 0.636619772367581343076;
const double TWO_OVER_SQRTPI = 1.12837916709551257390;
const double SQRT2 = 1.41421356237309504880;
const double SQRT1_2 = 0.707106781186547524401;
const double NLOG_E = 2.71828182845904523536;
const double LOG2E = 1.44269504088896340736;
const double LOG10E = 0.434294481903251827651;
const double LN2 = 0.693147180559945309417;
const double LN10 = 2.30258509299404568402;
const double MAX_D = std::numeric_limits<double>::max();
const double MIN_D = std::numeric_limits<double>::min();
}
#endif // ifndef imstkMath_h
/*=========================================================================
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 "imstkCube.h"
namespace imstk {
void
Cube::print() const
{
Geometry::print();
LOG(INFO) << "Width: " << m_width;
}
double
Cube::getVolume() const
{
return m_width*m_width;
}
const double&
Cube::getWidth() const
{
return m_width;
}
void
Cube::setWidth(const double& width)
{
m_width = width;
}
}
/*=========================================================================
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 imstkCube_h
#define imstkCube_h
#include "imstkGeometry.h"
namespace imstk {
///
/// \class Cube
///
/// \brief Cube geometry
///
class Cube : public Geometry
{
public:
Cube(const Vec3d & position = WORLD_ORIGIN,
const double& width = 1) :
Geometry(GeometryType::Cube,
position,
Quatd::Identity()),
m_width(width)
{}
~Cube() = default;
///
/// \brief Print the cube info
///
void print() const;
///
/// \brief Returns the volume of the cube
///
double getVolume() const override;
// Accessors
///
/// \brief Returns the width of the cube
///
const double& getWidth() const;
///
/// \brief Sets the width of the cube
///
void setWidth(const double& width);
protected:
double m_width; ///> width of the cube
};
}
#endif // ifndef imstkCube_h
/*=========================================================================
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 "imstkPlane.h"
namespace imstk {
void
Plane::print() const
{
Geometry::print();
LOG(INFO) << "Width: " << m_width;
}
double
Plane::getVolume() const
{
return 0.0;
}
Vec3d
Plane::getNormal() const
{
return m_orientation._transformVector(UP_VECTOR);
}
void
Plane::setNormal(const Vec3d& normal)
{
this->setOrientation(Quatd::FromTwoVectors(UP_VECTOR, normal));