VAPOR3 3.9.4
Grid.h
Go to the documentation of this file.
1#ifndef _Grid_
2#define _Grid_
3
4#include <algorithm>
5#include <iostream>
6#include <ostream>
7#include <vector>
8#include <cassert>
9#include <array>
10#include <string>
11#include <limits>
12#include "vapor/VAssert.h"
13#include <memory>
14#include <vapor/common.h>
15
16#ifdef WIN32
17 #pragma warning(disable : 4661 4251) // needed for template class
18#endif
19
20namespace VAPoR {
21
23using CoordType = std::array<double, 3>;
24
26using DimsType = std::array<size_t, 3>;
27
55//
57public:
86 Grid(const std::vector<size_t> &dims, const std::vector<size_t> &bs, const std::vector<float *> &blks, size_t topology_dimension);
87 Grid(const DimsType &dims, const DimsType &bs, const std::vector<float *> &blks, size_t topology_dimension);
88
90 virtual ~Grid() = default;
91
100 const DimsType &GetDimensions() const { return _dims; }
101
107 size_t GetNumDimensions() const { return GetNumDimensions(_dims); }
108
115 static size_t GetNumDimensions(DimsType dims);
116
127 virtual DimsType GetCoordDimensions(size_t dim) const = 0;
128
129 virtual std::string GetType() const = 0;
130
139 //
140 virtual size_t GetGeometryDim() const = 0;
141
142 virtual const DimsType &GetNodeDimensions() const = 0;
143 virtual const size_t GetNumNodeDimensions() const = 0;
144 virtual const DimsType &GetCellDimensions() const = 0;
145 virtual const size_t GetNumCellDimensions() const = 0;
146
155 //
156 const std::vector<size_t> GetDimensionInBlks() const { return (_bdimsDeprecated); }
157
162 //
163 const std::vector<size_t> &GetBlockSize() const { return (_bsDeprecated); }
164
168 const std::vector<float *> &GetBlks() const { return (_blks); };
169
182 virtual float GetValueAtIndex(const DimsType &indices) const;
183
185 //
186 virtual float GetValueAtIndex(const std::vector<size_t> &indices) const
187 {
188 DimsType a = {0, 0, 0};
189 CopyToArr3(indices, a);
190 return (GetValueAtIndex(a));
191 }
192
198 virtual void SetValue(const DimsType &indices, float value);
199
201 //
202 virtual void SetValue(const size_t indices[3], float value)
203 {
204 DimsType i3 = {0, 0, 0};
205 CopyToArr3(indices, GetNodeDimensions().size(), i3);
206 SetValue(i3, value);
207 }
208
217 //
218 virtual float AccessIJK(size_t i, size_t j = 0, size_t k = 0) const;
219
220 void SetValueIJK(size_t i, size_t j, size_t k, float v);
221 void SetValueIJK(size_t i, size_t j, float v) { SetValueIJK(i, j, 0, v); }
222 void SetValueIJK(size_t i, float v) { SetValueIJK(i, 0, 0, v); }
223
244 virtual float GetValue(const CoordType &coords) const;
245
247 //
248 virtual float GetValue(const std::vector<double> &coords) const
249 {
250 CoordType c3 = {0.0, 0.0, 0.0};
251 CopyToArr3(coords, c3);
252 return (GetValue(c3));
253 };
254
256 //
257 virtual float GetValue(const double coords[]) const
258 {
259 CoordType c3 = {0.0, 0.0, 0.0};
260 CopyToArr3(coords, GetGeometryDim(), c3);
261 return (GetValue(c3));
262 }
263
264 virtual float GetValue(double x, double y) const
265 {
266 double coords[] = {x, y, 0.0};
267 return (GetValue(coords));
268 }
269 virtual float GetValue(double x, double y, double z) const
270 {
271 double coords[] = {x, y, z};
272 return (GetValue(coords));
273 }
274
289 virtual void GetUserExtents(CoordType &minu, CoordType &maxu) const;
290
292 //
293 virtual void GetUserExtents(double minu[3], double maxu[3]) const
294 {
295 CoordType minu3 = {0.0, 0.0, 0.0};
296 CoordType maxu3 = {0.0, 0.0, 0.0};
297 GetUserExtents(minu3, maxu3);
298 CopyFromArr3(minu3, minu);
299 CopyFromArr3(maxu3, maxu);
300 }
301
303 //
304 virtual void GetUserExtents(std::vector<double> &minu, std::vector<double> &maxu) const
305 {
306 CoordType minu3 = {0.0, 0.0, 0.0};
307 CoordType maxu3 = {0.0, 0.0, 0.0};
308 GetUserExtents(minu3, maxu3);
309 CopyFromArr3(minu3, minu);
310 CopyFromArr3(maxu3, maxu);
311
312 // Much of the use of this method assumes that the size of the minu,maxu
313 // vectors can be used to determine the number of coordinates. So we
314 // maintain this property for now.
315 //
316 minu.resize(GetGeometryDim());
317 maxu.resize(GetGeometryDim());
318 }
319
346 virtual void GetBoundingBox(const DimsType &min, const DimsType &max, CoordType &minu, CoordType &maxu) const = 0;
347
348 virtual void GetBoundingBox(const std::vector<size_t> &min, const std::vector<size_t> &max, std::vector<double> &minu, std::vector<double> &maxu) const
349 {
350 VAssert(min.size() == max.size());
351 DimsType min3 = {0, 0, 0};
352 DimsType max3 = {0, 0, 0};
353 CoordType minu3 = {0.0, 0.0, 0.0};
354 CoordType maxu3 = {0.0, 0.0, 0.0};
355
356 CopyToArr3(min, min3);
357 CopyToArr3(max, max3);
358 GetBoundingBox(min3, max3, minu3, maxu3);
359 CopyFromArr3(minu3, minu);
360 CopyFromArr3(maxu3, maxu);
361 }
362
380 virtual bool GetEnclosingRegion(const CoordType &minu, const CoordType &maxu, DimsType &min, DimsType &max) const = 0;
381
388 virtual float GetMissingValue() const;
389
398 void SetMissingValue(float missing_value) { _missingValue = missing_value; };
399
406 //
407 void SetHasMissingValues(bool flag) { _hasMissing = flag; }
408
416 //
417 bool HasMissingData() const { return (_hasMissing); };
418
421 //
422 virtual bool HasInvertedCoordinateSystemHandiness() const { return (true); }
423
431 virtual int GetInterpolationOrder() const { return _interpolationOrder; };
432
444 virtual void SetInterpolationOrder(int order);
445
448 //
449 static DimsType Dims(const DimsType &min, const DimsType &max);
450
467 virtual void GetUserCoordinates(const DimsType &indices, CoordType &coords) const = 0;
468
469 virtual void GetUserCoordinates(const size_t indices[], double coords[]) const
470 {
471 DimsType indices3 = {0, 0, 0};
472 CoordType coords3 = {0.0, 0.0, 0.0};
473 CopyToArr3(indices, GetNodeDimensions().size(), indices3);
474 GetUserCoordinates(indices3, coords3);
475 CopyFromArr3(coords3, coords);
476 }
477
478 virtual void GetUserCoordinates(const std::vector<size_t> &indices, std::vector<double> &coords) const
479 {
480 DimsType indices3 = {0, 0, 0};
481 CoordType coords3 = {0.0, 0.0, 0.0};
482 CopyToArr3(indices, indices3);
483 GetUserCoordinates(indices3, coords3);
484 CopyFromArr3(coords3, coords);
485 }
486
487 virtual void GetUserCoordinates(size_t i, double &x, double &y, double &z) const;
488 virtual void GetUserCoordinates(size_t i, size_t j, double &x, double &y, double &z) const;
489 virtual void GetUserCoordinates(size_t i, size_t j, size_t k, double &x, double &y, double &z) const;
490
507 virtual bool GetIndicesCell(const CoordType &coords, DimsType &indices) const = 0;
508
510 //
511 virtual bool GetIndicesCell(const double coords[3], size_t indices[3]) const
512 {
513 CoordType c3 = {0.0, 0.0, 0.0};
514 DimsType i3 = {0, 0, 0};
515 CopyToArr3(coords, GetGeometryDim(), c3);
516 bool status = GetIndicesCell(c3, i3);
517 CopyFromArr3(i3, indices);
518 return (status);
519 }
520
522 //
523 virtual bool GetIndicesCell(const std::vector<double> &coords, std::vector<size_t> &indices) const
524 {
525 CoordType c3 = {0.0, 0.0, 0.0};
526 DimsType i3 = {0, 0, 0};
527 CopyToArr3(coords, c3);
528 bool status = GetIndicesCell(c3, i3);
529 CopyFromArr3(i3, indices);
530 return (status);
531 }
532
543 virtual void GetRange(float range[2]) const;
544
545 virtual void GetRange(const DimsType &min, const DimsType &max, float range[2]) const;
546
548 //
549 virtual void GetRange(std::vector<size_t> min, std::vector<size_t> max, float range[2]) const
550 {
551 DimsType min3 = {0, 0, 0};
552 DimsType max3 = {0, 0, 0};
553 CopyToArr3(min, min3);
554 CopyToArr3(max, max3);
555 GetRange(min3, max3, range);
556 }
557
567 virtual bool InsideGrid(const CoordType &coords) const = 0;
568
570 //
571 virtual bool InsideGrid(const double coords[3]) const
572 {
573 CoordType c3 = {0.0, 0.0, 0.0};
574 CopyToArr3(coords, GetGeometryDim(), c3);
575 return (InsideGrid(c3));
576 }
577
579 //
580 virtual bool InsideGrid(const std::vector<double> &coords) const
581 {
582 CoordType c3 = {0.0, 0.0, 0.0};
583 CopyToArr3(coords, c3);
584 return (InsideGrid(c3));
585 }
586
605 virtual bool GetCellNodes(const DimsType &cindices, std::vector<DimsType> &nodes) const = 0;
606
608 //
609 virtual bool GetCellNodes(const size_t cindices[], std::vector<DimsType> &nodes) const
610 {
611 DimsType i3 = {0, 0, 0};
612 CopyToArr3(cindices, GetCellDimensions().size(), i3);
613 return (GetCellNodes(i3, nodes));
614 }
615
630 virtual bool GetCellNeighbors(const DimsType &cindices, std::vector<DimsType> &cells) const = 0;
631
642 virtual bool GetNodeCells(const DimsType &indices, std::vector<DimsType> &cells) const = 0;
643
646 virtual size_t GetMaxVertexPerFace() const = 0;
647
650 virtual size_t GetMaxVertexPerCell() const = 0;
651
662 //
663 virtual void ClampCoord(const CoordType &coords, CoordType &cCoords) const = 0;
664
666 //
667 virtual void ClampCoord(const double coords[3], double cCoords[3]) const
668 {
669 CoordType c3 = {coords[0], coords[1], coords[2]};
670 CoordType cC3;
671 ClampCoord(c3, cC3);
672 cCoords[0] = cC3[0];
673 cCoords[1] = cC3[1];
674 cCoords[2] = cC3[2];
675 }
676
687 //
688 virtual void ClampIndex(const DimsType &indices, DimsType &cIndices) const { ClampIndex(GetNodeDimensions(), indices, cIndices); }
689
695 //
696 virtual void ClampCellIndex(const DimsType &indices, DimsType &cIndices) const { ClampIndex(GetCellDimensions(), indices, cIndices); }
697
706 //
707 virtual void SetPeriodic(const std::vector<bool> &periodic)
708 {
709 _periodic.clear();
710 int i = 0;
711 for (; i < periodic.size() && i < GetGeometryDim(); i++) { _periodic.push_back(periodic[i]); }
712 for (; i < GetGeometryDim(); i++) { _periodic.push_back(false); }
713 }
714
717 //
718 virtual const std::vector<bool> &GetPeriodic() const { return (_periodic); }
719
728 //
729 virtual size_t GetTopologyDim() const { return (_topologyDimension); }
730
734 //
735 virtual long GetNodeOffset() const { return (_nodeIDOffset); }
736 virtual void SetNodeOffset(long offset) { _nodeIDOffset = offset; }
737
741 //
742 virtual long GetCellOffset() const { return (_cellIDOffset); }
743 virtual void SetCellOffset(long offset) { _cellIDOffset = offset; }
744
755 virtual DimsType GetMinAbs() const { return (_minAbs); }
756
763 //
764 virtual void SetMinAbs(const DimsType &minAbs) { _minAbs = minAbs; }
765
780 //
781 static bool PointInsideBoundingRectangle(const double pt[], const double verts[], int n)
782 {
783 VAssert(n > 2);
784
785 double left = verts[0];
786 double right = verts[0];
787 double top = verts[1];
788 double bottom = verts[1];
789
790 for (int i = 1; i < n; i++) {
791 if (verts[i * 2 + 0] < left) left = verts[i * 2 + 0];
792 if (verts[i * 2 + 0] > right) right = verts[i * 2 + 0];
793 if (verts[i * 2 + 1] < top) top = verts[i * 2 + 1];
794 if (verts[i * 2 + 1] > bottom) bottom = verts[i * 2 + 1];
795 }
796
797 return ((left <= pt[0]) && (right >= pt[0]) && (top <= pt[1]) && (bottom >= pt[1]));
798 }
799
800
801 VDF_API friend std::ostream &operator<<(std::ostream &o, const Grid &g);
802
804 //
805 // Iterators
806 //
808
813 //
814 class InsideBox {
815 public:
816 InsideBox(const CoordType &min, const CoordType &max) : _min(min), _max(max) {}
818
819 bool operator()(const CoordType &pt) const
820 {
821 if (_min == _max) return (true);
822 for (int i = 0; i < _min.size() && i < pt.size(); i++) {
823 if (pt[i] < _min[i] || pt[i] > _max[i]) return (false);
824 }
825 return (true);
826 }
827 bool operator()(const double pt[]) const
828 {
829 if (_min == _max) return (true);
830 for (int i = 0; i < _min.size(); i++) {
831 if (pt[i] < _min[i] || pt[i] > _max[i]) return (false);
832 }
833 return (true);
834 }
835
836 bool Enabled() const { return (_min != _max); }
837
838 private:
839 CoordType _min;
840 CoordType _max;
841 };
842
843 //
844 // Define polymorphic iterator that can be used with any
845 // class derived from this class
846 //
847 //
848
849 // Interface for iterator specializations
850 //
851 template<typename T> class AbstractIterator {
852 public:
853 virtual ~AbstractIterator() {}
854 virtual void next() = 0;
855 virtual void next(const long &offset) = 0;
856 virtual T & deref() const = 0;
857 virtual const void * address() const = 0;
858 virtual bool equal(const void *other) const = 0;
859 virtual std::unique_ptr<AbstractIterator> clone() const = 0;
860 };
861
862 // Overloaded operators that will act on spealizations of
863 // AbstractIterator
864 //
865 template<typename T> class PolyIterator {
866 public:
867 PolyIterator(std::unique_ptr<AbstractIterator<T>> it) : _impl(std::move(it)) {}
868
869 PolyIterator(PolyIterator const &rhs) : _impl(rhs._impl->clone()) {}
870
872 {
873 _impl = rhs._impl->clone();
874 return *this;
875 }
876
877 // PolyIterator has a unique_ptr member so we must provide
878 // std::move constructors
879 //
880 PolyIterator(PolyIterator &&rhs) { _impl = std::move(rhs._impl); }
881
883 {
884 if (this != &rhs) { _impl = std::move(rhs._impl); }
885 return (*this);
886 }
887
888 PolyIterator() : _impl(nullptr) {}
889
891 { // ++prefix
892 _impl->next();
893 return *this;
894 };
895
897 { // postfix++
898 PolyIterator temp(*this);
899 ++(*this);
900 return (temp);
901 };
902
903 PolyIterator &operator+=(const long &offset)
904 {
905 _impl->next(offset);
906 return (*this);
907 };
908
909 PolyIterator operator+(const long &offset) const
910 {
911 PolyIterator temp(*this);
912 temp += offset;
913 return (temp);
914 }
915
916 const T &operator*() const { return _impl->deref(); }
917
918 bool operator==(const PolyIterator &rhs) const { return (_impl->equal(rhs._impl->address())); }
919
920 bool operator!=(const PolyIterator &rhs) const { return (!(*this == rhs)); }
921
922 private:
923 std::unique_ptr<AbstractIterator<T>> _impl;
924 };
925
935 //
939
941 //
942 virtual ConstCoordItr ConstCoordBegin() const = 0;
943 virtual ConstCoordItr ConstCoordEnd() const = 0;
944
953
955 //
958
960 //
961 // Iterators
962 //
964
965 //
966 // Node index iterator. Iterates over node indices
967 //
969 public:
970 ConstNodeIteratorSG(const Grid *g, bool begin);
973
975
976 virtual void next();
977 virtual void next(const long &offset);
978 virtual ConstIndexType &deref() const { return (_index); }
979 virtual const void * address() const { return this; };
980
981 virtual bool equal(const void *rhs) const
982 {
983 const ConstNodeIteratorSG *itrptr = static_cast<const ConstNodeIteratorSG *>(rhs);
984 return (_index == itrptr->_index);
985 }
986
987 virtual std::unique_ptr<ConstNodeIteratorAbstract> clone() const { return std::unique_ptr<ConstNodeIteratorAbstract>(new ConstNodeIteratorSG(*this)); };
988
989 protected:
993 };
994
996 public:
997 ConstNodeIteratorBoxSG(const Grid *g, const CoordType &minu, const CoordType &maxu);
1000
1002
1003 virtual void next();
1004 virtual void next(const long &offset);
1005
1006 virtual std::unique_ptr<ConstNodeIteratorAbstract> clone() const { return std::unique_ptr<ConstNodeIteratorAbstract>(new ConstNodeIteratorBoxSG(*this)); };
1007
1008 private:
1009 InsideBox _pred;
1010 ConstCoordItr _coordItr;
1011 };
1012
1021 virtual ConstNodeIterator ConstNodeBegin() const { return ConstNodeIterator(std::unique_ptr<ConstNodeIteratorAbstract>(new ConstNodeIteratorSG(this, true))); }
1022
1023 virtual ConstNodeIterator ConstNodeBegin(const CoordType &minu, const CoordType &maxu) const
1024 {
1025 return ConstNodeIterator(std::unique_ptr<ConstNodeIteratorAbstract>(new ConstNodeIteratorBoxSG(this, minu, maxu)));
1026 }
1027
1028 virtual ConstNodeIterator ConstNodeBegin(const std::vector<double> &minu, const std::vector<double> &maxu) const
1029 {
1030 CoordType minuCT, maxuCT;
1031 CopyToArr3(minu, minuCT);
1032 CopyToArr3(maxu, maxuCT);
1033 return ConstNodeBegin(minuCT, maxuCT);
1034 }
1035
1036 virtual ConstNodeIterator ConstNodeEnd() const { return ConstNodeIterator(std::unique_ptr<ConstNodeIteratorAbstract>(new ConstNodeIteratorSG(this, false))); }
1037
1038 //
1039 // Cell index iterator. Iterates over cell indices
1040 //
1042 public:
1043 ConstCellIteratorSG(const Grid *g, bool begin);
1046
1048
1049 virtual void next();
1050 virtual void next(const long &offset);
1051 virtual ConstIndexType &deref() const { return (_index); }
1052 virtual const void * address() const { return this; };
1053
1054 virtual bool equal(const void *rhs) const
1055 {
1056 const ConstCellIteratorSG *itrptr = static_cast<const ConstCellIteratorSG *>(rhs);
1057
1058 return (_index == itrptr->_index);
1059 }
1060
1061 virtual std::unique_ptr<ConstCellIteratorAbstract> clone() const { return std::unique_ptr<ConstCellIteratorAbstract>(new ConstCellIteratorSG(*this)); };
1062
1063 protected:
1067 };
1068
1070 public:
1071 ConstCellIteratorBoxSG(const Grid *g, const CoordType &minu, const CoordType &maxu);
1074
1076
1077 virtual void next();
1078 virtual void next(const long &offset);
1079
1080 virtual std::unique_ptr<ConstCellIteratorAbstract> clone() const { return std::unique_ptr<ConstCellIteratorAbstract>(new ConstCellIteratorBoxSG(*this)); };
1081
1082 private:
1083 InsideBox _pred;
1084 const Grid *_g;
1085 bool _cellInsideBox(const size_t cindices[]) const;
1086 };
1087
1096 virtual ConstCellIterator ConstCellBegin() const { return ConstCellIterator(std::unique_ptr<ConstCellIteratorAbstract>(new ConstCellIteratorSG(this, true))); }
1097
1098 virtual ConstCellIterator ConstCellBegin(const CoordType &minu, const CoordType &maxu) const
1099 {
1100 return ConstCellIterator(std::unique_ptr<ConstCellIteratorAbstract>(new ConstCellIteratorBoxSG(this, minu, maxu)));
1101 }
1102
1103 virtual ConstCellIterator ConstCellBegin(const std::vector<double> &minu, const std::vector<double> &maxu) const
1104 {
1105 CoordType minuCT, maxuCT;
1106 CopyToArr3(minu, minuCT);
1107 CopyToArr3(maxu, maxuCT);
1108 return ConstCellBegin(minuCT, maxuCT);
1109 }
1110
1111 virtual ConstCellIterator ConstCellEnd() const { return ConstCellIterator(std::unique_ptr<ConstCellIteratorAbstract>(new ConstCellIteratorSG(this, false))); }
1112
1128 //
1129 template<class T> class VDF_API ForwardIterator {
1130 public:
1131 ForwardIterator(T *rg, bool begin = true, const CoordType &minu = {0.0, 0.0, 0.0}, const CoordType &maxu = {0.0, 0.0, 0.0});
1136
1137 float &operator*() { return (*_itr); }
1138
1140
1142
1143 ForwardIterator<T> &operator+=(const long int &offset);
1144 ForwardIterator<T> operator+(const long int &offset) const;
1145
1148
1149 bool operator==(const ForwardIterator<T> &rhs) const { return (_index == rhs._index); }
1150 bool operator!=(const ForwardIterator<T> &rhs) { return (_index != rhs._index); }
1151
1152 const ConstCoordItr &GetCoordItr() { return (_coordItr); }
1153
1155 {
1156 std::swap(a._blks, b._blks);
1157 std::swap(a._dims3d, b._dims3d);
1158 std::swap(a._bdims3d, b._bdims3d);
1159 std::swap(a._bs3d, b._bs3d);
1160 std::swap(a._blocksize, b._blocksize);
1161 std::swap(a._coordItr, b._coordItr);
1162 std::swap(a._index, b._index);
1163 std::swap(a._lastIndex, b._lastIndex);
1164 std::swap(a._xb, b._xb);
1165 std::swap(a._itr, b._itr);
1166 std::swap(a._pred, b._pred);
1167 }
1168
1169 private:
1170 std::vector<float *> _blks;
1171 DimsType _dims3d;
1172 DimsType _bdims3d;
1173 DimsType _bs3d;
1174 size_t _blocksize;
1175 ConstCoordItr _coordItr;
1176 DimsType _index; // current index into grid
1177 DimsType _lastIndex; // Last valid index
1178 size_t _xb; // x index within a block
1179 float * _itr;
1180 InsideBox _pred;
1181 };
1182
1185
1188 //
1189 Iterator begin(const CoordType &minu, const CoordType &maxu) { return (Iterator(this, true, minu, maxu)); }
1190
1191 Iterator begin(const std::vector<double> &minu, const std::vector<double> &maxu)
1192 {
1193 CoordType minuCT, maxuCT;
1194 CopyToArr3(minu, minuCT);
1195 CopyToArr3(maxu, maxuCT);
1196 return begin(minuCT, maxuCT);
1197 }
1198
1199 Iterator begin() { return (Iterator(this, true)); }
1200
1201 Iterator end() { return (Iterator(this, false)); }
1202
1203 ConstIterator cbegin(const CoordType &minu, const CoordType &maxu) const { return (ConstIterator(this, true, minu, maxu)); }
1204
1205 ConstIterator cbegin(const std::vector<double> &minu, const std::vector<double> &maxu)
1206 {
1207 CoordType minuCT, maxuCT;
1208 CopyToArr3(minu, minuCT);
1209 CopyToArr3(maxu, maxuCT);
1210 return cbegin(minuCT, maxuCT);
1211 }
1212 ConstIterator cbegin() const { return (ConstIterator(this, true)); }
1213
1214 ConstIterator cend() const { return (ConstIterator(this, false)); }
1215
1216 template<typename T> static void CopyToArr3(const std::vector<T> &src, std::array<T, 3> &dst)
1217 {
1218 for (int i = 0; i < src.size() && i < dst.size(); i++) { dst[i] = src[i]; }
1219 }
1220
1221 template<typename T> static void CopyToArr3(const T *src, size_t n, std::array<T, 3> &dst)
1222 {
1223 for (int i = 0; i < n && i < dst.size(); i++) { dst[i] = src[i]; }
1224 }
1225 template<typename T> static void CopyFromArr3(const std::array<T, 3> &src, std::vector<T> &dst)
1226 {
1227 dst.resize(src.size());
1228 for (int i = 0; i < src.size() && i < dst.size(); i++) { dst[i] = src[i]; }
1229 }
1230 template<typename T> static void CopyFromArr3(const std::array<T, 3> &src, T *dst)
1231 {
1232 for (int i = 0; i < src.size(); i++) { dst[i] = src[i]; }
1233 }
1234
1235protected:
1236 virtual float GetValueNearestNeighbor(const CoordType &coords) const = 0;
1237
1238 virtual float GetValueLinear(const CoordType &coords) const = 0;
1239
1240 virtual void GetUserExtentsHelper(CoordType &minu, CoordType &maxu) const = 0;
1241
1242 virtual float *GetValuePtrAtIndex(const std::vector<float *> &blks, const DimsType &indices) const;
1243
1244 // Deprecated
1245 virtual void ClampIndex(const std::vector<size_t> &dims, const DimsType indices, DimsType &cIndices) const
1246 {
1247 cIndices = {0, 0, 0};
1248
1249 for (int i = 0; i < dims.size(); i++) {
1250 cIndices[i] = indices[i];
1251 if (cIndices[i] >= dims[i]) { cIndices[i] = dims[i] - 1; }
1252 }
1253 }
1254
1255 virtual void ClampIndex(const DimsType &dims, const DimsType indices, DimsType &cIndices) const
1256 {
1257 cIndices = {0, 0, 0};
1258
1259 for (int i = 0; i < dims.size(); i++) {
1260 cIndices[i] = indices[i];
1261 if (cIndices[i] >= dims[i]) {
1262 assert(dims[i] > 0);
1263 cIndices[i] = dims[i] - 1;
1264 }
1265 }
1266 }
1267
1268 float BilinearInterpolate(size_t i, size_t j, size_t k, const double xwgt, const double ywgt) const;
1269
1270 float TrilinearInterpolate(size_t i, size_t j, size_t k, const double xwgt, const double ywgt, const double zwgt) const;
1271
1272private:
1273 DimsType _dims; // dimensions of grid arrays
1274 DimsType _bs = {{1, 1, 1}}; // dimensions of each block
1275 DimsType _bdims = {{1, 1, 1}}; // dimensions (specified in blocks) of ROI
1276 std::vector<size_t> _bsDeprecated; // legacy API
1277 std::vector<size_t> _bdimsDeprecated; // legacy API
1278 std::vector<float *> _blks;
1279 std::vector<bool> _periodic; // periodicity of boundaries
1280 DimsType _minAbs; // Offset to start of grid
1281 size_t _topologyDimension = 0;
1282 float _missingValue = std::numeric_limits<float>::infinity();
1283 bool _hasMissing = false;
1284 int _interpolationOrder = 1; // Order of interpolation
1285 long _nodeIDOffset = 0;
1286 long _cellIDOffset = 0;
1287 mutable CoordType _minuCache = {{std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity()}};
1288 mutable CoordType _maxuCache = {{std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity()}};
1289
1290 void _grid(const DimsType &dims, const DimsType &bs, const std::vector<float *> &blks, size_t topology_dimension);
1291};
1292
1293template void Grid::CopyToArr3<size_t>(const std::vector<size_t> &src, std::array<size_t, 3> &dst);
1294
1295}; // namespace VAPoR
1296#endif
#define VAssert(expr)
Definition: VAssert.h:9
virtual T & deref() const =0
virtual std::unique_ptr< AbstractIterator > clone() const =0
virtual const void * address() const =0
virtual bool equal(const void *other) const =0
virtual void next(const long &offset)=0
virtual ~AbstractIterator()
Definition: Grid.h:853
virtual void next(const long &offset)
virtual std::unique_ptr< ConstCellIteratorAbstract > clone() const
Definition: Grid.h:1080
ConstCellIteratorBoxSG(const Grid *g, const CoordType &minu, const CoordType &maxu)
ConstCellIteratorBoxSG(const ConstCellIteratorBoxSG &rhs)
virtual ConstIndexType & deref() const
Definition: Grid.h:1051
virtual bool equal(const void *rhs) const
Definition: Grid.h:1054
virtual const void * address() const
Definition: Grid.h:1052
ConstCellIteratorSG(const ConstCellIteratorSG &rhs)
virtual std::unique_ptr< ConstCellIteratorAbstract > clone() const
Definition: Grid.h:1061
virtual void next(const long &offset)
ConstCellIteratorSG(const Grid *g, bool begin)
ConstNodeIteratorBoxSG(const Grid *g, const CoordType &minu, const CoordType &maxu)
virtual void next(const long &offset)
virtual std::unique_ptr< ConstNodeIteratorAbstract > clone() const
Definition: Grid.h:1006
ConstNodeIteratorBoxSG(const ConstNodeIteratorBoxSG &rhs)
virtual bool equal(const void *rhs) const
Definition: Grid.h:981
virtual void next(const long &offset)
virtual std::unique_ptr< ConstNodeIteratorAbstract > clone() const
Definition: Grid.h:987
ConstNodeIteratorSG(const ConstNodeIteratorSG &rhs)
virtual ConstIndexType & deref() const
Definition: Grid.h:978
virtual const void * address() const
Definition: Grid.h:979
ConstNodeIteratorSG(const Grid *g, bool begin)
ForwardIterator< T > & operator++()
ForwardIterator< T > & operator=(ForwardIterator< T > &rhs)=delete
const ConstCoordItr & GetCoordItr()
Definition: Grid.h:1152
ForwardIterator< T > & operator=(ForwardIterator< T > rhs)
ForwardIterator(T *rg, bool begin=true, const CoordType &minu={0.0, 0.0, 0.0}, const CoordType &maxu={0.0, 0.0, 0.0})
ForwardIterator(const ForwardIterator< T > &)=default
ForwardIterator(ForwardIterator< T > &&rhs)
friend void swap(Grid::ForwardIterator< T > &a, Grid::ForwardIterator< T > &b)
Definition: Grid.h:1154
ForwardIterator< T > operator++(int)
bool operator==(const ForwardIterator< T > &rhs) const
Definition: Grid.h:1149
ForwardIterator< T > & operator+=(const long int &offset)
bool operator!=(const ForwardIterator< T > &rhs)
Definition: Grid.h:1150
ForwardIterator< T > operator+(const long int &offset) const
bool Enabled() const
Definition: Grid.h:836
bool operator()(const CoordType &pt) const
Definition: Grid.h:819
InsideBox(const CoordType &min, const CoordType &max)
Definition: Grid.h:816
bool operator()(const double pt[]) const
Definition: Grid.h:827
bool operator!=(const PolyIterator &rhs) const
Definition: Grid.h:920
PolyIterator operator+(const long &offset) const
Definition: Grid.h:909
PolyIterator & operator=(PolyIterator &&rhs)
Definition: Grid.h:882
PolyIterator(PolyIterator const &rhs)
Definition: Grid.h:869
PolyIterator(PolyIterator &&rhs)
Definition: Grid.h:880
PolyIterator operator++(int)
Definition: Grid.h:896
PolyIterator(std::unique_ptr< AbstractIterator< T > > it)
Definition: Grid.h:867
PolyIterator & operator=(PolyIterator const &rhs)
Definition: Grid.h:871
PolyIterator & operator++()
Definition: Grid.h:890
bool operator==(const PolyIterator &rhs) const
Definition: Grid.h:918
const T & operator*() const
Definition: Grid.h:916
PolyIterator & operator+=(const long &offset)
Definition: Grid.h:903
Abstract base class for a 2D or 3D structured or unstructured grid.
Definition: Grid.h:56
virtual float GetValue(const CoordType &coords) const
Grid(const DimsType &dims, const DimsType &bs, const std::vector< float * > &blks, size_t topology_dimension)
virtual void GetUserExtents(CoordType &minu, CoordType &maxu) const
virtual ConstNodeIterator ConstNodeBegin(const std::vector< double > &minu, const std::vector< double > &maxu) const
Definition: Grid.h:1028
virtual void SetInterpolationOrder(int order)
const std::vector< size_t > & GetBlockSize() const
Definition: Grid.h:163
void SetValueIJK(size_t i, size_t j, size_t k, float v)
virtual void GetUserCoordinates(const std::vector< size_t > &indices, std::vector< double > &coords) const
Definition: Grid.h:478
virtual bool GetCellNodes(const size_t cindices[], std::vector< DimsType > &nodes) const
Definition: Grid.h:609
virtual std::string GetType() const =0
virtual void SetPeriodic(const std::vector< bool > &periodic)
Definition: Grid.h:707
virtual ConstNodeIterator ConstNodeBegin(const CoordType &minu, const CoordType &maxu) const
Definition: Grid.h:1023
virtual float GetMissingValue() const
Grid::PolyIterator< ConstIndexType > ConstNodeIterator
Definition: Grid.h:951
static size_t GetNumDimensions(DimsType dims)
virtual void GetUserExtentsHelper(CoordType &minu, CoordType &maxu) const =0
virtual ConstCoordItr ConstCoordBegin() const =0
Return constant grid coordinate iterator.
virtual float GetValue(const double coords[]) const
Definition: Grid.h:257
bool HasMissingData() const
Definition: Grid.h:417
Iterator begin(const std::vector< double > &minu, const std::vector< double > &maxu)
Definition: Grid.h:1191
virtual size_t GetMaxVertexPerFace() const =0
virtual float GetValue(const std::vector< double > &coords) const
Definition: Grid.h:248
virtual size_t GetGeometryDim() const =0
Grid::PolyIterator< ConstIndexType > ConstCellIterator
Cell index iterator. Iterates over grid cell indices.
Definition: Grid.h:956
ConstIterator cbegin(const std::vector< double > &minu, const std::vector< double > &maxu)
Definition: Grid.h:1205
virtual bool GetIndicesCell(const double coords[3], size_t indices[3]) const
Definition: Grid.h:511
virtual void GetUserCoordinates(size_t i, size_t j, size_t k, double &x, double &y, double &z) const
virtual int GetInterpolationOrder() const
Definition: Grid.h:431
virtual void SetNodeOffset(long offset)
Definition: Grid.h:736
virtual void SetValue(const size_t indices[3], float value)
Definition: Grid.h:202
virtual float GetValueAtIndex(const DimsType &indices) const
virtual bool InsideGrid(const double coords[3]) const
Definition: Grid.h:571
virtual ~Grid()=default
void SetMissingValue(float missing_value)
Definition: Grid.h:398
virtual bool GetIndicesCell(const std::vector< double > &coords, std::vector< size_t > &indices) const
Definition: Grid.h:523
const DimsType ConstIndexType
Definition: Grid.h:950
virtual const DimsType & GetCellDimensions() const =0
virtual float GetValueLinear(const CoordType &coords) const =0
virtual void GetUserCoordinates(const DimsType &indices, CoordType &coords) const =0
virtual void GetRange(std::vector< size_t > min, std::vector< size_t > max, float range[2]) const
Definition: Grid.h:549
virtual void SetCellOffset(long offset)
Definition: Grid.h:743
virtual bool GetNodeCells(const DimsType &indices, std::vector< DimsType > &cells) const =0
ConstIterator cbegin() const
Definition: Grid.h:1212
virtual ConstCellIterator ConstCellBegin(const CoordType &minu, const CoordType &maxu) const
Definition: Grid.h:1098
virtual void GetUserExtents(double minu[3], double maxu[3]) const
Definition: Grid.h:293
virtual float GetValueNearestNeighbor(const CoordType &coords) const =0
virtual float AccessIJK(size_t i, size_t j=0, size_t k=0) const
Iterator begin(const CoordType &minu, const CoordType &maxu)
Definition: Grid.h:1189
float TrilinearInterpolate(size_t i, size_t j, size_t k, const double xwgt, const double ywgt, const double zwgt) const
virtual bool GetEnclosingRegion(const CoordType &minu, const CoordType &maxu, DimsType &min, DimsType &max) const =0
Grid::ForwardIterator< Grid const > ConstIterator
Definition: Grid.h:1184
size_t GetNumDimensions() const
Definition: Grid.h:107
VDF_API friend std::ostream & operator<<(std::ostream &o, const Grid &g)
virtual bool GetCellNodes(const DimsType &cindices, std::vector< DimsType > &nodes) const =0
Grid(const std::vector< size_t > &dims, const std::vector< size_t > &bs, const std::vector< float * > &blks, size_t topology_dimension)
virtual bool HasInvertedCoordinateSystemHandiness() const
Definition: Grid.h:422
virtual void ClampCellIndex(const DimsType &indices, DimsType &cIndices) const
Definition: Grid.h:696
virtual long GetNodeOffset() const
Definition: Grid.h:735
const CoordType ConstCoordType
Definition: Grid.h:936
Grid::AbstractIterator< ConstIndexType > ConstCellIteratorAbstract
Definition: Grid.h:957
virtual const size_t GetNumNodeDimensions() const =0
const std::vector< float * > & GetBlks() const
Definition: Grid.h:168
virtual float GetValue(double x, double y, double z) const
Definition: Grid.h:269
virtual void GetBoundingBox(const DimsType &min, const DimsType &max, CoordType &minu, CoordType &maxu) const =0
void SetValueIJK(size_t i, size_t j, float v)
Definition: Grid.h:221
Iterator begin()
Definition: Grid.h:1199
Iterator end()
Definition: Grid.h:1201
virtual const std::vector< bool > & GetPeriodic() const
Definition: Grid.h:718
virtual bool InsideGrid(const CoordType &coords) const =0
virtual DimsType GetCoordDimensions(size_t dim) const =0
virtual const size_t GetNumCellDimensions() const =0
static void CopyToArr3(const T *src, size_t n, std::array< T, 3 > &dst)
Definition: Grid.h:1221
virtual void GetRange(float range[2]) const
static void CopyFromArr3(const std::array< T, 3 > &src, T *dst)
Definition: Grid.h:1230
const std::vector< size_t > GetDimensionInBlks() const
Definition: Grid.h:156
virtual void ClampIndex(const DimsType &indices, DimsType &cIndices) const
Definition: Grid.h:688
virtual void SetValue(const DimsType &indices, float value)
virtual bool InsideGrid(const std::vector< double > &coords) const
Definition: Grid.h:580
virtual float * GetValuePtrAtIndex(const std::vector< float * > &blks, const DimsType &indices) const
virtual float GetValueAtIndex(const std::vector< size_t > &indices) const
Definition: Grid.h:186
static void CopyFromArr3(const std::array< T, 3 > &src, std::vector< T > &dst)
Definition: Grid.h:1225
virtual long GetCellOffset() const
Definition: Grid.h:742
ConstIterator cend() const
Definition: Grid.h:1214
virtual size_t GetTopologyDim() const
Definition: Grid.h:729
virtual float GetValue(double x, double y) const
Definition: Grid.h:264
virtual ConstNodeIterator ConstNodeEnd() const
Definition: Grid.h:1036
virtual void GetUserCoordinates(size_t i, double &x, double &y, double &z) const
void SetValueIJK(size_t i, float v)
Definition: Grid.h:222
virtual size_t GetMaxVertexPerCell() const =0
virtual bool GetCellNeighbors(const DimsType &cindices, std::vector< DimsType > &cells) const =0
static void CopyToArr3(const std::vector< T > &src, std::array< T, 3 > &dst)
Definition: Grid.h:1216
static bool PointInsideBoundingRectangle(const double pt[], const double verts[], int n)
Definition: Grid.h:781
virtual DimsType GetMinAbs() const
Definition: Grid.h:755
virtual bool GetIndicesCell(const CoordType &coords, DimsType &indices) const =0
virtual void ClampIndex(const std::vector< size_t > &dims, const DimsType indices, DimsType &cIndices) const
Definition: Grid.h:1245
virtual ConstCoordItr ConstCoordEnd() const =0
const DimsType & GetDimensions() const
Definition: Grid.h:100
virtual ConstNodeIterator ConstNodeBegin() const
Definition: Grid.h:1021
virtual void SetMinAbs(const DimsType &minAbs)
Definition: Grid.h:764
virtual ConstCellIterator ConstCellBegin(const std::vector< double > &minu, const std::vector< double > &maxu) const
Definition: Grid.h:1103
virtual ConstCellIterator ConstCellBegin() const
Definition: Grid.h:1096
virtual void GetUserCoordinates(const size_t indices[], double coords[]) const
Definition: Grid.h:469
float BilinearInterpolate(size_t i, size_t j, size_t k, const double xwgt, const double ywgt) const
Grid::AbstractIterator< ConstCoordType > ConstCoordItrAbstract
Definition: Grid.h:938
virtual const DimsType & GetNodeDimensions() const =0
Grid::ForwardIterator< Grid > Iterator
Definition: Grid.h:1183
virtual ConstCellIterator ConstCellEnd() const
Definition: Grid.h:1111
virtual void GetUserExtents(std::vector< double > &minu, std::vector< double > &maxu) const
Definition: Grid.h:304
Grid::PolyIterator< ConstCoordType > ConstCoordItr
Definition: Grid.h:937
static DimsType Dims(const DimsType &min, const DimsType &max)
virtual void ClampCoord(const double coords[3], double cCoords[3]) const
Definition: Grid.h:667
void SetHasMissingValues(bool flag)
Definition: Grid.h:407
virtual void GetBoundingBox(const std::vector< size_t > &min, const std::vector< size_t > &max, std::vector< double > &minu, std::vector< double > &maxu) const
Definition: Grid.h:348
virtual void ClampIndex(const DimsType &dims, const DimsType indices, DimsType &cIndices) const
Definition: Grid.h:1255
ConstIterator cbegin(const CoordType &minu, const CoordType &maxu) const
Definition: Grid.h:1203
virtual void GetUserCoordinates(size_t i, size_t j, double &x, double &y, double &z) const
virtual void GetRange(const DimsType &min, const DimsType &max, float range[2]) const
virtual void ClampCoord(const CoordType &coords, CoordType &cCoords) const =0
Grid::AbstractIterator< ConstIndexType > ConstNodeIteratorAbstract
Definition: Grid.h:952
#define VDF_API
Definition: common.h:73
std::array< double, 3 > CoordType
Type for specifying floating point coordinates.
Definition: Grid.h:23
std::array< size_t, 3 > DimsType
Type for specifying integer indices.
Definition: Grid.h:26