SAGA API v9.10
Loading...
Searching...
No Matches
shape_part.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_part.cpp //
15// //
16// Copyright (C) 2008 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{
65 m_pOwner = pOwner;
66
67 m_nBuffer = 0;
68 m_nPoints = 0;
69 m_Points = NULL;
70 m_Z = NULL;
71 m_M = NULL;
72
73 m_bUpdate = true;
74}
75
76//---------------------------------------------------------
81
82//---------------------------------------------------------
84{
85 if( m_pOwner ) { m_pOwner->m_nPoints -= m_nPoints; }
86
87 m_nPoints = 0;
88 m_nBuffer = 0;
89
90 if( m_Points ) { SG_Free(m_Points); m_Points = NULL; }
91 if( m_Z ) { SG_Free(m_Z ); m_Z = NULL; }
92 if( m_M ) { SG_Free(m_M ); m_M = NULL; }
93
94 m_bUpdate = true;
95
97
98 return( true );
99}
100
101
103// //
105
106//---------------------------------------------------------
107#define GET_GROW_SIZE(n) (n < 128 ? 1 : (n < 2048 ? 32 : 256))
108
109//---------------------------------------------------------
111{
112 if( m_nPoints == nPoints )
113 {
114 return( true );
115 }
116
117 int nGrow = GET_GROW_SIZE(nPoints);
118 int nBuffer = (nPoints / nGrow) * nGrow;
119
120 while( nBuffer < nPoints )
121 {
122 nBuffer += nGrow;
123 }
124
125 if( m_nBuffer == nBuffer )
126 {
127 return( true );
128 }
129
130 m_nBuffer = nBuffer;
131
132 //-----------------------------------------------------
133 TSG_Point *Points = (TSG_Point *)SG_Realloc(m_Points, m_nBuffer * sizeof(TSG_Point));
134
135 if( Points == NULL )
136 {
137 return( false );
138 }
139
140 m_Points = Points;
141
142 //-----------------------------------------------------
143 if( m_Z || ((CSG_Shapes *)m_pOwner->Get_Table())->Get_Vertex_Type() != SG_VERTEX_TYPE_XY )
144 {
145 double *Z = (double *)SG_Realloc(m_Z, m_nBuffer * sizeof(double));
146
147 if( !Z )
148 {
149 return( false );
150 }
151
152 m_Z = Z;
153 }
154
155 //-----------------------------------------------------
156 if( m_M || ((CSG_Shapes *)m_pOwner->Get_Table())->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM )
157 {
158 double *M = (double *)SG_Realloc(m_M, m_nBuffer * sizeof(double));
159
160 if( !M )
161 {
162 return( false );
163 }
164
165 m_M = M;
166 }
167
168 return( true );
169}
170
171
173// //
175
176//---------------------------------------------------------
178{
179 if( _Alloc_Memory(pPart->m_nPoints) )
180 {
181 if( m_pOwner ) { m_pOwner->m_nPoints += pPart->m_nPoints - m_nPoints; }
182
183 m_nPoints = pPart->m_nPoints;
184
185 memcpy(m_Points, pPart->m_Points, m_nPoints * sizeof(TSG_Point));
186
187 m_Extent = pPart->m_Extent;
188
189 if( m_Z && pPart->m_Z )
190 {
191 memcpy(m_Z, pPart->m_Z, m_nPoints * sizeof(double));
192
193 m_ZMin = pPart->m_ZMin;
194 m_ZMax = pPart->m_ZMax;
195 }
196
197 if( m_M && pPart->m_M )
198 {
199 memcpy(m_M, pPart->m_M, m_nPoints * sizeof(double));
200
201 m_MMin = pPart->m_MMin;
202 m_MMax = pPart->m_MMax;
203 }
204
205 m_bUpdate = pPart->m_bUpdate;
206
207 if( m_pOwner )
208 {
209 m_pOwner->_Invalidate();
210 }
211
212 return( true );
213 }
214
215 return( false );
216}
217
218
220// //
222
223//---------------------------------------------------------
224int CSG_Shape_Part::Ins_Point(double x, double y, int iPoint)
225{
226 if( iPoint >= 0 && iPoint <= m_nPoints && _Alloc_Memory(m_nPoints + 1) )
227 {
228 for(int i=m_nPoints; i>iPoint; i--)
229 {
230 m_Points[i] = m_Points[i - 1];
231
232 if( m_Z ) { m_Z[i] = m_Z[i - 1]; }
233 if( m_M ) { m_M[i] = m_M[i - 1]; }
234 }
235
236 m_nPoints++; if( m_pOwner ) { m_pOwner->m_nPoints++; }
237
238 m_Points[iPoint].x = x;
239 m_Points[iPoint].y = y;
240
241 if( m_Z ) { m_Z[iPoint] = 0.; }
242 if( m_M ) { m_M[iPoint] = 0.; }
243
244 _Invalidate();
245
246 return( 1 );
247 }
248
249 return( 0 );
250}
251
253{
254 if( Ins_Point(p.x, p.y, iPoint) )
255 {
256 Set_Z(p.z, iPoint);
257
258 return( 1 );
259 }
260
261 return( 0 );
262}
263
265{
266 if( Ins_Point(p.x, p.y, iPoint) )
267 {
268 Set_Z(p.z, iPoint);
269 Set_M(p.m, iPoint);
270
271 return( 1 );
272 }
273
274 return( 0 );
275}
276
277//---------------------------------------------------------
278int CSG_Shape_Part::Set_Point(double x, double y, int iPoint)
279{
280 if( iPoint >= 0 && iPoint < m_nPoints )
281 {
282 m_Points[iPoint].x = x;
283 m_Points[iPoint].y = y;
284
285 _Invalidate();
286
287 return( 1 );
288 }
289
290 return( 0 );
291}
292
294{
295 if( Set_Point(p.x, p.y, iPoint) )
296 {
297 Set_Z(p.z, iPoint);
298
299 return( 1 );
300 }
301
302 return( 0 );
303}
304
306{
307 if( Set_Point(p.x, p.y, iPoint) )
308 {
309 Set_Z(p.z, iPoint);
310 Set_M(p.m, iPoint);
311
312 return( 1 );
313 }
314
315 return( 0 );
316}
317
318//---------------------------------------------------------
320{
321 if( iPoint >= 0 && iPoint < m_nPoints )
322 {
323 m_nPoints--; if( m_pOwner ) { m_pOwner->m_nPoints--; }
324
325 for(int i=iPoint; i<m_nPoints; i++)
326 {
327 m_Points[i] = m_Points[i + 1];
328
329 if( m_Z )
330 {
331 m_Z[i] = m_Z[i + 1];
332
333 if( m_M )
334 {
335 m_M[i] = m_M[i + 1];
336 }
337 }
338 }
339
341
342 _Invalidate();
343
344 return( 1 );
345 }
346
347 return( 0 );
348}
349
350
352// //
354
355//---------------------------------------------------------
356bool CSG_Shape_Part::Add_Points(CSG_Shape_Part *pPoints, bool bAscending)
357{
358 if( pPoints && pPoints->Get_Count() > 0 )
359 {
360 for(int i=0; i<pPoints->Get_Count(); i++)
361 {
362 Add_Point(pPoints->Get_Point_ZM(i, bAscending));
363 }
364
365 return( true );
366 }
367
368 return( false );
369}
370
371//---------------------------------------------------------
373{
374 for(int i=0, j=m_nPoints-1; i<j; i++, j--)
375 {
376 TSG_Point p = m_Points[i]; m_Points[i] = m_Points[j]; m_Points[j] = p;
377
378 if( m_Z ) { double z = m_Z[i]; m_Z[i] = m_Z[j]; m_Z[j] = z; }
379 if( m_M ) { double m = m_M[i]; m_M[i] = m_M[j]; m_M[j] = m; }
380 }
381
382 return( true );
383}
384
385
387// //
389
390//---------------------------------------------------------
392{
393 m_bUpdate = true;
394
395 if( m_pOwner )
396 {
397 m_pOwner->_Invalidate();
398 }
399}
400
401//---------------------------------------------------------
403{
404 if( m_bUpdate )
405 {
406 CSG_Simple_Statistics x, y, z, m;
407
408 for(int i=0; i<m_nPoints; i++)
409 {
410 x.Add_Value(m_Points[i].x);
411 y.Add_Value(m_Points[i].y);
412
413 if( m_Z ) { z.Add_Value(m_Z[i]); }
414 if( m_M ) { m.Add_Value(m_M[i]); }
415 }
416
417 m_Extent.Assign(
418 x.Get_Minimum(), y.Get_Minimum(),
419 x.Get_Maximum(), y.Get_Maximum()
420 );
421
424
425 m_bUpdate = false;
426 }
427}
428
429
431// //
432// //
433// //
435
436//---------------------------------------------------------
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
int Add_Point(double x, double y)
Definition shapes.h:396
int Ins_Point(double x, double y, int iPoint)
double m_ZMin
Definition shapes.h:459
int Set_Point(double x, double y, int iPoint)
CSG_Rect m_Extent
Definition shapes.h:463
bool m_bUpdate
Definition shapes.h:455
friend class CSG_Shape_Points
Definition shapes.h:370
double m_MMax
Definition shapes.h:459
virtual bool Destroy(void)
bool Revert_Points(void)
double m_MMin
Definition shapes.h:459
virtual ~CSG_Shape_Part(void)
virtual void _Update_Extent(void)
class CSG_Shape_Points * m_pOwner
Definition shapes.h:465
TSG_Point * m_Points
Definition shapes.h:461
void Set_Z(double z, int iPoint)
Definition shapes.h:413
CSG_Shape_Part(class CSG_Shape_Points *pOwner)
virtual bool Assign(CSG_Shape_Part *pPart)
double m_ZMax
Definition shapes.h:459
void Set_M(double m, int iPoint)
Definition shapes.h:428
int Del_Point(int iPoint)
int Get_Count(void) const
Definition shapes.h:383
bool Add_Points(CSG_Shape_Part *pPoints, bool bAscending=true)
virtual void _Invalidate(void)
virtual bool _Alloc_Memory(int nPoints)
double * m_M
Definition shapes.h:459
TSG_Point_4D Get_Point_ZM(int iPoint, bool bAscending=true) const
Definition shapes.h:432
double * m_Z
Definition shapes.h:459
double Get_Maximum(void)
Definition mat_tools.h:749
void Add_Value(double Value, double Weight=1.)
double Get_Minimum(void)
Definition mat_tools.h:748
struct SSG_Point TSG_Point
#define GET_GROW_SIZE(n)
@ SG_VERTEX_TYPE_XYZM
Definition shapes.h:94
@ SG_VERTEX_TYPE_XY
Definition shapes.h:92