1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCirclePackToPolyData.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkCirclePackToPolyData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkTimerLog.h"
#include "vtkTree.h"
#include "vtkStripper.h"
#include "vtkSectorSource.h"
#include "vtkAppendPolyData.h"
#include "vtkSmartPointer.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
vtkStandardNewMacro(vtkCirclePackToPolyData);
vtkCirclePackToPolyData::vtkCirclePackToPolyData()
{
this->SetCirclesArrayName("circles");
this->Resolution = 100;
}
vtkCirclePackToPolyData::~vtkCirclePackToPolyData()
{
}
int vtkCirclePackToPolyData::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTree");
return 1;
}
int vtkCirclePackToPolyData::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkTree *inputTree = vtkTree::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *outputPoly = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if( inputTree->GetNumberOfVertices() == 0 )
{
return 1;
}
vtkDataArray* circlesArray = this->GetInputArrayToProcess(0, inputTree);
if (!circlesArray)
{
vtkErrorMacro("Circles array not found.");
return 0;
}
double progress = 0.0;
this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
VTK_CREATE(vtkAppendPolyData, appendFilter);
for( int i = 0; i < inputTree->GetNumberOfVertices(); i++ )
{
// Grab coords from the input
double circle[3];
circlesArray->GetTuple(i,circle);
VTK_CREATE(vtkPolyData, circlePData);
this->CreateCircle(circle[0],
circle[1],
0.0,
circle[2],
this->Resolution,
circlePData);
appendFilter->AddInputData(circlePData);
if ( i%1000 == 0 )
{
progress = static_cast<double>(i) / inputTree->GetNumberOfVertices() * 0.8;
this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
}
}
appendFilter->Update();
outputPoly->ShallowCopy(appendFilter->GetOutput());
// Pass the input vertex data to the output cell data :)
vtkDataSetAttributes* const input_vertex_data = inputTree->GetVertexData();
vtkDataSetAttributes* const output_cell_data = outputPoly->GetCellData();
output_cell_data->PassData(input_vertex_data);
return 1;
}
void vtkCirclePackToPolyData::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Resolution: " << this->Resolution << endl;
}
void vtkCirclePackToPolyData::CreateCircle(const double& x,
const double& y,
const double& z,
const double& radius,
const int& resolution,
vtkPolyData* polyData)
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
points->SetNumberOfPoints( resolution );
cells->Allocate( 1, resolution );
cells->InsertNextCell( resolution );
for( int i = 0 ; i < resolution; ++i )
{
double theta = vtkMath::RadiansFromDegrees(360.*i/double(resolution));
double xp = x + radius*cos(theta);
double yp = y + radius*sin(theta);
points->SetPoint( i, xp, yp, z );
cells->InsertCellPoint( i );
}
polyData->Initialize();
polyData->SetPolys( cells );
polyData->SetPoints( points );
}
|