VTK学习笔记(二十三)vtk空间几何变换

  • 1、VTK相关的类有:
  • 2、OrientedArrow.py
  • 3、EllipticalCylinderDemo.py
  • 4、OrientedCylinder.py
  • 5、CollisionDetection.py

1、VTK相关的类有:

vtkTransform, vtkTransformFilter, vtkMatrix4x4等

相关的方法有:

• RotateX(angle)、RotateY(angle)、RotateZ(angle)• RotateWXYZ(angle,x,y,z)• Scale(x,y,z)• Translate(x,y,z)• SetMatrix(m)、GetMatrix(m)• PostMultiply()、PreMultiply()• SetPosition(x,y,z)、GetPosition(x,y,z)• SetOrientation(x,y,z)、 GetOrientation(x,y,z)• SetScale(sx,sy,sz)• SetOrigin(x,y,z)、GetOrigin

空间变换类

vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();

在VTK中,矩阵乘法默认是左乘(PreMultiply),在齐次矩阵符号中,M = M*A,其中M为当前变换矩阵,A为要应用矩阵(比如旋转矩阵或者平移矩阵)。

 trans->PreMultiply();//M=M*A

源码中是这样解释的: vtkTransform.h的156行

 * Sets the internal state of the transform to PreMultiply. All subsequent* operations will occur before those already represented in the* current transformation.  In homogeneous matrix notation, M = M*A where* M is the current transformation matrix and A is the applied matrix.* The default is PreMultiply.

A就是指“All subsequent operations”,M就是指“those already represented in the current transformation”
我对before的理解就是A左乘M(A的左边乘上M)

当然,也可以设置矩阵乘法为右乘(PostMultiply),在齐次矩阵符号中,M = A*M,其中M为当前变换矩阵,A为要应用矩阵(比如旋转矩阵或者平移矩阵)。

trans->PostMultiply();//M=A*M

源码中是这样写的:

   * Sets the internal state of the transform to PostMultiply. All subsequent* operations will occur after those already represented in the* current transformation.  In homogeneous matrix notation, M = A*M where* M is the current transformation matrix and A is the applied matrix.* The default is PreMultiply.

我对after的理解就是A右乘M(A的右边乘上M)

实例分析:

三维物体的初始齐次矩阵为:

矩阵右乘(post后置),先旋转再平移

