[vtkusers] adding cells to existing Polydata

Tasnim hanene-jeder at hotmail.fr
Mon Sep 10 11:43:06 EDT 2012


Sorry, i have a long code, but i will put it, in order that you can help me,
i hope, and thanks in advance:

#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCell.h>
#include <vtkCellArray.h>
#include <vtkIdList.h>
#include <vtkPointData.h>
#include <string>
#include <vtkPLYReader.h>
#include <vtkRendererCollection.h>
#include <vtkDataSetMapper.h>
#include <vtkIdTypeArray.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyData.h>
#include <vtkCellPicker.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkProperty.h>
#include <vtkSelectionNode.h>
#include <vtkSelection.h>
#include <vtkExtractSelection.h>
#include <vtkObjectFactory.h>
#include <vtkGenericCell.h>
#include <vtkButterflySubdivisionFilter.h>
#include <vtkSphereSource.h>
#include <vtkPointPicker.h>
#include <vector>
#include <vtkLoopSubdivisionFilter.h>
#include <vtkButterflySubdivisionFilter.h>
#include <vtkInterpolatingSubdivisionFilter.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <lexical_cast.hpp>
using boost::lexical_cast;
using namespace std;

class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
  public:
  static MouseInteractorStyle* New();
  vtkSmartPointer<vtkPLYReader> reader ;
  std::string fichier;
  vtkSmartPointer<vtkRenderer> renderer;

  MouseInteractorStyle()
  {
    selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    selectedActor = vtkSmartPointer<vtkActor>::New();
	vtkSmartPointer<vtkUnstructuredGrid> selected =
          vtkSmartPointer<vtkUnstructuredGrid>::New();
	selected = vtkSmartPointer<vtkUnstructuredGrid>::New();
	selectedActor->SetMapper(selectedMapper);
  /*    selectedActor->GetProperty()->EdgeVisibilityOn();
       selectedActor->GetProperty()->SetEdgeColor(0,0,0);
      selectedActor->GetProperty()->SetLineWidth(3);
         */
		
  }
  

 

 

    virtual void OnLeftButtonDown()
    {


			vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New(); 
			polydata=reader->GetOutput();
//here i have a function that will devide my mesh to 8 regions ( octree
nodes), it  is so long, so i prefer to not put it

