File: VtkXmlWriter.cpp

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (346 lines) | stat: -rw-r--r-- 9,780 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
153
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "Geometry/VtkXmlWriter.h"

#include <vector>
#include <map>
#include <sstream>
#include <fstream>
#include <stdexcept>

namespace esys
{
  namespace lsm
  {
    typedef std::vector<const SimpleParticle *>   ParticleVector;
    typedef std::vector<const BasicInteraction *> ConnectionVector;
    typedef std::map<int,int> IdIndexMap;
  //=============================================================================
    class ParticleDataVisitor::Impl
    {
    public:
      Impl()
      {
      }

      void append(const SimpleParticle &particle)
      {
        m_idIndexMap[particle.getID()] = m_particleVector.size();
        m_particleVector.push_back(&particle);
      }

      void append(const BasicInteraction &connection)
      {
        m_connectionVector.push_back(&connection);
      }

      ParticleVector    m_particleVector;
      ConnectionVector m_connectionVector;
      IdIndexMap        m_idIndexMap;
    };

  //=============================================================================
    ParticleDataVisitor::ParticleDataVisitor()
      : m_implPtr(new Impl())
    {
    }

    void ParticleDataVisitor::visitSimpleParticle(const SimpleParticle &particle)
    {
      m_implPtr->append(particle);
    }

    void ParticleDataVisitor::visitParticle(const Particle &particle)
    {
      visitSimpleParticle(particle);
    }

    void ParticleDataVisitor::visitBasicInteraction(const Connection &connection)
    {
      m_implPtr->append(connection);
    }

    void ParticleDataVisitor::visitConnection(const Connection &connection)
    {
      visitBasicInteraction(connection);
    }

    size_t ParticleDataVisitor::getNumParticles() const
    {
      return m_implPtr->m_particleVector.size();
    }

    size_t ParticleDataVisitor::getNumConnections() const
    {
      return m_implPtr->m_connectionVector.size();
    }

    template <class TmplContainer>
    class ConstContainerIterator
    {
    public:
      ConstContainerIterator(const TmplContainer &container)
        : m_it(container.begin()),
          m_end(container.end())
      {
      }

      bool hasNext() const
      {
        return (m_it != m_end);
      }

      typename TmplContainer::const_reference next()
      {
        typename TmplContainer::const_reference valueRef = (*m_it);
        ++m_it;
        return valueRef;
      }

    private:
      typename TmplContainer::const_iterator m_it;
      typename TmplContainer::const_iterator m_end;
    };

    class ParticleIterator
    {
    public:
      ParticleIterator(const ParticleVector &particleVector)
        : m_it(particleVector)
      {
      }

      bool hasNext() const
      {
        return m_it.hasNext();
      }

      const SimpleParticle &next()
      {
        return *m_it.next();
      }

    private:
      ConstContainerIterator<ParticleVector> m_it;
    };

    class ConnectionIterator
    {
    public:
      ConnectionIterator(const ConnectionVector &connectionVector)
        : m_it(connectionVector)
      {
      }

      bool hasNext() const
      {
        return m_it.hasNext();
      }

      const BasicInteraction &next()
      {
        return *m_it.next();
      }

    private:
      ConstContainerIterator<ConnectionVector> m_it;
    };

    void ParticleDataVisitor::writeCentrePoints(std::ostream &oStream) const
    {
      ParticleIterator it(m_implPtr->m_particleVector);
      while (it.hasNext())
      {
        oStream << it.next().getPos() << "\n";
      }
    }

    void ParticleDataVisitor::writeRadii(std::ostream &oStream) const
    {
      ParticleIterator it(m_implPtr->m_particleVector);
      while (it.hasNext())
      {
        oStream << it.next().getRad() << "\n";
      }
    }

    void ParticleDataVisitor::writeTags(std::ostream &oStream) const
    {
      ParticleIterator it(m_implPtr->m_particleVector);
      while (it.hasNext())
      {
        oStream << it.next().getTag() << "\n";
      }
    }

    void ParticleDataVisitor::writeIds(std::ostream &oStream) const
    {
      ParticleIterator it(m_implPtr->m_particleVector);
      while (it.hasNext())
      {
        oStream << it.next().getID() << "\n";
      }
    }

    int ParticleDataVisitor::getIndex(int particleId) const
    {
      IdIndexMap::const_iterator it = m_implPtr->m_idIndexMap.find(particleId);
      if (it == m_implPtr->m_idIndexMap.end())
      {
        std::stringstream msg;
        msg << "Could not find particle id " << particleId << " in index map.";
        throw std::runtime_error(msg.str());
      }
      return it->second;
    }

