QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 #ifndef M_PI
41 #define M_PI 3.14159265358979323846
42 #endif
43 
44 #ifndef M_PI_2
45 #define M_PI_2 1.57079632679489661923
46 #endif
47 
48 #ifndef M_SQRT2
49 #define M_SQRT2 1.41421356237309504880
50 #endif
51 
52 void pal::Util::sort( void** items, int N, bool ( *greater )( void *l, void *r ) )
53 {
54 
55  if ( N <= 0 )
56  return;
57 
58  unsigned int n = static_cast< unsigned int >( N ), i = n / 2, parent, child;
59 
60  void *t = nullptr;
61 
62  for ( ;; )
63  {
64  if ( i > 0 )
65  {
66  i--;
67  t = items[i];
68  }
69  else
70  {
71  n--;
72  if ( n == 0 ) return;
73  t = items[n];
74  items[n] = items[0];
75  }
76  parent = i;
77  child = i * 2 + 1;
78  while ( child < n )
79  {
80  if ( child + 1 < n && greater( items[child + 1], items[child] ) )
81  {
82  child++;
83  }
84  if ( greater( items[child], t ) )
85  {
86  items[parent] = items[child];
87  parent = child;
88  child = parent * 2 + 1;
89  }
90  else
91  {
92  break;
93  }
94  }
95  items[parent] = t;
96  }
97 }
98 
99 QLinkedList<const GEOSGeometry *>* pal::Util::unmulti( const GEOSGeometry *the_geom )
100 {
103 
104  const GEOSGeometry *geom;
105 
106  queue->append( the_geom );
107  int nGeom;
108  int i;
109 
110  GEOSContextHandle_t geosctxt = geosContext();
111 
112  while ( !queue->isEmpty() )
113  {
114  geom = queue->takeFirst();
115  int type = GEOSGeomTypeId_r( geosctxt, geom );
116  switch ( type )
117  {
118  case GEOS_MULTIPOINT:
119  case GEOS_MULTILINESTRING:
120  case GEOS_MULTIPOLYGON:
121  nGeom = GEOSGetNumGeometries_r( geosctxt, geom );
122  for ( i = 0; i < nGeom; i++ )
123  {
124  queue->append( GEOSGetGeometryN_r( geosctxt, geom, i ) );
125  }
126  break;
127  case GEOS_POINT:
128  case GEOS_LINESTRING:
129  case GEOS_POLYGON:
130  final_queue->append( geom );
131  break;
132  default:
133  QgsDebugMsg( QString( "unexpected geometry type:%1" ).arg( type ) );
134  delete final_queue;
135  delete queue;
136  return nullptr;
137  }
138  }
139  delete queue;
140 
141  return final_queue;
142 }
143 
144 
145 
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static QLinkedList< const GEOSGeometry * > * unmulti(const GEOSGeometry *the_geom)
Definition: util.cpp:99
bool isEmpty() const
static void sort(void **items, int N, bool(*greater)(void *l, void *r))
Sort an array of pointers.
Definition: util.cpp:52
GEOSContextHandle_t geosContext()
Get GEOS context handle to be used in all GEOS library calls with reentrant API.
Definition: pal.cpp:48
void append(const T &value)