DataSetBuilderExplicit.h 17.3 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
#include <vtkm/cont/ArrayPortalToIterators.h>
Dave Pugmire's avatar
Dave Pugmire committed
25
#include <vtkm/cont/Assert.h>
26
27
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/cont/DataSet.h>
Dave Pugmire's avatar
Dave Pugmire committed
28
29
30
31
32
33
34
35
36

namespace vtkm {
namespace cont {

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

class DataSetBuilderExplicit
{
37
  template<typename T>
38
39
  VTKM_CONT_EXPORT
  static
40
41
42
43
44
45
46
  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
47
public:
48
49
50
51
52
53
54
55
56
  VTKM_CONT_EXPORT
  DataSetBuilderExplicit() {}

  //Single cell explicits.
  //TODO

  //Zoo explicit cell
  template<typename T>
  VTKM_CONT_EXPORT
57
  static
58
59
60
61
62
63
64
65
66
67
  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);
68
69
    return DataSetBuilderExplicit::Create(xVals,yVals,zVals,
                                          shapes,numIndices,connectivity,
70
                                          coordsNm,cellNm);
71
72
73
74
  }

  template<typename T>
  VTKM_CONT_EXPORT
75
  static
76
77
78
79
80
81
82
83
84
85
86
87
  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_EXPORT
88
  static
89
90
91
92
93
94
95
96
97
98
  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")
  {
99
100
101
    return DataSetBuilderExplicit::BuildDataSet(
          xVals,yVals,zVals,
          shapes,numIndices,connectivity,
102
          coordsNm,cellNm);
103
104
105
106
107
  }


  template<typename T>
  VTKM_CONT_EXPORT
108
  static
109
110
111
112
113
114
115
116
117
118
  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_EXPORT
119
  static
120
121
122
123
124
125
126
127
  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")
  {
128
129
130
131
132
133
    return DataSetBuilderExplicit::BuildDataSet(coords,
                                                shapes,
                                                numIndices,
                                                connectivity,
                                                coordsNm,
                                                cellNm);
134
135
136
137
  }

  template<typename T, typename CellShapeTag>
  VTKM_CONT_EXPORT
138
  static
139
140
141
142
143
144
145
146
147
  vtkm::cont::DataSet
  Create(const std::vector<vtkm::Vec<T,3> > &coords,
         CellShapeTag tag,
         const std::vector<vtkm::Id> &connectivity,
         const std::string &coordsNm="coords",
         const std::string &cellNm="cells");

  template<typename T, typename CellShapeTag>
  VTKM_CONT_EXPORT
148
  static
149
150
151
152
153
154
155
  vtkm::cont::DataSet
  Create(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
         CellShapeTag tag,
         const vtkm::cont::ArrayHandle<vtkm::Id> &connectivity,
         const std::string &coordsNm="coords",
         const std::string &cellNm="cells")
  {
156
157
158
159
160
    return DataSetBuilderExplicit::BuildDataSet(coords,
                                                tag,
                                                connectivity,
                                                coordsNm,
                                                cellNm);
161
  }
162

163
private:
164
  template<typename T>
165
  static
166
167
168
169
170
171
172
173
174
175
176
177
  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_EXPORT
178
  static
179
180
181
182
183
184
185
186
187
188
  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_EXPORT
189
  static
190
191
192
193
194
195
  vtkm::cont::DataSet
  BuildDataSet(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
               CellShapeTag tag,
               const vtkm::cont::ArrayHandle<vtkm::Id> &connectivity,
               const std::string &coordsNm,
               const std::string &cellNm);
196
};
dpugmire's avatar
dpugmire committed
197

Dave Pugmire's avatar
Dave Pugmire committed
198
199
200
201
template<typename T>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<T> &xVals,
                               const std::vector<T> &yVals,
202
                               const std::vector<T> &zVals,
Dave Pugmire's avatar
Dave Pugmire committed
203
204
205
206
207
208
                               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)
{
209
210
211
212
213
  VTKM_ASSERT_CONT(xVals.size() == yVals.size() &&
                   yVals.size() == zVals.size() &&
                   xVals.size() > 0);

  vtkm::cont::ArrayHandle<T> Xc, Yc, Zc;
214
215
216
  DataSetBuilderExplicit::CopyInto(xVals, Xc);
  DataSetBuilderExplicit::CopyInto(yVals, Yc);
  DataSetBuilderExplicit::CopyInto(zVals, Zc);
217
218
219
220

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

225
  return DataSetBuilderExplicit::BuildDataSet(
226
        Xc,Yc,Zc, Sc,Nc,Cc, coordsNm, cellNm);
Dave Pugmire's avatar
Dave Pugmire committed
227
228
229
230
}

