SAGA API v9.10
Loading...
Searching...
No Matches
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//---------------------------------------------------------
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
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//---------------------------------------------------------
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//---------------------------------------------------------
199int 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//---------------------------------------------------------
213int 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//---------------------------------------------------------
226int 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//---------------------------------------------------------
245int 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//---------------------------------------------------------
251int 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//---------------------------------------------------------
264int 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//---------------------------------------------------------
283int 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//---------------------------------------------------------
397double 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//---------------------------------------------------------
421double 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//---------------------------------------------------------
unsigned long long uLong
Definition api_core.h:159
signed long long sLong
Definition api_core.h:158
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
virtual void Assign(double x, double y)
virtual bool is_Equal(const CSG_Point &Point, double epsilon=0.) const
Definition geo_tools.h:175
double Get_ZMax(void)
Definition shapes.h:416
double Get_MMin(void)
Definition shapes.h:430
const CSG_Rect & Get_Extent(void)
Definition shapes.h:381
void Set_Z(double z, int iPoint)
Definition shapes.h:413
double Get_MMax(void)
Definition shapes.h:431
void Set_M(double m, int iPoint)
Definition shapes.h:428
int Get_Count(void) const
Definition shapes.h:383
double Get_ZMin(void)
Definition shapes.h:415
virtual int Add_Point(double x, double y, int iPart=0)
Definition shapes.h:502
CSG_Shape_Points(class CSG_Shapes *pOwner, sLong Index)
virtual void _Invalidate(void)
Definition shapes.h:573
double m_ZMax
Definition shapes.h:562
virtual TSG_Point Get_Centroid(void)
friend class CSG_Shapes
Definition shapes.h:484
virtual int Add_Part(class CSG_Shape_Part *pPart, bool bRevert=false)
virtual void Set_M(double m, int iPoint=0, int iPart=0)
Definition shapes.h:535
friend class CSG_Shape_Part
Definition shapes.h:485
virtual TSG_Point Get_Point(int iPoint=0) const
virtual void Set_Z(double z, int iPoint=0, int iPart=0)
Definition shapes.h:530
virtual int Set_Point(double x, double y, int iPoint=0, int iPart=0)
CSG_Shape_Part ** m_pParts
Definition shapes.h:566
CSG_Rect m_Extent
Definition shapes.h:564
virtual CSG_Shape_Part * Get_Part(int iPart) const
Definition shapes.h:498
virtual int Del_Part(int iPart)
virtual ~CSG_Shape_Points(void)
virtual int Get_Part_Count(void) const
Definition shapes.h:495
virtual int Get_Point_Count(void) const
Definition shapes.h:493
virtual TSG_Intersection On_Intersects(CSG_Shape *pShape)
virtual bool On_Assign(CSG_Shape *pShape)
virtual double Get_Distance(TSG_Point Point) const
double m_MMax
Definition shapes.h:562
virtual int Ins_Point(double x, double y, int iPoint, int iPart=0)
virtual CSG_Shape_Part * _Get_Part(void)
Definition shapes.h:571
virtual void Destroy(void)
virtual int Del_Parts(void)
void _Update_Extent(void)
virtual double Get_ZMax(void)
Definition shapes.h:533
double m_ZMin
Definition shapes.h:562
virtual int Del_Point(int iPoint, int iPart=0)
double m_MMin
Definition shapes.h:562
virtual double Get_MMax(void)
Definition shapes.h:538
virtual int Get_Point_Count(void) const =0
TSG_Shape_Type Get_Type(void) const
Definition shape.cpp:88
CSG_Shape(class CSG_Shapes *pOwner, sLong Index)
Definition shape.cpp:63
virtual int Get_Part_Count(void) const =0
virtual TSG_Point Get_Point(int iPoint=0) const =0
virtual double Get_Z(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition shapes.h:192
virtual void Destroy(void)
Definition shape.cpp:79
TSG_Vertex_Type Get_Vertex_Type(void) const
Definition shape.cpp:94
virtual double Get_M(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition shapes.h:208
double SG_Get_Distance(double ax, double ay, double bx, double by, bool bPolar)
struct SSG_Point TSG_Point
TSG_Intersection
Definition geo_tools.h:101
@ INTERSECTION_Overlaps
Definition geo_tools.h:104
@ INTERSECTION_None
Definition geo_tools.h:102
@ INTERSECTION_Contains
Definition geo_tools.h:106
@ INTERSECTION_Contained
Definition geo_tools.h:105
struct SSG_Rect TSG_Rect
TSG_Vertex_Type
Definition shapes.h:91
@ SG_VERTEX_TYPE_XYZM
Definition shapes.h:94
@ SG_VERTEX_TYPE_XYZ
Definition shapes.h:93
@ SHAPE_TYPE_Point
Definition shapes.h:102
double x
Definition geo_tools.h:129
double y
Definition geo_tools.h:129
double xMin
Definition geo_tools.h:468
double xMax
Definition geo_tools.h:468
double yMin
Definition geo_tools.h:468
double yMax
Definition geo_tools.h:468