[vtkusers] vtkChartXY slow render, rubber band selection, etc.. with multiple scatter plot

Giuseppe Marco Randazzo gmrandazzo at gmail.com
Fri Sep 14 05:29:03 EDT 2012


Hello,
i need a 2D Scatter Plot and i found an example of this here 
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Plotting/ScatterPlot

Subsequently it was necessary have a multiple scatter plot with in 
addition the possibility of change size, shape and color of each single 
point. It was hard understand how to do this on the previous example and 
i think is not possible to do this with a single vtkPlot*. So i've done 
some modification to the previous code and are attached here.  But now 
the problem is that the rendering of the plot is too slow for a lot of 
points....

maybe i'm wrong but is possible to change shape, color and size for each 
single point in a more simple way than mine?


Thanks

Giuseppe Marco Randazzo

  ------------------  ------------------  ------------------ 
------------------
//ScatterPlot2.cxx

#include <vtkVersion.h>
#include <vtkSmartPointer.h>

#include <vtkChartXY.h>
#include <vtkContextScene.h>
#include <vtkContextView.h>
#include <vtkFloatArray.h>
#include <vtkPlotPoints.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTable.h>
#include <vtkStringArray.h>
#include <vtkVariantArray.h>
#include <vtkDoubleArray.h>

#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
using namespace std;

template <typename T>
   string NumberToString ( T Number )
   {
      ostringstream ss;
      ss << Number;
      return ss.str();
   }

int main(int, char*[])
{
   // Set up a 2D scene, add an XY chart to it
   vtkSmartPointer<vtkContextView> view =
     vtkSmartPointer<vtkContextView>::New();
   view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
   view->GetRenderWindow()->SetSize(400, 300);

   vtkSmartPointer<vtkChartXY> chart =
     vtkSmartPointer<vtkChartXY>::New();
   view->GetScene()->AddItem(chart);
   chart->SetShowLegend(false);

   std::vector< vtkSmartPointer<vtkTable> > tablist;
   std::vector< vtkPlot* > pointlist;

   int numPoints = 10000;
   float inc = 7.5 / (numPoints-1);
   for (int i = 0; i < numPoints; ++i){
   // Create a table with some points in it...
     vtkSmartPointer<vtkTable> table =
       vtkSmartPointer<vtkTable>::New();

     for(int j = 0; j < 4; j++){
     vtkSmartPointer<vtkFloatArray> arrX =
       vtkSmartPointer<vtkFloatArray>::New();
       string name = "Axis";
       name += NumberToString(j+1);
     arrX->SetName(name.c_str());
     table->AddColumn(arrX);
     }

   // Test charting with a few more points...

     table->SetNumberOfRows(1);

     table->SetValue(0, 0, i * inc);
     table->SetValue(0, 1, cos(i * inc) + 0.0);
     table->SetValue(0, 2, sin(i * inc) + 0.0);
     table->SetValue(0, 3, sin(i * inc) - cos(i * inc));

     tablist.push_back(table);
   }

   for (int i = 0; i < numPoints; ++i){
   // Add multiple scatter plots, setting the colors etc
     vtkPlot *points = chart->AddPlot(vtkChart::POINTS);
     points->SetInput(tablist[i], 0, 1);
     points->SetColor(0, 0, 0, 255);
     points->SetWidth(1.0);
vtkPlotPoints::SafeDownCast(points)->SetMarkerStyle(vtkPlotPoints::CIRCLE);
     pointlist.push_back(points);
   }


   int b = 1;
   for(int i = 0; i < numPoints; ++i){
     if(b == 4){
       b = 1;
     }

     pointlist[i]->SetInput(tablist[i], 0, b);
     pointlist[i]->SetColor(0, 0, 0, 255);
     pointlist[i]->SetWidth(4.0);

     if(b == 1){
vtkPlotPoints::SafeDownCast(pointlist[i])->SetMarkerStyle(vtkPlotPoints::PLUS);
     }
     else if(b == 2){
vtkPlotPoints::SafeDownCast(pointlist[i])->SetMarkerStyle(vtkPlotPoints::SQUARE);
     }
     else{
vtkPlotPoints::SafeDownCast(pointlist[i])->SetMarkerStyle(vtkPlotPoints::CIRCLE);
     }

     pointlist[i]->Modified();
     pointlist[i]->Update();
     b++;
   }

   //Finally render the scene
   view->GetRenderWindow()->SetMultiSamples(0);
   view->GetInteractor()->Initialize();
   view->GetInteractor()->Start();

   return EXIT_SUCCESS;
}

  ------------------  ------------------  ------------------ 
------------------
#CMakeLists.txt
PROJECT(ScatterPlot)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall -pedantic -ggdb 
-Wno-long-long")

ADD_EXECUTABLE(ScatterPlot ScatterPlot.cxx)
TARGET_LINK_LIBRARIES(ScatterPlot vtkHybrid vtkCharts)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120914/3acc7027/attachment.htm>


More information about the vtkusers mailing list