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 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPolyDataMapperNode.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 "vtkPolyDataMapperNode.h"
#include "vtkActor.h"
#include "vtkObjectFactory.h"
#include "vtkPolyDataMapper.h"
#include "vtkCellArray.h"
#include "vtkMath.h"
#include "vtkMatrix4x4.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolygon.h"
#include "vtkProperty.h"
//============================================================================
vtkStandardNewMacro(vtkPolyDataMapperNode);
//----------------------------------------------------------------------------
vtkPolyDataMapperNode::vtkPolyDataMapperNode()
{
}
//----------------------------------------------------------------------------
vtkPolyDataMapperNode::~vtkPolyDataMapperNode()
{
}
//----------------------------------------------------------------------------
void vtkPolyDataMapperNode::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkPolyDataMapperNode::TransformPoints(vtkActor *act,
vtkPolyData *poly,
std::vector<double> &_vertices)
{
vtkSmartPointer<vtkMatrix4x4> m = vtkSmartPointer<vtkMatrix4x4>::New();
act->GetMatrix(m);
bool ident = (act->GetIsIdentity()==1);
double inPos[4];
inPos[3] = 1.0;
double transPos[4];
for (int i = 0; i < poly->GetNumberOfPoints(); i++)
{
double *pos = poly->GetPoints()->GetPoint(i);
bool wasNan = false;
int fixIndex = i - 1;
do
{
wasNan = false;
for (int j = 0; j < 3; j++)
{
if (vtkMath::IsNan(pos[j]))
{
wasNan = true;
}
}
if (wasNan && fixIndex >= 0)
{
pos = poly->GetPoints()->GetPoint(fixIndex--);
}
} while (wasNan == true && fixIndex >= 0);
if (ident)
{
_vertices.push_back(pos[0]);
_vertices.push_back(pos[1]);
_vertices.push_back(pos[2]);
}
else
{
inPos[0] = pos[0];
inPos[1] = pos[1];
inPos[2] = pos[2];
m->MultiplyPoint(inPos, transPos);
//alternatively use an OSPRay instance of something like that
_vertices.push_back(transPos[0]);
_vertices.push_back(transPos[1]);
_vertices.push_back(transPos[2]);
}
}
}
namespace {
//Helpers for MakeConnectivity()
//The CreateXIndexBuffer's were adapted from vtkOpenGLIndexBufferObject.
//Apply rule of three if made again somewhere in VTK.
// ----------------------------------------------------------------------------
//Description:
//Homogenizes everything into a flat list of point indexes.
//At same time creates a reverse cell index array for obtaining cell quantities for points
void CreatePointIndexBuffer(vtkCellArray *cells,
std::vector<unsigned int> &indexArray,
std::vector<unsigned int> &reverseArray)
{
//TODO: restore the preallocate and append to offset features I omitted
vtkIdType* indices(NULL);
vtkIdType npts(0);
if (!cells->GetNumberOfCells())
{
return;
}
unsigned int cell_id = 0;
for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
{
for (int i = 0; i < npts; ++i)
{
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
reverseArray.push_back(cell_id);
}
cell_id++;
}
}
//----------------------------------------------------------------------------
//Description:
//Homogenizes lines into a flat list of line segments, each containing two point indexes
//At same time creates a reverse cell index array for obtaining cell quantities for points
void CreateLineIndexBuffer(vtkCellArray *cells,
std::vector<unsigned int> &indexArray,
std::vector<unsigned int> &reverseArray)
{
//TODO: restore the preallocate and append to offset features I omitted
vtkIdType* indices(NULL);
vtkIdType npts(0);
if (!cells->GetNumberOfCells())
{
return;
}
unsigned int cell_id = 0;
for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
{
for (int i = 0; i < npts-1; ++i)
{
indexArray.push_back(static_cast<unsigned int>(indices[i]));
indexArray.push_back(static_cast<unsigned int>(indices[i+1]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
cell_id++;
}
}
//----------------------------------------------------------------------------
//Description:
//Homogenizes polygons into a flat list of line segments, each containing two point indexes.
//At same time creates a reverse cell index array for obtaining cell quantities for points
//This differs from CreateLineIndexBuffer in that it closes loops, making a segment from last point
//back to first.
void CreateTriangleLineIndexBuffer(vtkCellArray *cells,
std::vector<unsigned int> &indexArray,
std::vector<unsigned int> &reverseArray)
{
//TODO: restore the preallocate and append to offset features I omitted
vtkIdType* indices(NULL);
vtkIdType npts(0);
if (!cells->GetNumberOfCells())
{
return;
}
unsigned int cell_id = 0;
for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
{
for (int i = 0; i < npts; ++i)
{
indexArray.push_back(static_cast<unsigned int>(indices[i]));
indexArray.push_back(static_cast<unsigned int>
(indices[i < npts-1 ? i+1 : 0]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
cell_id++;
}
}
//----------------------------------------------------------------------------
//Description:
//Homogenizes polygons into a flat list of triangles, each containing three point indexes
//At same time creates a reverse cell index array for obtaining cell quantities for points
void CreateTriangleIndexBuffer(vtkCellArray *cells, vtkPoints *points,
std::vector<unsigned int> &indexArray,
std::vector<unsigned int> &reverseArray)
{
//TODO: restore the preallocate and append to offset features I omitted
vtkIdType* indices(NULL);
vtkIdType npts(0);
if (!cells->GetNumberOfCells())
{
return;
}
unsigned int cell_id = 0;
// the folowing are only used if we have to triangulate a polygon
// otherwise they just sit at NULL
vtkPolygon *polygon = NULL;
vtkIdList *tris = NULL;
vtkPoints *triPoints = NULL;
for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
{
// ignore degenerate triangles
if (npts < 3)
{
cell_id++;
continue;
}
// triangulate needed
if (npts > 3)
{
// special case for quads, penta, hex which are common
if (npts == 4)
{
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[1]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
else if (npts == 5)
{
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[1]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
indexArray.push_back(static_cast<unsigned int>(indices[4]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
else if (npts == 6)
{
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[1]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
indexArray.push_back(static_cast<unsigned int>(indices[5]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
indexArray.push_back(static_cast<unsigned int>(indices[4]));
indexArray.push_back(static_cast<unsigned int>(indices[5]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
else // 7 sided polygon or higher, do a full smart triangulation
{
if (!polygon)
{
polygon = vtkPolygon::New();
tris = vtkIdList::New();
triPoints = vtkPoints::New();
}
vtkIdType *triIndices = new vtkIdType[npts];
triPoints->SetNumberOfPoints(npts);
for (int i = 0; i < npts; ++i)
{
int idx = indices[i];
triPoints->SetPoint(i, points->GetPoint(idx));
triIndices[i] = i;
}
polygon->Initialize(npts, triIndices, triPoints);
polygon->Triangulate(tris);
for (int j = 0; j < tris->GetNumberOfIds(); ++j)
{
indexArray.push_back(static_cast<unsigned int>
(indices[tris->GetId(j)]));
reverseArray.push_back(cell_id);
}
delete [] triIndices;
}
}
else
{
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
cell_id++;
}
if (polygon)
{
polygon->Delete();
tris->Delete();
triPoints->Delete();
}
}
//----------------------------------------------------------------------------
//Description:
//Homogenizes triangle strips.
//Depending on wireframeTriStrips it will produce either line segments (two indices per edge)
//or triangles (three indices per face).
//At same time creates a reverse cell index array for obtaining cell quantities for points
void CreateStripIndexBuffer(vtkCellArray *cells,
std::vector<unsigned int> &indexArray,
std::vector<unsigned int> &reverseArray,
bool wireframeTriStrips)
{
if (!cells->GetNumberOfCells())
{
return;
}
unsigned int cell_id = 0;
vtkIdType *pts = 0;
vtkIdType npts = 0;
size_t triCount = cells->GetNumberOfConnectivityEntries()
- 3*cells->GetNumberOfCells();
size_t targetSize = wireframeTriStrips ? 2*(triCount*2+1)
: triCount*3;
indexArray.reserve(targetSize);
if (wireframeTriStrips)
{
for (cells->InitTraversal(); cells->GetNextCell(npts,pts); )
{
indexArray.push_back(static_cast<unsigned int>(pts[0]));
indexArray.push_back(static_cast<unsigned int>(pts[1]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
for (int j = 0; j < npts-2; ++j)
{
indexArray.push_back(static_cast<unsigned int>(pts[j]));
indexArray.push_back(static_cast<unsigned int>(pts[j+2]));
indexArray.push_back(static_cast<unsigned int>(pts[j+1]));
indexArray.push_back(static_cast<unsigned int>(pts[j+2]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
cell_id++;
}
}
else
{
for (cells->InitTraversal(); cells->GetNextCell(npts,pts); )
{
for (int j = 0; j < npts-2; ++j)
{
indexArray.push_back(static_cast<unsigned int>(pts[j]));
indexArray.push_back(static_cast<unsigned int>(pts[j+1+j%2]));
indexArray.push_back(static_cast<unsigned int>(pts[j+1+(j+1)%2]));
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
reverseArray.push_back(cell_id);
}
cell_id++;
}
}
}
}
//----------------------------------------------------------------------------
void vtkPolyDataMapperNode::MakeConnectivity(vtkPolyData *poly,
int representation,
std::vector<unsigned int> &vertex_index,
std::vector<unsigned int> &vertex_reverse,
std::vector<unsigned int> &line_index,
std::vector<unsigned int> &line_reverse,
std::vector<unsigned int> &triangle_index,
std::vector<unsigned int> &triangle_reverse,
std::vector<unsigned int> &strip_index,
std::vector<unsigned int> &strip_reverse
)
{
vtkCellArray *prims[4];
prims[0] = poly->GetVerts();
prims[1] = poly->GetLines();
prims[2] = poly->GetPolys();
prims[3] = poly->GetStrips();
CreatePointIndexBuffer(prims[0], vertex_index, vertex_reverse);
switch (representation)
{
case VTK_POINTS:
{
CreatePointIndexBuffer(prims[1], line_index, line_reverse);
CreatePointIndexBuffer(prims[2], triangle_index, triangle_reverse);
CreatePointIndexBuffer(prims[3], strip_index, strip_reverse);
break;
}
case VTK_WIREFRAME:
{
CreateLineIndexBuffer(prims[1], line_index, line_reverse);
CreateTriangleLineIndexBuffer(prims[2], triangle_index, triangle_reverse);
CreateStripIndexBuffer(prims[3], strip_index, strip_reverse, true);
break;
}
default:
{
CreateLineIndexBuffer(prims[1], line_index, line_reverse);
CreateTriangleIndexBuffer(prims[2], poly->GetPoints(), triangle_index, triangle_reverse);
CreateStripIndexBuffer(prims[3], strip_index, strip_reverse, false);
}
}
}
|