1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2009    Sandro Santilli <strk@kbt.io>
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/OrientedCoordinateArray.java rev. 1.1 (JTS-1.9)
16  *
17  **********************************************************************/
18 
19 #ifndef GEOS_NODING_ORIENTEDCOORDINATEARRAY_H
20 #define GEOS_NODING_ORIENTEDCOORDINATEARRAY_H
21 
22 #include <geos/export.h>
23 
24 #include <cstddef>
25 
26 // Forward declarations
27 namespace geos {
28 namespace geom {
29 class CoordinateSequence;
30 }
31 namespace noding {
32 //class SegmentString;
33 }
34 }
35 
36 namespace geos {
37 namespace noding { // geos.noding
38 
39 /** \brief
40  * Allows comparing {@link geom::CoordinateSequence}s
41  * in an orientation-independent way.
42  */
43 class GEOS_DLL OrientedCoordinateArray {
44 public:
45 
46     /**
47      * Creates a new {@link OrientedCoordinateArray}
48      * for the given {@link geom::CoordinateSequence}.
49      *
50      * @param p_pts the coordinates to orient
51      */
OrientedCoordinateArray(const geom::CoordinateSequence & p_pts)52     OrientedCoordinateArray(const geom::CoordinateSequence& p_pts)
53         :
54         pts(&p_pts),
55         orientationVar(orientation(p_pts))
56     {
57     }
58 
59     /** \brief
60      * Compares two {@link OrientedCoordinateArray}s for their
61      * relative order
62      *
63      * @return -1 this one is smaller
64      * @return 0 the two objects are equal
65      * @return 1 this one is greater
66      *
67      * In JTS, this is used automatically by ordered lists.
68      * In C++, operator< would be used instead....
69      */
70     int compareTo(const OrientedCoordinateArray& o1) const;
71 
72     bool operator==(const OrientedCoordinateArray& other) const;
73 
74     struct GEOS_DLL HashCode {
75         size_t operator()(const OrientedCoordinateArray & oca) const;
76     };
77 
78 private:
79 
80     static int compareOriented(const geom::CoordinateSequence& pts1,
81                                bool orientation1,
82                                const geom::CoordinateSequence& pts2,
83                                bool orientation2);
84 
85 
86     /**
87      * Computes the canonical orientation for a coordinate array.
88      *
89      * @param pts the array to test
90      * @return <code>true</code> if the points are oriented forwards
91      * @return <code>false</code> if the points are oriented in reverse
92      */
93     static bool orientation(const geom::CoordinateSequence& pts);
94 
95     /// Externally owned
96     const geom::CoordinateSequence* pts;
97 
98     bool orientationVar;
99 
100 };
101 
102 /// Strict weak ordering operator for OrientedCoordinateArray
103 ///
104 /// This is the C++ equivalent of JTS's compareTo
105 inline bool
106 operator< (const OrientedCoordinateArray& oca1,
107            const OrientedCoordinateArray& oca2)
108 {
109     return oca1.compareTo(oca2) < 0;
110 }
111 
112 } // namespace geos.noding
113 } // namespace geos
114 
115 
116 #endif // GEOS_NODING_ORIENTEDCOORDINATEARRAY_H
117 
118