template<typename T>
vtkm::cont::DataSet
231
232
233
234
235
236
237
238
239
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
240
{
241
242
243
244
245
246
247
  VTKM_ASSERT_CONT(X.GetNumberOfValues() == Y.GetNumberOfValues() &&
                  Y.GetNumberOfValues() == Z.GetNumberOfValues() &&
                  X.GetNumberOfValues() > 0 &&
                  shapes.GetNumberOfValues() == numIndices.GetNumberOfValues());

  vtkm::cont::DataSet dataSet;
  dataSet.AddCoordinateSystem(
248
      vtkm::cont::CoordinateSystem(coordsNm,
249
250
      make_ArrayHandleCompositeVector(X,0, Y,0, Z,0)));
  vtkm::Id nPts = X.GetNumberOfValues();
251
  vtkm::cont::CellSetExplicit<> cellSet(nPts, cellNm);
252
253
254
255
256

  cellSet.Fill(shapes, numIndices, connectivity);
  dataSet.AddCellSet(cellSet);

  return dataSet;
Dave Pugmire's avatar
Dave Pugmire committed
257
258
259
260
261
262
263
264
265
266
267
}

template<typename T>
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)
{
268
  vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
269
  DataSetBuilderExplicit::CopyInto(coords, coordsArray);
270

271
272
273
  vtkm::cont::ArrayHandle<vtkm::UInt8> Sc;
  vtkm::cont::ArrayHandle<vtkm::IdComponent> Nc;
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
274
275
276
  DataSetBuilderExplicit::CopyInto(shapes, Sc);
  DataSetBuilderExplicit::CopyInto(numIndices, Nc);
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
277

278
  return DataSetBuilderExplicit::Create(
279
        coordsArray, Sc, Nc, Cc, coordsNm, cellNm);
280
}
281

282
283
284
285
template<typename T>
VTKM_CONT_EXPORT
vtkm::cont::DataSet
DataSetBuilderExplicit::BuildDataSet(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
286
287
288
289
290
                     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)
291
{
292
  vtkm::cont::DataSet dataSet;
293

294
  dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem(coordsNm,
295
                               coords));
296
  vtkm::Id nPts = static_cast<vtkm::Id>(coords.GetNumberOfValues());
297
  vtkm::cont::CellSetExplicit<> cellSet(nPts, cellNm);
298

299
300
  cellSet.Fill(shapes, numIndices, connectivity);
  dataSet.AddCellSet(cellSet);
301

302
  return dataSet;
Dave Pugmire's avatar
Dave Pugmire committed
303
304
}

305
306
307
308
309
310
311
312
template<typename T, typename CellShapeTag>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<vtkm::Vec<T,3> > &coords,
                               CellShapeTag tag,
                               const std::vector<vtkm::Id> &connectivity,
                               const std::string &coordsNm,
                               const std::string &cellNm)
{
313
  vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
314
  DataSetBuilderExplicit::CopyInto(coords, coordsArray);
315

316
  vtkm::cont::ArrayHandle<vtkm::Id> Cc;
317
  DataSetBuilderExplicit::CopyInto(connectivity, Cc);
318

319
  return DataSetBuilderExplicit::Create(coordsArray, tag, Cc, coordsNm, cellNm);
320
321
322
323
324
325
326
327
328
329
330
}

template<typename T, typename CellShapeTag>
VTKM_CONT_EXPORT
vtkm::cont::DataSet
DataSetBuilderExplicit::BuildDataSet(const vtkm::cont::ArrayHandle<vtkm::Vec<T,3> > &coords,
                                     CellShapeTag tag,
                                     const vtkm::cont::ArrayHandle<vtkm::Id> &connectivity,
                                     const std::string &coordsNm,
                                     const std::string &cellNm)
{
331
  vtkm::cont::DataSet dataSet;
332

333
  dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem(coordsNm, coords));
334
  vtkm::cont::CellSetSingleType<> cellSet(tag, cellNm);
335

336
337
  cellSet.Fill(connectivity);
  dataSet.AddCellSet(cellSet);
338

339
  return dataSet;
340
341
342
}


343
class DataSetBuilderExplicitIterative
344
345
{
public:
346
347
348
349
  VTKM_CONT_EXPORT
  DataSetBuilderExplicitIterative() {}

  VTKM_CONT_EXPORT
350
  void Begin(const std::string &coordName="coords",
351
352
353
354
             const std::string &cellName="cells")
  {
    this->coordNm = coordName;
    this->cellNm = cellName;
355
356
357
358
    this->points.resize(0);
    this->shapes.resize(0);
    this->numIdx.resize(0);
    this->connectivity.resize(0);
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
  }

  //Define points.
  VTKM_CONT_EXPORT
  vtkm::cont::DataSet Create();

  VTKM_CONT_EXPORT
  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;
  }

  VTKM_CONT_EXPORT
  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>
  VTKM_CONT_EXPORT
  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>
  VTKM_CONT_EXPORT
  vtkm::Id AddPoint(const vtkm::Vec<T,3> &pt)
  {
    return AddPoint(static_cast<vtkm::Vec<vtkm::Float32,3> >(pt));
  }

  //Define cells.
  VTKM_CONT_EXPORT
  void AddCell(vtkm::UInt8 shape)
  {
    this->shapes.push_back(shape);
    this->numIdx.push_back(0);
  }

  VTKM_CONT_EXPORT
  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());
  }

  VTKM_CONT_EXPORT
  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]);
      }
  }

  VTKM_CONT_EXPORT
  void AddCellPoint(vtkm::Id pointIndex)
  {
    VTKM_ASSERT_CONT(this->numIdx.size() > 0);
    this->connectivity.push_back(pointIndex);
    this->numIdx.back() += 1;
  }
