/* Copyright (c) 2015, M. Kerber, D. Morozov, A. Nigmetov All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. You are under no obligation whatsoever to provide any bug fixes, patches, or upgrades to the features, functionality or performance of the source code (Enhancements) to anyone; however, if you choose to make your Enhancements available either publicly, or directly to copyright holder, without imposing a separate written license agreement for such Enhancements, then you hereby grant the following license: a non-exclusive, royalty-free perpetual license to install, use, modify, prepare derivative works, incorporate into other computer software, distribute, and sublicense such enhancements or derivative works thereof, in binary and source code form. */ #ifndef HERA_BASIC_DEFS_BT_H #define HERA_BASIC_DEFS_BT_H #ifdef _WIN32 #include #endif #include #include #include #include #include #include #include #include #include "def_debug_bt.h" #ifndef FOR_R_TDA #include #endif namespace hera { namespace bt { typedef int IdType; constexpr IdType MinValidId = 10; template struct Point { Real x, y; bool operator==(const Point &other) const { return ((this->x == other.x) and (this->y == other.y)); } bool operator!=(const Point &other) const { return !(*this == other); } Point(Real ax, Real ay) : x(ax), y(ay) {} Point() : x(0.0), y(0.0) {} #ifndef FOR_R_TDA template friend std::ostream& operator<<(std::ostream& output, const Point& p) { output << "(" << p.x << ", " << p.y << ")"; return output; } #endif }; template struct DiagramPoint { // Points above the diagonal have type NORMAL // Projections onto the diagonal have type DIAG // for DIAG points only x-coordinate is relevant // to-do: add getters/setters, checks in constructors, etc enum Type { NORMAL, DIAG }; // data members private: Real x, y; public: Type type; IdType id; // operators, constructors bool operator==(const DiagramPoint &other) const { // compare by id only assert(this->id >= MinValidId); assert(other.id >= MinValidId); bool areEqual{ this->id == other.id }; assert(!areEqual or ((this->x == other.x) and (this->y == other.y) and (this->type == other.type))); return areEqual; } bool operator!=(const DiagramPoint &other) const { return !(*this == other); } DiagramPoint(Real _x, Real _y, Type _type, IdType _id) : x(_x), y(_y), type(_type), id(_id) { if ( _y == _x and _type != DIAG) throw std::runtime_error("Point on the main diagonal must have DIAG type"); } bool isDiagonal(void) const { return type == DIAG; } bool isNormal(void) const { return type == NORMAL; } Real inline getRealX() const // return the x-coord { return x; } Real inline getRealY() const // return the y-coord { return y; } #ifndef FOR_R_TDA template friend std::ostream& operator<<(std::ostream& output, const DiagramPoint& p) { if ( p.isDiagonal() ) { output << "(" << p.x << ", " << p.y << ", " << 0.5 * (p.x + p.y) << ", " << p.id << " DIAG )"; } else { output << "(" << p.x << ", " << p.y << ", " << p.id << " NORMAL)"; } return output; } #endif }; // compute l-inf distance between two diagram points template Real distLInf(const DiagramPoint& a, const DiagramPoint& b) { if ( a.isDiagonal() and b.isDiagonal() ) { // distance between points on the diagonal is 0 return 0.0; } // otherwise distance is a usual l-inf distance return std::max(fabs(a.getRealX() - b.getRealX()), fabs(a.getRealY() - b.getRealY())); } template inline void hash_combine(std::size_t & seed, const T & v) { std::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } //template //struct PointHash { // size_t operator()(const Point &p) const // { // size_t seed = 0; // hash_combine(seed, p.x); // hash_combine(seed, p.y); // return seed; // } //}; template struct DiagramPointHash { size_t operator()(const DiagramPoint &p) const { assert(p.id >= MinValidId); return std::hash()(p.id); } }; template Real distLInf(const DiagramPoint &a, const DiagramPoint &b); //template //typedef std::unordered_set PointSet; template class DiagramPointSet; template void addProjections(DiagramPointSet &A, DiagramPointSet &B); template class DiagramPointSet { public: using Real = Real_; using DgmPoint = DiagramPoint; using DgmPointHash = DiagramPointHash; using const_iterator = typename std::unordered_set::const_iterator; using iterator = typename std::unordered_set::iterator; private: bool isLinked { false }; IdType maxId { MinValidId + 1 }; std::unordered_set points; public: void insert(const DgmPoint& p) { points.insert(p); if (p.id > maxId) { maxId = p.id + 1; } } void erase(const DgmPoint& p, bool doCheck = true) { // if doCheck, erasing non-existing elements causes assert auto it = points.find(p); if (it != points.end()) { points.erase(it); } else { assert(!doCheck); } } void erase(const const_iterator it) { points.erase(it); } void removeDiagonalPoints() { if (isLinked) { auto ptIter = points.begin(); while(ptIter != points.end()) { if (ptIter->isDiagonal()) { ptIter = points.erase(ptIter); } else { ptIter++; } } isLinked = false; } } size_t size() const { return points.size(); } void reserve(const size_t newSize) { points.reserve(newSize); } void clear() { points.clear(); } bool empty() const { return points.empty(); } bool hasElement(const DgmPoint &p) const { return points.find(p) != points.end(); } iterator find(const DgmPoint &p) { return points.find(p); } iterator begin() { return points.begin(); } iterator end() { return points.end(); } const_iterator find(const DgmPoint &p) const { return points.find(p); } const_iterator cbegin() const { return points.cbegin(); } const_iterator cend() const { return points.cend(); } #ifndef FOR_R_TDA template friend std::ostream& operator<<(std::ostream& output, const DiagramPointSet& ps) { output << "{ "; for(auto pit = ps.cbegin(); pit != ps.cend(); ++pit) { output << *pit << ", "; } output << "\b\b }"; return output; } #endif friend void addProjections(DiagramPointSet& A, DiagramPointSet& B); template void fillIn(PairIterator begin_iter, PairIterator end_iter) { isLinked = false; clear(); IdType uniqueId = MinValidId + 1; for (auto iter = begin_iter; iter != end_iter; ++iter) { insert(DgmPoint(iter->first, iter->second, DgmPoint::NORMAL, uniqueId++)); } } template void fillIn(const PointContainer& dgm_cont) { using Traits = DiagramTraits; isLinked = false; clear(); IdType uniqueId = MinValidId + 1; for (const auto& pt : dgm_cont) { Real x = Traits::get_x(pt); Real y = Traits::get_y(pt); insert(DgmPoint(x, y, DgmPoint::NORMAL, uniqueId++)); } } // ctor from range template DiagramPointSet(PairIterator begin_iter, PairIterator end_iter) { fillIn(begin_iter, end_iter); } // ctor from container, uses DiagramTraits template DiagramPointSet(const PointContainer& dgm) { fillIn(dgm); } // default ctor, empty diagram DiagramPointSet(IdType minId = MinValidId + 1) : maxId(minId + 1) {}; IdType nextId() { return maxId + 1; } }; // DiagramPointSet template Real getFurthestDistance3Approx(DiagPointContainer& A, DiagPointContainer& B) { Real result{0.0}; DiagramPoint begA = *(A.begin()); DiagramPoint optB = *(B.begin()); for (const auto &pointB : B) { if (distLInf(begA, pointB) > result) { result = distLInf(begA, pointB); optB = pointB; } } for (const auto &pointA : A) { if (distLInf(pointA, optB) > result) { result = distLInf(pointA, optB); } } return result; } // preprocess diagrams A and B by adding projections onto diagonal of points of // A to B and vice versa. NB: ids of points will be changed! template void addProjections(DiagramPointSet& A, DiagramPointSet& B) { using Real = Real_; using DgmPoint = DiagramPoint; using DgmPointSet = DiagramPointSet; IdType uniqueId {MinValidId + 1}; DgmPointSet newA, newB; // copy normal points from A to newA // add projections to newB for(auto& pA : A) { if (pA.isNormal()) { DgmPoint dpA {pA.getRealX(), pA.getRealY(), DgmPoint::NORMAL, uniqueId++}; DgmPoint dpB {(pA.getRealX() +pA.getRealY())/2, (pA.getRealX() +pA.getRealY())/2, DgmPoint::DIAG, uniqueId++}; newA.insert(dpA); newB.insert(dpB); } } for(auto& pB : B) { if (pB.isNormal()) { DgmPoint dpB {pB.getRealX(), pB.getRealY(), DgmPoint::NORMAL, uniqueId++}; DgmPoint dpA {(pB.getRealX() +pB.getRealY())/2, (pB.getRealX() +pB.getRealY())/2, DgmPoint::DIAG, uniqueId++}; newB.insert(dpB); newA.insert(dpA); } } A = newA; B = newB; A.isLinked = true; B.isLinked = true; } //#ifndef FOR_R_TDA //template //std::ostream& operator<<(std::ostream& output, const DiagramPoint& p) //{ // if ( p.isDiagonal() ) { // output << "(" << p.x << ", " << p.y << ", " << 0.5 * (p.x + p.y) << ", " << p.id << " DIAG )"; // } else { // output << "(" << p.x << ", " << p.y << ", " << p.id << " NORMAL)"; // } // return output; //} //template //std::ostream& operator<<(std::ostream& output, const DiagramPointSet& ps) //{ // output << "{ "; // for(auto pit = ps.cbegin(); pit != ps.cend(); ++pit) { // output << *pit << ", "; // } // output << "\b\b }"; // return output; //} //#endif // FOR_R_TDA } // end namespace bt } // end namespace hera #endif // HERA_BASIC_DEFS_BT_H