/* * © Copyright 2014 – Urheberrechtshinweis * Alle Rechte vorbehalten / All Rights Reserved * * Programmcode ist urheberrechtlich geschuetzt. * Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner. * Keine Verwendung ohne explizite Genehmigung. * (vgl. § 106 ff UrhG / § 97 UrhG) */ #ifndef GRIDPOINT_H #define GRIDPOINT_H #include #include "../geo/Point3.h" #include "../Assertions.h" struct GridPoint { /** x-position (in centimeter) */ float x_cm; /** y-position (in centimeter) */ float y_cm; /** z-position (in centimeter) */ float z_cm; /** empty ctor */ GridPoint() : x_cm(0), y_cm(0), z_cm(0) {;} /** ctor */ GridPoint(const float x_cm, const float y_cm, const float z_cm) : x_cm(x_cm), y_cm(y_cm), z_cm(z_cm) {;} /** equal? */ bool operator == (const GridPoint& o) const { return x_cm == o.x_cm && y_cm == o.y_cm && z_cm == o.z_cm; } /** not equal? */ bool operator != (const GridPoint& o) const { return x_cm != o.x_cm || y_cm != o.y_cm || z_cm != o.z_cm; } GridPoint operator * (const float f) const {return GridPoint(x_cm*f, y_cm*f, z_cm*f);} GridPoint operator + (const GridPoint& o) const {return GridPoint(x_cm+o.x_cm, y_cm+o.y_cm, z_cm+o.z_cm);} GridPoint& operator += (const GridPoint& o) {x_cm += o.x_cm; y_cm += o.y_cm; z_cm += o.z_cm; return *this;} GridPoint& operator /= (const float f) {x_cm /= f; y_cm /= f; z_cm /= f; return *this;} /** get the distance (in meter) betwen this and the given point */ float getDistanceInMeter(const GridPoint& other) const { return getDistanceInCM(other) / 100.0f; } /** get the distance (in centimeter) betwen this and the given point */ float getDistanceInCM(const GridPoint& other) const { const int dx = x_cm - other.x_cm; const int dy = y_cm - other.y_cm; const int dz = z_cm - other.z_cm; return std::sqrt(dx*dx + dy*dy + dz*dz); } ///** cast to Point3 */ //operator Point3() const {return Point3(x_cm, y_cm, z_cm);} /** convert to Point3 in centimeter */ Point3 inCentimeter() const {return Point3(x_cm, y_cm, z_cm);} /** convert to Point3 in meter */ Point3 inMeter() const {return Point3(x_cm/100.0f, y_cm/100.0f, z_cm/100.0f);} /** cast to string */ operator std::string() const {return asString();} /** get as string */ std::string asString() const { return "(" + std::to_string(x_cm) + "," + std::to_string(y_cm) + "," + std::to_string(z_cm) + ")"; } /** read-only array access */ float operator [] (const int idx) const { Assert::isBetween(idx, 0, 2, "index out of bounds"); if (0 == idx) {return x_cm;} if (1 == idx) {return y_cm;} {return z_cm;} } }; namespace std { template<> struct hash { int64_t operator() (const GridPoint& gp) const { return (((int64_t)gp.x_cm) << 0) + (((int64_t)gp.y_cm) << 8) + (((int64_t)gp.z_cm) << 16); } }; } #endif // GRIDPOINT_H