xmsgrid  1.0
XmUGridUtils.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
6 //------------------------------------------------------------------------------
7 
8 //----- Included files ---------------------------------------------------------
9 
10 // 1. Precompiled header
11 
12 // 2. My own header
14 
15 // 3. Standard library headers
16 #include <fstream>
17 
18 // 4. External library headers
19 #include <boost/bimap.hpp>
20 
21 // 5. Shared code headers
24 #include <xmscore/misc/XmError.h>
25 #include <xmscore/misc/XmLog.h>
26 #include <xmscore/stl/vector.h>
27 
28 // 6. Non-shared code headers
29 #include <xmsgrid/ugrid/XmUGrid.h>
30 
31 //----- Forward declarations ---------------------------------------------------
32 
33 //----- External globals -------------------------------------------------------
34 
35 //----- Namespace declaration --------------------------------------------------
36 
38 namespace xms
39 {
40 //----- Constants / Enumerations -----------------------------------------------
41 
42 //----- Classes / Structs ------------------------------------------------------
43 
44 //----- Internal functions -----------------------------------------------------
45 
46 //----- Class / Function definitions -------------------------------------------
47 
48 namespace
49 {
50 typedef boost::bimap<std::string, int> CellNameToType;
51 
52 //------------------------------------------------------------------------------
55 //------------------------------------------------------------------------------
56 const CellNameToType& iCellTypeStringIntPair()
57 {
58  static CellNameToType cellNameToInt;
59  if (cellNameToInt.empty())
60  {
61  // clang-format off
62  cellNameToInt.insert(CellNameToType::value_type("INVALID_CELL_TYPE", XMU_INVALID_CELL_TYPE));
63  cellNameToInt.insert(CellNameToType::value_type("EMPTY_CELL", XMU_EMPTY_CELL));
64  cellNameToInt.insert(CellNameToType::value_type("VERTEX", XMU_VERTEX));
65  cellNameToInt.insert(CellNameToType::value_type("POLY_VERTEX", XMU_POLY_VERTEX));
66  cellNameToInt.insert(CellNameToType::value_type("LINE", XMU_LINE));
67  cellNameToInt.insert(CellNameToType::value_type("POLY_LINE", XMU_POLY_LINE));
68  cellNameToInt.insert(CellNameToType::value_type("TRIANGLE", XMU_TRIANGLE));
69  cellNameToInt.insert(CellNameToType::value_type("TRIANGLE_STRIP", XMU_TRIANGLE_STRIP));
70  cellNameToInt.insert(CellNameToType::value_type("POLYGON", XMU_POLYGON));
71  cellNameToInt.insert(CellNameToType::value_type("PIXEL", XMU_PIXEL));
72  cellNameToInt.insert(CellNameToType::value_type("QUAD", XMU_QUAD));
73  cellNameToInt.insert(CellNameToType::value_type("TETRA", XMU_TETRA));
74  cellNameToInt.insert(CellNameToType::value_type("VOXEL", XMU_VOXEL));
75  cellNameToInt.insert(CellNameToType::value_type("HEXAHEDRON", XMU_HEXAHEDRON));
76  cellNameToInt.insert(CellNameToType::value_type("WEDGE", XMU_WEDGE));
77  cellNameToInt.insert(CellNameToType::value_type("PYRAMID", XMU_PYRAMID));
78  cellNameToInt.insert(CellNameToType::value_type("PENTAGONAL_PRISM", XMU_PENTAGONAL_PRISM));
79  cellNameToInt.insert(CellNameToType::value_type("HEXAGONAL_PRISM", XMU_HEXAGONAL_PRISM));
80  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_EDGE", XMU_QUADRATIC_EDGE));
81  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_TRIANGLE", XMU_QUADRATIC_TRIANGLE));
82  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_QUAD", XMU_QUADRATIC_QUAD));
83  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_POLYGON", XMU_QUADRATIC_POLYGON));
84  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_TETRA", XMU_QUADRATIC_TETRA));
85  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_HEXAHEDRON", XMU_QUADRATIC_HEXAHEDRON));
86  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_WEDGE", XMU_QUADRATIC_WEDGE));
87  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_PYRAMID", XMU_QUADRATIC_PYRAMID));
88  cellNameToInt.insert(CellNameToType::value_type("BIQUADRATIC_QUAD", XMU_BIQUADRATIC_QUAD));
89  cellNameToInt.insert(CellNameToType::value_type("TRIQUADRATIC_HEXAHEDRON", XMU_TRIQUADRATIC_HEXAHEDRON));
90  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_LINEAR_QUAD", XMU_QUADRATIC_LINEAR_QUAD));
91  cellNameToInt.insert(CellNameToType::value_type("QUADRATIC_LINEAR_WEDGE", XMU_QUADRATIC_LINEAR_WEDGE));
92  cellNameToInt.insert(CellNameToType::value_type("BIQUADRATIC_QUADRATIC_WEDGE", XMU_BIQUADRATIC_QUADRATIC_WEDGE));
93  cellNameToInt.insert(CellNameToType::value_type("BIQUADRATIC_QUADRATIC_HEXAHEDRON", XMU_BIQUADRATIC_QUADRATIC_HEXAHEDRON));
94  cellNameToInt.insert(CellNameToType::value_type("BIQUADRATIC_TRIANGLE", XMU_BIQUADRATIC_TRIANGLE));
95  cellNameToInt.insert(CellNameToType::value_type("CUBIC_LINE", XMU_CUBIC_LINE));
96  cellNameToInt.insert(CellNameToType::value_type("CONVEX_POINT_SET", XMU_CONVEX_POINT_SET));
97  cellNameToInt.insert(CellNameToType::value_type("POLYHEDRON", XMU_POLYHEDRON));
98  cellNameToInt.insert(CellNameToType::value_type("PARAMETRIC_CURVE", XMU_PARAMETRIC_CURVE));
99  cellNameToInt.insert(CellNameToType::value_type("PARAMETRIC_SURFACE", XMU_PARAMETRIC_SURFACE));
100  cellNameToInt.insert(CellNameToType::value_type("PARAMETRIC_TRI_SURFACE", XMU_PARAMETRIC_TRI_SURFACE));
101  cellNameToInt.insert(CellNameToType::value_type("PARAMETRIC_QUAD_SURFACE", XMU_PARAMETRIC_QUAD_SURFACE));
102  cellNameToInt.insert(CellNameToType::value_type("PARAMETRIC_TETRA_REGION", XMU_PARAMETRIC_TETRA_REGION));
103  cellNameToInt.insert(CellNameToType::value_type("PARAMETRIC_HEX_REGION", XMU_PARAMETRIC_HEX_REGION));
104  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_EDGE", XMU_HIGHER_ORDER_EDGE));
105  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_TRIANGLE", XMU_HIGHER_ORDER_TRIANGLE));
106  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_QUAD", XMU_HIGHER_ORDER_QUAD));
107  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_POLYGON", XMU_HIGHER_ORDER_POLYGON));
108  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_TETRAHEDRON", XMU_HIGHER_ORDER_TETRAHEDRON));
109  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_WEDGE", XMU_HIGHER_ORDER_WEDGE));
110  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_PYRAMID", XMU_HIGHER_ORDER_PYRAMID));
111  cellNameToInt.insert(CellNameToType::value_type("HIGHER_ORDER_HEXAHEDRON", XMU_HIGHER_ORDER_HEXAHEDRON));
112  // clang-format on
113  }
114 
115  return cellNameToInt;
116 } // iCellTypeStringIntPair
117 
118 //------------------------------------------------------------------------------
122 //------------------------------------------------------------------------------
123 std::string iStringFromCellType(int a_type)
124 {
125  const CellNameToType& biMap = iCellTypeStringIntPair();
126  auto it = biMap.right.find(a_type);
127  if (it != biMap.right.end())
128  return it->second;
129  XM_ASSERT(0);
130  return std::string();
131 } // iStringFromCellType
132 
133 //------------------------------------------------------------------------------
137 //------------------------------------------------------------------------------
138 int iCellTypeFromString(const std::string& a_type)
139 {
140  const CellNameToType& biMap = iCellTypeStringIntPair();
141  auto it = biMap.left.find(a_type);
142  if (it != biMap.left.end())
143  return it->second;
144  return XMU_INVALID_CELL_TYPE;
145 } // iCellTypeFromString
146 
147 //------------------------------------------------------------------------------
152 //------------------------------------------------------------------------------
153 bool iReadPointsVersion1(DaStreamReader& a_reader, VecPt3d& locations)
154 {
155  int pointCount;
156  if (!a_reader.ReadIntLine("NUM_POINTS", pointCount))
157  {
158  XM_LOG(xmlog::error, "Unable to read XmUGrid point count.");
159  return false;
160  }
161 
162  locations.reserve(pointCount);
163  for (int pointIdx = 0; pointIdx < pointCount; ++pointIdx)
164  {
165  Pt3d point;
166  if (a_reader.Read3DoubleLine("POINT", point.x, point.y, point.z))
167  {
168  locations.push_back(point);
169  }
170  else
171  {
172  XM_LOG(xmlog::error, "Unable to read XmUGrid point.");
173  return false;
174  }
175  }
176 
177  return true;
178 } // iReadPointsVersion1
179 
180 //------------------------------------------------------------------------------
185 //------------------------------------------------------------------------------
186 bool iReadCellStreamSizeVersion1(DaStreamReader& a_reader, int& cellStreamSize)
187 {
188  if (!a_reader.ReadIntLine("NUM_CELL_ITEMS", cellStreamSize))
189  {
190  XM_LOG(xmlog::error, "Unable to read XmUGrid cells.");
191  return false;
192  }
193  return true;
194 } // iReadCellStreamSizeVersion1
195 
196 //------------------------------------------------------------------------------
201 //------------------------------------------------------------------------------
202 bool iReadCellStreamSizeVersion2(DaStreamReader& a_reader, int& cellStreamSize)
203 {
204  if (!a_reader.ReadIntLine("CELL_STREAM", cellStreamSize))
205  {
206  XM_LOG(xmlog::error, "Unable to read XmUGrid cells.");
207  return false;
208  }
209  return true;
210 } // iReadCellStreamSizeVersion1
211 
212 //------------------------------------------------------------------------------
218 //------------------------------------------------------------------------------
219 bool iReadCellLineVersion1(DaStreamReader& a_reader, std::string& a_cellLine, int& a_cellType)
220 {
221  a_cellType = XMU_INVALID_CELL_TYPE;
222  if (daReadIntFromLine(a_cellLine, a_cellType))
223  {
224  std::string cellTypeString = iStringFromCellType(a_cellType);
225  if (cellTypeString.empty())
226  a_cellType = XMU_INVALID_CELL_TYPE;
227  }
228  else
229  {
230  a_cellType = XMU_INVALID_CELL_TYPE;
231  }
232 
233  if (a_cellType == XMU_INVALID_CELL_TYPE)
234  {
235  XM_LOG(xmlog::error, "Found invalid cell type.");
236  return false;
237  }
238  return true;
239 } // iReadCellLineVersion1
240 
241 //------------------------------------------------------------------------------
247 //------------------------------------------------------------------------------
248 bool iReadCellLineVersion2(DaStreamReader& a_reader, int a_cellIdx, int& a_cellType)
249 {
250  int readCellIdx = -1;
251  if (!a_reader.ReadInt(readCellIdx) || readCellIdx != a_cellIdx)
252  {
253  XM_LOG(xmlog::error, "Unable to read cell line.");
254  return false;
255  }
256 
257  a_cellType = XMU_INVALID_CELL_TYPE;
258  std::string cellTypeString;
259  if (a_reader.ReadString(cellTypeString))
260  {
261  a_cellType = iCellTypeFromString(cellTypeString);
262  }
263  else
264  {
265  a_cellType = XMU_INVALID_CELL_TYPE;
266  }
267 
268  if (a_cellType == XMU_INVALID_CELL_TYPE)
269  {
270  XM_LOG(xmlog::error, "Found invalid cell type.");
271  return false;
272  }
273 
274  return true;
275 } // iReadCellLineVersion2
276 
277 //------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------
285 bool iReadCellStreamVersion1(DaStreamReader& a_reader,
286  std::string& a_cellLine,
287  int a_cellType,
288  VecInt& a_cellstream)
289 {
290  a_cellstream.push_back(a_cellType);
291  if (a_cellType == XMU_POLYHEDRON)
292  {
293  int numFaces;
294  std::string faceString;
295  daReadIntFromLine(a_cellLine, numFaces);
296  a_cellstream.push_back(numFaces);
297  for (int faceIdx = 0; faceIdx < numFaces; ++faceIdx)
298  {
299  if (!a_reader.ReadLine(a_cellLine))
300  {
301  XM_LOG(xmlog::error, "Unable to read cell stream.");
302  return false;
303  }
304 
305  int numPoints;
306  daReadIntFromLine(a_cellLine, numPoints);
307  a_cellstream.push_back(numPoints);
308  for (int i = 0; i < numPoints; ++i)
309  {
310  int ptIdx;
311  daReadIntFromLine(a_cellLine, ptIdx);
312  a_cellstream.push_back(ptIdx);
313  }
314  }
315  }
316  else
317  {
318  int numPoints;
319  daReadIntFromLine(a_cellLine, numPoints);
320  a_cellstream.push_back(numPoints);
321  for (int i = 0; i < numPoints; ++i)
322  {
323  int ptIdx;
324  daReadIntFromLine(a_cellLine, ptIdx);
325  a_cellstream.push_back(ptIdx);
326  }
327  }
328  return true;
329 } // iReadCellStreamVersion1
330 
331 //------------------------------------------------------------------------------
337 //------------------------------------------------------------------------------
338 bool iReadCellStreamVersion2(DaStreamReader& a_reader, int a_cellType, VecInt& a_cellstream)
339 {
340  a_cellstream.push_back(a_cellType);
341  if (a_cellType == XMU_POLYHEDRON)
342  {
343  int numFaces;
344  std::string faceString;
345  a_reader.ReadInt(numFaces);
346 
347  a_cellstream.push_back(numFaces);
348  for (int faceIdx = 0; faceIdx < numFaces; ++faceIdx)
349  {
350  if (!a_reader.NextLine())
351  {
352  XM_LOG(xmlog::error, "Unable to read cell stream.");
353  return false;
354  }
355 
356  a_reader.ReadString(faceString);
357  if (faceString != "FACE")
358  {
359  XM_LOG(xmlog::error, "Unable to read cell stream.");
360  return false;
361  }
362 
363  int faceIdxRead;
364  a_reader.ReadInt(faceIdxRead);
365  if (faceIdxRead != faceIdx)
366  {
367  XM_LOG(xmlog::error, "Unable to read cell stream.");
368  return false;
369  }
370 
371  int numPoints;
372  a_reader.ReadInt(numPoints);
373  a_cellstream.push_back(numPoints);
374  for (int i = 0; i < numPoints; ++i)
375  {
376  int ptIdx;
377  a_reader.ReadInt(ptIdx);
378  a_cellstream.push_back(ptIdx);
379  }
380  }
381  }
382  else
383  {
384  int numPoints;
385  a_reader.ReadInt(numPoints);
386  a_cellstream.push_back(numPoints);
387  for (int i = 0; i < numPoints; ++i)
388  {
389  int ptIdx;
390  a_reader.ReadInt(ptIdx);
391  a_cellstream.push_back(ptIdx);
392  }
393  }
394  return true;
395 } // iReadCellStreamVersion2
396 
397 //------------------------------------------------------------------------------
402 //------------------------------------------------------------------------------
403 bool iReadCellsVersion1(DaStreamReader& a_reader, VecInt& cellstream)
404 {
405  int cellStreamSize;
406  if (!iReadCellStreamSizeVersion1(a_reader, cellStreamSize))
407  return false;
408 
409  cellstream.reserve(cellStreamSize);
410  while ((int)cellstream.size() < cellStreamSize)
411  {
412  std::string cellLine;
413  a_reader.ReadLine(cellLine);
414  std::string cardName;
415  DaStreamReader::ReadStringFromLine(cellLine, cardName);
416  if (cardName != "CELL")
417  {
418  XM_LOG(xmlog::error, "Unable to read XmUGrid cell.");
419  return false;
420  }
421 
422  int cellType;
423  if (!iReadCellLineVersion1(a_reader, cellLine, cellType))
424  return false;
425 
426  if (!iReadCellStreamVersion1(a_reader, cellLine, cellType, cellstream))
427  return false;
428  }
429 
430  return true;
431 } // iReadCellsVersion1
432 
433 //------------------------------------------------------------------------------
438 //------------------------------------------------------------------------------
439 bool iReadCellsVersion2(DaStreamReader& a_reader, VecInt& cellstream)
440 {
441  int cellStreamSize;
442  if (!iReadCellStreamSizeVersion2(a_reader, cellStreamSize))
443  return false;
444 
445  cellstream.reserve(cellStreamSize);
446  int cellIdx = 0;
447  while ((int)cellstream.size() < cellStreamSize)
448  {
449  std::string cardName;
450  a_reader.ReadString(cardName);
451  if (cardName != "CELL")
452  {
453  XM_LOG(xmlog::error, "Unable to read XmUGrid cell.");
454  return false;
455  }
456 
457  int cellType;
458  if (!iReadCellLineVersion2(a_reader, cellIdx, cellType))
459  return false;
460 
461  if (!iReadCellStreamVersion2(a_reader, cellType, cellstream))
462  return false;
463 
464  a_reader.NextLine();
465  ++cellIdx;
466  }
467 
468  return true;
469 } // iReadCellsVersion2
470 //------------------------------------------------------------------------------
474 //------------------------------------------------------------------------------
475 void iWriteCellStream(DaStreamWriter& a_writer, const VecInt& a_cellstream)
476 {
477  // number of cell stream items
478  int cellstreamSize = (int)a_cellstream.size();
479  a_writer.WriteIntLine("CELL_STREAM", cellstreamSize);
480 
481  // cells
482  std::ostringstream out;
483  int currIdx = 0;
484  int cellIdx = 0;
485  while (currIdx < cellstreamSize)
486  {
487  int cellType = a_cellstream[currIdx++];
488  a_writer.WriteString(" CELL");
489  a_writer.AppendInt(cellIdx);
490  std::string cellTypeString = iStringFromCellType(cellType);
491  if (!cellTypeString.empty())
492  {
493  a_writer.AppendString(cellTypeString);
494  }
495  else
496  {
497  std::ostringstream err;
498  err << "Unknown cell type (" << cellType << ").";
499  XM_LOG(xmlog::error, err.str());
500  return;
501  }
502 
503  int numItems = a_cellstream[currIdx++];
504  if (cellType == XMU_POLYHEDRON)
505  {
506  int numFaces = numItems;
507  a_writer.AppendInt(numFaces);
508  a_writer.EndLine();
509  for (int faceIdx = 0; faceIdx < numFaces; ++faceIdx)
510  {
511  a_writer.WriteString(" FACE");
512  a_writer.AppendInt(faceIdx);
513  numItems = a_cellstream[currIdx++];
514  a_writer.AppendInt(numItems);
515  a_writer.AppendInts(&a_cellstream[currIdx], numItems);
516  a_writer.EndLine();
517  currIdx += numItems;
518  }
519  }
520  else
521  {
522  a_writer.AppendInt(numItems);
523  a_writer.AppendInts(&a_cellstream[currIdx], numItems);
524  a_writer.EndLine();
525  currIdx += numItems;
526  }
527  ++cellIdx;
528  }
529 } // iWriteCellStream
530 //------------------------------------------------------------------------------
536 //------------------------------------------------------------------------------
537 void iWriteUGridToStream(const XmUGrid& a_ugrid,
538  std::ostream& a_outStream,
539  bool a_binary,
540  int a_blockSize = 32 * 1024)
541 {
542  if (a_binary)
543  a_outStream << "Binary XmUGrid Version 2\n";
544  else
545  a_outStream << "ASCII XmUGrid Version 2\n";
546  DaStreamWriter writer(a_outStream, a_binary);
547  writer.SetBinaryBlockSize(a_blockSize);
548  writer.WriteVecPt3d("LOCATIONS", a_ugrid.GetLocations());
549  if (a_binary)
550  writer.WriteVecInt("CELL_STREAM", a_ugrid.GetCellstream());
551  else
552  iWriteCellStream(writer, a_ugrid.GetCellstream());
553 } // iWriteUGridToStream
554 
555 } // namespace
556 
557 //------------------------------------------------------------------------------
562 //------------------------------------------------------------------------------
563 std::shared_ptr<XmUGrid> XmReadUGridFromAsciiFile(const std::string& a_filePath)
564 {
565  std::ifstream inFile(a_filePath);
566  return XmReadUGridFromStream(inFile);
567 } // XmReadUGridFromAsciiFile
568 
569 //------------------------------------------------------------------------------
573 //------------------------------------------------------------------------------
574 std::shared_ptr<XmUGrid> XmReadUGridFromStream(std::istream& a_inStream)
575 {
576  if (a_inStream.eof())
577  {
578  return nullptr;
579  }
580 
581  std::string versionString;
582  if (!daReadLine(a_inStream, versionString))
583  {
584  XM_LOG(xmlog::error, "Unable to read file version or file type.");
585  return nullptr;
586  }
587 
588  if (versionString == "ASCII XmUGrid Version 1.0")
589  {
590  DaStreamReader reader(a_inStream, false);
591  VecPt3d locations;
592  bool success = iReadPointsVersion1(reader, locations);
593  VecInt cellstream;
594  success = success && iReadCellsVersion1(reader, cellstream);
595  if (success)
596  return XmUGrid::New(locations, cellstream);
597  }
598 
599  bool binary;
600  if (versionString == "ASCII XmUGrid Version 2")
601  binary = false;
602  else if (versionString == "Binary XmUGrid Version 2")
603  binary = true;
604  else
605  {
606  XM_LOG(xmlog::error, "Unsupported file version or file type.");
607  return nullptr;
608  }
609 
610  DaStreamReader reader(a_inStream, binary);
611  VecPt3d locations;
612  bool success = reader.ReadVecPt3d("LOCATIONS", locations);
613  VecInt cellstream;
614  if (binary)
615  success = success && reader.ReadVecInt("CELL_STREAM", cellstream);
616  else
617  success = success && iReadCellsVersion2(reader, cellstream);
618 
619  if (success)
620  return XmUGrid::New(locations, cellstream);
621 
622  return nullptr;
623 } // XmReadUGridFromStream
624 //------------------------------------------------------------------------------
629 //------------------------------------------------------------------------------
630 void XmWriteUGridToAsciiFile(std::shared_ptr<XmUGrid> a_ugrid, const std::string& a_filePath)
631 {
632  std::ofstream outFile(a_filePath);
633  XmWriteUGridToStream(a_ugrid, outFile);
634 } // XmWriteUGridToAsciiFile
635 //------------------------------------------------------------------------------
639 //------------------------------------------------------------------------------
640 void XmWriteUGridToStream(std::shared_ptr<XmUGrid> a_ugrid, std::ostream& a_outStream)
641 {
642  XmWriteUGridToStream(*a_ugrid, a_outStream);
643 } // XmWriteUGridToStream
644 //------------------------------------------------------------------------------
649 //------------------------------------------------------------------------------
650 void XmWriteUGridToStream(const XmUGrid& a_ugrid,
651  std::ostream& a_outStream,
652  bool a_binary /*= true*/)
653 {
654  iWriteUGridToStream(a_ugrid, a_outStream, a_binary);
655 } // XmWriteUGridToStream
656 
657 } // namespace xms
658 
659 #ifdef CXX_TEST
660 //------------------------------------------------------------------------------
661 // Unit Tests
662 //------------------------------------------------------------------------------
663 #include <xmsgrid/ugrid/XmUGrid.t.h>
664 
667 
668 using namespace xms;
669 
670 namespace
671 {
672 //------------------------------------------------------------------------------
675 //------------------------------------------------------------------------------
676 std::string TestFilesPath()
677 {
678  return std::string(XMS_TEST_PATH);
679 } // TestFilesPath
680 } // namespace
681 
686 //------------------------------------------------------------------------------
688 //------------------------------------------------------------------------------
690 {
691  std::shared_ptr<XmUGrid> ugrid = XmUGrid::New();
692  std::ostringstream output;
693  XmWriteUGridToStream(ugrid, output);
694 
695  std::string outputBase =
696  "ASCII XmUGrid Version 2\n"
697  "LOCATIONS 0\n"
698  "CELL_STREAM 0\n";
699  TS_ASSERT_EQUALS(outputBase, output.str());
700 } // XmUGridReaderTests::testWriteEmptyUGrid
701 //------------------------------------------------------------------------------
703 //------------------------------------------------------------------------------
705 {
706  std::shared_ptr<XmUGrid> ugrid = TEST_XmUGrid1Left90Tri();
707  std::ostringstream output;
708  XmWriteUGridToStream(ugrid, output);
709 
710  std::string outputBase =
711  "ASCII XmUGrid Version 2\n"
712  "LOCATIONS 3\n"
713  " POINT 0 0.0 0.0 0.0\n"
714  " POINT 1 20.0 0.0 0.0\n"
715  " POINT 2 0.0 20.0 0.0\n"
716  "CELL_STREAM 5\n"
717  " CELL 0 TRIANGLE 3 0 1 2\n";
718  TS_ASSERT_EQUALS(outputBase, output.str());
719 } // XmUGridUtilsTests::testWriteBasicUGrid
720 //------------------------------------------------------------------------------
722 //------------------------------------------------------------------------------
724 {
725  std::shared_ptr<XmUGrid> ugrid = TEST_XmUGridHexagonalPolyhedron();
726  std::ostringstream output;
727  XmWriteUGridToStream(ugrid, output);
728 
729  std::string outputBase =
730  "ASCII XmUGrid Version 2\n"
731  "LOCATIONS 8\n"
732  " POINT 0 0.0 0.0 10.0\n"
733  " POINT 1 10.0 0.0 10.0\n"
734  " POINT 2 10.0 10.0 10.0\n"
735  " POINT 3 0.0 10.0 10.0\n"
736  " POINT 4 0.0 0.0 0.0\n"
737  " POINT 5 10.0 0.0 0.0\n"
738  " POINT 6 10.0 10.0 0.0\n"
739  " POINT 7 0.0 10.0 0.0\n"
740  "CELL_STREAM 32\n"
741  " CELL 0 POLYHEDRON 6\n"
742  " FACE 0 4 0 1 2 3\n"
743  " FACE 1 4 4 5 7 2\n"
744  " FACE 2 4 5 6 2 1\n"
745  " FACE 3 4 6 7 3 2\n"
746  " FACE 4 4 7 4 0 3\n"
747  " FACE 5 4 4 7 6 5\n";
748  TS_ASSERT_EQUALS(outputBase, output.str());
749 } // XmUGridUtilsTests::testWritePolyhedronUGrid
750 //------------------------------------------------------------------------------
753 //------------------------------------------------------------------------------
755 {
756  std::shared_ptr<XmUGrid> ugrid = TEST_XmUGrid2dLinear();
757  std::ostringstream output;
758  XmWriteUGridToStream(ugrid, output);
759 
760  std::string outputBase =
761  "ASCII XmUGrid Version 2\n"
762  "LOCATIONS 14\n"
763  " POINT 0 0.0 0.0 0.0\n"
764  " POINT 1 10.0 0.0 0.0\n"
765  " POINT 2 20.0 0.0 0.0\n"
766  " POINT 3 30.0 0.0 0.0\n"
767  " POINT 4 40.0 0.0 0.0\n"
768  " POINT 5 0.0 10.0 0.0\n"
769  " POINT 6 10.0 10.0 0.0\n"
770  " POINT 7 20.0 10.0 0.0\n"
771  " POINT 8 40.0 10.0 0.0\n"
772  " POINT 9 0.0 20.0 0.0\n"
773  " POINT 10 10.0 20.0 0.0\n"
774  " POINT 11 20.0 20.0 0.0\n"
775  " POINT 12 30.0 20.0 0.0\n"
776  " POINT 13 40.0 20.0 0.0\n"
777  "CELL_STREAM 34\n"
778  " CELL 0 QUAD 4 0 1 6 5\n"
779  " CELL 1 PIXEL 4 1 2 6 7\n"
780  " CELL 2 TRIANGLE 3 2 3 7\n"
781  " CELL 3 POLYGON 6 3 4 8 13 12 7\n"
782  " CELL 4 POLY_LINE 3 7 11 10\n"
783  " CELL 5 LINE 2 5 9\n";
784  TS_ASSERT_EQUALS(outputBase, output.str());
785 } // XmUGridUtilsTests::testWriteLinear2dCells
786 //------------------------------------------------------------------------------
789 //------------------------------------------------------------------------------
791 {
792  std::shared_ptr<XmUGrid> ugrid = TEST_XmUGrid3dLinear();
793  std::ostringstream output;
794  XmWriteUGridToStream(ugrid, output);
795 
796  std::string outputBase =
797  "ASCII XmUGrid Version 2\n"
798  "LOCATIONS 30\n"
799  " POINT 0 0.0 0.0 0.0\n"
800  " POINT 1 10.0 0.0 0.0\n"
801  " POINT 2 20.0 0.0 0.0\n"
802  " POINT 3 30.0 0.0 0.0\n"
803  " POINT 4 40.0 0.0 0.0\n"
804  " POINT 5 0.0 10.0 0.0\n"
805  " POINT 6 10.0 10.0 0.0\n"
806  " POINT 7 20.0 10.0 0.0\n"
807  " POINT 8 30.0 10.0 0.0\n"
808  " POINT 9 40.0 10.0 0.0\n"
809  " POINT 10 0.0 20.0 0.0\n"
810  " POINT 11 10.0 20.0 0.0\n"
811  " POINT 12 20.0 20.0 0.0\n"
812  " POINT 13 30.0 20.0 0.0\n"
813  " POINT 14 40.0 20.0 0.0\n"
814  " POINT 15 0.0 0.0 10.0\n"
815  " POINT 16 10.0 0.0 10.0\n"
816  " POINT 17 20.0 0.0 10.0\n"
817  " POINT 18 30.0 0.0 10.0\n"
818  " POINT 19 40.0 0.0 10.0\n"
819  " POINT 20 0.0 10.0 10.0\n"
820  " POINT 21 10.0 10.0 10.0\n"
821  " POINT 22 20.0 10.0 10.0\n"
822  " POINT 23 30.0 10.0 10.0\n"
823  " POINT 24 40.0 10.0 10.0\n"
824  " POINT 25 0.0 20.0 10.0\n"
825  " POINT 26 10.0 20.0 10.0\n"
826  " POINT 27 20.0 20.0 10.0\n"
827  " POINT 28 30.0 20.0 10.0\n"
828  " POINT 29 40.0 20.0 10.0\n"
829  "CELL_STREAM 73\n"
830  " CELL 0 TETRA 4 0 1 5 15\n"
831  " CELL 1 VOXEL 8 1 2 6 7 16 17 21 22\n"
832  " CELL 2 HEXAHEDRON 8 2 3 8 7 17 18 23 22\n"
833  " CELL 3 POLYHEDRON 6\n"
834  " FACE 0 4 9 8 13 14\n"
835  " FACE 1 4 8 9 24 23\n"
836  " FACE 2 4 9 14 29 24\n"
837  " FACE 3 4 14 13 28 29\n"
838  " FACE 4 4 8 13 28 23\n"
839  " FACE 5 4 23 24 29 28\n"
840  " CELL 4 WEDGE 6 3 4 18 8 9 23\n"
841  " CELL 5 PYRAMID 5 5 6 11 10 20\n";
842  std::string outputString = output.str();
843  TS_ASSERT_EQUALS(outputBase, outputString);
844 } // XmUGridUtilsTests::testWriteLinear3dCells
845 //------------------------------------------------------------------------------
847 //------------------------------------------------------------------------------
849 {
850  std::string inputText =
851  "ASCII XmUGrid Version 2\n"
852  "LOCATIONS 0\n"
853  "CELL_STREAM 0\n";
854  std::istringstream input;
855  input.str(inputText);
856  std::shared_ptr<XmUGrid> ugrid = XmReadUGridFromStream(input);
857  TS_REQUIRE_NOT_NULL(ugrid);
858 
859  TS_ASSERT(ugrid->GetLocations().empty());
860  TS_ASSERT(ugrid->GetCellstream().empty());
861 } // XmUGridReaderTests::testReadEmptyUGridAsciiFile
862 //------------------------------------------------------------------------------
864 //------------------------------------------------------------------------------
866 {
867  std::string inputText =
868  "ASCII XmUGrid Version 2\n"
869  "LOCATIONS 3\n"
870  " POINT 0 0.0 0.0 0.0\n"
871  " POINT 1 20.0 0.0 0.0\n"
872  " POINT 2 0.0 20.0 0.0\n"
873  "CELL_STREAM 5\n"
874  " CELL 0 TRIANGLE 3 0 1 2\n";
875  std::istringstream input;
876  input.str(inputText);
877  std::shared_ptr<XmUGrid> ugrid = XmReadUGridFromStream(input);
878  TS_REQUIRE_NOT_NULL(ugrid);
879 
880  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid1Left90Tri();
881  VecPt3d locations = ugrid->GetLocations();
882  TS_ASSERT_EQUALS(ugridBase->GetLocations(), locations);
883  VecInt cellstream = ugrid->GetCellstream();
884  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), cellstream);
885 } // XmUGridUtilsTests::testReadBasicUGrid
886 //------------------------------------------------------------------------------
888 //------------------------------------------------------------------------------
890 {
891  std::string inputText =
892  "ASCII XmUGrid Version 2\n"
893  "LOCATIONS 8\n"
894  " POINT 0 0.0 0.0 10.0\n"
895  " POINT 1 10.0 0.0 10.0\n"
896  " POINT 2 10.0 10.0 10.0\n"
897  " POINT 3 0.0 10.0 10.0\n"
898  " POINT 4 0.0 0.0 0.0\n"
899  " POINT 5 10.0 0.0 0.0\n"
900  " POINT 6 10.0 10.0 0.0\n"
901  " POINT 7 0.0 10.0 0.0\n"
902  "CELL_STREAM 32\n"
903  " CELL 0 POLYHEDRON 6\n"
904  " FACE 0 4 0 1 2 3\n"
905  " FACE 1 4 4 5 7 2\n"
906  " FACE 2 4 5 6 2 1\n"
907  " FACE 3 4 6 7 3 2\n"
908  " FACE 4 4 7 4 0 3\n"
909  " FACE 5 4 4 7 6 5\n";
910  std::istringstream input;
911  input.str(inputText);
912  std::shared_ptr<XmUGrid> ugrid = XmReadUGridFromStream(input);
913  TS_REQUIRE_NOT_NULL(ugrid);
914 
915  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGridHexagonalPolyhedron();
916  TS_ASSERT_EQUALS(ugridBase->GetLocations(), ugrid->GetLocations());
917  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), ugrid->GetCellstream());
918 } // XmUGridUtilsTests::testReadPolyhedronUGrid
919 //------------------------------------------------------------------------------
921 //------------------------------------------------------------------------------
923 {
924  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid2dLinear();
925 
926  // write
927  std::ostringstream output;
928  XmWriteUGridToStream(ugridBase, output);
929 
930  // read
931  std::istringstream input;
932  input.str(output.str());
933  std::shared_ptr<XmUGrid> ugridOut = XmReadUGridFromStream(input);
934 
935  TS_ASSERT_EQUALS(ugridBase->GetLocations(), ugridOut->GetLocations());
936  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), ugridOut->GetCellstream());
937 } // XmUGridUtilsTests::testLinear2dWriteThenRead
938 //------------------------------------------------------------------------------
940 //------------------------------------------------------------------------------
942 {
943  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid3dLinear();
944 
945  // write
946  std::ostringstream output;
947  XmWriteUGridToStream(ugridBase, output);
948 
949  // read
950  std::istringstream input;
951  input.str(output.str());
952  std::shared_ptr<XmUGrid> ugridOut = XmReadUGridFromStream(input);
953 
954  TS_ASSERT_EQUALS(ugridBase->GetLocations(), ugridOut->GetLocations());
955  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), ugridOut->GetCellstream());
956 } // XmUGridUtilsTests::testLinear3dWriteThenRead
957 //------------------------------------------------------------------------------
959 //------------------------------------------------------------------------------
961 {
962  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid3dLinear();
963 
964  // write
965  std::string outFileName(TestFilesPath() + "3d_grid_linear.xmugrid");
966  std::ofstream output(outFileName);
967  XmWriteUGridToStream(ugridBase, output);
968  output.close();
969 
970  // read
971  std::ifstream input(TestFilesPath() + "3d_grid_linear.xmugrid");
972  std::shared_ptr<XmUGrid> ugridOut = XmReadUGridFromStream(input);
973  input.close();
974  TS_REQUIRE_NOT_NULL(ugridOut);
975 
976  TS_ASSERT_EQUALS(ugridBase->GetLocations(), ugridOut->GetLocations());
977  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), ugridOut->GetCellstream());
978 } // XmUGridUtilsTests::testWriteThenReadUGridFile
980 //------------------------------------------------------------------------------
982 //------------------------------------------------------------------------------
984 {
985  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid3dLinear();
986 
987  // write
988  std::string outFileName(TestFilesPath() + "3d_grid_linear.xmugrid");
989  XmWriteUGridToAsciiFile(ugridBase, outFileName);
990 
991  // read
992  std::string input(TestFilesPath() + "3d_grid_linear.xmugrid");
993  std::shared_ptr<XmUGrid> ugridOut = XmReadUGridFromAsciiFile(input);
994  TS_REQUIRE_NOT_NULL(ugridOut);
995 
996  TS_ASSERT_EQUALS(ugridBase->GetLocations(), ugridOut->GetLocations());
997  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), ugridOut->GetCellstream());
998 } // XmUGridUtilsTests::testWriteThenReadUGridFile
1000 //------------------------------------------------------------------------------
1002 //------------------------------------------------------------------------------
1004 {
1005  std::string inputText =
1006  "ASCII XmUGrid Version 1.0\n"
1007  "NUM_POINTS 30\n"
1008  " POINT 0.0 0.0 0.0\n"
1009  " POINT 10.0 0.0 0.0\n"
1010  " POINT 20.0 0.0 0.0\n"
1011  " POINT 30.0 0.0 0.0\n"
1012  " POINT 40.0 0.0 0.0\n"
1013  " POINT 0.0 10.0 0.0\n"
1014  " POINT 10.0 10.0 0.0\n"
1015  " POINT 20.0 10.0 0.0\n"
1016  " POINT 30.0 10.0 0.0\n"
1017  " POINT 40.0 10.0 0.0\n"
1018  " POINT 0.0 20.0 0.0\n"
1019  " POINT 10.0 20.0 0.0\n"
1020  " POINT 20.0 20.0 0.0\n"
1021  " POINT 30.0 20.0 0.0\n"
1022  " POINT 40.0 20.0 0.0\n"
1023  " POINT 0.0 0.0 10.0\n"
1024  " POINT 10.0 0.0 10.0\n"
1025  " POINT 20.0 0.0 10.0\n"
1026  " POINT 30.0 0.0 10.0\n"
1027  " POINT 40.0 0.0 10.0\n"
1028  " POINT 0.0 10.0 10.0\n"
1029  " POINT 10.0 10.0 10.0\n"
1030  " POINT 20.0 10.0 10.0\n"
1031  " POINT 30.0 10.0 10.0\n"
1032  " POINT 40.0 10.0 10.0\n"
1033  " POINT 0.0 20.0 10.0\n"
1034  " POINT 10.0 20.0 10.0\n"
1035  " POINT 20.0 20.0 10.0\n"
1036  " POINT 30.0 20.0 10.0\n"
1037  " POINT 40.0 20.0 10.0\n"
1038  "NUM_CELL_ITEMS 73\n"
1039  " CELL 10 4 0 1 5 15\n"
1040  " CELL 11 8 1 2 6 7 16 17 21 22\n"
1041  " CELL 12 8 2 3 8 7 17 18 23 22\n"
1042  " CELL 42 6\n"
1043  " 4 9 8 13 14\n"
1044  " 4 8 9 24 23\n"
1045  " 4 9 14 29 24\n"
1046  " 4 14 13 28 29\n"
1047  " 4 8 13 28 23\n"
1048  " 4 23 24 29 28\n"
1049  " CELL 13 6 3 4 18 8 9 23\n"
1050  " CELL 14 5 5 6 11 10 20\n";
1051  std::istringstream input(inputText);
1052  std::shared_ptr<XmUGrid> ugrid = XmReadUGridFromStream(input);
1053  TS_REQUIRE_NOT_NULL(ugrid);
1054 
1055  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid3dLinear();
1056  TS_ASSERT_EQUALS(ugridBase->GetLocations(), ugrid->GetLocations());
1057  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), ugrid->GetCellstream());
1058 } // XmUGridUtilsTests::testReadVersion1Dot0File
1059 //------------------------------------------------------------------------------
1061 //------------------------------------------------------------------------------
1063 {
1064  std::shared_ptr<XmUGrid> ugridOut = TEST_XmUGrid3dLinear();
1065  std::ostringstream output;
1066  bool binary = true;
1067  iWriteUGridToStream(*ugridOut, output, binary, 120);
1068 
1069  std::string outputBase =
1070  "Binary XmUGrid Version 2\n"
1071  "LOCATIONS 30\n"
1072  "BINARY_BLOCK 34 120\n"
1073  "eAFjYMAHVBywy5rgELfDIe6CIQ4AXnwB2w\n"
1074  "BINARY_BLOCK 46 120\n"
1075  "eAFjYEAGKg7IPAYGGB9Gw2RNoOrQxe1wiLtgiAMAzoQDzw\n"
1076  "BINARY_BLOCK 46 120\n"
1077  "eAFjYEAGJg7IPAYGFSgfXRzGh9EwXXY41LtgiAMA4KQEHw\n"
1078  "BINARY_BLOCK 43 120\n"
1079  "eAFjYMAGVBwgojAapgbGN4HKo4vb4RB3wRAHAL7kA88\n"
1080  "BINARY_BLOCK 35 120\n"
1081  "eAFjYEAGKg4QHiHaBIc6OxziLhjiAC77BcM\n"
1082  "BINARY_BLOCK 44 120\n"
1083  "eAFjYEAGJg4QngoajS4O48NomHo7qD50cRcMcQBBGwYT\n"
1084  "CELL_STREAM 73\n"
1085  "BINARY_BLOCK 90 120\n"
1086  "eAEtzQEKgCAQAMGrzLKCTMj+0P8f2C4kDBzHcm4RkeAbMOPEjhXuRmQsqLhwo+OAnc30z3Y2DQ/sXnjDvwo+QSwBSA\n"
1087  "BINARY_BLOCK 82 120\n"
1088  "eAE1yzEOACAIBEEKY7DQVgv9/zfdLSgmhIPLiJhYaEgMHGyYuXt/MDdzt3dhXt3Kquu044+/3js+aLQB2w\n"
1089  "BINARY_BLOCK 52 52\n"
1090  "eAFjZmBgYAFiISDmAGJOIBYHYj4gZoViNiDNDcRcQCwCxAAN9ACJ\n";
1091  std::string outputString = output.str();
1092  TS_ASSERT_EQUALS(outputBase, outputString);
1093 
1094  std::istringstream input(outputString);
1095  std::shared_ptr<XmUGrid> ugridIn = XmReadUGridFromStream(input);
1096  TS_REQUIRE_NOT_NULL(ugridIn);
1097  TS_ASSERT_EQUALS(ugridOut->GetLocations(), ugridIn->GetLocations());
1098  TS_ASSERT_EQUALS(ugridOut->GetCellstream(), ugridIn->GetCellstream());
1099 } // XmUGridUtilsTests::testWriteThenReadUGridBinary
1100 //------------------------------------------------------------------------------
1102 //------------------------------------------------------------------------------
1104 {
1105  TS_ASSERT_EQUALS("INVALID_CELL_TYPE", iStringFromCellType(XMU_INVALID_CELL_TYPE));
1106  TS_ASSERT_EQUALS("EMPTY_CELL", iStringFromCellType(XMU_EMPTY_CELL));
1107  TS_ASSERT_EQUALS("VERTEX", iStringFromCellType(XMU_VERTEX));
1108  TS_ASSERT_EQUALS("POLY_VERTEX", iStringFromCellType(XMU_POLY_VERTEX));
1109  TS_ASSERT_EQUALS("LINE", iStringFromCellType(XMU_LINE));
1110  TS_ASSERT_EQUALS("POLY_LINE", iStringFromCellType(XMU_POLY_LINE));
1111  TS_ASSERT_EQUALS("TRIANGLE", iStringFromCellType(XMU_TRIANGLE));
1112  TS_ASSERT_EQUALS("TRIANGLE_STRIP", iStringFromCellType(XMU_TRIANGLE_STRIP));
1113  TS_ASSERT_EQUALS("POLYGON", iStringFromCellType(XMU_POLYGON));
1114  TS_ASSERT_EQUALS("PIXEL", iStringFromCellType(XMU_PIXEL));
1115  TS_ASSERT_EQUALS("QUAD", iStringFromCellType(XMU_QUAD));
1116  TS_ASSERT_EQUALS("TETRA", iStringFromCellType(XMU_TETRA));
1117  TS_ASSERT_EQUALS("VOXEL", iStringFromCellType(XMU_VOXEL));
1118  TS_ASSERT_EQUALS("HEXAHEDRON", iStringFromCellType(XMU_HEXAHEDRON));
1119  TS_ASSERT_EQUALS("WEDGE", iStringFromCellType(XMU_WEDGE));
1120  TS_ASSERT_EQUALS("PYRAMID", iStringFromCellType(XMU_PYRAMID));
1121  TS_ASSERT_EQUALS("PENTAGONAL_PRISM", iStringFromCellType(XMU_PENTAGONAL_PRISM));
1122  TS_ASSERT_EQUALS("HEXAGONAL_PRISM", iStringFromCellType(XMU_HEXAGONAL_PRISM));
1123  TS_ASSERT_EQUALS("QUADRATIC_EDGE", iStringFromCellType(XMU_QUADRATIC_EDGE));
1124  TS_ASSERT_EQUALS("QUADRATIC_TRIANGLE", iStringFromCellType(XMU_QUADRATIC_TRIANGLE));
1125  TS_ASSERT_EQUALS("QUADRATIC_QUAD", iStringFromCellType(XMU_QUADRATIC_QUAD));
1126  TS_ASSERT_EQUALS("QUADRATIC_POLYGON", iStringFromCellType(XMU_QUADRATIC_POLYGON));
1127  TS_ASSERT_EQUALS("QUADRATIC_TETRA", iStringFromCellType(XMU_QUADRATIC_TETRA));
1128  TS_ASSERT_EQUALS("QUADRATIC_HEXAHEDRON", iStringFromCellType(XMU_QUADRATIC_HEXAHEDRON));
1129  TS_ASSERT_EQUALS("QUADRATIC_WEDGE", iStringFromCellType(XMU_QUADRATIC_WEDGE));
1130  TS_ASSERT_EQUALS("QUADRATIC_PYRAMID", iStringFromCellType(XMU_QUADRATIC_PYRAMID));
1131  TS_ASSERT_EQUALS("BIQUADRATIC_QUAD", iStringFromCellType(XMU_BIQUADRATIC_QUAD));
1132  TS_ASSERT_EQUALS("TRIQUADRATIC_HEXAHEDRON", iStringFromCellType(XMU_TRIQUADRATIC_HEXAHEDRON));
1133  TS_ASSERT_EQUALS("QUADRATIC_LINEAR_QUAD", iStringFromCellType(XMU_QUADRATIC_LINEAR_QUAD));
1134  TS_ASSERT_EQUALS("QUADRATIC_LINEAR_WEDGE", iStringFromCellType(XMU_QUADRATIC_LINEAR_WEDGE));
1135  TS_ASSERT_EQUALS("BIQUADRATIC_QUADRATIC_WEDGE",
1136  iStringFromCellType(XMU_BIQUADRATIC_QUADRATIC_WEDGE));
1137  TS_ASSERT_EQUALS("BIQUADRATIC_QUADRATIC_HEXAHEDRON",
1138  iStringFromCellType(XMU_BIQUADRATIC_QUADRATIC_HEXAHEDRON));
1139  TS_ASSERT_EQUALS("BIQUADRATIC_TRIANGLE", iStringFromCellType(XMU_BIQUADRATIC_TRIANGLE));
1140  TS_ASSERT_EQUALS("CUBIC_LINE", iStringFromCellType(XMU_CUBIC_LINE));
1141  TS_ASSERT_EQUALS("CONVEX_POINT_SET", iStringFromCellType(XMU_CONVEX_POINT_SET));
1142  TS_ASSERT_EQUALS("POLYHEDRON", iStringFromCellType(XMU_POLYHEDRON));
1143  TS_ASSERT_EQUALS("PARAMETRIC_CURVE", iStringFromCellType(XMU_PARAMETRIC_CURVE));
1144  TS_ASSERT_EQUALS("PARAMETRIC_SURFACE", iStringFromCellType(XMU_PARAMETRIC_SURFACE));
1145  TS_ASSERT_EQUALS("PARAMETRIC_TRI_SURFACE", iStringFromCellType(XMU_PARAMETRIC_TRI_SURFACE));
1146  TS_ASSERT_EQUALS("PARAMETRIC_QUAD_SURFACE", iStringFromCellType(XMU_PARAMETRIC_QUAD_SURFACE));
1147  TS_ASSERT_EQUALS("PARAMETRIC_TETRA_REGION", iStringFromCellType(XMU_PARAMETRIC_TETRA_REGION));
1148  TS_ASSERT_EQUALS("PARAMETRIC_HEX_REGION", iStringFromCellType(XMU_PARAMETRIC_HEX_REGION));
1149  TS_ASSERT_EQUALS("HIGHER_ORDER_EDGE", iStringFromCellType(XMU_HIGHER_ORDER_EDGE));
1150  TS_ASSERT_EQUALS("HIGHER_ORDER_TRIANGLE", iStringFromCellType(XMU_HIGHER_ORDER_TRIANGLE));
1151  TS_ASSERT_EQUALS("HIGHER_ORDER_QUAD", iStringFromCellType(XMU_HIGHER_ORDER_QUAD));
1152  TS_ASSERT_EQUALS("HIGHER_ORDER_POLYGON", iStringFromCellType(XMU_HIGHER_ORDER_POLYGON));
1153  TS_ASSERT_EQUALS("HIGHER_ORDER_TETRAHEDRON", iStringFromCellType(XMU_HIGHER_ORDER_TETRAHEDRON));
1154  TS_ASSERT_EQUALS("HIGHER_ORDER_WEDGE", iStringFromCellType(XMU_HIGHER_ORDER_WEDGE));
1155  TS_ASSERT_EQUALS("HIGHER_ORDER_PYRAMID", iStringFromCellType(XMU_HIGHER_ORDER_PYRAMID));
1156  TS_ASSERT_EQUALS("HIGHER_ORDER_HEXAHEDRON", iStringFromCellType(XMU_HIGHER_ORDER_HEXAHEDRON));
1157 
1158  TS_ASSERT_EQUALS(XMU_INVALID_CELL_TYPE, iCellTypeFromString("INVALID_CELL_TYPE"));
1159  TS_ASSERT_EQUALS(XMU_EMPTY_CELL, iCellTypeFromString("EMPTY_CELL"));
1160  TS_ASSERT_EQUALS(XMU_VERTEX, iCellTypeFromString("VERTEX"));
1161  TS_ASSERT_EQUALS(XMU_POLY_VERTEX, iCellTypeFromString("POLY_VERTEX"));
1162  TS_ASSERT_EQUALS(XMU_LINE, iCellTypeFromString("LINE"));
1163  TS_ASSERT_EQUALS(XMU_POLY_LINE, iCellTypeFromString("POLY_LINE"));
1164  TS_ASSERT_EQUALS(XMU_TRIANGLE, iCellTypeFromString("TRIANGLE"));
1165  TS_ASSERT_EQUALS(XMU_TRIANGLE_STRIP, iCellTypeFromString("TRIANGLE_STRIP"));
1166  TS_ASSERT_EQUALS(XMU_POLYGON, iCellTypeFromString("POLYGON"));
1167  TS_ASSERT_EQUALS(XMU_PIXEL, iCellTypeFromString("PIXEL"));
1168  TS_ASSERT_EQUALS(XMU_QUAD, iCellTypeFromString("QUAD"));
1169  TS_ASSERT_EQUALS(XMU_TETRA, iCellTypeFromString("TETRA"));
1170  TS_ASSERT_EQUALS(XMU_VOXEL, iCellTypeFromString("VOXEL"));
1171  TS_ASSERT_EQUALS(XMU_HEXAHEDRON, iCellTypeFromString("HEXAHEDRON"));
1172  TS_ASSERT_EQUALS(XMU_WEDGE, iCellTypeFromString("WEDGE"));
1173  TS_ASSERT_EQUALS(XMU_PYRAMID, iCellTypeFromString("PYRAMID"));
1174  TS_ASSERT_EQUALS(XMU_PENTAGONAL_PRISM, iCellTypeFromString("PENTAGONAL_PRISM"));
1175  TS_ASSERT_EQUALS(XMU_HEXAGONAL_PRISM, iCellTypeFromString("HEXAGONAL_PRISM"));
1176  TS_ASSERT_EQUALS(XMU_QUADRATIC_EDGE, iCellTypeFromString("QUADRATIC_EDGE"));
1177  TS_ASSERT_EQUALS(XMU_QUADRATIC_TRIANGLE, iCellTypeFromString("QUADRATIC_TRIANGLE"));
1178  TS_ASSERT_EQUALS(XMU_QUADRATIC_QUAD, iCellTypeFromString("QUADRATIC_QUAD"));
1179  TS_ASSERT_EQUALS(XMU_QUADRATIC_POLYGON, iCellTypeFromString("QUADRATIC_POLYGON"));
1180  TS_ASSERT_EQUALS(XMU_QUADRATIC_TETRA, iCellTypeFromString("QUADRATIC_TETRA"));
1181  TS_ASSERT_EQUALS(XMU_QUADRATIC_HEXAHEDRON, iCellTypeFromString("QUADRATIC_HEXAHEDRON"));
1182  TS_ASSERT_EQUALS(XMU_QUADRATIC_WEDGE, iCellTypeFromString("QUADRATIC_WEDGE"));
1183  TS_ASSERT_EQUALS(XMU_QUADRATIC_PYRAMID, iCellTypeFromString("QUADRATIC_PYRAMID"));
1184  TS_ASSERT_EQUALS(XMU_BIQUADRATIC_QUAD, iCellTypeFromString("BIQUADRATIC_QUAD"));
1185  TS_ASSERT_EQUALS(XMU_TRIQUADRATIC_HEXAHEDRON, iCellTypeFromString("TRIQUADRATIC_HEXAHEDRON"));
1186  TS_ASSERT_EQUALS(XMU_QUADRATIC_LINEAR_QUAD, iCellTypeFromString("QUADRATIC_LINEAR_QUAD"));
1187  TS_ASSERT_EQUALS(XMU_QUADRATIC_LINEAR_WEDGE, iCellTypeFromString("QUADRATIC_LINEAR_WEDGE"));
1188  TS_ASSERT_EQUALS(XMU_BIQUADRATIC_QUADRATIC_WEDGE,
1189  iCellTypeFromString("BIQUADRATIC_QUADRATIC_WEDGE"));
1190  TS_ASSERT_EQUALS(XMU_BIQUADRATIC_QUADRATIC_HEXAHEDRON,
1191  iCellTypeFromString("BIQUADRATIC_QUADRATIC_HEXAHEDRON"));
1192  TS_ASSERT_EQUALS(XMU_BIQUADRATIC_TRIANGLE, iCellTypeFromString("BIQUADRATIC_TRIANGLE"));
1193  TS_ASSERT_EQUALS(XMU_CUBIC_LINE, iCellTypeFromString("CUBIC_LINE"));
1194  TS_ASSERT_EQUALS(XMU_CONVEX_POINT_SET, iCellTypeFromString("CONVEX_POINT_SET"));
1195  TS_ASSERT_EQUALS(XMU_POLYHEDRON, iCellTypeFromString("POLYHEDRON"));
1196  TS_ASSERT_EQUALS(XMU_PARAMETRIC_CURVE, iCellTypeFromString("PARAMETRIC_CURVE"));
1197  TS_ASSERT_EQUALS(XMU_PARAMETRIC_SURFACE, iCellTypeFromString("PARAMETRIC_SURFACE"));
1198  TS_ASSERT_EQUALS(XMU_PARAMETRIC_TRI_SURFACE, iCellTypeFromString("PARAMETRIC_TRI_SURFACE"));
1199  TS_ASSERT_EQUALS(XMU_PARAMETRIC_QUAD_SURFACE, iCellTypeFromString("PARAMETRIC_QUAD_SURFACE"));
1200  TS_ASSERT_EQUALS(XMU_PARAMETRIC_TETRA_REGION, iCellTypeFromString("PARAMETRIC_TETRA_REGION"));
1201  TS_ASSERT_EQUALS(XMU_PARAMETRIC_HEX_REGION, iCellTypeFromString("PARAMETRIC_HEX_REGION"));
1202  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_EDGE, iCellTypeFromString("HIGHER_ORDER_EDGE"));
1203  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_TRIANGLE, iCellTypeFromString("HIGHER_ORDER_TRIANGLE"));
1204  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_QUAD, iCellTypeFromString("HIGHER_ORDER_QUAD"));
1205  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_POLYGON, iCellTypeFromString("HIGHER_ORDER_POLYGON"));
1206  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_TETRAHEDRON, iCellTypeFromString("HIGHER_ORDER_TETRAHEDRON"));
1207  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_WEDGE, iCellTypeFromString("HIGHER_ORDER_WEDGE"));
1208  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_PYRAMID, iCellTypeFromString("HIGHER_ORDER_PYRAMID"));
1209  TS_ASSERT_EQUALS(XMU_HIGHER_ORDER_HEXAHEDRON, iCellTypeFromString("HIGHER_ORDER_HEXAHEDRON"));
1210  TS_ASSERT_EQUALS(50, iCellTypeStringIntPair().size());
1211 }
1212 // XmUGridUtilsTests::testCellStringToEnum
1213 //------------------------------------------------------------------------------
1215 //------------------------------------------------------------------------------
1216 //#define SPEEDTEST
1217 #ifdef SPEEDTEST
1218 #include <boost/timer/timer.hpp>
1219 #endif
1221 {
1222 #ifdef SPEEDTEST
1223  int rows = 1000;
1224  int cols = 500;
1225  int lays = 4;
1226  std::shared_ptr<xms::XmUGrid> grid;
1227  {
1228  boost::timer::cpu_timer timer;
1229  grid = TEST_XmUBuildPolyhedronUgrid(rows, cols, lays);
1230  std::cerr << "Build time: " + timer.format();
1231  }
1232 
1233  {
1234  std::ofstream output("speed_test_out.txt");
1235  boost::timer::cpu_timer timer;
1236  bool binary = true;
1237  XmWriteUGridToStream(*grid, output, binary);
1238  std::cerr << "Write time: " + timer.format();
1239  }
1240 
1241  std::shared_ptr<XmUGrid> gridRead;
1242  {
1243  std::ifstream input("speed_test_out.txt");
1244  boost::timer::cpu_timer timer;
1245  gridRead = XmReadUGridFromStream(input);
1246  std::cerr << "Read time: " + timer.format();
1247  }
1248 
1249  TS_ASSERT_EQUALS(grid->GetLocations(), gridRead->GetLocations());
1250  TS_ASSERT_EQUALS(grid->GetCellstream(), gridRead->GetCellstream());
1251 
1252 #endif
1253 } // XmUGridUtilsTests::testLargeUGridBinarySpeed
1254 //------------------------------------------------------------------------------
1257 //------------------------------------------------------------------------------
1259 {
1260  std::string inputText =
1261  "ASCII XmUGrid Version 2\n"
1262  "LOCATIONS 3\n"
1263  " POINT 0 0.0 0.0 0.0\n"
1264  " POINT 1 20.0 0.0 0.0\n"
1265  " POINT 2 0.0 20.0 0.0\n"
1266  "CELL_STREAM 5\n"
1267  " CELL 0 TRIANGLE 3 0 1 2\n"
1268  "LEFT OVER LINE\n";
1269  std::istringstream input;
1270  input.str(inputText);
1271  std::shared_ptr<XmUGrid> ugrid = XmReadUGridFromStream(input);
1272  TS_REQUIRE_NOT_NULL(ugrid);
1273 
1274  std::shared_ptr<XmUGrid> ugridBase = TEST_XmUGrid1Left90Tri();
1275  VecPt3d locations = ugrid->GetLocations();
1276  TS_ASSERT_EQUALS(ugridBase->GetLocations(), locations);
1277  VecInt cellstream = ugrid->GetCellstream();
1278  TS_ASSERT_EQUALS(ugridBase->GetCellstream(), cellstream);
1279 
1280  // read ignores leftover line
1281  std::string line;
1282  TS_ASSERT(daReadLine(input, line));
1283  TS_ASSERT_EQUALS("LEFT OVER LINE", line);
1284 } // XmUGridUtilsTests::testReadReadyAtNextLine
1285 
1286 #endif
std::shared_ptr< XmUGrid > TEST_XmUGrid1Left90Tri()
Builds a 1 cell (left 90 degree triangle) 2D XmUGrid for testing.
Definition: XmUGrid.cpp:3639
std::shared_ptr< XmUGrid > TEST_XmUGrid3dLinear()
Builds an XmUGrid with supported 3D linear cells for testing.
Definition: XmUGrid.cpp:3705
#define XM_LOG(A, B)
Contains the XmUGrid Class and supporting data types.
bool daReadLine(std::istream &a_inStream, std::string &a_line)
std::vector< int > VecInt
bool ReadString(std::string &a_val)
void testLinear2dWriteThenRead()
Test reading an ASCII file for a single triangle UGrid.
std::shared_ptr< XmUGrid > XmReadUGridFromStream(std::istream &a_inStream)
Read an XmUGrid from ASCII text from an input stream.
void testReadBasicUGrid()
Test reading an ASCII file for a single triangle UGrid.
std::shared_ptr< xms::XmUGrid > TEST_XmUBuildPolyhedronUgrid(int a_rows, int a_cols, int a_lays)
Builds a UGrid of Quads at 1 spacing for rows & cols specified.
Definition: XmUGrid.cpp:3883
void AppendInts(const int *a_vals, int a_numVals)
void testReadPolyhedronUGrid()
Test reading an ASCII file for a single triangle UGrid.
void testWriteLinear2dCells()
Test writing an ASCII file for UGrid with supported 1D and 2D linear cell types.
Contains IO functions as well as several utility functions for XmUGrid.
void WriteIntLine(const char *a_name, int a_val)
void XmWriteUGridToStream(std::shared_ptr< XmUGrid > a_ugrid, std::ostream &a_outStream)
Save an XmUGrid ASCII text to output stream.
void testWriteEmptyUGrid()
Test writing an ASCII file for an empty UGrid.
const VecPt3d & GetLocations() const
Get vector of UGrid points.
Definition: XmUGrid.cpp:3101
bool ReadInt(int &a_val)
void testWriteThenReadUGridBinary()
Test writing then reading an XmUGrid binary file.
std::shared_ptr< XmUGrid > TEST_XmUGrid2dLinear()
Builds an XmUGrid with supported 1D and 2D linear cells for testing.
Definition: XmUGrid.cpp:3679
void testWriteBasicUGrid()
Test writing an ASCII file for a single triangle UGrid.
void testWriteLinear3dCells()
Test writing an ASCII file for UGrid with supported 1D and 2D linear cell types.
bool ReadIntLine(const char *a_name, int &a_val)
bool ReadLine(std::string &a_line)
void WriteString(const char *a_string)
bool ReadVecInt(const char *a_name, VecInt &a_vec)
void XmWriteUGridToAsciiFile(std::shared_ptr< XmUGrid > a_ugrid, const std::string &a_filePath)
Write an XmUGrid to an ASCII file.
void testLinear3dWriteThenRead()
Test reading an ASCII file for a single triangle UGrid.
bool Read3DoubleLine(const char *a_name, double &a_val1, double &a_val2, double &a_val3)
void testWriteThenReadUGridFileToAscii()
[snip_test_WriteReadAscii]
std::shared_ptr< XmUGrid > TEST_XmUGridHexagonalPolyhedron()
Builds a 1 cell hexagon with cell type polyhedron.
Definition: XmUGrid.cpp:3740
void testWriteThenReadUGridFile()
Test reading from file.
void AppendString(const std::string &a_val)
void testCellStringToEnum()
Test conversion between cell type string and cell type enum.
void testLargeUGridBinarySpeed()
Tests creating a large UGrid and checks the time spent.
Geometry for an unstructured grid. An XmUGrid is defined as a vector of 3d points and a stream of cel...
Definition: XmUGrid.h:115
#define TS_REQUIRE_NOT_NULL(pointer)
#define XM_ASSERT(x)
XMS Namespace.
Definition: geoms.cpp:34
void AppendInt(int a_val)
const VecInt & GetCellstream() const
Get cell stream vector for the entire UGrid. A cellstream is defined as follows: Polyhedrons: Cell ty...
Definition: XmUGrid.cpp:3346
void testWritePolyhedronUGrid()
Test writing an ASCII file for a single polyhedron UGrid.
static bool ReadStringFromLine(std::string &a_line, std::string &a_val)
void testReadReadyAtNextLine()
Test reading XmUGrid to make sure it doesn&#39;t read the whole file. Should leave stream at next line af...
bool ReadVecPt3d(const char *a_name, VecPt3d &a_vec)
void testReadEmptyUGridAsciiFile()
Test reading an ASCII file for an empty UGrid.
static std::shared_ptr< XmUGrid > New()
Create a new XmUGrid.
Definition: XmUGrid.cpp:2874
bool daReadIntFromLine(std::string &a_line, int &a_val)
std::vector< Pt3d > VecPt3d
std::shared_ptr< XmUGrid > XmReadUGridFromAsciiFile(const std::string &a_filePath)
Read XmUGrid from an ASCII file.
void testReadVersion1Dot0File()
[snip_test_WriteReadAscii]