QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
util.h
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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #ifndef _UTIL_H
35 #define _UTIL_H
36 
37 #include <cmath>
38 #include <cstring>
39 #include <stddef.h>
40 #include <geos_c.h>
41 
42 #include <pal/pal.h>
43 
44 #include "rtree.hpp"
45 #include "pointset.h"
46 
47 namespace pal
48 {
49 
50  class LabelPosition;
51  class Layer;
52  class FeaturePart;
53 
54  LinkedList<const GEOSGeometry*> * unmulti( const GEOSGeometry* the_geom );
55 
59  class Feats
60  {
61  public:
64  double priority;
65  int nblp;
67  };
68 
69 
71  {
72  int feat;
73  int old_label;
74  int new_label;
75  } ElemTrans;
76 
77  struct Point
78  {
79  double x, y;
80  };
81 
82 
83 
84 #define EPSILON 1e-9
85 
86 
87  inline int max( int a, int b )
88  { return a > b ? a : b; }
89 
90  inline double max( double a, double b )
91  { return a > b ? a : b; }
92 
93  inline int min( int a, int b )
94  { return a < b ? a : b; }
95 
96  inline double min( double a, double b )
97  { return a < b ? a : b; }
98 
99  inline double vabs( double x )
100  { return x >= 0 ? x : -x; }
101 
102 
103 
104 
105  inline double degree2meter( double delta_deg )
106  {
107  double lat = delta_deg * 0.5;
108  const static double rads = ( 4.0 * atan( 1.0 ) ) / 180.0;
109  double a = cos( lat * rads );
110  a = a * a;
111  double c = 2.0 * atan2( sqrt( a ), sqrt( 1.0 - a ) );
112  const static double ra = 6378000; // [m]
113  const static double e = 0.0810820288;
114  double radius = ra * ( 1.0 - e * e ) / pow( 1.0 - e * e * sin( lat * rads ) * sin( lat * rads ), 1.5 );
115  double meters = ( delta_deg ) / 180.0 * radius * c; // [m]
116 
117  return meters;
118  }
119 
120  inline double unit_convert( double x, Units from, Units to, int dpi, double scale, double delta_canvas_width )
121  {
122  /* nothing to convert */
123  if ( from == to )
124  {
125  return x;
126  }
127 
128  switch ( from )
129  {
130  case pal::PIXEL:
131  switch ( to )
132  {
133  case pal::METER:
134  return (( x / double( dpi ) ) * 0.0254 ) * scale;
135  case pal::FOOT:
136  return (( x / double( dpi ) )*12 ) * scale;
137  case pal::DEGREE:
138  {
139  double iw = degree2meter( delta_canvas_width ) * 39.3700787;
140  return ( x * delta_canvas_width * scale ) / ( iw * dpi );
141  }
142  default:
143  fprintf( stderr, "Target unit undefined\n" );
144  return 0.0;
145  }
146  break;
147  case pal::METER:
148  switch ( to )
149  {
150  case pal::PIXEL:
151  return ( x*dpi ) / ( 2.54*scale );
152  case pal::FOOT:
153  return x / 0.3048;
154  case pal::DEGREE:
155  {
156  double mw = degree2meter( delta_canvas_width );
157  return ( x * delta_canvas_width ) / mw;
158  }
159  default:
160  fprintf( stderr, "Target unit undefined\n" );
161  return 0.0;
162  }
163  break;
164  case pal::FOOT:
165  switch ( to )
166  {
167  case pal::PIXEL:
168  return ( x*dpi ) / ( 12*scale );
169  case pal::METER:
170  return x*0.3048;
171  case pal::DEGREE:
172  {
173  double iw = degree2meter( delta_canvas_width ) * 39.3700787;
174  return ( x * delta_canvas_width ) / iw;
175  }
176  default:
177  fprintf( stderr, "Target unit undefined\n" );
178  return 0.0;
179  }
180  break;
181  case pal::DEGREE:
182  switch ( to )
183  {
184  case pal::PIXEL:
185  fprintf( stderr, "Degree to pixel not yet implemented\n" );
186  break;
187  case pal::METER:
188  fprintf( stderr, "Degree to meter not yet implemented\n" );
189  break;
190  case pal::FOOT:
191  fprintf( stderr, "Degree to foot not yet implemented\n" );
192  break;
193  default:
194  fprintf( stderr, "Target unit undefined\n" );
195  return 0.0;
196  }
197  break;
198  default:
199  fprintf( stderr, "Source unit undefined" );
200  return 0.0;
201 
202  }
203 
204  fprintf( stderr, "Unable to convert. Unknown units\n" );
205  return 0.0;
206  }
207 
208 
209  /* From meters to PostScript Point */
210  inline void convert2pt( int *x, double scale, int dpi )
211  {
212  *x = ( int )((( double ) * x / scale ) * 39.3700787402 * dpi + 0.5 );
213  }
214 
215 
216  inline int convert2pt( double x, double scale, int dpi )
217  {
218  return ( int )(( x / scale ) * 39.3700787402 * dpi + 0.5 );
219  }
220 
221 
222  void sort( double* heap, int* x, int* y, int N );
223 
224 
225  inline bool intCompare( int a, int b )
226  {
227  return a == b;
228  }
229 
230  inline bool strCompare( char * a, char * b )
231  {
232  return strcmp( a, b ) == 0;
233  }
234 
236  {
237  return a == b;
238  }
239 
240  inline bool ptrPSetCompare( PointSet * a, PointSet * b )
241  {
242  return a == b;
243  }
244 
245  inline bool ptrFeatureCompare( Feature * a, Feature * b )
246  {
247  return a == b;
248  }
250  {
251  return a == b;
252  }
253 
254  inline bool ptrFeatsCompare( Feats * a, Feats * b )
255  {
256  return a == b;
257  }
258 
259  inline bool ptrLayerCompare( Layer * a, Layer * b )
260  {
261  return a == b;
262  }
263 
264 
265  inline bool ptrETCompare( ElemTrans * a, ElemTrans * b )
266  {
267  return a == b;
268  }
269 
276  void sort( void** items, int N, bool ( *greater )( void *l, void *r ) );
277 
278  void tabcpy( int n, const int* const x, const int* const y,
279  const double* const prob, int *cx, int *cy, double *p );
280 
281 
282 } // namespace
283 
284 #endif