- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List

# CentralEndpointIntersector.h

    00001 /**********************************************************************
    00002  * $Id: CentralEndpointIntersector.h 2137 2008-07-25 21:00:09Z mloskot $
    00003  *
    00004  * GEOS - Geometry Engine Open Source
    00005  * http://geos.refractions.net
    00006  *
    00007  * Copyright (C) 2006 Refractions Research Inc.
    00008  *
    00009  * This is free software; you can redistribute and/or modify it under
    00010  * the terms of the GNU Lesser General Public Licence as published
    00011  * by the Free Software Foundation.
    00012  * See the COPYING file for more information.
    00013  *
    00014  **********************************************************************
    00015  *
    00016  * Last port: algorithm/CentralEndpointIntersector.java rev. 1.1
    00017  *
    00018  **********************************************************************/
    00019
    00020 #ifndef GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H
    00021 #define GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H
    00022
    00023 #include <geos/geom/Coordinate.h>
    00024
    00025 #include <string>
    00026 #include <limits>
    00027
    00028 // Forward declarations
    00029 namespace geos {
    00030         namespace geom {
    00031                 //class PrecisionModel;
    00032         }
    00033 }
    00034
    00035 namespace geos {
    00036 namespace algorithm { // geos::algorithm
    00037
    00057 class CentralEndpointIntersector {
    00058
    00059 public:
    00060
    00061         static const geom::Coordinate& getIntersection(const geom::Coordinate& p00,
    00062                         const geom::Coordinate& p01, const geom::Coordinate& p10,
    00063                         const geom::Coordinate& p11)
    00064         {
    00065                 CentralEndpointIntersector intor(p00, p01, p10, p11);
    00066                 return intor.getIntersection();
    00067         }
    00068
    00069         CentralEndpointIntersector(const geom::Coordinate& p00,
    00070                         const geom::Coordinate& p01,
    00071                         const geom::Coordinate& p10,
    00072                         const geom::Coordinate& p11)
    00073                 :
    00074                 _pts(4)
    00075         {
    00076                 _pts[0]=p00;
    00077                 _pts[1]=p01;
    00078                 _pts[2]=p10;
    00079                 _pts[3]=p11;
    00080                 compute();
    00081         }
    00082
    00083         const geom::Coordinate& getIntersection() const
    00084         {
    00085                 return _intPt;
    00086         }
    00087
    00088
    00089 private:
    00090
    00091         // This is likely overkill.. we'll be allocating heap
    00092         // memory at every call !
    00093         std::vector<geom::Coordinate> _pts;
    00094
    00095         geom::Coordinate _intPt;
    00096
    00097         void compute()
    00098         {
    00099                 geom::Coordinate centroid = average(_pts);
    00100                 _intPt = findNearestPoint(centroid, _pts);
    00101         }
    00102
    00103         static geom::Coordinate average(
    00104                         const std::vector<geom::Coordinate>& pts)
    00105         {
    00106                 geom::Coordinate avg(0, 0);
    00107                 size_t n = pts.size();
    00108                 if ( ! n ) return avg;
    00109                 for (size_t i=0; i<n; ++i)
    00110                 {
    00111                         avg.x += pts[i].x;
    00112                         avg.y += pts[i].y;
    00113                 }
    00114                 avg.x /= n;
    00115                 avg.y /= n;
    00116                 return avg;
    00117         }
    00118
    00129         geom::Coordinate findNearestPoint(const geom::Coordinate& p,
    00130                         const std::vector<geom::Coordinate>& pts) const
    00131         {
    00132                 double minDist = std::numeric_limits<double>::max();
    00133                 geom::Coordinate result = geom::Coordinate::getNull();
    00134                 for (size_t i = 0, n=pts.size(); i < n; ++i) {
    00135                         double dist = p.distance(pts[i]);
    00136                         if (dist < minDist) {
    00137                                 minDist = dist;
    00138                                 result = pts[i];
    00139                         }
    00140                 }
    00141                 return result;
    00142         }
    00143
    00144
    00145
    00146 };
    00147
    00148 } // namespace geos::algorithm
    00149 } // namespace geos
    00150
    00151
    00152 #endif // GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H
    00153
    00154 /**********************************************************************
    00155  * $Log$
    00156  **********************************************************************/
    00157