DataSetBuilderExplicit.h 11.9 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
23
24
25
26
27
28
29
//============================================================================
//  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

#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/cont/Assert.h>

namespace vtkm {
namespace cont {

30
31
typedef vtkm::cont::DeviceAdapterAlgorithm<VTKM_DEFAULT_DEVICE_ADAPTER_TAG> DFA;

Dave Pugmire's avatar
Dave Pugmire committed
32
33
34
35
36
37
38
39
40
//Coordinates builder??
//Need a singlecellset handler.

class DataSetBuilderExplicit
{
public:
    VTKM_CONT_EXPORT
    DataSetBuilderExplicit() {}

dpugmire's avatar
dpugmire committed
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
    //Single cell explicits.
    template<typename T, typename CellType>
    VTKM_CONT_EXPORT
    vtkm::cont::DataSet
    Create(const std::vector<T> &xVals,
           const std::vector<T> &yVals,
           const std::vector<vtkm::Id> &connectivity,
           const std::string &coordsNm="coords",
           const std::string &cellNm="cells");

    template<typename T, typename CellType>
    VTKM_CONT_EXPORT
    vtkm::cont::DataSet
    Create(const std::vector<T> &xVals,
           const std::vector<T> &yVals,
           const std::vector<T> &zVals,
           const std::vector<vtkm::Id> &connectivity,
           const std::string &coordsNm="coords",
           const std::string &cellNm="cells");

    template<typename T, typename CellType>
    VTKM_CONT_EXPORT
    vtkm::cont::DataSet
    Create(const std::vector<vtkm::Vec<T,3> > &coords,
           const std::vector<vtkm::Id> &connectivity,
           const std::string &coordsNm="coords",
           const std::string &cellNm="cells");

    //Zoo explicit cell
Dave Pugmire's avatar
Dave Pugmire committed
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
    template<typename T>
    VTKM_CONT_EXPORT
    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");

    template<typename T>
    VTKM_CONT_EXPORT
    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
    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");
private:
};

dpugmire's avatar
dpugmire committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
template<typename T, typename CellType>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<T> &xVals,
			       const std::vector<T> &yVals,
			       const std::vector<vtkm::Id> &connectivity,
			       const std::string &coordsNm,
			       const std::string &cellNm)
{
    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());
    
dpugmire's avatar
dpugmire committed
119
    for (size_t i=0; i < coords.size(); i++)
dpugmire's avatar
dpugmire committed
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
    {
        coords[i][0] = xVals[i];
        coords[i][1] = yVals[i];
        coords[i][2] = 0;
    }
    dataSet.AddCoordinateSystem(
        vtkm::cont::CoordinateSystem(coordsNm, 1, coords));

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

    return dataSet;
}

template<typename T, typename CellType>
vtkm::cont::DataSet
DataSetBuilderExplicit::Create(const std::vector<T> &xVals,
			       const std::vector<T> &yVals,
			       const std::vector<T> &zVals,
			       const std::vector<vtkm::Id> &connectivity,
			       const std::string &coordsNm,
			       const std::string &cellNm)
{
    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());
dpugmire's avatar
dpugmire committed
153
    for (vtkm::Id i=0; i < nPts; i++)
dpugmire's avatar
dpugmire committed
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
    {
        coords[i][0] = xVals[i];
        coords[i][1] = yVals[i];
        coords[i][2] = zVals[i];
    }
    dataSet.AddCoordinateSystem(
        vtkm::cont::CoordinateSystem(coordsNm, 1, coords));

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

    return dataSet;
}

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)
{
    vtkm::cont::DataSet dataSet;

    vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
    DFA::Copy(vtkm::cont::make_ArrayHandle(coords), coordsArray);

    dataSet.AddCoordinateSystem(
        vtkm::cont::CoordinateSystem(coordsNm, 1, coordsArray));

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

    return dataSet;
}


Dave Pugmire's avatar
Dave Pugmire committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
template<typename T>
vtkm::cont::DataSet
DataSetBuilderExplicit::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,
                               const std::string &cellNm)
{
    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());
    
dpugmire's avatar
dpugmire committed
209
    for (size_t i=0; i < xVals.size(); i++)
