SAGA API  v9.6
shape_line.cpp
Go to the documentation of this file.
1 
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Application Programming Interface //
9 // //
10 // Library: SAGA_API //
11 // //
12 //-------------------------------------------------------//
13 // //
14 // shape_line.cpp //
15 // //
16 // Copyright (C) 2005 by Olaf Conrad //
17 // //
18 //-------------------------------------------------------//
19 // //
20 // This file is part of 'SAGA - System for Automated //
21 // Geoscientific Analyses'. //
22 // //
23 // This library is free software; you can redistribute //
24 // it and/or modify it under the terms of the GNU Lesser //
25 // General Public License as published by the Free //
26 // Software Foundation, either version 2.1 of the //
27 // License, or (at your option) any later version. //
28 // //
29 // This library is distributed in the hope that it will //
30 // be useful, but WITHOUT ANY WARRANTY; without even the //
31 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
32 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
33 // License for more details. //
34 // //
35 // You should have received a copy of the GNU Lesser //
36 // General Public License along with this program; if //
37 // not, see <http://www.gnu.org/licenses/>. //
38 // //
39 //-------------------------------------------------------//
40 // //
41 // contact: Olaf Conrad //
42 // Institute of Geography //
43 // University of Goettingen //
44 // Goldschmidtstr. 5 //
45 // 37077 Goettingen //
46 // Germany //
47 // //
48 // e-mail: oconrad@saga-gis.org //
49 // //
51 
52 //---------------------------------------------------------
53 #include "shapes.h"
54 
55 
57 // //
58 // //
59 // //
61 
62 //---------------------------------------------------------
64  : CSG_Shape_Points(pOwner, Index)
65 {}
66 
67 //---------------------------------------------------------
69 {}
70 
71 
73 // //
74 // //
75 // //
77 
78 //---------------------------------------------------------
80 {
81  if( pShape->Get_Type() == SHAPE_TYPE_Point || pShape->Get_Type() == SHAPE_TYPE_Points )
82  {
83  bool bIn = false;
84  bool bOut = false;
85 
86  for(int iPart=0; iPart<m_nParts; iPart++)
87  {
88  for(int jPart=0; jPart<pShape->Get_Part_Count(); jPart++)
89  {
90  for(int jPoint=1; jPoint<pShape->Get_Point_Count(jPart); jPoint++)
91  {
92  TSG_Point Point;
93 
94  if( Get_Distance(pShape->Get_Point(jPoint, jPart), Point, iPart) == 0.0 )
95  {
96  bIn = true;
97  }
98  else
99  {
100  bOut = true;
101  }
102 
103  if( bIn && bOut )
104  {
105  return( INTERSECTION_Overlaps );
106  }
107  }
108  }
109  }
110 
111  if( bIn )
112  {
113  return( INTERSECTION_Contained );
114  }
115  }
116 
117  //-----------------------------------------------------
118  else if( pShape->Get_Type() == SHAPE_TYPE_Line )
119  {
120  TSG_Point iA, iB, jA, jB, Crossing;
121 
122  for(int iPart=0; iPart<m_nParts; iPart++)
123  {
124  if( Get_Point_Count(iPart) > 1 )
125  {
126  iA = Get_Point(0, iPart);
127 
128  for(int iPoint=1; iPoint<Get_Point_Count(iPart); iPoint++)
129  {
130  iB = iA;
131  iA = Get_Point(iPoint, iPart);
132 
133  for(int jPart=0; jPart<pShape->Get_Part_Count(); jPart++)
134  {
135  if( pShape->Get_Point_Count(jPart) > 1 )
136  {
137  jA = pShape->Get_Point(0, jPart);
138 
139  for(int jPoint=1; jPoint<pShape->Get_Point_Count(jPart); jPoint++)
140  {
141  jB = jA;
142  jA = pShape->Get_Point(jPoint, jPart);
143 
144  if( SG_Get_Crossing(Crossing, iA, iB, jA, jB) )
145  {
146  return( INTERSECTION_Overlaps );
147  }
148  }
149  }
150  }
151  }
152  }
153  }
154  }
155 
156  return( INTERSECTION_None );
157 }
158 
159 //---------------------------------------------------------
161 {
162  // called if polygon's bounding box contains or overlaps with region.
163  // now let's figure out how region intersects with polygon itself
164 
165  //-----------------------------------------------------
166  for(int iPart=0; iPart<m_nParts; iPart++)
167  {
168  CSG_Shape_Part *pPart = m_pParts[iPart];
169 
170  switch( pPart->Get_Extent().Intersects(Region) )
171  {
172  case INTERSECTION_None: // region and line part are distinct
173  break;
174 
175  case INTERSECTION_Identical: // region contains line part
177  return( Get_Extent().Intersects(Region) );
178 
180  case INTERSECTION_Overlaps: // region overlaps with line part's extent, now let's look at the line part itself!
181  if( pPart->Get_Count() > 1 )
182  {
183  TSG_Point *pa, *pb, c;
184 
185  pb = pPart->m_Points;
186  pa = pb + 1;
187 
188  for(int iPoint=1; iPoint<pPart->Get_Count(); iPoint++, pb=pa++)
189  {
190  if( SG_Get_Crossing_InRegion(c, *pa, *pb, Region) )
191  {
192  return( INTERSECTION_Overlaps );
193  }
194  }
195  }
196  break;
197  }
198  }
199 
200  //-----------------------------------------------------
201  TSG_Point p = Get_Point(0, 0);
202 
203  if( Region.xMin <= p.x && p.x <= Region.xMax
204  && Region.yMin <= p.y && p.y <= Region.yMax )
205  {
206  return( INTERSECTION_Contained );
207  }
208 
209  return( INTERSECTION_None );
210 }
211 
212 
214 // //
215 // //
216 // //
218 
219 //---------------------------------------------------------
221 {
222  return( Get_Extent().Get_Center() );
223 }
224 
225 
227 // //
228 // //
229 // //
231 
232 //---------------------------------------------------------
234 {
235  int iPart;
236  double Length;
237 
238  for(iPart=0, Length=0.0; iPart<m_nParts; iPart++)
239  {
240  Length += Get_Length(iPart);
241  }
242 
243  return( Length );
244 }
245 
246 //---------------------------------------------------------
247 double CSG_Shape_Line::Get_Length(int iPart)
248 {
249  int iPoint;
250  double Length;
251  TSG_Point *pA, *pB;
252 
253  if( iPart >= 0 && iPart < m_nParts && m_pParts[iPart]->Get_Count() > 1 )
254  {
255  pB = m_pParts[iPart]->m_Points;
256  pA = pB + 1;
257 
258  for(iPoint=1, Length=0.0; iPoint<m_pParts[iPart]->Get_Count(); iPoint++, pB=pA++)
259  {
260  Length += SG_Get_Distance(*pA, *pB);
261  }
262 
263  return( Length );
264  }
265 
266  return( 0.0 );
267 }
268 
269 
271 // //
272 // //
273 // //
275 
276 //---------------------------------------------------------
277 double CSG_Shape_Line::Get_Distance(TSG_Point Point, TSG_Point &Next, int iPart) const
278 {
279  double Distance = -1.;
280 
281  if( iPart >= 0 && iPart < m_nParts && m_pParts[iPart]->Get_Count() > 1 )
282  {
283  TSG_Point *pB = m_pParts[iPart]->m_Points;
284  TSG_Point *pA = pB + 1, C;
285 
286  Distance = SG_Get_Nearest_Point_On_Line(Point, *pA, *pB, Next);
287 
288  for(int i=1; i<m_pParts[iPart]->Get_Count() && Distance!=0.; i++, pB=pA++)
289  {
290  double d = SG_Get_Nearest_Point_On_Line(Point, *pA, *pB, C);
291 
292  if( d >= 0. && (d < Distance || Distance < 0.) )
293  {
294  Distance = d;
295  Next = C;
296  }
297  }
298  }
299 
300  return( Distance );
301 }
302 
303 
305 // //
306 // //
307 // //
309 
310 //---------------------------------------------------------
CSG_Shape_Points::m_pParts
CSG_Shape_Part ** m_pParts
Definition: shapes.h:569
TSG_Intersection
TSG_Intersection
Definition: geo_tools.h:101
SG_Get_Crossing_InRegion
bool SG_Get_Crossing_InRegion(TSG_Point &Crossing, const TSG_Point &a, const TSG_Point &b, const TSG_Rect &Region)
Definition: geo_functions.cpp:342
CSG_Shape_Line::Get_Distance
virtual double Get_Distance(TSG_Point Point, TSG_Point &Next, int iPart) const
Definition: shape_line.cpp:277
C
#define C
INTERSECTION_Contains
@ INTERSECTION_Contains
Definition: geo_tools.h:106
CSG_Rect::Intersects
TSG_Intersection Intersects(const CSG_Rect &Rect) const
Definition: geo_classes.cpp:881
CSG_Shape_Points
Definition: shapes.h:486
CSG_Shape_Line::On_Intersects
virtual TSG_Intersection On_Intersects(CSG_Shape *pShape)
Definition: shape_line.cpp:79
SSG_Rect::xMax
double xMax
Definition: geo_tools.h:465
CSG_Shape::Intersects
TSG_Intersection Intersects(CSG_Shape *pShape)
Definition: shape.cpp:118
SSG_Point
Definition: geo_tools.h:128
SG_Get_Nearest_Point_On_Line
double SG_Get_Nearest_Point_On_Line(const TSG_Point &Point, const TSG_Point &Line_A, const TSG_Point &Line_B, TSG_Point &Line_Point, bool bExactMatch)
Definition: geo_functions.cpp:421
CSG_Shape::Get_Type
TSG_Shape_Type Get_Type(void) const
Definition: shape.cpp:88
SSG_Rect::xMin
double xMin
Definition: geo_tools.h:465
SSG_Rect
Definition: geo_tools.h:464
CSG_Shape_Points::Get_Point
virtual TSG_Point Get_Point(int iPoint=0) const
Definition: shape_points.cpp:294
CSG_Shape::Get_Point
virtual TSG_Point Get_Point(int iPoint=0) const =0
CSG_Shape::Get_Part_Count
virtual int Get_Part_Count(void) const =0
CSG_Shape_Line::Get_Length
double Get_Length(void)
Definition: shape_line.cpp:233
CSG_Shape_Part::Get_Extent
const CSG_Rect & Get_Extent(void)
Definition: shapes.h:384
INTERSECTION_None
@ INTERSECTION_None
Definition: geo_tools.h:102
CSG_Shape_Part::m_Points
TSG_Point * m_Points
Definition: shapes.h:464
SG_Get_Distance
double SG_Get_Distance(double ax, double ay, double bx, double by, bool bPolar)
Definition: geo_functions.cpp:103
sLong
signed long long sLong
Definition: api_core.h:158
INTERSECTION_Identical
@ INTERSECTION_Identical
Definition: geo_tools.h:103
SSG_Rect::yMax
double yMax
Definition: geo_tools.h:465
SHAPE_TYPE_Line
@ SHAPE_TYPE_Line
Definition: shapes.h:104
CSG_Shape_Points::m_nParts
int m_nParts
Definition: shapes.h:563
CSG_Shape_Line::~CSG_Shape_Line
virtual ~CSG_Shape_Line(void)
Definition: shape_line.cpp:68
CSG_Shape::Get_Point_Count
virtual int Get_Point_Count(void) const =0
shapes.h
SHAPE_TYPE_Point
@ SHAPE_TYPE_Point
Definition: shapes.h:102
SSG_Point::x
double x
Definition: geo_tools.h:129
CSG_Shape_Points::Get_Extent
virtual const CSG_Rect & Get_Extent(void)
Definition: shapes.h:543
SSG_Rect::yMin
double yMin
Definition: geo_tools.h:465
CSG_Shape_Part::Get_Count
int Get_Count(void) const
Definition: shapes.h:386
SSG_Point::y
double y
Definition: geo_tools.h:129
CSG_Shape_Points::Get_Point_Count
virtual int Get_Point_Count(void) const
Definition: shapes.h:496
CSG_Shape_Line::CSG_Shape_Line
CSG_Shape_Line(class CSG_Shapes *pOwner, sLong Index)
Definition: shape_line.cpp:63
CSG_Shapes
Definition: shapes.h:775
SHAPE_TYPE_Points
@ SHAPE_TYPE_Points
Definition: shapes.h:103
INTERSECTION_Contained
@ INTERSECTION_Contained
Definition: geo_tools.h:105
SG_Get_Crossing
bool SG_Get_Crossing(TSG_Point &Crossing, const TSG_Point &a1, const TSG_Point &a2, const TSG_Point &b1, const TSG_Point &b2, bool bExactMatch)
Definition: geo_functions.cpp:280
CSG_Shape_Line::Get_Centroid
virtual TSG_Point Get_Centroid(void)
Definition: shape_line.cpp:220
INTERSECTION_Overlaps
@ INTERSECTION_Overlaps
Definition: geo_tools.h:104
CSG_Shape
Definition: shapes.h:141
CSG_Shape_Part
Definition: shapes.h:372