/
labelposition.h
314 lines (249 loc) · 9.13 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* 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 LABELPOSITION_H
#define LABELPOSITION_H
#include "pointset.h"
#include "rtree.hpp"
#include <fstream>
namespace pal
{
class FeaturePart;
class Pal;
class Label;
/**
* \brief LabelPosition is a candidate feature label position
* \class pal::LabelPosition
* \note not available in Python bindings
*/
class CORE_EXPORT LabelPosition : public PointSet
{
friend class CostCalculator;
friend class PolygonCostCalculator;
public:
/**
* \brief Position of label candidate relative to feature.
*/
enum Quadrant
{
QuadrantAboveLeft,
QuadrantAbove,
QuadrantAboveRight,
QuadrantLeft,
QuadrantOver,
QuadrantRight,
QuadrantBelowLeft,
QuadrantBelow,
QuadrantBelowRight
};
/**
* \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
* \param isReversed label is reversed
* \param quadrant relative position of label to feature
*/
LabelPosition( int id, double x1, double y1,
double w, double h,
double alpha, double cost,
FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
/** 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 ) const;
/** Returns true if this label crosses the specified line */
bool crossesLine( PointSet* line ) const;
/** Returns true if this label crosses the boundary of the specified polygon */
bool crossesBoundary( PointSet* polygon ) const;
/** Returns cost of position intersection with polygon (testing area of intersection and center).
* Cost ranges between 0 and 12, with extra cost if center of label position is covered.
*/
int polygonIntersectionCost( PointSet* polygon ) const;
/** 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 );
}
/** Returns the candidate label position's geographical cost.
* @see setCost
*/
double cost() const { return mCost; }
/** Sets the candidate label position's geographical cost.
* @param newCost new cost for position
* @see cost
*/
void setCost( double newCost ) { mCost = newCost; }
/** Sets whether the position is marked as conflicting with an obstacle feature.
* @param conflicts set to true to mark candidate as being in conflict
* @note This method applies to all label parts for the candidate position.
* @see conflictsWithObstacle
*/
void setConflictsWithObstacle( bool conflicts );
/** Returns whether the position is marked as conflicting with an obstacle feature.
* @see setConflictsWithObstacle
*/
bool conflictsWithObstacle() const { return mHasObstacleConflict; }
/** 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; }
Quadrant getQuadrant() const { return quadrant; }
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
{
Pal* pal;
FeaturePart *obstacle;
} PruneCtx;
/** Check whether the candidate in ctx overlap with obstacle feat */
static bool pruneCallback( LabelPosition *candidatePosition, 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( pal::FeaturePart *obstacle, void *ctx );
protected:
int id;
FeaturePart *feature;
// bug # 1 (maxence 10/23/2008)
int probFeat;
int nbOverlap;
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;
LabelPosition::Quadrant quadrant;
bool isInConflictSinglePart( LabelPosition* lp );
bool isInConflictMultiPart( LabelPosition* lp );
private:
double mCost;
bool mHasObstacleConflict;
/** Calculates the total number of parts for this label position
*/
int partCount() const;
/** Calculates the polygon intersection cost for a single label position part
* @returns double between 0 - 12
*/
double polygonIntersectionCostForParts( PointSet* polygon ) const;
};
} // end namespace
#endif