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