changeset 38:cd3bd9ea53c5

imported patch debug
author Jordi Gutiérrez Hermoso <jordigh@gmail.com>
date Thu, 11 Mar 2010 23:55:38 -0600
parents e87cb53283b4
children 664d2f784d14
files src/include/vtkplot.hpp src/vtkplot.cpp
diffstat 2 files changed, 165 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/src/include/vtkplot.hpp
+++ b/src/include/vtkplot.hpp
@@ -1,3 +1,4 @@
+
 #ifndef __VTKPLOTTING_HPP__
 #define __VTKPLOTTING_HPP__
 
@@ -37,7 +38,7 @@
     ///Direction from which to plot
     vector view_direction;
    ///The plot data
-    vtkSmartPointer<vtkDelaunay2D> delaunay;
+    vtkSmartPointer<vtkPolyData> polydata;
     ///Whether to plot offscreen or not
     bool offscreen;
   };
--- a/src/vtkplot.cpp
+++ b/src/vtkplot.cpp
@@ -25,63 +25,113 @@
 #include <vtkWindowToImageFilter.h>
 #include <vtkPNGWriter.h>
 
-#include <string>
-using std::string;
 
-#include "include/vtkplot.hpp"
-#include "include/error.hpp"
+#include <vtkSmartPointer.h>
+#include <vtkObjectFactory.h>
+#include <vtkSphereSource.h>
+#include <vtkPolyDataMapper.h>
+#include <vtkActor.h>
+#include <vtkRenderWindow.h>
+#include <vtkRenderer.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkProgrammableFilter.h>
+#include <vtkCommand.h>
+#include <vtkCamera.h>
 
-namespace kwantix{
-  vtkplot::vtkplot(const matrix& data)
-  {
-    if(data.cols() != 3)
-    {
-      badArgument exc;
-      exc.reason = "vtkplot only works with N x 3 matrices.";
-      exc.line = __LINE__;
-      exc.file = __FILE__;
-      throw exc;
-    }
+#include <string>
+#include <sstream>
+#include <iomanip> 
+
+using namespace std;
+
+unsigned int GridSize = 50;
+
+
+double F(double x, double y, double t)
+{
+  return sin(y)*cos(x)*sin(t);
+}
 
-    //perform initial triangulation
-    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
-    vtkSmartPointer<vtkDoubleArray> scalars = vtkSmartPointer<vtkDoubleArray>::New();
+class CommandSubclass : public vtkCommand
+{
+public:
+  vtkTypeRevisionMacro(CommandSubclass, vtkCommand);
+  static CommandSubclass *New()
+  {
+    return new CommandSubclass;
+  }
+ 
+  void Execute(vtkObject *caller, unsigned long eventId, 
+               void *callData)
+  {
+    cout << "timer callback" << endl;
  
-    for(size_t i = 1; i < data.rows(); i++)
-    {
-      vtkIdType idx = points->InsertNextPoint(data(i,1), data(i,2), data(i,3) );
-      scalars -> InsertTuple(idx, &data(i,3) );
-    }
+    vtkRenderWindowInteractor *iren = 
+      static_cast<vtkRenderWindowInteractor*>(caller);
+ 
+    this->ProgrammableFilter->Modified();
+ 
+    iren->Render();
+ 
+  }
+ 
+  vtkSmartPointer<vtkProgrammableFilter> ProgrammableFilter;
+ 
+};
+
+vtkCxxRevisionMacro(CommandSubclass, "$Revision: 1.1 $");
  
-    //add the grid points to the polydata object
-    vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();
-    polydata->SetPoints(points);
-    polydata->GetPointData() -> SetScalars(scalars);
+void AdjustPoints(void* arguments)
+{
+  cout << "AdjustPoints" << endl;
+  vtkProgrammableFilter* programmableFilter = 
+    static_cast<vtkProgrammableFilter*>(arguments);
+ 
+  vtkPoints* inPts = programmableFilter->GetPolyDataInput()->GetPoints();
+  vtkIdType numPts = inPts->GetNumberOfPoints();
+  vtkSmartPointer<vtkPoints> newPts =
+    vtkSmartPointer<vtkPoints>::New();
+  newPts->SetNumberOfPoints(numPts);
  
-    //triangulate the grid points
-    vtkSmartPointer<vtkDelaunay2D> delaunay = vtkDelaunay2D::New();
-    delaunay->SetInput(polydata);
-    delaunay->Update();
-  }
+  vtkSmartPointer<vtkDoubleArray> newScalars 
+    = vtkSmartPointer<vtkDoubleArray>::New();
+
+  static double t = 0;
+
+  for(vtkIdType i = 0; i < numPts; i++)
+  {
+    double p[3];
+    inPts -> GetPoint(i,p);
+    p[2] = F(p[0],p[1],t);
+    newPts -> SetPoint(i,p);
+    newScalars -> InsertTuple(i, &p[2]);
+	}
+
+  t += 0.05;
+ 
+  programmableFilter->
+    GetPolyDataOutput()->CopyStructure(programmableFilter->GetPolyDataInput());
+
+  programmableFilter->GetPolyDataOutput()->SetPoints(newPts);
+  programmableFilter
+    ->GetPolyDataOutput()
+    ->GetPointData() -> SetScalars(newScalars);
 }
 
 // *************** move this stuff into a class ************************
 
-typedef double (*Func3d)(double, double);
+typedef double (*Func3d)(double, double, double);
 
 void InitOffscreen(bool offscreen);
 
-vtkSmartPointer<vtkDelaunay2D> 
-TriangulateTerrain(kwantix::matrix points);
+vtkSmartPointer<vtkDelaunay2D> TriangulateTerrain(Func3d func);
 
 void PlotPoints(vtkSmartPointer<vtkDelaunay2D> delaunay, 
-                bool offscreen=true,
-                const string& filename="vtkscreenshot.png");
+                bool offscreen);
 
