/
labelposition.h
270 lines (209 loc) · 7.35 KB
/
labelposition.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
/*
* 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/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef _LABELPOSITION_H
#define _LABELPOSITION_H
#include <fstream>
#include "rtree.hpp"
namespace pal
{
class FeaturePart;
class Pal;
class Label;
/**
* \brief LabelPositon is a candidate feature label position
*/
class CORE_EXPORT LabelPosition
{
friend class CostCalculator;
friend class PolygonCostCalculator;
protected:
int id;
double cost;
FeaturePart *feature;
// bug # 1 (maxence 10/23/2008)
int probFeat;
int nbOverlap;
double x[4], y[4];
double alpha;
double w;
double h;
LabelPosition* nextPart;
int partId;
//True if label direction is the same as line / polygon ring direction.
//Could be used by the application to draw a directional arrow ('<' or '>')
//if the layer arrangement is P_LINE
bool reversed;
bool upsideDown;
bool isInConflictSinglePart( LabelPosition* lp );
bool isInConflictMultiPart( LabelPosition* lp );
public:
/**
* \brief create a new LabelPosition
*
* \param id id of this labelposition
* \param x1 down-left x coordinate
* \param y1 down-left y coordinate
* \param w label width
* \param h label height
* \param alpha rotation in rad
* \param cost geographic cost
* \param feature labelpos owners
*/
LabelPosition( int id, double x1, double y1,
double w, double h,
double alpha, double cost,
FeaturePart *feature, bool isReversed = false );
/** copy constructor */
LabelPosition( const LabelPosition& other );
~LabelPosition() { delete nextPart; }
/**
* \brief Is the labelposition in the bounding-box ? (intersect or inside????)
*
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
*/
bool isIn( double *bbox );
/**
* \brief Is the labelposition intersect the bounding-box ?
*
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
*/
bool isIntersect( double *bbox );
/**
* \brief Is the labelposition inside the bounding-box ?
*
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
*/
bool isInside( double *bbox );
/**
* \brief Check whether or not this overlap with another labelPosition
*
* \param ls other labelposition
* \return true or false
*/
bool isInConflict( LabelPosition *ls );
/** return bounding box - amin: xmin,ymin - amax: xmax,ymax */
void getBoundingBox( double amin[2], double amax[2] ) const;
/** get distance from this label to a point. If point lies inside, returns negative number. */
double getDistanceToPoint( double xp, double yp );
/** returns true if this label crosses the specified line */
bool isBorderCrossingLine( PointSet* feat );
/** returns number of intersections with polygon (testing border and center) */
int getNumPointsInPolygon( int npol, double *xp, double *yp );
/** shift the label by specified offset */
void offsetPosition( double xOffset, double yOffset );
/** \brief return id
* \return id
*/
int getId() const;
/** \brief return the feature corresponding to this labelposition
* \return the feature
*/
FeaturePart * getFeaturePart();
double getNumOverlaps() const { return nbOverlap; }
void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
int getProblemFeatureId() const { return probFeat; }
/** set problem feature ID and assigned label candidate ID.
* called from pal.cpp during extraction */
void setProblemIds( int probFid, int lpId )
{
probFeat = probFid; id = lpId;
if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
}
/** return pointer to layer's name. used for stats */
char* getLayerName() const;
/**
* \brief get the position geographical cost
* \return geographical cost
*/
double getCost() const;
/** Modify candidate's cost */
void setCost( double newCost ) { cost = newCost; }
/** Make sure the cost is less than 1 */
void validateCost();
/**
* \brief get the down-left x coordinate
* \return x coordinate
*/
double getX( int i = 0 ) const;
/**
* \brief get the down-left y coordinate
* \return y coordinate
*/
double getY( int i = 0 ) const;
double getWidth() const { return w; }
double getHeight() const { return h; }
/**
* \brief get alpha
* \return alpha to rotate text (in rad)
*/
double getAlpha() const;
bool getReversed() const { return reversed; }
bool getUpsideDown() const { return upsideDown; }
void print();
LabelPosition* getNextPart() const { return nextPart; }
void setNextPart( LabelPosition* next ) { nextPart = next; }
// -1 if not multi-part
int getPartId() const { return partId; }
void setPartId( int id ) { partId = id; }
void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
typedef struct
{
double scale;
Pal* pal;
PointSet *obstacle;
} PruneCtx;
/** Check whether the candidate in ctx overlap with obstacle feat */
static bool pruneCallback( LabelPosition *lp, void *ctx );
// for sorting
static bool costShrink( void *l, void *r );
static bool costGrow( void *l, void *r );
// for counting number of overlaps
typedef struct
{
LabelPosition *lp;
int *nbOv;
double *cost;
double *inactiveCost;
//int *feat;
} CountContext;
/*
* count overlap, ctx = p_lp
*/
static bool countOverlapCallback( LabelPosition *lp, void *ctx );
static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
// for polygon cost calculation
static bool polygonObstacleCallback( PointSet *feat, void *ctx );
};
} // end namespac
#endif