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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPOutlineFilterInternals.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.
=========================================================================*/
#include "vtkPOutlineFilterInternals.h"
#include "vtkAMRInformation.h"
#include "vtkAppendPolyData.h"
#include "vtkBoundingBox.h"
#include "vtkCompositeDataIterator.h"
#include "vtkDataObjectTree.h"
#include "vtkGraph.h"
#include "vtkMath.h"
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkOutlineCornerSource.h"
#include "vtkOutlineSource.h"
#include "vtkOverlappingAMR.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include "vtkUniformGrid.h"
// ----------------------------------------------------------------------------
class AddBoundsListOperator : public vtkCommunicator::Operation
{
// Description:
// Performs a "B.AddBounds(A)" operation.
void Function(const void *A, void *B, vtkIdType length, int datatype) VTK_OVERRIDE
{
(void)datatype;
assert((datatype == VTK_DOUBLE) && (length%6==0));
assert("pre: A vector is NULL" && (A != NULL) );
assert("pre: B vector is NULL" && (B != NULL) );
vtkBoundingBox box;
const double *aPtr = reinterpret_cast<const double*>(A);
double *bPtr = reinterpret_cast<double*>(B);
for(vtkIdType idx=0; idx < length; idx+=6 )
{
box.SetBounds(&bPtr[ idx ]);
box.AddBounds(&aPtr[ idx ]);
box.GetBounds(&bPtr[ idx ]);
}
}
// Description:
// Sets Commutative to true for this operation
int Commutative() VTK_OVERRIDE { return 1; }
};
// ----------------------------------------------------------------------------
vtkPOutlineFilterInternals::vtkPOutlineFilterInternals()
{
this->Controller = 0;
this->IsCornerSource = false;
this->CornerFactor = 0.2;
}
// ----------------------------------------------------------------------------
vtkPOutlineFilterInternals::~vtkPOutlineFilterInternals()
{
this->Controller = 0;
}
// ----------------------------------------------------------------------------
void vtkPOutlineFilterInternals::SetController(vtkMultiProcessController* controller)
{
this->Controller = controller;
}
// ----------------------------------------------------------------------------
void vtkPOutlineFilterInternals::SetCornerFactor(double cornerFactor)
{
this->CornerFactor = cornerFactor;
}
// ----------------------------------------------------------------------------
void vtkPOutlineFilterInternals::SetIsCornerSource(bool value)
{
this->IsCornerSource = value;
}
// ----------------------------------------------------------------------------
int vtkPOutlineFilterInternals::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkDataObject* input = vtkDataObject::GetData(inputVector[0], 0);
vtkPolyData* output = vtkPolyData::GetData(outputVector, 0);
if (input == NULL || output == NULL)
{
vtkGenericWarningMacro("Missing input or output.");
return 0;
}
if (this->Controller == NULL)
{
vtkGenericWarningMacro("Missing Controller.");
return 0;
}
vtkOverlappingAMR* oamr = vtkOverlappingAMR::SafeDownCast(input);
if (oamr)
{
return this->RequestData(oamr, output);
}
vtkUniformGridAMR* amr = vtkUniformGridAMR::SafeDownCast(input);
if (amr)
{
return this->RequestData(amr, output);
}
vtkDataObjectTree* cd = vtkDataObjectTree::SafeDownCast(input);
if (cd)
{
return this->RequestData(cd, output);
}
vtkDataSet* ds = vtkDataSet::SafeDownCast(input);
if (ds)
{
return this->RequestData(ds, output);
}
vtkGraph *graph = vtkGraph::SafeDownCast(input);
if (graph)
{
return this->RequestData(graph, output);
}
return 0;
}
// ----------------------------------------------------------------------------
void vtkPOutlineFilterInternals::CollectCompositeBounds(vtkDataObject* input)
{
vtkDataSet* ds = vtkDataSet::SafeDownCast(input);
vtkCompositeDataSet* compInput = vtkCompositeDataSet::SafeDownCast(input);
if (ds != NULL)
{
double bounds[6];
ds->GetBounds(bounds);
this->BoundsList.push_back(vtkBoundingBox(bounds));
}
else if (compInput != NULL)
{
vtkCompositeDataIterator* iter = compInput->NewIterator();
iter->SkipEmptyNodesOff();
iter->GoToFirstItem();
for (; !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
this->CollectCompositeBounds(iter->GetCurrentDataObject());
}
iter->Delete();
}
else
{
double bounds[6];
vtkMath::UninitializeBounds(bounds);
this->BoundsList.push_back(bounds);
}
}
// ----------------------------------------------------------------------------
int vtkPOutlineFilterInternals::RequestData(
vtkDataObjectTree* input, vtkPolyData* output)
{
// Check Output and Input
// Collect local bounds.
this->CollectCompositeBounds(input);
// Make an array of bounds from collected bounds
std::vector<double> boundsList;
boundsList.resize(6*this->BoundsList.size());
for(size_t i =0; i < this->BoundsList.size(); ++i)
{
this->BoundsList[i].GetBounds(&boundsList[i*6]);
}
// Collect global bounds and copy into the array
if (this->Controller && this->Controller->GetNumberOfProcesses () >1)
{
AddBoundsListOperator operation;
double* temp = new double[6*this->BoundsList.size()];
this->Controller->Reduce(&boundsList[0],
temp,
6*this->BoundsList.size(),
&operation,
0);
memcpy(&boundsList[0], temp, 6*this->BoundsList.size()*sizeof(double));
delete [] temp;
if (this->Controller->GetLocalProcessId() > 0)
{
// only root node will produce the output.
return 1;
}
}
// Make output with collected bounds
vtkNew<vtkAppendPolyData> appender;
for (size_t i=0; i < 6*this->BoundsList.size (); i+=6)
{
vtkBoundingBox box(&boundsList[i]);
if (box.IsValid())
{
if(this->IsCornerSource)
{
vtkNew<vtkOutlineCornerSource> corner;
corner->SetBounds(&boundsList[i]);
corner->SetCornerFactor(this->CornerFactor);
corner->Update();
appender->AddInputData(corner->GetOutput());
}
else
{
vtkNew<vtkOutlineSource> corner;
corner->SetBounds(&boundsList[i]);
corner->Update();
appender->AddInputData(corner->GetOutput());
}
}
}
if (appender->GetNumberOfInputConnections(0) > 1)
{
appender->Update();
output->ShallowCopy(appender->GetOutput());
}
return 1;
}
// ----------------------------------------------------------------------------
int vtkPOutlineFilterInternals::RequestData(
vtkOverlappingAMR* input, vtkPolyData* output)
{
// For Overlapping AMR, we have meta-data on all processes for the complete
// AMR structure. Hence root node can build the outlines using that
// meta-data, itself.
int procid = this->Controller->GetLocalProcessId();
if (procid != 0)
{
// we only generate output on the root node.
return 1;
}
vtkNew<vtkAppendPolyData> appender;
for (unsigned int level=0; level < input->GetNumberOfLevels(); ++level )
{
unsigned int num_datasets = input->GetNumberOfDataSets(level);
for (unsigned int dataIdx=0; dataIdx < num_datasets; ++dataIdx)
{
double bounds[6];
input->GetAMRInfo()->GetBounds(level, dataIdx, bounds);
// Check if the bounds recieved are not default bounding box
if (vtkBoundingBox::IsValid(bounds))
{
if (this->IsCornerSource)
{
vtkNew<vtkOutlineCornerSource> corner;
corner->SetBounds(bounds);
corner->SetCornerFactor(this->CornerFactor);
corner->Update();
appender->AddInputData(corner->GetOutput());
}
else
{
vtkNew<vtkOutlineSource> corner;
corner->SetBounds(bounds);
corner->Update();
appender->AddInputData(corner->GetOutput());
}
}
}
}
if (appender->GetNumberOfInputConnections(0) > 1)
{
appender->Update();
output->ShallowCopy(appender->GetOutput());
}
return 1;
}
// ----------------------------------------------------------------------------
int vtkPOutlineFilterInternals::RequestData(vtkUniformGridAMR* input,
vtkPolyData* output)
{
// All processes simply produce the outline for the non-null blocks that exist
// on the process.
vtkNew<vtkAppendPolyData> appender;
unsigned int block_id=0;
for (unsigned int level=0; level < input->GetNumberOfLevels(); ++level )
{
unsigned int num_datasets = input->GetNumberOfDataSets(level);
for (unsigned int dataIdx=0; dataIdx < num_datasets; ++dataIdx, block_id++)
{
vtkUniformGrid *ug = input->GetDataSet( level, dataIdx );
if(ug)
{
double bounds[6];
ug->GetBounds(bounds);
// Check if the bounds recieved are not default bounding box
if (vtkBoundingBox::IsValid(bounds))
{
if (this->IsCornerSource)
{
vtkNew<vtkOutlineCornerSource> corner;
corner->SetBounds(bounds);
corner->SetCornerFactor(this->CornerFactor);
corner->Update();
appender->AddInputData(corner->GetOutput());
}
else
{
vtkNew<vtkOutlineSource> corner;
corner->SetBounds(bounds);
corner->Update();
appender->AddInputData(corner->GetOutput());
}
}
}
}
}
if (appender->GetNumberOfInputConnections(0) > 1)
{
appender->Update();
output->ShallowCopy(appender->GetOutput());
}
return 1;
}
// ----------------------------------------------------------------------------
int vtkPOutlineFilterInternals::RequestData(
vtkDataSet* input, vtkPolyData* output)
{
double bounds[6];
input->GetBounds(bounds);
if (this->Controller->GetNumberOfProcesses() > 1)
{
double reduced_bounds[6];
int procid = this->Controller->GetLocalProcessId();
AddBoundsListOperator operation;
this->Controller->Reduce(bounds, reduced_bounds, 6, &operation, 0);
if (procid > 0)
{
// Satellite node
return 1;
}
memcpy(bounds, reduced_bounds, 6*sizeof(double));
}
if (vtkMath::AreBoundsInitialized(bounds))
{
// only output in process 0.
if(this->IsCornerSource)
{
vtkNew<vtkOutlineCornerSource> corner;
corner->SetBounds(bounds);
corner->SetCornerFactor(this->CornerFactor);
corner->Update();
output->ShallowCopy(corner->GetOutput());
}
else
{
vtkNew<vtkOutlineSource> corner;
corner->SetBounds(bounds);
corner->Update();
output->ShallowCopy(corner->GetOutput());
}
}
return 1;
}
// ----------------------------------------------------------------------------
int vtkPOutlineFilterInternals::RequestData(vtkGraph *input,
vtkPolyData *output)
{
double bounds[6];
input->GetBounds(bounds);
if (this->Controller->GetNumberOfProcesses() > 1)
{
double reduced_bounds[6];
int procid = this->Controller->GetLocalProcessId();
AddBoundsListOperator operation;
this->Controller->Reduce(bounds, reduced_bounds, 6, &operation, 0);
if (procid > 0)
{
// Satellite node
return 1;
}
memcpy(bounds, reduced_bounds, 6*sizeof(double));
}
if (vtkMath::AreBoundsInitialized(bounds))
{
// only output in process 0.
if(this->IsCornerSource)
{
vtkNew<vtkOutlineCornerSource> corner;
corner->SetBounds(bounds);
corner->SetCornerFactor(this->CornerFactor);
corner->Update();
output->ShallowCopy(corner->GetOutput());
}
else
{
vtkNew<vtkOutlineSource> corner;
corner->SetBounds(bounds);
corner->Update();
output->ShallowCopy(corner->GetOutput());
}
}
return 1;
}
|