SAGA API  v9.5
shape_points.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_points.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(pOwner, Index)
65 {
66  m_pParts = NULL;
67  m_nParts = 0;
68 
69  m_nPoints = 0; // total number of points
70 
71  m_bUpdate = true;
72 }
73 
74 //---------------------------------------------------------
76 {
77  Destroy();
78 }
79 
80 
82 // //
84 
85 //---------------------------------------------------------
87 {
89 
90  Del_Parts();
91 }
92 
93 
95 // //
97 
98 //---------------------------------------------------------
100 {
101  Del_Parts();
102 
103  TSG_Vertex_Type Vertex_Type = Get_Vertex_Type();
104 
105  if( pShape->Get_Type() == SHAPE_TYPE_Point ) // just in case...
106  {
107  Add_Point(pShape->Get_Point());
108 
109  switch( Vertex_Type )
110  {
111  case SG_VERTEX_TYPE_XYZM: Get_Part(0)->Set_M(pShape->Get_M(0), 0);
112  case SG_VERTEX_TYPE_XYZ : Get_Part(0)->Set_Z(pShape->Get_Z(0), 0);
113  default: break;
114  }
115 
116  return( true );
117  }
118 
119  for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
120  {
121  Add_Part(((CSG_Shape_Points *)pShape)->Get_Part(iPart));
122  }
123 
124  return( true );
125 }
126 
127 
129 // //
131 
132 //---------------------------------------------------------
134 {
136 
137  m_pParts[m_nParts++] = _Get_Part();
138 
139  return( m_nParts );
140 }
141 
142 //---------------------------------------------------------
144 {
145  int iPart = m_nParts;
146 
147  if( pPart && _Add_Part() > iPart )
148  {
149  m_pParts[iPart]->Assign(pPart);
150 
151  if( bRevert )
152  {
153  m_pParts[iPart]->Revert_Points();
154  }
155  }
156 
157  return( m_nParts );
158 }
159 
160 //---------------------------------------------------------
161 int CSG_Shape_Points::Del_Part(int del_Part)
162 {
163  if( del_Part >= 0 && del_Part < m_nParts )
164  {
165  m_nParts--;
166 
167  delete(m_pParts[del_Part]);
168 
169  for(int iPart=del_Part; iPart<m_nParts; iPart++)
170  {
171  m_pParts[iPart] = m_pParts[iPart + 1];
172  }
173 
175 
176  _Invalidate();
177  }
178 
179  return( m_nParts );
180 }
181 
182 //---------------------------------------------------------
184 {
185  for(int iPart=m_nParts-1; iPart>=0; iPart--)
186  {
187  Del_Part(iPart);
188  }
189 
190  return( m_nParts );
191 }
192 
193 
195 // //
197 
198 //---------------------------------------------------------
199 int CSG_Shape_Points::Ins_Point(double x, double y, int iPoint, int iPart)
200 {
201  if( iPart >= m_nParts )
202  {
203  for(int i=m_nParts; i<=iPart; i++)
204  {
205  _Add_Part();
206  }
207  }
208 
209  return( iPart >= 0 && iPart < m_nParts ? m_pParts[iPart]->Ins_Point(x, y, iPoint) : 0 );
210 }
211 
212 //---------------------------------------------------------
213 int CSG_Shape_Points::Ins_Point(const CSG_Point_3D &p, int iPoint, int iPart)
214 {
215  if( Ins_Point(p.x, p.y, iPoint, iPart) )
216  {
217  Set_Z(p.z, iPoint, iPart);
218 
219  return( 1 );
220  }
221 
222  return( 0 );
223 }
224 
225 //---------------------------------------------------------
226 int CSG_Shape_Points::Ins_Point(const CSG_Point_4D &p, int iPoint, int iPart)
227 {
228  if( Ins_Point(p.x, p.y, iPoint, iPart) )
229  {
230  Set_Z(p.z, iPoint, iPart);
231  Set_M(p.m, iPoint, iPart);
232 
233  return( 1 );
234  }
235 
236  return( 0 );
237 }
238 
239 
241 // //
243 
244 //---------------------------------------------------------
245 int CSG_Shape_Points::Set_Point(double x, double y, int iPoint, int iPart)
246 {
247  return( iPart >= 0 && iPart < m_nParts ? m_pParts[iPart]->Set_Point(x, y, iPoint) : 0 );
248 }
249 
250 //---------------------------------------------------------
251 int CSG_Shape_Points::Set_Point(const CSG_Point_3D &p, int iPoint, int iPart)
252 {
253  if( Set_Point(p.x, p.y, iPoint, iPart) )
254  {
255  Set_Z(p.z, iPoint, iPart);
256 
257  return( 1 );
258  }
259 
260  return( 0 );
261 }
262 
263 //---------------------------------------------------------
264 int CSG_Shape_Points::Set_Point(const CSG_Point_4D &p, int iPoint, int iPart)
265 {
266  if( Set_Point(p.x, p.y, iPoint, iPart) )
267  {
268  Set_Z(p.z, iPoint, iPart);
269  Set_M(p.m, iPoint, iPart);
270 
271  return( 1 );
272  }
273 
274  return( 0 );
275 }
276 
277 
279 // //
281 
282 //---------------------------------------------------------
283 int CSG_Shape_Points::Del_Point(int iPoint, int iPart)
284 {
285  return( iPart >= 0 && iPart < m_nParts ? m_pParts[iPart]->Del_Point(iPoint) : 0 );
286 }
287 
288 
290 // //
292 
293 //---------------------------------------------------------
295 {
296  for(int iPart=0; iPart<m_nParts; iPoint-=m_pParts[iPart++]->Get_Count())
297  {
298  if( iPoint < m_pParts[iPart]->Get_Count() )
299  {
300  return( m_pParts[iPart]->Get_Point(iPoint) );
301  }
302  }
303 
304  return( (TSG_Point)CSG_Point(0., 0.) );
305 }
306 
307 
309 // //
311 
312 //---------------------------------------------------------
314 {
315  if( m_bUpdate )
316  {
317  int nPoints = 0;
318 
319  for(int iPart=0; iPart<m_nParts; iPart++)
320  {
321  CSG_Shape_Part *pPart = m_pParts[iPart];
322 
323  if( pPart->Get_Count() > 0 )
324  {
325  if( nPoints == 0 )
326  {
327  nPoints = pPart->Get_Count();
328 
329  m_Extent = pPart->Get_Extent();
330 
331  m_ZMin = pPart->Get_ZMin();
332  m_ZMax = pPart->Get_ZMax();
333 
334  m_MMin = pPart->Get_MMin();
335  m_MMax = pPart->Get_MMax();
336  }
337  else
338  {
339  nPoints += pPart->Get_Count();
340 
341  m_Extent.Union(pPart->Get_Extent());
342 
343  if ( m_ZMin > pPart->Get_ZMin() ) { m_ZMin = pPart->Get_ZMin(); }
344  else if( m_ZMax < pPart->Get_ZMax() ) { m_ZMax = pPart->Get_ZMax(); }
345 
346  if ( m_MMin > pPart->Get_MMin() ) { m_MMin = pPart->Get_MMin(); }
347  else if( m_MMax < pPart->Get_MMax() ) { m_MMax = pPart->Get_MMax(); }
348  }
349  }
350  }
351 
352  m_bUpdate = false;
353 
354  //_ASSERT(nPoints == m_nPoints);
355  }
356 }
357 
358 
360 // //
362 
363 //---------------------------------------------------------
365 {
366  int n = 0; CSG_Point c(0., 0.);
367 
368  for(int iPart=0; iPart<Get_Part_Count(); iPart++)
369  {
370  for(int iPoint=0; iPoint<Get_Point_Count(iPart); iPoint++)
371  {
372  c += Get_Point(iPoint, iPart);
373  n ++;
374  }
375  }
376 
377  if( n > 0 )
378  {
379  c.Assign(c.x / n, c.y / n);
380  }
381 
382  return( (TSG_Point)c );
383 }
384 
385 
387 // //
389 
390 //---------------------------------------------------------
392 {
393  TSG_Point Next; return( Get_Distance(Point, Next) );
394 }
395 
396 //---------------------------------------------------------
397 double CSG_Shape_Points::Get_Distance(TSG_Point Point, int iPart) const
398 {
399  TSG_Point Next; return( Get_Distance(Point, Next, iPart) );
400 }
401 
402 //---------------------------------------------------------
404 {
405  double Distance = Get_Distance(Point, Next, 0);
406 
407  for(int iPart=1; iPart<m_nParts && Distance!=0.; iPart++)
408  {
409  TSG_Point iNext; double iDistance = Get_Distance(Point, iNext, iPart);
410 
411  if( iDistance >= 0. && (iDistance < Distance || Distance < 0.) )
412  {
413  Distance = iDistance; Next = iNext;
414  }
415  }
416 
417  return( Distance );
418 }
419 
420 //---------------------------------------------------------
421 double CSG_Shape_Points::Get_Distance(TSG_Point Point, TSG_Point &Next, int iPart) const
422 {
423  double Distance = -1.;
424 
425  if( iPart >= 0 && iPart < m_nParts )
426  {
427  TSG_Point *pPoint = m_pParts[iPart]->m_Points;
428 
429  for(int iPoint=0; iPoint<m_pParts[iPart]->Get_Count() && Distance!=0.; iPoint++, pPoint++)
430  {
431  double iDistance = SG_Get_Distance(Point, *pPoint);
432 
433  if( iDistance < Distance || Distance < 0. )
434  {
435  Distance = iDistance; Next = *pPoint;
436  }
437  }
438  }
439 
440  return( Distance );
441 }
442 
443 
445 // //
447 
448 //---------------------------------------------------------
450 {
451  CSG_Shape *piPoints, *pjPoints;
452 
453  if( Get_Point_Count() < pShape->Get_Point_Count() )
454  {
455  piPoints = this;
456  pjPoints = pShape;
457  }
458  else
459  {
460  piPoints = pShape;
461  pjPoints = this;
462  }
463 
464  bool bIn = false;
465  bool bOut = false;
466 
467  for(int iPart=0; iPart<piPoints->Get_Part_Count(); iPart++)
468  {
469  for(int iPoint=0; iPoint<piPoints->Get_Point_Count(iPart); iPoint++)
470  {
471  CSG_Point Point = piPoints->Get_Point(iPoint, iPart);
472 
473  for(int jPart=0; jPart<pjPoints->Get_Part_Count(); jPart++)
474  {
475  for(int jPoint=0; jPoint<pjPoints->Get_Point_Count(jPart); jPoint++)
476  {
477  if( Point.is_Equal(pjPoints->Get_Point(jPoint, jPart)) )
478  {
479  bIn = true;
480  }
481  else
482  {
483  bOut = true;
484  }
485 
486  if( bIn && bOut )
487  {
488  return( INTERSECTION_Overlaps );
489  }
490  }
491  }
492  }
493  }
494 
495  if( bIn )
496  {
497  return( piPoints == this ? INTERSECTION_Contained : INTERSECTION_Contains );
498  }
499 
500  return( INTERSECTION_None );
501 }
502 
503 //---------------------------------------------------------
505 {
506  for(int iPart=0; iPart<m_nParts; iPart++)
507  {
508  TSG_Point *p = m_pParts[iPart]->m_Points;
509 
510  for(int iPoint=0; iPoint<m_pParts[iPart]->Get_Count(); iPoint++, p++)
511  {
512  if( Extent.xMin <= p->x && p->x <= Extent.xMax
513  && Extent.yMin <= p->y && p->y <= Extent.yMax )
514  {
515  return( INTERSECTION_Overlaps );
516  }
517  }
518  }
519 
520  return( INTERSECTION_None );
521 }
522 
523 
525 // //
526 // //
527 // //
529 
530 //---------------------------------------------------------
CSG_Shape_Part::Get_MMax
double Get_MMax(void)
Definition: shapes.h:434
CSG_Shape_Points::m_pParts
CSG_Shape_Part ** m_pParts
Definition: shapes.h:569
CSG_Shape_Points::Get_MMax
virtual double Get_MMax(void)
Definition: shapes.h:541
TSG_Intersection
TSG_Intersection
Definition: geo_tools.h:101
CSG_Shape_Part::Revert_Points
bool Revert_Points(void)
Definition: shape_part.cpp:372
CSG_Shape::Get_Z
virtual double Get_Z(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition: shapes.h:195
CSG_Shape_Part::Get_ZMin
double Get_ZMin(void)
Definition: shapes.h:418
CSG_Shape_Points::Set_Z
virtual void Set_Z(double z, int iPoint=0, int iPart=0)
Definition: shapes.h:533
CSG_Shape_Points::Del_Parts
virtual int Del_Parts(void)
Definition: shape_points.cpp:183
CSG_Shape_Points::m_MMax
double m_MMax
Definition: shapes.h:565
INTERSECTION_Contains
@ INTERSECTION_Contains
Definition: geo_tools.h:106
CSG_Shape_Points
Definition: shapes.h:486
CSG_Shape_Points::On_Assign
virtual bool On_Assign(CSG_Shape *pShape)
Definition: shape_points.cpp:99
SSG_Point_3D::x
double x
Definition: geo_tools.h:265
SSG_Rect::xMax
double xMax
Definition: geo_tools.h:465
CSG_Shape_Points::_Update_Extent
void _Update_Extent(void)
Definition: shape_points.cpp:313
SSG_Point
Definition: geo_tools.h:128
CSG_Shape_Points::Destroy
virtual void Destroy(void)
Definition: shape_points.cpp:86
CSG_Shape_Points::m_ZMax
double m_ZMax
Definition: shapes.h:565
CSG_Shape_Points::Set_Point
virtual int Set_Point(double x, double y, int iPoint=0, int iPart=0)
Definition: shape_points.cpp:245
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
CSG_Shape_Part::Assign
virtual bool Assign(CSG_Shape_Part *pPart)
Definition: shape_part.cpp:177
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_Points::m_MMin
double m_MMin
Definition: shapes.h:565
CSG_Shape::Destroy
virtual void Destroy(void)
Definition: shape.cpp:79
CSG_Shape_Points::Get_Distance
virtual double Get_Distance(TSG_Point Point) const
Definition: shape_points.cpp:391
CSG_Shape_Points::Set_M
virtual void Set_M(double m, int iPoint=0, int iPart=0)
Definition: shapes.h:538
CSG_Shape::Get_Part_Count
virtual int Get_Part_Count(void) const =0
CSG_Shape_Points::Ins_Point
virtual int Ins_Point(double x, double y, int iPoint, int iPart=0)
Definition: shape_points.cpp:199
CSG_Point
Definition: geo_tools.h:135
SG_VERTEX_TYPE_XYZM
@ SG_VERTEX_TYPE_XYZM
Definition: shapes.h:94
SSG_Point_4D::m
double m
Definition: geo_tools.h:358
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_Points::m_ZMin
double m_ZMin
Definition: shapes.h:565
CSG_Shape_Points::Get_ZMax
virtual double Get_ZMax(void)
Definition: shapes.h:536
CSG_Shape_Part::m_Points
TSG_Point * m_Points
Definition: shapes.h:464
SSG_Point_4D::z
double z
Definition: geo_tools.h:358
CSG_Point_3D
Definition: geo_tools.h:271
CSG_Shape::Get_Vertex_Type
TSG_Vertex_Type Get_Vertex_Type(void) const
Definition: shape.cpp:94
SG_Get_Distance
double SG_Get_Distance(double ax, double ay, double bx, double by, bool bPolar)
Definition: geo_functions.cpp:103
CSG_Point_4D
Definition: geo_tools.h:364
sLong
signed long long sLong
Definition: api_core.h:158
CSG_Shape_Part::Set_M
void Set_M(double m, int iPoint)
Definition: shapes.h:431
CSG_Shape_Points::_Get_Part
virtual CSG_Shape_Part * _Get_Part(void)
Definition: shapes.h:574
CSG_Point::Assign
virtual void Assign(double x, double y)
Definition: geo_classes.cpp:86
CSG_Shape_Points::Add_Point
virtual int Add_Point(double x, double y, int iPart=0)
Definition: shapes.h:505
CSG_Shape_Points::~CSG_Shape_Points
virtual ~CSG_Shape_Points(void)
Definition: shape_points.cpp:75
CSG_Shape_Points::_Add_Part
int _Add_Part(void)
Definition: shape_points.cpp:133
CSG_Shape_Part::Set_Z
void Set_Z(double z, int iPoint)
Definition: shapes.h:416
CSG_Shape::Get_M
virtual double Get_M(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition: shapes.h:211
SSG_Rect::yMax
double yMax
Definition: geo_tools.h:465
CSG_Rect::Union
CSG_Rect & Union(double x, double y)
Definition: geo_classes.cpp:824
CSG_Shape_Points::m_nParts
int m_nParts
Definition: shapes.h:563
CSG_Shape_Points::m_Extent
CSG_Rect m_Extent
Definition: shapes.h:567
SSG_Point_4D::y
double y
Definition: geo_tools.h:358
CSG_Shape_Points::_Invalidate
virtual void _Invalidate(void)
Definition: shapes.h:576
CSG_Shape::Get_Point_Count
virtual int Get_Point_Count(void) const =0
TSG_Vertex_Type
TSG_Vertex_Type
Definition: shapes.h:91
shapes.h
CSG_Shape_Points::Add_Part
virtual int Add_Part(class CSG_Shape_Part *pPart, bool bRevert=false)
Definition: shape_points.cpp:143
SHAPE_TYPE_Point
@ SHAPE_TYPE_Point
Definition: shapes.h:102
CSG_Shape_Points::Get_Part
virtual CSG_Shape_Part * Get_Part(int iPart) const
Definition: shapes.h:501
CSG_Shape_Points::CSG_Shape_Points
CSG_Shape_Points(class CSG_Shapes *pOwner, sLong Index)
Definition: shape_points.cpp:63
SSG_Point_3D::y
double y
Definition: geo_tools.h:265
SSG_Point::x
double x
Definition: geo_tools.h:129
SSG_Point_4D::x
double x
Definition: geo_tools.h:358
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::On_Intersects
virtual TSG_Intersection On_Intersects(CSG_Shape *pShape)
Definition: shape_points.cpp:449
SSG_Point_3D::z
double z
Definition: geo_tools.h:265
CSG_Shape_Points::Del_Point
virtual int Del_Point(int iPoint, int iPart=0)
Definition: shape_points.cpp:283
CSG_Shape_Part::Get_ZMax
double Get_ZMax(void)
Definition: shapes.h:419
CSG_Shape_Points::Get_Point_Count
virtual int Get_Point_Count(void) const
Definition: shapes.h:496
CSG_Shapes
Definition: shapes.h:775
CSG_Shape_Points::m_nPoints
int m_nPoints
Definition: shapes.h:563
INTERSECTION_Contained
@ INTERSECTION_Contained
Definition: geo_tools.h:105
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
CSG_Shape_Points::Get_Part_Count
virtual int Get_Part_Count(void) const
Definition: shapes.h:498
CSG_Shape_Points::Del_Part
virtual int Del_Part(int iPart)
Definition: shape_points.cpp:161
CSG_Point::is_Equal
virtual bool is_Equal(const CSG_Point &Point, double epsilon=0.) const
Definition: geo_tools.h:174
CSG_Shape_Points::m_bUpdate
bool m_bUpdate
Definition: shapes.h:561
uLong
unsigned long long uLong
Definition: api_core.h:159
INTERSECTION_Overlaps
@ INTERSECTION_Overlaps
Definition: geo_tools.h:104
CSG_Shape_Points::Get_Centroid
virtual TSG_Point Get_Centroid(void)
Definition: shape_points.cpp:364
CSG_Shape
Definition: shapes.h:141
CSG_Shape_Part
Definition: shapes.h:372
SG_VERTEX_TYPE_XYZ
@ SG_VERTEX_TYPE_XYZ
Definition: shapes.h:93
CSG_Shape_Part::Get_MMin
double Get_MMin(void)
Definition: shapes.h:433