SAGA API  v9.6
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 //---------------------------------------------------------
75 CSG_TIN * SG_Create_TIN(const char *File) { return( SG_Create_TIN(CSG_String(File)) ); }
76 CSG_TIN * SG_Create_TIN(const wchar_t *File) { return( SG_Create_TIN(CSG_String(File)) ); }
78 {
79  CSG_TIN *pTIN = new CSG_TIN(File);
80 
81  if( !pTIN->is_Valid() ) { delete(pTIN); return( NULL ); } return( pTIN );
82 }
83 
84 //---------------------------------------------------------
86 {
87  return( new CSG_TIN(pShapes) );
88 }
89 
90 
92 // //
93 // //
94 // //
96 
97 //---------------------------------------------------------
99  : CSG_Table()
100 {
102 }
103 
104 //---------------------------------------------------------
106  : CSG_Table()
107 {
108  _On_Construction(); Create(TIN);
109 }
110 
111 //---------------------------------------------------------
112 CSG_TIN::CSG_TIN(const char *File) : CSG_TIN(CSG_String(File)) {}
113 CSG_TIN::CSG_TIN(const wchar_t *File) : CSG_TIN(CSG_String(File)) {}
115  : CSG_Table()
116 {
117  _On_Construction(); Create(File);
118 }
119 
120 //---------------------------------------------------------
122  : CSG_Table()
123 {
124  _On_Construction(); Create(pShapes);
125 }
126 
127 
129 // //
131 
132 //---------------------------------------------------------
134 {
136 
137  m_Edges = NULL; m_nEdges = 0;
138  m_Triangles = NULL; m_nTriangles = 0;
139 }
140 
141 
143 // //
145 
146 //---------------------------------------------------------
147 bool CSG_TIN::Create(const CSG_TIN &TIN)
148 {
149  return( Assign((CSG_Data_Object *)&TIN) );
150 }
151 
152 //---------------------------------------------------------
153 bool CSG_TIN::Create(const char *File) { return( Create(CSG_String(File)) ); }
154 bool CSG_TIN::Create(const wchar_t *File) { return( Create(CSG_String(File)) ); }
155 bool CSG_TIN::Create(const CSG_String &File)
156 {
157  CSG_Shapes Shapes(File);
158 
159  if( Create(&Shapes) )
160  {
161  Get_History().Add_Child(_TL("Created from file"), File);
163 
164  Set_File_Name(File, true);
165  Set_Modified(false);
166  Set_Update_Flag();
167 
168  return( true );
169  }
170 
171  return( false );
172 }
173 
174 //---------------------------------------------------------
176 {
177  Destroy();
178 
179  if( pShapes && pShapes->is_Valid() )
180  {
181  SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Create TIN from shapes"), pShapes->Get_Name()), true);
182 
183  CSG_Table::Create(pShapes);
184 
185  Set_Name(pShapes->Get_Name());
186 
187  //-------------------------------------------------
188  for(sLong iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++)
189  {
190  CSG_Shape *pShape = pShapes->Get_Shape(iShape);
191 
192  for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
193  {
194  for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
195  {
196  Add_Node(pShape->Get_Point(iPoint, iPart), pShape, false);
197  }
198  }
199  }
200 
202 
203  m_bTriangulate = true;
204 
205  if( Update() )
206  {
207  SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS);
208 
209  return( true );
210  }
211  }
212 
213  SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
214 
215  return( false );
216 }
217 
218 
220 // //
222 
223 //---------------------------------------------------------
225 {
226  Destroy();
227 }
228 
229 //---------------------------------------------------------
231 {
233  _Destroy_Edges();
234  _Destroy_Nodes();
235 
237 
238  return( true );
239 }
240 
241 
243 // //
245 
246 //---------------------------------------------------------
248 {
249  return( Del_Records() );
250 }
251 
252 //---------------------------------------------------------
254 {
255  if( m_nEdges > 0 )
256  {
257  for(sLong i=0; i<m_nEdges; i++)
258  {
259  delete(m_Edges[i]);
260  }
261 
262  SG_Free(m_Edges); m_Edges = NULL; m_nEdges = 0;
263  }
264 
265  return( true );
266 }
267 
268 //---------------------------------------------------------
270 {
271  if( m_nTriangles > 0 )
272  {
273  for(sLong i=0; i<m_nTriangles; i++)
274  {
275  delete(m_Triangles[i]);
276  }
277 
279  }
280 
281  return( true );
282 }
283 
284 
286 // //
288 
289 //---------------------------------------------------------
291 {
292  if( pObject && pObject->is_Valid() && pObject->Get_ObjectType() == Get_ObjectType() )
293  {
294  CSG_TIN *pTIN = (CSG_TIN *)pObject;
295 
296  Destroy();
297 
298  CSG_Table::Create(pTIN);
299 
300  Set_Name(pTIN->Get_Name());
301 
302  Get_History() = pTIN->Get_History();
303 
304  //-------------------------------------------------
305  for(sLong iNode=0; iNode<pTIN->Get_Node_Count(); iNode++)
306  {
307  CSG_TIN_Node *pNode = pTIN->Get_Node(iNode);
308 
309  Add_Node(pNode->Get_Point(), pNode, false);
310  }
311 
312  //-------------------------------------------------
313  for(sLong iTriangle=0; iTriangle<pTIN->Get_Triangle_Count(); iTriangle++)
314  {
315  CSG_TIN_Triangle *pTriangle = pTIN->Get_Triangle(iTriangle);
316 
318  Get_Node(pTriangle->Get_Node(0)->Get_Index()),
319  Get_Node(pTriangle->Get_Node(1)->Get_Index()),
320  Get_Node(pTriangle->Get_Node(2)->Get_Index())
321  );
322  }
323 
324  m_bTriangulate = false;
325  Update();
327 
328  return( true );
329  }
330 
331  return( false );
332 }
333 
334 
336 // //
338 
339 //---------------------------------------------------------
340 bool CSG_TIN::Triangulate(bool bOn, bool bUpdate)
341 {
342  if( m_bTriangulate != bOn )
343  {
344  m_bTriangulate = bOn;
345  }
346 
347  if( bUpdate )
348  {
349  Update();
350  }
351 
352  return( true );
353 }
354 
355 
357 // //
359 
360 //---------------------------------------------------------
361 bool CSG_TIN::Save(const CSG_String &File, int Format)
362 {
363  bool bResult = false;
364 
365  if( Get_Triangle_Count() > 0 )
366  {
367  switch( Format )
368  {
369  case 0: default:
370  {
371  CSG_Shapes Points;
372 
373  Points.Create(SHAPE_TYPE_Point, Get_Name(), this);
374 
375  for(sLong i=0; i<Get_Node_Count(); i++)
376  {
377  CSG_TIN_Node *pNode = Get_Node(i);
378 
379  Points.Add_Shape(pNode)->Add_Point(pNode->Get_Point());
380  }
381 
382  bResult = Points.Save(File);
383  }
384  break;
385  }
386  }
387 
388  if( bResult )
389  {
390  Set_Modified(false);
391 
392  Set_File_Name(File, true);
393  }
394 
395  return( bResult );
396 }
397 
398 
400 // //
402 
403 //---------------------------------------------------------
405 {
406  return( new CSG_TIN_Node(this, Index) );
407 }
408 
409 //---------------------------------------------------------
410 CSG_TIN_Node * CSG_TIN::Add_Node(const TSG_Point &Point, CSG_Table_Record *pRecord, bool bUpdateNow)
411 {
412  CSG_TIN_Node *pNode = (CSG_TIN_Node *)Add_Record(pRecord);
413 
414  if( pNode )
415  {
416  pNode->m_Point = Point;
417 
418  if( bUpdateNow )
419  {
420  Update();
421  }
422  }
423 
424  return( pNode );
425 }
426 
427 //---------------------------------------------------------
428 bool CSG_TIN::Del_Node(sLong Index, bool bUpdateNow)
429 {
430  if( Del_Record(Index) )
431  {
432  if( bUpdateNow )
433  {
434  Update();
435  }
436 
437  return( true );
438  }
439 
440  return( false );
441 }
442 
443 
445 // //
447 
448 //---------------------------------------------------------
450 {
451  return( _Add_Triangle((CSG_TIN_Node *)p0, (CSG_TIN_Node *)p1, (CSG_TIN_Node *)p2) );
452 }
453 
455 {
456  return( Add_Triangle(p[0], p[1], p[2]) );
457 }
458 
459 
461 // //
463 
464 //---------------------------------------------------------
466 {
467  CSG_TIN_Edge *pEdge = new CSG_TIN_Edge(a, b);
468 
469  m_Edges = (CSG_TIN_Edge **)SG_Realloc(m_Edges, (m_nEdges + 1) * sizeof(CSG_TIN_Edge *));
470  m_Edges[m_nEdges++] = pEdge;
471 
472  return( pEdge );
473 }
474 
475 //---------------------------------------------------------
477 {
478  CSG_TIN_Triangle *pTriangle = new CSG_TIN_Triangle(a, b, c);
479 
481  m_Triangles[m_nTriangles++] = pTriangle;
482 
483  if( a->_Add_Neighbor(b) )
484  {
485  b->_Add_Neighbor(a);
486  _Add_Edge(a, b);
487  }
488 
489  if( b->_Add_Neighbor(c) )
490  {
491  c->_Add_Neighbor(b);
492  _Add_Edge(b, c);
493  }
494 
495  if( c->_Add_Neighbor(a) )
496  {
497  a->_Add_Neighbor(c);
498  _Add_Edge(c, a);
499  }
500 
501  a->_Add_Triangle(pTriangle);
502  b->_Add_Triangle(pTriangle);
503  c->_Add_Triangle(pTriangle);
504 
505  return( pTriangle );
506 }
507 
508 
510 // //
512 
513 //---------------------------------------------------------
515 {
516  if( m_bTriangulate )
517  {
518  return( _Triangulate() );
519  }
520 
521  if( Get_Node_Count() >= 3 ) // Update extent...
522  {
523  m_Extent.Assign(Get_Node(0)->Get_Point(), Get_Node(0)->Get_Point());
524 
525  for(int i=1; i<Get_Node_Count(); i++)
526  {
528  }
529 
530  return( true );
531  }
532 
533  m_Extent.Assign(0., 0., 0., 0.);
534 
535  return( false );
536 }
537 
538 
540 // //
542 
543 //---------------------------------------------------------
545 {
546  return( Create(Get_File_Name(false)) );
547 }
548 
549 //---------------------------------------------------------
551 {
552  return( false );
553 }
554 
555 
557 // //
558 // //
559 // //
561 
562 //---------------------------------------------------------
CSG_Rect::Assign
CSG_Rect & Assign(double xMin, double yMin, double xMax, double yMax)
Definition: geo_classes.cpp:727
CSG_TIN::Add_Node
CSG_TIN_Node * Add_Node(const TSG_Point &Point, CSG_Table_Record *pRecord=NULL, bool bUpdateNow=false)
Definition: tin.cpp:410
CSG_TIN::m_Triangles
CSG_TIN_Triangle ** m_Triangles
Definition: tin.h:299
_TL
#define _TL(s)
Definition: api_core.h:1489
CSG_Table::Del_Records
virtual bool Del_Records(void)
Definition: table.cpp:908
CSG_Data_Object::Set_File_Name
void Set_File_Name(const CSG_String &FileName)
Definition: dataobject.cpp:366
CSG_Data_Object::Get_History
CSG_MetaData & Get_History(void)
Definition: dataobject.h:236
CSG_Shapes::Get_Shape
virtual CSG_Shape * Get_Shape(const CSG_Point &Point, double Epsilon=0.)
Definition: shapes.cpp:499
CSG_Data_Object::Update
bool Update(bool bForce=false)
Definition: dataobject.cpp:773
CSG_TIN::Del_Node
bool Del_Node(sLong Index, bool bUpdateNow)
Definition: tin.cpp:428
CSG_Table_Record
Definition: table.h:130
CSG_TIN_Triangle::Get_Node
CSG_TIN_Node * Get_Node(int iNode)
Definition: tin.h:178
CSG_TIN::_Destroy_Nodes
bool _Destroy_Nodes(void)
Definition: tin.cpp:247
SG_UI_MSG_STYLE_SUCCESS
@ SG_UI_MSG_STYLE_SUCCESS
Definition: api_core.h:1505
CSG_TIN::Get_Node
CSG_TIN_Node * Get_Node(sLong Index) const
Definition: tin.h:263
CSG_TIN::Save
virtual bool Save(const CSG_String &File, int Format=0)
Definition: tin.cpp:361
CSG_TIN::On_Reload
virtual bool On_Reload(void)
Definition: tin.cpp:544
CSG_Shapes::Create
bool Create(const CSG_Shapes &Shapes)
Definition: shapes.cpp:204
CSG_TIN::is_Valid
virtual bool is_Valid(void) const
Definition: tin.h:252
CSG_TIN::Get_Node_Count
sLong Get_Node_Count(void) const
Definition: tin.h:262
CSG_TIN::On_Delete
virtual bool On_Delete(void)
Definition: tin.cpp:550
CSG_TIN::m_nTriangles
sLong m_nTriangles
Definition: tin.h:293
CSG_TIN::Get_ObjectType
virtual TSG_Data_Object_Type Get_ObjectType(void) const
Returns the object type as defined by TSG_Data_Object_Type. Used for run time type checking.
Definition: tin.h:244
CSG_TIN::Get_Triangle_Count
sLong Get_Triangle_Count(void) const
Definition: tin.h:268
CSG_Table::Destroy
virtual bool Destroy(void)
Definition: table.cpp:332
CSG_Data_Object::Get_Name
const SG_Char * Get_Name(void) const
Definition: dataobject.cpp:349
SG_Free
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
Definition: api_memory.cpp:83
CSG_Data_Object::Get_ObjectType
virtual TSG_Data_Object_Type Get_ObjectType(void) const =0
Returns the object type as defined by TSG_Data_Object_Type. Used for run time type checking.
SG_Create_TIN
CSG_TIN * SG_Create_TIN(void)
Definition: tin.cpp:63
tin.h
CSG_TIN::_Destroy_Triangles
bool _Destroy_Triangles(void)
Definition: tin.cpp:269
SSG_Point
Definition: geo_tools.h:128
CSG_Shapes::is_Valid
virtual bool is_Valid(void) const
Definition: shapes.h:807
CSG_Data_Object::Set_Update_Flag
void Set_Update_Flag(bool bOn=true)
Definition: dataobject.h:285
CSG_Table::Set_Modified
virtual void Set_Modified(bool bModified=true)
Definition: table.cpp:1113
CSG_Shape::Get_Point
virtual TSG_Point Get_Point(int iPoint=0) const =0
CSG_Shapes::Add_Shape
virtual CSG_Shape * Add_Shape(CSG_Table_Record *pCopy=NULL, TSG_ADD_Shape_Copy_Mode mCopy=SHAPE_COPY)
Definition: shapes.cpp:418
SG_UI_MSG_STYLE_FAILURE
@ SG_UI_MSG_STYLE_FAILURE
Definition: api_core.h:1506
CSG_TIN
Definition: tin.h:222
CSG_Shape::Get_Part_Count
virtual int Get_Part_Count(void) const =0
CSG_Data_Object
Definition: dataobject.h:180
CSG_TIN::Assign
virtual bool Assign(CSG_Data_Object *pObject)
Definition: tin.cpp:290
CSG_TIN::m_bTriangulate
bool m_bTriangulate
Definition: tin.h:291
CSG_Shapes::Save
virtual bool Save(const CSG_String &File, int Format=0)
Definition: shapes_io.cpp:141
sLong
signed long long sLong
Definition: api_core.h:158
CSG_TIN::CSG_TIN
CSG_TIN(void)
Definition: tin.cpp:98
CSG_TIN::Add_Triangle
CSG_TIN_Triangle * Add_Triangle(CSG_TIN_Node *p0, CSG_TIN_Node *p1, CSG_TIN_Node *p2)
Definition: tin.cpp:449
CSG_TIN::Triangulate
bool Triangulate(bool bOn=true, bool bUpdate=false)
Definition: tin.cpp:340
CSG_TIN::_On_Construction
void _On_Construction(void)
Definition: tin.cpp:133
CSG_Table::Get_Count
sLong Get_Count(void) const
Definition: table.h:392
CSG_Data_Object::Get_File_Name
const SG_Char * Get_File_Name(bool bNative=true) const
Definition: dataobject.cpp:390
CSG_Shape::Add_Point
virtual int Add_Point(double x, double y, int iPart=0)=0
CSG_Rect::Union
CSG_Rect & Union(double x, double y)
Definition: geo_classes.cpp:824
CSG_Data_Object::Set_Name
void Set_Name(const CSG_String &Name)
Definition: dataobject.cpp:300
CSG_MetaData::Add_Children
bool Add_Children(const CSG_MetaData &MetaData)
Definition: metadata.cpp:359
CSG_String::Format
static CSG_String Format(const char *Format,...)
Definition: api_string.cpp:270
CSG_Table
Definition: table.h:283
CSG_Shape::Get_Point_Count
virtual int Get_Point_Count(void) const =0
CSG_Table::Del_Record
virtual bool Del_Record(sLong Index)
Definition: table.cpp:869
CSG_TIN_Triangle
Definition: tin.h:173
CSG_String
Definition: api_core.h:563
SHAPE_TYPE_Point
@ SHAPE_TYPE_Point
Definition: shapes.h:102
SG_UI_Process_Set_Progress
bool SG_UI_Process_Set_Progress(int Position, int Range)
Definition: api_callback.cpp:255
CSG_TIN_Node
Definition: tin.h:93
CSG_TIN::Destroy
virtual bool Destroy(void)
Definition: tin.cpp:230
CSG_TIN::m_Extent
CSG_Rect m_Extent
Definition: tin.h:295
CSG_TIN::_Add_Triangle
CSG_TIN_Triangle * _Add_Triangle(CSG_TIN_Node *a, CSG_TIN_Node *b, CSG_TIN_Node *c)
Definition: tin.cpp:476
CSG_TIN::m_nEdges
sLong m_nEdges
Definition: tin.h:293
CSG_Table_Record::Get_Index
sLong Get_Index(void) const
Definition: table.h:136
CSG_TIN_Edge
Definition: tin.h:146
CSG_Table::Create
bool Create(void)
Definition: table.cpp:139
CSG_TIN::Get_Triangle
CSG_TIN_Triangle * Get_Triangle(sLong Index) const
Definition: tin.h:269
CSG_Shapes
Definition: shapes.h:775
CSG_TIN::_Add_Edge
CSG_TIN_Edge * _Add_Edge(CSG_TIN_Node *a, CSG_TIN_Node *b)
Definition: tin.cpp:465
CSG_TIN::_Get_New_Record
virtual CSG_Table_Record * _Get_New_Record(sLong Index)
Definition: tin.cpp:404
SG_UI_Process_Set_Ready
bool SG_UI_Process_Set_Ready(void)
Definition: api_callback.cpp:305
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
CSG_MetaData::Add_Child
CSG_MetaData * Add_Child(void)
Definition: metadata.cpp:166
CSG_TIN::~CSG_TIN
virtual ~CSG_TIN(void)
Definition: tin.cpp:224
CSG_Table::Add_Record
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition: table.cpp:795
CSG_TIN::On_Update
virtual bool On_Update(void)
Definition: tin.cpp:514
CSG_TIN::_Triangulate
bool _Triangulate(void)
Definition: tin_triangulation.cpp:94
CSG_Shape
Definition: shapes.h:141
CSG_Data_Object::is_Valid
virtual bool is_Valid(void) const =0
CSG_TIN::_Destroy_Edges
bool _Destroy_Edges(void)
Definition: tin.cpp:253
CSG_TIN::m_Edges
CSG_TIN_Edge ** m_Edges
Definition: tin.h:297
SG_UI_Msg_Add
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
Definition: api_callback.cpp:502
CSG_Table::_On_Construction
virtual void _On_Construction(void)
Definition: table.cpp:302
CSG_TIN_Node::Get_Point
const TSG_Point & Get_Point(void)
Definition: tin.h:100