#3876 Implement rotated labels on contour maps

This commit is contained in:
Gaute Lindkvist
2019-01-03 17:07:55 +01:00
parent 71cacc550f
commit 7a275be335
14 changed files with 593 additions and 208 deletions

View File

@@ -3,14 +3,17 @@
#include "RiaColorTools.h"
#include "RiaFontCache.h"
#include "RiaWeightedMeanCalculator.h"
#include "RigCellGeometryTools.h"
#include "RivMeshLinesSourceInfo.h"
#include "RivScalarMapperUtils.h"
#include "RivPartPriority.h"
#include "RimContourMapView.h"
#include "RimContourMapProjection.h"
#include "cafEffectGenerator.h"
#include "cvfCamera.h"
#include "cvfDrawableText.h"
#include "cvfGeometryBuilderFaceList.h"
#include "cvfGeometryUtils.h"
@@ -18,6 +21,7 @@
#include "cvfPart.h"
#include "cvfPrimitiveSetIndexedUInt.h"
#include "cvfScalarMapper.h"
#include "cvfRay.h"
#include <cmath>
@@ -30,52 +34,26 @@ RivContourMapProjectionPartMgr::RivContourMapProjectionPartMgr(RimContourMapProj
m_parentContourMap = contourMap;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivContourMapProjectionPartMgr::createProjectionGeometry()
{
m_contourMapProjection->generateContourPolygons();
m_contourLinePolygons = m_contourMapProjection->contourPolygons();
m_contourMapTriangles = m_contourMapProjection->generateTriangles();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivContourMapProjectionPartMgr::appendProjectionToModel(cvf::ModelBasicList* model, const caf::DisplayCoordTransform* displayCoordTransform) const
{
cvf::ScalarMapper* mapper = m_contourMapProjection->legendConfig()->scalarMapper();
std::vector<std::vector<cvf::ref<cvf::Drawable>>> contourDrawablesForAllLevels = createContourPolygons(displayCoordTransform);
cvf::ref<cvf::Part> mapPart = createProjectionMapPart(displayCoordTransform);
if (mapPart.notNull())
{
model->addPart(mapPart.p());
}
if (m_contourMapProjection->showContourLines())
{
std::vector<double> tickValues;
mapper->majorTickValues(&tickValues);
for (size_t i = 0; i < contourDrawablesForAllLevels.size(); ++i)
{
std::vector<cvf::ref<cvf::Drawable>> contourDrawables = contourDrawablesForAllLevels[i];
cvf::Color3f backgroundColor(mapper->mapToColor(tickValues[i]));
cvf::Color3f lineColor = RiaColorTools::contrastColor(backgroundColor, true);
for (cvf::ref<cvf::Drawable> contourDrawable : contourDrawables)
{
if (contourDrawable.notNull() && contourDrawable->boundingBox().isValid())
{
caf::MeshEffectGenerator meshEffectGen(lineColor);
meshEffectGen.setLineWidth(1.0f);
meshEffectGen.createAndConfigurePolygonOffsetRenderState(caf::PO_1);
cvf::ref<cvf::Effect> effect = meshEffectGen.generateCachedEffect();
cvf::ref<cvf::Part> part = new cvf::Part;
part->setDrawable(contourDrawable.p());
part->setEffect(effect.p());
part->setSourceInfo(new RivMeshLinesSourceInfo(m_contourMapProjection.p()));
model->addPart(part.p());
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
@@ -127,6 +105,70 @@ cvf::ref<cvf::Vec2fArray> RivContourMapProjectionPartMgr::createTextureCoords(co
return textureCoords;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivContourMapProjectionPartMgr::appendContourLinesToModel(const cvf::Camera* camera,
cvf::ModelBasicList* model,
const caf::DisplayCoordTransform* displayCoordTransform) const
{
if (m_contourMapProjection->showContourLines())
{
cvf::ScalarMapper* mapper = m_contourMapProjection->legendConfig()->scalarMapper();
std::vector<std::vector<cvf::BoundingBox>> labelBBoxes;
std::vector<cvf::ref<cvf::Drawable>> labelDrawables = createContourLabels(camera, displayCoordTransform, &labelBBoxes);
std::vector<std::vector<cvf::ref<cvf::Drawable>>> contourDrawablesForAllLevels =
createContourPolygons(displayCoordTransform, labelBBoxes);
std::vector<double> tickValues;
mapper->majorTickValues(&tickValues);
for (size_t i = 0; i < contourDrawablesForAllLevels.size(); ++i)
{
std::vector<cvf::ref<cvf::Drawable>> contourDrawables = contourDrawablesForAllLevels[i];
cvf::Color3f backgroundColor(mapper->mapToColor(tickValues[i]));
cvf::Color3f lineColor = RiaColorTools::contrastColor(backgroundColor);
for (cvf::ref<cvf::Drawable> contourDrawable : contourDrawables)
{
if (contourDrawable.notNull() && contourDrawable->boundingBox().isValid())
{
caf::MeshEffectGenerator meshEffectGen(lineColor);
meshEffectGen.setLineWidth(1.0f);
meshEffectGen.createAndConfigurePolygonOffsetRenderState(caf::PO_1);
cvf::ref<cvf::Effect> effect = meshEffectGen.generateCachedEffect();
cvf::ref<cvf::Part> part = new cvf::Part;
part->setDrawable(contourDrawable.p());
part->setEffect(effect.p());
part->setSourceInfo(new RivMeshLinesSourceInfo(m_contourMapProjection.p()));
model->addPart(part.p());
}
}
}
for (auto labelDrawableRef : labelDrawables)
{
caf::MeshEffectGenerator meshEffectGen(cvf::Color3::BLACK);
meshEffectGen.setLineWidth(1.0f);
meshEffectGen.createAndConfigurePolygonOffsetRenderState(caf::PO_2);
cvf::ref<cvf::Effect> effect = meshEffectGen.generateCachedEffect();
cvf::ref<cvf::Part> part = new cvf::Part;
part->setDrawable(labelDrawableRef.p());
part->setEffect(effect.p());
part->setPriority(RivPartPriority::Text);
part->setSourceInfo(new RivMeshLinesSourceInfo(m_contourMapProjection.p()));
model->addPart(part.p());
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -136,13 +178,14 @@ cvf::ref<cvf::DrawableText> RivContourMapProjectionPartMgr::createTextLabel(cons
cvf::ref<cvf::DrawableText> labelDrawable = new cvf::DrawableText();
labelDrawable->setFont(font.p());
labelDrawable->setCheckPosVisible(true);
labelDrawable->setCheckPosVisible(false);
labelDrawable->setUseDepthBuffer(true);
labelDrawable->setDrawBorder(false);
labelDrawable->setDrawBackground(false);
labelDrawable->setBackgroundColor(backgroundColor);
labelDrawable->setVerticalAlignment(cvf::TextDrawer::BASELINE);
labelDrawable->setVerticalAlignment(cvf::TextDrawer::CENTER);
labelDrawable->setTextColor(textColor);
labelDrawable->setBorderColor(textColor);
return labelDrawable;
}
@@ -152,7 +195,7 @@ cvf::ref<cvf::DrawableText> RivContourMapProjectionPartMgr::createTextLabel(cons
//--------------------------------------------------------------------------------------------------
cvf::ref<cvf::Part> RivContourMapProjectionPartMgr::createProjectionMapPart(const caf::DisplayCoordTransform* displayCoordTransform) const
{
std::vector<cvf::Vec4d> vertices = m_contourMapProjection->generateTriangles();
const std::vector<cvf::Vec4d>& vertices = m_contourMapTriangles;
if (vertices.size() < 3u)
{
return cvf::ref<cvf::Part>();
@@ -192,60 +235,100 @@ cvf::ref<cvf::Part> RivContourMapProjectionPartMgr::createProjectionMapPart(cons
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::vector<cvf::ref<cvf::Drawable>>> RivContourMapProjectionPartMgr::createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform) const
std::vector<std::vector<cvf::ref<cvf::Drawable>>> RivContourMapProjectionPartMgr::createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform, const std::vector<std::vector<cvf::BoundingBox>>& labelBBoxes) const
{
m_contourMapProjection->generateContourPolygons();
const std::vector<RimContourMapProjection::ContourPolygons>& contourPolygons = m_contourMapProjection->contourPolygons();
const cvf::ScalarMapper* mapper = m_contourMapProjection->legendConfig()->scalarMapper();
std::vector<double> tickValues;
mapper->majorTickValues(&tickValues);
std::vector<std::vector<cvf::ref<cvf::Drawable>>> contourDrawablesForAllLevels;
std::vector<cvf::ref<cvf::Drawable>> labelDrawables;
for (int64_t i = (int64_t) contourPolygons.size() - 1; i > 0; --i)
contourDrawablesForAllLevels.resize(tickValues.size());
for (size_t i = 1; i < m_contourLinePolygons.size(); ++i)
{
std::vector<cvf::ref<cvf::Drawable>> contourDrawables;
cvf::Color3f backgroundColor(mapper->mapToColor(tickValues[i]));
cvf::Color3f textColor = RiaColorTools::contrastColor(backgroundColor);
for (size_t j = 0; j < contourPolygons[i].size(); ++j)
for (size_t j = 0; j < m_contourLinePolygons[i].size(); ++j)
{
if (contourPolygons[i][j].vertices.empty()) continue;
if (m_contourLinePolygons[i][j].vertices.empty()) continue;
size_t nVertices = contourPolygons[i][j].vertices.size();
size_t nLabels = m_contourMapProjection->showContourLabels() ? std::max((size_t)1, nVertices / 100u) : 0u;
for (size_t l = 0; l < nLabels; ++l)
{
cvf::ref<cvf::DrawableText> label = createTextLabel(textColor, backgroundColor);
cvf::Vec3d globalVertex = contourPolygons[i][j].vertices[(nVertices * l) / nLabels] + m_contourMapProjection->origin3d();
cvf::Vec3f displayVertex(displayCoordTransform->transformToDisplayCoord(globalVertex));
displayVertex.z() += 3.0f;
label->addText(cvf::String(contourPolygons[i][j].value), displayVertex);
bool overlaps = false;
cvf::BoundingBox bbox = label->boundingBox();
for (cvf::ref<cvf::Drawable> existingLabel : labelDrawables)
{
if (existingLabel->boundingBox().intersects(bbox))
{
overlaps = true;
break;
}
}
if (!overlaps)
{
labelDrawables.push_back(label);
}
}
cvf::ref<cvf::Vec3fArray> vertexArray = new cvf::Vec3fArray(nVertices);
cvf::String labelText(m_contourLinePolygons[i][j].value);
size_t nVertices = m_contourLinePolygons[i][j].vertices.size();
std::vector<cvf::Vec3f> displayLines; displayLines.reserve(nVertices * 2);
for (size_t v = 0; v < nVertices; ++v)
{
cvf::Vec3d globalVertex = contourPolygons[i][j].vertices[v] + m_contourMapProjection->origin3d();
cvf::Vec3d displayVertex1 = displayCoordTransform->transformToDisplayCoord(globalVertex);
(*vertexArray)[v] = cvf::Vec3f(displayVertex1);
cvf::Vec3d globalVertex1 = m_contourLinePolygons[i][j].vertices[v] + m_contourMapProjection->origin3d();
cvf::Vec3d displayVertex1 = displayCoordTransform->transformToDisplayCoord(globalVertex1);
cvf::Vec3d globalVertex2;
if (v < nVertices - 1)
globalVertex2 = m_contourLinePolygons[i][j].vertices[v + 1] + m_contourMapProjection->origin3d();
else
globalVertex2 = m_contourLinePolygons[i][j].vertices[0] + m_contourMapProjection->origin3d();
cvf::Vec3d displayVertex2 = displayCoordTransform->transformToDisplayCoord(globalVertex2);
cvf::BoundingBox lineBBox;
lineBBox.add(displayVertex1);
lineBBox.add(displayVertex2);
bool addOriginalSegment = true;
for (const cvf::BoundingBox& existingBBox : labelBBoxes[i])
{
if (lineBBox.intersects(existingBBox))
{
if (existingBBox.contains(displayVertex1) && existingBBox.contains(displayVertex2))
{
addOriginalSegment = false;
}
else
{
cvf::Vec3d dir = displayVertex2 - displayVertex1;
cvf::Ray ray;
ray.setOrigin(displayVertex1);
ray.setDirection(dir.getNormalized());
ray.setMaximumDistance(dir.length());
if (!existingBBox.contains(displayVertex1))
{
cvf::Vec3d intersection;
bool hit = ray.boxIntersect(existingBBox, &intersection);
if (hit)
{
displayLines.push_back(cvf::Vec3f(displayVertex1));
displayLines.push_back(cvf::Vec3f(intersection));
addOriginalSegment = false;
}
}
if (!existingBBox.contains(displayVertex2))
{
ray.setOrigin(displayVertex2);
ray.setDirection(-ray.direction());
cvf::Vec3d intersection;
bool hit = ray.boxIntersect(existingBBox, &intersection);
if (hit)
{
displayLines.push_back(cvf::Vec3f(intersection));
displayLines.push_back(cvf::Vec3f(displayVertex2));
addOriginalSegment = false;
}
}
}
}
}
if (addOriginalSegment)
{
displayLines.push_back(cvf::Vec3f(displayVertex1));
displayLines.push_back(cvf::Vec3f(displayVertex2));
}
}
cvf::ref<cvf::Vec3fArray> vertexArray = new cvf::Vec3fArray(displayLines);
std::vector<cvf::uint> indices;
indices.reserve(vertexArray->size());
for (cvf::uint k = 0; k < vertexArray->size(); ++k)
@@ -253,7 +336,7 @@ std::vector<std::vector<cvf::ref<cvf::Drawable>>> RivContourMapProjectionPartMgr
indices.push_back(k);
}
cvf::ref<cvf::PrimitiveSetIndexedUInt> indexedUInt = new cvf::PrimitiveSetIndexedUInt(cvf::PrimitiveType::PT_LINE_LOOP);
cvf::ref<cvf::PrimitiveSetIndexedUInt> indexedUInt = new cvf::PrimitiveSetIndexedUInt(cvf::PrimitiveType::PT_LINES);
cvf::ref<cvf::UIntArray> indexArray = new cvf::UIntArray(indices);
indexedUInt->setIndices(indexArray.p());
@@ -263,17 +346,89 @@ std::vector<std::vector<cvf::ref<cvf::Drawable>>> RivContourMapProjectionPartMgr
geo->setVertexArray(vertexArray.p());
contourDrawables.push_back(geo);
}
for (cvf::ref<cvf::Drawable> labelDrawable : labelDrawables)
{
contourDrawables.push_back(labelDrawable);
}
contourDrawablesForAllLevels.push_back(contourDrawables);
contourDrawablesForAllLevels[i] = contourDrawables;
}
return contourDrawablesForAllLevels;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<cvf::ref<cvf::Drawable>>
RivContourMapProjectionPartMgr::createContourLabels(const cvf::Camera* camera,
const caf::DisplayCoordTransform* displayCoordTransform,
std::vector<std::vector<cvf::BoundingBox>>* labelBBoxes) const
{
CVF_ASSERT(camera && displayCoordTransform && labelBBoxes);
const cvf::ScalarMapper* mapper = m_contourMapProjection->legendConfig()->scalarMapper();
std::vector<double> tickValues;
mapper->majorTickValues(&tickValues);
std::vector<cvf::ref<cvf::Drawable>> labelDrawables;
labelBBoxes->clear();
labelBBoxes->resize(m_contourLinePolygons.size());
for (int64_t i = (int64_t)m_contourLinePolygons.size() - 1; i > 0; --i)
{
cvf::Color3f backgroundColor(mapper->mapToColor(tickValues[i]));
cvf::Color3f textColor = RiaColorTools::contrastColor(backgroundColor);
cvf::ref<cvf::DrawableText> label = createTextLabel(textColor, backgroundColor);
for (size_t j = 0; j < m_contourLinePolygons[i].size(); ++j)
{
if (m_contourLinePolygons[i][j].vertices.empty()) continue;
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, nVertices / 50u) : 0u;
for (size_t l = 0; l < nLabels; ++l)
{
size_t nVertex = (nVertices * l) / nLabels;
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();
cvf::Vec3d globalVertex = 0.5 * (globalVertex1 + globalVertex2);
cvf::Vec3f segment = cvf::Vec3f(globalVertex2 - globalVertex1).getNormalized();
cvf::Vec3d displayVertex = displayCoordTransform->transformToDisplayCoord(globalVertex);
cvf::Vec3d windowVertex;
camera->project(displayVertex, &windowVertex);
displayVertex.z() += 10.0f;
cvf::BoundingBox windowBBox = label->textBoundingBox(labelText, cvf::Vec3f::ZERO, segment);
cvf::Vec3d displayBBoxMin, displayBoxMax;
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);
bool overlaps = false;
for (auto boxVector : *labelBBoxes)
{
for (const cvf::BoundingBox& existingBBox : boxVector)
{
if (existingBBox.intersects(displayBBox))
{
overlaps = true;
break;
}
}
}
if (!overlaps)
{
label->addText(labelText, cvf::Vec3f(displayVertex), segment);
labelBBoxes->at(i).push_back(displayBBox);
}
}
}
if (label->numberOfTexts() != 0u)
{
labelDrawables.push_back(label);
}
}
return labelDrawables;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -18,6 +18,8 @@
#pragma once
#include "RimContourMapProjection.h"
#include "cafPdmPointer.h"
#include "cafDisplayCoordTransform.h"
@@ -26,29 +28,37 @@
#include "cvfDrawableText.h"
#include "cvfModelBasicList.h"
#include "cvfObject.h"
#include "cvfVector4.h"
class RimContourMapView;
class RimContourMapProjection;
class RivContourMapProjectionPartMgr : public cvf::Object
{
public:
RivContourMapProjectionPartMgr(RimContourMapProjection* contourMapProjection, RimContourMapView* contourMap);
void createProjectionGeometry();
void appendProjectionToModel(cvf::ModelBasicList* model,
const caf::DisplayCoordTransform* displayCoordTransform) const;
void appendContourLinesToModel(const cvf::Camera* camera, cvf::ModelBasicList* model, const caf::DisplayCoordTransform* displayCoordTransform) const;
void appendPickPointVisToModel(cvf::ModelBasicList* model,
const caf::DisplayCoordTransform* displayCoordTransform) const;
cvf::ref<cvf::Vec2fArray> createTextureCoords(const std::vector<double>& values) const;
private:
static cvf::ref<cvf::DrawableText> createTextLabel(const cvf::Color3f& textColor, const cvf::Color3f& backgroundColor);
cvf::ref<cvf::Part> createProjectionMapPart(const caf::DisplayCoordTransform* displayCoordTransform) const;
std::vector<std::vector<cvf::ref<cvf::Drawable>>> createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform) const;
std::vector<std::vector<cvf::ref<cvf::Drawable>>> createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform, const std::vector<std::vector<cvf::BoundingBox>>& labelBBoxes) const;
std::vector<cvf::ref<cvf::Drawable>> createContourLabels(const cvf::Camera* camera, const caf::DisplayCoordTransform* displayCoordTransform, std::vector<std::vector<cvf::BoundingBox>>* labelBBoxes) const;
cvf::ref<cvf::DrawableGeo> createPickPointVisDrawable(const caf::DisplayCoordTransform* displayCoordTransform) const;
private:
caf::PdmPointer<RimContourMapProjection> m_contourMapProjection;
caf::PdmPointer<RimContourMapView> m_parentContourMap;
std::vector<RimContourMapProjection::ContourPolygons> m_contourLinePolygons;
std::vector<cvf::Vec4d> m_contourMapTriangles;
std::vector<std::vector<cvf::BoundingBox>> m_labelBoundingBoxes;
};