DataSetBuilderExplicit.h 14.8 KB
Newer Older
Dave Pugmire's avatar
Dave Pugmire committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt 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.
//
//  Copyright 2015 Sandia Corporation.
//  Copyright 2015 UT-Battelle, LLC.
//  Copyright 2015 Los Alamos National Security.
//
//  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
#ifndef vtk_m_cont_DataSetBuilderExplicit_h
#define vtk_m_cont_DataSetBuilderExplicit_h

23
#include <vtkm/cont/ArrayHandleCompositeVector.h>
24
25
26
#include <vtkm/cont/ArrayPortalToIterators.h>
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/cont/DataSet.h>
Dave Pugmire's avatar
Dave Pugmire committed
27

28
29
30
31
namespace vtkm
{
namespace cont
{
Dave Pugmire's avatar
Dave Pugmire committed
32
33
34
35
36
37

//Coordinates builder??
//Need a singlecellset handler.

class DataSetBuilderExplicit
{
38
39
  template <typename T>
  VTKM_CONT static void CopyInto(const std::vector<T>& input, vtkm::cont::ArrayHandle<T>& output)
40
  {
41
42
    output.Allocate(static_cast<vtkm::Id>(input.size()));
    std::copy(input.begin(), input.end(), ArrayPortalToIteratorBegin(output.GetPortalControl()));
43
  }
44

Dave Pugmire's avatar
Dave Pugmire committed
45
public:
46
  VTKM_CONT
47
48
49
50
51
52
  DataSetBuilderExplicit() {}

  //Single cell explicits.
  //TODO

  //Zoo explicit cell
53
54
55
56
57
58
59
  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<T>& xVals,
                                              const std::vector<vtkm::UInt8>& shapes,
                                              const std::vector<vtkm::IdComponent>& numIndices,
                                              const std::vector<vtkm::Id>& connectivity,
                                              const std::string& coordsNm = "coords",
                                              const std::string& cellNm = "cells")
60
  {
61
62
63
    std::vector<T> yVals(xVals.size(), 0), zVals(xVals.size(), 0);
    return DataSetBuilderExplicit::Create(xVals, yVals, zVals, shapes, numIndices, connectivity,
                                          coordsNm, cellNm);
64
  }
65
66
67
68
69
70
71
72
73

  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<T>& xVals,
                                              const std::vector<T>& yVals,
                                              const std::vector<vtkm::UInt8>& shapes,
                                              const std::vector<vtkm::IdComponent>& numIndices,
                                              const std::vector<vtkm::Id>& connectivity,
                                              const std::string& coordsNm = "coords",
                                              const std::string& cellNm = "cells")
74
  {
75
76
77
    std::vector<T> zVals(xVals.size(), 0);
    return DataSetBuilderExplicit::Create(xVals, yVals, zVals, shapes, numIndices, connectivity,
                                          coordsNm, cellNm);
78
79
  }

80
81
82
83
84
85
86
87
88
89
90
91
92
93
  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet Create(
    const std::vector<T>& xVals, const std::vector<T>& yVals, const std::vector<T>& zVals,
    const std::vector<vtkm::UInt8>& shapes, const std::vector<vtkm::IdComponent>& numIndices,
    const std::vector<vtkm::Id>& connectivity, const std::string& coordsNm = "coords",
    const std::string& cellNm = "cells");

  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet Create(
    const vtkm::cont::ArrayHandle<T>& xVals, const vtkm::cont::ArrayHandle<T>& yVals,
    const vtkm::cont::ArrayHandle<T>& zVals, const vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes,
    const vtkm::cont::ArrayHandle<vtkm::IdComponent>& numIndices,
    const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity, const std::string& coordsNm = "coords",
    const std::string& cellNm = "cells")
94
  {
95
96
    return DataSetBuilderExplicit::BuildDataSet(xVals, yVals, zVals, shapes, numIndices,
                                                connectivity, coordsNm, cellNm);
97
98
  }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<vtkm::Vec<T, 3>>& coords,
                                              const std::vector<vtkm::UInt8>& shapes,
                                              const std::vector<vtkm::IdComponent>& numIndices,
                                              const std::vector<vtkm::Id>& connectivity,
                                              const std::string& coordsNm = "coords",
                                              const std::string& cellNm = "cells");

  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet Create(
    const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>>& coords,
    const vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes,
    const vtkm::cont::ArrayHandle<vtkm::IdComponent>& numIndices,
    const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity, const std::string& coordsNm = "coords",
    const std::string& cellNm = "cells")
