GeometryFunctions.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/geometry/GeometryFunctions.h
22 
23  \brief A list of common used Geometry functions for the Geometry Module.
24 */
25 
26 #ifndef __TERRALIB_GEOMETRY_INTERNAL_GEOMETRYFUNCTIONS_H
27 #define __TERRALIB_GEOMETRY_INTERNAL_GEOMETRYFUNCTIONS_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "CommonDataStructures.h"
32 #include "Point.h"
33 
34 #include <vector>
35 
36 namespace te
37 {
38  namespace gm
39  {
40 // Forward declarations
41  struct Coord2D;
42  class Envelope;
43  class Geometry;
44  class LineString;
45  class Point;
46 
48  {
49 
50  public:
51 
52  /*!
53  \brief Checks if a coordinate (x1, x2) is exactly equal to the coordinate (x2, y2)
54 
55  \param x1 The X of the first coordinate
56  \param y1 The Y of the first coordinate
57  \param x2 The X of the second coordinate
58  \param y2 The Y of the second coordinate
59 
60  \return TRUE if the points are equal. FALSE otherwise
61  */
62  static bool IsEqual(double x1, double y1, double x2, double y2);
63 
64  /*!
65  \brief Checks if a coordinate c1 is exactly equal to the coordinate c2
66 
67  \param c1 The first coordinate
68  \param c2 The second coordinate
69 
70  \return TRUE if the points are equal. FALSE otherwise
71  */
72  static bool IsEqual(const te::gm::Coord2D& c1, const te::gm::Coord2D& c2);
73 
74  /*!
75  \brief Checks if a point p1 is exactly equal to the point p2
76 
77  \param p1 The first point
78  \param p2 The second point
79 
80  \return TRUE if the points are equal. FALSE otherwise
81  */
82  static bool IsEqual(const te::gm::Point& p1, const te::gm::Point& p2);
83 
84  /*!
85  \brief Calculates the distance between c1 and c2
86 
87  \param c1 The first coordinate
88  \param c2 The second coordinate
89 
90  \return The distance between the given coordinates
91  */
92  static double CalculateDistance(const te::gm::Coord2D& c1, const te::gm::Coord2D& c2);
93 
94  /*!
95  \brief Calculates the squared distance between c1 and c2, that is, the distance calculation without the final square root step. This is particularly useful when we just need to compare distance values, and not know the distance itself.
96 
97  \param c1 The first coordinate
98  \param c2 The second coordinate
99 
100  \return The squared distance between the given coordinates
101  */
102  static double CalculateSquaredDistance(const te::gm::Coord2D& c1, const te::gm::Coord2D& c2);
103 
104  /*!
105  \brief Calculates the squared distance between p1 and p2, that is, the distance calculation without the final square root step. This is particularly useful when we just need to compare distance values, and not know the distance itself.
106 
107  \param c1 The first point
108  \param c2 The second point
109 
110  \return The squared distance between the given coordinates
111  */
112  static double CalculateSquaredDistance(const te::gm::Point& p1, const te::gm::Point& p2);
113 
114  /*!
115  \brief Checks in an optimized way if the given coordinates are within the given distance. The optimization is achieved by avoiding the calculation of the square root
116 
117  \param c1 The first coordinate
118  \param c2 The second coordinate
119  \param distance The distance to be considered
120 
121  \return TRUE if the coordinates are within the given distance. FALSE otherwise
122  */
123  static bool IsWithinDistance(const te::gm::Coord2D& c1, const te::gm::Coord2D& c2, double distance);
124 
125  /*!
126  \brief Checks in an optimized way if the given coordinates are within the given distance. The optimization is achieved by avoiding the calculation of the square root
127 
128  \param p1 The first point
129  \param p2 The second point
130  \param distance The distance to be considered
131 
132  \return TRUE if the coordinates are within the given distance. FALSE otherwise
133  */
134  static bool IsWithinDistance(const te::gm::Point& p1, const te::gm::Point& p2, double distance);
135 
136  /*!
137  \brief Checks if the given coordToCheck lies within the line
138 
139  \param coordToCheck The coordinate to be checked
140  \param c1 First coordinate of the line segment
141  \param c2 Last coordinate of the line segment
142  \return TRUE if the coordinate lies within the segment. FALSE otherwise
143  */
144  static bool IsOnLine(const te::gm::Coord2D& coordToCheck, const te::gm::Coord2D& c1, const te::gm::Coord2D& c2);
145 
146  /*!
147  \brief Calculate the perpendicular squared distance of a point in a segment.
148 
149  \param first The first segment coordinate.
150  \param last The last segment coordinate.
151  \param pin The coordinate to be calculated.
152  \param pinter A intern coordinate calculated based on pin projected intersection with the segment.
153 
154  \return The perpendicular distance.
155  */
156  static double PerpendicularSquaredDistance(const te::gm::Coord2D& first, const te::gm::Coord2D& last, const te::gm::Coord2D& pin, te::gm::Coord2D& projectedCoordinate, bool& isOnSegment);
157 
158  /*!
159  \brief Calculate the perpendicular squared distance of a point in a segment.
160 
161  \param first The first segment point.
162  \param last The last segment point.
163  \param pin The point to be calculated.
164  \param pinter A intern point calculated based on pin projected intersection with the segment.
165 
166  \return The perpendicular distance.
167  */
168  static double PerpendicularSquaredDistance(const te::gm::Point& first, const te::gm::Point& last, const te::gm::Point& pin, Point& projectedPoint, bool& isOnSegment);
169 
170  /*!
171  \brief Calculate the perpendicular distance of a point in a segment.
172 
173  \param first The first segment coordinate.
174  \param last The last segment coordinate.
175  \param pin The coordinate to be calculated.
176  \param pinter A intern coordinate calculated based on pin projected intersection with the segment.
177 
178  \return The perpendicular distance.
179  */
180  static double PerpendicularDistance(const te::gm::Coord2D& first, const te::gm::Coord2D& last, const te::gm::Coord2D& pin, te::gm::Coord2D& projectedCoordinate, bool& isOnSegment);
181 
182  /*!
183  \brief Calculate the perpendicular distance of a point in a segment.
184 
185  \param first The first segment point.
186  \param last The last segment point.
187  \param pin The point to be calculated.
188  \param pinter A intern point calculated based on pin projected intersection with the segment.
189 
190  \return The perpendicular distance.
191  */
192  static double PerpendicularDistance(const te::gm::Point& first, const te::gm::Point& last, const te::gm::Point& pin, te::gm::Point& projectedPoint, bool& isOnSegment);
193 
194  /*!
195  \brief Detects and adds all the coordinates from given geometryVector which projected coordinates lies within any segment of the geometries and is within the given 'distance'
196 
197  \param geometryVector The geometryVector to be processed.
198  \param distance The distance to be considered for the projected coordinates
199 
200  \return The processed geometryVector
201  */
202  static te::gm::GeometryVector AddProjectedCoordinates(const te::gm::GeometryVector& geometryVector, double distance, bool& wasChanged);
203  };
204 
206  {
207  public:
208 
209  static void populateCoordinateIndex(const te::gm::Geometry* geometry, te::gm::KdTreeSingle& spatialIndex);
210  static void populateCoordinateIndex(const te::gm::GeometryVector& geometryVector, te::gm::KdTreeSingle& spatialIndex);
211  };
212 
213  /*!
214  \struct CoordDistanceOrderFunctor
215 
216  \brief This local struct is used sorts a vector of coordinates by distance.
217  */
219  {
221 
223  : m_reference(reference)
224  {
225  }
226 
227  bool operator()(const te::gm::Coord2D& c1, const te::gm::Coord2D& c2) const
228  {
231 
232  if (distance1 < distance2)
233  return true;
234 
235  return false;
236  }
237  };
238 
239  /*!
240  \struct PointDistanceOrderFunctor
241 
242  \brief This local struct is used sorts a vector of points by distance.
243  */
245  {
247 
249  : m_reference(reference)
250  {
251 
252  }
253 
254  bool operator()(const te::gm::Point& p1, const te::gm::Point& p2) const
255  {
258 
259  if (distance1 < distance2)
260  return true;
261 
262  return false;
263  }
264  };
265 
266 
267  } // end namespace gm
268 } // end namespace te
269 
270 #endif // __TERRALIB_GEOMETRY_INTERNAL_GEOMETRYFUNCTIONS_H
std::vector< te::gm::Geometry * > GeometryVector
static double CalculateSquaredDistance(const te::gm::Coord2D &c1, const te::gm::Coord2D &c2)
Calculates the squared distance between c1 and c2, that is, the distance calculation without the fina...
A point with x and y coordinate values.
TESAEXPORT double CalculateDistance(te::gm::Geometry *geom, te::gm::Coord2D &coord)
Function used to calculate the distance from a coord to the center of a geometry. ...
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TEGEOMEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:76
This local struct is used sorts a vector of points by distance.
A point with x and y coordinate values.
Definition: Point.h:50
TerraLib.
A class that represents a two dimensional K-d Tree (2-d Tree).
Definition: Index.h:67
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:77
PointDistanceOrderFunctor(const te::gm::Point &reference)
CoordDistanceOrderFunctor(const te::gm::Coord2D &reference)
This local struct is used sorts a vector of coordinates by distance.
Configuration flags for the Vector Geometry Model of TerraLib.
bool operator()(const te::gm::Coord2D &c1, const te::gm::Coord2D &c2) const
TEMNTEXPORT double PerpendicularDistance(te::gm::Coord2D &first, te::gm::Coord2D &last, te::gm::Coord2D &pin, te::gm::Coord2D &pinter)
bool operator()(const te::gm::Point &p1, const te::gm::Point &p2) const