trans->PostMultiply();//M=A*M
trans->RotateZ(30);
trans->Translate(1, 0, 0);
#include <vtkLineSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkAxesActor.h>
#include <vtkConeSource.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkTransform.h>
#include <vtkSphereSource.h>#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType)int main(int, char *[])
{vtkSmartPointer<vtkSphereSource> sphereSource =vtkSmartPointer<vtkSphereSource>::New();sphereSource->SetRadius(0.1);//设置半径sphereSource->Update();vtkSmartPointer<vtkPolyDataMapper> sphereMapper =vtkSmartPointer<vtkPolyDataMapper>::New();sphereMapper->SetInputConnection(sphereSource->GetOutputPort());vtkSmartPointer<vtkActor> sphereActor =vtkSmartPointer<vtkActor>::New();sphereActor->SetPosition(0, 0, 0);//设置演员的位置sphereActor->SetMapper(sphereMapper);//设置演员的映射器sphereActor->GetProperty()->SetColor(1, 0, 0);//设置演员的颜色vtkSmartPointer<vtkConeSource> coneSource =vtkSmartPointer<vtkConeSource>::New();coneSource->SetRadius(.2);coneSource->SetHeight(.5);coneSource->SetCenter(0, 0, 0);vtkSmartPointer<vtkPolyDataMapper> coneMapper =vtkSmartPointer<vtkPolyDataMapper>::New();coneMapper->SetInputConnection(coneSource->GetOutputPort());vtkSmartPointer<vtkActor> coneActor =vtkSmartPointer<vtkActor>::New();coneActor->SetMapper(coneMapper);vtkSmartPointer<vtkActor> oriConeActor =vtkSmartPointer<vtkActor>::New();oriConeActor->SetMapper(coneMapper);
#define AXIS_LEN 1.vtkSmartPointer<vtkAxesActor> oriAxesActor =vtkSmartPointer<vtkAxesActor>::New();oriAxesActor->SetPosition(0, 0, 0);oriAxesActor->SetTotalLength(AXIS_LEN, AXIS_LEN, AXIS_LEN);oriAxesActor->SetShaftType(0);oriAxesActor->SetAxisLabels(0);oriAxesActor->SetCylinderRadius(0.02);vtkSmartPointer<vtkAxesActor> axesActor =vtkSmartPointer<vtkAxesActor>::New();axesActor->SetPosition(0, 0, 0);axesActor->SetTotalLength(AXIS_LEN, AXIS_LEN, AXIS_LEN);axesActor->SetShaftType(0);axesActor->SetAxisLabels(0);axesActor->SetCylinderRadius(0.02);vtkSmartPointer<vtkTextActor> textActor =vtkSmartPointer<vtkTextActor>::New();textActor->SetPosition2(100, 40);textActor->GetTextProperty()->SetFontSize(24);textActor->GetTextProperty()->SetColor(1, 0, 0);vtkSmartPointer<vtkTransform> trans =vtkSmartPointer<vtkTransform>::New();//trans->PostMultiply();//M=A*Mtrans->PreMultiply();//M=M*Atrans->RotateZ(30);//coneActor->SetPosition(1, 0, 0);永远M=M*Atrans->Translate(1, 0, 0);//trans->RotateZ(30);//trans->Translate(1, 0, 0);//trans->RotateZ(30);coneActor->SetUserTransform(trans);textActor->SetInput("PostMultiply()\nTranslate(1, 0, 0)\nRotateZ(30)");cout << "GetMatrix:" << endl;if (coneActor->GetMatrix() != NULL){coneActor->GetMatrix()->Print(cout);}else{cout << "NULL" << endl;}cout << "GetUserMatrix:" << endl;if (coneActor->GetUserMatrix() != NULL){coneActor->GetUserMatrix()->Print(cout);}else{cout << "NULL" << endl;}vtkSmartPointer<vtkRenderer> renderer1 =vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> renderer2 =vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->SetSize(800, 400);renderWindow->AddRenderer(renderer1);renderWindow->AddRenderer(renderer2);vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->SetRenderWindow(renderWindow);double leftViewport[] = { 0.0, 0.0, 0.5, 1.0 };double rightViewport[] = { 0.5, 0.0, 1.0, 1.0 };renderer1->AddActor(oriAxesActor);renderer1->AddActor(sphereActor);renderer1->AddActor(oriConeActor);renderer2->AddActor(axesActor);renderer2->AddActor(sphereActor);renderer2->AddActor(coneActor);renderer2->AddActor2D(textActor);renderer1->SetBackground(.3, .3, .5);renderer2->SetBackground(.2, .4, .5);renderer1->SetViewport(leftViewport);renderer2->SetViewport(rightViewport);renderWindow->Render();renderWindowInteractor->Start();return EXIT_SUCCESS;
}

参考:【VTK学习】空间几何变换
参考:三维空间几何变换原理[平移、旋转、错切]
参考:VTK中模型的旋转与平移
参考:VTK笔记——空间几何变换(Transform),平移、旋转和缩放

