From 0b9b7e8eb7de14d2b6c166af39a6dbe2a8534ad2 Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Wed, 16 Jan 2019 14:08:30 +0100 Subject: [PATCH] #3971 Contour maps: add a label candidate for every segment but use bounding boxes and minimum curve distance to skip. --- .../RivContourMapProjectionPartMgr.cpp | 52 ++++++++++++++----- Fwk/VizFwk/LibRender/cvfDrawableText.cpp | 2 +- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/ApplicationCode/ModelVisualization/RivContourMapProjectionPartMgr.cpp b/ApplicationCode/ModelVisualization/RivContourMapProjectionPartMgr.cpp index 73eff83c59..4a1a44ef3b 100644 --- a/ApplicationCode/ModelVisualization/RivContourMapProjectionPartMgr.cpp +++ b/ApplicationCode/ModelVisualization/RivContourMapProjectionPartMgr.cpp @@ -382,42 +382,68 @@ std::vector> cvf::String labelText(m_contourLinePolygons[i][j].value); size_t nVertices = m_contourLinePolygons[i][j].vertices.size(); - size_t nLabels = m_contourMapProjection->showContourLabels() ? std::max((size_t)1, (size_t) (nVertices / 50u * m_contourMapProjection->sampleSpacingFactor())) : 0u; + size_t nLabels = nVertices; + double distanceSinceLastLabel = std::numeric_limits::infinity(); for (size_t l = 0; l < nLabels; ++l) { size_t nVertex = (nVertices * l) / nLabels; + size_t nextVertex = (nVertex + 1) % nVertices; cvf::Vec3d globalVertex1 = m_contourLinePolygons[i][j].vertices[nVertex] + m_contourMapProjection->origin3d(); cvf::Vec3d globalVertex2 = - m_contourLinePolygons[i][j].vertices[nVertex + 1 % nVertices] + m_contourMapProjection->origin3d(); + m_contourLinePolygons[i][j].vertices[nextVertex] + m_contourMapProjection->origin3d(); cvf::Vec3d globalVertex = 0.5 * (globalVertex1 + globalVertex2); - cvf::Vec3f segment = cvf::Vec3f(globalVertex2 - globalVertex1).getNormalized(); + cvf::Vec3d segment = globalVertex2 - globalVertex1; cvf::Vec3d displayVertex = displayCoordTransform->transformToDisplayCoord(globalVertex); cvf::Vec3d windowVertex; camera->project(displayVertex, &windowVertex); + CVF_ASSERT(!windowVertex.isUndefined()); displayVertex.z() += 10.0f; - cvf::BoundingBox windowBBox = label->textBoundingBox(labelText, cvf::Vec3f::ZERO, segment); - cvf::Vec3d displayBBoxMin, displayBoxMax; + cvf::BoundingBox windowBBox = label->textBoundingBox(labelText, cvf::Vec3f::ZERO, cvf::Vec3f(segment.getNormalized())); + cvf::Vec3d displayBBoxMin, displayBBoxMax; camera->unproject(windowBBox.min() + windowVertex, &displayBBoxMin); - camera->unproject(windowBBox.max() + windowVertex, &displayBoxMax); - cvf::BoundingBox displayBBox(displayBBoxMin - cvf::Vec3d::Z_AXIS * 20.0, displayBoxMax + cvf::Vec3d::Z_AXIS * 20.0); + camera->unproject(windowBBox.max() + windowVertex, &displayBBoxMax); + + CVF_ASSERT(!displayBBoxMin.isUndefined()); + CVF_ASSERT(!displayBBoxMax.isUndefined()); + + cvf::BoundingBox displayBBox(displayBBoxMin - cvf::Vec3d::Z_AXIS * 20.0, displayBBoxMax + cvf::Vec3d::Z_AXIS * 20.0); + + cvf::Vec3d currentExtent = displayBBoxMax - displayBBoxMin; bool overlaps = false; - for (auto boxVector : *labelBBoxes) + if (distanceSinceLastLabel < currentExtent.length() * 10.0) + { + overlaps = true; + } + + if (!overlaps) { - for (const cvf::BoundingBox& existingBBox : boxVector) + for (auto boxVector : *labelBBoxes) { - if (existingBBox.intersects(displayBBox)) + for (const cvf::BoundingBox& existingBBox : boxVector) { - overlaps = true; - break; + double dist = (displayBBox.center() - existingBBox.center()).length(); + if (dist < segment.length() || existingBBox.intersects(displayBBox)) + { + overlaps = true; + break; + } } } } + if (!overlaps) { - label->addText(labelText, cvf::Vec3f(displayVertex), segment); + cvf::Vec3f displayVertexV(displayVertex); + CVF_ASSERT(!displayVertex.isUndefined()); + label->addText(labelText, displayVertexV, cvf::Vec3f(segment.getNormalized())); labelBBoxes->at(i).push_back(displayBBox); + distanceSinceLastLabel = 0.0; + } + else + { + distanceSinceLastLabel += segment.length(); } } } diff --git a/Fwk/VizFwk/LibRender/cvfDrawableText.cpp b/Fwk/VizFwk/LibRender/cvfDrawableText.cpp index d60f7ba718..7ea34edc59 100644 --- a/Fwk/VizFwk/LibRender/cvfDrawableText.cpp +++ b/Fwk/VizFwk/LibRender/cvfDrawableText.cpp @@ -378,7 +378,7 @@ void DrawableText::renderText(OpenGLContext* oglContext, ShaderProgram* shaderPr { Vec3d proj; GeometryUtils::project(modelViewProjectionMatrix, matrixState.viewportPosition(), matrixState.viewportSize(), Vec3d(m_positions[pos]), &proj); - + CVF_ASSERT(!proj.isUndefined()); if (!m_checkPosVisible || labelAnchorVisible(oglContext, proj, m_positions[pos], shaderProgram == NULL)) { // Note: Need to adjust for the current viewport, as the coords returned from project are in global windows coordinates