SAGA API v9.10
Loading...
Searching...
No Matches
tin.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// tin.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 "tin.h"
54
55
57// //
58// //
59// //
61
62//---------------------------------------------------------
64{
65 return( new CSG_TIN );
66}
67
68//---------------------------------------------------------
70{
71 return( new CSG_TIN(TIN) );
72}
73
74//---------------------------------------------------------
75CSG_TIN * SG_Create_TIN(const char *File) { return( SG_Create_TIN(CSG_String(File)) ); }
76CSG_TIN * SG_Create_TIN(const wchar_t *File) { return( SG_Create_TIN(CSG_String(File)) ); }
78{
79 CSG_TIN *pTIN = new CSG_TIN();
80
81 if( pTIN->Create(File) )
82 {
83 return( pTIN );
84 }
85
86 delete(pTIN); return( NULL );
87}
88
89//---------------------------------------------------------
91{
92 return( new CSG_TIN(pShapes) );
93}
94
95
97// //
98// //
99// //
101
102//---------------------------------------------------------
104 : CSG_Table()
105{
107}
108
109//---------------------------------------------------------
111 : CSG_Table()
112{
113 _On_Construction(); Create(TIN);
114}
115
116//---------------------------------------------------------
117CSG_TIN::CSG_TIN(const char *File) : CSG_TIN(CSG_String(File)) {}
118CSG_TIN::CSG_TIN(const wchar_t *File) : CSG_TIN(CSG_String(File)) {}
120 : CSG_Table()
121{
122 _On_Construction(); Create(File);
123}
124
125//---------------------------------------------------------
127 : CSG_Table()
128{
129 _On_Construction(); Create(pShapes);
130}
131
132
134// //
136
137//---------------------------------------------------------
139{
141
142 m_Edges = NULL; m_nEdges = 0;
143 m_Triangles = NULL; m_nTriangles = 0;
144}
145
146
148// //
150
151//---------------------------------------------------------
152bool CSG_TIN::Create(const CSG_TIN &TIN)
153{
154 return( Assign((CSG_Data_Object *)&TIN) );
155}
156
157//---------------------------------------------------------
158bool CSG_TIN::Create(const char *File) { return( Create(CSG_String(File)) ); }
159bool CSG_TIN::Create(const wchar_t *File) { return( Create(CSG_String(File)) ); }
161{
162 CSG_Shapes Shapes(File);
163
164 if( Create(&Shapes) )
165 {
166 Get_History().Add_Child(_TL("Created from file"), File);
168
169 Set_File_Name(File, true);
170 Set_Modified(false);
172
173 return( true );
174 }
175
176 return( false );
177}
178
179//---------------------------------------------------------
181{
182 Destroy();
183
184 if( pShapes && pShapes->is_Valid() )
185 {
186 SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Create TIN from shapes"), pShapes->Get_Name()), true);
187
188 CSG_Table::Create(pShapes);
189
190 Set_Name(pShapes->Get_Name());
191
192 //-------------------------------------------------
193 for(sLong iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++)
194 {
195 CSG_Shape *pShape = pShapes->Get_Shape(iShape);
196
197 for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
198 {
199 for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
200 {
201 Add_Node(pShape->Get_Point(iPoint, iPart), pShape, false);
202 }
203 }
204 }
205
207
208 m_bTriangulate = true;
209
210 if( Update() )
211 {
213
214 return( true );
215 }
216 }
217
218 SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
219
220 return( false );
221}
222
223
225// //
227
228//---------------------------------------------------------
230{
231 Destroy();
232}
233
234//---------------------------------------------------------
236{
240
242
243 return( true );
244}
245
246
248// //
250
251//---------------------------------------------------------
253{
254 return( Del_Records() );
255}
256
257//---------------------------------------------------------
259{
260 if( m_nEdges > 0 )
261 {
262 for(sLong i=0; i<m_nEdges; i++)
263 {
264 delete(m_Edges[i]);
265 }
266
267 SG_Free(m_Edges); m_Edges = NULL; m_nEdges = 0;
268 }
269
270 return( true );
271}
272
273//---------------------------------------------------------
275{
276 if( m_nTriangles > 0 )
277 {
278 for(sLong i=0; i<m_nTriangles; i++)
279 {
280 delete(m_Triangles[i]);
281 }
282
284 }
285
286 return( true );
287}
288
289
291// //
293
294//---------------------------------------------------------
295bool CSG_TIN::Assign(CSG_Data_Object *pObject, bool bProgress)
296{
297 if( pObject && pObject->asTIN() && pObject->is_Valid() )
298 {
299 CSG_TIN *pTIN = pObject->asTIN();
300
301 Destroy();
302
303 CSG_Table::Create(pTIN);
304
305 Set_Name(pTIN->Get_Name());
306
307 Get_History() = pTIN->Get_History();
308
309 //-------------------------------------------------
310 for(sLong iNode=0; iNode<pTIN->Get_Node_Count(); iNode++)
311 {
312 CSG_TIN_Node *pNode = pTIN->Get_Node(iNode);
313
314 Add_Node(pNode->Get_Point(), pNode, false);
315 }
316
317 //-------------------------------------------------
318 for(sLong iTriangle=0; iTriangle<pTIN->Get_Triangle_Count(); iTriangle++)
319 {
320 CSG_TIN_Triangle *pTriangle = pTIN->Get_Triangle(iTriangle);
321
323 Get_Node(pTriangle->Get_Node(0)->Get_Index()),
324 Get_Node(pTriangle->Get_Node(1)->Get_Index()),
325 Get_Node(pTriangle->Get_Node(2)->Get_Index())
326 );
327 }
328
329 m_bTriangulate = false;
330 Update();
332
333 return( true );
334 }
335
336 return( false );
337}
338
339
341// //
343
344//---------------------------------------------------------
345bool CSG_TIN::Triangulate(bool bOn, bool bUpdate)
346{
347 if( m_bTriangulate != bOn )
348 {
349 m_bTriangulate = bOn;
350 }
351
352 if( bUpdate )
353 {
354 Update();
355 }
356
357 return( true );
358}
359
360
362// //
364
365//---------------------------------------------------------
366bool CSG_TIN::Save(const CSG_String &File, int Format)
367{
368 if( File.is_Empty() )
369 {
370 return( *Get_File_Name(false) ? Save(Get_File_Name(false), Format) : false );
371 }
372
373 bool bResult = false;
374
375 if( Get_Triangle_Count() > 0 )
376 {
377 switch( Format )
378 {
379 case 0: default: {
380 CSG_Shapes Points(SHAPE_TYPE_Point, Get_Name(), this);
381
382 for(sLong i=0; i<Get_Node_Count(); i++)
383 {
384 Points.Add_Shape(Get_Node(i))->Add_Point(Get_Node(i)->Get_Point());
385 }
386
387 bResult = Points.Save(File);
388
389 break; }
390 }
391 }
392
393 if( bResult )
394 {
395 Set_Modified(false);
396
397 Set_File_Name(File, true);
398 }
399
400 return( bResult );
401}
402
403
405// //
407
408//---------------------------------------------------------
410{
411 return( new CSG_TIN_Node(this, Index) );
412}
413
414//---------------------------------------------------------
415CSG_TIN_Node * CSG_TIN::Add_Node(const TSG_Point &Point, CSG_Table_Record *pRecord, bool bUpdateNow)
416{
417 CSG_TIN_Node *pNode = (CSG_TIN_Node *)Add_Record(pRecord);
418
419 if( pNode )
420 {
421 pNode->m_Point = Point;
422
423 if( bUpdateNow )
424 {
425 Update();
426 }
427 }
428
429 return( pNode );
430}
431
432//---------------------------------------------------------
433bool CSG_TIN::Del_Node(sLong Index, bool bUpdateNow)
434{
435 if( Del_Record(Index) )
436 {
437 if( bUpdateNow )
438 {
439 Update();
440 }
441
442 return( true );
443 }
444
445 return( false );
446}
447
448
450// //
452
453//---------------------------------------------------------
458
460{
461 return( Add_Triangle(p[0], p[1], p[2]) );
462}
463
464
466// //
468
469//---------------------------------------------------------
471{
472 CSG_TIN_Edge *pEdge = new CSG_TIN_Edge(a, b);
473
475 m_Edges[m_nEdges++] = pEdge;
476
477 return( pEdge );
478}
479
480//---------------------------------------------------------
482{
483 CSG_TIN_Triangle *pTriangle = new CSG_TIN_Triangle(a, b, c);
484
486 m_Triangles[m_nTriangles++] = pTriangle;
487
488 if( a->_Add_Neighbor(b) )
489 {
490 b->_Add_Neighbor(a);
491 _Add_Edge(a, b);
492 }
493
494 if( b->_Add_Neighbor(c) )
495 {
496 c->_Add_Neighbor(b);
497 _Add_Edge(b, c);
498 }
499
500 if( c->_Add_Neighbor(a) )
501 {
502 a->_Add_Neighbor(c);
503 _Add_Edge(c, a);
504 }
505
506 a->_Add_Triangle(pTriangle);
507 b->_Add_Triangle(pTriangle);
508 c->_Add_Triangle(pTriangle);
509
510 return( pTriangle );
511}
512
513
515// //
517
518//---------------------------------------------------------
520{
521 if( m_bTriangulate )
522 {
523 return( _Triangulate() );
524 }
525
526 if( Get_Node_Count() >= 3 ) // Update extent...
527 {
528 m_Extent.Assign(Get_Node(0)->Get_Point(), Get_Node(0)->Get_Point());
529
530 for(int i=1; i<Get_Node_Count(); i++)
531 {
532 m_Extent.Union(Get_Node(i)->Get_Point());
533 }
534
535 return( true );
536 }
537
538 m_Extent.Assign(0., 0., 0., 0.);
539
540 return( false );
541}
542
543
545// //
547
548//---------------------------------------------------------
550{
551 return( Create(Get_File_Name(false)) );
552}
553
554//---------------------------------------------------------
556{
557 return( false );
558}
559
560
562// //
563// //
564// //
566
567//---------------------------------------------------------
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
bool SG_UI_Process_Set_Ready(void)
bool SG_UI_Process_Set_Progress(int Position, int Range)
@ SG_UI_MSG_STYLE_FAILURE
Definition api_core.h:1585
@ SG_UI_MSG_STYLE_SUCCESS
Definition api_core.h:1584
signed long long sLong
Definition api_core.h:158
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
#define _TL(s)
Definition api_core.h:1568
const SG_Char * Get_Name(void) const
void Set_Name(const CSG_String &Name)
virtual bool is_Valid(void) const =0
void Set_Update_Flag(bool bOn=true)
Definition dataobject.h:285
const SG_Char * Get_File_Name(bool bNative=true) const
bool Update(bool bForce=false)
class CSG_TIN * asTIN(bool bPolymorph=false) const
CSG_MetaData & Get_History(void)
Definition dataobject.h:236
void Set_File_Name(const CSG_String &FileName)
bool Add_Children(const CSG_MetaData &MetaData)
Definition metadata.cpp:359
CSG_MetaData * Add_Child(void)
Definition metadata.cpp:166
virtual int Get_Point_Count(void) const =0
virtual int Add_Point(double x, double y, int iPart=0)=0
virtual int Get_Part_Count(void) const =0
virtual TSG_Point Get_Point(int iPoint=0) const =0
virtual bool is_Valid(void) const
Definition shapes.h:804
virtual CSG_Shape * Add_Shape(CSG_Table_Record *pCopy=NULL, TSG_ADD_Shape_Copy_Mode mCopy=SHAPE_COPY)
Definition shapes.cpp:402
virtual bool Save(const CSG_String &File, int Format=0)
virtual CSG_Shape * Get_Shape(const CSG_Point &Point, double Epsilon=0.)
Definition shapes.cpp:483
static CSG_String Format(const char *Format,...)
bool is_Empty(void) const
const TSG_Point & Get_Point(void)
Definition tin.h:100
CSG_TIN_Node * Get_Node(int iNode)
Definition tin.h:178
Definition tin.h:222
virtual ~CSG_TIN(void)
Definition tin.cpp:229
void _On_Construction(void)
Definition tin.cpp:138
virtual bool Save(const CSG_String &File, int Format=0)
Definition tin.cpp:366
virtual bool On_Reload(void)
Definition tin.cpp:549
CSG_TIN_Triangle * Get_Triangle(sLong Index) const
Definition tin.h:269
CSG_TIN_Triangle * _Add_Triangle(CSG_TIN_Node *a, CSG_TIN_Node *b, CSG_TIN_Node *c)
Definition tin.cpp:481
CSG_TIN_Triangle ** m_Triangles
Definition tin.h:299
CSG_Rect m_Extent
Definition tin.h:295
CSG_TIN_Triangle * Add_Triangle(CSG_TIN_Node *p0, CSG_TIN_Node *p1, CSG_TIN_Node *p2)
Definition tin.cpp:454
sLong Get_Node_Count(void) const
Definition tin.h:262
bool Del_Node(sLong Index, bool bUpdateNow)
Definition tin.cpp:433
CSG_TIN_Node * Get_Node(sLong Index) const
Definition tin.h:263
CSG_TIN_Edge * _Add_Edge(CSG_TIN_Node *a, CSG_TIN_Node *b)
Definition tin.cpp:470
CSG_TIN_Edge ** m_Edges
Definition tin.h:297
virtual bool On_Update(void)
Definition tin.cpp:519
bool _Destroy_Edges(void)
Definition tin.cpp:258
virtual CSG_Table_Record * _Get_New_Record(sLong Index)
Definition tin.cpp:409
bool _Destroy_Triangles(void)
Definition tin.cpp:274
bool _Triangulate(void)
CSG_TIN(void)
Definition tin.cpp:103
sLong m_nTriangles
Definition tin.h:293
virtual bool Assign(CSG_Data_Object *pObject, bool bProgress=false)
Definition tin.cpp:295
virtual bool Destroy(void)
Definition tin.cpp:235
CSG_TIN_Node * Add_Node(const TSG_Point &Point, CSG_Table_Record *pRecord=NULL, bool bUpdateNow=false)
Definition tin.cpp:415
virtual bool On_Delete(void)
Definition tin.cpp:555
bool Create(const CSG_TIN &TIN)
Definition tin.cpp:152
sLong m_nEdges
Definition tin.h:293
bool _Destroy_Nodes(void)
Definition tin.cpp:252
bool m_bTriangulate
Definition tin.h:291
sLong Get_Triangle_Count(void) const
Definition tin.h:268
bool Triangulate(bool bOn=true, bool bUpdate=false)
Definition tin.cpp:345
sLong Get_Index(void) const
Definition table.h:136
bool Create(void)
Definition table.cpp:153
sLong Get_Count(void) const
Definition table.h:400
virtual bool Destroy(void)
Definition table.cpp:314
virtual bool Del_Records(void)
Definition table.cpp:936
CSG_Table(void)
Definition table.cpp:147
friend class CSG_Table_Record
Definition table.h:286
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition table.cpp:823
virtual bool Del_Record(sLong Index)
Definition table.cpp:897
virtual void Set_Modified(bool bModified=true)
Definition table.cpp:1141
virtual void _On_Construction(void)
Definition table.cpp:295
struct SSG_Point TSG_Point
@ SHAPE_TYPE_Point
Definition shapes.h:102
CSG_TIN * SG_Create_TIN(void)
Definition tin.cpp:63