1 2 #ifndef WK_WKCOORD_H 3 #define WK_WKCOORD_H 4 5 #include <cmath> 6 #include <vector> 7 #include <stdexcept> 8 9 class WKCoord { 10 public: 11 double x; 12 double y; 13 double z; 14 double m; 15 bool hasZ; 16 bool hasM; 17 WKCoord()18 WKCoord(): x(NAN), y(NAN), z(NAN), m(NAN), hasZ(false), hasM(false) {} WKCoord(double x,double y,double z,double m,bool hasZ,bool hasM)19 WKCoord(double x, double y, double z, double m, bool hasZ, bool hasM): 20 x(x), y(y), z(z), m(m), hasZ(hasZ), hasM(hasM) {} 21 operator ==(WKCoord & other)22 bool operator == (WKCoord& other) { 23 if (this->hasZ != other.hasZ || this->hasM != other.hasM) { 24 return false; 25 } 26 27 for (size_t i = 0; i < this->size(); i++) { 28 if ((*this)[i] != other[i]) { 29 return false; 30 } 31 } 32 33 return true; 34 } 35 operator [](std::size_t idx)36 double& operator[](std::size_t idx) { 37 switch (idx) { 38 case 0: return x; 39 case 1: return y; 40 case 2: 41 if (hasZ) { 42 return z; 43 } else if (hasM) { 44 return m; 45 } 46 case 3: 47 if (hasM) return m; 48 default: 49 throw std::runtime_error("Coordinate subscript out of range"); 50 } 51 } 52 operator [](std::size_t idx) const53 const double& operator[](std::size_t idx) const { 54 switch (idx) { 55 case 0: return x; 56 case 1: return y; 57 case 2: 58 if (hasZ) { 59 return z; 60 } else if (hasM) { 61 return m; 62 } 63 case 3: 64 if (hasM) return m; 65 default: 66 throw std::runtime_error("Coordinate subscript out of range"); 67 } 68 } 69 size() const70 const size_t size() const { 71 return 2 + hasZ + hasM; 72 } 73 xy(double x,double y)74 static const WKCoord xy(double x, double y) { 75 return WKCoord(x, y, NAN, NAN, false, false); 76 } 77 xyz(double x,double y,double z)78 static const WKCoord xyz(double x, double y, double z) { 79 return WKCoord(x, y, z, NAN, true, false); 80 } 81 xym(double x,double y,double m)82 static const WKCoord xym(double x, double y, double m) { 83 return WKCoord(x, y, NAN, m, false, true); 84 } 85 xyzm(double x,double y,double z,double m)86 static const WKCoord xyzm(double x, double y, double z, double m) { 87 return WKCoord(x, y, z, m, true, true); 88 } 89 }; 90 91 #endif 92