2、OrientedArrow.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-import vtk'''
There are two alternative ways to apply the transform.1) Use vtkTransformPolyDataFilter to create a new transformed polydata.This method is useful if the transformed polydata is neededlater in the pipelineTo do this, set USER_MATRIX = True2) Apply the transform directly to the actor using vtkProp3D's SetUserMatrix.No new data is produced.To do this, set USER_MATRIX = False
'''
USER_MATRIX = Truedef main():colors = vtk.vtkNamedColors()# Set the background color.colors.SetColor("BkgColor", [26, 51, 77, 255])# Create an arrow.arrowSource = vtk.vtkArrowSource()# Generate a random start and end pointstartPoint = [0] * 3endPoint = [0] * 3rng = vtk.vtkMinimalStandardRandomSequence()rng.SetSeed(8775070)  # For testing.for i in range(0, 3):rng.Next()startPoint[i] = rng.GetRangeValue(-10, 10)rng.Next()endPoint[i] = rng.GetRangeValue(-10, 10)# Compute a basisnormalizedX = [0] * 3normalizedY = [0] * 3normalizedZ = [0] * 3# The X axis is a vector from start to endvtk.vtkMath.Subtract(endPoint, startPoint, normalizedX)length = vtk.vtkMath.Norm(normalizedX)vtk.vtkMath.Normalize(normalizedX)# The Z axis is an arbitrary vector cross Xarbitrary = [0] * 3for i in range(0, 3):rng.Next()arbitrary[i] = rng.GetRangeValue(-10, 10)vtk.vtkMath.Cross(normalizedX, arbitrary, normalizedZ)vtk.vtkMath.Normalize(normalizedZ)# The Y axis is Z cross Xvtk.vtkMath.Cross(normalizedZ, normalizedX, normalizedY)matrix = vtk.vtkMatrix4x4()# Create the direction cosine matrixmatrix.Identity()for i in range(0, 3):matrix.SetElement(i, 0, normalizedX[i])matrix.SetElement(i, 1, normalizedY[i])matrix.SetElement(i, 2, normalizedZ[i])# Apply the transformstransform = vtk.vtkTransform()transform.Translate(startPoint)transform.Concatenate(matrix)transform.Scale(length, length, length)# Transform the polydatatransformPD = vtk.vtkTransformPolyDataFilter()transformPD.SetTransform(transform)transformPD.SetInputConnection(arrowSource.GetOutputPort())# Create a mapper and actor for the arrowmapper = vtk.vtkPolyDataMapper()actor = vtk.vtkActor()if USER_MATRIX:mapper.SetInputConnection(arrowSource.GetOutputPort())actor.SetUserMatrix(transform.GetMatrix())else:mapper.SetInputConnection(transformPD.GetOutputPort())actor.SetMapper(mapper)actor.GetProperty().SetColor(colors.GetColor3d("Cyan"))# Create spheres for start and end pointsphereStartSource = vtk.vtkSphereSource()sphereStartSource.SetCenter(startPoint)sphereStartSource.SetRadius(0.8)sphereStartMapper = vtk.vtkPolyDataMapper()sphereStartMapper.SetInputConnection(sphereStartSource.GetOutputPort())sphereStart = vtk.vtkActor()sphereStart.SetMapper(sphereStartMapper)sphereStart.GetProperty().SetColor(colors.GetColor3d("Yellow"))sphereEndSource = vtk.vtkSphereSource()sphereEndSource.SetCenter(endPoint)sphereEndSource.SetRadius(0.8)sphereEndMapper = vtk.vtkPolyDataMapper()sphereEndMapper.SetInputConnection(sphereEndSource.GetOutputPort())sphereEnd = vtk.vtkActor()sphereEnd.SetMapper(sphereEndMapper)sphereEnd.GetProperty().SetColor(colors.GetColor3d("Magenta"))# Create a renderer, render window, and interactorrenderer = vtk.vtkRenderer()renderWindow = vtk.vtkRenderWindow()renderWindow.SetWindowName("Oriented Arrow")renderWindow.AddRenderer(renderer)renderWindowInteractor = vtk.vtkRenderWindowInteractor()renderWindowInteractor.SetRenderWindow(renderWindow)# Add the actor to the scenerenderer.AddActor(actor)renderer.AddActor(sphereStart)renderer.AddActor(sphereEnd)renderer.SetBackground(colors.GetColor3d("BkgColor"))# Render and interactrenderWindow.Render()renderWindowInteractor.Start()if __name__ == '__main__':main()

