GEOS  3.8.0dev
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 
26 namespace geos {
27 namespace noding { // geos.noding
28 
39 class GEOS_DLL SegmentPointComparator {
40 
41 public:
42 
51  static int
52  compare(int octant, const geom::Coordinate& p0,
53  const geom::Coordinate& p1)
54  {
55  // nodes can only be equal if their coordinates are equal
56  if(p0.equals2D(p1)) {
57  return 0;
58  }
59 
60  int xSign = relativeSign(p0.x, p1.x);
61  int ySign = relativeSign(p0.y, p1.y);
62 
63  switch(octant) {
64  case 0:
65  return compareValue(xSign, ySign);
66  case 1:
67  return compareValue(ySign, xSign);
68  case 2:
69  return compareValue(ySign, -xSign);
70  case 3:
71  return compareValue(-xSign, ySign);
72  case 4:
73  return compareValue(-xSign, -ySign);
74  case 5:
75  return compareValue(-ySign, -xSign);
76  case 6:
77  return compareValue(-ySign, xSign);
78  case 7:
79  return compareValue(xSign, -ySign);
80  }
81  assert(0); // invalid octant value
82  return 0;
83 
84  }
85 
86  static int
87  relativeSign(double x0, double x1)
88  {
89  if(x0 < x1) {
90  return -1;
91  }
92  if(x0 > x1) {
93  return 1;
94  }
95  return 0;
96  }
97 
98  static int
99  compareValue(int compareSign0, int compareSign1)
100  {
101  if(compareSign0 < 0) {
102  return -1;
103  }
104  if(compareSign0 > 0) {
105  return 1;
106  }
107  if(compareSign1 < 0) {
108  return -1;
109  }
110  if(compareSign1 > 0) {
111  return 1;
112  }
113  return 0;
114  }
115 
116 };
117 
118 } // namespace geos.noding
119 } // namespace geos
120 
121 #endif // GEOS_NODING_SEGMENTPOINTCOMPARATOR_H
double y
y-coordinate
Definition: Coordinate.h:83
static int compare(int octant, const geom::Coordinate &p0, const geom::Coordinate &p1)
Definition: SegmentPointComparator.h:52
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
double x
x-coordinate
Definition: Coordinate.h:80
Definition: SegmentPointComparator.h:39