| 356 | } |
| 357 | |
| 358 | void vtkImageToPolyDataFilter::PolygonalizeImage(vtkUnsignedCharArray* pixels, int dims[3], |
| 359 | double origin[3], double spacing[3], vtkPolyData* output) |
| 360 | { |
| 361 | int numPolys; |
| 362 | int numPixels = dims[0] * dims[1]; |
| 363 | |
| 364 | // Perform connected traversal on quantized points. This builds |
| 365 | // the initial "polygons" in implicit form. |
| 366 | // |
| 367 | this->PolyColors = vtkUnsignedCharArray::New(); |
| 368 | this->PolyColors->SetNumberOfComponents(3); |
| 369 | this->PolyColors->Allocate(5000); |
| 370 | |
| 371 | numPolys = this->ProcessImage(pixels, dims); |
| 372 | vtkDebugMacro(<< "Visited regions..." << numPolys << " polygons"); |
| 373 | |
| 374 | // Build edges around the boundary of the polygons. Also identify |
| 375 | // junction points where 3 or 4 polygons meet. |
| 376 | // |
| 377 | vtkPoints* points = vtkPoints::New(); |
| 378 | points->Allocate(numPixels / 2, numPixels / 2); |
| 379 | |
| 380 | vtkUnsignedCharArray* pointDescr = vtkUnsignedCharArray::New(); |
| 381 | pointDescr->Allocate(numPixels / 2, numPixels / 2); |
| 382 | |
| 383 | vtkCellArray* edgeConn = vtkCellArray::New(); |
| 384 | edgeConn->AllocateEstimate(numPixels / 2, 1); |
| 385 | vtkPolyData* edges = vtkPolyData::New(); |
| 386 | edges->SetPoints(points); |
| 387 | edges->SetLines(edgeConn); |
| 388 | points->Delete(); |
| 389 | edgeConn->Delete(); |
| 390 | |
| 391 | this->BuildEdges(pixels, dims, origin, spacing, pointDescr, edges); |
| 392 | vtkDebugMacro(<< "Edges built..."); |
| 393 | |
| 394 | // Now that we've got the edges, we have to build the "loops" around the |
| 395 | // polygons that define the polygon explicitly. |
| 396 | // |
| 397 | vtkUnsignedCharArray* polyColors = vtkUnsignedCharArray::New(); |
| 398 | polyColors->SetNumberOfComponents(3); |
| 399 | polyColors->SetNumberOfValues(numPolys * 3); |
| 400 | |
| 401 | this->BuildPolygons(pointDescr, edges, numPolys, polyColors); |
| 402 | this->PolyColors->Delete(); |
| 403 | delete[] this->Visited; |
| 404 | vtkDebugMacro(<< "Constructed polygons..."); |
| 405 | |
| 406 | // Smooth edge network. Some points are identified as fixed, others |
| 407 | // move using Laplacian smoothing. |
| 408 | // |
| 409 | if (this->Smoothing) |
| 410 | { |
| 411 | this->SmoothEdges(pointDescr, edges); |
| 412 | vtkDebugMacro(<< "Edges smoothed..."); |
| 413 | } |
| 414 | |
| 415 | // Decimate edge network. There will be colinear vertices along edges. |
no test coverage detected