iMSTK
Interactive Medical Simulation Toolkit
sdfHaptics.cs
1 using System;
2 using Imstk;
3 
4 public class SdfHaptics
5 {
6  public static void Main(string[] args)
7  {
8  Logger.startLogger();
9 
10  Scene scene = new Scene("SDFHaptics");
11  SurfaceMesh axesMesh = MeshIO.readSurfaceMesh("../data/axesPoly.vtk");
12  ImageData sdfImage = MeshIO.readImageData("../data/stanfordBunny/stanfordBunny_SDF.nii");
13  SignedDistanceField sdf = new SignedDistanceField(sdfImage.cast((byte)Utils.IMSTK_DOUBLE));
14  {
15  scene.getActiveCamera().setPosition(-2.3, 23.81, 45.65);
16  scene.getActiveCamera().setFocalPoint(9.41, 8.45, 5.76);
17 
18  CollidingObject bunnyObj = new CollidingObject("Bunny");
19  {
20  bunnyObj.setCollidingGeometry(sdf);
21 
22  SurfaceMeshFlyingEdges isoExtract = new SurfaceMeshFlyingEdges();
23  isoExtract.setInputImage(sdfImage);
24  isoExtract.update();
25 
26  isoExtract.getOutputMesh().flipNormals();
27  bunnyObj.setVisualGeometry(isoExtract.getOutputMesh());
28 
29  scene.addSceneObject(bunnyObj);
30  }
31 
32  SceneObject axesObj = new SceneObject("Axes");
33  {
34  axesObj.setVisualGeometry(axesMesh);
35  scene.addSceneObject(axesObj);
36  }
37 
38  // Light (white)
39  DirectionalLight whiteLight = new DirectionalLight();
40  {
41  whiteLight.setDirection(new Vec3d(5.0, -8.0, -5.0));
42  whiteLight.setIntensity(1.0);
43  scene.addLight("whiteLight", whiteLight);
44  }
45  }
46 
47  DeviceManager hapticManager = DeviceManagerFactory.makeDeviceManager();
48  DeviceClient client = hapticManager.makeDeviceClient();
49 
50  // Run the simulation
51  {
52  // Setup a viewer to render in its own thread
53  VTKViewer viewer = new VTKViewer("Viewer");
54  viewer.setActiveScene(scene);
55 
56  // Setup a scene manager to advance the scene in its own thread
57  SceneManager sceneManager = new SceneManager("Scene Manager");
58  sceneManager.setActiveScene(scene);
59  sceneManager.setExecutionType(Module.ExecutionType.ADAPTIVE);
60 
61  SimulationManager driver = new SimulationManager();
62  driver.addModule(viewer);
63  driver.addModule(sceneManager);
64  driver.addModule(hapticManager);
65 
66  ImplicitFunctionCentralGradient centralGrad = new ImplicitFunctionCentralGradient();
67  centralGrad.setFunction(sdf);
68  centralGrad.setDx(sdf.getImage().getSpacing());
69 
70  Utils.connectEvent(sceneManager, Utils.SceneManager_getPostUpdate_cb,
71  (Event e) =>
72  {
73  Vec3d tmpPos = client.getPosition();
74  Vec3d pos = tmpPos * 0.1 + new Vec3d(0.0, 0.1, 10.0);
75 
76  client.update();
77  axesMesh.setTranslation(pos);
78  axesMesh.setRotation(client.getOrientation());
79  axesMesh.postModified();
80 
81  double dx = sdf.getFunctionValue(pos);
82  if (dx < 0.0)
83  {
84  Vec3d g = centralGrad.compute(pos);
85  Vec3d nrm_g = new Vec3d(-g[0] * dx * 4.0, -g[1] * dx * 4.0, -g[2] * dx * 4.0);
86  client.setForce(nrm_g);
87  }
88  });
89 
90  // Add mouse and keyboard controls to the viewer
91  {
92  MouseSceneControl mouseControl = new MouseSceneControl();
93  mouseControl.setDevice(viewer.getMouseDevice());
94  mouseControl.setSceneManager(sceneManager);
95  scene.addControl(mouseControl);
96 
97  KeyboardSceneControl keyControl = new KeyboardSceneControl();
98  keyControl.setDevice(viewer.getKeyboardDevice());
99  keyControl.setSceneManager(new SceneManagerWeakPtr(sceneManager));
100  keyControl.setModuleDriver(new ModuleDriverWeakPtr(driver));
101  scene.addControl(keyControl);
102  }
103 
104  driver.start();
105  }
106  }
107 }
lazy initialized singleton