File: GeometryInfo.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 (333 lines) | stat: -rw-r--r-- 8,698 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
/////////////////////////////////////////////////////////////
//                                                         //
// 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/GeometryInfo.h"

#include <stdexcept>
#include <sstream>

namespace esys
{
  namespace lsm
  {
    class GeometryInfo::Impl
    {
    public:
      Impl();
      
      Impl(
        float            version,
        const Vec3       &bBoxMin,
        const Vec3       &bBoxMax,
        const BoolVector &periodicDimensions,
        bool             is2d = false
      );

      Impl &operator=(const Impl &impl);

      bool operator==(const Impl &impl) const;

      ~Impl();

      void read(std::istream &iStream);

      void write(std::ostream &oStream) const;

      float      m_version;
      Vec3       m_bBoxMin;
      Vec3       m_bBoxMax;
      /**
       * Indicates the dimensions which are periodic, element 0 indicates
       * whether the x-dim is periodic, element 1 indicates whether the y-dim
       * is periodic and element 2 indicates whether the z-dim is periodic.
       */
      BoolVector m_periodicDimensions;
      bool       m_is2d;      
    };

    GeometryInfo::Impl::Impl()
      : m_version(0.0),
        m_bBoxMin(),
        m_bBoxMax(),
        m_periodicDimensions(3, false),
        m_is2d(false)        
    {
    }

    GeometryInfo::Impl::Impl(
      float            version,
      const Vec3       &bBoxMin,
      const Vec3       &bBoxMax,
      const BoolVector &periodicDimensions,
      bool             is2d
    )
      : m_version(version),
        m_bBoxMin(bBoxMin),
        m_bBoxMax(bBoxMax),
        m_periodicDimensions(periodicDimensions),
        m_is2d(is2d)
    {
    }

    GeometryInfo::Impl::~Impl()
    {
    }

    GeometryInfo::Impl &GeometryInfo::Impl::operator=(const GeometryInfo::Impl &impl)
    {
      m_version            = impl.m_version;
      m_bBoxMin            = impl.m_bBoxMin;
      m_bBoxMax            = impl.m_bBoxMax;
      m_periodicDimensions = impl.m_periodicDimensions;
      m_is2d               = impl.m_is2d;      

      return *this;
    }

    bool GeometryInfo::Impl::operator==(const GeometryInfo::Impl &impl) const
    {
      return
        (
          (m_version == impl.m_version)
          &&
          (m_bBoxMin == impl.m_bBoxMin)
          &&
          (m_bBoxMax == impl.m_bBoxMax)
          &&
          (m_is2d == impl.m_is2d)
          &&
          (m_periodicDimensions == impl.m_periodicDimensions)
        );
    }

    void GeometryInfo::Impl::write(std::ostream &oStream) const
    {
      oStream << "LSMGeometry " << m_version << "\n";
      oStream << "BoundingBox ";
      oStream 
        << m_bBoxMin.X() << " "
        << m_bBoxMin.Y() << " "
        << m_bBoxMin.Z() << " ";
      oStream 
        << m_bBoxMax.X() << " "
        << m_bBoxMax.Y() << " "
        << m_bBoxMax.Z() << "\n";

      oStream << "PeriodicBoundaries ";
      oStream 
        << m_periodicDimensions[0] << " "
        << m_periodicDimensions[1] << " "
        << m_periodicDimensions[2];

      if (m_version == 1.2f)
      {
        oStream
          << "\nDimension "
          << (m_is2d ? "2D" : "3D");
      }
    }

    void GeometryInfo::Impl::read(std::istream &iStream)
    {
      Impl impl;

      // read and check file type marker
      std::string fileType;
      iStream >> fileType;
      if (fileType != "LSMGeometry") { // wrong file type -> bail out
        throw runtime_error("Unrecognised file type " + fileType + " expected LSMGeometry.");
      }

      // read and check version number
      iStream >> impl.m_version;
      if((impl.m_version != 1.1f) && (impl.m_version != 1.2f)){
        std::stringstream msg;
        msg
          << "Can only read version 1.1 or 1.2 geometry files, this is version "
          << impl.m_version;
        // throw std::runtime_error(msg.str().c_str());
        impl.m_version=1.2; // temporary hack
      }

      // read boundary box
      string bbx;
      iStream >> bbx;
      // check boundary box marker
      if (bbx != "BoundingBox") {
        throw std::runtime_error("Expected BoundingBox, got " + bbx);;
      }
      iStream >> impl.m_bBoxMin.X() >> impl.m_bBoxMin.Y() >> impl.m_bBoxMin.Z();
      iStream >> impl.m_bBoxMax.X() >> impl.m_bBoxMax.Y() >> impl.m_bBoxMax.Z();

      // read boundary periodicity
      string bdry;
      iStream >> bdry;
      // check boundary periodicity marker
      if(bdry != "PeriodicBoundaries")
      {
        throw std::runtime_error("Expected PeriodicBoundaries, got " + bdry);
      }
      for (int i = 0; i < 3; i++) {
        bool isPeriodic = false;
        iStream >> isPeriodic;
        impl.m_periodicDimensions[i] = isPeriodic;
      }

      // if 1.2, read in 2D or 3D
      if(impl.m_version == 1.2f){
        std::string dims;
        iStream >> dims;
        if(dims != "Dimension") {
          throw std::runtime_error("Expected 'Dimension', got '" + dims + "'");
        }

        std::string r2d;        
        iStream >> r2d;
        if((r2d == "2D") || (r2d == "2d")){
          impl.m_is2d = true;
        } else {
          impl.m_is2d = false;
        }
      } else {
        impl.m_is2d = true;
      }

      *this = impl;
    }

