SAGA API  v9.2
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;
138  m_nEdges = 0;
139 
140  m_Triangles = NULL;
141  m_nTriangles = 0;
142 }
143 
144 
146 // //
148 
149 //---------------------------------------------------------
150 bool CSG_TIN::Create(const CSG_TIN &TIN)
151 {
152  return( Assign((CSG_Data_Object *)&TIN) );
153 }
154 
155 //---------------------------------------------------------
156 bool CSG_TIN::Create(const char *File) { return( Create(CSG_String(File)) ); }
157 bool CSG_TIN::Create(const wchar_t *File) { return( Create(CSG_String(File)) ); }
158 bool CSG_TIN::Create(const CSG_String &File)
159 {
160  CSG_Shapes Shapes(File);
161 
162  if( Create(&Shapes) )
163  {
164  Get_History().Add_Child(_TL("Created from file"), File);
166 
167  Set_File_Name(File, true);
168  Set_Modified(false);
169  Set_Update_Flag();
170 
171  return( true );
172  }
173 
174  return( false );
175 }
176 
177 //---------------------------------------------------------
179 {
180  Destroy();
181 
182  if( pShapes && pShapes->is_Valid() )
183  {
184  SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Create TIN from shapes"), pShapes->Get_Name()), true);
185 
186  CSG_Table::Create(pShapes);
187 
188  Set_Name(pShapes->Get_Name());
189 
190  //-------------------------------------------------
191  for(sLong iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++)
192  {
193  CSG_Shape *pShape = pShapes->Get_Shape(iShape);
194 
195  for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
196  {
197  for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
198  {
199  Add_Node(pShape->Get_Point(iPoint, iPart), pShape, false);
200  }
201  }
202  }
203 
205 
206  if( Update() )
207  {
208  SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS);
209 
210  return( true );
211  }
212  }
213 
214  SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
215 
216  return( false );
217 }
218 
219 
221 // //
223 
224 //---------------------------------------------------------
226 {
227  Destroy();
228 }
229 
230 //---------------------------------------------------------
232 {
234  _Destroy_Edges();
235  _Destroy_Nodes();
236 
238 
239  return( true );
240 }
241 
242 
244 // //
246 
247 //---------------------------------------------------------
249 {
250  return( Del_Records() );
251 }
252 
253 //---------------------------------------------------------
255 {
256  if( m_nEdges > 0 )
257  {
258  for(sLong i=0; i<m_nEdges; i++)
259  {
260  delete(m_Edges[i]);
261  }
262 
263  SG_Free(m_Edges);
264  m_Edges = NULL;
265  m_nEdges = 0;
266  }
267 
268  return( true );
269 }
270 
271 //---------------------------------------------------------
273 {
274  if( m_nTriangles > 0 )
275  {
276  for(sLong i=0; i<m_nTriangles; i++)
277  {
278  delete(m_Triangles[i]);
279  }
280 
282  m_Triangles = NULL;
283  m_nTriangles = 0;
284  }
285 
286  return( true );
287 }
288 
289 
291 // //
293 
294 //---------------------------------------------------------
296 {
297  if( pObject && pObject->is_Valid() && pObject->Get_ObjectType() == Get_ObjectType() )
298  {
299  CSG_TIN *pTIN = (CSG_TIN *)pObject;
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  return( true );
330  }
331 
332  return( false );
333 }
334 
335 
337 // //
339 
340 //---------------------------------------------------------
341 bool CSG_TIN::Save(const CSG_String &File, int Format)
342 {
343  bool bResult = false;
344 
345  if( Get_Triangle_Count() > 0 )
346  {
347  switch( Format )
348  {
349  case 0: default:
350  {
351  CSG_Shapes Points;
352 
353  Points.Create(SHAPE_TYPE_Point, Get_Name(), this);
354 
355  for(sLong i=0; i<Get_Node_Count(); i++)
356  {
357  CSG_TIN_Node *pNode = Get_Node(i);
358 
359  Points.Add_Shape(pNode)->Add_Point(pNode->Get_Point());
360  }
361 
362  bResult = Points.Save(File);
363  }
364  break;
365  }
366  }
367 
368  if( bResult )
369  {
370  Set_Modified(false);
371 
372  Set_File_Name(File, true);
373  }
374 
375  return( bResult );
376 }
377 
378 
380 // //
382 
383 //---------------------------------------------------------
385 {
386  return( new CSG_TIN_Node(this, Index) );
387 }
388 
389 //---------------------------------------------------------
390 CSG_TIN_Node * CSG_TIN::Add_Node(TSG_Point Point, CSG_Table_Record *pRecord, bool bUpdateNow)
391 {
392  CSG_TIN_Node *pNode = (CSG_TIN_Node *)Add_Record(pRecord);
393 
394  if( pNode )
395  {
396  pNode->m_Point = Point;
397 
398  if( bUpdateNow )
399  {
400  Update();
401  }
402  }
403 
404  return( pNode );
405 }
406 
407 //---------------------------------------------------------
408 bool CSG_TIN::Del_Node(sLong Index, bool bUpdateNow)
409 {
410  if( Del_Record(Index) )
411  {
412  if( bUpdateNow )
413  {
414  Update();
415  }
416 
417  return( true );
418  }
419 
420  return( false );
421 }
422 
423 
425 // //
427 
428 //---------------------------------------------------------
430 {
431  m_Edges = (CSG_TIN_Edge **)SG_Realloc(m_Edges, (m_nEdges + 1) * sizeof(CSG_TIN_Edge *));
432  m_Edges[m_nEdges++] = new CSG_TIN_Edge(a, b);
433 
434  return( true );
435 }
436 
437 //---------------------------------------------------------
439 {
440  CSG_TIN_Triangle *pTriangle;
441 
443  m_Triangles[m_nTriangles++] = pTriangle = new CSG_TIN_Triangle(a, b, c);
444 
445  if( a->_Add_Neighbor(b) )
446  {
447  b->_Add_Neighbor(a);
448  _Add_Edge(a, b);
449  }
450 
451  if( b->_Add_Neighbor(c) )
452  {
453  c->_Add_Neighbor(b);
454  _Add_Edge(b, c);
455  }
456 
457  if( c->_Add_Neighbor(a) )
458  {
459  a->_Add_Neighbor(c);
460  _Add_Edge(c, a);
461  }
462 
463  a->_Add_Triangle(pTriangle);
464  b->_Add_Triangle(pTriangle);
465  c->_Add_Triangle(pTriangle);
466 
467  return( true );
468 }
469 
470 
472 // //
474 
475 //---------------------------------------------------------
477 {
478  return( _Triangulate() );
479 }
480 
481 
483 // //
485 
486 //---------------------------------------------------------
488 {
489  return( Create(Get_File_Name(false)) );
490 }
491 
492 //---------------------------------------------------------
494 {
495  return( false );
496 }
497 
498 
500 // //
501 // //
502 // //
504 
505 //---------------------------------------------------------
CSG_TIN::_Add_Edge
bool _Add_Edge(CSG_TIN_Node *a, CSG_TIN_Node *b)
Definition: tin.cpp:429
CSG_TIN::m_Triangles
CSG_TIN_Triangle ** m_Triangles
Definition: tin.h:294
_TL
#define _TL(s)
Definition: api_core.h:1480
CSG_Table::Del_Records
virtual bool Del_Records(void)
Definition: table.cpp:912
CSG_Data_Object::Set_File_Name
void Set_File_Name(const CSG_String &FileName)
Definition: dataobject.cpp:307
CSG_Data_Object::Get_History
CSG_MetaData & Get_History(void)
Definition: dataobject.h:227
CSG_Shapes::Get_Shape
virtual CSG_Shape * Get_Shape(const CSG_Point &Point, double Epsilon=0.)
Definition: shapes.cpp:586
CSG_Data_Object::Update
bool Update(bool bForce=false)
Definition: dataobject.cpp:714
CSG_TIN::Del_Node
bool Del_Node(sLong Index, bool bUpdateNow)
Definition: tin.cpp:408
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:248
SG_UI_MSG_STYLE_SUCCESS
@ SG_UI_MSG_STYLE_SUCCESS
Definition: api_core.h:1496
CSG_TIN::Get_Node
CSG_TIN_Node * Get_Node(sLong Index) const
Definition: tin.h:262
CSG_TIN::Save
virtual bool Save(const CSG_String &File, int Format=0)
Definition: tin.cpp:341
CSG_TIN::On_Reload
virtual bool On_Reload(void)
Definition: tin.cpp:487
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:261
CSG_TIN::On_Delete
virtual bool On_Delete(void)
Definition: tin.cpp:493
CSG_TIN::m_nTriangles
sLong m_nTriangles
Definition: tin.h:288
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:267
CSG_Table::Destroy
virtual bool Destroy(void)
Definition: table.cpp:327
CSG_Data_Object::Get_Name
const SG_Char * Get_Name(void) const
Definition: dataobject.cpp:290
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:272
SSG_Point
Definition: geo_tools.h:128
CSG_Shapes::is_Valid
virtual bool is_Valid(void) const
Definition: shapes.h:805
CSG_Data_Object::Set_Update_Flag
void Set_Update_Flag(bool bOn=true)
Definition: dataobject.h:273
CSG_Table::Set_Modified
virtual void Set_Modified(bool bModified=true)
Definition: table.cpp:1117
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:505
SG_UI_MSG_STYLE_FAILURE
@ SG_UI_MSG_STYLE_FAILURE
Definition: api_core.h:1497
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:295
CSG_Shapes::Save
virtual bool Save(const CSG_String &File, int Format=0)
Definition: shapes.cpp:432
CSG_TIN::Add_Node
CSG_TIN_Node * Add_Node(TSG_Point Point, CSG_Table_Record *pRecord, bool bUpdateNow)
Definition: tin.cpp:390
sLong
signed long long sLong
Definition: api_core.h:158
CSG_TIN::CSG_TIN
CSG_TIN(void)
Definition: tin.cpp:98
CSG_TIN::_On_Construction
void _On_Construction(void)
Definition: tin.cpp:133
CSG_Table::Get_Count
sLong Get_Count(void) const
Definition: table.h:385
CSG_Data_Object::Get_File_Name
const SG_Char * Get_File_Name(bool bNative=true) const
Definition: dataobject.cpp:331
CSG_Shape::Add_Point
virtual int Add_Point(double x, double y, int iPart=0)=0
CSG_Data_Object::Set_Name
void Set_Name(const CSG_String &Name)
Definition: dataobject.cpp:235
CSG_MetaData::Add_Children
bool Add_Children(const CSG_MetaData &MetaData)
Definition: metadata.cpp:351
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:873
CSG_TIN_Triangle
Definition: tin.h:173
CSG_Table::Create
bool Create(const CSG_Table &Table)
Definition: table.cpp:146
CSG_String
Definition: api_core.h:557
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:249
CSG_TIN_Node
Definition: tin.h:93
CSG_TIN::Destroy
virtual bool Destroy(void)
Definition: tin.cpp:231
CSG_TIN::m_nEdges
sLong m_nEdges
Definition: tin.h:288
CSG_Table_Record::Get_Index
sLong Get_Index(void) const
Definition: table.h:136
CSG_TIN_Edge
Definition: tin.h:146
CSG_TIN::Get_Triangle
CSG_TIN_Triangle * Get_Triangle(sLong Index) const
Definition: tin.h:268
CSG_Shapes
Definition: shapes.h:773
CSG_TIN::_Get_New_Record
virtual CSG_Table_Record * _Get_New_Record(sLong Index)
Definition: tin.cpp:384
SG_UI_Process_Set_Ready
bool SG_UI_Process_Set_Ready(void)
Definition: api_callback.cpp:299
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:174
CSG_TIN::~CSG_TIN
virtual ~CSG_TIN(void)
Definition: tin.cpp:225
CSG_Table::Add_Record
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition: table.cpp:799
CSG_TIN::On_Update
virtual bool On_Update(void)
Definition: tin.cpp:476
CSG_TIN::_Triangulate
bool _Triangulate(void)
Definition: tin_triangulation.cpp:94
CSG_Shape
Definition: shapes.h:141
CSG_TIN::Create
bool Create(const CSG_TIN &TIN)
Definition: tin.cpp:150
CSG_Data_Object::is_Valid
virtual bool is_Valid(void) const =0
CSG_TIN::_Destroy_Edges
bool _Destroy_Edges(void)
Definition: tin.cpp:254
CSG_TIN::m_Edges
CSG_TIN_Edge ** m_Edges
Definition: tin.h:292
CSG_TIN::_Add_Triangle
bool _Add_Triangle(CSG_TIN_Node *a, CSG_TIN_Node *b, CSG_TIN_Node *c)
Definition: tin.cpp:438
SG_UI_Msg_Add
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
Definition: api_callback.cpp:490
CSG_Table::_On_Construction
virtual void _On_Construction(void)
Definition: table.cpp:297
CSG_TIN_Node::Get_Point
const TSG_Point & Get_Point(void)
Definition: tin.h:100