int* pos = this->GetInteractor()->GetEventPosition();
 
	  vtkSmartPointer<vtkPointPicker> picker =
        vtkSmartPointer<vtkPointPicker>::New();
     

      // Pick from this location.
      picker->Pick(pos[0], pos[1], pos[2], this->GetDefaultRenderer());
 
      double* worldPosition = picker->GetPickPosition();
	  vtkIdType pointId=picker->GetPointId();

	  if(pointId!= -1)
	  {
int  region=PointsNode->GetValue(pointId);
int nbIdr=0;
vector<vtkIdType>ids(1,1);
for (int i=1; i<polydata->GetNumberOfPoints(); i++)
{
	if(PointsNode->GetValue(i)==region)
		{
			ids.push_back(i);
	nbIdr++;	
	}
	
}
cout<<"the number of vertex in this  region is : "<<nbIdr;
int nbpp=polydata->GetNumberOfPoints();
/***************************  open the file of corresponding point's region  
*************************/
strstream ss;    
 ss << "codde" << region << ".txt";    
 std::string filess = ss.str();    
 ifstream fichierss(filess,ios::in); // open the file : contains the point's
coordinates
 int nbrligne=0;  
 std::string contenu; 
  
int nbrligne;  //number of vertex (points) in the file  

 int  nbrcor;   
nbrcor=nbrligne;  
int dim_allouee = 0;   
double** sommet=0;    
sommet=new double * [nbrcor];    // 2D vector which will contains the
point's coordinates
//std::fill_n( sommet, nbrcor, static_cast<double*>( 0 ) );    
for ( dim_allouee = 0; dim_allouee < nbrcor; ++dim_allouee)    
 {    
 sommet[ dim_allouee ] = new double[ 3 ];    
  }    
ifstream fichiersopen(filess,ios::in); // open file 
int w=0;  
for(w=0;w<nbrcor;w++)  
{  
while( getline(fichiersopen, contenu))  
{  
int x=0;  
istringstream t(contenu);    
string mot;    
while ( std::getline( t, mot, ' ' ) )    
  {    double z;
z=10 *lexical_cast&lt;double>(mot); //get the coordinates
      sommet[w][x] =z;
 x=x+1;  
   } 
double* so=sommet[w];
int nbp=polydata->GetNumberOfPoints();
int nbp1=nbp+1;
polydata->GetPoints()->InsertPoint(nbp1,so);// add point to the existed
polydata
//polydata->GetPoints()->InsertNextPoint(so);
polydata->GetPoints()->Modified();
polydata->Modified();
polydata->Update();

}  
}  
 fichiersopen.close();// close the file
 /**********************************open the file of faces 
*************************/
 vtkIdList* points = vtkIdList::New(); 
strstream sface; 
  sface << "face" << region << ".txt";  
  std::string face = sface.str();   
 ifstream fichierface(face,ios::in); // ouverture du fichier  
 int nbrligneface=0; 
 std::string contenuface; 
while(getline(fichierface, contenuface))    
   {    

	   istringstream t(contenuface); 
	   string mot;    
while ( std::getline( t, face, ' ' ) )   
  {    
	  int z;

istringstream ( face )>>z;
 int zz=z+nbpp;
	   points->InsertNextId(zz); 
   } 
int cell=polydata->GetPolys()->GetNumberOfCells();
polydata->GetPolys()->InsertNextCell(points);
polydata->BuildCells();
polydata->BuildLinks();
polydata->GetPolys()->Modified();
int cells=cell+1;
polydata->GetPolys()->UpdateCellCount(cells);
nbrligneface++;  
 }    
fichierface.close(); // close the file

};


       
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
 
        
      // Forward events
      vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }
 
    vtkSmartPointer<vtkPolyData> Data;
    vtkSmartPointer<vtkDataSetMapper> selectedMapper;
    vtkSmartPointer<vtkActor> selectedActor;
	  vtkSmartPointer<vtkUnstructuredGrid> selected;
 
};
 
vtkStandardNewMacro(MouseInteractorStyle);
 

int main(int argc, char *argv[])
{
  if(argc != 2)
    {
    std::cout << "Usage: " << argv[0] << " Filename(.ply)" << std::endl;
    return EXIT_FAILURE;
    }
 
  std::string filename = argv[1];
  vtkSmartPointer<vtkPLYReader> reader =
    vtkSmartPointer<vtkPLYReader>::New();
  reader->SetFileName(filename.c_str());
  reader->Update();
  
  
  vtkSmartPointer<vtkTriangleFilter> triangleFilter =
    vtkSmartPointer<vtkTriangleFilter>::New();
  triangleFilter->SetInputConnection(reader->GetOutputPort());
  triangleFilter->Update();
 
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(triangleFilter->GetOutputPort());
 
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
 
  
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
      renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =   
vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();
   vtkSmartPointer<MouseInteractorStyle> style =
    vtkSmartPointer<MouseInteractorStyle>::New();
  style->SetDefaultRenderer(renderer);
  style->reader=reader; 
  style->renderer=renderer;
  renderWindowInteractor->SetInteractorStyle(style);
 
  renderer->AddActor(actor);
  renderer->ResetCamera();  
  renderWindow->Render();
  renderWindowInteractor->Start();
  return EXIT_SUCCESS;
}

I know that my code is soooooooooooo long, but in order that you can
understand what i'm going to, i put it as it is.
Thanks in advance.
Regards,
Tasnim







--
View this message in context: http://vtk.1045678.n5.nabble.com/adding-cells-to-existing-Polydata-tp5715971p5715980.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list