    GeometryInfo::GeometryInfo()
      : m_pImpl(new GeometryInfo::Impl())
    {
    }

    GeometryInfo::GeometryInfo(
      float            version,
      const Vec3       &bBoxMin,
      const Vec3       &bBoxMax,
      const BoolVector &periodicDimensions,
      bool             is2d
    )
      : m_pImpl(new Impl(version, bBoxMin, bBoxMax, periodicDimensions, is2d))
    {
    }

    GeometryInfo::GeometryInfo(const GeometryInfo &geoInfo)
      : m_pImpl(new GeometryInfo::Impl(*(geoInfo.m_pImpl)))
    {
    }

    GeometryInfo &GeometryInfo::operator=(const GeometryInfo &geoInfo)
    {
      *m_pImpl = *geoInfo.m_pImpl;
      return *this;
    }

    GeometryInfo::~GeometryInfo()
    {
      delete m_pImpl;
    }

    bool GeometryInfo::operator==(const GeometryInfo &geoInfo) const
    {
      return (*m_pImpl == *geoInfo.m_pImpl);
    }

    bool GeometryInfo::hasAnyPeriodicDimensions() const
    {
      return 
        (
          ((m_pImpl->m_periodicDimensions.size() > 0) && (m_pImpl->m_periodicDimensions[0]))
          ||
          ((m_pImpl->m_periodicDimensions.size() > 1) && (m_pImpl->m_periodicDimensions[1]))
          ||
          ((m_pImpl->m_periodicDimensions.size() > 2) && (m_pImpl->m_periodicDimensions[2]))
        );
    }

    bool GeometryInfo::is2d() const
    {
      return m_pImpl->m_is2d;
    }

    void GeometryInfo::set_is2d(bool do2d)
    {
      m_pImpl->m_is2d = do2d;
    }

    Vec3Vector GeometryInfo::getBBoxCorners() const
    {
      Vec3Vector corners;
      corners.push_back(m_pImpl->m_bBoxMin);
      corners.push_back(m_pImpl->m_bBoxMax);

      return corners;
    }

    Vec3 GeometryInfo::getMinBBoxCorner() const
    {
      return m_pImpl->m_bBoxMin;
    }

    Vec3 GeometryInfo::getMaxBBoxCorner() const
    {
      return m_pImpl->m_bBoxMax;
    }

    IntVector GeometryInfo::getPeriodicDimensions() const
    {
      return 
        IntVector(
          m_pImpl->m_periodicDimensions.begin(),
          m_pImpl->m_periodicDimensions.end()
        );
    }

    void GeometryInfo::setPeriodicDimensions(BoolVector periodicDimensions)
    {
      m_pImpl->m_periodicDimensions = periodicDimensions;
    }

    void GeometryInfo::setLsmGeoVersion(float version)
    {
      m_pImpl->m_version = version;
    }
      
    void GeometryInfo::read(std::istream &iStream)
    {
      m_pImpl->read(iStream);
    }

    void GeometryInfo::write(std::ostream &oStream) const
    {
      m_pImpl->write(oStream);
    }

    void GeometryInfo::setBBox(const Vec3 &min, const Vec3 &max)
    {
      m_pImpl->m_bBoxMin = min;
      m_pImpl->m_bBoxMax = max;
    }
    
    std::ostream &operator<<(std::ostream &oStream, const GeometryInfo &geoInfo)
    {
      geoInfo.write(oStream);
      return oStream;
    }

    std::istream &operator<<(std::istream &iStream, GeometryInfo &geoInfo)
    {
      geoInfo.read(iStream);
      return iStream;
    }
  }
}