SAGA API  v9.5
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 //---------------------------------------------------------
78 {
79  Destroy();
80 }
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 
96  _Invalidate();
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  {
210  }
211 
212  return( true );
213  }
214 
215  return( false );
216 }
217 
218 
220 // //
222 
223 //---------------------------------------------------------
224 int 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 
252 int CSG_Shape_Part::Ins_Point(const CSG_Point_3D &p, int iPoint)
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 
264 int CSG_Shape_Part::Ins_Point(const CSG_Point_4D &p, int iPoint)
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 //---------------------------------------------------------
278 int 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 
293 int CSG_Shape_Part::Set_Point(const CSG_Point_3D &p, int iPoint)
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 
305 int CSG_Shape_Part::Set_Point(const CSG_Point_4D &p, int iPoint)
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 //---------------------------------------------------------
356 bool 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  {
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 
418  x.Get_Minimum(), y.Get_Minimum(),
419  x.Get_Maximum(), y.Get_Maximum()
420  );
421 
422  m_ZMin = z.Get_Minimum(); m_ZMax = z.Get_Maximum();
423  m_MMin = m.Get_Minimum(); m_MMax = m.Get_Maximum();
424 
425  m_bUpdate = false;
426  }
427 }
428 
429 
431 // //
432 // //
433 // //
435 
436 //---------------------------------------------------------
CSG_Rect::Assign
CSG_Rect & Assign(double xMin, double yMin, double xMax, double yMax)
Definition: geo_classes.cpp:727
CSG_Shape_Part::m_pOwner
class CSG_Shape_Points * m_pOwner
Definition: shapes.h:468
CSG_Shape_Part::m_MMax
double m_MMax
Definition: shapes.h:462
GET_GROW_SIZE
#define GET_GROW_SIZE(n)
Definition: shape_part.cpp:107
CSG_Shape_Part::Revert_Points
bool Revert_Points(void)
Definition: shape_part.cpp:372
CSG_Shape_Part::Destroy
virtual bool Destroy(void)
Definition: shape_part.cpp:83
CSG_Shape_Points
Definition: shapes.h:486
SSG_Point_3D::x
double x
Definition: geo_tools.h:265
SG_Free
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
Definition: api_memory.cpp:83
CSG_Shape_Part::_Invalidate
virtual void _Invalidate(void)
Definition: shape_part.cpp:391
SSG_Point
Definition: geo_tools.h:128
CSG_Shape_Part::m_nBuffer
int m_nBuffer
Definition: shapes.h:460
CSG_Table_Record::Get_Table
class CSG_Table * Get_Table(void)
Definition: table.h:135
CSG_Shape_Part::CSG_Shape_Part
CSG_Shape_Part(class CSG_Shape_Points *pOwner)
Definition: shape_part.cpp:63
CSG_Shape_Part::Assign
virtual bool Assign(CSG_Shape_Part *pPart)
Definition: shape_part.cpp:177
CSG_Shape_Part::Ins_Point
int Ins_Point(double x, double y, int iPoint)
Definition: shape_part.cpp:224
SG_VERTEX_TYPE_XY
@ SG_VERTEX_TYPE_XY
Definition: shapes.h:92
CSG_Shape_Part::~CSG_Shape_Part
virtual ~CSG_Shape_Part(void)
Definition: shape_part.cpp:77
CSG_Simple_Statistics::Get_Maximum
double Get_Maximum(void)
Definition: mat_tools.h:747
CSG_Shape_Part::m_ZMin
double m_ZMin
Definition: shapes.h:462
CSG_Shape_Part::m_Z
double * m_Z
Definition: shapes.h:462
SG_VERTEX_TYPE_XYZM
@ SG_VERTEX_TYPE_XYZM
Definition: shapes.h:94
SSG_Point_4D::m
double m
Definition: geo_tools.h:358
CSG_Simple_Statistics::Get_Minimum
double Get_Minimum(void)
Definition: mat_tools.h:746
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_Point_4D
Definition: geo_tools.h:364
CSG_Shape_Part::m_nPoints
int m_nPoints
Definition: shapes.h:460
CSG_Shape_Part::Set_M
void Set_M(double m, int iPoint)
Definition: shapes.h:431
CSG_Shape_Part::m_M
double * m_M
Definition: shapes.h:462
CSG_Shape_Part::Set_Z
void Set_Z(double z, int iPoint)
Definition: shapes.h:416
CSG_Shape_Part::m_Extent
CSG_Rect m_Extent
Definition: shapes.h:466
CSG_Shape_Part::_Update_Extent
virtual void _Update_Extent(void)
Definition: shape_part.cpp:402
CSG_Shape_Part::m_ZMax
double m_ZMax
Definition: shapes.h:462
CSG_Shape_Part::m_MMin
double m_MMin
Definition: shapes.h:462
SSG_Point_4D::y
double y
Definition: geo_tools.h:358
CSG_Shape_Points::_Invalidate
virtual void _Invalidate(void)
Definition: shapes.h:576
shapes.h
CSG_Simple_Statistics::Add_Value
void Add_Value(double Value, double Weight=1.)
Definition: mat_tools.cpp:527
CSG_Shape_Part::Add_Point
int Add_Point(double x, double y)
Definition: shapes.h:399
CSG_Shape_Part::Del_Point
int Del_Point(int iPoint)
Definition: shape_part.cpp:319
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
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_Part::Set_Point
int Set_Point(double x, double y, int iPoint)
Definition: shape_part.cpp:278
SSG_Point_3D::z
double z
Definition: geo_tools.h:265
CSG_Shape_Part::_Alloc_Memory
virtual bool _Alloc_Memory(int nPoints)
Definition: shape_part.cpp:110
CSG_Shapes
Definition: shapes.h:775
CSG_Shape_Part::Add_Points
bool Add_Points(CSG_Shape_Part *pPoints, bool bAscending=true)
Definition: shape_part.cpp:356
CSG_Shape_Points::m_nPoints
int m_nPoints
Definition: shapes.h:563
CSG_Shape_Part::Get_Point_ZM
TSG_Point_4D Get_Point_ZM(int iPoint, bool bAscending=true) const
Definition: shapes.h:435
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
CSG_Shape_Part::m_bUpdate
bool m_bUpdate
Definition: shapes.h:458
CSG_Simple_Statistics
Definition: mat_tools.h:723
CSG_Shape_Part
Definition: shapes.h:372