[vtkusers] how to draw a 3D spring

dean.inglis at camris.ca dean.inglis at camris.ca
Tue Jan 18 07:01:48 EST 2005


Hi Shi,

one way is to try the first bit of code in 
Hybrid/Testing/Cxx/TestOrientationMarkerWidget.cxx:

////////////////////////////////////////////////////////////////////////////////
  // create an actor out of parts of vtkAxesActor and vtkAnnotatedCubeActor
  // to have the widget follow.
  // part 1 is a helical spring to test vtkAxesActor SetUserDefinedShaft
  //
  double dt = vtkMath::DoublePi() / 20.0;
  double t = 0.0;
  double x = 0.0;
  int nPoints = 120;
  double dx = 0.8 / nPoints;

  vtkPoints* points  = vtkPoints::New(VTK_DOUBLE);
  vtkCellArray* line = vtkCellArray::New();
  line->InsertNextCell( nPoints + 80 );

  int i = 0;
// form a ring
  do
    {
    points->InsertPoint( i, 0.1*cos(t), x, 0.1*sin(t) );
    line->InsertCellPoint( i );
    t += dt;
    }while( ++i < 40 );

// form  spiral
  do
    {
    points->InsertPoint( i, 0.1*cos(t), x, 0.1*sin(t) );
    line->InsertCellPoint(i);
    t += dt;
    x += dx;
    }while( ++i < nPoints + 40 );

// form a ring
  do
    {
    points->InsertPoint( i, 0.1*cos(t), x, 0.1*sin(t) );
    line->InsertCellPoint( i );
    t += dt;
    }while( ++i < nPoints + 80 );

  vtkPolyData* wiggle =  vtkPolyData::New();
  wiggle->SetPoints( points );
  wiggle->SetLines( line );

  vtkTubeFilter* tube = vtkTubeFilter::New();
  tube->SetInput( wiggle );
  tube->SetGenerateTCoordsToOff();
  tube->CappingOff();
  tube->SetVaryRadiusToVaryRadiusOff();
  tube->SetRadius( 0.02 );
  tube->SetNumberOfSides( 5 );

////////////////////////////////////////////////////////////////////////////////
send the tube filter output to a vtkPolyDataMapper, then
to a vtkActor etc.
You can preview the effect if you have VTKData baseline images
in  VTKData/Baseline/Hybrid/TestOrientationMarkerWidget.png

regards,
Dean




More information about the vtkusers mailing list