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

ENH: Add Camera API

parent 4d477065
......@@ -22,9 +22,10 @@
#include "imstkViewer.h"
#include "vtkRenderer.h"
#include "vtkCamera.h"
#include "vtkLight.h"
#include "vtkLightActor.h"
#include "vtkCamera.h"
#include "vtkCameraActor.h"
#include "vtkAxesActor.h"
#include "vtkInteractorStyleTrackballCamera.h"
......@@ -45,6 +46,7 @@ Viewer::initRenderer()
// Create and add renderer
auto renderer = vtkSmartPointer<vtkRenderer>::New();
//renderer->UseShadowsOn();
m_renderWindow->AddRenderer(renderer);
// Create and add actors
......@@ -62,19 +64,20 @@ Viewer::initRenderer()
if( light->isPositional() )
{
auto lightActor = vtkSmartPointer<vtkLightActor>::New();
lightActor->SetLight(light->getVtkLight());
renderer->AddViewProp(lightActor);
lightActor->SetLight( light->getVtkLight() );
renderer->AddActor( lightActor );
}
}
/// WIP : make the following a renderer setting and optional
// Camera
auto camera = renderer->MakeCamera();
camera->SetPosition(8, 8, 8);
camera->SetFocalPoint(0, 0, 0);
renderer->SetActiveCamera(camera);
// Add camera
//auto camActor = vtkSmartPointer<vtkCameraActor>::New();
//camActor->SetCamera( m_currentScene->getCamera()->getVtkCamera() );
//renderer->AddActor( camActor );
//renderer->GetActiveCamera()->SetFocalPoint(0,0,0);
renderer->SetActiveCamera( m_currentScene->getCamera()->getVtkCamera() );
renderer->ResetCameraClippingRange();
// Global Axis
auto axes = vtkSmartPointer<vtkAxesActor>::New();
renderer->AddActor(axes);
......@@ -83,8 +86,6 @@ Viewer::initRenderer()
renderer->SetBackground(0.66,0.66,0.66);
renderer->SetBackground2(157.0/255.0*0.66,186/255.0*0.66,192.0/255.0*0.66);
renderer->GradientBackgroundOn();
//renderer->UseShadowsOn();
}
void
......
......@@ -6,10 +6,12 @@ imstk_add_library( Scene
H_FILES
imstkScene.h
imstkLight.h
imstkCamera.h
imstkSceneObject.h
CPP_FILES
imstkScene.cpp
imstkLight.cpp
imstkCamera.cpp
imstkSceneObject.cpp
LIBRARIES
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 "imstkCamera.h"
#include <g3log/g3log.hpp>
namespace imstk {
const Vec3d
Camera::getPosition() const
{
double p[3];
m_vtkCamera->GetPosition(p);
return Vec3d(p[0], p[1], p[2]);
}
void
Camera::setPosition(const Vec3d& p)
{
this->setPosition(p[0], p[1], p[2]);
}
void
Camera::setPosition(const double& x,
const double& y,
const double& z)
{
m_vtkCamera->SetPosition(x, y, z);
}
const Vec3d
Camera::getFocalPoint() const
{
double p[3];
m_vtkCamera->GetFocalPoint(p);
return Vec3d(p[0], p[1], p[2]);
}
void
Camera::setFocalPoint(const Vec3d& p)
{
this->setFocalPoint(p[0], p[1], p[2]);
}
void
Camera::setFocalPoint(const double& x,
const double& y,
const double& z)
{
m_vtkCamera->SetFocalPoint(x, y, z);
}
const Vec3d
Camera::getViewUp() const
{
double v[3];
m_vtkCamera->GetViewUp(v);
return Vec3d(v[0], v[1], v[2]);
}
void
Camera::setViewUp(const Vec3d& v)
{
this->setViewUp(v[0], v[1], v[2]);
}
void
Camera::setViewUp(const double& x,
const double& y,
const double& z)
{
m_vtkCamera->SetViewUp(x, y, z);
}
const double
Camera::getViewAngle() const
{
return m_vtkCamera->GetViewAngle();
}
void
Camera::setViewAngle(const double& angle)
{
m_vtkCamera->SetViewAngle(angle);
}
vtkSmartPointer<vtkCamera>
Camera::getVtkCamera() const
{
return m_vtkCamera;
}
}
/*=========================================================================
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 imstkCamera_h
#define imstkCamera_h
#include <string>
#include <vtkSmartPointer.h>
#include <vtkCamera.h>
#include "imstkMath.h"
namespace imstk {
class Camera
{
public:
Camera() = default;
~Camera() = default;
const Vec3d getPosition() const;
void setPosition(const Vec3d& p);
void setPosition(const double& x,
const double& y,
const double& z);
const Vec3d getFocalPoint() const;
void setFocalPoint(const Vec3d& p);
void setFocalPoint(const double& x,
const double& y,
const double& z);
const Vec3d getViewUp() const;
void setViewUp(const Vec3d& v);
void setViewUp(const double& x,
const double& y,
const double& z);
const double getViewAngle() const;
void setViewAngle(const double& angle);
vtkSmartPointer<vtkCamera> getVtkCamera() const;
protected:
vtkSmartPointer<vtkCamera> m_vtkCamera = vtkSmartPointer<vtkCamera>::New();
};
}
#endif // ifndef imstkCamera_h
......@@ -41,9 +41,7 @@ class Light
{
public:
Light(std::string name) :
m_name(name)
{}
Light(std::string name) : m_name(name) {}
~Light() = default;
......
......@@ -30,10 +30,10 @@ Scene::isObjectRegistered(std::string sceneObjectName) const
return m_sceneObjectsMap.find(sceneObjectName) != m_sceneObjectsMap.end();
}
const std::vector<std::shared_ptr<SceneObject> >
const std::vector<std::shared_ptr<SceneObject>>
Scene::getSceneObjects() const
{
std::vector<std::shared_ptr<SceneObject> > v;
std::vector<std::shared_ptr<SceneObject>> v;
for (auto it = m_sceneObjectsMap.begin();
it != m_sceneObjectsMap.end();
......@@ -114,7 +114,7 @@ Scene::getLight(std::string lightName) const
{
if (!this->isLightRegistered(lightName))
{
LOG(WARNING) << "No scene light named '" << lightName
LOG(WARNING) << "No light named '" << lightName
<< "' was registered in this scene.";
return nullptr;
}
......@@ -152,6 +152,12 @@ Scene::removeLight(std::string lightName)
LOG(INFO) << lightName << " light removed from " << m_name;
}
std::shared_ptr<Camera>
Scene::getCamera()
{
return m_camera;
}
void
Scene::initModule()
{
......@@ -168,5 +174,6 @@ void
Scene::runModule()
{
LOG(DEBUG) << m_name << " : running";
LOG(INFO) << m_camera->getPosition();
}
}
......@@ -29,10 +29,14 @@
#include "imstkModule.h"
#include "imstkSceneObject.h"
#include "imstkLight.h"
#include "imstkCamera.h"
namespace imstk {
class Scene : public Module
{
template<class T>
using NamedMap = std::unordered_map<std::string, std::shared_ptr<T>>;
public:
Scene(std::string name) : Module(name) {}
......@@ -40,25 +44,28 @@ public:
~Scene() = default;
bool isObjectRegistered(std::string sceneObjectName) const;
const std::vector<std::shared_ptr<SceneObject>>getSceneObjects() const;
const std::vector<std::shared_ptr<SceneObject>> getSceneObjects() const;
std::shared_ptr<SceneObject> getSceneObject(std::string sceneObjectName) const;
void addSceneObject(std::shared_ptr<SceneObject> newSceneObject);
void removeSceneObject(std::string sceneObjectName);
bool isLightRegistered(std::string lightName) const;
const std::vector<std::shared_ptr<Light>>getLights() const;
const std::vector<std::shared_ptr<Light>> getLights() const;
std::shared_ptr<Light> getLight(std::string lightName) const;
void addLight(std::shared_ptr<Light> newLight);
void removeLight(std::string lightName);
std::shared_ptr<Camera> getCamera();
protected:
void initModule() override;
void runModule() override;
void cleanUpModule() override;
std::unordered_map<std::string, std::shared_ptr<SceneObject> > m_sceneObjectsMap;
std::unordered_map<std::string, std::shared_ptr<Light> > m_lightsMap;
NamedMap<SceneObject> m_sceneObjectsMap;
NamedMap<Light> m_lightsMap;
std::shared_ptr<Camera> m_camera = std::make_shared<Camera>();
};
}
......
......@@ -40,9 +40,7 @@ class SceneObject
{
public:
SceneObject(std::string name) :
m_name(name)
{}
SceneObject(std::string name) : m_name(name) {}
~SceneObject() = default;
......
......@@ -4,13 +4,14 @@
#include <thread>
#include <iomanip>
#include "imstkMath.h"
#include "imstkSimulationManager.h"
#include "imstkSceneObject.h"
#include "imstkPlane.h"
#include "imstkSphere.h"
#include "imstkCube.h"
#include "imstkLight.h"
#include "imstkMath.h"
#include "imstkSimulationManager.h"
#include "imstkCamera.h"
// Maps
#include "imstkIsometricMap.h"
......@@ -47,18 +48,18 @@ int main()
auto sphereGeom = std::make_shared<imstk::Sphere>();
sphereGeom->scale(0.3);
sphereGeom->translate(1, 2, 0.5);
sphereGeom->translate(0, 2, 0);
auto sphereObj = std::make_shared<imstk::VisualObject>("VisualSphere");
sphereObj->setVisualGeometry(sphereGeom);
auto whiteLight = std::make_shared<imstk::Light>("whiteLight");
whiteLight->setPosition(imstk::Vec3d(5,8,5));
whiteLight->setPosition(imstk::Vec3d(5, 8, 5));
whiteLight->setPositional();
auto colorLight = std::make_shared<imstk::Light>("colorLight");
colorLight->setPosition(imstk::Vec3d(4,-3,1));
colorLight->setFocalPoint(imstk::Vec3d(0,0,0));
colorLight->setColor(imstk::Color::Green);
colorLight->setPosition(imstk::Vec3d(4, -3, 1));
colorLight->setFocalPoint(imstk::Vec3d(0, 0, 0));
colorLight->setColor(imstk::Color::Red);
colorLight->setPositional();
colorLight->setSpotAngle(15);
......@@ -70,6 +71,10 @@ int main()
sceneTest->addLight(whiteLight);
sceneTest->addLight(colorLight);
auto cam1 = sceneTest->getCamera();
cam1->setPosition(imstk::Vec3d(-5.5, 2.5, 32));
cam1->setFocalPoint(imstk::Vec3d(1, 1, 0));
sdk->startSimulation("SceneTest");
......
Supports Markdown
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