QGIS API Documentation  2.99.0-Master (f1c3692)
util.cpp
Go to the documentation of this file.
1 /*
2  * libpal - Automated Placement of Labels Library
3  *
4  * Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD
5  * University of Applied Sciences, Western Switzerland
6  * http://www.hes-so.ch
7  *
8  * Contact:
9  * maxence.laurent <at> heig-vd <dot> ch
10  * or
11  * eric.taillard <at> heig-vd <dot> ch
12  *
13  * This file is part of libpal.
14  *
15  * libpal is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * libpal is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with libpal. If not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29 
30 #include "layer.h"
31 #include "internalexception.h"
32 #include "util.h"
33 #include "labelposition.h"
34 #include "feature.h"
35 #include "geomfunction.h"
36 
37 #include "qgslogger.h"
38 #include <cfloat>
39 
40 void pal::Util::sort( void **items, int N, bool ( *greater )( void *l, void *r ) )
41 {
42 
43  if ( N <= 0 )
44  return;
45 
46  unsigned int n = static_cast< unsigned int >( N ), i = n / 2, parent, child;
47 
48  void *t = nullptr;
49 
50  for ( ;; )
51  {
52  if ( i > 0 )
53  {
54  i--;
55  t = items[i];
56  }
57  else
58  {
59  n--;
60  if ( n == 0 ) return;
61  t = items[n];
62  items[n] = items[0];
63  }
64  parent = i;
65  child = i * 2 + 1;
66  while ( child < n )
67  {
68  if ( child + 1 < n && greater( items[child + 1], items[child] ) )
69  {
70  child++;
71  }
72  if ( greater( items[child], t ) )
73  {
74  items[parent] = items[child];
75  parent = child;
76  child = parent * 2 + 1;
77  }
78  else
79  {
80  break;
81  }
82  }
83  items[parent] = t;
84  }
85 }
86 
87 QLinkedList<const GEOSGeometry *> *pal::Util::unmulti( const GEOSGeometry *the_geom )
88 {
89  QLinkedList<const GEOSGeometry *> *queue = new QLinkedList<const GEOSGeometry *>;
90  QLinkedList<const GEOSGeometry *> *final_queue = new QLinkedList<const GEOSGeometry *>;
91 
92  const GEOSGeometry *geom = nullptr;
93 
94  queue->append( the_geom );
95  int nGeom;
96  int i;
97 
98  GEOSContextHandle_t geosctxt = geosContext();
99 
100  while ( !queue->isEmpty() )
101  {
102  geom = queue->takeFirst();
103  int type = GEOSGeomTypeId_r( geosctxt, geom );
104  switch ( type )
105  {
106  case GEOS_MULTIPOINT:
107  case GEOS_MULTILINESTRING:
108  case GEOS_MULTIPOLYGON:
109  nGeom = GEOSGetNumGeometries_r( geosctxt, geom );
110  for ( i = 0; i < nGeom; i++ )
111  {
112  queue->append( GEOSGetGeometryN_r( geosctxt, geom, i ) );
113  }
114  break;
115  case GEOS_POINT:
116  case GEOS_LINESTRING:
117  case GEOS_POLYGON:
118  final_queue->append( geom );
119  break;
120  default:
121  QgsDebugMsg( QString( "unexpected geometry type:%1" ).arg( type ) );
122  delete final_queue;
123  delete queue;
124  return nullptr;
125  }
126  }
127  delete queue;
128 
129  return final_queue;
130 }
131 
132 
133 
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
static QLinkedList< const GEOSGeometry * > * unmulti(const GEOSGeometry *the_geom)
Definition: util.cpp:87
static void sort(void **items, int N, bool(*greater)(void *l, void *r))
Sort an array of pointers.
Definition: util.cpp:40
GEOSContextHandle_t geosContext()
Get GEOS context handle to be used in all GEOS library calls with reentrant API.
Definition: pal.cpp:48