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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkHardwareSelectionPolyDataPainter.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 "vtkHardwareSelectionPolyDataPainter.h"
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkHardwareSelector.h"
#include "vtkIdTypeArray.h"
#include "vtkObjectFactory.h"
#include "vtkPainterDeviceAdapter.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkTimerLog.h"
#include "vtkUnsignedIntArray.h"
vtkStandardNewMacro(vtkHardwareSelectionPolyDataPainter);
//-----------------------------------------------------------------------------
static inline int vtkHardwareSelectionPolyDataPainterGetTotalCells(vtkPolyData* pd,
unsigned long typeflags)
{
int total_cells = 0;
total_cells += (typeflags & vtkPainter::VERTS)?
pd->GetNumberOfVerts() : 0;
total_cells += (typeflags & vtkPainter::LINES)?
pd->GetNumberOfLines() : 0;
total_cells += (typeflags & vtkPainter::POLYS)?
pd->GetNumberOfPolys() : 0;
total_cells += (typeflags & vtkPainter::STRIPS)?
pd->GetNumberOfStrips() : 0;
return total_cells;
}
//----------------------------------------------------------------------------
vtkHardwareSelectionPolyDataPainter::vtkHardwareSelectionPolyDataPainter()
{
this->EnableSelection = 1;
this->PointIdArrayName = NULL;
this->CellIdArrayName = NULL;
this->ProcessIdArrayName = NULL;
this->CompositeIdArrayName = NULL;
}
//----------------------------------------------------------------------------
vtkHardwareSelectionPolyDataPainter::~vtkHardwareSelectionPolyDataPainter()
{
this->SetPointIdArrayName(NULL);
this->SetCellIdArrayName(NULL);
this->SetProcessIdArrayName(NULL);
this->SetCompositeIdArrayName(NULL);
}
//-----------------------------------------------------------------------------
void vtkHardwareSelectionPolyDataPainter::RenderInternal(
vtkRenderer* renderer,
vtkActor* vtkNotUsed(actor),
unsigned long typeflags,
bool vtkNotUsed(forceCompileOnly))
{
if (typeflags == 0)
{
// No primitive to render.
return;
}
vtkPainterDeviceAdapter* device =
renderer->GetRenderWindow()->GetPainterDeviceAdapter();
if (device == NULL)
{
vtkErrorMacro("Painter Device Adapter missing!");
return;
}
vtkPolyData* pd = this->GetInputAsPolyData();
this->TotalCells = vtkHardwareSelectionPolyDataPainterGetTotalCells(pd, typeflags);
if (this->TotalCells == 0)
{
// skip empty polydatas.
this->TimeToDraw = 0;
return;
}
vtkHardwareSelector* selector = renderer->GetSelector();
if (this->EnableSelection)
{
selector->BeginRenderProp();
// We emphasis the vertex size to make sure they will be properly detected.
if (selector->GetFieldAssociation() == vtkDataObject::FIELD_ASSOCIATION_POINTS &&
selector->GetCurrentPass() > vtkHardwareSelector::ACTOR_PASS)
{
device->MakeVertexEmphasis(true);
}
}
this->Timer->StartTimer();
vtkIdType startCell = 0;
if (typeflags & vtkPainter::VERTS)
{
this->DrawCells(VTK_POLY_VERTEX, pd->GetVerts(), startCell, renderer);
}
startCell += pd->GetNumberOfVerts();
if (typeflags & vtkPainter::LINES)
{
this->DrawCells(VTK_POLY_LINE, pd->GetLines(), startCell, renderer);
}
startCell += pd->GetNumberOfLines();
if (typeflags & vtkPainter::POLYS)
{
this->DrawCells(VTK_POLYGON, pd->GetPolys(), startCell, renderer);
}
startCell += pd->GetNumberOfPolys();
if (typeflags & vtkPainter::STRIPS)
{
this->DrawCells(VTK_TRIANGLE_STRIP, pd->GetStrips(), startCell, renderer);
}
if (this->EnableSelection)
{
selector->EndRenderProp();
// We revert back our Vertex emphasis
if (selector->GetFieldAssociation() == vtkDataObject::FIELD_ASSOCIATION_POINTS &&
selector->GetCurrentPass() > vtkHardwareSelector::ACTOR_PASS)
{
device->MakeVertexEmphasis(false);
}
}
this->Timer->StopTimer();
this->TimeToDraw = this->Timer->GetElapsedTime();
}
//-----------------------------------------------------------------------------
void vtkHardwareSelectionPolyDataPainter::DrawCells(
int mode, vtkCellArray *connectivity, vtkIdType startCellId,
vtkRenderer *renderer)
{
vtkPolyData* pd = this->GetInputAsPolyData();
vtkPainterDeviceAdapter* device = renderer->GetRenderWindow()->
GetPainterDeviceAdapter();
vtkHardwareSelector* selector = renderer->GetSelector();
int attributeMode = selector->GetFieldAssociation();
// While looking at point selection we render only vertex so each pass
// should fill the same pixels without risking of detecting vertex belonging
// to other cells or block. BUT we do that after the ACTOR_PASS to make sure
// we have the proper oclusion as we keep the Z-buffer arround. In that way
// vertex that are hidden by some surface won't get selected.
if (attributeMode == vtkDataObject::FIELD_ASSOCIATION_POINTS &&
selector->GetCurrentPass() > vtkHardwareSelector::ACTOR_PASS &&
this->EnableSelection)
{
mode = VTK_POLY_VERTEX;
}
vtkPoints* p = pd->GetPoints();
vtkIdType npts, *pts;
vtkIdType cellId = startCellId;
vtkUnsignedIntArray* procIdsArray = this->ProcessIdArrayName?
vtkUnsignedIntArray::SafeDownCast(
pd->GetPointData()->GetArray(this->ProcessIdArrayName)) : NULL;
vtkIdTypeArray* pidArray = this->PointIdArrayName? vtkIdTypeArray::SafeDownCast(
pd->GetPointData()->GetArray(this->PointIdArrayName)) : NULL;
vtkIdTypeArray* cidArray = this->CellIdArrayName? vtkIdTypeArray::SafeDownCast(
pd->GetCellData()->GetArray(this->CellIdArrayName)) : NULL;
vtkUnsignedIntArray* compositeIdArray = this->CompositeIdArrayName?
vtkUnsignedIntArray::SafeDownCast(
pd->GetCellData()->GetArray(this->CompositeIdArrayName)) : NULL;
int pointtype = p->GetDataType();
void* voidpoints = p->GetVoidPointer(0);
int count = 0;
// Note that cell attributes are overridden by point attributes.
for (connectivity->InitTraversal(); connectivity->GetNextCell(npts, pts); count++)
{
device->BeginPrimitive(mode);
if (this->EnableSelection && compositeIdArray)
{
selector->RenderCompositeIndex(compositeIdArray->GetValue(cellId));
}
if (attributeMode == vtkDataObject::FIELD_ASSOCIATION_CELLS &&
this->EnableSelection)
{
selector->RenderAttributeId(
cidArray? cidArray->GetValue(cellId) : cellId);
}
for (vtkIdType cellpointi = 0; cellpointi < npts; cellpointi++)
{
vtkIdType pointId = pts[cellpointi];
if (attributeMode == vtkDataObject::FIELD_ASSOCIATION_POINTS &&
this->EnableSelection)
{
selector->RenderAttributeId(
pidArray? pidArray->GetValue(pointId) : pointId);
}
if (this->EnableSelection && procIdsArray &&
selector->GetUseProcessIdFromData())
{
selector->RenderProcessId(procIdsArray->GetPointer(0)[pointId]);
}
device->SendAttribute(vtkPointData::NUM_ATTRIBUTES, 3,
pointtype, voidpoints, 3*pointId);
}
device->EndPrimitive();
cellId++;
if (count == 10000)
{
count = 0;
// report progress
this->UpdateProgress(static_cast<double>(cellId - startCellId)/this->TotalCells);
// Abort render.
if (renderer->GetRenderWindow()->CheckAbortStatus())
{
return;
}
}
}
}
//----------------------------------------------------------------------------
void vtkHardwareSelectionPolyDataPainter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "EnableSelection: " << this->EnableSelection << endl;
os << indent << "CellIdArrayName: " <<
(this->CellIdArrayName? this->CellIdArrayName : "(none)") << endl;
os << indent << "PointIdArrayName: " <<
(this->PointIdArrayName? this->PointIdArrayName: "(none)") << endl;
os << indent << "CompositeIdArrayName: "
<< (this->CompositeIdArrayName? this->CompositeIdArrayName : "(none)")
<< endl;
}
|