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 448 449 450
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/*----------------------------------------------------------------------------
This test was written by Menno Deij - van Rijswijk (MARIN).
----------------------------------------------------------------------------*/
#include "vtkMappedUnstructuredGridGenerator.h"
#include "vtkCell.h" // for cell types
#include "vtkCellArray.h"
#include "vtkCellIterator.h"
#include "vtkDataArray.h"
#include "vtkGenericCell.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkMappedUnstructuredGrid.h"
#include "vtkNew.h"
#include "vtkPoints.h"
#include "vtkUnstructuredGrid.h"
#include <algorithm>
#include <fstream>
#include <string>
namespace
{ // this namespace contains the supporting mapped grid definition used in the test
template <class I>
class MappedCellIterator : public vtkCellIterator
{
public:
vtkTemplateTypeMacro(MappedCellIterator<I>, vtkCellIterator);
typedef MappedCellIterator<I> ThisType;
static MappedCellIterator<I>* New();
void SetMappedUnstructuredGrid(vtkMappedUnstructuredGrid<I, ThisType>* grid);
void PrintSelf(std::ostream& os, vtkIndent id) override;
bool IsDoneWithTraversal() override;
vtkIdType GetCellId() override;
protected:
MappedCellIterator();
~MappedCellIterator() override;
void ResetToFirstCell() override { this->CellId = 0; }
void IncrementToNextCell() override { this->CellId++; }
void FetchCellType() override;
void FetchPointIds() override;
void FetchPoints() override;
void FetchFaces() override;
private:
MappedCellIterator(const MappedCellIterator&) = delete;
void operator=(const MappedCellIterator&) = delete;
vtkIdType CellId;
vtkIdType NumberOfCells;
vtkSmartPointer<I> Impl;
vtkSmartPointer<vtkPoints> GridPoints;
};
template <class I>
MappedCellIterator<I>* MappedCellIterator<I>::New()
{
VTK_STANDARD_NEW_BODY(ThisType);
}
template <class I>
MappedCellIterator<I>::MappedCellIterator()
: CellId(0)
, NumberOfCells(0)
, Impl(nullptr)
, GridPoints(nullptr)
{
}
template <class I>
void MappedCellIterator<I>::PrintSelf(ostream& os, vtkIndent indent)
{
os << indent << "Mapped Internal Block" << endl;
}
template <class I>
MappedCellIterator<I>::~MappedCellIterator() = default;
template <class I>
void MappedCellIterator<I>::SetMappedUnstructuredGrid(vtkMappedUnstructuredGrid<I, ThisType>* grid)
{
this->Impl = grid->GetImplementation();
this->CellId = 0;
this->GridPoints = grid->GetPoints();
this->NumberOfCells = grid->GetNumberOfCells();
}
template <class I>
bool MappedCellIterator<I>::IsDoneWithTraversal()
{
if (!this->Impl)
return true;
return CellId >= this->NumberOfCells;
}
template <class I>
vtkIdType MappedCellIterator<I>::GetCellId()
{
return this->CellId;
}
template <class I>
void MappedCellIterator<I>::FetchCellType()
{
this->CellType = Impl->GetCellType(this->CellId);
}
template <class I>
void MappedCellIterator<I>::FetchPointIds()
{
this->Impl->GetCellPoints(this->CellId, this->PointIds);
}
template <class I>
void MappedCellIterator<I>::FetchPoints()
{
this->GridPoints->GetPoints(this->GetPointIds(), this->Points);
}
template <class I>
void MappedCellIterator<I>::FetchFaces()
{
this->Impl->GetPolyhedronFaces(this->CellId, this->Faces);
}
class MappedGridImpl : public vtkObject
{
public:
static MappedGridImpl* New();
void Initialize(vtkUnstructuredGrid* ug)
{
ug->Register(this);
Owner->SetPoints(ug->GetPoints());
_grid = ug;
}
void PrintSelf(std::ostream& os, vtkIndent id) override;
// API for vtkMappedUnstructuredGrid implementation
virtual int GetCellType(vtkIdType cellId);
virtual void GetCellPoints(vtkIdType cellId, vtkIdList* ptIds);
virtual void GetFaceStream(vtkIdType cellId, vtkIdList* ptIds);
virtual void GetPolyhedronFaces(vtkIdType cellId, vtkCellArray* ptIds);
virtual void GetPointCells(vtkIdType ptId, vtkIdList* cellIds);
virtual int GetMaxCellSize();
virtual void GetIdsOfCellsOfType(int type, vtkIdTypeArray* array);
virtual int IsHomogeneous();
// This container is read only -- these methods do nothing but print a warning.
void Allocate(vtkIdType numCells, int extSize = 1000);
vtkIdType InsertNextCell(int type, vtkIdList* ptIds);
vtkIdType InsertNextCell(int type, vtkIdType npts, const vtkIdType ptIds[])
VTK_SIZEHINT(ptIds, npts);
vtkIdType InsertNextCell(int type, vtkIdType npts, const vtkIdType ptIds[], vtkCellArray* faces)
VTK_SIZEHINT(ptIds, npts);
void ReplaceCell(vtkIdType cellId, int npts, const vtkIdType pts[]) VTK_SIZEHINT(pts, npts);
vtkIdType GetNumberOfCells();
void SetOwner(vtkPointSet* owner) { this->Owner = owner; }
vtkPoints* GetPoints() { return _grid->GetPoints(); }
protected:
MappedGridImpl() = default;
~MappedGridImpl() override { _grid->UnRegister(this); }
private:
vtkUnstructuredGrid* _grid;
vtkPointSet* Owner;
};
vtkStandardNewMacro(MappedGridImpl);
void MappedGridImpl::PrintSelf(ostream& os, vtkIndent indent)
{
os << indent << "Mapped Grid Implementation" << endl;
}
int MappedGridImpl::GetCellType(vtkIdType cellId)
{
return _grid->GetCellType(cellId);
}
int MappedGridImpl::GetMaxCellSize()
{
return _grid->GetMaxCellSize();
}
void MappedGridImpl::GetCellPoints(vtkIdType cellId, vtkIdList* ptIds)
{
_grid->GetCellPoints(cellId, ptIds);
}
void MappedGridImpl::GetFaceStream(vtkIdType cellId, vtkIdList* ptIds)
{
_grid->GetFaceStream(cellId, ptIds);
}
void MappedGridImpl::GetPolyhedronFaces(vtkIdType cellId, vtkCellArray* faces)
{
_grid->GetPolyhedronFaces(cellId, faces);
}
void MappedGridImpl::GetPointCells(vtkIdType ptId, vtkIdList* cellIds)
{
_grid->GetPointCells(ptId, cellIds);
}
int MappedGridImpl::IsHomogeneous()
{
return _grid->IsHomogeneous();
}
vtkIdType MappedGridImpl::GetNumberOfCells()
{
return _grid->GetNumberOfCells();
}
void MappedGridImpl::GetIdsOfCellsOfType(int type, vtkIdTypeArray* array)
{
_grid->GetIdsOfCellsOfType(type, array);
}
void MappedGridImpl::Allocate(vtkIdType vtkNotUsed(numCells), int vtkNotUsed(extSize))
{
vtkWarningMacro(<< "Read only block\n");
}
vtkIdType MappedGridImpl::InsertNextCell(int vtkNotUsed(type), vtkIdList* vtkNotUsed(ptIds))
{
vtkWarningMacro(<< "Read only block\n");
return -1;
}
vtkIdType MappedGridImpl::InsertNextCell(
int vtkNotUsed(type), vtkIdType vtkNotUsed(npts), const vtkIdType vtkNotUsed(ptIds)[])
{
vtkWarningMacro(<< "Read only block\n");
return -1;
}
vtkIdType MappedGridImpl::InsertNextCell(int vtkNotUsed(type), vtkIdType vtkNotUsed(npts),
const vtkIdType vtkNotUsed(ptIds)[], vtkCellArray* vtkNotUsed(nfaces))
{
vtkWarningMacro(<< "Read only block\n");
return -1;
}
void MappedGridImpl::ReplaceCell(
vtkIdType vtkNotUsed(cellId), int vtkNotUsed(npts), const vtkIdType vtkNotUsed(pts)[])
{
vtkWarningMacro(<< "Read only block\n");
}
class MappedGrid
: public vtkMappedUnstructuredGrid<MappedGridImpl, MappedCellIterator<MappedGridImpl>>
{
public:
typedef vtkMappedUnstructuredGrid<MappedGridImpl, MappedCellIterator<MappedGridImpl>> _myBase;
int GetDataObjectType() override { return VTK_UNSTRUCTURED_GRID_BASE; }
static MappedGrid* New();
vtkPoints* GetPoints() override { return this->GetImplementation()->GetPoints(); }
vtkIdType GetNumberOfPoints() override
{
return this->GetImplementation()->GetPoints()->GetNumberOfPoints();
}
protected:
MappedGrid()
{
MappedGridImpl* ig = MappedGridImpl::New();
ig->SetOwner(this);
this->SetImplementation(ig);
ig->Delete();
}
~MappedGrid() override = default;
private:
MappedGrid(const MappedGrid&) = delete;
void operator=(const MappedGrid&) = delete;
};
vtkStandardNewMacro(MappedGrid);
} // end anonymous namespace
VTK_ABI_NAMESPACE_BEGIN
void vtkMappedUnstructuredGridGenerator::GenerateUnstructuredGrid(vtkUnstructuredGrid** grid)
{
vtkNew<vtkPoints> points;
points->InsertNextPoint(0, 0, 0);
points->InsertNextPoint(1, 0, 0);
points->InsertNextPoint(1, 1, 0);
points->InsertNextPoint(0, 1, 0);
points->InsertNextPoint(0, 0, 1);
points->InsertNextPoint(1, 0, 1);
points->InsertNextPoint(1, 1, 1);
points->InsertNextPoint(0, 1, 1);
points->InsertNextPoint(.5, .5, 2);
points->InsertNextPoint(.5, .5, -1);
vtkUnstructuredGrid* ug = vtkUnstructuredGrid::New();
ug->SetPoints(points);
ug->Allocate(3); // allocate for 3 cells
vtkNew<vtkIdList> ids;
// add a hexahedron of the first 8 points (i.e. a cube)
ids->InsertNextId(0);
ids->InsertNextId(1);
ids->InsertNextId(2);
ids->InsertNextId(3);
ids->InsertNextId(4);
ids->InsertNextId(5);
ids->InsertNextId(6);
ids->InsertNextId(7);
ug->InsertNextCell(VTK_HEXAHEDRON, ids.GetPointer());
ids->Reset();
// add a polyhedron comprise of the top hexahedron face
// and four triangles to the 9th point
ids->InsertNextId(4);
ids->InsertNextId(5);
ids->InsertNextId(6);
ids->InsertNextId(7);
ids->InsertNextId(8);
vtkNew<vtkCellArray> faces;
// top face of four points
faces->InsertNextCell(4);
faces->InsertCellPoint(4);
faces->InsertCellPoint(5);
faces->InsertCellPoint(6);
faces->InsertCellPoint(7);
// four triangle side faces, each of three points
faces->InsertNextCell(3);
faces->InsertCellPoint(4);
faces->InsertCellPoint(5);
faces->InsertCellPoint(8);
faces->InsertNextCell(3);
faces->InsertCellPoint(5);
faces->InsertCellPoint(6);
faces->InsertCellPoint(8);
faces->InsertNextCell(3);
faces->InsertCellPoint(6);
faces->InsertCellPoint(7);
faces->InsertCellPoint(8);
faces->InsertNextCell(3);
faces->InsertCellPoint(7);
faces->InsertCellPoint(4);
faces->InsertCellPoint(8);
// insert the polyhedron cell
ug->InsertNextCell(VTK_POLYHEDRON, 5, ids.GetPointer()->GetPointer(0), faces);
// put another pyramid on the bottom towards the 10th point
faces->Reset();
ids->Reset();
// the list of points that the pyramid references
ids->InsertNextId(0);
ids->InsertNextId(1);
ids->InsertNextId(2);
ids->InsertNextId(3);
ids->InsertNextId(9);
// bottom face of four points
faces->InsertNextCell(4);
faces->InsertCellPoint(0);
faces->InsertCellPoint(1);
faces->InsertCellPoint(2);
faces->InsertCellPoint(3);
// four side faces, each of three points
faces->InsertNextCell(3);
faces->InsertCellPoint(0);
faces->InsertCellPoint(1);
faces->InsertCellPoint(9);
faces->InsertNextCell(3);
faces->InsertCellPoint(1);
faces->InsertCellPoint(2);
faces->InsertCellPoint(9);
faces->InsertNextCell(3);
faces->InsertCellPoint(2);
faces->InsertCellPoint(3);
faces->InsertCellPoint(9);
faces->InsertNextCell(3);
faces->InsertCellPoint(3);
faces->InsertCellPoint(0);
faces->InsertCellPoint(9);
// insert the cell. We now have two pyramids with a cube in between
ug->InsertNextCell(VTK_POLYHEDRON, 5, ids.GetPointer()->GetPointer(0), faces);
*grid = ug;
}
void vtkMappedUnstructuredGridGenerator::GenerateMappedUnstructuredGrid(
vtkUnstructuredGridBase** grid)
{
vtkUnstructuredGrid* ug;
GenerateUnstructuredGrid(&ug);
// create a mapped grid which basically takes the original grid
// and uses it to map to.
MappedGrid* mg = MappedGrid::New();
mg->GetImplementation()->Initialize(ug);
ug->Delete(); // the mapped grid holds the only reference to ug
*grid = mg;
}
vtkMappedUnstructuredGridGenerator* vtkMappedUnstructuredGridGenerator::New()
{
return new vtkMappedUnstructuredGridGenerator;
}
void vtkMappedUnstructuredGridGenerator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "vtkMappedUnstructuredGridGenerator object";
}
VTK_ABI_NAMESPACE_END
|