QGIS API Documentation  2.12.0-Lyon
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 <iostream>
37 #include <cfloat>
38 //#include <cfloat>
39 #include <cstdarg>
40 #include <ctime>
41 
42 #ifndef M_PI
43 #define M_PI 3.14159265358979323846
44 #endif
45 
46 #ifndef M_PI_2
47 #define M_PI_2 1.57079632679489661923
48 #endif
49 
50 #ifndef M_SQRT_2
51 #define M_SQRT_2 0.707106781186547524401
52 #endif
53 
54 #ifndef M_SQRT2
55 #define M_SQRT2 1.41421356237309504880
56 #endif
57 
58 namespace pal
59 {
60 
61  void sort( void** items, int N, bool ( *greater )( void *l, void *r ) )
62  {
63 
64  if ( N <= 0 )
65  return;
66 
67  unsigned int n = ( unsigned int ) N, i = n / 2, parent, child;
68 
69  void *t = NULL;
70 
71  for ( ;; )
72  {
73  if ( i > 0 )
74  {
75  i--;
76  t = items[i];
77  }
78  else
79  {
80  n--;
81  if ( n == 0 ) return;
82  t = items[n];
83  items[n] = items[0];
84  }
85  parent = i;
86  child = i * 2 + 1;
87  while ( child < n )
88  {
89  if ( child + 1 < n && greater( items[child + 1], items[child] ) )
90  {
91  child++;
92  }
93  if ( greater( items[child], t ) )
94  {
95  items[parent] = items[child];
96  parent = child;
97  child = parent * 2 + 1;
98  }
99  else
100  {
101  break;
102  }
103  }
104  items[parent] = t;
105  }
106  }
107 
108  inline bool ptrGeomEq( const GEOSGeometry *l, const GEOSGeometry *r )
109  {
110  return l == r;
111  }
112 
113  QLinkedList<const GEOSGeometry *> *unmulti( const GEOSGeometry *the_geom )
114  {
117 
118  const GEOSGeometry *geom;
119 
120  queue->append( the_geom );
121  int nGeom;
122  int i;
123 
124  while ( queue->size() > 0 )
125  {
126  geom = queue->takeFirst();
127  GEOSContextHandle_t geosctxt = geosContext();
128  switch ( GEOSGeomTypeId_r( geosctxt, geom ) )
129  {
130  case GEOS_MULTIPOINT:
131  case GEOS_MULTILINESTRING:
132  case GEOS_MULTIPOLYGON:
133  nGeom = GEOSGetNumGeometries_r( geosctxt, geom );
134  for ( i = 0; i < nGeom; i++ )
135  {
136  queue->append( GEOSGetGeometryN_r( geosctxt, geom, i ) );
137  }
138  break;
139  case GEOS_POINT:
140  case GEOS_LINESTRING:
141  case GEOS_POLYGON:
142  final_queue->append( geom );
143  break;
144  default:
145  delete final_queue;
146  delete queue;
147  return NULL;
148  }
149  }
150  delete queue;
151 
152  return final_queue;
153  }
154 
155 
156 } // namespace
157 
158 
159 
bool ptrGeomEq(const GEOSGeometry *l, const GEOSGeometry *r)
Definition: util.cpp:108
QLinkedList< const GEOSGeometry * > * unmulti(const GEOSGeometry *the_geom)
Definition: util.cpp:113
GEOSContextHandle_t geosContext()
Get GEOS context handle to be used in all GEOS library calls with reentrant API.
Definition: pal.cpp:57
void sort(void **items, int N, bool(*greater)(void *l, void *r))
Sort an array of pointers.
Definition: util.cpp:61
int size() const
void append(const T &value)