GEOS 3.9.1
SegmentPointComparator.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2006 Refractions Research Inc.
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: noding/SegmentPointComparator.java r320 (JTS-1.12)
16 *
17 **********************************************************************/
18
19#ifndef GEOS_NODING_SEGMENTPOINTCOMPARATOR_H
20#define GEOS_NODING_SEGMENTPOINTCOMPARATOR_H
21
22#include <geos/export.h>
23#include <geos/geom/Coordinate.h>
24#include <cassert>
25
26namespace geos {
27namespace noding { // geos.noding
28
40class GEOS_DLL SegmentPointComparator {
41
42public:
43
52 static int
53 compare(int octant, const geom::Coordinate& p0,
54 const geom::Coordinate& p1)
55 {
56 // nodes can only be equal if their coordinates are equal
57 if(p0.equals2D(p1)) {
58 return 0;
59 }
60
61 int xSign = relativeSign(p0.x, p1.x);
62 int ySign = relativeSign(p0.y, p1.y);
63
64 switch(octant) {
65 case 0:
66 return compareValue(xSign, ySign);
67 case 1:
68 return compareValue(ySign, xSign);
69 case 2:
70 return compareValue(ySign, -xSign);
71 case 3:
72 return compareValue(-xSign, ySign);
73 case 4:
74 return compareValue(-xSign, -ySign);
75 case 5:
76 return compareValue(-ySign, -xSign);
77 case 6:
78 return compareValue(-ySign, xSign);
79 case 7:
80 return compareValue(xSign, -ySign);
81 }
82 assert(0); // invalid octant value
83 return 0;
84
85 }
86
87 static int
88 relativeSign(double x0, double x1)
89 {
90 if(x0 < x1) {
91 return -1;
92 }
93 if(x0 > x1) {
94 return 1;
95 }
96 return 0;
97 }
98
99 static int
100 compareValue(int compareSign0, int compareSign1)
101 {
102 if(compareSign0 < 0) {
103 return -1;
104 }
105 if(compareSign0 > 0) {
106 return 1;
107 }
108 if(compareSign1 < 0) {
109 return -1;
110 }
111 if(compareSign1 > 0) {
112 return 1;
113 }
114 return 0;
115 }
116
117};
118
119} // namespace geos.noding
120} // namespace geos
121
122#endif // GEOS_NODING_SEGMENTPOINTCOMPARATOR_H
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:60
double y
y-coordinate
Definition Coordinate.h:83
double x
x-coordinate
Definition Coordinate.h:80
Implements a robust method of comparing the relative position of two points along the same segment.
Definition SegmentPointComparator.h:40
static int compare(int octant, const geom::Coordinate &p0, const geom::Coordinate &p1)
Compares two Coordinates for their relative position along a segment lying in the specified Octant.
Definition SegmentPointComparator.h:53
Basic namespace for all GEOS functionalities.
Definition IndexedNestedRingTester.h:26