GEOS  3.6.2
PointPairDistance.h
00001 /**********************************************************************
00002  *
00003  * GEOS - Geometry Engine Open Source
00004  * http://geos.osgeo.org
00005  *
00006  * Copyright (C) 2009  Sandro Santilli <strk@keybit.net>
00007  *
00008  * This is free software; you can redistribute and/or modify it under
00009  * the terms of the GNU Lesser General Public Licence as published
00010  * by the Free Software Foundation. 
00011  * See the COPYING file for more information.
00012  *
00013  **********************************************************************
00014  *
00015  * Last port: algorithm/distance/PointPairDistance.java 1.1 (JTS-1.9)
00016  *
00017  **********************************************************************/
00018 
00019 #ifndef GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
00020 #define GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
00021 
00022 #include <geos/platform.h> // for DoubleNotANumber
00023 #include <geos/geom/Coordinate.h> // for inlines
00024 
00025 #include <vector> // for composition
00026 #include <cassert>
00027 
00028 namespace geos {
00029 namespace algorithm { // geos::algorithm
00030 namespace distance { // geos::algorithm::distance
00031 
00037 class PointPairDistance
00038 {
00039 public:
00040 
00041         PointPairDistance()
00042                 :
00043                 pt(2),
00044                 distance(DoubleNotANumber),
00045                 isNull(true)
00046         {
00047                 assert(pt.size() == 2);
00048         }
00049 
00050         void initialize()
00051         {
00052                 isNull = true;
00053         }
00054 
00055         void initialize(const geom::Coordinate& p0, const geom::Coordinate& p1)
00056         {
00057                 pt[0] = p0;
00058                 pt[1] = p1;
00059                 distance = p0.distance(p1);
00060                 isNull = false;
00061         }
00062 
00063         double getDistance() const
00064         {
00065                 return distance;
00066         }
00067 
00068         const std::vector<geom::Coordinate>& getCoordinates() const
00069         {
00070                 return pt;
00071         }
00072 
00073         const geom::Coordinate& getCoordinate(unsigned int i) const
00074         {
00075                 assert(i<pt.size());
00076                 return pt[i];
00077         }
00078 
00079         void setMaximum(const PointPairDistance& ptDist)
00080         {
00081                 setMaximum(ptDist.pt[0], ptDist.pt[1]);
00082         }
00083 
00084         void setMaximum(const geom::Coordinate& p0, const geom::Coordinate& p1)
00085         {
00086                 if (isNull) {
00087                         initialize(p0, p1);
00088                         return;
00089                 }
00090                 double dist = p0.distance(p1);
00091                 if (dist > distance)
00092                         initialize(p0, p1, dist);
00093         }
00094 
00095         void setMinimum(const PointPairDistance& ptDist)
00096         {
00097                 setMinimum(ptDist.pt[0], ptDist.pt[1]);
00098         }
00099 
00100         void setMinimum(const geom::Coordinate& p0, const geom::Coordinate& p1)
00101         {
00102                 if (isNull) {
00103                         initialize(p0, p1);
00104                         return;
00105                 }
00106                 double dist = p0.distance(p1);
00107                 if (dist < distance)
00108                         initialize(p0, p1, dist);
00109         }
00110 
00111 private:
00112 
00119         void initialize(const geom::Coordinate& p0, const geom::Coordinate& p1,
00120                         double dist)
00121         {
00122                 pt[0] = p0;
00123                 pt[1] = p1;
00124                 distance = dist;
00125                 isNull = false;
00126         }
00127 
00128         std::vector<geom::Coordinate> pt;
00129 
00130         double distance;
00131 
00132         bool isNull;
00133 };
00134 
00135 } // geos::algorithm::distance
00136 } // geos::algorithm
00137 } // geos
00138 
00139 #endif // GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
00140