GEOS: PointPairDistance.h Source File

GEOS 3.5.0

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

- File List

PointPairDistance.h
1 /**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2009 Sandro Santilli <strk@keybit.net>
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************
14 *
15 * Last port: algorithm/distance/PointPairDistance.java 1.1 (JTS-1.9)
16 *
17 **********************************************************************/
18
19 #ifndef GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
20 #define GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
21
22 #include <geos/platform.h> // for DoubleNotANumber
23 #include <geos/geom/Coordinate.h> // for inlines
24
25 #include <vector> // for composition
26 #include <cassert>
27
28 namespace geos {
29 namespace algorithm { // geos::algorithm
30 namespace distance { // geos::algorithm::distance
31
37 class PointPairDistance
38 {
39 public:
40
41 PointPairDistance()
42 :
43 pt(2),
44 distance(DoubleNotANumber),
45 isNull(true)
46 {
47 assert(pt.size() == 2);
48 }
49
50 void initialize()
51 {
52 isNull = true;
53 }
54
55 void initialize(const geom::Coordinate& p0, const geom::Coordinate& p1)
56 {
57 pt[0] = p0;
58 pt[1] = p1;
59 distance = p0.distance(p1);
60 isNull = false;
61 }
62
63 double getDistance() const
64 {
65 return distance;
66 }
67
68 const std::vector<geom::Coordinate>& getCoordinates() const
69 {
70 return pt;
71 }
72
73 const geom::Coordinate& getCoordinate(unsigned int i) const
74 {
75 assert(i<pt.size());
76 return pt[i];
77 }
78
79 void setMaximum(const PointPairDistance& ptDist)
80 {
81 setMaximum(ptDist.pt[0], ptDist.pt[1]);
82 }
83
84 void setMaximum(const geom::Coordinate& p0, const geom::Coordinate& p1)
85 {
86 if (isNull) {
87 initialize(p0, p1);
88 return;
89 }
90 double dist = p0.distance(p1);
91 if (dist > distance)
92 initialize(p0, p1, dist);
93 }
94
95 void setMinimum(const PointPairDistance& ptDist)
96 {
97 setMinimum(ptDist.pt[0], ptDist.pt[1]);
98 }
99
100 void setMinimum(const geom::Coordinate& p0, const geom::Coordinate& p1)
101 {
102 if (isNull) {
103 initialize(p0, p1);
104 return;
105 }
106 double dist = p0.distance(p1);
107 if (dist < distance)
108 initialize(p0, p1, dist);
109 }
110
111 private:
112
119 void initialize(const geom::Coordinate& p0, const geom::Coordinate& p1,
120 double dist)
121 {
122 pt[0] = p0;
123 pt[1] = p1;
124 distance = dist;
125 isNull = false;
126 }
127
128 std::vector<geom::Coordinate> pt;
129
130 double distance;
131
132 bool isNull;
133 };
134
135 } // geos::algorithm::distance
136 } // geos::algorithm
137 } // geos
138
139 #endif // GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
140
geos::geom::Coordinate
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
geos::algorithm::distance::PointPairDistance
Definition: PointPairDistance.h:37
geos
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
geos::geom::Coordinate::distance
double distance(const Coordinate &p) const