Dave Pugmire's avatar
Dave Pugmire committed
210
211
212
213
214
215
216
217
    {
        coords[i][0] = xVals[i];
        coords[i][1] = yVals[i];
        coords[i][2] = 0;
    }
    dataSet.AddCoordinateSystem(
        vtkm::cont::CoordinateSystem(coordsNm, 1, coords));

dpugmire's avatar
dpugmire committed
218
    vtkm::Id nPts = static_cast<vtkm::Id>(coords.size());
dpugmire's avatar
dpugmire committed
219
    vtkm::cont::CellSetExplicit<> cellSet(nPts, cellNm, 2);
Dave Pugmire's avatar
Dave Pugmire committed
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
    cellSet.FillViaCopy(shapes, numIndices, connectivity);
    dataSet.AddCellSet(cellSet);

    return dataSet;
}

template<typename T>
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)
{
    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());
    
dpugmire's avatar
dpugmire committed
246
    for (size_t i=0; i < coords.size(); i++)
Dave Pugmire's avatar
Dave Pugmire committed
247
248
249
250
251
252
253
254
    {
        coords[i][0] = xVals[i];
        coords[i][1] = yVals[i];
        coords[i][2] = zVals[i];
    }
    dataSet.AddCoordinateSystem(
        vtkm::cont::CoordinateSystem(coordsNm, 1, coords));

dpugmire's avatar
dpugmire committed
255
    vtkm::Id nPts = static_cast<vtkm::Id>(coords.size());
dpugmire's avatar
dpugmire committed
256
    vtkm::cont::CellSetExplicit<> cellSet(nPts, cellNm, 2);
Dave Pugmire's avatar
Dave Pugmire committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    cellSet.FillViaCopy(shapes, numIndices, connectivity);
    dataSet.AddCellSet(cellSet);

    return dataSet;
}

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)
{
    vtkm::cont::DataSet dataSet;

274
275
276
    vtkm::cont::ArrayHandle<Vec<T,3> > coordsArray;
    DFA::Copy(vtkm::cont::make_ArrayHandle(coords), coordsArray);

Dave Pugmire's avatar
Dave Pugmire committed
277
    dataSet.AddCoordinateSystem(
278
        vtkm::cont::CoordinateSystem(coordsNm, 1, coordsArray));
Dave Pugmire's avatar
Dave Pugmire committed
279

dpugmire's avatar
dpugmire committed
280
    vtkm::Id nPts = static_cast<vtkm::Id>(coords.size());
281
    vtkm::cont::CellSetExplicit<> cellSet(nPts, cellNm, 2);
Dave Pugmire's avatar
Dave Pugmire committed
282
283
    cellSet.FillViaCopy(shapes, numIndices, connectivity);
    dataSet.AddCellSet(cellSet);
284

Dave Pugmire's avatar
Dave Pugmire committed
285
286
287
    return dataSet;
}

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
class DataSetIterativeBuilderExplicit
{
public:
    VTKM_CONT_EXPORT
    DataSetIterativeBuilderExplicit() {}

    VTKM_CONT_EXPORT
    void Begin(const std::string &_coordNm="coords",
	       const std::string &_cellNm="cells")
    {
	coordNm = _coordNm;
	cellNm = _cellNm;
        points.resize(0);
        shapes.resize(0);
        numIdx.resize(0);
        connectivity.resize(0);
    }

    //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(const vtkm::UInt8 &shape, const std::vector<vtkm::Id> &conn)
    {
	shapes.push_back(shape);
	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)
    {
        shapes.push_back(shape);
        numIdx.push_back(n);
        for (int i = 0; i < n; i++)
            connectivity.push_back(conn[i]);
    }
    
private:
    std::string coordNm, cellNm;

    std::vector<vtkm::Vec<vtkm::Float32,3> > points;
    std::vector<vtkm::UInt8> shapes;
    std::vector<vtkm::IdComponent> numIdx;
    std::vector<vtkm::Id> connectivity;
};

vtkm::cont::DataSet
DataSetIterativeBuilderExplicit::Create()
{
    DataSetBuilderExplicit dsb;
    return dsb.Create(points, shapes, numIdx, connectivity, coordNm, cellNm);
}

Dave Pugmire's avatar
Dave Pugmire committed
377
378
379
380
381

}
}

#endif //vtk_m_cont_DataSetBuilderExplicit_h