Line.cpp
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/Line.cpp
22 
23  \brief A Line is LineString with 2 points.
24 */
25 
26 // TerraLib
27 #include "Line.h"
28 #include "Point.h"
29 
30 
31 // STL
32 #include <cassert>
33 #include <cmath>
34 #include <climits>
35 
36 //GEOS
37 #include <geos/algorithm/LineIntersector.h>
38 
39 
41  : LineString(2, t, srid, mbr)
42 {
43 }
44 
45 te::gm::Line::Line(const Point& first, const Point& second, GeomType t, int srid, Envelope* mbr)
46  : LineString(2, t, srid, mbr)
47 {
48  m_coords[0].x = first.getX();
49  m_coords[0].y = first.getY();
50 
51  m_coords[1].x = second.getX();
52  m_coords[1].y = second.getY();
53 
54  if((t & 0xF00) == 0x300)
55  {
56  m_zA[0] = first.getZ();
57  m_zA[1] = second.getZ();
58  }
59  else if((t & 0xF00) == 0x700)
60  {
61  m_mA[0] = first.getM();
62  m_mA[1] = second.getM();
63  }
64  else if((t & 0xF00) == 0xB00)
65  {
66  m_zA[0] = first.getZ();
67  m_zA[1] = second.getZ();
68 
69  m_mA[0] = first.getM();
70  m_mA[1] = second.getM();
71  }
72 }
73 
74 te::gm::Line::Line(const Line& rhs)
75 
76  = default;
77 
78 te::gm::Line::~Line() = default;
79 
80 te::gm::Line& te::gm::Line::operator=(const Line& rhs) = default;
81 
83 {
84  return new Line(*this);
85 }
86 
87 bool te::gm::Line::intersection(const Line& line, Point& ret) const
88 {
89  geos::algorithm::LineIntersector li;
90  geos::geom::Coordinate p0(m_coords[0].x, m_coords[0].y);
91  geos::geom::Coordinate p1(m_coords[1].x, m_coords[1].y);
92  geos::geom::Coordinate lp0(line.m_coords[0].x, line.m_coords[0].y);
93  geos::geom::Coordinate lp1(line.m_coords[1].x, line.m_coords[1].y);
94  li.computeIntersection(p0, p1, lp0, lp1);
95  if (li.hasIntersection())
96  {
97  ret.setX(li.getIntersection(0).x);
98  ret.setY(li.getIntersection(0).y);
99  return true;
100  }
101  return false;
102 }
103 
105 {
106  double dx = m_coords[0].x - m_coords[1].x;
107  double dy = m_coords[0].y - m_coords[1].y;
108  if (dx == 0.)
109  return std::numeric_limits< double >::max();
110  return(dy / dx);
111 }
112 
113 
114 void te::gm::Line::setCoord(int index, double x, double y, double z, double m)
115 {
116  m_coords[index].x = x;
117  m_coords[index].y = y;
118  if ((m_gType & 0xF00) == 0x300)
119  {
120  m_zA[index] = z;
121  }
122  else if ((m_gType & 0xF00) == 0x700)
123  {
124  m_mA[index] = m;
125  }
126  else if ((m_gType & 0xF00) == 0xB00)
127  {
128  m_zA[index] = z;
129  m_mA[index] = m;
130  }
131 }
132 
134 {
135  double A, B, C, distance;
136 
137  // coefficients of line p1,p2
138  A = m_coords[0].y - m_coords[1].y;
139  B = m_coords[1].x - m_coords[0].x;
140  C = (m_coords[0].x - m_coords[1].x)* m_coords[0].y + (m_coords[1].y - m_coords[0].y)*m_coords[0].x;
141  distance = std::fabs(A*(p.getX()) + B*(p.getY()) + C) / std::sqrt(A*A + B*B);
142 
143  return(distance);
144 }
145 
146 
147 
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
A Line is LineString with 2 points.
Definition: Line.h:50
double y
y-coordinate.
Definition: Coord2D.h:114
A Line is LineString with 2 points.
static te::dt::Date dx(2010, 12, 31)
double x
x-coordinate.
Definition: Coord2D.h:113
A point with x and y coordinate values.
Line & operator=(const Line &rhs)
Assignment operator.
double distance(te::gm::Point p)
Definition: Line.cpp:133
bool intersection(const Line &line, Point &coord) const
Definition: Line.cpp:87
double * m_zA
A pointer to z values.
Definition: LineString.h:494
unsigned int line
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:152
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
~Line()
Virtual destructor.
double Angle()
Definition: Line.cpp:104
te::gm::Polygon * p
void setCoord(int index, double x, double y, double z=0., double m=0.)
Definition: Line.cpp:114
te::dt::AbstractData * clone() const
It clones the line.
Definition: Line.cpp:82
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
Coord2D * m_coords
A pointer to x, y values.
Definition: LineString.h:493
const double & getZ() const
It returns the Point z-coordinate value, if it has one or DoubleNotANumber otherwise.
Definition: Point.h:166
void setX(const double &x)
It sets the Point x-coordinate value.
Definition: Point.h:145
Line(GeomType t, int srid=0, Envelope *mbr=0)
It initializes the line with the specified spatial reference system id and envelope.
Definition: Line.cpp:40
const double & getM() const
It returns the Point m-coordinate value, if it has one or DoubleNotANumber otherwise.
Definition: Point.h:180
GeomType m_gType
Internal geometry type.
void setY(const double &y)
It sets the Point y-coordinate value.
Definition: Point.h:159
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:138
double * m_mA
A pointer to m values.
Definition: LineString.h:495