433

434
private:
435
  std::string coordNm, cellNm;
436

437
438
439
440
  std::vector<vtkm::Vec<vtkm::Float32,3> > points;
  std::vector<vtkm::UInt8> shapes;
  std::vector<vtkm::IdComponent> numIdx;
  std::vector<vtkm::Id> connectivity;
441
442
443
};

vtkm::cont::DataSet
444
DataSetBuilderExplicitIterative::Create()
445
{
446
  DataSetBuilderExplicit dsb;
447
  return dsb.Create(points, shapes, numIdx, connectivity, coordNm, cellNm);
448
449
}

Dave Pugmire's avatar
Dave Pugmire committed
450

451
452
453
454
455

#if 0
template<typename T, typename CellType>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<T> &xVals,
456
457
458
459
             const std::vector<T> &yVals,
             const std::vector<vtkm::Id> &connectivity,
             const std::string &coordsNm,
             const std::string &cellNm)
460
{
461
462
463
464
465
466
467
468
469
470
471
472
473
  VTKM_CONT_ASSERT(xVals.size() == yVals.size() && xVals.size() > 0);
  vtkm::cont::DataSet dataSet;

  typedef vtkm::Vec<vtkm::Float32,3> CoordType;
  std::vector<CoordType> coords(xVals.size());

  for (size_t i=0; i < coords.size(); i++)
  {
    coords[i][0] = xVals[i];
    coords[i][1] = yVals[i];
    coords[i][2] = 0;
  }
  dataSet.AddCoordinateSystem(
474
      vtkm::cont::CoordinateSystem(coordsNm, coords));
475
476
477
478
479
480

  vtkm::cont::CellSetSingleType< > cellSet(CellType(), cellNm);
  cellSet.FillViaCopy(connectivity);
  dataSet.AddCellSet(cellSet);

  return dataSet;
481
482
483
484
485
}

template<typename T, typename CellType>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<T> &xVals,
486
487
488
489
490
             const std::vector<T> &yVals,
             const std::vector<T> &zVals,
             const std::vector<vtkm::Id> &connectivity,
             const std::string &coordsNm,
             const std::string &cellNm)
491
{
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  VTKM_CONT_ASSERT(xVals.size() == yVals.size() &&
                   yVals.size() == zVals.size() &&
                   xVals.size() > 0);
  vtkm::cont::DataSet dataSet;

  typedef vtkm::Vec<vtkm::Float32,3> CoordType;
  std::vector<CoordType> coords(xVals.size());

  vtkm::Id nPts = static_cast<vtkm::Id>(coords.size());
  for (vtkm::Id i=0; i < nPts; i++)
  {
    coords[i][0] = xVals[i];
    coords[i][1] = yVals[i];
    coords[i][2] = zVals[i];
  }
  dataSet.AddCoordinateSystem(
508
      vtkm::cont::CoordinateSystem(coordsNm, coords));
509
510
511
512
513
514

  vtkm::cont::CellSetSingleType< > cellSet(CellType(), cellNm);
  cellSet.FillViaCopy(connectivity);
  dataSet.AddCellSet(cellSet);

  return dataSet;
515
516
517
518
519
520
521
522
523
}

template<typename T, typename CellType>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<vtkm::Vec<T,3> > &coords,
                               const std::vector<vtkm::Id> &connectivity,
                               const std::string &coordsNm,
                               const std::string &cellNm)
{
524
  vtkm::cont::DataSet dataSet;
525

526
527
  vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
  CopyInto(coords, coordsArray);
528

529
  dataSet.AddCoordinateSystem(
530
      vtkm::cont::CoordinateSystem(coordsNm, coordsArray));
531

532
533
534
  vtkm::cont::CellSetSingleType< > cellSet(CellType(), cellNm);
  cellSet.FillViaCopy(connectivity);
  dataSet.AddCellSet(cellSet);
535

536
  return dataSet;
537
538
539
}
#endif

Dave Pugmire's avatar
Dave Pugmire committed
540
541
542
543
}
}

#endif //vtk_m_cont_DataSetBuilderExplicit_h