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