[vtkusers] Filter update does not work.Easy mistake? (samplecode with cell normals)

vtklearny msjean at gmx.de
Fri Jun 12 08:17:32 EDT 2009


Hello there,

I just have started learning about vtk.
Please have a look at this code and tell me what I'm doing wrong here.
Thanks a lot 

vtkLearny


Objectives: 
1. load *.vtk //works
2. install normalFilter //computes normal data
3. change point data //works
4. filter update (recompute normals)// seems not to work
5. render //only original data is shown,not even points moved
6. enjoy the look in wireframe and surface mode  //far from it
-------------------------------------


-----------CODE----------------------

#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkInteractorStyleTrackballCamera.h"

#include "vtkSmartPointer.h"
#include  "vtkPolyDataNormals.h"
#include  "vtkAlgorithmOutput.h"
#include "vtkPolyDataReader.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkCellData.h"
#include "vtkPointData.h"

int main()
{
//######################################################################
// 1. --------------------------------------------------------	
	vtkSmartPointer<vtkPolyDataReader> reader =
vtkSmartPointer<vtkPolyDataReader>::New(); 
	vtkPolyData* polydata=reader->GetOutput();
	const char* filename=".../anyfile.vtk"; //mine was a cubeObject
	reader->SetFileName(filename);
	
	if(!reader->OpenVTKFile()){
		cout<<"\tERROR: Cannot read file "<<filename<<"!"<<endl;
		return NULL;
	}else 
		cout << "path " << filename << " found" << endl;
// 2. --------------------------------------------------------	
	vtkSmartPointer<vtkPolyDataNormals> tmpFilter =
vtkSmartPointer<vtkPolyDataNormals>::New();
	tmpFilter->SetInput(polydata);
//	tmpFilter->ComputeCellNormals();
	tmpFilter->ComputeCellNormalsOn();
	vtkPolyDataMapper *Mapper = vtkPolyDataMapper::New();
	 Mapper->ScalarVisibilityOff();
	 tmpFilter->Modified();
	 tmpFilter->Update();
	 Mapper->SetInput(tmpFilter->GetOutput());

	 cout << " manifold:" << tmpFilter->GetNonManifoldTraversal();

	 /*Mapper->SetInputConnection(tmpFilter->GetOutputPort());
	 Mapper->GetInputConnection(0,0)->GetProducer()->Print(cout);*/	

	 //Mapper->SetInput(polydata);
	 //Mapper->Modified();

// 3. --------------------------------------------------------	
	vtkPolyData* MappedPolyData=Mapper->GetInput();
	 vtkPoints* points=MappedPolyData->GetPoints();

	 cout << "points->GetNumberOfPoints():" << points->GetNumberOfPoints();

	 unsigned int numOfMovedPoints=points->GetNumberOfPoints();
	 for (unsigned int i=0;i<numOfMovedPoints;i++){
		double xyz[3]={1.0,1.0,1.0};
		double point[3];
		points->GetPoint(i,point);
	//	cout << "P[ "<< point[0]<<" , "<< point[1]<< " , "<<point[2] <<" ] => "
;
		
		//do some modification
		xyz[0]=point[0]+i*(1.0/numOfMovedPoints)*0.3;
		xyz[1]=point[1]-i*(1.0/numOfMovedPoints)*0.1;
		xyz[2]=point[2]+i*(1.0/numOfMovedPoints)*0.5;

		points->SetPoint(i,xyz);

		//controle
		points->GetPoint(i,point);
	//	cout << "P[ "<< point[0]<<" , "<< point[1]<< " , "<<point[2] <<" ] " <<
endl;
	 }
// 4. --------------------------------------------------------	

	 tmpFilter->Modified();
	 tmpFilter->Update();
	  Mapper->Modified();
	 //Mapper->Update();

	//int
numOfTuples=tmpFilter->GetOutput()->GetCellData()->GetNormals()->GetNumberOfTuples();
	//cout << "normals.num of Tupels(): "  << numOfTuples << endl;
	//vtkDataArray* normals;
	//normals=tmpFilter->GetOutput()->GetCellData()->GetNormals();
	/*for (int i=0;i < numOfTuples;i++)
    {
				
		if (numOfTuples>i&&numOfTuples>0&&(i%500==0)){
			double newTN[3]={1.0,1.0,1.0};
			double TN[3];
			cout << "cell "<<  i << ":" ;
			

			normals->GetTuple(i,TN);
			cout << "TN[ "
				<< TN[0] << " , "
				<< TN[1] << " , "
				<< TN[2] 		
			<< " ]" ;
			

			newTN[0]*=i/(numOfTuples*1.0);
			newTN[1]+=i/(numOfTuples*1.0);
			newTN[2]-=i/(numOfTuples*1.0);

			normals->SetTuple(i,newTN);
			normals->GetTuple(i,TN);

			cout << " ==> TN[ "
				<< TN[0] << " , "
				<< TN[1] << " , "
				<< TN[2]
			<< " ]" << endl;
		}
    
    }*/

	//tmpFilter->GetOutput()->GetCellData()->SetNormals(normals);			
	
	//no reaction
	/*tmpFilter->Modified();
	tmpFilter->Update();

	Mapper->Modified();	*/
	
//######################################################################
//####Std operations##########

  vtkActor *Actor = vtkActor::New();  
  Actor->SetMapper(Mapper);

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( Actor );
  ren1->SetBackground( .1, 0.2, 0.4 );


  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );


  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  vtkInteractorStyleTrackballCamera *style = 
    vtkInteractorStyleTrackballCamera::New();
  iren->SetInteractorStyle(style);  



  iren->Initialize();
  iren->Start();

  
//delete
  Mapper->Delete();
  Actor->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();
  style->Delete();

  return 0;
}
-------------

-- 
View this message in context: http://www.nabble.com/Filter-update-does-not-work.Easy-mistake---%28samplecode-with-cell-normals%29-tp23997785p23997785.html
Sent from the VTK - Users mailing list archive at Nabble.com.




More information about the vtkusers mailing list