VTK custom 8 point drawing cube

Click to open the link http://blog.163.com/god_sun/blog/static/56399926201110108481537/


java code:

import vtk.vtkActor;
import vtk.vtkCamera;
import vtk.vtkCellArray;
import vtk.vtkExtractEdges;
import vtk.vtkFloatArray;
import vtk.vtkInteractorStyleTrackballActor;
import vtk.vtkPoints;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.* ;  

//Then we define our class.  
public class Lifangti {
	
	  static {
	        if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
	          for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
	            if (!lib.IsLoaded()) {
	              System.out.println(lib.GetLibraryName() + " not loaded");
	            }
	          }
	        }
	        vtkNativeLibrary.DisableOutputWindow(null);
	      }

	  
	  
	  
	  public static void makeLifangti(){
		  int i ;
		  //You can see that there are 27 small cubes in the figure to form a large cube
		  //x[8][3], is the x, y, z value of the 8 points of each small cube
		  //java float type needs to be followed by f to represent float type data
		  //float[][] x= {{0,0,0},{0.3f,0,0},{0.3f,0.3f,0},{0,0.3f,0}, {0,0,0.3f},{0.3f,0,0.3f},{0.3f,0.3f,0.3f},{0,0.3f,0.3f}};
		  
		  double[][]  x={{0,0,0},{0.3,0,0},{0.3,0.3,0},{0,0.3,0},
			       {0,0,0.3},{0.3,0,0.3},{0.3,0.3,0.3},{0,0.3,0.3}};
		  //This review of the VTK documentation found that the explanation was different from what I understood. . . Most likely I don't understand it. . .
		  //My understanding is this, a cube is composed of 6 faces, and our program is also planning to use the cell with the smallest face position to form the cube we want
		  //Of course, we can also use lines to draw, so we can only draw the border of the cube, we can't use the PolyData used below, but use the vtkLine object as the CELL
		  //Since it is selected to be composed of faces, six faces are generated. The first face is composed of four points 0, 1, 2, and 3 of the 8 points defined above, and so on.
		  //Specially note that the points 0, 1, 2, and 3 here are not the corresponding x[8][3]. The specific reasons for the 0, 1, 2, and 3 here will be explained below.
		  //Each number represents the index subscript of the point data
		  int [] [] pts = {{0,1,2,3}, {4,5,6,7}, {0,1,5,4}, {1,2,6,5}, {2 , 3,7,6}, {3,0,4,7}};
		  //concrete dataset represents vertices, lines, polygons, and triangle strips, this is the description in the document, it is more clear.
		  vtkPolyData  cube =new vtkPolyData();
		  //This is used to save what we call the topology between the points, because according to the VTK process, we have to do this and pack it step by step.
		  vtkCellArray  polys =new vtkCellArray();
		  //This is about the attribute value of the color, so I won't focus on it here.
		  vtkFloatArray  scalars =new vtkFloatArray();
		  //It's still the same reason, because we use the points defined by ourselves, not the vtksphere, vtkcone and other sources in VTK, so we must put the points in vtkPoints
		  vtkPoints  points =new vtkPoints();
		  //Put our defined points into points, where i is the real index, if our loop starts from i=8
		  //The index of the real point, and the reference in the cell, will be consistent with this index
		  for (i=0;i<8;i++) points.InsertPoint(i, x[i]);
		  // Topology of the insertion point,
		 //November 10, 2011 - god_sun - Sui Feng¤ Xinhui

		  //Although the first parameter is of type vtkIdType, it is actually of type int. The explanation in the document is that the first parameter is the number of points, and the second is the content of the point.
		 //The setting unit consists of several points, and each unit specifies the corresponding point
		  for (i=0;i<6;i++)
		  {
			   polys.InsertNextCell(4);
			   for(int j=0;j<4;j++){
				   polys.InsertCellPoint(pts[i][j]);
			   }
		  }
		  for (i=0;i<8;i++) scalars.InsertTuple1(i,i);
		  //Then the pipeline process of VTK starts.
		  cube.SetPoints(points);
		  cube.SetPolys(polys);
		         cube.GetPointData().SetScalars(scalars);

		  //// This code is to show the border around the cube. . . But setting visibilityOn or visibilityOff doesn't seem very obvious.
		  //When I used to draw a cube in photoshop, I would draw a border around it, which would make it feel fuller. . .
		  //Use SetInputData(cube) after vtk5; the previous format is SetInput(cube)
		  vtkExtractEdges  extract=new vtkExtractEdges();
		         extract.SetInputData(cube);
		  vtkPolyDataMapper  mapEdges=new vtkPolyDataMapper();
		         mapEdges.SetInputConnection(extract.GetOutputPort());
		   mapEdges.SetScalarVisibility(0);
		  vtkActor  edgeActor=new vtkActor();
		         edgeActor.SetMapper(mapEdges);
		   edgeActor.VisibilityOn();
		  

		  vtkPolyDataMapper  cubeMapper =new  vtkPolyDataMapper();
		  cubeMapper.SetInputData(cube);
		  cubeMapper.SetScalarRange(0,7);
		  int No=0;
		  //p,j,k are to set the different positions of the cube.
		  float p=0.0f,j=0.0f,k=0.0f;
		  vtkRenderer  renderer =new  vtkRenderer();
		  for (p=0.0f;p<0.9;p=p+0.3f)
		  {
		   for (j=0.0f;j<0.9;j=j+0.3f)
		   {
		    for(k=0.0f;k<0.9;k=k+0.3f)
		    {
		     vtkActor  cubeActor =new vtkActor();
		     //Set different positions of ACTOR to display the final graph.
		     cubeActor.SetPosition(p,j,k);
		     vtkActor tempactor = new vtkActor ();
		     cubeActor.SetMapper(cubeMapper);
		     renderer.AddActor(cubeActor);
		    }
		   }
		  }
		  
		  vtkCamera  camera =new vtkCamera();
		  camera.SetPosition(1,1,1);
		  camera.SetFocalPoint(0,0,0);
		  
		  
		  vtkRenderWindow  reWin = new vtkRenderWindow();
		  reWin.AddRenderer(renderer);

		  vtkRenderWindowInteractor  iren =new  vtkRenderWindowInteractor();
		  iren.SetRenderWindow (reWin);
		  //If this style is removed, the entire cube will rotate together when interacting
		  //If added, each cube rotates independently
		  vtkInteractorStyleTrackballActor  style =new  vtkInteractorStyleTrackballActor();
		  iren.SetInteractorStyle(style);
		   
		  renderer.SetActiveCamera(camera);
		   renderer.ResetCamera();
		   renderer.SetBackground(0,1,1);

		  reWin.SetSize(300,300);

		  reWin.Render();
		  iren.Initialize();
		  iren.Start();

	  }
	  
	  
	  public static void main(String[] args) {
			// TODO Auto-generated method stub
		  makeLifangti();
		}
 
 
}  