3、EllipticalCylinderDemo.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-import mathimport vtkdef main():nx, ny, nz = get_program_parameters()colors = vtk.vtkNamedColors()angle = 0r1 = 50r2 = 30centerX = 10.0centerY = 5.0points = vtk.vtkPoints()idx = 0while angle <= 2.0 * vtk.vtkMath.Pi() + (vtk.vtkMath.Pi() / 60.0):points.InsertNextPoint(r1 * math.cos(angle) + centerX,r2 * math.sin(angle) + centerY,0.0)angle = angle + (vtk.vtkMath.Pi() / 60.0)idx += 1line = vtk.vtkPolyLine()line.GetPointIds().SetNumberOfIds(idx)for i in range(0, idx):line.GetPointIds().SetId(i, i)lines = vtk.vtkCellArray()lines.InsertNextCell(line)polyData = vtk.vtkPolyData()polyData.SetPoints(points)polyData.SetLines(lines)extrude = vtk.vtkLinearExtrusionFilter()extrude.SetInputData(polyData)extrude.SetExtrusionTypeToNormalExtrusion()extrude.SetVector(nx, ny, nz)extrude.Update()# Create an oriented arrowstartPoint = [0.0] * 3endPoint = [0.0] * 3startPoint[0] = centerXstartPoint[1] = centerYstartPoint[2] = 0.0endPoint[0] = startPoint[0] + extrude.GetVector()[0]endPoint[1] = startPoint[1] + extrude.GetVector()[1]endPoint[2] = startPoint[2] + extrude.GetVector()[2]# Compute a basisnormalizedX = [0.0] * 3normalizedY = [0.0] * 3normalizedZ = [0.0] * 3# The X axis is a vector from start to endvtk.vtkMath.Subtract(endPoint, startPoint, normalizedX)length = vtk.vtkMath.Norm(normalizedX)vtk.vtkMath.Normalize(normalizedX)# The Z axis is an arbitrary vector cross Xarbitrary = [0.0] * 3arbitrary[0] = vtk.vtkMath.Random(-10, 10)arbitrary[1] = vtk.vtkMath.Random(-10, 10)arbitrary[2] = vtk.vtkMath.Random(-10, 10)vtk.vtkMath.Cross(normalizedX, arbitrary, normalizedZ)vtk.vtkMath.Normalize(normalizedZ)# The Y axis is Z cross Xvtk.vtkMath.Cross(normalizedZ, normalizedX, normalizedY)matrix = vtk.vtkMatrix4x4()# Create the direction cosine matrixmatrix.Identity()for i in range(0, 3):matrix.SetElement(i, 0, normalizedX[i])matrix.SetElement(i, 1, normalizedY[i])matrix.SetElement(i, 2, normalizedZ[i])# Apply the transformstransform = vtk.vtkTransform()transform.Translate(startPoint)transform.Concatenate(matrix)transform.Scale(length, length, length)arrowSource = vtk.vtkArrowSource()arrowSource.SetTipResolution(31)arrowSource.SetShaftResolution(21)# Transform the polydatatransformPD = vtk.vtkTransformPolyDataFilter()transformPD.SetTransform(transform)transformPD.SetInputConnection(arrowSource.GetOutputPort())# Create a mapper and actor for the arrowarrowMapper = vtk.vtkPolyDataMapper()arrowMapper.SetInputConnection(transformPD.GetOutputPort())arrowActor = vtk.vtkActor()arrowActor.SetMapper(arrowMapper)arrowActor.GetProperty().SetColor(colors.GetColor3d("Tomato"))tubes = vtk.vtkTubeFilter()tubes.SetInputData(polyData)tubes.SetRadius(2.0)tubes.SetNumberOfSides(21)lineMapper = vtk.vtkPolyDataMapper()lineMapper.SetInputConnection(tubes.GetOutputPort())lineActor = vtk.vtkActor()lineActor.SetMapper(lineMapper)lineActor.GetProperty().SetColor(colors.GetColor3d("Peacock"))mapper = vtk.vtkPolyDataMapper()mapper.SetInputConnection(extrude.GetOutputPort())actor = vtk.vtkActor()actor.SetMapper(mapper)actor.GetProperty().SetColor(colors.GetColor3d("Banana"))actor.GetProperty().SetOpacity(.7)ren = vtk.vtkRenderer()ren.SetBackground(colors.GetColor3d("SlateGray"))ren.AddActor(actor)ren.AddActor(lineActor)ren.AddActor(arrowActor)renWin = vtk.vtkRenderWindow()renWin.SetWindowName("Elliptical Cylinder Demo")renWin.AddRenderer(ren)renWin.SetSize(600, 600)iren = vtk.vtkRenderWindowInteractor()iren.SetRenderWindow(renWin)style = vtk.vtkInteractorStyleTrackballCamera()iren.SetInteractorStyle(style)camera = vtk.vtkCamera()camera.SetPosition(0, 1, 0)camera.SetFocalPoint(0, 0, 0)camera.SetViewUp(0, 0, 1)camera.Azimuth(30)camera.Elevation(30)ren.SetActiveCamera(camera)ren.ResetCamera()ren.ResetCameraClippingRange()renWin.Render()iren.Start()def get_program_parameters():import argparsedescription = 'Elliptical Cylinder Demo.'epilogue = '''
The example shows the vtkPolyLine that forms the base of the elliptical cylinderand an oriented arrow that represents the vector that vtkLinearExtrusionFilteruses to create the cylinder.
The example takes an optional triple that defines the vector for the filter.
The length of the vector is the height of the cylinder.'''parser = argparse.ArgumentParser(description=description, epilog=epilogue,formatter_class=argparse.RawDescriptionHelpFormatter)parser.add_argument('-nx', nargs='?', const=0.0, default=0.0, type=float)parser.add_argument('-ny', nargs='?', const=0.0, default=0.0, type=float)parser.add_argument('-nz', nargs='?', const=100.0, default=100.0, type=float)args = parser.parse_args()return args.nx, args.ny, args.nzif __name__ == '__main__':main()

4、OrientedCylinder.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-import vtk'''
There are two alternative ways to apply the transform.1) Use vtkTransformPolyDataFilter to create a new transformed polydata.This method is useful if the transformed polydata is neededlater in the pipelineTo do this, set USER_MATRIX = True2) Apply the transform directly to the actor using vtkProp3D's SetUserMatrix.No new data is produced.To do this, set USER_MATRIX = False
'''
USER_MATRIX = Truedef main():colors = vtk.vtkNamedColors()# Set the background color.colors.SetColor("BkgColor", [26, 51, 77, 255])# Create a cylinder.# Cylinder height vector is (0,1,0).# Cylinder center is in the middle of the cylindercylinderSource = vtk.vtkCylinderSource()cylinderSource.SetResolution(15)# Generate a random start and end pointstartPoint = [0] * 3endPoint = [0] * 3rng = vtk.vtkMinimalStandardRandomSequence()rng.SetSeed(8775070)  # For testing.for i in range(0, 3):rng.Next()startPoint[i] = rng.GetRangeValue(-10, 10)rng.Next()endPoint[i] = rng.GetRangeValue(-10, 10)# Compute a basisnormalizedX = [0] * 3normalizedY = [0] * 3normalizedZ = [0] * 3# The X axis is a vector from start to endvtk.vtkMath.Subtract(endPoint, startPoint, normalizedX)length = vtk.vtkMath.Norm(normalizedX)vtk.vtkMath.Normalize(normalizedX)# The Z axis is an arbitrary vector cross Xarbitrary = [0] * 3for i in range(0, 3):rng.Next()arbitrary[i] = rng.GetRangeValue(-10, 10)vtk.vtkMath.Cross(normalizedX, arbitrary, normalizedZ)vtk.vtkMath.Normalize(normalizedZ)# The Y axis is Z cross Xvtk.vtkMath.Cross(normalizedZ, normalizedX, normalizedY)matrix = vtk.vtkMatrix4x4()# Create the direction cosine matrixmatrix.Identity()for i in range(0, 3):matrix.SetElement(i, 0, normalizedX[i])matrix.SetElement(i, 1, normalizedY[i])matrix.SetElement(i, 2, normalizedZ[i])# Apply the transformstransform = vtk.vtkTransform()transform.Translate(startPoint)  # translate to starting pointtransform.Concatenate(matrix)  # apply direction cosinestransform.RotateZ(-90.0)  # align cylinder to x axistransform.Scale(1.0, length, 1.0)  # scale along the height vectortransform.Translate(0, .5, 0)  # translate to start of cylinder# Transform the polydatatransformPD = vtk.vtkTransformPolyDataFilter()transformPD.SetTransform(transform)transformPD.SetInputConnection(cylinderSource.GetOutputPort())# Create a mapper and actor for the arrowmapper = vtk.vtkPolyDataMapper()actor = vtk.vtkActor()if USER_MATRIX:mapper.SetInputConnection(cylinderSource.GetOutputPort())actor.SetUserMatrix(transform.GetMatrix())else:mapper.SetInputConnection(transformPD.GetOutputPort())actor.SetMapper(mapper)actor.GetProperty().SetColor(colors.GetColor3d("Cyan"))# Create spheres for start and end pointsphereStartSource = vtk.vtkSphereSource()sphereStartSource.SetCenter(startPoint)sphereStartSource.SetRadius(0.8)sphereStartMapper = vtk.vtkPolyDataMapper()sphereStartMapper.SetInputConnection(sphereStartSource.GetOutputPort())sphereStart = vtk.vtkActor()sphereStart.SetMapper(sphereStartMapper)sphereStart.GetProperty().SetColor(colors.GetColor3d("Yellow"))sphereEndSource = vtk.vtkSphereSource()sphereEndSource.SetCenter(endPoint)sphereEndSource.SetRadius(0.8)sphereEndMapper = vtk.vtkPolyDataMapper()sphereEndMapper.SetInputConnection(sphereEndSource.GetOutputPort())sphereEnd = vtk.vtkActor()sphereEnd.SetMapper(sphereEndMapper)sphereEnd.GetProperty().SetColor(colors.GetColor3d("Magenta"))# Create a renderer, render window, and interactorrenderer = vtk.vtkRenderer()renderWindow = vtk.vtkRenderWindow()renderWindow.AddRenderer(renderer)renderWindow.SetWindowName("Oriented Cylinder")renderWindowInteractor = vtk.vtkRenderWindowInteractor()renderWindowInteractor.SetRenderWindow(renderWindow)# Add the actor to the scenerenderer.AddActor(actor)renderer.AddActor(sphereStart)renderer.AddActor(sphereEnd)renderer.SetBackground(colors.GetColor3d("BkgColor"))# Render and interactrenderWindow.Render()renderWindowInteractor.Start()if __name__ == '__main__':main()

5、CollisionDetection.py

#!/usr/bin/env pythonimport timeimport vtkdef get_program_parameters():import argparsedescription = 'Collision detection.'epilogue = '''
This examples uses vtkCollisionDetectionFilter to find the intersection between afixed (solid white) and moving (red wireframe) sphere.
The animation places the moving sphere some distance from the fixed sphere andmoves the moving sphere until it contacts the fixed sphere.Three collision modes are available and can be set as the first argument on the command line.1. All contacts (0) finds all the contacting cell pairs with two points per collision.
2. First contact (1) quickly find the first contact point.
3. Half contacts (2) finds all the contacting cell pairs with one points per collision.'''parser = argparse.ArgumentParser(description=description, epilog=epilogue,formatter_class=argparse.RawDescriptionHelpFormatter)parser.add_argument('contactMode', nargs='?', default=0, type=int, help='Contact mode 0 (default), 1, or 2.')args = parser.parse_args()return args.contactModedef main():contactMode = get_program_parameters()# Define colorscolors = vtk.vtkNamedColors()sphere0 = vtk.vtkSphereSource()sphere0.SetRadius(.29)sphere0.SetPhiResolution(31)sphere0.SetThetaResolution(31)sphere0.SetCenter(0.0, 0, 0)sphere1 = vtk.vtkSphereSource()sphere1.SetPhiResolution(30)sphere1.SetThetaResolution(30)sphere1.SetRadius(0.3)matrix1 = vtk.vtkMatrix4x4()transform0 = vtk.vtkTransform()collide = vtk.vtkCollisionDetectionFilter()collide.SetInputConnection(0, sphere0.GetOutputPort())collide.SetTransform(0, transform0)collide.SetInputConnection(1, sphere1.GetOutputPort())collide.SetMatrix(1, matrix1)collide.SetBoxTolerance(0.0)collide.SetCellTolerance(0.0)collide.SetNumberOfCellsPerNode(2)if contactMode == 0:collide.SetCollisionModeToAllContacts()elif contactMode == 1:collide.SetCollisionModeToFirstContact()else:collide.SetCollisionModeToHalfContacts()collide.GenerateScalarsOn()# Visualizemapper1 = vtk.vtkPolyDataMapper()mapper1.SetInputConnection(collide.GetOutputPort(0))mapper1.ScalarVisibilityOff()actor1 = vtk.vtkActor()actor1.SetMapper(mapper1)actor1.GetProperty().BackfaceCullingOn()actor1.SetUserTransform(transform0)actor1.GetProperty().SetDiffuseColor(colors.GetColor3d("Tomato"))actor1.GetProperty().SetRepresentationToWireframe()mapper2 = vtk.vtkPolyDataMapper()mapper2.SetInputConnection(collide.GetOutputPort(1))actor2 = vtk.vtkActor()actor2.SetMapper(mapper2)actor2.GetProperty().BackfaceCullingOn()actor2.SetUserMatrix(matrix1)mapper3 = vtk.vtkPolyDataMapper()mapper3.SetInputConnection(collide.GetContactsOutputPort())mapper3.SetResolveCoincidentTopologyToPolygonOffset()actor3 = vtk.vtkActor()actor3.SetMapper(mapper3)actor3.GetProperty().SetColor(colors.GetColor3d("Black"))actor3.GetProperty().SetLineWidth(3.0)txt = vtk.vtkTextActor()txt.GetTextProperty().SetFontSize(18)renderer = vtk.vtkRenderer()renderer.UseHiddenLineRemovalOn()renderer.AddActor(actor1)renderer.AddActor(actor2)renderer.AddActor(actor3)renderer.AddActor(txt)renderer.SetBackground(colors.GetColor3d("Gray"))renderer.UseHiddenLineRemovalOn()renderWindow = vtk.vtkRenderWindow()renderWindow.SetSize(640, 480)renderWindow.AddRenderer(renderer)interactor = vtk.vtkRenderWindowInteractor()interactor.SetRenderWindow(renderWindow)# Move the first objectnumSteps = 100dx = 1.0 / float(numSteps) * 2.0transform0.Translate(-numSteps * dx - .3, 0.0, 0.0)renderWindow.Render()renderer.GetActiveCamera().Azimuth(-60)renderer.GetActiveCamera().Elevation(45)renderer.GetActiveCamera().Dolly(1.2)renderWindow.SetWindowName('CollisionDetection')renderWindow.Render()for i in range(0, numSteps):transform0.Translate(dx, 0.0, 0.0)renderer.ResetCameraClippingRange()s = '{:s}: Number of contact cells is {:d}'.format(collide.GetCollisionModeAsString(),collide.GetNumberOfContacts())txt.SetInput(s)renderWindow.Render()if collide.GetNumberOfContacts() > 0:break# The total animation time is 3 secondstime.sleep(3.0 / numSteps)renderer.ResetCamera()renderWindow.Render()renderWindow.Render()interactor.Start()# In Field Data there will be an array named "ContactCells".# This array indexes contacting cells (e.g.) index 10 of array 0#  points to a cell (triangle) which contacts/intersects a cell#  at index 10 of array 1.# You can directly obtain these, see GetContactCells(int i)#  in the documentation.# print(collide.GetOutput(0))# print(collide.GetOutput(1))if __name__ == '__main__':main()

VTK学习笔记(二十三)vtk空间几何变换相关推荐

  1. 立创eda学习笔记二十三:如何将原理图和焊盘关联?(交叉选择)

    这是我在网上看到的一个提问,我对这个提问有两个理解, 1,如何显示原理图符号和PCB封装之间的对应位置关系? 那么可以使用以下的交叉选择功能. 这个功能是用来跳转原理图符号和PCB封装之间的对应位置. ...

  2. python学习笔记(二十三) -- 多进程和多线程

    目录 多线程多进程的意义 多进程的使用 方式一(fork):  只能在Unix/Linux/Mac系统下执行,windows不可以 方式二(multiprocessing.Process): 全平台通 ...

  3. Mr.J-- jQuery学习笔记(二十三)--applycall

    之前在学习JavaScript的时候写的:Mr.J--JS学习(继承模式发展史)里面写了我对原生JS call和apply的理解,以及原型链的应用:prototype apply&call J ...

  4. IOS学习笔记二十三对象归档(NSKeyedArchiver、NSKeyedUnArchiver、NSCodeing)

    1.NSKeyedArchiver.NSKeyedUnArchiver 1).archiveRootObject:toFile 归档对象到这个路径文件 2).unarchiveObjectWithFi ...

  5. Java学习笔记(二十三)日志体系(logback)

    Java日志体系(logback) logback 简介 师出同门,与log4j一样,logback也是由Ceki Gülcü开发的开源日志组件,可以说是log4j的改进版:在现如今的项目中,logb ...

  6. VTK学习笔记(二十一)vtk裁剪求截面面积

    VTK学习笔记(二十一)vtk裁剪求界面面积 1.代码 2.CMakeLists.txt 3.运行输出 4.面积正确性验证 4.1.代码 4.2.执行结果 1.代码 #pragma once#incl ...

  7. VTK学习笔记3:IO操作读取写入xml文件和vtk文件

    1.vtu文件 vtk实战(二十四)--读入vtu数据 vtk实战(三十)--写入vtu数据 2.vtp文件 2.1读入vtp文件 2.2 创建vtp文件 从网络示例和在线文档中,我收集了一些信息,创 ...

  8. VTK学习笔记(三十六)VTK图像填充

    VTK学习笔记(三十六)VTK图像填充 1.官方示例 2.其他例子 总结 1.官方示例 来自官方示例代码,自己只是添加了理解. 代码: #include <vtkCamera.h> #in ...

  9. OpenCV学习笔记(十三):霍夫变换:HoughLines(),HoughLinesP(),HoughCircles( )

    OpenCV学习笔记(十三):霍夫变换:HoughLines(),HoughLinesP(),HoughCircles( ) 1.霍夫线变换HoughLines() OpenCV支持三种不同的霍夫线变 ...

  10. Spring Security技术栈学习笔记(十三)Spring Social集成第三方登录验证开发流程介绍

    开发第三方登录,我们必须首先要了解OAuth协议(本文所讲述的OAuth协议指的是OAuth2协议),本文首先简单介绍OAuth协议,然后基于Spring Social来阐述开发第三方登录需要做哪些准 ...

最新文章

  1. 我的FizzBuzz和一点感想
  2. 在织梦模板中不适用ajax,直接用标签也能调用当前会员的信息
  3. C 语言中double类型数据占字节数为,C 语言中 double 类型数据占字节数为_____
  4. 前端(jQuery UI)(2)-- jQuery UI interactions
  5. (转)什么时候加上android.intent.category.DEFAULT和LAUNCHER
  6. elasticsearch删除索引
  7. mssql sqlserver 验证整型函数分享
  8. HG255D[OpenWrt]从入门到精通
  9. 什么软件画er图方便_er图绘制工具|er图绘制工具(powerdesigner) v16.5 免费版 - 软件下载 - 绿茶软件园|33LC.com...
  10. HTML5期末大作业:网页设计作业网站设计——千与千寻-电影图文(9页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码
  11. free pascal语言学习笔记(一)
  12. LVDS,接口,时序讲解,非常好的文章
  13. jieba对word文档词频分析
  14. linux mysql免安装版配置_Linux下MySQL免安装版安装配置记录
  15. 网络字节序与主机字节序的相互转换
  16. 史上最全最新微信小程序自动化教程
  17. 数学符号的英文表达(持续更新中)
  18. Linux下Samba服务器配置,已成功
  19. 记一次手机本地时间修改引起的https请求失效的bug分享 转 萧竹
  20. Leetcode 刷题必须Review 二 Leetcode(912 215 315 53)

热门文章

  1. 电机与拖动综合控制实验matlab,电机与拖动基础及MATLAB仿真
  2. android电视root权限获取,各大安卓电视盒子通用!用无线获取ROOT权限教程!
  3. tampermonkey(油猴) GM_addStyle
  4. 阿里巴巴是怎么处理大数据的?重磅揭秘!
  5. FakeUserAgentError('Maximum amount of retries reached') 彻底解决办法
  6. APP 自动化框架设计思路分享
  7. js几种常见的设计模式
  8. 同文输入法 android,同文输入法app下载-同文输入法手机版-同文输入法最新版_易玩网...
  9. 计算机二进制乘法运算(原码,补码)
  10. WinAPI设置RGB背景色以及前景色