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

Made a python version of ParametricKuenDemo.

parent 98cc3c09
No related branches found
No related tags found
No related merge requests found
#include <vtkSmartPointer.h>
#include <vtkParametricKuen.h>
#include <vtkParametricFunctionSource.h>
#include <vtkCamera.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkParametricFunctionSource.h>
#include <vtkParametricKuen.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkMath.h>
#include <vtkSliderWidget.h>
#include <vtkSliderRepresentation2D.h>
#include <vtkSliderWidget.h>
#include <vtkSmartPointer.h>
// These callbacks do the actual work.
// Callbacks for the interactions
......@@ -112,6 +110,22 @@ public:
int main(int, char *[])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// Set the background color.
auto SetColor = [&colors](std::array<double, 3>& v,
std::string const& colorName) {
auto const scaleFactor = 256.0;
std::transform(std::begin(v), std::end(v), std::begin(v),
[=](double const& n) { return n / scaleFactor; });
colors->SetColor(colorName, v.data());
return;
};
std::array<double, 3> bkg{{51, 77, 102}};
// std::array<double, 3> bkg{{26, 51, 77}};
SetColor(bkg, "BkgColor");
vtkSmartPointer<vtkParametricKuen> surface =
vtkSmartPointer<vtkParametricKuen>::New();
vtkSmartPointer<vtkParametricFunctionSource> source =
......@@ -126,7 +140,7 @@ int main(int, char *[])
vtkSmartPointer<vtkProperty> backProperty =
vtkSmartPointer<vtkProperty>::New();
backProperty->SetColor(1.0000, 0.3882, 0.2784);
backProperty->SetColor(colors->GetColor3d("Tomato").GetData());
// Create a parametric function source, renderer, mapper, and actor
source->SetParametricFunction(surface);
......@@ -135,16 +149,17 @@ int main(int, char *[])
actor->SetMapper(mapper);
actor->SetBackfaceProperty(backProperty);
actor->GetProperty()->SetDiffuseColor(0.8900, 0.8100, 0.3400);
actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
actor->GetProperty()->SetSpecular(.5);
actor->GetProperty()->SetSpecularPower(20);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetWindowName("Parametric Kuen Demonstration");
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640,480);
renderer->AddActor(actor);
renderer->SetBackground(.2, .3, .4);
renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(-30);
......
......@@ -145,6 +145,7 @@ It would be appreciated if there are any Python VTK experts who could convert an
| Example Name | Classes Demonstrated | Description | Image |
| -------------- | ---------------------- | ------------- | ------- |
[ParametricKuenDemo](/Python/GeometricObjects/ParametricKuenDemo) | vtkParametricKuen | Interactively change the parameters for a Kuen Surface.
[ParametricObjects](/Python/GeometricObjects/ParametricObjects) | | Uncomment the object that you wish to be displayed.
[ParametricObjectsDemo](/Python/GeometricObjects/ParametricObjectsDemo) | vtkParametricBoy vtkParametricConicSpiral vtkParametricCrossCap vtkParametricDini vtkParametricEllipsoid vtkParametricEnneper vtkParametricFigure8Klein vtkParametricKlein vtkParametricMobius vtkParametricRandomHills vtkParametricRoman vtkParametricSpline vtkParametricSuperEllipsoid vtkParametricSuperToroid vtkParametricTorus | Demonstration of of the vtkParametric classes added by Andrew Maclean. All the objects are displayed in a 4X4 array.
[ParametricObjectsDemo2](/Python/GeometricObjects/ParametricObjectsDemo2) | vtkParametricBohemianDome vtkParametricBour vtkParametricCatalanMinimal vtkParametricHenneberg vtkParametricKuen vtkParametricPluckerConoid vtkParametricPseudosphere | Demonstration of additional vtkParametric* classes added by Tim Meehan. All the objects are displayed in a 4X2 array.
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import vtk
def main():
colors = vtk.vtkNamedColors()
# Set the background color. Match those in VTKTextbook.pdf.
bkg = map(lambda x: x / 256.0, [51, 77, 102])
# bkg = map(lambda x: x / 256.0, [26, 51, 77])
colors.SetColor("BkgColor", *bkg)
surface = vtk.vtkParametricKuen()
source = vtk.vtkParametricFunctionSource()
renderer = vtk.vtkRenderer()
mapper = vtk.vtkPolyDataMapper()
actor = vtk.vtkActor()
backProperty = vtk.vtkProperty()
backProperty.SetColor(colors.GetColor3d("Tomato"))
# Create a parametric function source, renderer, mapper, and actor
source.SetParametricFunction(surface)
mapper.SetInputConnection(source.GetOutputPort())
actor.SetMapper(mapper)
actor.SetBackfaceProperty(backProperty)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d("Banana"))
actor.GetProperty().SetSpecular(.5)
actor.GetProperty().SetSpecularPower(20)
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetWindowName("Parametric Kuen Demonstration")
renderWindow.AddRenderer(renderer)
renderWindow.SetSize(640, 480)
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d("BkgColor"))
renderer.ResetCamera()
renderer.GetActiveCamera().Azimuth(30)
renderer.GetActiveCamera().Elevation(-30)
renderer.GetActiveCamera().Zoom(0.9)
renderer.ResetCameraClippingRange()
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderWindow)
# Setup a slider widget for each varying parameter
tubeWidth = 0.004
sliderLength = 0.004
titleHeight = 0.02
labelHeight = 0.02
sliderRepMinimumU = vtk.vtkSliderRepresentation2D()
sliderRepMinimumU.SetMinimumValue(-4.5)
sliderRepMinimumU.SetMaximumValue(4.5)
sliderRepMinimumU.SetValue(-4.5)
sliderRepMinimumU.SetTitleText("U min")
sliderRepMinimumU.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMinimumU.GetPoint1Coordinate().SetValue(.1, .1)
sliderRepMinimumU.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMinimumU.GetPoint2Coordinate().SetValue(.9, .1)
sliderRepMinimumU.SetTubeWidth(tubeWidth)
sliderRepMinimumU.SetSliderLength(sliderLength)
sliderRepMinimumU.SetTitleHeight(titleHeight)
sliderRepMinimumU.SetLabelHeight(labelHeight)
sliderWidgetMinimumU = vtk.vtkSliderWidget()
sliderWidgetMinimumU.SetInteractor(interactor)
sliderWidgetMinimumU.SetRepresentation(sliderRepMinimumU)
sliderWidgetMinimumU.SetAnimationModeToAnimate()
sliderWidgetMinimumU.EnabledOn()
sliderWidgetMinimumU.AddObserver('InteractionEvent', SliderCallbackMinimumU(surface))
sliderRepMaximumU = vtk.vtkSliderRepresentation2D()
sliderRepMaximumU.SetMinimumValue(-4.5)
sliderRepMaximumU.SetMaximumValue(4.5)
sliderRepMaximumU.SetValue(4.5)
sliderRepMaximumU.SetTitleText("U max")
sliderRepMaximumU.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMaximumU.GetPoint1Coordinate().SetValue(.1, .9)
sliderRepMaximumU.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMaximumU.GetPoint2Coordinate().SetValue(.9, .9)
sliderRepMaximumU.SetTubeWidth(tubeWidth)
sliderRepMaximumU.SetSliderLength(sliderLength)
sliderRepMaximumU.SetTitleHeight(titleHeight)
sliderRepMaximumU.SetLabelHeight(labelHeight)
sliderWidgetMaximumU = vtk.vtkSliderWidget()
sliderWidgetMaximumU.SetInteractor(interactor)
sliderWidgetMaximumU.SetRepresentation(sliderRepMaximumU)
sliderWidgetMaximumU.SetAnimationModeToAnimate()
sliderWidgetMaximumU.EnabledOn()
sliderWidgetMaximumU.AddObserver('InteractionEvent', SliderCallbackMaximumU(surface))
sliderRepMinimumV = vtk.vtkSliderRepresentation2D()
sliderRepMinimumV.SetMinimumValue(0.05)
sliderRepMinimumV.SetMaximumValue(vtk.vtkMath.Pi())
sliderRepMinimumV.SetValue(0.0)
sliderRepMinimumV.SetTitleText("V min")
sliderRepMinimumV.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMinimumV.GetPoint1Coordinate().SetValue(.1, .1)
sliderRepMinimumV.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMinimumV.GetPoint2Coordinate().SetValue(.1, .9)
sliderRepMinimumV.SetTubeWidth(tubeWidth)
sliderRepMinimumV.SetSliderLength(sliderLength)
sliderRepMinimumV.SetTitleHeight(titleHeight)
sliderRepMinimumV.SetLabelHeight(labelHeight)
sliderWidgetMinimumV = vtk.vtkSliderWidget()
sliderWidgetMinimumV.SetInteractor(interactor)
sliderWidgetMinimumV.SetRepresentation(sliderRepMinimumV)
sliderWidgetMinimumV.SetAnimationModeToAnimate()
sliderWidgetMinimumV.EnabledOn()
sliderWidgetMinimumV.AddObserver(vtk.vtkCommand.InteractionEvent, SliderCallbackMinimumV(surface))
sliderRepMaximumV = vtk.vtkSliderRepresentation2D()
sliderRepMaximumV.SetMinimumValue(0.05)
sliderRepMaximumV.SetMaximumValue(vtk.vtkMath.Pi() - .05)
sliderRepMaximumV.SetValue(vtk.vtkMath.Pi())
sliderRepMaximumV.SetTitleText("V max")
sliderRepMaximumV.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMaximumV.GetPoint1Coordinate().SetValue(.9, .1)
sliderRepMaximumV.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
sliderRepMaximumV.GetPoint2Coordinate().SetValue(.9, .9)
sliderRepMaximumV.SetTubeWidth(tubeWidth)
sliderRepMaximumV.SetSliderLength(sliderLength)
sliderRepMaximumV.SetTitleHeight(titleHeight)
sliderRepMaximumV.SetLabelHeight(labelHeight)
sliderWidgetMaximumV = vtk.vtkSliderWidget()
sliderWidgetMaximumV.SetInteractor(interactor)
sliderWidgetMaximumV.SetRepresentation(sliderRepMaximumV)
sliderWidgetMaximumV.SetAnimationModeToAnimate()
sliderWidgetMaximumV.EnabledOn()
sliderWidgetMaximumV.AddObserver(vtk.vtkCommand.InteractionEvent, SliderCallbackMaximumV(surface))
surface.SetMinimumU(-4.5)
surface.SetMaximumU(4.5)
surface.SetMinimumV(0.05)
surface.SetMaximumV(vtk.vtkMath.Pi() - .05)
renderer.ResetCamera()
renderer.GetActiveCamera().Azimuth(30)
renderer.GetActiveCamera().Elevation(-30)
renderer.GetActiveCamera().Zoom(0.9)
renderer.ResetCameraClippingRange()
renderWindow.Render()
interactor.Initialize()
interactor.Start()
# These callbacks do the actual work.
# Callbacks for the interactions
class SliderCallbackMinimumU(object):
def __init__(self, kuen):
self.kuen = kuen
def __call__(self, caller, ev):
sliderWidget = caller
value = sliderWidget.GetRepresentation().GetValue()
if value > 0.9 * self.kuen.GetMaximumU():
value = 0.99 * self.kuen.GetMaximumU()
sliderWidget.GetRepresentation().SetValue(value)
self.kuen.SetMinimumU(value)
class SliderCallbackMaximumU(object):
def __init__(self, kuen):
self.kuen = kuen
def __call__(self, caller, ev):
sliderWidget = caller
value = sliderWidget.GetRepresentation().GetValue()
if value < self.kuen.GetMinimumU() + .01:
value = self.kuen.GetMinimumU() + .01
sliderWidget.GetRepresentation().SetValue(value)
self.kuen.SetMaximumU(value)
class SliderCallbackMinimumV(object):
def __init__(self, kuen):
self.kuen = kuen
def __call__(self, caller, ev):
sliderWidget = caller
value = sliderWidget.GetRepresentation().GetValue()
if value > 0.9 * self.kuen.GetMaximumV():
value = 0.99 * self.kuen.GetMaximumV()
sliderWidget.GetRepresentation().SetValue(value)
self.kuen.SetMinimumV(value)
class SliderCallbackMaximumV(object):
def __init__(self, kuen):
self.kuen = kuen
def __call__(self, caller, ev):
sliderWidget = caller
value = sliderWidget.GetRepresentation().GetValue()
if value < self.kuen.GetMinimumV() + .01:
value = self.kuen.GetMinimumV() + .01
sliderWidget.GetRepresentation().SetValue(value)
self.kuen.SetMaximumV(value)
if __name__ == '__main__':
main()
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