Skip to content

Commit 6b93874

Browse files
author
wonder
committed
Produce several candidates when using curved line labels
git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11178 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 3ddaa11 commit 6b93874

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

src/core/pal/feature.cpp

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ namespace pal
635635
index++;
636636
if (index >= path_positions->nbPoints) // Bail out if we run off the end of the shape
637637
{
638-
std::cerr << "err5" << std::endl;
638+
//std::cerr << "err5" << std::endl;
639639
return NULL;
640640
}
641641
new_x = path_positions->x[index];
@@ -644,7 +644,7 @@ namespace pal
644644
dy = new_y - old_y;
645645
segment_length = path_distances[index];
646646

647-
std::cerr << "-> " << sqrt(pow(start_x - new_x,2) + pow(start_y - new_y,2)) << " vs " << ci.width << std::endl;
647+
//std::cerr << "-> " << sqrt(pow(start_x - new_x,2) + pow(start_y - new_y,2)) << " vs " << ci.width << std::endl;
648648

649649
} while (sqrt(pow(start_x - new_x,2) + pow(start_y - new_y,2)) < ci.width); // Distance from start_ to new_
650650

@@ -689,7 +689,7 @@ namespace pal
689689
render_angle += M_PI;
690690
}
691691

692-
std::cerr << "adding part: " << render_x << " " << render_y << std::endl;
692+
//std::cerr << "adding part: " << render_x << " " << render_y << std::endl;
693693
LabelPosition* tmp = new LabelPosition(0, render_x /*- xBase*/, render_y /*- yBase*/, ci.width, string_height, -render_angle, 0.0001, this);
694694
tmp->setPartId( orientation > 0 ? i : labelInfo->char_num-i-1 );
695695
if (slp == NULL)
@@ -722,7 +722,7 @@ namespace pal
722722
else
723723
{
724724
// Otherwise we have failed to find a placement
725-
std::cerr << "err7" << std::endl;
725+
//std::cerr << "err7" << std::endl;
726726
return NULL;
727727
}
728728
}
@@ -738,6 +738,7 @@ namespace pal
738738

739739
// distance calculation
740740
double* path_distances = new double[mapShape->nbPoints];
741+
double total_distance = 0;
741742
double old_x, old_y, new_x, new_y;
742743
for (int i = 0; i < mapShape->nbPoints; i++)
743744
{
@@ -747,21 +748,57 @@ namespace pal
747748
path_distances[i] = sqrt( pow(old_x - mapShape->x[i], 2) + pow(old_y - mapShape->y[i],2) );
748749
old_x = mapShape->x[i];
749750
old_y = mapShape->y[i];
751+
752+
total_distance += path_distances[i];
750753
}
751754

752-
// TODO: generate more labels
755+
if (total_distance == 0)
756+
return 0;
753757

754-
// generate curved label
755-
LabelPosition* slp = curvedPlacementAtOffset(mapShape, path_distances, 0, 1, 0.0);
758+
int nbp = 0;
759+
LinkedList<LabelPosition*> *positions = new LinkedList<LabelPosition*> ( ptrLPosCompare );
760+
double delta = max( labelInfo->label_height, total_distance/10.0 );
756761

757-
if (!slp)
758-
return 0;
762+
// generate curved labels
763+
std::cerr << "------" << std::endl;
764+
for (int i = 0; i*delta < total_distance; i++)
765+
{
766+
LabelPosition* slp = curvedPlacementAtOffset(mapShape, path_distances, 0, 1, i*delta);
767+
768+
if (slp)
769+
{
770+
// evaluate cost
771+
double angle_diff = 0, angle_last, diff;
772+
LabelPosition* tmp = slp;
773+
while (tmp)
774+
{
775+
if (tmp != slp) // not first?
776+
{
777+
diff = fabs(tmp->getAlpha() - angle_last);
778+
if (diff > 2*M_PI) diff -= 2*M_PI;
779+
angle_diff += diff;
780+
}
781+
782+
angle_last = tmp->getAlpha();
783+
tmp = tmp->getNextPart();
784+
}
785+
double cost = angle_diff/100.0;
786+
if (cost < 0.0001) cost = 0.0001;
787+
std::cerr << "cost " << angle_diff << std::endl;
788+
slp->setCost(cost);
789+
790+
positions->push_back(slp);
791+
nbp++;
792+
}
793+
}
759794

760-
// TODO: evaluate cost
761795

762-
int nbp = 1;
763796
( *lPos ) = new LabelPosition*[nbp];
764-
(*lPos)[0] = slp;
797+
for (int i = 0; i < nbp; i++)
798+
{
799+
(*lPos)[i] = positions->pop_front();
800+
}
801+
delete positions;
765802

766803
return nbp;
767804
}

0 commit comments

Comments
 (0)