114
  {
115
    return DataSetBuilderExplicit::BuildDataSet(coords, shapes, numIndices, connectivity, coordsNm,
116
                                                cellNm);
117
118
  }

119
120
121
122
123
124
125
126
127
128
129
130
131
  template <typename T, typename CellShapeTag>
  VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<vtkm::Vec<T, 3>>& coords,
                                              CellShapeTag tag,
                                              vtkm::IdComponent numberOfPointsPerCell,
                                              const std::vector<vtkm::Id>& connectivity,
                                              const std::string& coordsNm = "coords",
                                              const std::string& cellNm = "cells");

  template <typename T, typename CellShapeTag>
  VTKM_CONT static vtkm::cont::DataSet Create(
    const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>>& coords, CellShapeTag tag,
    vtkm::IdComponent numberOfPointsPerCell, const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity,
    const std::string& coordsNm = "coords", const std::string& cellNm = "cells")
132
  {
133
134
    return DataSetBuilderExplicit::BuildDataSet(coords, tag, numberOfPointsPerCell, connectivity,
                                                coordsNm, cellNm);
135
  }
136

137
private:
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  template <typename T>
  static vtkm::cont::DataSet BuildDataSet(
    const vtkm::cont::ArrayHandle<T>& X, const vtkm::cont::ArrayHandle<T>& Y,
    const vtkm::cont::ArrayHandle<T>& Z, const vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes,
    const vtkm::cont::ArrayHandle<vtkm::IdComponent>& numIndices,
    const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity, const std::string& coordsNm,
    const std::string& cellNm);

  template <typename T>
  VTKM_CONT static vtkm::cont::DataSet BuildDataSet(
    const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>>& coords,
    const vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes,
    const vtkm::cont::ArrayHandle<vtkm::IdComponent>& numIndices,
    const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity, const std::string& coordsNm,
    const std::string& cellNm);

  template <typename T, typename CellShapeTag>
  VTKM_CONT static vtkm::cont::DataSet BuildDataSet(
    const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>>& coords, CellShapeTag tag,
    vtkm::IdComponent numberOfPointsPerCell, const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity,
    const std::string& coordsNm, const std::string& cellNm);
159
};
dpugmire's avatar
dpugmire committed
160

161
162
163
164
165
template <typename T>
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicit::Create(
  const std::vector<T>& xVals, const std::vector<T>& yVals, const std::vector<T>& zVals,
  const std::vector<vtkm::UInt8>& shapes, const std::vector<vtkm::IdComponent>& numIndices,
  const std::vector<vtkm::Id>& connectivity, const std::string& coordsNm, const std::string& cellNm)
Dave Pugmire's avatar
Dave Pugmire committed
166
{
167
  VTKM_ASSERT(xVals.size() == yVals.size() && yVals.size() == zVals.size() && xVals.size() > 0);
168
169

  vtkm::cont::ArrayHandle<T> Xc, Yc, Zc;
170
171
172
  DataSetBuilderExplicit::CopyInto(xVals, Xc);
  DataSetBuilderExplicit::CopyInto(yVals, Yc);
  DataSetBuilderExplicit::CopyInto(zVals, Zc);
173
174
175
176

  vtkm::cont::ArrayHandle<vtkm::UInt8> Sc;
  vtkm::cont::ArrayHandle<vtkm::IdComponent> Nc;
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
177
178
179
  DataSetBuilderExplicit::CopyInto(shapes, Sc);
  DataSetBuilderExplicit::CopyInto(numIndices, Nc);
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
180

181
  return DataSetBuilderExplicit::BuildDataSet(Xc, Yc, Zc, Sc, Nc, Cc, coordsNm, cellNm);
Dave Pugmire's avatar
Dave Pugmire committed
182
183
}

184
185
186
187
188
189
190
template <typename T>
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicit::BuildDataSet(
  const vtkm::cont::ArrayHandle<T>& X, const vtkm::cont::ArrayHandle<T>& Y,
  const vtkm::cont::ArrayHandle<T>& Z, const vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes,
  const vtkm::cont::ArrayHandle<vtkm::IdComponent>& numIndices,
  const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity, const std::string& coordsNm,
  const std::string& cellNm)
Dave Pugmire's avatar
Dave Pugmire committed
191
{
192
  VTKM_ASSERT(X.GetNumberOfValues() == Y.GetNumberOfValues() &&
193
194
              Y.GetNumberOfValues() == Z.GetNumberOfValues() && X.GetNumberOfValues() > 0 &&
              shapes.GetNumberOfValues() == numIndices.GetNumberOfValues());
195
196
197

  vtkm::cont::DataSet dataSet;
  dataSet.AddCoordinateSystem(
198
    vtkm::cont::CoordinateSystem(coordsNm, make_ArrayHandleCompositeVector(X, 0, Y, 0, Z, 0)));
199
  vtkm::Id nPts = X.GetNumberOfValues();
200
  vtkm::cont::CellSetExplicit<> cellSet(cellNm);
201

202
  cellSet.Fill(nPts, shapes, numIndices, connectivity);
203
204
205
  dataSet.AddCellSet(cellSet);

  return dataSet;
Dave Pugmire's avatar
Dave Pugmire committed
206
207
}

208
209
210
211
212
template <typename T>
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicit::Create(
  const std::vector<vtkm::Vec<T, 3>>& coords, const std::vector<vtkm::UInt8>& shapes,
  const std::vector<vtkm::IdComponent>& numIndices, const std::vector<vtkm::Id>& connectivity,
  const std::string& coordsNm, const std::string& cellNm)
Dave Pugmire's avatar
Dave Pugmire committed
213
{
214
  vtkm::cont::ArrayHandle<Vec<T, 3>> coordsArray;
215
  DataSetBuilderExplicit::CopyInto(coords, coordsArray);
216

217
218
219
  vtkm::cont::ArrayHandle<vtkm::UInt8> Sc;
  vtkm::cont::ArrayHandle<vtkm::IdComponent> Nc;
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
220
221
222
  DataSetBuilderExplicit::CopyInto(shapes, Sc);
  DataSetBuilderExplicit::CopyInto(numIndices, Nc);
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
223

224
  return DataSetBuilderExplicit::Create(coordsArray, Sc, Nc, Cc, coordsNm, cellNm);
225
}
226

227
228
229
230
231
232
233
template <typename T>
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicit::BuildDataSet(
  const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>>& coords,
  const vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes,
  const vtkm::cont::ArrayHandle<vtkm::IdComponent>& numIndices,
  const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity, const std::string& coordsNm,
  const std::string& cellNm)
234
{
235
  vtkm::cont::DataSet dataSet;
236

237
  dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem(coordsNm, coords));
238
  vtkm::Id nPts = static_cast<vtkm::Id>(coords.GetNumberOfValues());
239
  vtkm::cont::CellSetExplicit<> cellSet(cellNm);
240

241
  cellSet.Fill(nPts, shapes, numIndices, connectivity);
242
  dataSet.AddCellSet(cellSet);
243

244
  return dataSet;
Dave Pugmire's avatar
Dave Pugmire committed
245
246
}

247
248
249
250
251
template <typename T, typename CellShapeTag>
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicit::Create(
  const std::vector<vtkm::Vec<T, 3>>& coords, CellShapeTag tag,
  vtkm::IdComponent numberOfPointsPerCell, const std::vector<vtkm::Id>& connectivity,
  const std::string& coordsNm, const std::string& cellNm)
252
{
253
  vtkm::cont::ArrayHandle<Vec<T, 3>> coordsArray;
254
  DataSetBuilderExplicit::CopyInto(coords, coordsArray);
255

256
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
257
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
258

259
260
  return DataSetBuilderExplicit::Create(coordsArray, tag, numberOfPointsPerCell, Cc, coordsNm,
                                        cellNm);
261
262
}

263
264
265
266
267
template <typename T, typename CellShapeTag>
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicit::BuildDataSet(
  const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>>& coords, CellShapeTag tag,
  vtkm::IdComponent numberOfPointsPerCell, const vtkm::cont::ArrayHandle<vtkm::Id>& connectivity,
  const std::string& coordsNm, const std::string& cellNm)
