/
util.h
280 lines (230 loc) · 6.48 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* libpal - Automated Placement of Labels Library
*
* Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD
* University of Applied Sciences, Western Switzerland
* http://www.hes-so.ch
*
* Contact:
* maxence.laurent <at> heig-vd <dot> ch
* or
* eric.taillard <at> heig-vd <dot> ch
*
* This file is part of libpal.
*
* libpal is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libpal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libpal. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef _UTIL_H
#define _UTIL_H
#include <cmath>
#include <cstring>
#include <stddef.h>
#include <geos_c.h>
#include <pal/pal.h>
#include "rtree.hpp"
#include "pointset.h"
namespace pal
{
class LabelPosition;
class Layer;
class FeaturePart;
LinkedList<const GEOSGeometry*> * unmulti( const GEOSGeometry* the_geom );
/**
* \brief For usage in problem solving algorithm
*/
class Feats
{
public:
FeaturePart *feature;
PointSet *shape;
double priority;
int nblp;
LabelPosition **lPos;
};
typedef struct _elementary_transformation
{
int feat;
int old_label;
int new_label;
} ElemTrans;
struct Point
{
double x, y;
};
#define EPSILON 1e-9
inline int max( int a, int b )
{ return a > b ? a : b; }
inline double max( double a, double b )
{ return a > b ? a : b; }
inline int min( int a, int b )
{ return a < b ? a : b; }
inline double min( double a, double b )
{ return a < b ? a : b; }
inline double vabs( double x )
{ return x >= 0 ? x : -x; }
inline double degree2meter( double delta_deg )
{
double lat = delta_deg * 0.5;
const static double rads = ( 4.0 * atan( 1.0 ) ) / 180.0;
double a = cos( lat * rads );
a = a * a;
double c = 2.0 * atan2( sqrt( a ), sqrt( 1.0 - a ) );
const static double ra = 6378000; // [m]
const static double e = 0.0810820288;
double radius = ra * ( 1.0 - e * e ) / pow( 1.0 - e * e * sin( lat * rads ) * sin( lat * rads ), 1.5 );
double meters = ( delta_deg ) / 180.0 * radius * c; // [m]
return meters;
}
inline double unit_convert( double x, Units from, Units to, int dpi, double scale, double delta_canvas_width )
{
/* nothing to convert */
if ( from == to )
{
return x;
}
switch ( from )
{
case pal::PIXEL:
switch ( to )
{
case pal::METER:
return (( x / double( dpi ) ) * 0.0254 ) * scale;
case pal::FOOT:
return (( x / double( dpi ) )*12 ) * scale;
case pal::DEGREE:
{
double iw = degree2meter( delta_canvas_width ) * 39.3700787;
return ( x * delta_canvas_width * scale ) / ( iw * dpi );
}
default:
fprintf( stderr, "Target unit undefined\n" );
return 0.0;
}
break;
case pal::METER:
switch ( to )
{
case pal::PIXEL:
return ( x*dpi ) / ( 2.54*scale );
case pal::FOOT:
return x / 0.3048;
case pal::DEGREE:
{
double mw = degree2meter( delta_canvas_width );
return ( x * delta_canvas_width ) / mw;
}
default:
fprintf( stderr, "Target unit undefined\n" );
return 0.0;
}
break;
case pal::FOOT:
switch ( to )
{
case pal::PIXEL:
return ( x*dpi ) / ( 12*scale );
case pal::METER:
return x*0.3048;
case pal::DEGREE:
{
double iw = degree2meter( delta_canvas_width ) * 39.3700787;
return ( x * delta_canvas_width ) / iw;
}
default:
fprintf( stderr, "Target unit undefined\n" );
return 0.0;
}
break;
case pal::DEGREE:
switch ( to )
{
case pal::PIXEL:
fprintf( stderr, "Degree to pixel not yet implemented\n" );
break;
case pal::METER:
fprintf( stderr, "Degree to meter not yet implemented\n" );
break;
case pal::FOOT:
fprintf( stderr, "Degree to foot not yet implemented\n" );
break;
default:
fprintf( stderr, "Target unit undefined\n" );
return 0.0;
}
break;
default:
fprintf( stderr, "Source unit undefined" );
return 0.0;
}
fprintf( stderr, "Unable to convert. Unknown units\n" );
return 0.0;
}
/* From meters to PostScript Point */
inline void convert2pt( int *x, double scale, int dpi )
{
*x = ( int )((( double ) * x / scale ) * 39.3700787402 * dpi + 0.5 );
}
inline int convert2pt( double x, double scale, int dpi )
{
return ( int )(( x / scale ) * 39.3700787402 * dpi + 0.5 );
}
void sort( double* heap, int* x, int* y, int N );
inline bool intCompare( int a, int b )
{
return a == b;
}
inline bool strCompare( char * a, char * b )
{
return strcmp( a, b ) == 0;
}
inline bool ptrLPosCompare( LabelPosition * a, LabelPosition * b )
{
return a == b;
}
inline bool ptrPSetCompare( PointSet * a, PointSet * b )
{
return a == b;
}
inline bool ptrFeatureCompare( Feature * a, Feature * b )
{
return a == b;
}
inline bool ptrFeaturePartCompare( FeaturePart * a, FeaturePart * b )
{
return a == b;
}
inline bool ptrFeatsCompare( Feats * a, Feats * b )
{
return a == b;
}
inline bool ptrLayerCompare( Layer * a, Layer * b )
{
return a == b;
}
inline bool ptrETCompare( ElemTrans * a, ElemTrans * b )
{
return a == b;
}
/**
* \brief Sort an array of pointers
* \param items arays of pointers to sort
* \param N number of items
* \param greater function to compare two items
**/
void sort( void** items, int N, bool ( *greater )( void *l, void *r ) );
void tabcpy( int n, const int* const x, const int* const y,
const double* const prob, int *cx, int *cy, double *p );
} // namespace
#endif