Commit fea4577f authored by Alexis Girault's avatar Alexis Girault

WIP: Eigen, Geometry, Plane

parent 94c7b9c2
......@@ -4,6 +4,7 @@
include(imstkAddLibrary)
imstk_add_library( Core
H_FILES
imstkMath.h
imstkModule.h
CPP_FILES
imstkModule.cpp
......
/*=========================================================================
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;
// Dynamic size vector
using VecNf = Eigen::VectorXf;
using VecNd = Eigen::VectorXd;
// Quaternion
using Quatf = Eigen::Quaternionf;
using Quatd = Eigen::Quaterniond;
#define UP Vec3d(0.0, 1.0, 0.0)
#define DOWN Vec3d(0, -1, 0)
#define RIGHT Vec3d(1, 0, 0)
#define LEFT Vec3d(-1, 0, 0)
#define FORWARD Vec3d(0, 0, 1)
#define BACKWARD Vec3d(0, 0, -1)
}
#endif // ifndef imstkMath_h
#-----------------------------------------------------------------------------
# Create target
#-----------------------------------------------------------------------------
include(imstkAddLibrary)
imstk_add_library( Geometry
H_FILES
imstkGeometry.h
imstkAnalyticalGeometry.h
imstkPlane.h
CPP_FILES
imstkGeometry.cpp
imstkAnalyticalGeometry.cpp
imstkPlane.cpp
LIBRARIES
Core
)
#-----------------------------------------------------------------------------
# Testing
#-----------------------------------------------------------------------------
if( iMSTK_BUILD_TESTING )
add_subdirectory( Testing )
endif()
/*=========================================================================
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 "imstkAnalyticalGeometry.h"
namespace imstk {
void
AnalyticalGeometry::translate(const Vec3d& t)
{
m_position += t;
}
void
AnalyticalGeometry::rotate(const Quatd& r)
{
m_orientation *= r;
}
void
AnalyticalGeometry::rotate(const Vec3d& axis, const double& angle)
{
this->rotate(Quatd(Eigen::AngleAxisd(angle, axis)));
}
const Vec3d&
AnalyticalGeometry::getPosition() const
{
return m_position;
}
void
AnalyticalGeometry::setPosition(const Vec3d& position)
{
m_position = position;
}
void
AnalyticalGeometry::setOrientation(const Quatd& orientation)
{
m_orientation = orientation;
}
const Quatd&
AnalyticalGeometry::getOrientation() const
{
return m_orientation;
}
}
/*=========================================================================
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 imstkAnalyticalGeometry_h
#define imstkAnalyticalGeometry_h
#include "imstkGeometry.h"
namespace imstk {
class AnalyticalGeometry : public Geometry
{
public:
~AnalyticalGeometry() = default;
void translate(const Vec3d& t) override;
void rotate(const Quatd& r) override;
void rotate(const Vec3d & axis,
const double& angle) override;
const Vec3d& getPosition() const;
void setPosition(const Vec3d& position);
const Quatd& getOrientation() const;
void setOrientation(const Quatd& orientation);
protected:
AnalyticalGeometry(GeometryType type,
const Vec3d& position = Vec3d(),
const Quatd& orientation = Quatd()) :
Geometry(type),
m_position(position),
m_orientation(orientation)
{}
Vec3d m_position;
Quatd m_orientation;
};
}
#endif // ifndef imstkAnalyticalGeometry_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 "imstkGeometry.h"
namespace imstk {
const GeometryType&
Geometry::getType() const
{
return m_type;
}
}
/*=========================================================================
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 imstkGeometry_h
#define imstkGeometry_h
#include "imstkMath.h"
namespace imstk {
enum class GeometryType
{
Plane,
Sphere,
Cube,
SurfaceMesh,
TetrahedralMesh,
HexahedralMesh
};
class Geometry
{
public:
~Geometry() = default;
virtual void translate(const Vec3d& t) = 0;
virtual void rotate(const Quatd& r) = 0;
virtual void rotate(const Vec3d & axis,
const double& angle) = 0;
const GeometryType& getType() const;
protected:
Geometry(GeometryType type) : m_type(type) {}
GeometryType m_type;
};
}
#endif // ifndef imstkGeometry_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 {
Vec3d
Plane::getNormal()
{
return m_orientation._transformVector(UP);
}
void
Plane::setNormal(const Vec3d& normal)
{
this->setOrientation(Quatd::FromTwoVectors(UP, normal));
}
const double&
Plane::getWidth() const
{
return m_width;
}
void
Plane::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 imstkPlane_h
#define imstkPlane_h
#include "imstkAnalyticalGeometry.h"
namespace imstk {
class Plane : public AnalyticalGeometry
{
public:
Plane(const Vec3d & position = Vec3d(),
const Vec3d & normal = UP,
const double& width = 100) :
AnalyticalGeometry(GeometryType::Plane,
position,
Quatd::FromTwoVectors(UP, normal)),
m_width(width)
{}
~Plane() = default;
Vec3d getNormal();
void setNormal(const Vec3d& normal);
const double& getWidth() const;
void setWidth(const double& width);
protected:
double m_width;
};
}
#endif // ifndef imstkPlane_h
......@@ -9,6 +9,7 @@ imstk_add_library( Scene
imstkScene.cpp
LIBRARIES
Core
Geometry
)
#-----------------------------------------------------------------------------
......
......@@ -26,17 +26,20 @@
#include "g3log/g3log.hpp"
namespace imstk {
void Scene::initModule()
void
Scene::initModule()
{
LOG(DEBUG) << m_name << " : init";
}
void Scene::cleanUpModule()
void
Scene::cleanUpModule()
{
LOG(DEBUG) << m_name << " : cleanUp";
}
void Scene::runModule()
void
Scene::runModule()
{
LOG(DEBUG) << m_name << " : running";
}
......
#-----------------------------------------------------------------------------
# Add External Project
#-----------------------------------------------------------------------------
include(imstkAddExternalProject)
imstk_add_external_project( Eigen
REPOSITORY ${git_protocol}://github.com/RLovelett/eigen.git
GIT_TAG e81ac4502ebbfde43a1e1761b36853d3ead47c33
#DEPENDENCIES ""
#VERBOSE
)
......@@ -26,7 +26,7 @@ ExternalProject_Include_Dependencies( ${extProj}
# Set extProject directory
#-----------------------------------------------------------------------------
set(${extProj}_SOURCE_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/src)
set(${extProj}_PREFIX_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/prefix)
set(${extProj}_PREFIX_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/cmake)
set(${extProj}_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/build)
#-----------------------------------------------------------------------------
......
#-----------------------------------------------------------------------------
# Set min version to find if not set
#-----------------------------------------------------------------------------
if(NOT Eigen_FIND_VERSION)
if(NOT Eigen_FIND_VERSION_MAJOR)
set(Eigen_FIND_VERSION_MAJOR 3)
endif()
if(NOT Eigen_FIND_VERSION_MINOR)
set(Eigen_FIND_VERSION_MINOR 0)
endif()
if(NOT Eigen_FIND_VERSION_PATCH)
set(Eigen_FIND_VERSION_PATCH 0)
endif()
set(Eigen_FIND_VERSION "${Eigen_FIND_VERSION_MAJOR}.${Eigen_FIND_VERSION_MINOR}.${Eigen_FIND_VERSION_PATCH}")
endif()
#-----------------------------------------------------------------------------
# Macro checking version
#-----------------------------------------------------------------------------
macro(_eigen_check_version)
endmacro(_eigen_check_version)
#-----------------------------------------------------------------------------
# Find path
#-----------------------------------------------------------------------------
set(Eigen_INCLUDE_DIR ${Eigen_SOURCE_DIR})
mark_as_advanced(Eigen_INCLUDE_DIR)
message(STATUS "Eigen_INCLUDE_DIR : ${Eigen_INCLUDE_DIR}")
#-----------------------------------------------------------------------------
# Macro checking version
#-----------------------------------------------------------------------------
file(READ "${Eigen_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen_version_header)
string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen_world_version_match "${_eigen_version_header}")
set(EIGEN_WORLD_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen_major_version_match "${_eigen_version_header}")
set(EIGEN_MAJOR_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen_minor_version_match "${_eigen_version_header}")
set(EIGEN_MINOR_VERSION "${CMAKE_MATCH_1}")
set(Eigen_VERSION ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})
if(${Eigen_VERSION} VERSION_LESS ${Eigen_FIND_VERSION})
set(Eigen_VERSION_OK FALSE)
else(${Eigen_VERSION} VERSION_LESS ${Eigen_FIND_VERSION})
set(Eigen_VERSION_OK TRUE)
endif(${Eigen_VERSION} VERSION_LESS ${Eigen_FIND_VERSION})
if(NOT Eigen_VERSION_OK)
message(STATUS "Eigen version ${Eigen_VERSION} found in ${Eigen_INCLUDE_DIR},"
"but at least version ${Eigen_FIND_VERSION} is required")
endif(NOT Eigen_VERSION_OK)
#-----------------------------------------------------------------------------
# Find package
#-----------------------------------------------------------------------------
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Eigen
REQUIRED_VARS
Eigen_INCLUDE_DIR
Eigen_VERSION_OK)
#-----------------------------------------------------------------------------
# If missing target, create it
#-----------------------------------------------------------------------------
if(G3LOG_FOUND AND NOT TARGET Eigen)
add_library(Eigen INTERFACE IMPORTED)
set_target_properties(Eigen PROPERTIES
INTERFACE_LINK_LIBRARIES "${Eigen_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${Eigen_INCLUDE_DIR}"
)
endif()
......@@ -43,7 +43,7 @@ macro(imstk_add_external_project extProj)
# Set project directory
#-----------------------------------------------------------------------------
set(${extProj}_SOURCE_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/src)
set(${extProj}_PREFIX_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/prefix)
set(${extProj}_PREFIX_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/cmake)
set(${extProj}_DIR ${CMAKE_BINARY_DIR}/External/${extProj}/build-${CMAKE_BUILD_TYPE})
#-----------------------------------------------------------------------------
......
......@@ -70,6 +70,11 @@ list(APPEND ${PROJECT_NAME}_DEPENDENCIES Uncrustify)
option(USE_SYSTEM_Uncrustify "Exclude Uncrustify from superbuild and use an existing build." OFF)
mark_as_advanced(USE_SYSTEM_Uncrustify)
# Eigen
list(APPEND ${PROJECT_NAME}_DEPENDENCIES Eigen)
option(USE_SYSTEM_Eigen "Exclude Eigen from superbuild and use an existing build." OFF)
mark_as_advanced(USE_SYSTEM_Eigen)
#-----------------------------------------------------------------------------
# SUPERBUILD
#-----------------------------------------------------------------------------
......@@ -97,11 +102,16 @@ include_directories( ${g3log_INCLUDE_DIR} )
# Uncrustify
include(SetupUncrustifyConfig)
# Eigen
find_package( Eigen 3.1.2 REQUIRED )
include_directories( ${Eigen_INCLUDE_DIR} )
#--------------------------------------------------------------------------
# Add Source code subdirectories
#--------------------------------------------------------------------------
add_subdirectory(Base/Utilities)
add_subdirectory(Base/Core)
add_subdirectory(Base/Geometry)
add_subdirectory(Base/Scene)
add_subdirectory(Base/SimulationManager)
......
......@@ -28,4 +28,5 @@ add_executable(${PROJECT_NAME} main.cpp)
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME}
SimulationManager
Geometry
)
......@@ -4,6 +4,7 @@
#include <thread>
#include "imstkSimulationManager.h"
#include "imstkPlane.h"
int main()
{
......@@ -11,53 +12,83 @@ int main()
<< "Starting Sandbox" << std::endl
<< "****************" << std::endl;
// Creating SimulationManager
std::shared_ptr<imstk::SimulationManager> sdk =
std::make_shared<imstk::SimulationManager>();
LOG(INFO) << "-- Test add scenes";
// Create scene and add it (scene1)
std::shared_ptr<imstk::Scene> scene1 =
std::make_shared<imstk::Scene>("scene1");
scene1->setLoopDelay(500);
sdk->addScene(scene1);
// Create new scene through sdk (scene2)
sdk->createNewScene("scene2");
std::shared_ptr<imstk::Scene> scene2 = sdk->getScene("scene2");
scene2->setLoopDelay(500);
// Create new scene through sdk (auto : "Scene_X")
std::shared_ptr<imstk::Scene> scene3 = sdk->createNewScene();
// Remove scene3
sdk->removeScene("Scene_3");
// Test switch
LOG(INFO) << "-- Test scene switch";
sdk->startSimulation("scene1");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene2", false);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene1", true);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Test pause/run
LOG(INFO) << "-- Test simulation pause/run";
sdk->startSimulation("scene2");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->runSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Quit
while (sdk->getStatus() != imstk::SimulationStatus::INACTIVE) {}
// Geometry
// - Plane
std::cout << "-- Plane : Init" << std::endl;
auto pos = imstk::Vec3d(5, 2, 5);
auto norm = imstk::LEFT;
norm.normalize();
auto width = 10;
std::cout << "p = " << pos << std::endl;
std::cout << "n = " << norm << std::endl;
std::cout << "w = " << width << std::endl;
std::cout << "-- Plane : Create" << std::endl;
auto plane = std::make_shared<imstk::Plane>(pos, norm, width);
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "n = " << plane->getNormal() << std::endl;
std::cout << "w = " << plane->getWidth() << std::endl;
std::cout << "-- Plane : Translate" << std::endl;
plane->translate(imstk::Vec3d(2, 1, -3));
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "-- Plane : Rotate" << std::endl;
plane->rotate(imstk::UP, 0.25);
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "n = " << plane->getNormal() << std::endl;
/*
// Creating SimulationManager
std::shared_ptr<imstk::SimulationManager> sdk =
std::make_shared<imstk::SimulationManager>();
LOG(INFO) << "-- Test add scenes";
// Create scene and add it (scene1)
std::shared_ptr<imstk::Scene> scene1 =
std::make_shared<imstk::Scene>("scene1");
scene1->setLoopDelay(500);
sdk->addScene(scene1);
// Create new scene through sdk (scene2)
sdk->createNewScene("scene2");
std::shared_ptr<imstk::Scene> scene2 = sdk->getScene("scene2");
scene2->setLoopDelay(500);
// Create new scene through sdk (auto : "Scene_X")
std::shared_ptr<imstk::Scene> scene3 = sdk->createNewScene();
// Remove scene3
sdk->removeScene("Scene_3");
// Test switch
LOG(INFO) << "-- Test scene switch";
sdk->startSimulation("scene1");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene2", false);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene1", true);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Test pause/run
LOG(INFO) << "-- Test simulation pause/run";
sdk->startSimulation("scene2");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->runSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Quit
while (sdk->getStatus() != imstk::SimulationStatus::INACTIVE) {}
*/
return 0;
}
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