[vtkusers] Tubefilter with changing radius and color

Phuong Toan Tran PhuongToan.Tran at kuleuven.be
Wed May 14 07:56:00 EDT 2014


Dear all,

I adapted the code from this example (http://www.vtk.org/Wiki/VTK/Examples/Cxx/VisualizationAlgorithms/TubesWithVaryingRadiusAndColors) to have a broken line with varying radius and colors generated from a line source (see code below). I would like to have a tube with radii and colors that change rapidly, i.e. display two distinct segments with different radii and colors with a clear separation. Everything works well if the points of the line source are close enough (highResTube.png, distance = 1). But when increasing the distance between those points, there is no clear separation between the two parts of the line anymore (see lowResTube.png, distance=10) because of the interpolation that the tube filter performs.

Is there a way to have this distinct separation (i.e. like in highResTube.png) without increasing the number of points (vtkPoints) that the lineSource takes as input ? I tried

lineSource_->SetResolution(100);

but without success. Also tried to put the arrays containing the radii and colors in cell data, but no success either. Anyone would have a clue on this ? =)

Regards,
Toan

----------------

    double res = 1.; //Tube appearance changes depending on proximity of points, i.e. resolution
    double start = 0.;
    double end = 50.;
    int nPoints = static_cast<int>((end-start) / res);

    // VTK points
    vtkSmartPointer<vtkPoints> points_ = vtkSmartPointer<vtkPoints>::New();
    points_->SetNumberOfPoints(nPoints);
    double sum = 0;
    for(int ii = 0; ii < nPoints; ++ii)
    {
        points_->SetPoint(ii, sum, 0, 0);
        sum += res;
    }

    // VTK line source
    vtkSmartPointer<vtkLineSource> lineSource_ = vtkSmartPointer<vtkLineSource>::New();
    lineSource_->SetPoints(points_);
    lineSource_->Update();

    // Radius
    vtkSmartPointer<vtkDoubleArray> radius_ = vtkSmartPointer<vtkDoubleArray>::New();
    radius_->SetName("TubeRadius");
    radius_->SetNumberOfTuples(nPoints);
    for(int ii = 0; ii < nPoints; ++ii)
    {
        if(ii < nPoints/2)
            radius_->SetTuple1(ii, 2);
        else
            radius_->SetTuple1(ii, 4);
    }

    // Color
    vtkSmartPointer<vtkUnsignedCharArray> color_ = vtkSmartPointer<vtkUnsignedCharArray>::New();
    color_->SetName("Colors");
    color_->SetNumberOfComponents(3);
    color_->SetNumberOfTuples(nPoints);
    for(int ii = 0; ii < nPoints; ++ii)
    {
        if(ii < nPoints/2)
            color_->InsertTuple3(ii, 255, 0, 0);
        else
            color_->InsertTuple3(ii, 0, 255, 0);
    }

    lineSource_->GetOutput()->GetPointData()->AddArray(radius_);
    lineSource_->GetOutput()->GetPointData()->SetActiveScalars(radius_->GetName());
    lineSource_->GetOutput()->GetPointData()->AddArray(color_);

    // VTK tube filter
    vtkSmartPointer<vtkTubeFilter> tubeFilter_ = vtkSmartPointer<vtkTubeFilter>::New();
    tubeFilter_->SetInputData(lineSource_->GetOutput());

    tubeFilter_->CappingOn();
    tubeFilter_->SetNumberOfSides(10);
    tubeFilter_->SetVaryRadiusToVaryRadiusByAbsoluteScalar();

    // VTK mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(tubeFilter_->GetOutputPort());
    mapper->ScalarVisibilityOn();
    mapper->SetScalarModeToUsePointFieldData();
    mapper->SelectColorArray(color_->GetName());

    // VTK actor
    vtkSmartPointer<vtkActor> actor_ = vtkSmartPointer<vtkActor>::New();
    actor_->SetMapper(mapper);

    renderer_->AddActor(actor_);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140514/a8e07be7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lowResTube.png
Type: image/png
Size: 9344 bytes
Desc: lowResTube.png
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140514/a8e07be7/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: highResTube.png
Type: image/png
Size: 8659 bytes
Desc: highResTube.png
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140514/a8e07be7/attachment-0001.png>


More information about the vtkusers mailing list