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