SAGA API  v9.7
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();
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 //---------------------------------------------------------
117 CSG_TIN::CSG_TIN(const char *File) : CSG_TIN(CSG_String(File)) {}
118 CSG_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 //---------------------------------------------------------
152 bool CSG_TIN::Create(const CSG_TIN &TIN)
153 {
154  return( Assign((CSG_Data_Object *)&TIN) );
155 }
156 
157 //---------------------------------------------------------
158 bool CSG_TIN::Create(const char *File) { return( Create(CSG_String(File)) ); }
159 bool CSG_TIN::Create(const wchar_t *File) { return( Create(CSG_String(File)) ); }
160 bool CSG_TIN::Create(const 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);
171  Set_Update_Flag();
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  {
212  SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS);
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 {
238  _Destroy_Edges();
239  _Destroy_Nodes();
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 //---------------------------------------------------------
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  m_bTriangulate = false;
330  Update();
332 
333  return( true );
334  }
335 
336  return( false );
337 }
338 
339 
341 // //
343 
344 //---------------------------------------------------------
345 bool 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 //---------------------------------------------------------
366 bool CSG_TIN::Save(const CSG_String &File, int Format)
367 {
368  bool bResult = false;
369 
370  if( Get_Triangle_Count() > 0 )
371  {
372  switch( Format )
373  {
374  case 0: default:
375  {
376  CSG_Shapes Points;
377 
378  Points.Create(SHAPE_TYPE_Point, Get_Name(), this);
379 
380  for(sLong i=0; i<Get_Node_Count(); i++)
381  {
382  CSG_TIN_Node *pNode = Get_Node(i);
383 
384  Points.Add_Shape(pNode)->Add_Point(pNode->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 //---------------------------------------------------------
415 CSG_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 //---------------------------------------------------------
433 bool 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 //---------------------------------------------------------
455 {
456  return( _Add_Triangle((CSG_TIN_Node *)p0, (CSG_TIN_Node *)p1, (CSG_TIN_Node *)p2) );
457 }
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 
474  m_Edges = (CSG_TIN_Edge **)SG_Realloc(m_Edges, (m_nEdges + 1) * sizeof(CSG_TIN_Edge *));
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  {
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 //---------------------------------------------------------
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:415
CSG_TIN::m_Triangles
CSG_TIN_Triangle ** m_Triangles
Definition: tin.h:299
_TL
#define _TL(s)
Definition: api_core.h:1507
CSG_Table::Del_Records
virtual bool Del_Records(void)
Definition: table.cpp:901
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:483
CSG_Data_Object::Update
bool Update(bool bForce=false)
Definition: dataobject.cpp:777
CSG_TIN::Del_Node
bool Del_Node(sLong Index, bool bUpdateNow)
Definition: tin.cpp:433
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:252
SG_UI_MSG_STYLE_SUCCESS
@ SG_UI_MSG_STYLE_SUCCESS
Definition: api_core.h:1523
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:366
CSG_TIN::On_Reload
virtual bool On_Reload(void)
Definition: tin.cpp:549
CSG_Shapes::Create
bool Create(const CSG_Shapes &Shapes)
Definition: shapes.cpp:209
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:555
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:325
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:274
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:285
CSG_Table::Set_Modified
virtual void Set_Modified(bool bModified=true)
Definition: table.cpp:1106
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:402
SG_UI_MSG_STYLE_FAILURE
@ SG_UI_MSG_STYLE_FAILURE
Definition: api_core.h:1524
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_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:103
CSG_TIN::Add_Triangle
CSG_TIN_Triangle * Add_Triangle(CSG_TIN_Node *p0, CSG_TIN_Node *p1, CSG_TIN_Node *p2)
Definition: tin.cpp:454
CSG_TIN::Triangulate
bool Triangulate(bool bOn=true, bool bUpdate=false)
Definition: tin.cpp:345
CSG_TIN::_On_Construction
void _On_Construction(void)
Definition: tin.cpp:138
CSG_Table::Get_Count
sLong Get_Count(void) const
Definition: table.h:397
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:285
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:862
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:235
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:481
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:153
CSG_TIN::Get_Triangle
CSG_TIN_Triangle * Get_Triangle(sLong Index) const
Definition: tin.h:269
CSG_Shapes
Definition: shapes.h:773
CSG_TIN::_Add_Edge
CSG_TIN_Edge * _Add_Edge(CSG_TIN_Node *a, CSG_TIN_Node *b)
Definition: tin.cpp:470
CSG_TIN::_Get_New_Record
virtual CSG_Table_Record * _Get_New_Record(sLong Index)
Definition: tin.cpp:409
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:229
CSG_Table::Add_Record
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition: table.cpp:788
CSG_TIN::On_Update
virtual bool On_Update(void)
Definition: tin.cpp:519
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:152
CSG_Data_Object::is_Valid
virtual bool is_Valid(void) const =0
CSG_TIN::_Destroy_Edges
bool _Destroy_Edges(void)
Definition: tin.cpp:258
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:295
CSG_TIN_Node::Get_Point
const TSG_Point & Get_Point(void)
Definition: tin.h:100