QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
problem.h
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 #ifndef PAL_PROBLEM_H
31 #define PAL_PROBLEM_H
32 
33 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include <list>
38 #include <QList>
39 #include "palrtree.h"
40 #include <memory>
41 #include <vector>
42 
43 namespace pal
44 {
45 
46  class LabelPosition;
47  class Label;
48 
55  struct Chain
56  {
57  int degree;
58  double delta;
59  int *feat = nullptr;
60  int *label = nullptr;
61  };
62 
69  class CORE_EXPORT Problem
70  {
71  friend class Pal;
72 
73  public:
74 
80  Problem( const QgsRectangle &extent );
81 
82  //Problem(char *lorena_file, bool displayAll);
83 
85 
87  Problem( const Problem &other ) = delete;
89  Problem &operator=( const Problem &other ) = delete;
90 
96  void addCandidatePosition( std::unique_ptr< LabelPosition > position ) { mLabelPositions.emplace_back( std::move( position ) ); }
97 
101  std::size_t featureCount() const { return mFeatureCount; }
102 
106  int featureCandidateCount( int feature ) const { return mFeatNbLp[feature]; }
107 
111  LabelPosition *featureCandidate( int feature, int candidate ) const { return mLabelPositions[ mFeatStartId[feature] + candidate ].get(); }
112 
113  void reduce();
114 
118  void chain_search();
119 
132  QList<LabelPosition *> getSolution( bool returnInactive, QList<LabelPosition *> *unlabeled = nullptr );
133 
134  /* useful only for postscript post-conversion*/
135  //void toFile(char *label_file);
136 
137  void init_sol_falp();
138 
145  std::vector< std::unique_ptr< LabelPosition > > *positionsWithNoCandidates()
146  {
147  return &mPositionsWithNoCandidates;
148  }
149 
153  PalRtree< LabelPosition > &allCandidatesIndex() { return mAllCandidatesIndex; }
154 
155  private:
156 
160  int mLayerCount = 0;
161 
165  QStringList labelledLayersName;
166 
170  int mTotalCandidates = 0;
171 
175  int mAllNblp = 0;
176 
180  std::size_t mFeatureCount = 0;
181 
185  bool mDisplayAll = false;
186 
190  double mMapExtentBounds[4] = {0, 0, 0, 0};
191 
192  std::vector< std::unique_ptr< LabelPosition > > mLabelPositions;
193 
194  PalRtree<LabelPosition> mAllCandidatesIndex;
195  PalRtree<LabelPosition> mActiveCandidatesIndex;
196 
197  std::vector< std::unique_ptr< LabelPosition > > mPositionsWithNoCandidates;
198 
199  std::vector< int > mFeatStartId;
200  std::vector< int > mFeatNbLp;
201  std::vector< double > mInactiveCost;
202 
203  class Sol
204  {
205  public:
206 
208  std::vector< int > activeLabelIds;
209 
210  double totalCost = 0;
211 
212  void init( std::size_t featureCount )
213  {
214  activeLabelIds.resize( featureCount, -1 );
215  totalCost = 0;
216  }
217  };
218 
219  Sol mSol;
220  double mNbOverlap = 0.0;
221 
222  Chain *chain( int seed );
223 
224  Pal *pal = nullptr;
225 
226  void solution_cost();
227  };
228 
229 } // namespace
230 
231 #endif
pal::Problem::featureCount
std::size_t featureCount() const
Returns the total number of features considered during the labeling problem.
Definition: problem.h:101
pal::Chain::label
int * label
Definition: problem.h:60
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
pal::Problem
Representation of a labeling problem.
Definition: problem.h:70
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
pal
Definition: qgsdiagramrenderer.h:49
pal::Problem::addCandidatePosition
void addCandidatePosition(std::unique_ptr< LabelPosition > position)
Adds a candidate label position to the problem.
Definition: problem.h:96
pal::Problem::positionsWithNoCandidates
std::vector< std::unique_ptr< LabelPosition > > * positionsWithNoCandidates()
Returns a reference to the list of label positions which correspond to features with no candidates.
Definition: problem.h:145
palrtree.h
pal::Problem::allCandidatesIndex
PalRtree< LabelPosition > & allCandidatesIndex()
Returns the index containing all label candidates.
Definition: problem.h:153
pal::Chain
Definition: problem.h:56
pal::Chain::feat
int * feat
Definition: problem.h:59
pal::Problem::~Problem
~Problem()
pal::Chain::degree
int degree
Definition: problem.h:57
Sol
Definition: problem.h:47
pal::Problem::operator=
Problem & operator=(const Problem &other)=delete
Problem cannot be copied.
pal::Chain::delta
double delta
Definition: problem.h:58
PalRtree
A rtree spatial index for use in the pal labeling engine.
Definition: palrtree.h:36
pal::Pal
Main Pal labeling class.
Definition: pal.h:80
pal::Problem::featureCandidate
LabelPosition * featureCandidate(int feature, int candidate) const
Returns the candidate corresponding to the specified feature and candidate index.
Definition: problem.h:111
pal::Problem::featureCandidateCount
int featureCandidateCount(int feature) const
Returns the number of candidates generated for the feature at the specified index.
Definition: problem.h:106
pal::Problem::Problem
Problem(const Problem &other)=delete
Problem cannot be copied.