/*========================================================================= Program: Visualization Toolkit Module: $RCSfile: vtk3DGridSource.cxx,v $ Language: C++ Date: $Date: 2002/04/18 12:15:58 $ Version: $Revision: 1.3 $ Made by Rasmus Paulsen email: rrp@imm.dtu.dk web: www.imm.dtu.dk/~rrp/VTK This class is not mature enough to enter the official VTK release. =========================================================================*/ #include "vtk3DGridSource.h" #include "vtkPoints.h" #include "vtkObjectFactory.h" vtkCxxRevisionMacro(vtk3DGridSource, "$Revision: 1.3 $"); vtkStandardNewMacro(vtk3DGridSource); //---------------------------------------------------------------------------- vtk3DGridSource::vtk3DGridSource(float xL, float yL, float zL) { this->XLength = fabs(xL); this->YLength = fabs(yL); this->ZLength = fabs(zL); this->Center[0] = 0.0; this->Center[1] = 0.0; this->Center[2] = 0.0; this->XCubes = 10; this->YCubes = 10; this->ZCubes = 10; } void vtk3DGridSource::Execute() { vtkPolyData *output = this->GetOutput(); vtkDebugMacro(<<"Creating 3D grid"); vtkPoints *pts = vtkPoints::New(); vtkCellArray *lines = vtkCellArray::New(); // number of verts in grid const int ntx = this->XCubes+1; const int nty = this->YCubes+1; const int ntz = this->ZCubes+1; const int nx2 = this->XCubes/2; const int ny2 = this->YCubes/2; const int nz2 = this->ZCubes/2; // side length of cube const double sx = (double)this->XLength/(double)this->XCubes; const double sy = (double)this->YLength/(double)this->YCubes; const double sz = (double)this->ZLength/(double)this->ZCubes; pts->SetNumberOfPoints(ntx*nty*ntz); for (int tx = -nx2; tx <= nx2; tx++) { for (int ty = -ny2; ty <= ny2; ty++) { for (int tz = -nz2; tz <= nz2; tz++) { // pointID of center point int pIDc = (tx+nx2) + (ty+ny2) * ntx + (tz+nz2) * ntx * nty; // start of cube double x = tx * sx + this->Center[0]; double y = ty * sy + this->Center[1]; double z = tz * sz + this->Center[2]; pts->SetPoint(pIDc, x, y, z); if (tx != nx2) { int pID2 = (tx+nx2+1) + (ty+ny2) * ntx + (tz+nz2) * ntx * nty; pts->SetPoint(pID2, x+sx, y, z); lines->InsertNextCell(2); lines->InsertCellPoint(pIDc); lines->InsertCellPoint(pID2); } if (ty != ny2) { int pID2 = (tx+nx2) + (ty+1+ny2) * ntx + (tz+nz2) * ntx * nty; pts->SetPoint(pID2, x, y+sy, z); lines->InsertNextCell(2); lines->InsertCellPoint(pIDc); lines->InsertCellPoint(pID2); } if (tz != nz2) { int pID2 = (tx+nx2) + (ty+ny2) * ntx + (tz+1+nz2) * ntx * nty; pts->SetPoint(pID2, x, y, z+1); lines->InsertNextCell(2); lines->InsertCellPoint(pIDc); lines->InsertCellPoint(pID2); } } } } output->SetPoints(pts); pts->Delete(); output->SetLines(lines); lines->Delete(); } void vtk3DGridSource::PrintSelf(ostream& os, vtkIndent indent) { vtkPolyDataSource::PrintSelf(os,indent); os << indent << "X Length: " << this->XLength << "\n"; os << indent << "Y Length: " << this->YLength << "\n"; os << indent << "Z Length: " << this->ZLength << "\n"; os << indent << "Center: (" << this->Center[0] << ", " << this->Center[1] << ", " << this->Center[2] << ")\n"; os << indent << "X cubes: " << this->XCubes << "\n"; os << indent << "Y cubes: " << this->YCubes << "\n"; os << indent << "Z cubes: " << this->ZCubes << "\n"; }