Skip to content
Snippets Groups Projects
Commit 17f7a10d authored by Andrew Maclean's avatar Andrew Maclean
Browse files

Adding LineWidget2

parent 6811b6fd
No related branches found
No related tags found
1 merge request!397C++ to python api 12
......@@ -29,15 +29,18 @@ public:
vtkLineWidget2* lineWidget = reinterpret_cast<vtkLineWidget2*>(caller);
// Get the actual box coordinates of the line
vtkLineRepresentation* rep =
static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation());
vtkNew<vtkPolyData> polydata;
static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
->GetPolyData(polydata);
rep->GetPolyData(polydata);
// Display one of the points, just so we know it's working
// Get the actual box coordinates of the line.
double p[3];
polydata->GetPoint(0, p);
std::cout << "P: " << p[0] << " " << p[1] << " " << p[2] << std::endl;
std::cout << "P1: " << p[0] << " " << p[1] << " " << p[2] << std::endl;
polydata->GetPoint(1, p);
std::cout << "P2: " << p[0] << " " << p[1] << " " << p[2] << std::endl;
std::cout << "Distance: " << rep->GetDistance() << std::endl;
}
vtkLineCallback()
{
......@@ -50,35 +53,38 @@ int main(int, char*[])
vtkNew<vtkNamedColors> colors;
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
// Create a mapper and actor
// Create a mapper and actor.
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
// A renderer and render window
// A renderer, render window and interactor.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("LineWidget2");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
// An interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<vtkLineWidget2> lineWidget;
lineWidget->SetInteractor(renderWindowInteractor);
lineWidget->CreateDefaultRepresentation();
// You could do this if you want to set properties at this point:
// vtkNew<vtkLineRepresentation> lineRepresentation;
// lineWidget->SetRepresentation(lineRepresentation);
vtkNew<vtkLineRepresentation> lineRepresentation;
lineRepresentation->GetLineProperty()->SetColor(
colors->GetColor3d("Yellow").GetData());
lineRepresentation->GetEndPointProperty()->SetColor(
colors->GetColor3d("Magenta").GetData());
lineRepresentation->GetEndPoint2Property()->SetColor(
colors->GetColor3d("Magenta").GetData());
lineWidget->SetRepresentation(lineRepresentation);
vtkNew<vtkLineCallback> lineCallback;
......@@ -91,7 +97,7 @@ int main(int, char*[])
renderWindow->Render();
lineWidget->On();
// Begin mouse interaction
// Begin the mouse interaction.
renderWindowInteractor->Start();
return EXIT_SUCCESS;
......
......@@ -707,6 +707,7 @@ See [this tutorial](http://www.vtk.org/Wiki/VTK/Tutorials/3DDataTypes) for a bri
[ImplicitAnnulusWidget](/PythonicAPI/Widgets/ImplicitAnnulusWidget) | Clip polydata with an implicit annulus.
[ImplicitConeWidget](/PythonicAPI/Widgets/ImplicitConeWidget) | An interactive implicit cone widget.
[ImplicitPlaneWidget2](/PythonicAPI/Widgets/ImplicitPlaneWidget2) | Clip polydata with an implicit plane.
[LineWidget2](/PythonicAPI/Widgets/LineWidget2) |
[PolygonalSurfacePointPlacer](/PythonicAPI/PolyData/PolygonalSurfacePointPlacer) | Used in conjunction with vtkContourWidget to draw curves on a surface.
[RectilinearWipeWidget](/PythonicAPI/Widgets/RectilinearWipeWidget) | Compare two images.
[Slider2D](/PythonicAPI/Widgets/Slider2D) | 2D Slider.
......
#!/usr/bin/env python3
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingUI
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkInteractionWidgets import (
vtkLineRepresentation,
vtkLineWidget2
)
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
sphere_source = vtkSphereSource()
# Create a mapper and actor.
mapper = vtkPolyDataMapper()
sphere_source >> mapper
actor = vtkActor(mapper=mapper)
actor.property.color = colors.GetColor3d('MistyRose')
# A renderer, render window and interactor.
renderer = vtkRenderer(background=colors.GetColor3d('MidnightBlue'))
render_window = vtkRenderWindow(window_name='LineWidget2')
render_window.AddRenderer(renderer)
render_window_interactor = vtkRenderWindowInteractor()
render_window_interactor.render_window = render_window
renderer.AddActor(actor)
# The line widget.
line_widget = vtkLineWidget2(interactor=render_window_interactor)
line_widget.CreateDefaultRepresentation()
# You could do this if you want to set properties at this point:
line_representation = vtkLineRepresentation()
line_representation.line_property.SetColor(colors.GetColor3d('Yellow'))
line_representation.end_point_property.SetColor(colors.GetColor3d('Magenta'))
line_representation.end_point2_property.SetColor(colors.GetColor3d('Magenta'))
line_widget.SetRepresentation(line_representation)
line_callback = LineCallback()
line_widget.AddObserver('InteractionEvent', line_callback)
# Render
render_window.Render()
render_window_interactor.Initialize()
render_window.Render()
line_widget.On()
# Begin the mouse interaction.
render_window_interactor.Start()
class LineCallback:
def __call__(self, caller, ev):
# This is a vtkLineWidget2 line representation.
rep = caller.representation
polydata = vtkPolyData()
rep.GetPolyData(polydata)
pt = [0] * 3
# Get the actual box coordinates of the line.
for i in range(0, 2):
polydata.GetPoint(i, pt)
print(f'Point {i}: {fmt_floats(pt)}')
print(f'Distance: {rep.distance:0.6g}')
def fmt_floats(v, w=0, d=6, pt='g'):
"""
Pretty print a list or tuple of floats.
:param v: The list or tuple of floats.
:param w: Total width of the field.
:param d: The number of decimal places.
:param pt: The presentation type, 'f', 'g' or 'e'.
:return: A string.
"""
pt = pt.lower()
if pt not in ['f', 'g', 'e']:
pt = 'f'
return ', '.join([f'{element:{w}.{d}{pt}}' for element in v])
if __name__ == '__main__':
main()
src/Testing/Baseline/Cxx/Widgets/TestLineWidget2.png

130 B | W: | H:

src/Testing/Baseline/Cxx/Widgets/TestLineWidget2.png

130 B | W: | H:

src/Testing/Baseline/Cxx/Widgets/TestLineWidget2.png
src/Testing/Baseline/Cxx/Widgets/TestLineWidget2.png
src/Testing/Baseline/Cxx/Widgets/TestLineWidget2.png
src/Testing/Baseline/Cxx/Widgets/TestLineWidget2.png
  • 2-up
  • Swipe
  • Onion skin
src/Testing/Baseline/PythonicAPI/Widgets/TestLineWidget2.png

130 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment