DataSetBuilderExplicit.h 15.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
32
33
34
35

namespace vtkm {
namespace cont {

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

class DataSetBuilderExplicit
{
36
  template<typename T>
37
  VTKM_CONT
38
  static
39
40
41
42
43
44
45
  void CopyInto(const std::vector<T>& input,
                vtkm::cont::ArrayHandle<T>& output )
  {
    output.Allocate( static_cast<vtkm::Id>(input.size()) );
    std::copy( input.begin(), input.end(),
               ArrayPortalToIteratorBegin(output.GetPortalControl()) );
  }
Dave Pugmire's avatar
Dave Pugmire committed
46
public:
47
  VTKM_CONT
48
49
50
51
52
53
  DataSetBuilderExplicit() {}

  //Single cell explicits.
  //TODO

  //Zoo explicit cell
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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")
  {
    std::vector<T> yVals(xVals.size(),0), zVals(xVals.size(),0);
    return DataSetBuilderExplicit::Create(xVals,yVals,zVals,
                                          shapes,numIndices,connectivity,
                                          coordsNm,cellNm);
  }
    
71
  template<typename T>
72
  VTKM_CONT
73
  static
74
75
76
77
78
79
80
81
82
83
  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")
  {
    std::vector<T> zVals(xVals.size(),0);
84
85
    return DataSetBuilderExplicit::Create(xVals,yVals,zVals,
                                          shapes,numIndices,connectivity,
86
                                          coordsNm,cellNm);
87
88
89
  }

  template<typename T>
90
  VTKM_CONT
91
  static
92
93
94
95
96
97
98
99
100
101
102
  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>
103
  VTKM_CONT
104
  static
105
106
107
108
109
110
111
112
113
114
  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")
  {
115
116
117
    return DataSetBuilderExplicit::BuildDataSet(
          xVals,yVals,zVals,
          shapes,numIndices,connectivity,
118
          coordsNm,cellNm);
119
120
121
122
  }


  template<typename T>
123
  VTKM_CONT
124
  static
125
126
127
128
129
130
131
132
133
  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>
134
  VTKM_CONT
135
  static
136
137
138
139
140
141
142
143
  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")
  {
144
145
146
147
148
149
    return DataSetBuilderExplicit::BuildDataSet(coords,
                                                shapes,
                                                numIndices,
                                                connectivity,
                                                coordsNm,
                                                cellNm);
150
151
152
  }

  template<typename T, typename CellShapeTag>
153
  VTKM_CONT
154
  static
155
156
157
  vtkm::cont::DataSet
  Create(const std::vector<vtkm::Vec<T,3> > &coords,
         CellShapeTag tag,
158
         vtkm::IdComponent numberOfPointsPerCell,
159
160
161
162
163
         const std::vector<vtkm::Id> &connectivity,
         const std::string &coordsNm="coords",
         const std::string &cellNm="cells");

  template<typename T, typename CellShapeTag>
164
  VTKM_CONT
165
  static
166
167
168
  vtkm::cont::DataSet
  Create(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
         CellShapeTag tag,
169
         vtkm::IdComponent numberOfPointsPerCell,
170
171
172
173
         const vtkm::cont::ArrayHandle<vtkm::Id> &connectivity,
         const std::string &coordsNm="coords",
         const std::string &cellNm="cells")
  {
174
175
    return DataSetBuilderExplicit::BuildDataSet(coords,
                                                tag,
176
                                                numberOfPointsPerCell,
177
178
179
                                                connectivity,
                                                coordsNm,
                                                cellNm);
180
  }
181

182
private:
183
  template<typename T>
184
  static
185
186
187
188
189
190
191
192
193
194
195
  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>
196
  VTKM_CONT
197
  static
198
199
200
201
202
203
204
205
206
  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>
207
  VTKM_CONT
208
  static
209
210
211
  vtkm::cont::DataSet
  BuildDataSet(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
               CellShapeTag tag,
212
               vtkm::IdComponent numberOfPointsPerCell,
213
214
215
               const vtkm::cont::ArrayHandle<vtkm::Id> &connectivity,
               const std::string &coordsNm,
               const std::string &cellNm);
216
};
dpugmire's avatar
dpugmire committed
217

Dave Pugmire's avatar
Dave Pugmire committed
218
template<typename T>
219
inline VTKM_CONT
Dave Pugmire's avatar
Dave Pugmire committed
220
221
222
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<T> &xVals,
                               const std::vector<T> &yVals,
223
                               const std::vector<T> &zVals,
Dave Pugmire's avatar
Dave Pugmire committed
224
225
226
227
228
229
                               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)
{
230
231
232
  VTKM_ASSERT(xVals.size() == yVals.size() &&
              yVals.size() == zVals.size() &&
              xVals.size() > 0);
233
234

  vtkm::cont::ArrayHandle<T> Xc, Yc, Zc;
235
236
237
  DataSetBuilderExplicit::CopyInto(xVals, Xc);
  DataSetBuilderExplicit::CopyInto(yVals, Yc);
  DataSetBuilderExplicit::CopyInto(zVals, Zc);
238
239
240
241

  vtkm::cont::ArrayHandle<vtkm::UInt8> Sc;
  vtkm::cont::ArrayHandle<vtkm::IdComponent> Nc;
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
242
243
244
  DataSetBuilderExplicit::CopyInto(shapes, Sc);
  DataSetBuilderExplicit::CopyInto(numIndices, Nc);
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
245

246
  return DataSetBuilderExplicit::BuildDataSet(
247
        Xc,Yc,Zc, Sc,Nc,Cc, coordsNm, cellNm);
Dave Pugmire's avatar
Dave Pugmire committed
248
249
250
}

template<typename T>
251
inline VTKM_CONT
Dave Pugmire's avatar
Dave Pugmire committed
252
vtkm::cont::DataSet
253
254
255
256
257
258
259
260
261
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
262
{
263
  VTKM_ASSERT(X.GetNumberOfValues() == Y.GetNumberOfValues() &&
264
265
266
267
268
269
                  Y.GetNumberOfValues() == Z.GetNumberOfValues() &&
                  X.GetNumberOfValues() > 0 &&
                  shapes.GetNumberOfValues() == numIndices.GetNumberOfValues());

  vtkm::cont::DataSet dataSet;
  dataSet.AddCoordinateSystem(
270
      vtkm::cont::CoordinateSystem(coordsNm,
271
272
      make_ArrayHandleCompositeVector(X,0, Y,0, Z,0)));
  vtkm::Id nPts = X.GetNumberOfValues();
273
  vtkm::cont::CellSetExplicit<> cellSet(cellNm);
274

275
  cellSet.Fill(nPts, shapes, numIndices, connectivity);
276
277
278
  dataSet.AddCellSet(cellSet);

  return dataSet;
Dave Pugmire's avatar
Dave Pugmire committed
279
280
281
}

template<typename T>
282
inline VTKM_CONT
Dave Pugmire's avatar
Dave Pugmire committed
283
284
285
286
287
288
289
290
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)
{
291
  vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
292
  DataSetBuilderExplicit::CopyInto(coords, coordsArray);
293

294
295
296
  vtkm::cont::ArrayHandle<vtkm::UInt8> Sc;
  vtkm::cont::ArrayHandle<vtkm::IdComponent> Nc;
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
297
298
299
  DataSetBuilderExplicit::CopyInto(shapes, Sc);
  DataSetBuilderExplicit::CopyInto(numIndices, Nc);
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
300

301
  return DataSetBuilderExplicit::Create(
302
        coordsArray, Sc, Nc, Cc, coordsNm, cellNm);
303
}
304

305
template<typename T>
306
inline VTKM_CONT
307
308
vtkm::cont::DataSet
DataSetBuilderExplicit::BuildDataSet(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
309
310
311
312
313
                     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)
314
{
315
  vtkm::cont::DataSet dataSet;
316

317
  dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem(coordsNm,
318
                               coords));
319
  vtkm::Id nPts = static_cast<vtkm::Id>(coords.GetNumberOfValues());
320
  vtkm::cont::CellSetExplicit<> cellSet(cellNm);
321

322
  cellSet.Fill(nPts, shapes, numIndices, connectivity);
323
  dataSet.AddCellSet(cellSet);
324

325
  return dataSet;
Dave Pugmire's avatar
Dave Pugmire committed
326
327
}

328
template<typename T, typename CellShapeTag>
329
inline VTKM_CONT
330
331
332
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<vtkm::Vec<T,3> > &coords,
                               CellShapeTag tag,
333
                               vtkm::IdComponent numberOfPointsPerCell,
334
335
336
337
                               const std::vector<vtkm::Id> &connectivity,
                               const std::string &coordsNm,
                               const std::string &cellNm)
{
338
  vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
339
  DataSetBuilderExplicit::CopyInto(coords, coordsArray);
340

341
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
342
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
343

344
345
  return DataSetBuilderExplicit::Create(
        coordsArray, tag, numberOfPointsPerCell, Cc, coordsNm, cellNm);
346
347
348
}

template<typename T, typename CellShapeTag>
349
inline VTKM_CONT
350
351
352
vtkm::cont::DataSet
DataSetBuilderExplicit::BuildDataSet(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
                                     CellShapeTag tag,
353
                                     vtkm::IdComponent numberOfPointsPerCell,
354
355
356
357
                                     const vtkm::cont::ArrayHandle<vtkm::Id> &connectivity,
                                     const std::string &coordsNm,
                                     const std::string &cellNm)
{
358
  vtkm::cont::DataSet dataSet;
359

360
  dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem(coordsNm, coords));
361
  vtkm::cont::CellSetSingleType<> cellSet(cellNm);
362

363
364
365
366
  cellSet.Fill(coords.GetNumberOfValues(),
               tag.Id,
               numberOfPointsPerCell,
               connectivity);
367
  dataSet.AddCellSet(cellSet);
368

369
  return dataSet;
370
371
372
}


373
class DataSetBuilderExplicitIterative
374
375
{
public:
376
  VTKM_CONT
377
378
  DataSetBuilderExplicitIterative() {}

379
  VTKM_CONT
380
  void Begin(const std::string &coordName="coords",
381
382
383
384
             const std::string &cellName="cells")
  {
    this->coordNm = coordName;
    this->cellNm = cellName;
385
386
387
388
    this->points.resize(0);
    this->shapes.resize(0);
    this->numIdx.resize(0);
    this->connectivity.resize(0);
389
390
391
  }

  //Define points.
392
  VTKM_CONT
393
394
  vtkm::cont::DataSet Create();

395
  VTKM_CONT
396
397
398
399
400
401
402
  vtkm::Id AddPoint(const vtkm::Vec<vtkm::Float32, 3> &pt)
  {
    points.push_back(pt);
    vtkm::Id id = static_cast<vtkm::Id>(points.size());
    return id;
  }

403
  VTKM_CONT
404
405
406
407
408
409
410
411
412
413
  vtkm::Id AddPoint(const vtkm::Float32 &x,
        const vtkm::Float32 &y,
        const vtkm::Float32 &z=0)
  {
    points.push_back(vtkm::make_Vec(x,y,z));
    vtkm::Id id = static_cast<vtkm::Id>(points.size());
    return id;
  }

  template<typename T>
414
  VTKM_CONT
415
416
417
418
419
420
421
422
  vtkm::Id AddPoint(const T &x, const T &y, const T &z=0)
  {
    return AddPoint(static_cast<vtkm::Float32>(x),
      static_cast<vtkm::Float32>(y),
      static_cast<vtkm::Float32>(z));
  }

  template<typename T>
423
  VTKM_CONT
424
425
426
427
428
429
  vtkm::Id AddPoint(const vtkm::Vec<T,3> &pt)
  {
    return AddPoint(static_cast<vtkm::Vec<vtkm::Float32,3> >(pt));
  }

  //Define cells.
430
  VTKM_CONT
431
432
433
434
435
436
  void AddCell(vtkm::UInt8 shape)
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(0);
  }

437
  VTKM_CONT
438
439
440
441
442
443
444
  void AddCell(const vtkm::UInt8 &shape, const std::vector<vtkm::Id> &conn)
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(static_cast<vtkm::IdComponent>(conn.size()));
    connectivity.insert(connectivity.end(), conn.begin(), conn.end());
  }

445
  VTKM_CONT
446
447
448
449
450
451
452
453
454
455
  void AddCell(const vtkm::UInt8 &shape, const vtkm::Id *conn, const vtkm::IdComponent &n)
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(n);
    for (int i = 0; i < n; i++)
      {
        connectivity.push_back(conn[i]);
      }
  }

456
  VTKM_CONT
457
458
  void AddCellPoint(vtkm::Id pointIndex)
  {
459
    VTKM_ASSERT(this->numIdx.size() > 0);
460
461
462
    this->connectivity.push_back(pointIndex);
    this->numIdx.back() += 1;
  }
463

464
private:
465
  std::string coordNm, cellNm;
466

467
468
469
470
  std::vector<vtkm::Vec<vtkm::Float32,3> > points;
  std::vector<vtkm::UInt8> shapes;
  std::vector<vtkm::IdComponent> numIdx;
  std::vector<vtkm::Id> connectivity;
471
472
};

473
inline VTKM_CONT
474
vtkm::cont::DataSet
475
DataSetBuilderExplicitIterative::Create()
476
{
477
  DataSetBuilderExplicit dsb;
478
  return dsb.Create(points, shapes, numIdx, connectivity, coordNm, cellNm);
479
480
}

Dave Pugmire's avatar
Dave Pugmire committed
481
482
483
484
485

}
}

#endif //vtk_m_cont_DataSetBuilderExplicit_h