File: GeometryInfo.cpp

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (439 lines) | stat: -rw-r--r-- 12,307 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
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
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
433
434
435
436
437
438
439
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "Geometry/GeometryInfo.h"

// --- STL includes --- 
#include <stdexcept>
#include <sstream>
#include <algorithm> 

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

    /*!
      Write complete geometry info to output stream

      \param oStream the output stream
    */
    void GeometryInfo::Impl::write(std::ostream &oStream) const
    {
      oStream << "LSMGeometry " << m_version << "\n";
      writeWithoutVersion(oStream);
    }

    /*!
      Write geometry info to output stream, except the version nr. 
      of the .geo file. This output can't be read by the "read" function.
      It is used for the snapshot headers.

      \param oStream the output stream
    */
    void GeometryInfo::Impl::writeWithoutVersion(std::ostream &oStream) const
    {
      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];

      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;
      size_t ftl=std::min(fileType.size(),size_t(4));
      std::string fileTypeHead=fileType.substr(0,ftl);
      if ((fileType != "LSMGeometry") && (fileTypeHead!="ESyS")) { // wrong file type -> bail out
        throw runtime_error("Unrecognised file type " + fileType + " expected LSMGeometry or ESyS-Particle.");
      }

      // read and check version number
      iStream >> impl.m_version;
      if((fileType=="LSMGeometry") && ((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());
      } else if (fileTypeHead=="ESyS") {
	impl.m_version=1.2f; // 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;
    }
      
    /*!
	get the version of the loaded geometry
    */    
    float GeometryInfo::getLsmGeoVersion() const
    {
      return m_pImpl->m_version;
    }
    
    void GeometryInfo::read(std::istream &iStream)
    {
      m_pImpl->read(iStream);
    }

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

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

    void GeometryInfo::setBBox(const Vec3 &min, const Vec3 &max)
    {
      m_pImpl->m_bBoxMin = min;
      m_pImpl->m_bBoxMax = max;
    }

	/*!
	 check if a model with the geometry described in the GeoInfo given in the argument
	 can be loaded into a pre-existing model 
	
	 \param gi the new geometry info
	 */
	bool GeometryInfo::isCompatible(const GeometryInfo& gi) const
	{
		bool res=true;
		
		// check if circular dimensions agree
		res=res && (m_pImpl->m_periodicDimensions[0]==gi.m_pImpl->m_periodicDimensions[0]) 
			&& (m_pImpl->m_periodicDimensions[1]==gi.m_pImpl->m_periodicDimensions[1]) 
			&& (m_pImpl->m_periodicDimensions[2]==gi.m_pImpl->m_periodicDimensions[2]); 
		// check if min/max in circular dimensions agree and 
		// if new (argument) bounding box fits into old bbx in the non-circular dimensions
		// x
		if(m_pImpl->m_periodicDimensions[0]) {
			res = res && (m_pImpl->m_bBoxMin[0]==gi.m_pImpl->m_bBoxMin[0])
				&& (m_pImpl->m_bBoxMax[0]==gi.m_pImpl->m_bBoxMax[0]);
		} else {
			res = res && (m_pImpl->m_bBoxMin[0]<=gi.m_pImpl->m_bBoxMin[0])
				&& (m_pImpl->m_bBoxMax[0]>=gi.m_pImpl->m_bBoxMax[0]);
		}
		// y
 		if(m_pImpl->m_periodicDimensions[1]) {
			res = res && (m_pImpl->m_bBoxMin[1]==gi.m_pImpl->m_bBoxMin[1])
				&& (m_pImpl->m_bBoxMax[1]==gi.m_pImpl->m_bBoxMax[1]);
		} else {
			res = res && (m_pImpl->m_bBoxMin[1]<=gi.m_pImpl->m_bBoxMin[1])
				&& (m_pImpl->m_bBoxMax[1]>=gi.m_pImpl->m_bBoxMax[1]);
		}
		// z
		if(m_pImpl->m_periodicDimensions[2]) {
			res = res && (m_pImpl->m_bBoxMin[2]==gi.m_pImpl->m_bBoxMin[2])
				&& (m_pImpl->m_bBoxMax[2]==gi.m_pImpl->m_bBoxMax[2]);
		} else {
			res = res && (m_pImpl->m_bBoxMin[2]<=gi.m_pImpl->m_bBoxMin[2])
				&& (m_pImpl->m_bBoxMax[2]>=gi.m_pImpl->m_bBoxMax[2]);
		}
		
		return res;
	}

    
	/*!
		Check if the geometrical dimensions in two GeometryInfo objects are identical
	
		\param gi the new geometry info
	 */
	bool GeometryInfo::isIdenticalGeometry(const GeometryInfo& gi) const
	{
		bool res=true;
		
		// check if circular dimensions agree
		res=res && (m_pImpl->m_periodicDimensions[0]==gi.m_pImpl->m_periodicDimensions[0]) 
			&& (m_pImpl->m_periodicDimensions[1]==gi.m_pImpl->m_periodicDimensions[1]) 
			&& (m_pImpl->m_periodicDimensions[2]==gi.m_pImpl->m_periodicDimensions[2]); 
		// check if dimensions agree 
		// x
		res = res && (m_pImpl->m_bBoxMin[0]==gi.m_pImpl->m_bBoxMin[0])
				&& (m_pImpl->m_bBoxMax[0]==gi.m_pImpl->m_bBoxMax[0]);
		// y
 		res = res && (m_pImpl->m_bBoxMin[1]==gi.m_pImpl->m_bBoxMin[1])
				&& (m_pImpl->m_bBoxMax[1]==gi.m_pImpl->m_bBoxMax[1]);
		// z
		res = res && (m_pImpl->m_bBoxMin[2]==gi.m_pImpl->m_bBoxMin[2])
				&& (m_pImpl->m_bBoxMax[2]==gi.m_pImpl->m_bBoxMax[2]);
		
		return res;
	}

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