QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
labelposition.cpp
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 #define _CRT_SECURE_NO_DEPRECATE
35 
36 #include <iostream>
37 #include <fstream>
38 #include <cmath>
39 #include <cstring>
40 #include <cfloat>
41 
42 #include <pal/layer.h>
43 #include <pal/pal.h>
44 
45 #include "costcalculator.h"
46 #include "feature.h"
47 #include "geomfunction.h"
48 #include "labelposition.h"
49 
50 #ifndef M_PI
51 #define M_PI 3.1415926535897931159979634685
52 #endif
53 
54 
55 namespace pal
56 {
57  LabelPosition::LabelPosition( int id, double x1, double y1, double w, double h, double alpha, double cost, FeaturePart *feature, bool isReversed, Quadrant quadrant )
58  : id( id ), cost( cost ), feature( feature ), probFeat( 0 ), nbOverlap( 0 ), alpha( alpha ), w( w ), h( h ), nextPart( NULL ), partId( -1 ), reversed( isReversed ), upsideDown( false ),
59  quadrant( quadrant )
60  {
61 
62  // alpha take his value bw 0 and 2*pi rad
63  while ( this->alpha > 2*M_PI )
64  this->alpha -= 2 * M_PI;
65 
66  while ( this->alpha < 0 )
67  this->alpha += 2 * M_PI;
68 
69  double beta = this->alpha + ( M_PI / 2 );
70 
71  double dx1, dx2, dy1, dy2;
72 
73  double tx, ty;
74 
75  dx1 = cos( this->alpha ) * w;
76  dy1 = sin( this->alpha ) * w;
77 
78  dx2 = cos( beta ) * h;
79  dy2 = sin( beta ) * h;
80 
81  x[0] = x1;
82  y[0] = y1;
83 
84  x[1] = x1 + dx1;
85  y[1] = y1 + dy1;
86 
87  x[2] = x1 + dx1 + dx2;
88  y[2] = y1 + dy1 + dy2;
89 
90  x[3] = x1 + dx2;
91  y[3] = y1 + dy2;
92 
93  // upside down ? (curved labels are always correct)
94  if ( feature->getLayer()->getArrangement() != P_CURVED &&
95  this->alpha > M_PI / 2 && this->alpha <= 3*M_PI / 2 )
96  {
97  bool uprightLabel = false;
98 
99  switch ( feature->getLayer()->getUpsidedownLabels() )
100  {
101  case Layer::Upright:
102  uprightLabel = true;
103  break;
104  case Layer::ShowDefined:
105  // upright only dynamic labels
106  if ( !feature->getFixedRotation() || ( !feature->getFixedPosition() && feature->getLabelAngle() == 0.0 ) )
107  {
108  uprightLabel = true;
109  }
110  break;
111  case Layer::ShowAll:
112  break;
113  default:
114  uprightLabel = true;
115  }
116 
117  if ( uprightLabel )
118  {
119  tx = x[0];
120  ty = y[0];
121 
122  x[0] = x[2];
123  y[0] = y[2];
124 
125  x[2] = tx;
126  y[2] = ty;
127 
128  tx = x[1];
129  ty = y[1];
130 
131  x[1] = x[3];
132  y[1] = y[3];
133 
134  x[3] = tx;
135  y[3] = ty;
136 
137  if ( this->alpha < M_PI )
138  this->alpha += M_PI;
139  else
140  this->alpha -= M_PI;
141 
142  // labels with text shown upside down are not classified as upsideDown,
143  // only those whose boundary points have been inverted
144  upsideDown = true;
145  }
146  }
147  }
148 
150  {
151  id = other.id;
152  cost = other.cost;
153  feature = other.feature;
154  probFeat = other.probFeat;
155  nbOverlap = other.nbOverlap;
156 
157  memcpy( x, other.x, sizeof( double )*4 );
158  memcpy( y, other.y, sizeof( double )*4 );
159  alpha = other.alpha;
160  w = other.w;
161  h = other.h;
162 
163  if ( other.nextPart )
164  nextPart = new LabelPosition( *other.nextPart );
165  else
166  nextPart = NULL;
167  partId = other.partId;
168  upsideDown = other.upsideDown;
169  reversed = other.reversed;
170  quadrant = other.quadrant;
171  }
172 
173  bool LabelPosition::isIn( double *bbox )
174  {
175  int i;
176 
177  for ( i = 0; i < 4; i++ )
178  {
179  if ( x[i] >= bbox[0] && x[i] <= bbox[2] &&
180  y[i] >= bbox[1] && y[i] <= bbox[3] )
181  return true;
182  }
183 
184  if ( nextPart )
185  return nextPart->isIn( bbox );
186  else
187  return false;
188 
189  }
190 
191  bool LabelPosition::isIntersect( double *bbox )
192  {
193  int i;
194 
195  for ( i = 0; i < 4; i++ )
196  {
197  if ( x[i] >= bbox[0] && x[i] <= bbox[2] &&
198  y[i] >= bbox[1] && y[i] <= bbox[3] )
199  return true;
200  }
201 
202  if ( nextPart )
203  return nextPart->isIntersect( bbox );
204  else
205  return false;
206  }
207 
208  bool LabelPosition::isInside( double *bbox )
209  {
210  for ( int i = 0; i < 4; i++ )
211  {
212  if ( !( x[i] >= bbox[0] && x[i] <= bbox[2] &&
213  y[i] >= bbox[1] && y[i] <= bbox[3] ) )
214  return false;
215  }
216 
217  if ( nextPart )
218  return nextPart->isInside( bbox );
219  else
220  return true;
221 
222  }
223 
225  {
226  std::cout << feature->getLayer()->getName() << "/" << feature->getUID() << "/" << id;
227  std::cout << " cost: " << cost;
228  std::cout << " alpha" << alpha << std::endl;
229  std::cout << x[0] << ", " << y[0] << std::endl;
230  std::cout << x[1] << ", " << y[1] << std::endl;
231  std::cout << x[2] << ", " << y[2] << std::endl;
232  std::cout << x[3] << ", " << y[3] << std::endl;
233  std::cout << std::endl;
234  }
235 
237  {
238  if ( this->probFeat == lp->probFeat ) // bugfix #1
239  return false; // always overlaping itself !
240 
241  if ( nextPart == NULL && lp->nextPart == NULL )
242  return isInConflictSinglePart( lp );
243  else
244  return isInConflictMultiPart( lp );
245  }
246 
248  {
249  // TODO: add bounding box test to possibly avoid cross product calculation
250 
251  int i, i2, j;
252  int d1, d2;
253  double cp1, cp2;
254 
255  for ( i = 0; i < 4; i++ )
256  {
257  i2 = ( i + 1 ) % 4;
258  d1 = -1;
259  d2 = -1;
260 
261  for ( j = 0; j < 4; j++ )
262  {
263  cp1 = cross_product( x[i], y[i], x[i2], y[i2], lp->x[j], lp->y[j] );
264  if ( cp1 > 0 )
265  {
266  d1 = 1;
267  }
268  cp2 = cross_product( lp->x[i], lp->y[i],
269  lp->x[i2], lp->y[i2],
270  x[j], y[j] );
271 
272  if ( cp2 > 0 )
273  {
274  d2 = 1;
275  }
276  }
277 
278  if ( d1 == -1 || d2 == -1 ) // disjoint
279  return false;
280  }
281  return true;
282  }
283 
285  {
286  // check all parts against all parts of other one
287  LabelPosition* tmp1 = this;
288  while ( tmp1 )
289  {
290  // check tmp1 against parts of other label
291  LabelPosition* tmp2 = lp;
292  while ( tmp2 )
293  {
294  if ( tmp1->isInConflictSinglePart( tmp2 ) )
295  return true;
296  tmp2 = tmp2->nextPart;
297  }
298 
299  tmp1 = tmp1->nextPart;
300  }
301  return false; // no conflict found
302  }
303 
304  void LabelPosition::offsetPosition( double xOffset, double yOffset )
305  {
306  for ( int i = 0; i < 4; i++ )
307  {
308  x[i] += xOffset;
309  y[i] += yOffset;
310  }
311 
312  if ( nextPart )
313  nextPart->offsetPosition( xOffset, yOffset );
314  }
315 
316 
318  {
319  return id;
320  }
321 
322  double LabelPosition::getX( int i ) const
323  {
324  return ( i >= 0 && i < 4 ? x[i] : -1 );
325  }
326 
327  double LabelPosition::getY( int i ) const
328  {
329  return ( i >= 0 && i < 4 ? y[i] : -1 );
330  }
331 
332  double LabelPosition::getAlpha() const
333  {
334  return alpha;
335  }
336 
337  double LabelPosition::getCost() const
338  {
339  return cost;
340  }
341 
343  {
344  if ( cost >= 1 )
345  {
346  std::cout << " Warning: lp->cost == " << cost << " (from feat: " << feature->getUID() << "/" << getLayerName() << ")" << std::endl;
347  cost -= int ( cost ); // label cost up to 1
348  }
349  }
350 
352  {
353  return feature;
354  }
355 
356  void LabelPosition::getBoundingBox( double amin[2], double amax[2] ) const
357  {
358  if ( nextPart )
359  {
360  //std::cout << "using next part" <<
361  nextPart->getBoundingBox( amin, amax );
362  }
363  else
364  {
365  amin[0] = DBL_MAX;
366  amax[0] = -DBL_MAX;
367  amin[1] = DBL_MAX;
368  amax[1] = -DBL_MAX;
369  }
370  for ( int c = 0; c < 4; c++ )
371  {
372  if ( x[c] < amin[0] )
373  amin[0] = x[c];
374  if ( x[c] > amax[0] )
375  amax[0] = x[c];
376  if ( y[c] < amin[1] )
377  amin[1] = y[c];
378  if ( y[c] > amax[1] )
379  amax[1] = y[c];
380  }
381  }
382 
384  {
385  return feature->getLayer()->name;
386  }
387 
388  bool LabelPosition::costShrink( void *l, void *r )
389  {
390  return (( LabelPosition* ) l )->cost < (( LabelPosition* ) r )->cost;
391  }
392 
393  bool LabelPosition::costGrow( void *l, void *r )
394  {
395  return (( LabelPosition* ) l )->cost > (( LabelPosition* ) r )->cost;
396  }
397 
398 
400  {
402 
403  LabelPosition *lp = pCost->getLabel();
404  if (( feat == lp->feature ) || ( feat->getHoleOf() && feat->getHoleOf() != lp->feature ) )
405  {
406  return true;
407  }
408 
409  pCost->update( feat );
410 
411  return true;
412  }
413 
414 
416  {
417  double amin[2];
418  double amax[2];
419  getBoundingBox( amin, amax );
420  index->Remove( amin, amax, this );
421  }
422 
423 
425  {
426  double amin[2];
427  double amax[2];
428  getBoundingBox( amin, amax );
429  index->Insert( amin, amax, this );
430  }
431 
432 
434 
436  {
437  PointSet *feat = (( PruneCtx* ) ctx )->obstacle;
438 
439  if (( feat == lp->feature ) || ( feat->getHoleOf() && feat->getHoleOf() != lp->feature ) )
440  {
441  return true;
442  }
443 
445 
446  return true;
447  }
448 
449 
451  {
452  LabelPosition *lp2 = ( LabelPosition* ) ctx;
453 
454  //std::cerr << "checking " << lp2->getFeature()->getUID() << " x " << lp->getFeature()->getUID() << std::endl;
455  if ( lp2->isInConflict( lp ) )
456  {
457  //std::cerr << "conflict!" << std::endl;
458  lp2->nbOverlap++;
459  }
460 
461  return true;
462  }
463 
465  {
466  LabelPosition *lp2 = (( CountContext* ) ctx )->lp;
467  double *cost = (( CountContext* ) ctx )->cost;
468  //int *feat = ((CountContext*)ctx)->feat;
469  int *nbOv = (( CountContext* ) ctx )->nbOv;
470  double *inactiveCost = (( CountContext* ) ctx )->inactiveCost;
471  if ( lp2->isInConflict( lp ) )
472  {
473 #ifdef _DEBUG_FULL_
474  std::cout << "count overlap : " << lp->id << "<->" << lp2->id << std::endl;
475 #endif
476  ( *nbOv ) ++;
477  *cost += inactiveCost[lp->probFeat] + lp->getCost();
478 
479  }
480 
481  return true;
482  }
483 
484 
486  {
487  LabelPosition *lp2 = ( LabelPosition * ) ctx;
488 
489  if ( lp2->isInConflict( lp ) )
490  {
491  //std::cout << " hit !" << std::endl;
492  lp->nbOverlap--;
493  lp2->nbOverlap--;
494  }
495 
496  return true;
497  }
498 
499 
500 
501  double LabelPosition::getDistanceToPoint( double xp, double yp )
502  {
503  int i;
504  int j;
505 
506  double mx[4];
507  double my[4];
508 
509  double dist_min = DBL_MAX;
510  double dist;
511 
512  for ( i = 0; i < 4; i++ )
513  {
514  j = ( i + 1 ) % 4;
515  mx[i] = ( x[i] + x[j] ) / 2.0;
516  my[i] = ( y[i] + y[j] ) / 2.0;
517  }
518 
519  if ( vabs( cross_product( mx[0], my[0], mx[2], my[2], xp, yp ) / h ) < w / 2 )
520  {
521  dist = cross_product( x[1], y[1], x[0], y[0], xp, yp ) / w;
522  if ( vabs( dist ) < vabs( dist_min ) )
523  dist_min = dist;
524 
525  dist = cross_product( x[3], y[3], x[2], y[2], xp, yp ) / w;
526  if ( vabs( dist ) < vabs( dist_min ) )
527  dist_min = dist;
528  }
529 
530  if ( vabs( cross_product( mx[1], my[1], mx[3], my[3], xp, yp ) / w ) < h / 2 )
531  {
532  dist = cross_product( x[2], y[2], x[1], y[1], xp, yp ) / h;
533  if ( vabs( dist ) < vabs( dist_min ) )
534  dist_min = dist;
535 
536  dist = cross_product( x[0], y[0], x[3], y[3], xp, yp ) / h;
537  if ( vabs( dist ) < vabs( dist_min ) )
538  dist_min = dist;
539  }
540 
541  for ( i = 0; i < 4; i++ )
542  {
543  dist = dist_euc2d( x[i], y[i], xp, yp );
544  if ( vabs( dist ) < vabs( dist_min ) )
545  dist_min = dist;
546  }
547 
548  if ( nextPart && dist_min > 0 )
549  return min( dist_min, nextPart->getDistanceToPoint( xp, yp ) );
550 
551  return dist_min;
552  }
553 
554 
556  {
557  double ca, cb;
558  for ( int i = 0; i < 4; i++ )
559  {
560  for ( int j = 0; j < feat->getNumPoints() - 1; j++ )
561  {
562  ca = cross_product( x[i], y[i], x[( i+1 ) %4], y[( i+1 ) %4],
563  feat->x[j], feat->y[j] );
564  cb = cross_product( x[i], y[i], x[( i+1 ) %4], y[( i+1 ) %4],
565  feat->x[j+1], feat->y[j+1] );
566 
567  if (( ca < 0 && cb > 0 ) || ( ca > 0 && cb < 0 ) )
568  {
569  ca = cross_product( feat->x[j], feat->y[j], feat->x[j+1], feat->y[j+1],
570  x[i], y[i] );
571  cb = cross_product( feat->x[j], feat->y[j], feat->x[j+1], feat->y[j+1],
572  x[( i+1 ) %4], y[( i+1 ) %4] );
573  if (( ca < 0 && cb > 0 ) || ( ca > 0 && cb < 0 ) )
574  return true;
575  }
576  }
577  }
578 
579  if ( nextPart )
580  return nextPart->isBorderCrossingLine( feat );
581 
582  return false;
583  }
584 
585  int LabelPosition::getNumPointsInPolygon( int npol, double *xp, double *yp )
586  {
587  int a, k, count = 0;
588  double px, py;
589 
590  // cheack each corner
591  for ( k = 0; k < 4; k++ )
592  {
593  px = x[k];
594  py = y[k];
595 
596  for ( a = 0; a < 2; a++ ) // and each middle of segment
597  {
598  if ( isPointInPolygon( npol, xp, yp, px, py ) )
599  count++;
600  px = ( x[k] + x[( k+1 ) %4] ) / 2.0;
601  py = ( y[k] + y[( k+1 ) %4] ) / 2.0;
602  }
603  }
604 
605  px = ( x[0] + x[2] ) / 2.0;
606  py = ( y[0] + y[2] ) / 2.0;
607 
608  // and the label center
609  if ( isPointInPolygon( npol, xp, yp, px, py ) )
610  count += 4; // virtually 4 points
611 
612  // TODO: count with nextFeature
613 
614  return count;
615  }
616 
617 } // end namespace
618 
FeaturePart * feature
Definition: labelposition.h:80
static unsigned index
FeaturePart * getFeaturePart()
return the feature corresponding to this labelposition
bool isBorderCrossingLine(PointSet *feat)
returns true if this label crosses the specified line
PointSet * getHoleOf()
returns NULL if this isn't a hole.
Definition: pointset.h:175
double getCost() const
get the position geographical cost
bool getFixedRotation()
Definition: feature.h:307
void validateCost()
Make sure the cost is less than 1.
static bool removeOverlapCallback(LabelPosition *lp, void *ctx)
static bool countOverlapCallback(LabelPosition *lp, void *ctx)
void offsetPosition(double xOffset, double yOffset)
shift the label by specified offset
static bool costGrow(void *l, void *r)
bool isInConflictMultiPart(LabelPosition *lp)
Layer * getLayer()
return the layer that feature belongs to
Definition: feature.cpp:267
char * name
Definition: layer.h:88
static void addObstacleCostPenalty(LabelPosition *lp, PointSet *feat)
increase candidate's cost according to its collision with passed feature
bool isIn(double *bbox)
Is the labelposition in the bounding-box ? (intersect or inside????)
bool isPointInPolygon(int npol, double *xp, double *yp, double x, double y)
bool isInConflict(LabelPosition *ls)
Check whether or not this overlap with another labelPosition.
int getId() const
return id
char * getLayerName() const
return pointer to layer's name.
UpsideDownLabels getUpsidedownLabels() const
Definition: layer.h:296
void getBoundingBox(double amin[2], double amax[2]) const
return bounding box - amin: xmin,ymin - amax: xmax,ymax
bool isIntersect(double *bbox)
Is the labelposition intersect the bounding-box ?
static bool polygonObstacleCallback(PointSet *feat, void *ctx)
double getY(int i=0) const
get the down-left y coordinate
double getAlpha() const
get alpha
LabelPosition * nextPart
Definition: labelposition.h:92
double * x
Definition: pointset.h:102
bool getFixedPosition()
Definition: feature.h:309
double getDistanceToPoint(double xp, double yp)
get distance from this label to a point.
bool isInConflictSinglePart(LabelPosition *lp)
double dist_euc2d(double x1, double y1, double x2, double y2)
Definition: geomfunction.h:56
void update(PointSet *pset)
void removeFromIndex(RTree< LabelPosition *, double, 2, double > *index)
static bool countFullOverlapCallback(LabelPosition *lp, void *ctx)
void insertIntoIndex(RTree< LabelPosition *, double, 2, double > *index)
double cross_product(double x1, double y1, double x2, double y2, double x3, double y3)
Definition: geomfunction.h:51
const char * getUID()
get the unique id of the feature
Definition: feature.cpp:273
Main class to handle feature.
Definition: feature.h:138
int getNumPoints() const
Definition: pointset.h:177
double getLabelAngle()
Definition: feature.h:308
Arrangement getArrangement()
get arrangement policy
Definition: layer.cpp:152
static bool pruneCallback(LabelPosition *lp, void *ctx)
Check whether the candidate in ctx overlap with obstacle feat.
int min(int a, int b)
Definition: util.h:93
double * y
Definition: pointset.h:103
const char * getName()
get layer's name
Definition: layer.cpp:147
LabelPosition(int id, double x1, double y1, double w, double h, double alpha, double cost, FeaturePart *feature, bool isReversed=false, Quadrant quadrant=QuadrantOver)
create a new LabelPosition
#define M_PI
LabelPosition is a candidate feature label position.
Definition: labelposition.h:53
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:63
double getX(int i=0) const
get the down-left x coordinate
Data structure to compute polygon's candidates costs.
bool isInside(double *bbox)
Is the labelposition inside the bounding-box ?
int getNumPointsInPolygon(int npol, double *xp, double *yp)
returns number of intersections with polygon (testing border and center)
static bool costShrink(void *l, void *r)
LabelPosition::Quadrant quadrant
double vabs(double x)
Definition: util.h:99