728x90
1. 개요
Target이 되는 Actor 을 지정하고 이를 Interaction 에 따라 변경되는 위치를 추적하여 따라다니는 Tracking 2D Actor를 구현한다.
2. Tracking Image Actor2D
vtkImageMapper 에 할당된 이미지를 vtkActor2D 에 입력하고 Tracking을 위한 Interaction event 를 입력한다.
public void TestTrackingImageActor2D()
{
EnableDrawAxes = true;
AxesLength = 15;
var filePath = Path.Combine(DirPath, "reconnect.png");
var imgData = VtkImageUtils.ReadImageData(filePath);
vtkRenderer overlayRenderer = vtkRenderer.New();
renderWindow.AddRenderer(overlayRenderer);
renderWindow.SetNumberOfLayers(2);
renderer.SetLayer(0);
overlayRenderer.SetLayer(1);
overlayRenderer.InteractiveOff();
renderer.InteractiveOn();
vtkImageMapper imgMapper = vtkImageMapper.New();
imgMapper.SetInputData(imgData);
imgMapper.SetColorLevel(127.5);
imgMapper.SetColorWindow(255);
vtkActor2D trackingActor = vtkActor2D.New();
trackingActor.SetMapper(imgMapper);
trackingActor.SetPosition(10, 10);
// Tracking actor 를 overlay Renderer 에 표시한다.
overlayRenderer.AddActor2D(trackingActor);
// Target 이되는 actor이다.
var cube = VtkGeometryUtils.MakeCubeSource();
var cubeMapper = vtkPolyDataMapper.New();
cubeMapper.SetInputData(cube.GetOutput());
var targetActor = vtkActor.New();
targetActor.SetMapper(cubeMapper);
targetActor.GetProperty().SetColor(VtkColorUtils.GetNamedColor3dPtr("SkyBlue"));
renderer.AddActor(targetActor);
void OnInteractorOnInteractionEvt(vtkObject sender, vtkObjectEventArgs args)
{
var targetPos = targetActor.GetPosition();
worldCoordinate.SetValue(targetPos.ToIntPtr());
var scrPos = worldCoordinate.GetComputedDisplayValue(renderer);
// Tracking Actor가 표시될 delta 위치 값을 설정한다.
var scrPosTrl = scrPos.Add(new[] { 10, 10 });
trackingActor.SetPosition(scrPosTrl[0], scrPosTrl[1]);
}
// 이벤트 등록 및 제거
interactor.InteractionEvt += OnInteractorOnInteractionEvt;
interactor.StartInteractionEvt += OnInteractorOnInteractionEvt;
interactor.EndInteractionEvt += OnInteractorOnInteractionEvt;
interactor.MouseWheelForwardEvt += OnInteractorOnInteractionEvt;
interactor.MouseWheelBackwardEvt += OnInteractorOnInteractionEvt;
closingEvt += (sender, args) =>
{
interactor.InteractionEvt -= OnInteractorOnInteractionEvt;
interactor.StartInteractionEvt -= OnInteractorOnInteractionEvt;
interactor.EndInteractionEvt -= OnInteractorOnInteractionEvt;
interactor.MouseWheelForwardEvt -= OnInteractorOnInteractionEvt;
interactor.MouseWheelBackwardEvt -= OnInteractorOnInteractionEvt;
};
}
3. Tracking Text Actor2D
위의 구현에서 TextActor로 변경하고 Interaction 에 따라 TextMapper의 SetInput을 업데이트하는 것 이외에는 모두 동일한다.
public void TestTrackingTextActor2D()
{
EnableDrawAxes = true;
AxesLength = 15;
var filePath = Path.Combine(DirPath, "reconnect.png");
var imgData = VtkImageUtils.ReadImageData(filePath);
vtkRenderer overlayRenderer = vtkRenderer.New();
renderWindow.AddRenderer(overlayRenderer);
renderWindow.SetNumberOfLayers(2);
renderer.SetLayer(0);
overlayRenderer.SetLayer(1);
overlayRenderer.InteractiveOff();
renderer.InteractiveOn();
vtkTextProperty sliceTextProp = vtkTextProperty.New();
sliceTextProp.SetColor(VtkColorUtils.GetNamedColor3d("Blue").ToIntPtr());
sliceTextProp.SetFontFamilyToCourier();
sliceTextProp.SetFontSize(20);
sliceTextProp.SetVerticalJustificationToBottom();
sliceTextProp.SetJustificationToLeft();
vtkTextMapper textMapper = vtkTextMapper.New();
textMapper.SetInput("Readty to move");
textMapper.SetTextProperty(sliceTextProp);
vtkActor2D trackingActor = vtkActor2D.New();
trackingActor.SetMapper(textMapper);
trackingActor.SetPosition(15, 10);
// Tracking actor 를 overlay Renderer 에 표시한다.
overlayRenderer.AddActor2D(trackingActor);
// Target 이되는 actor이다.
var cube = VtkGeometryUtils.MakeCubeSource();
var cubeMapper = vtkPolyDataMapper.New();
cubeMapper.SetInputData(cube.GetOutput());
var targetActor = vtkActor.New();
targetActor.SetMapper(cubeMapper);
targetActor.GetProperty().SetColor(VtkColorUtils.GetNamedColor3dPtr("SkyBlue"));
renderer.AddActor(targetActor);
void OnInteractorOnInteractionEvt(vtkObject sender, vtkObjectEventArgs args)
{
var targetPos = targetActor.GetPosition();
worldCoordinate.SetValue(targetPos.ToIntPtr());
var scrPos = worldCoordinate.GetComputedDisplayValue(renderer);
textMapper.SetInput($"Tracking : {scrPos.ToStringJoin()}");
// Tracking Actor가 표시될 delta 위치 값을 설정한다.
var scrPosTrl = scrPos.Add(new[] { 10, 10 });
trackingActor.SetPosition(scrPosTrl[0], scrPosTrl[1]);
}
// 이벤트 등록 및 제거
interactor.InteractionEvt += OnInteractorOnInteractionEvt;
interactor.StartInteractionEvt += OnInteractorOnInteractionEvt;
interactor.EndInteractionEvt += OnInteractorOnInteractionEvt;
interactor.MouseWheelForwardEvt += OnInteractorOnInteractionEvt;
interactor.MouseWheelBackwardEvt += OnInteractorOnInteractionEvt;
closingEvt += (sender, args) =>
{
interactor.InteractionEvt -= OnInteractorOnInteractionEvt;
interactor.StartInteractionEvt -= OnInteractorOnInteractionEvt;
interactor.EndInteractionEvt -= OnInteractorOnInteractionEvt;
interactor.MouseWheelForwardEvt -= OnInteractorOnInteractionEvt;
interactor.MouseWheelBackwardEvt -= OnInteractorOnInteractionEvt;
};
}
728x90
728x90