[vtkusers] TubesFromSplines change the tube and line color

Abhishek abhishekworld at gmail.com
Tue Oct 30 05:36:00 EDT 2018


Hello Everyone,

I have modified the TubesFromSplines to set the specific color
(blue(0,0,255) to a tube.

I have tried setting the color directly to the actore and many other
ways but no luck.

Can some one please help me out with this? what I am missing here?




#include <QVector>

#include <QColor>

#include <QList>

#include <QApplication>

#include <QVBoxLayout>

#include "QVTKOpenGLWidget.h"


#include "vtkPolyLine.h"

#include "vtkPoints.h"

#include "vtkCellArray.h"

#include "vtkPolyData.h"

#include "vtkAppendPolyData.h"

#include "vtkAlgorithmOutput.h"

#include "vtkUnsignedCharArray.h"

#include "vtkActor.h"

#include "vtkPolyDataMapper.h"

#include "vtkParametricSpline.h"

#include "vtkParametricFunctionSource.h"

#include "vtkTupleInterpolator.h"

#include "vtkTubeFilter.h"

#include "vtkDoubleArray.h"

#include "vtkInteractorStyleTrackballCamera.h"

#include "vtkPointData.h"

#include "vtkRenderer.h"

#include "vtkRenderWindow.h"

#include "vtkGenericOpenGLRenderWindow.h"


#include "vtkAutoInit.h"

VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2

VTK_MODULE_INIT(vtkInteractionStyle);

VTK_MODULE_INIT(vtkRenderingFreeType);


double convertToz(double n) {

    //NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax
- OldMin)) + NewMin

    if(n < 100) {

        n = 100;

    } else if(n > 300) {

        n = 300;

    }


    return (((n-0)*(0.06-0.0))/(300-100))+ 0.0;

}