268
{
269
  vtkm::cont::DataSet dataSet;
270

271
  dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem(coordsNm, coords));
272
  vtkm::cont::CellSetSingleType<> cellSet(cellNm);
273

274
  cellSet.Fill(coords.GetNumberOfValues(), tag.Id, numberOfPointsPerCell, connectivity);
275
  dataSet.AddCellSet(cellSet);
276

277
  return dataSet;
278
279
}

280
class DataSetBuilderExplicitIterative
281
282
{
public:
283
  VTKM_CONT
284
285
  DataSetBuilderExplicitIterative() {}

286
  VTKM_CONT
287
  void Begin(const std::string& coordName = "coords", const std::string& cellName = "cells")
288
289
290
  {
    this->coordNm = coordName;
    this->cellNm = cellName;
291
292
293
294
    this->points.resize(0);
    this->shapes.resize(0);
    this->numIdx.resize(0);
    this->connectivity.resize(0);
295
296
297
  }

  //Define points.
298
  VTKM_CONT
299
300
  vtkm::cont::DataSet Create();

301
  VTKM_CONT
302
  vtkm::Id AddPoint(const vtkm::Vec<vtkm::Float32, 3>& pt)
303
304
305
306
307
308
  {
    points.push_back(pt);
    vtkm::Id id = static_cast<vtkm::Id>(points.size());
    return id;
  }

309
  VTKM_CONT
310
  vtkm::Id AddPoint(const vtkm::Float32& x, const vtkm::Float32& y, const vtkm::Float32& z = 0)
311
  {
312
    points.push_back(vtkm::make_Vec(x, y, z));
313
314
315
316
    vtkm::Id id = static_cast<vtkm::Id>(points.size());
    return id;
  }

317
318
  template <typename T>
  VTKM_CONT vtkm::Id AddPoint(const T& x, const T& y, const T& z = 0)
319
  {
320
321
    return AddPoint(static_cast<vtkm::Float32>(x), static_cast<vtkm::Float32>(y),
                    static_cast<vtkm::Float32>(z));
322
323
  }

324
325
  template <typename T>
  VTKM_CONT vtkm::Id AddPoint(const vtkm::Vec<T, 3>& pt)
326
  {
327
    return AddPoint(static_cast<vtkm::Vec<vtkm::Float32, 3>>(pt));
328
329
330
  }

  //Define cells.
331
  VTKM_CONT
332
333
334
335
336
337
  void AddCell(vtkm::UInt8 shape)
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(0);
  }

338
  VTKM_CONT
339
  void AddCell(const vtkm::UInt8& shape, const std::vector<vtkm::Id>& conn)
340
341
342
343
344
345
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(static_cast<vtkm::IdComponent>(conn.size()));
    connectivity.insert(connectivity.end(), conn.begin(), conn.end());
  }

346
  VTKM_CONT
347
  void AddCell(const vtkm::UInt8& shape, const vtkm::Id* conn, const vtkm::IdComponent& n)
348
349
350
351
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(n);
    for (int i = 0; i < n; i++)
352
353
354
    {
      connectivity.push_back(conn[i]);
    }
355
356
  }

357
  VTKM_CONT
358
359
  void AddCellPoint(vtkm::Id pointIndex)
  {
360
    VTKM_ASSERT(this->numIdx.size() > 0);
361
362
363
    this->connectivity.push_back(pointIndex);
    this->numIdx.back() += 1;
  }
364

365
private:
366
  std::string coordNm, cellNm;
367

368
  std::vector<vtkm::Vec<vtkm::Float32, 3>> points;
369
370
371
  std::vector<vtkm::UInt8> shapes;
  std::vector<vtkm::IdComponent> numIdx;
  std::vector<vtkm::Id> connectivity;
372
373
};

374
inline VTKM_CONT vtkm::cont::DataSet DataSetBuilderExplicitIterative::Create()
375
{
376
  DataSetBuilderExplicit dsb;
377
  return dsb.Create(points, shapes, numIdx, connectivity, coordNm, cellNm);
378
}
Dave Pugmire's avatar
Dave Pugmire committed
379
380
381
382
}
}

#endif //vtk_m_cont_DataSetBuilderExplicit_h