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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCellLinks.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 "vtkCellLinks.h"
#include "vtkCellArray.h"
#include "vtkDataSet.h"
#include "vtkGenericCell.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkSMPTools.h"
#include <vector>
VTK_ABI_NAMESPACE_BEGIN
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkCellLinks);
//------------------------------------------------------------------------------
vtkCellLinks::vtkCellLinks()
: Array(nullptr)
, Size(0)
, MaxId(-1)
, Extend(1000)
, NumberOfPoints(0)
, NumberOfCells(0)
{
this->Type = vtkAbstractCellLinks::CELL_LINKS;
}
//------------------------------------------------------------------------------
vtkCellLinks::~vtkCellLinks()
{
this->Initialize();
}
//------------------------------------------------------------------------------
void vtkCellLinks::Initialize()
{
if (this->Array != nullptr)
{
for (vtkIdType i = 0; i <= this->MaxId; i++)
{
delete[] this->Array[i].cells;
}
delete[] this->Array;
this->Array = nullptr;
}
this->Size = 0;
this->NumberOfPoints = 0;
this->NumberOfCells = 0;
this->Modified();
}
//------------------------------------------------------------------------------
void vtkCellLinks::Allocate(vtkIdType sz, vtkIdType ext)
{
static vtkCellLinks::Link linkInit = { 0, nullptr };
this->Initialize();
this->Size = sz;
this->Array = new vtkCellLinks::Link[sz];
this->Extend = ext;
this->MaxId = -1;
for (vtkIdType i = 0; i < sz; i++)
{
this->Array[i] = linkInit;
}
this->Modified();
}
//------------------------------------------------------------------------------
namespace
{
struct LinkAllocator
{
private:
vtkCellLinks::Link* LinkArray;
public:
LinkAllocator(vtkCellLinks::Link* linkArray)
: LinkArray(linkArray)
{
}
void operator()(vtkIdType cellId, vtkIdType endCellId)
{
for (; cellId < endCellId; ++cellId)
{
this->LinkArray[cellId].cells = new vtkIdType[this->LinkArray[cellId].ncells];
}
}
};
} // namespace
//----------------------------------------------------------------------------
// Allocate memory for the list of lists of cell ids.
void vtkCellLinks::AllocateLinks(vtkIdType n)
{
LinkAllocator allocator(this->Array);
vtkSMPTools::For(0, n, allocator);
}
//------------------------------------------------------------------------------
// Reclaim any unused memory.
void vtkCellLinks::Squeeze()
{
this->Resize(this->MaxId + 1);
}
//------------------------------------------------------------------------------
void vtkCellLinks::Reset()
{
this->MaxId = -1;
this->Modified();
}
//------------------------------------------------------------------------------
//
// Private function does "reallocate"
//
vtkCellLinks::Link* vtkCellLinks::Resize(vtkIdType sz)
{
vtkIdType i;
vtkCellLinks::Link* newArray;
vtkIdType newSize;
vtkCellLinks::Link linkInit = { 0, nullptr };
if (sz >= this->Size)
{
newSize = this->Size + sz;
}
else
{
newSize = sz;
}
newArray = new vtkCellLinks::Link[newSize];
for (i = 0; i < sz && i < this->Size; i++)
{
newArray[i] = this->Array[i];
}
for (i = this->Size; i < newSize; i++)
{
newArray[i] = linkInit;
}
this->Size = newSize;
delete[] this->Array;
this->Array = newArray;
return this->Array;
}
//------------------------------------------------------------------------------
// Build the link list array.
void vtkCellLinks::BuildLinks()
{
// don't rebuild if build time is newer than modified and dataset modified time
if (this->Array && this->BuildTime > this->MTime && this->BuildTime > this->DataSet->GetMTime())
{
return;
}
vtkIdType numPts = this->NumberOfPoints = this->DataSet->GetNumberOfPoints();
vtkIdType numCells = this->NumberOfCells = this->DataSet->GetNumberOfCells();
int j;
vtkIdType cellId;
if (this->Array == nullptr)
{
this->Allocate(numPts);
}
// This is checked to capture changes in the size of the allocation
else if (this->DataSet->GetMTime() > this->BuildTime && this->DataSet->GetMTime() > this->MTime)
{
this->Allocate(numPts);
}
// fill out lists with number of references to cells
std::vector<vtkIdType> linkLoc(numPts, 0);
// Use fast path if polydata
if (this->DataSet->GetDataObjectType() == VTK_POLY_DATA)
{
vtkIdType npts;
const vtkIdType* pts;
vtkPolyData* pdata = static_cast<vtkPolyData*>(this->DataSet);
// traverse data to determine number of uses of each point
for (cellId = 0; cellId < numCells; cellId++)
{
pdata->GetCellPoints(cellId, npts, pts);
for (j = 0; j < npts; j++)
{
this->IncrementLinkCount(pts[j]);
}
}
// now allocate storage for the links
this->AllocateLinks(numPts);
this->MaxId = numPts - 1;
for (cellId = 0; cellId < numCells; cellId++)
{
pdata->GetCellPoints(cellId, npts, pts);
for (j = 0; j < npts; j++)
{
this->InsertCellReference(pts[j], (linkLoc[pts[j]])++, cellId);
}
}
}
else // any other type of dataset
{
vtkIdType numberOfPoints, ptId;
vtkGenericCell* cell = vtkGenericCell::New();
// traverse data to determine number of uses of each point
for (cellId = 0; cellId < numCells; cellId++)
{
this->DataSet->GetCell(cellId, cell);
numberOfPoints = cell->GetNumberOfPoints();
for (j = 0; j < numberOfPoints; j++)
{
this->IncrementLinkCount(cell->PointIds->GetId(j));
}
}
// now allocate storage for the links
this->AllocateLinks(numPts);
this->MaxId = numPts - 1;
for (cellId = 0; cellId < numCells; cellId++)
{
this->DataSet->GetCell(cellId, cell);
numberOfPoints = cell->GetNumberOfPoints();
for (j = 0; j < numberOfPoints; j++)
{
ptId = cell->PointIds->GetId(j);
this->InsertCellReference(ptId, (linkLoc[ptId])++, cellId);
}
}
cell->Delete();
} // end else
this->BuildTime.Modified();
}
//------------------------------------------------------------------------------
// Insert a new point into the cell-links data structure. The size parameter
// is the initial size of the list.
vtkIdType vtkCellLinks::InsertNextPoint(int numLinks)
{
if (++this->MaxId >= this->Size)
{
this->Resize(this->MaxId + 1);
}
this->Array[this->MaxId].cells = new vtkIdType[numLinks];
return this->MaxId;
}
//------------------------------------------------------------------------------
// Mark cells with one or more points whose degree lies in the range indicated.
void vtkCellLinks::SelectCells(vtkIdType minMaxDegree[2], unsigned char* cellSelection)
{
std::fill_n(cellSelection, this->NumberOfCells, 0);
vtkSMPTools::For(0, this->NumberOfPoints,
[this, minMaxDegree, cellSelection](vtkIdType ptId, vtkIdType endPtId) {
for (; ptId < endPtId; ++ptId)
{
vtkIdType degree = this->GetNcells(0);
if (degree >= minMaxDegree[0] && degree < minMaxDegree[1])
{
vtkIdType* cells = this->GetCells(ptId);
for (auto i = 0; i < degree; ++i)
{
cellSelection[cells[i]] = 1;
}
}
} // for all points in this batch
}); // end lambda
}
//------------------------------------------------------------------------------
unsigned long vtkCellLinks::GetActualMemorySize()
{
vtkIdType size = 0;
vtkIdType ptId;
for (ptId = 0; ptId < (this->MaxId + 1); ptId++)
{
size += this->GetNcells(ptId);
}
size *= sizeof(int*); // references to cells
size += (this->MaxId + 1) * sizeof(vtkCellLinks::Link); // list of cell lists
return static_cast<unsigned long>(ceil(size / 1024.0)); // kibibytes
}
//------------------------------------------------------------------------------
void vtkCellLinks::DeepCopy(vtkAbstractCellLinks* src)
{
this->SetDataSet(src->GetDataSet());
this->SetSequentialProcessing(src->GetSequentialProcessing());
vtkCellLinks* clinks = static_cast<vtkCellLinks*>(src);
this->Allocate(clinks->Size, clinks->Extend);
vtkSMPTools::For(0, clinks->MaxId + 1, [this, clinks](vtkIdType ptId, vtkIdType endPtId) {
for (; ptId < endPtId; ++ptId)
{
vtkIdType ncells = clinks->GetNcells(ptId);
this->Array[ptId].cells = new vtkIdType[ncells];
this->Array[ptId].ncells = ncells;
std::copy_n(clinks->Array[ptId].cells, ncells, this->Array[ptId].cells);
}
});
this->MaxId = clinks->MaxId;
this->BuildTime.Modified();
}
//------------------------------------------------------------------------------
void vtkCellLinks::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Size: " << this->Size << "\n";
os << indent << "MaxId: " << this->MaxId << "\n";
os << indent << "Extend: " << this->Extend << "\n";
}
VTK_ABI_NAMESPACE_END
|