The c++ code is as follows:

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkExtractEdges.h>
typedef vtkActorCollection* vtkActorCollection_Array;
intmain()
{
 int i ;
 //You can see that there are 27 small cubes in the figure to form a large cube
       //x[8][3], is the x, y, z value of the 8 points of each small cube
 float x[8][3]={{0,0,0},{0.3,0,0},{0.3,0.3,0},{0,0.3,0},
       {0,0,0.3},{0.3,0,0.3},{0.3,0.3,0.3},{0,0.3,0.3}};
 //This review of the VTK documentation found that the explanation was different from what I understood. . . Most likely I don't understand it. . .
 //My understanding is this, a cube is composed of 6 faces, and our program is also planning to use the cell with the smallest face position to form the cube we want
 //Of course, we can also use lines to draw, so we can only draw the border of the cube, we can't use the PolyData used below, but use the vtkLine object as the CELL
 //Since it is selected to be composed of faces, six faces are generated. The first face is composed of four points 0, 1, 2, and 3 of the 8 points defined above, and so on.
 //Specially note that the points 0, 1, 2, and 3 here are not the corresponding x[8][3]. The specific reasons for the 0, 1, 2, and 3 here will be explained below.
 vtkIdType pts[6][4]={{0,1,2,3},{4,5,6,7},{0,1,5,4},
        {1,2,6,5},{2,3,7,6},{3,0,4,7}};
 //concrete dataset represents vertices, lines, polygons, and triangle strips, this is the description in the document, it is more clear.
 vtkPolyData *cube = vtkPolyData::New();
 //This is used to save what we call the topology between the points, because according to the VTK process, we have to do this and pack it step by step.
 vtkCellArray *polys = vtkCellArray::New();
 //This is about the attribute value of the color, so I won't focus on it here.
 vtkFloatArray *scalars = vtkFloatArray::New();
 //It's still the same reason, because we use the points defined by ourselves, not the vtksphere, vtkcone and other sources in VTK, so we must put the points in vtkPoints
 vtkPoints *points = vtkPoints::New();
 //Put our defined points into points, where i is the real index, if our loop starts from i=8
 //The index of the real point, and the reference in the cell, will be consistent with this index
 for (i=0;i<8;i++)points->InsertPoint(i,x[i]);
 // Topology of the insertion point,
November 10, 2011 - god_sun - Sui Feng¤ Xinhui

 //Although the first parameter is of type vtkIdType, it is actually of type int. The explanation in the document is that the first parameter is the number of points, and the second is the content of the point.
 for (i=0;i<6;i++) polys->InsertNextCell(4,pts[i]);
 for (i=0;i<8;i++) scalars->InsertTuple1(i,i);
 //Then the pipeline process of VTK starts.
 cube->SetPoints(points);
 cube->SetPolys(polys);
        cube->GetPointData()->SetScalars(scalars);

 //// This code is to show the border around the cube. . . But setting visibilityOn or visibilityOff doesn't seem very obvious.
 //When I used to draw a cube in photoshop, I would draw a border around it, which would make it feel fuller. . .
 vtkExtractEdges *extract=vtkExtractEdges::New();
        extract->SetInput(cube);
 vtkPolyDataMapper *mapEdges=vtkPolyDataMapper::New();
        mapEdges->SetInputConnection(extract->GetOutputPort());
  mapEdges->SetScalarVisibility(0);
 vtkActor *edgeActor=vtkActor::New();
        edgeActor->SetMapper(mapEdges);
  edgeActor->VisibilityOn();
 

 vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
 cubeMapper->SetInput(cube);
 cubeMapper->SetScalarRange(0,7);
 int No=0;
 //p,j,k are to set the different positions of the cube.
 float p=0.0,j=0.0,k=0.0;
 vtkRenderer *renderer = vtkRenderer::New();
 for (p=0.0;p<0.9;p=p+0.3)
 {
  for (j=0.0;j<0.9;j=j+0.3)
  {
   for(k=0.0;k<0.9;k=k+0.3)
   {
    vtkActor *cubeActor = vtkActor::New();
    //Set different positions of ACTOR to display the final graph.
    cubeActor->SetPosition(p,j,k);
    vtkActor * tempactor = vtkActor :: New ();
    cubeActor->SetMapper(cubeMapper);
    renderer->AddActor(cubeActor);
   }
  }
 }
 
 vtkCamera *camera =vtkCamera::New();
 camera->SetPosition(1,1,1);
 camera->SetFocalPoint(0,0,0);
 
 
 vtkRenderWindow *reWin = vtkRenderWindow::New();
 reWin->AddRenderer(renderer);

 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
 iren-> SetRenderWindow (reWin);
 //If this style is removed, the entire cube will rotate together when interacting
 //If added, each cube rotates independently
 vtkInteractorStyleTrackballActor *style = vtkInteractorStyleTrackballActor::New();
 iren->SetInteractorStyle(style);
  
 renderer->SetActiveCamera(camera);
  renderer->ResetCamera();
  renderer->SetBackground(0,1,1);

 reWin->SetSize(300,300);

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

 points->Delete();
 cube->Delete();
 cubeMapper->Delete();
 renderer->Delete();
 reWin->Delete();
 iren->Delete();
 polys->Delete();
  scalars->Delete();
 return 0;
}

How to use the points defined by yourself to draw a picture
Insert your own defined points into vtkpoints
This is how the defined topology is inserted into the vtkcellarray.

 
 

The idea of ​​drawing the above picture:

8 points define the 8 vertices of a cube;

Every 4 points form a face, a total of 6 faces;

8 points are inserted into VTKpoints;

6 faces are inserted into the VTKcellarray in the topology;

Assemble a cube, and then translate the cube 9 times to form a final large cube;

Each small cube can be rotated independently;




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324664838&siteId=291194637