#3876 First label implementation. Not rotated yet.

This commit is contained in:
Gaute Lindkvist 2018-12-21 08:44:30 +01:00
parent 37a3342db5
commit 6754f837d4
4 changed files with 105 additions and 19 deletions

View File

@ -1,5 +1,7 @@
#include "RivContourMapProjectionPartMgr.h"
#include "RiaColorTools.h"
#include "RiaFontCache.h"
#include "RiaWeightedMeanCalculator.h"
#include "RivMeshLinesSourceInfo.h"
#include "RivScalarMapperUtils.h"
@ -9,11 +11,13 @@
#include "cafEffectGenerator.h"
#include "cvfDrawableText.h"
#include "cvfGeometryBuilderFaceList.h"
#include "cvfGeometryUtils.h"
#include "cvfMeshEdgeExtractor.h"
#include "cvfPart.h"
#include "cvfPrimitiveSetIndexedUInt.h"
#include "cvfScalarMapper.h"
#include <cmath>
@ -48,8 +52,8 @@ void RivContourMapProjectionPartMgr::appendProjectionToModel(cvf::ModelBasicList
if (m_contourMapProjection->showContourLines())
{
std::vector<cvf::ref<cvf::DrawableGeo>> contourDrawables = createContourPolygons(displayCoordTransform);
for (cvf::ref<cvf::DrawableGeo> contourDrawable : contourDrawables)
std::vector<cvf::ref<cvf::Drawable>> contourDrawables = createContourPolygons(displayCoordTransform);
for (cvf::ref<cvf::Drawable> contourDrawable : contourDrawables)
{
if (contourDrawable.notNull() && contourDrawable->boundingBox().isValid())
{
@ -148,6 +152,27 @@ cvf::ref<cvf::Vec2fArray> RivContourMapProjectionPartMgr::createTextureCoords()
return textureCoords;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::ref<cvf::DrawableText> RivContourMapProjectionPartMgr::createTextLabel(const cvf::Color3f& backgroundColor)
{
auto font = RiaFontCache::getFont(RiaFontCache::FONT_SIZE_8);
cvf::ref<cvf::DrawableText> labelDrawable = new cvf::DrawableText();
labelDrawable->setFont(font.p());
labelDrawable->setCheckPosVisible(true);
labelDrawable->setUseDepthBuffer(false);
labelDrawable->setDrawBorder(true);
labelDrawable->setDrawBackground(true);
labelDrawable->setBackgroundColor(backgroundColor);
labelDrawable->setVerticalAlignment(cvf::TextDrawer::BASELINE);
cvf::Color3f textColor = RiaColorTools::constrastColor(backgroundColor);
labelDrawable->setTextColor(textColor);
return labelDrawable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -181,23 +206,55 @@ cvf::ref<cvf::DrawableGeo> RivContourMapProjectionPartMgr::createProjectionMapDr
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<cvf::ref<cvf::DrawableGeo>> RivContourMapProjectionPartMgr::createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform) const
std::vector<cvf::ref<cvf::Drawable>> RivContourMapProjectionPartMgr::createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform) const
{
m_contourMapProjection->generateContourPolygons();
const std::vector<RimContourMapProjection::ContourPolygons>& contourPolygons = m_contourMapProjection->contourPolygons();
std::vector<cvf::ref<cvf::DrawableGeo>> contourDrawables;
for (size_t i = 0; i < contourPolygons.size(); ++i)
const cvf::ScalarMapper* mapper = m_contourMapProjection->legendConfig()->scalarMapper();
std::vector<double> tickValues;
mapper->majorTickValues(&tickValues);
std::vector<cvf::ref<cvf::Drawable>> contourDrawables;
std::vector<cvf::ref<cvf::Drawable>> labelDrawables;
for (int64_t i = (int64_t) contourPolygons.size() - 1; i > 0; --i)
{
cvf::Color3f backgroundColor(mapper->mapToColor(tickValues[i]));
for (size_t j = 0; j < contourPolygons[i].size(); ++j)
{
if (contourPolygons[i][j].empty()) continue;
if (contourPolygons[i][j].vertices.empty()) continue;
cvf::ref<cvf::Vec3fArray> vertexArray = new cvf::Vec3fArray(contourPolygons[i][j].size());
for (size_t v = 0; v < contourPolygons[i][j].size(); ++v)
size_t nVertices = contourPolygons[i][j].vertices.size();
size_t nLabels = m_contourMapProjection->showContourLabels() ? std::max((size_t)1, nVertices / 150u) : 0u;
for (size_t l = 0; l < nLabels; ++l)
{
cvf::Vec3f displayVertex(displayCoordTransform->transformToDisplayCoord(contourPolygons[i][j][v]));
(*vertexArray)[v] = displayVertex;
cvf::ref<cvf::DrawableText> label = createTextLabel(backgroundColor);
cvf::Vec3f labelVertex(displayCoordTransform->transformToDisplayCoord(contourPolygons[i][j].vertices[(nVertices * l) / nLabels]));
labelVertex.z() += 3.0f;
label->addText(contourPolygons[i][j].label, labelVertex);
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);
for (size_t v = 0; v < nVertices; v += 2)
{
cvf::Vec3d displayVertex1 = displayCoordTransform->transformToDisplayCoord(contourPolygons[i][j].vertices[v]);
cvf::Vec3d displayVertex2 = displayCoordTransform->transformToDisplayCoord(contourPolygons[i][j].vertices[v + 1]);
(*vertexArray)[v] = cvf::Vec3f(displayVertex1);
(*vertexArray)[v + 1] = cvf::Vec3f(displayVertex2);
}
std::vector<cvf::uint> indices;
@ -218,6 +275,11 @@ std::vector<cvf::ref<cvf::DrawableGeo>> RivContourMapProjectionPartMgr::createCo
contourDrawables.push_back(geo);
}
}
for (cvf::ref<cvf::Drawable> labelDrawable : labelDrawables)
{
contourDrawables.push_back(labelDrawable);
}
return contourDrawables;
}

View File

@ -23,6 +23,7 @@
#include "cvfBase.h"
#include "cvfDrawableGeo.h"
#include "cvfDrawableText.h"
#include "cvfModelBasicList.h"
#include "cvfObject.h"
@ -42,11 +43,12 @@ public:
cvf::ref<cvf::Vec2fArray> createTextureCoords() const;
private:
static cvf::ref<cvf::DrawableText> createTextLabel(const cvf::Color3f& backgroundColor);
cvf::ref<cvf::DrawableGeo> createProjectionMapDrawable(const caf::DisplayCoordTransform* displayCoordTransform) const;
std::vector<cvf::ref<cvf::DrawableGeo>> createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform) const;
std::vector<cvf::ref<cvf::Drawable>> createContourPolygons(const caf::DisplayCoordTransform* displayCoordTransform) const;
cvf::ref<cvf::DrawableGeo> createPickPointVisDrawable(const caf::DisplayCoordTransform* displayCoordTransform) const;
private:
caf::PdmPointer<RimContourMapProjection> m_contourMapProjection;
caf::PdmPointer<RimContourMapView> m_parentContourMap;
caf::PdmPointer<RimContourMapProjection> m_contourMapProjection;
caf::PdmPointer<RimContourMapView> m_parentContourMap;
};

View File

@ -78,6 +78,7 @@ RimContourMapProjection::RimContourMapProjection()
CAF_PDM_InitFieldNoDefault(&m_resultAggregation, "ResultAggregation", "Result Aggregation", "", "", "");
CAF_PDM_InitField(&m_showContourLines, "ContourLines", true, "Show Contour Lines", "", "", "");
CAF_PDM_InitField(&m_showContourLabels, "ContourLabels", false, "Show Contour Labels", "", "", "");
CAF_PDM_InitField(&m_weightByParameter, "WeightByParameter", false, "Weight by Result Parameter", "", "", "");
CAF_PDM_InitFieldNoDefault(&m_weightingResult, "WeightingResult", "", "", "", "");
@ -144,26 +145,29 @@ void RimContourMapProjection::generateContourPolygons()
int nContourLevels = static_cast<int>(contourLevels.size());
if (nContourLevels > 2)
{
std::vector<double> fudgedContourLevels = contourLevels;
if (legendConfig()->mappingMode() == RimRegularLegendConfig::LINEAR_CONTINUOUS || legendConfig()->mappingMode() == RimRegularLegendConfig::LINEAR_DISCRETE)
{
// Slight fudge to avoid very jagged contour lines at the very edge
// Shift the contour levels inwards.
contourLevels[0] += (contourLevels[1] - contourLevels[0]) * 0.1;
contourLevels[nContourLevels - 1] -= (contourLevels[nContourLevels - 1] - contourLevels[nContourLevels - 2]) * 0.1;
fudgedContourLevels[0] += (contourLevels[1] - contourLevels[0]) * 0.1;
fudgedContourLevels[nContourLevels - 1] -= (contourLevels[nContourLevels - 1] - contourLevels[nContourLevels - 2]) * 0.1;
}
std::vector<caf::ContourLines::ClosedPolygons> closedContourLines =
caf::ContourLines::create(m_aggregatedVertexResults, xVertexPositions(), yVertexPositions(), contourLevels);
caf::ContourLines::create(m_aggregatedVertexResults, xVertexPositions(), yVertexPositions(), fudgedContourLevels);
contourPolygons.resize(closedContourLines.size());
for (size_t i = 0; i < closedContourLines.size(); ++i)
{
for (size_t j = 0; j < closedContourLines[i].size(); ++j)
{
ContourPolygon contourPolygon; contourPolygon.reserve(closedContourLines[i][j].size());
ContourPolygon contourPolygon;
contourPolygon.label = cvf::String(contourLevels[i]);
contourPolygon.vertices.reserve(closedContourLines[i][j].size());
for (size_t k = 0; k < closedContourLines[i][j].size(); ++k)
{
cvf::Vec3d contourPoint3d = cvf::Vec3d(closedContourLines[i][j][k], m_fullBoundingBox.min().z());
contourPolygon.push_back(contourPoint3d);
contourPolygon.vertices.push_back(contourPoint3d);
}
contourPolygons[i].push_back(contourPolygon);
}
@ -318,6 +322,14 @@ bool RimContourMapProjection::showContourLines() const
return m_showContourLines();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimContourMapProjection::showContourLabels() const
{
return m_showContourLines() && m_showContourLabels();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -660,6 +672,8 @@ void RimContourMapProjection::defineUiOrdering(QString uiConfigName, caf::PdmUiO
caf::PdmUiGroup* mainGroup = uiOrdering.addNewGroup("Projection Settings");
mainGroup->add(&m_relativeSampleSpacing);
mainGroup->add(&m_showContourLines);
mainGroup->add(&m_showContourLabels);
m_showContourLabels.uiCapability()->setUiReadOnly(!m_showContourLines());
mainGroup->add(&m_resultAggregation);
caf::PdmUiGroup* weightingGroup = uiOrdering.addNewGroup("Mean Weighting Options");

View File

@ -29,6 +29,7 @@
#include "cvfBoundingBox.h"
#include "cvfGeometryBuilderFaceList.h"
#include "cvfString.h"
#include "cvfVector2.h"
class RigMainGrid;
@ -45,6 +46,12 @@ class RimContourMapProjection : public RimCheckableNamedObject
{
CAF_PDM_HEADER_INIT;
public:
struct ContourPolygon
{
std::vector<cvf::Vec3d> vertices;
cvf::String label;
};
enum ResultAggregationEnum
{
RESULTS_TOP_VALUE,
@ -60,7 +67,6 @@ public:
RESULTS_HC_COLUMN
};
typedef caf::AppEnum<ResultAggregationEnum> ResultAggregation;
typedef std::vector<cvf::Vec3d> ContourPolygon;
typedef std::vector<ContourPolygon> ContourPolygons;
RimContourMapProjection();
@ -77,6 +83,7 @@ public:
double sampleSpacing() const;
double sampleSpacingFactor() const;
bool showContourLines() const;
bool showContourLabels() const;
QString resultAggregationText() const;
QString resultDescriptionText() const;
@ -162,6 +169,7 @@ protected:
caf::PdmField<double> m_relativeSampleSpacing;
caf::PdmField<ResultAggregation> m_resultAggregation;
caf::PdmField<bool> m_showContourLines;
caf::PdmField<bool> m_showContourLabels;
caf::PdmField<bool> m_weightByParameter;
caf::PdmChildField<RimEclipseResultDefinition*> m_weightingResult;