    void ParticleDataVisitor::writeParticleIndexConnections(std::ostream &oStream) const
    {
      ConnectionIterator it(m_implPtr->m_connectionVector);
      while (it.hasNext())
      {
        const BasicInteraction &connection = it.next();
        oStream 
          << getIndex(connection.first())
          << " "
          << getIndex(connection.second())
          << "\n";
      }
    }

    void ParticleDataVisitor::writeConnectionTags(std::ostream &oStream) const
    {
      ConnectionIterator it(m_implPtr->m_connectionVector);
      while (it.hasNext())
      {
        oStream 
          << it.next().getTag()
          << "\n";
      }
    }

  //=============================================================================
    class VtkXmlWriter::Impl
    {
    public:
      Impl() : m_pParticleData(NULL)
      {
      }
      
      const ParticleDataVisitor *m_pParticleData;
    };

  //=============================================================================
    VtkXmlWriter::VtkXmlWriter()
      : m_implPtr(new Impl())
    {
    }

    VtkXmlWriter::~VtkXmlWriter()
    {
    }
    
    void VtkXmlWriter::setData(const ParticleDataVisitor &particleData)
    {
      m_implPtr->m_pParticleData = &particleData;
    }
    
    size_t VtkXmlWriter::getNumParticles() const
    {
      return m_implPtr->m_pParticleData->getNumParticles();
    }

    size_t VtkXmlWriter::getNumConnections() const
    {
      return m_implPtr->m_pParticleData->getNumConnections();
    }

    void VtkXmlWriter::writePoints(std::ostream &oStream)
    {
      oStream << "<Points>\n";
      oStream << "<DataArray NumberOfComponents=\"3\" type=\"Float32\" format=\"ascii\">\n";
      m_implPtr->m_pParticleData->writeCentrePoints(oStream);
      oStream << "</DataArray>\n";
      oStream << "</Points>\n";
    }

    void VtkXmlWriter::writePointData(std::ostream &oStream)
    {
      oStream << "<PointData Scalars=\"radius\">\n";

      oStream << "<DataArray type=\"Float32\" Name=\"radius\" NumberOfComponents=\"1\" format=\"ascii\">\n";
      m_implPtr->m_pParticleData->writeRadii(oStream);
      oStream << "</DataArray>\n";
      oStream << "<DataArray type=\"Int32\" Name=\"particleTag\" NumberOfComponents=\"1\" format=\"ascii\">\n";
      m_implPtr->m_pParticleData->writeTags(oStream);
      oStream << "</DataArray>\n";
      oStream << "<DataArray type=\"Int32\" Name=\"Id\" NumberOfComponents=\"1\" format=\"ascii\">\n";
      m_implPtr->m_pParticleData->writeIds(oStream);
      oStream << "</DataArray>\n";

      oStream << "</PointData>\n";
    }

    void VtkXmlWriter::writeCells(std::ostream &oStream)
    {
      oStream << "<Cells>\n";
      oStream << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"connectivity\" format=\"ascii\">\n";
      m_implPtr->m_pParticleData->writeParticleIndexConnections(oStream);
      oStream << "</DataArray>";
      
      oStream << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"offsets\" format=\"ascii\">\n";
      for (size_t i = 1; i < getNumConnections()*2; i+=2) oStream << i+1 << "\n";
      oStream << "</DataArray>\n";

      oStream << "<DataArray type=\"UInt8\" NumberOfComponents=\"1\" Name=\"types\" format=\"ascii\">\n";
      const int CELL_LINE_TYPE = 3;
      for (size_t i = 0; i < getNumConnections(); i++) oStream << CELL_LINE_TYPE << "\n";
      oStream << "</DataArray>\n";

      oStream << "</Cells>\n";
    }

    void VtkXmlWriter::writeCellData(std::ostream &oStream)
    {
      oStream << "<CellData>\n";
      oStream << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"bondTag\" format=\"ascii\">\n";
      m_implPtr->m_pParticleData->writeConnectionTags(oStream);
      oStream << "</DataArray>\n";
      oStream << "</CellData>\n";
    }

    void VtkXmlWriter::write(std::ostream &oStream)
    {
      oStream << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n";
      oStream << "<UnstructuredGrid>\n";
      oStream 
        << "<Piece NumberOfPoints=\"" << getNumParticles()
        << "\" NumberOfCells=\"" << getNumConnections() << "\">\n";
      writePoints(oStream);
      writePointData(oStream);
      writeCells(oStream);
      writeCellData(oStream);
      oStream << "</Piece>\n";
      oStream << "</UnstructuredGrid>\n";
      oStream << "</VTKFile>\n";
    }

    void VtkXmlWriter::writeToFile(const std::string &fileName)
    {
      std::ofstream oStream(fileName.c_str());
      oStream << "<?xml version=\"1.0\"?>\n";
      write(oStream);
    }
  };
};