iMSTK
Interactive Medical Simulation Toolkit
ObjectControllerExample.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkCamera.h"
8 #include "imstkCapsule.h"
9 #include "imstkCollidingObject.h"
10 #include "imstkCylinder.h"
11 #include "imstkDeviceManager.h"
12 #include "imstkDeviceManagerFactory.h"
13 #include "imstkDirectionalLight.h"
14 #include "imstkKeyboardDeviceClient.h"
15 #include "imstkKeyboardSceneControl.h"
16 #include "imstkLogger.h"
17 #include "imstkMouseDeviceClient.h"
18 #include "imstkMouseSceneControl.h"
19 #include "imstkOrientedBox.h"
20 #include "imstkPlane.h"
21 #include "imstkScene.h"
22 #include "imstkSceneManager.h"
23 #include "imstkSceneObjectController.h"
24 #include "imstkSimulationManager.h"
25 #include "imstkSimulationUtils.h"
26 #include "imstkSphere.h"
27 #include "imstkVTKViewer.h"
28 
29 using namespace imstk;
30 
35 int
36 main()
37 {
38  // Setup logger (write to file and stdout)
40 
41  // Create Scene
42  auto scene = std::make_shared<Scene>("ObjectController");
43 
44  // Setup default haptics manager
45  std::shared_ptr<DeviceManager> hapticManager = DeviceManagerFactory::makeDeviceManager();
46  std::shared_ptr<DeviceClient> deviceClient = hapticManager->makeDeviceClient();
47 
48  std::shared_ptr<AnalyticalGeometry> geometries[] = {
49  std::make_shared<OrientedBox>(Vec3d::Zero(), Vec3d(0.01, 0.05, 0.01)),
50  std::make_shared<Plane>(Vec3d::Zero(), Vec3d(0.0, 0.01, 0.0)),
51  std::make_shared<Capsule>(Vec3d::Zero(), 0.005, 0.01),
52  std::make_shared<Cylinder>(Vec3d::Zero(), 0.05, 0.01),
53  std::make_shared<Sphere>(Vec3d::Zero(), 0.02)
54  };
55 
56  auto object = std::make_shared<SceneObject>("VirtualObject");
57  object->setVisualGeometry(geometries[0]);
58  scene->addSceneObject(object);
59 
60  auto controller = std::make_shared<SceneObjectController>();
61  controller->setControlledObject(object);
62  controller->setDevice(deviceClient);
63  scene->addControl(controller);
64 
65  // Update Camera position
66  std::shared_ptr<Camera> cam = scene->getActiveCamera();
67  cam->setPosition(Vec3d(0.0, 0.0, 0.3));
68  cam->setFocalPoint(geometries[0]->getPosition());
69 
70  // Light
71  auto light = std::make_shared<DirectionalLight>();
72  light->setDirection(Vec3d(5.0, -8.0, -5.0));
73  light->setIntensity(1.0);
74  scene->addLight("light", light);
75 
76  // Run the simulation
77  {
78  // Setup a viewer to render
79  auto viewer = std::make_shared<VTKViewer>();
80  viewer->setActiveScene(scene);
81 
82  // Setup a scene manager to advance the scene
83  auto sceneManager = std::make_shared<SceneManager>();
84  sceneManager->setActiveScene(scene);
85 
86  auto driver = std::make_shared<SimulationManager>();
87  driver->addModule(viewer);
88  driver->addModule(sceneManager);
89  driver->addModule(hapticManager);
90  driver->setDesiredDt(0.01);
91 
92  // Add default mouse and keyboard controls to the viewer
93  std::shared_ptr<Entity> mouseAndKeyControls =
94  SimulationUtils::createDefaultSceneControl(driver);
95  scene->addSceneObject(mouseAndKeyControls);
96 
97  driver->start();
98  }
99  return 0;
100 }
Compound Geometry.
static std::shared_ptr< DeviceManager > makeDeviceManager()
Attempts to create a new DeviceManager by whichever is default If multiple haptic managers are built ...
static LoggerG3 & startLogger()
Starts logger with default sinks, use getInstance to create a logger with no sinks.