Commit e6df0f61 authored by Stephen Thompson's avatar Stephen Thompson

Issue #1 trying to get model to world working for both locators and actors, nearly there

parent 3ec5d4be
Pipeline #3770 passed with stages
in 6 minutes and 24 seconds
566.222 0 311.281
0 565.696 245.08
0 0 1
0.137847 -0.058787 0.00508153 -0.00782094
......@@ -2,7 +2,8 @@
"tracker config" :
{
"tracker type" : "aruco",
"debug" : true
"debug" : true,
"calibration" : "configuration/calibration.txt"
},
"camera" : {
......@@ -11,6 +12,9 @@
"bounding box" : [0.0, 640.0, 0.0, 480.0, -100.0, 0.0]
},
"models" : "data/"
"models" : "data/",
"model to world" : "configuration/model_to_world.txt",
"logo" : false,
"search radius" : 10000
}
1. 0. 0. -616.
0. 1. 0. -202.
0. 0. 1. 1558.
0. 0. 0. 1.
# coding=utf-8
""" Algorithms for the surgery evaluation application """
import vtk
from numpy import inf, eye
from numpy import inf, eye, loadtxt, float32
from sksurgerynditracker.nditracker import NDITracker
from sksurgeryarucotracker.arucotracker import ArUcoTracker
from sksurgeryvtk.models. vtk_surface_model_directory_loader \
......@@ -76,13 +76,13 @@ def configure_tracker(config):
return tracker
def populate_models(path_name, model_to_world=eye(4, 4)):
def populate_models(path_name, model_to_world):
"""
Loads vtk models from a directory and returns
a list of vtk actors and associated vtkPointLocators
:param: pathname: directory where models are
:param: model_to_world: optional
:param: model_to_world: 4x4 matrix, of dtype float32
:return: locators
:return: actors
......@@ -94,12 +94,51 @@ def populate_models(path_name, model_to_world=eye(4, 4)):
locators = []
transform = vtk.vtkTransform()
transform.SetMatrix(np2vtk(model_to_world))
transformer = vtk.vtkTransformPolyDataFilter()
transformer.SetTransform(transform)
for model in models:
model.set_model_transform(np2vtk(model_to_world))
model.transform_filter.Update()
print(model.source.GetCenter())
transformer.SetInputData(model.source)
target = vtk.vtkPolyData()
transformer.SetOutput(target)
transformer.Update()
model.source = target
transformer.SetInputConnection(model.normals.GetOutputPort())
model.mapper = vtk.vtkPolyDataMapper()
model.mapper.SetInputConnection(transformer.GetOutputPort())
model.mapper.Update()
model.actor.SetMapper(model.mapper)
print("after trans", model.source.GetCenter())
point_locator = vtk.vtkPointLocator()
point_locator.SetDataSet(model.source)
point_locator.Update()
locators.append(point_locator)
return models, locators
def set_model_to_world(config):
"""
Creates a 4x4 model to world matrix
:param: the configuration, if model to world is defined it will load, if
not will set a identity model to world
:return the model to world matrix
:raises: ValueError if file does not contain a 4x4 matrix
"""
model_to_world = eye(4, dtype=float32)
if "model to world" in config:
model_to_world = loadtxt(config.get("model to world"), dtype=float32)
if (model_to_world.shape == (4, 4) and model_to_world.dtype == float32):
return model_to_world
raise ValueError(('model to world should be a 4x4 matrix of type float32'),
model_to_world.shape, model_to_world.dtype)
......@@ -5,14 +5,13 @@ from math import isnan
from sksurgeryutils.common_overlay_apps import OverlayBaseApp
from sksurgeryvtk.text.text_overlay import VTKCornerAnnotation
from sksurgeryeval.algorithms.algorithms import (
configure_tracker, populate_models, np2vtk, point_in_locator)
configure_tracker, populate_models, np2vtk, point_in_locator,
set_model_to_world)
from sksurgeryeval.algorithms.background_image import \
OverlayBackground
from sksurgeryeval.shapes.cone import VTKConeModel
class OverlayApp(OverlayBaseApp):
"""Inherits from OverlayBaseApp, adding code to test the
proximity of a tracked object to a set of vtk objects"""
......@@ -38,7 +37,12 @@ class OverlayApp(OverlayBaseApp):
if "tracker config" in config:
self._tracker = configure_tracker(config.get("tracker config"))
models, self._locators = populate_models(config.get("models"))
model_to_world = set_model_to_world(config)
models, self._locators = populate_models(config.get("models"), model_to_world)
self._pointer = VTKConeModel(5.0, 2.5, (1.0, 1.0, 1.0), "pointer")
self.vtk_overlay_window.add_vtk_actor(self._pointer.actor)
self.vtk_overlay_window.add_vtk_models(models)
if "camera" in config:
camera_config = config.get("camera")
......@@ -51,10 +55,9 @@ class OverlayApp(OverlayBaseApp):
self._tracker_handle = 0
self._search_radius = 10.0
self._pointer = VTKConeModel(5.0, 2.5, (1.0, 1.0, 1.0), "pointer")
self.vtk_overlay_window.add_vtk_actor(self._pointer.actor)
self.vtk_overlay_window.add_vtk_models(models)
if "search radius" in config:
self._search_radius = config.get("search radius")
self._text = VTKCornerAnnotation()
self._text.set_text(["Hello World","","",""])
self.vtk_overlay_window.add_vtk_actor(self._text.text_actor)
......@@ -86,6 +89,7 @@ class OverlayApp(OverlayBaseApp):
if not isnan(quality[ph_index]):
self._pointer.actor.SetUserMatrix(np2vtk(tracking[ph_index]))
index, distance = point_in_locator(tracking[ph_index][0:3,3], self._locators, self._search_radius)
self._text.set_text([str(index),str(distance),str(tracking[ph_index][0:3,3]),""])
# self._text.set_text([str(index),str(distance),str(tracking[ph_index][0:3,3]),""])
self._text.set_text([str(index),str(distance),str(tracking[ph_index]),""])
......@@ -24,7 +24,7 @@ def test_populate_models():
Tests that populate models function reads data from
a directory and returns a list of polydata and pointlocators
"""
models, locators = populate_models("data")
models, locators = populate_models("data", model_to_world=numpy.eye(4))
point_in, _ = point_in_locator((570.0, 270.0, -1386.0),
locators, 3.0)
assert len(models) == 1
......
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