Viskores  1.0
VTKDataSetCells.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_io_internal_VTKDataSetCells_h
19 #define viskores_io_internal_VTKDataSetCells_h
20 
21 #include <viskores/CellShape.h>
22 #include <viskores/Types.h>
25 #include <viskores/io/ErrorIO.h>
26 
27 #include <algorithm>
28 #include <vector>
29 
30 namespace viskores
31 {
32 namespace io
33 {
34 namespace internal
35 {
36 
37 enum UnsupportedVTKCells
38 {
39  CELL_SHAPE_POLY_VERTEX = 2,
41  CELL_SHAPE_TRIANGLE_STRIP = 6,
42  CELL_SHAPE_PIXEL = 8,
43  CELL_SHAPE_VOXEL = 11
44 };
45 
46 inline void FixupCellSet(viskores::cont::ArrayHandle<viskores::Id>& connectivity,
50 {
51  std::vector<viskores::Id> newConnectivity;
52  std::vector<viskores::IdComponent> newNumIndices;
53  std::vector<viskores::UInt8> newShapes;
54  std::vector<viskores::Id> permutationVec;
55 
56  viskores::Id connIdx = 0;
57  auto shapesPortal = shapes.ReadPortal();
58  auto indicesPortal = numIndices.ReadPortal();
59  auto connPortal = connectivity.ReadPortal();
60  for (viskores::Id i = 0; i < shapes.GetNumberOfValues(); ++i)
61  {
62  viskores::UInt8 shape = shapesPortal.Get(i);
63  viskores::IdComponent numInds = indicesPortal.Get(i);
64  switch (shape)
65  {
74  {
75  newShapes.push_back(shape);
76  newNumIndices.push_back(numInds);
77  for (viskores::IdComponent j = 0; j < numInds; ++j)
78  {
79  newConnectivity.push_back(connPortal.Get(connIdx++));
80  }
81  permutationVec.push_back(i);
82  break;
83  }
85  {
86  viskores::IdComponent numVerts = numInds;
88  if (numVerts == 3)
89  {
91  }
92  else if (numVerts == 4)
93  {
94  newShape = viskores::CELL_SHAPE_QUAD;
95  }
96  newShapes.push_back(newShape);
97  newNumIndices.push_back(numVerts);
98  for (viskores::IdComponent j = 0; j < numVerts; ++j)
99  {
100  newConnectivity.push_back(connPortal.Get(connIdx++));
101  }
102  permutationVec.push_back(i);
103  break;
104  }
105  case CELL_SHAPE_POLY_VERTEX:
106  {
107  viskores::IdComponent numVerts = numInds;
108  for (viskores::IdComponent j = 0; j < numVerts; ++j)
109  {
110  newShapes.push_back(viskores::CELL_SHAPE_VERTEX);
111  newNumIndices.push_back(1);
112  newConnectivity.push_back(connPortal.Get(connIdx));
113  permutationVec.push_back(i);
114  ++connIdx;
115  }
116  break;
117  }
119  {
120  viskores::IdComponent numLines = numInds - 1;
121  for (viskores::IdComponent j = 0; j < numLines; ++j)
122  {
123  newShapes.push_back(viskores::CELL_SHAPE_LINE);
124  newNumIndices.push_back(2);
125  newConnectivity.push_back(connPortal.Get(connIdx));
126  newConnectivity.push_back(connPortal.Get(connIdx + 1));
127  permutationVec.push_back(i);
128  ++connIdx;
129  }
130  connIdx += 1;
131  break;
132  }
133  case CELL_SHAPE_TRIANGLE_STRIP:
134  {
135  viskores::IdComponent numTris = numInds - 2;
136  for (viskores::IdComponent j = 0; j < numTris; ++j)
137  {
138  newShapes.push_back(viskores::CELL_SHAPE_TRIANGLE);
139  newNumIndices.push_back(3);
140  if (j % 2)
141  {
142  newConnectivity.push_back(connPortal.Get(connIdx));
143  newConnectivity.push_back(connPortal.Get(connIdx + 1));
144  newConnectivity.push_back(connPortal.Get(connIdx + 2));
145  }
146  else
147  {
148  newConnectivity.push_back(connPortal.Get(connIdx + 2));
149  newConnectivity.push_back(connPortal.Get(connIdx + 1));
150  newConnectivity.push_back(connPortal.Get(connIdx));
151  }
152  permutationVec.push_back(i);
153  ++connIdx;
154  }
155  connIdx += 2;
156  break;
157  }
158  case CELL_SHAPE_PIXEL:
159  {
160  newShapes.push_back(viskores::CELL_SHAPE_QUAD);
161  newNumIndices.push_back(numInds);
162  newConnectivity.push_back(connPortal.Get(connIdx + 0));
163  newConnectivity.push_back(connPortal.Get(connIdx + 1));
164  newConnectivity.push_back(connPortal.Get(connIdx + 3));
165  newConnectivity.push_back(connPortal.Get(connIdx + 2));
166  permutationVec.push_back(i);
167  connIdx += 4;
168  break;
169  }
170  case CELL_SHAPE_VOXEL:
171  {
172  newShapes.push_back(viskores::CELL_SHAPE_HEXAHEDRON);
173  newNumIndices.push_back(numInds);
174  newConnectivity.push_back(connPortal.Get(connIdx + 0));
175  newConnectivity.push_back(connPortal.Get(connIdx + 1));
176  newConnectivity.push_back(connPortal.Get(connIdx + 3));
177  newConnectivity.push_back(connPortal.Get(connIdx + 2));
178  newConnectivity.push_back(connPortal.Get(connIdx + 4));
179  newConnectivity.push_back(connPortal.Get(connIdx + 5));
180  newConnectivity.push_back(connPortal.Get(connIdx + 7));
181  newConnectivity.push_back(connPortal.Get(connIdx + 6));
182  permutationVec.push_back(i);
183  connIdx += 8;
184  break;
185  }
186  default:
187  {
188  throw viskores::io::ErrorIO("Encountered unsupported cell type");
189  }
190  }
191  }
192 
193  if (newShapes.size() == static_cast<std::size_t>(shapes.GetNumberOfValues()))
194  {
195  permutationVec.clear();
196  }
197  else
198  {
199  permutation.Allocate(static_cast<viskores::Id>(permutationVec.size()));
200  std::copy(permutationVec.begin(),
201  permutationVec.end(),
203  }
204 
205  shapes.Allocate(static_cast<viskores::Id>(newShapes.size()));
206  std::copy(newShapes.begin(),
207  newShapes.end(),
209  numIndices.Allocate(static_cast<viskores::Id>(newNumIndices.size()));
210  std::copy(newNumIndices.begin(),
211  newNumIndices.end(),
213  connectivity.Allocate(static_cast<viskores::Id>(newConnectivity.size()));
214  std::copy(newConnectivity.begin(),
215  newConnectivity.end(),
217 }
218 
219 inline bool IsSingleShape(const viskores::cont::ArrayHandle<viskores::UInt8>& shapes)
220 {
221  if (shapes.GetNumberOfValues() < 1)
222  {
223  // If the data has no cells, is it single shape? That would make sense, but having
224  // a single shape cell set requires you to slect a shape, and there are no cells to
225  // make that selection from. We could get around that, but it's easier just to treat
226  // it as a general explicit grid.
227  return false;
228  }
229 
230  auto shapesPortal = shapes.ReadPortal();
231  viskores::UInt8 shape0 = shapesPortal.Get(0);
232  for (viskores::Id i = 1; i < shapes.GetNumberOfValues(); ++i)
233  {
234  if (shapesPortal.Get(i) != shape0)
235  return false;
236  }
237 
238  return true;
239 }
240 }
241 }
242 } // viskores::io::internal
243 
244 #endif // viskores_io_internal_VTKDataSetCells_h
viskores::CELL_SHAPE_PYRAMID
@ CELL_SHAPE_PYRAMID
A pyramid with a quadrilateral base and four triangular faces.0.
Definition: CellShape.h:76
viskores::CELL_SHAPE_POLYGON
@ CELL_SHAPE_POLYGON
A general polygon shape.
Definition: CellShape.h:61
ArrayHandle.h
viskores::cont::ArrayHandle::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:447
Types.h
ArrayPortalToIterators.h
viskores::cont::ArrayHandle< viskores::Id >
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::CELL_SHAPE_HEXAHEDRON
@ CELL_SHAPE_HEXAHEDRON
A hexahedron.
Definition: CellShape.h:70
CellShape.h
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores::CELL_SHAPE_WEDGE
@ CELL_SHAPE_WEDGE
A wedge.
Definition: CellShape.h:74
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::CELL_SHAPE_TRIANGLE
@ CELL_SHAPE_TRIANGLE
A triangle.
Definition: CellShape.h:56
viskores::io::ErrorIO
This class is thrown when Viskores encounters an error with the file system.
Definition: ErrorIO.h:33
viskores::cont::ArrayPortalToIteratorBegin
viskores::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorBegin(const PortalType &portal)
Convenience function for converting an ArrayPortal to a begin iterator.
Definition: ArrayPortalToIterators.h:189
viskores::cont::ArrayHandle::Allocate
void Allocate(viskores::Id numberOfValues, viskores::CopyFlag preserve, viskores::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:504
ErrorIO.h
viskores::cont::ArrayHandle::GetNumberOfValues
viskores::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:482
viskores::CELL_SHAPE_TETRA
@ CELL_SHAPE_TETRA
A tetrahedron.
Definition: CellShape.h:67
viskores::cont::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:468
viskores::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:177
viskores::CELL_SHAPE_VERTEX
@ CELL_SHAPE_VERTEX
Vertex cells of a single point.
Definition: CellShape.h:47
viskores::CELL_SHAPE_QUAD
@ CELL_SHAPE_QUAD
A four-sided polygon.
Definition: CellShape.h:64
viskores::CELL_SHAPE_POLY_LINE
@ CELL_SHAPE_POLY_LINE
A sequence of line segments.
Definition: CellShape.h:54
viskores::CELL_SHAPE_LINE
@ CELL_SHAPE_LINE
A line cell connecting two points.
Definition: CellShape.h:50