int main(int argc, char *argv[]){

 QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

    QApplication a(argc, argv);

QColor color(0,0, 255, 50);

QVector<QVector<double>> positions;



    QList<double>p1;

    p1<< 1.0 <<0.0 <<0.0 << 131.162;

    positions.append(QVector<double>::fromList(p1));

    QList<double>p2;

    p2<< 0.0 <<1.0 <<0.0 << 130.408;

    positions.append(QVector<double>::fromList(p2));

    QList<double>p3;

    p3<< 3.0 <<1.0 <<0.0 << 131.512;

    positions.append(QVector<double>::fromList(p3));

    QList<double>p4;

    p4<< 0.0 <<2.0 <<1.0 << 134.464;

    positions.append(QVector<double>::fromList(p4));

    QList<double>p5;

    p5<< 0.0 <<3.0 <<2.0 << 132.8;

    positions.append(QVector<double>::fromList(p5));

    QList<double>p6;

    p6<< 1.0 <<2.0 <<3.0 << 129.458;

    positions.append(QVector<double>::fromList(p6));



    vtkSmartPointer<vtkPoints> points;

    vtkSmartPointer<vtkParametricSpline> spline;

    vtkSmartPointer<vtkParametricFunctionSource> functionSource;

    vtkSmartPointer<vtkTupleInterpolator> interpolatedRadius;

    vtkSmartPointer<vtkDoubleArray> tubeRadius;

    vtkSmartPointer<vtkPolyData> tubePolyData;

    vtkSmartPointer<vtkTubeFilter> tuber;

    vtkSmartPointer<vtkPolyDataMapper> lineMapper;

    vtkSmartPointer<vtkPolyDataMapper> tubeMapper;

    vtkSmartPointer<vtkActor> lineActor;

    vtkSmartPointer<vtkActor> tubeActor;

    vtkSmartPointer<vtkUnsignedCharArray> lineColors;


    QVTKOpenGLWidget* vtkWidget;

    vtkSmartPointer<vtkRenderer> renderer;

    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow;

    vtkSmartPointer<vtkRenderWindowInteractor> rInteractor;

    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style ;




    lineColors = vtkSmartPointer<vtkUnsignedCharArray>::New();

    lineColors->SetName("Colors");

    lineColors->SetNumberOfComponents(4);

    points = vtkSmartPointer<vtkPoints>::New();

    for(int i=0; i < positions.size(); i++){

        points->InsertPoint(i, positions[i][0], positions[i][1],
positions[i][2]);


     float c[4] = { static_cast<float>(color.red()),
static_cast<float>(color.green()),

                       static_cast<float>(color.blue()),
static_cast<float>(color.alpha()) };

        lineColors->InsertNextTuple(c);

    }


    // Fit a spline to the points

    spline = vtkSmartPointer<vtkParametricSpline>::New();

    spline->SetPoints(points);

    functionSource = vtkSmartPointer<vtkParametricFunctionSource>::New();

    functionSource->SetParametricFunction(spline);

    functionSource->SetUResolution(10 * points->GetNumberOfPoints());

    functionSource->Update();


    // Interpolate the scalars

    interpolatedRadius = vtkSmartPointer<vtkTupleInterpolator> ::New();

    interpolatedRadius->SetInterpolationTypeToLinear();

    interpolatedRadius->SetNumberOfComponents(1);

    for(int j=0; j < positions.size(); j++){

        double rad = convertToz(positions[j][3]);


        interpolatedRadius->AddTuple(j, &rad);


    }

    // Generate the radius scalars

    tubeRadius = vtkSmartPointer<vtkDoubleArray>::New();

    unsigned int n = functionSource->GetOutput()->GetNumberOfPoints();

    tubeRadius->SetNumberOfTuples(n);

    tubeRadius->SetName("TubeRadius");

    double tMin = interpolatedRadius->GetMinimumT();

    double tMax = interpolatedRadius->GetMaximumT();

    double r;

    for (unsigned int i = 0; i < n; ++i)

    {

        double t = (tMax - tMin) / (n - 1) * i + tMin;

        interpolatedRadius->InterpolateTuple(t, &r);

        tubeRadius->SetTuple1(i, r);

        float c[4] = { static_cast<float>(color.red()),
static_cast<float>(color.green()),

                       static_cast<float>(color.blue()),
static_cast<float>(color.alpha()) };

        lineColors->InsertNextTuple(c);

    }


    // Add the scalars to the polydata

    tubePolyData = vtkSmartPointer<vtkPolyData>::New();

    tubePolyData = functionSource->GetOutput();

    tubePolyData->GetPointData()->AddArray(tubeRadius);

    tubePolyData->GetPointData()->SetActiveScalars("TubeRadius");

    // set the color array

    tubePolyData->GetPointData()->AddArray(lineColors);

    // Create the tubes

    tuber = vtkSmartPointer<vtkTubeFilter>::New();


    tuber->SetInputData(tubePolyData);


    tuber->SetNumberOfSides(20);

    tuber->SetVaryRadiusToVaryRadiusByAbsoluteScalar();


    //--------------

    // Setup actors and mappers

    lineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();


    lineMapper->SetInputData(tubePolyData);


    //    lineMapper->SetScalarRange(tubePolyData->GetScalarRange());

    lineMapper->SelectColorArray("Colors");

    lineActor = vtkSmartPointer<vtkActor>::New();

    lineActor->SetMapper(lineMapper);


    tubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();

    tubeMapper->SetInputConnection(tuber->GetOutputPort());

    tubeMapper->SetScalarRange(tubePolyData->GetScalarRange());

    // Activate the color array

    tubeMapper->SelectColorArray("Colors");





    tubeActor = vtkSmartPointer<vtkActor>::New();


    tubeActor->SetMapper(tubeMapper);


    vtkWidget = new QVTKOpenGLWidget;

    vtkWidget->setMinimumSize(800,600);

    renderer = vtkSmartPointer<vtkRenderer>::New();

    renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();

    style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();


    renderer->AddActor(tubeActor);

    renderer->AddActor(lineActor);


    vtkWidget->SetRenderWindow(renderWindow);

    vtkWidget->GetRenderWindow()->AddRenderer(renderer);


    vtkWidget->GetInteractor()->SetInteractorStyle(style);

    QWidget widget;

    QVBoxLayout* layout = new QVBoxLayout();

    layout->addWidget(vtkWidget);

    widget.setLayout(layout);

    widget.setMaximumSize(866,600);

    widget.show();

    return a.exec();


}



Thanks,

-- 
Abhishek
http://zeroth.me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20181030/dbf4b330/attachment.html>


More information about the vtkusers mailing list