-int run ()
+int main ()
 {
-  //	auto delaunay = TriangulateTerrain();	
-  //  PlotPoints(delaunay,true);
+  TriangulateTerrain(&F);	
 	return 0;
 }
 
@@ -89,59 +139,71 @@
 {
   // Graphics Factory
   vtkSmartPointer<vtkGraphicsFactory> graphics_factory
-    = vtkGraphicsFactory::New();
+    = vtkSmartPointer<vtkGraphicsFactory>::New();
   graphics_factory->SetOffScreenOnlyMode( offscreen );
   graphics_factory->SetUseMesaClasses( offscreen );
 
   // Imaging Factory
   vtkSmartPointer<vtkImagingFactory>  imaging_factory
-    = vtkImagingFactory::New();
+    = vtkSmartPointer<vtkImagingFactory>::New();
   imaging_factory->SetUseMesaClasses( offscreen );
 }
 
+double sign(double x)
+{
+  if(x > 0)
+    return 1;
+  else
+    return -1;
+}
+
 vtkSmartPointer<vtkDelaunay2D> TriangulateTerrain(Func3d func)
 {
 
 	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
   vtkSmartPointer<vtkDoubleArray> scalars = vtkSmartPointer<vtkDoubleArray>::New();
  
-	unsigned int GridSize = 50;
+
 	for(double x = -3; x < 3; x += 6.0/GridSize)
 	{
 		for(double y = -3; y < 3; y += 6.0/GridSize)
 		{
-      double z = func(x,y);
-			vtkIdType idx = points->InsertNextPoint(x, y, z);
+      double r = sign(y)*sqrt(9-y*y)*sqrt(9-x*x)/3;
+      double z = func(x,r,0);
+			vtkIdType idx = points->InsertNextPoint(x,r, z);
       scalars -> InsertTuple(idx, &z);
 		}
 	}
  
 	//add the grid points to a polydata object
-	vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();
+	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
 	polydata->SetPoints(points);
   polydata->GetPointData() -> SetScalars(scalars);
- 
+
 	//triangulate the grid points
-	vtkSmartPointer<vtkDelaunay2D> delaunay = vtkDelaunay2D::New();
+	vtkSmartPointer<vtkDelaunay2D> delaunay 
+    = vtkSmartPointer<vtkDelaunay2D>::New();
 	delaunay->SetInput(polydata);
 	delaunay->Update();
 
-  return delaunay;
-}
-
-void PlotPoints(vtkSmartPointer<vtkDelaunay2D> delaunay, 
-                bool offscreen, 
-                const string& filename)
-{
+  bool offscreen = false;
   InitOffscreen(offscreen);
 
+  vtkSmartPointer<vtkProgrammableFilter> programmableFilter = 
+    vtkSmartPointer<vtkProgrammableFilter>::New();
+  programmableFilter->SetInput(delaunay -> GetOutput());
+  programmableFilter->SetExecuteMethod(AdjustPoints, programmableFilter);
+
+
   //Normals for Gourad shading
-  vtkSmartPointer<vtkPolyDataNormals> normals = vtkPolyDataNormals::New();
-  normals -> SetInputConnection(delaunay ->GetOutputPort() );
+  vtkSmartPointer<vtkPolyDataNormals> normals 
+    =  vtkSmartPointer<vtkPolyDataNormals>::New();
+  normals -> SetInputConnection(programmableFilter -> GetOutputPort() );
   normals -> SetFeatureAngle(60);
 
   //Set the colours for the rendering
-  vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
+  vtkSmartPointer<vtkLookupTable> lut 
+    = vtkSmartPointer<vtkLookupTable>::New();
   lut -> SetHueRange(0.66667, 0.0);
   lut -> SetNumberOfColors(256);
   lut -> SetRampToLinear();
@@ -149,7 +211,8 @@
 
   
 	// map the contours to graphical primitives
-	vtkSmartPointer<vtkPolyDataMapper> contMapper = vtkPolyDataMapper::New();
+	vtkSmartPointer<vtkPolyDataMapper> contMapper 
+    = vtkSmartPointer<vtkPolyDataMapper>::New();
   contMapper->SetInput(normals -> GetOutput() );
   contMapper->SetScalarRange(0,1);
   contMapper->SetLookupTable(lut);
@@ -157,12 +220,15 @@
 
  
   // create an actor for the contours
-	vtkSmartPointer<vtkActor> contActor = vtkActor::New();
+	vtkSmartPointer<vtkActor> contActor 
+    = vtkSmartPointer<vtkActor>::New();
 	contActor->SetMapper(contMapper);
  
   // a renderer and render window
-	vtkSmartPointer<vtkRenderer> ren1 = vtkRenderer::New();
-	vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
+	vtkSmartPointer<vtkRenderer> ren1 =
+    vtkSmartPointer<vtkRenderer>::New();
+	vtkSmartPointer<vtkRenderWindow> renWin 
+    = vtkSmartPointer<vtkRenderWindow>::New();
   if(offscreen)
   {
     renWin->SetOffScreenRendering(1);
@@ -171,13 +237,23 @@
 
   // an interactor
   vtkSmartPointer<vtkRenderWindowInteractor> iren 
-    = vtkRenderWindowInteractor::New();
+    = vtkSmartPointer<vtkRenderWindowInteractor>::New();
   iren->SetRenderWindow(renWin);  
 
+  // Initialize must be called prior to creating timer events.
+  iren->Initialize();
+  iren->CreateRepeatingTimer(10);
+ 
+  vtkSmartPointer<CommandSubclass> timerCallback = 
+    vtkSmartPointer<CommandSubclass>::New();
+  timerCallback->ProgrammableFilter = programmableFilter;
+ 
+  iren->AddObserver ( vtkCommand::TimerEvent, timerCallback );
+
 	// add the actors to the scene
 	ren1->SetBackground(0.0,0.0,0.0);
   ren1->GetActiveCamera()->SetViewUp(0,0,1);
-  ren1->GetActiveCamera()->SetPosition(1, 1, 1);
+  ren1->GetActiveCamera()->SetPosition(1, 0, 1);
   ren1->GetActiveCamera()->SetParallelProjection(1);
 	ren1->AddActor(contActor);
   ren1->ResetCamera();
@@ -187,22 +263,34 @@
 
   if(offscreen)
   {
-    // Write to file
-    vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = 
-      vtkSmartPointer<vtkWindowToImageFilter>::New();
-    windowToImageFilter->SetInput(renWin);
-    windowToImageFilter->Update();
+    for(size_t i = 0; i < 1000; i++)
+    {
+      string filename = "foo";
+      stringstream ss; string i_str;
+      ss << setw(4) << setfill('0')  << i;
+      ss >> i_str;
+      filename += i_str + ".png";
+   
+      // Write to file
+      vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = 
+        vtkSmartPointer<vtkWindowToImageFilter>::New();
+      windowToImageFilter->SetInput(renWin);
+      windowToImageFilter->Update();
  
-    vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
-    writer->SetFileName(filename.c_str());
-    writer->SetInput(windowToImageFilter->GetOutput());
-    writer->Write();
+      vtkSmartPointer<vtkPNGWriter> writer 
+        = vtkSmartPointer<vtkPNGWriter>::New();
+      writer->SetFileName(filename.c_str() );
+      writer->SetInput(windowToImageFilter->GetOutput());
+      writer->Write();
+
+      programmableFilter -> Modified();
+    }
   }
   else
   {
     //The style
     vtkSmartPointer<vtkInteractorStyleTerrain> terrain_style 
-      = vtkInteractorStyleTerrain::New();
+      = vtkSmartPointer<vtkInteractorStyleTerrain>::New();
  
     // Set the style
     iren -> SetInteractorStyle(terrain_style);
@@ -210,4 +298,5 @@
     // begin mouse interaction
     iren->Start(); 
   }
+  return delaunay;
 }