From fbfd6fd21230843712361e65c1b41d1620a22006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20St=C3=B8ren?= Date: Fri, 20 Nov 2015 13:26:41 +0100 Subject: [PATCH] (#166) More refactoring to support geomech cross sections --- .../RivCrossSectionGeometryGenerator.cpp | 113 +++++++----------- .../RivCrossSectionGeometryGenerator.h | 47 +++----- .../RivCrossSectionPartMgr.cpp | 52 +++++++- .../RivCrossSectionPartMgr.h | 3 +- 4 files changed, 113 insertions(+), 102 deletions(-) diff --git a/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.cpp b/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.cpp index 5f243c4972..67070da78e 100644 --- a/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.cpp +++ b/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.cpp @@ -31,10 +31,10 @@ //-------------------------------------------------------------------------------------------------- RivCrossSectionGeometryGenerator::RivCrossSectionGeometryGenerator(const std::vector &polyline, const cvf::Vec3d& extrusionDirection, - const RigMainGrid* grid) + const RivCrossSectionHexGridIntf* grid) : m_polyLine(polyline), m_extrusionDirection(extrusionDirection), - m_mainGrid(grid) + m_hexGrid(grid) { m_triangleVxes = new cvf::Vec3fArray; m_cellBorderLineVxes = new cvf::Vec3fArray; @@ -968,17 +968,15 @@ void RivCrossSectionGeometryGenerator::calculateArrays() if (m_triangleVxes->size()) return; m_extrusionDirection.normalize(); + std::vector m_adjustedPolyline; - adjustPolyline(); + adjustPolyline(m_polyLine, m_extrusionDirection, &m_adjustedPolyline); std::vector triangleVertices; std::vector cellBorderLineVxes; - EclipseCrossSectionGrid eclHexGrid(m_mainGrid.p()); - cvf::CrossSectionHexGridIntf* hexGrid = &eclHexGrid; - - cvf::Vec3d displayOffset = hexGrid->displayOffset(); - cvf::BoundingBox gridBBox = hexGrid->boundingBox(); + cvf::Vec3d displayOffset = m_hexGrid->displayOffset(); + cvf::BoundingBox gridBBox = m_hexGrid->boundingBox(); size_t lineCount = m_adjustedPolyline.size(); for (size_t lIdx = 0; lIdx < lineCount - 1; ++lIdx) @@ -996,7 +994,7 @@ void RivCrossSectionGeometryGenerator::calculateArrays() sectionBBox.add(p2 - m_extrusionDirection*maxSectionHeight); std::vector columnCellCandidates; - hexGrid->findIntersectingCells(sectionBBox, &columnCellCandidates); + m_hexGrid->findIntersectingCells(sectionBBox, &columnCellCandidates); cvf::Plane plane; plane.setFromPoints(p1, p2, p2 + m_extrusionDirection*maxSectionHeight); @@ -1018,11 +1016,11 @@ void RivCrossSectionGeometryGenerator::calculateArrays() { size_t globalCellIdx = columnCellCandidates[cccIdx]; - if (!hexGrid->useCell(globalCellIdx)) continue; + if (!m_hexGrid->useCell(globalCellIdx)) continue; hexPlaneCutTriangleVxes.clear(); - hexGrid->cellCornerVertices(globalCellIdx, cellCorners); - hexGrid->cellCornerIndices(globalCellIdx, cornerIndices); + m_hexGrid->cellCornerVertices(globalCellIdx, cellCorners); + m_hexGrid->cellCornerIndices(globalCellIdx, cornerIndices); int triangleCount = planeHexIntersectionMC(plane, cellCorners, @@ -1142,69 +1140,50 @@ cvf::ref RivCrossSectionGeometryGenerator::createMeshDrawable( return geo; } - //-------------------------------------------------------------------------------------------------- -/// Calculates the texture coordinates in a "nearly" one dimentional texture. -/// Undefined values are coded with a y-texturecoordinate value of 1.0 instead of the normal 0.5 +/// Remove the lines from the polyline that is nearly parallel to the extrusion direction //-------------------------------------------------------------------------------------------------- -void RivCrossSectionGeometryGenerator::textureCoordinates(cvf::Vec2fArray* textureCoords, - const RigResultAccessor* resultAccessor, - const cvf::ScalarMapper* mapper) const +void RivCrossSectionGeometryGenerator::adjustPolyline(const std::vector& polyLine, + const cvf::Vec3d extrDir, + std::vector* adjustedPolyline) { - if (!resultAccessor) return; + size_t lineCount = polyLine.size(); + if (!polyLine.size()) return; - size_t numVertices = m_triangleVxes->size(); - - textureCoords->resize(numVertices); - cvf::Vec2f* rawPtr = textureCoords->ptr(); - - double cellScalarValue; - cvf::Vec2f texCoord; - - int triangleCount = static_cast(m_triangleToCellIdxMap.size()); - -#pragma omp parallel for private(texCoord, cellScalarValue) - for (int tIdx = 0; tIdx < triangleCount; tIdx++) - { - cellScalarValue = resultAccessor->cellScalarGlobIdx(m_triangleToCellIdxMap[tIdx]); - texCoord = mapper->mapToTextureCoord(cellScalarValue); - if (cellScalarValue == HUGE_VAL || cellScalarValue != cellScalarValue) // a != a is true for NAN's - { - texCoord[1] = 1.0f; - } - - size_t j; - for (j = 0; j < 3; j++) - { - rawPtr[tIdx*3 + j] = texCoord; - } - } -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RivCrossSectionGeometryGenerator::adjustPolyline() -{ - size_t lineCount = m_polyLine.size(); - if (!m_polyLine.size()) return; - - m_adjustedPolyline.push_back(m_polyLine[0]); - cvf::Vec3d p1 = m_polyLine[0]; + adjustedPolyline->push_back(polyLine[0]); + cvf::Vec3d p1 = polyLine[0]; for (size_t lIdx = 1; lIdx < lineCount; ++lIdx) { - cvf::Vec3d p2 = m_polyLine[lIdx]; + cvf::Vec3d p2 = polyLine[lIdx]; cvf::Vec3d p1p2 = p2 - p1; - if ((p1p2 - (p1p2 * m_extrusionDirection)*m_extrusionDirection).length() > 0.1 ) + if ((p1p2 - (p1p2 * extrDir)*extrDir).length() > 0.1 ) { - m_adjustedPolyline.push_back(p2); + adjustedPolyline->push_back(p2); p1 = p2; } } } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +const std::vector& RivCrossSectionGeometryGenerator::triangleToCellIndex() const +{ + CVF_ASSERT(m_triangleVxes->size()); + return m_triangleToCellIdxMap; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +const std::vector& RivCrossSectionGeometryGenerator::triangleVxToCellCornerInterpolationWeights() const +{ + CVF_ASSERT(m_triangleVxes->size()); + return m_triVxToCellCornerWeights; +} + //-------------------------------------------------------------------------------------------------- /// @@ -1213,7 +1192,7 @@ void RivCrossSectionGeometryGenerator::adjustPolyline() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -EclipseCrossSectionGrid::EclipseCrossSectionGrid(const RigMainGrid * mainGrid) : m_mainGrid(mainGrid) +RivEclipseCrossSectionGrid::RivEclipseCrossSectionGrid(const RigMainGrid * mainGrid) : m_mainGrid(mainGrid) { } @@ -1221,7 +1200,7 @@ EclipseCrossSectionGrid::EclipseCrossSectionGrid(const RigMainGrid * mainGrid) : //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -cvf::Vec3d EclipseCrossSectionGrid::displayOffset() const +cvf::Vec3d RivEclipseCrossSectionGrid::displayOffset() const { return m_mainGrid->displayModelOffset(); } @@ -1229,7 +1208,7 @@ cvf::Vec3d EclipseCrossSectionGrid::displayOffset() const //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -cvf::BoundingBox EclipseCrossSectionGrid::boundingBox() const +cvf::BoundingBox RivEclipseCrossSectionGrid::boundingBox() const { return m_mainGrid->boundingBox(); } @@ -1237,7 +1216,7 @@ cvf::BoundingBox EclipseCrossSectionGrid::boundingBox() const //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void EclipseCrossSectionGrid::findIntersectingCells(const cvf::BoundingBox& intersectingBB, std::vector* intersectedCells) const +void RivEclipseCrossSectionGrid::findIntersectingCells(const cvf::BoundingBox& intersectingBB, std::vector* intersectedCells) const { m_mainGrid->findIntersectingCells(intersectingBB, intersectedCells); } @@ -1245,7 +1224,7 @@ void EclipseCrossSectionGrid::findIntersectingCells(const cvf::BoundingBox& inte //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool EclipseCrossSectionGrid::useCell(size_t cellIndex) const +bool RivEclipseCrossSectionGrid::useCell(size_t cellIndex) const { const RigCell& cell = m_mainGrid->cells()[cellIndex]; @@ -1255,7 +1234,7 @@ bool EclipseCrossSectionGrid::useCell(size_t cellIndex) const //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void EclipseCrossSectionGrid::cellCornerVertices(size_t cellIndex, cvf::Vec3d cellCorners[8]) const +void RivEclipseCrossSectionGrid::cellCornerVertices(size_t cellIndex, cvf::Vec3d cellCorners[8]) const { m_mainGrid->cellCornerVertices(cellIndex, cellCorners); } @@ -1263,7 +1242,7 @@ void EclipseCrossSectionGrid::cellCornerVertices(size_t cellIndex, cvf::Vec3d ce //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void EclipseCrossSectionGrid::cellCornerIndices(size_t cellIndex, size_t cornerIndices[8]) const +void RivEclipseCrossSectionGrid::cellCornerIndices(size_t cellIndex, size_t cornerIndices[8]) const { const caf::SizeTArray8& cornerIndicesSource = m_mainGrid->cells()[cellIndex].cornerIndices(); memcpy(cornerIndices, cornerIndicesSource.data(), 8); diff --git a/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.h b/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.h index c36685c30f..0c3416e3b1 100644 --- a/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.h +++ b/ApplicationCode/ModelVisualization/RivCrossSectionGeometryGenerator.h @@ -35,25 +35,24 @@ namespace cvf } #include "cvfBoundingBox.h" -namespace cvf { -class CrossSectionHexGridIntf : public Object + +class RivCrossSectionHexGridIntf : public cvf::Object { public: - virtual Vec3d displayOffset() const = 0; - virtual BoundingBox boundingBox() const = 0; - virtual void findIntersectingCells(const BoundingBox& intersectingBB, std::vector* intersectedCells) const = 0; + virtual cvf::Vec3d displayOffset() const = 0; + virtual cvf::BoundingBox boundingBox() const = 0; + virtual void findIntersectingCells(const cvf::BoundingBox& intersectingBB, std::vector* intersectedCells) const = 0; virtual bool useCell(size_t cellIndex) const = 0; - virtual void cellCornerVertices(size_t cellIndex, Vec3d cellCorners[8]) const = 0; + virtual void cellCornerVertices(size_t cellIndex, cvf::Vec3d cellCorners[8]) const = 0; virtual void cellCornerIndices(size_t cellIndex, size_t cornerIndices[8]) const = 0; }; -} -class EclipseCrossSectionGrid : public cvf::CrossSectionHexGridIntf +class RivEclipseCrossSectionGrid : public RivCrossSectionHexGridIntf { public: - EclipseCrossSectionGrid(const RigMainGrid * mainGrid); + RivEclipseCrossSectionGrid(const RigMainGrid * mainGrid); virtual cvf::Vec3d displayOffset() const; virtual cvf::BoundingBox boundingBox() const; @@ -109,40 +108,32 @@ class RivCrossSectionGeometryGenerator : public cvf::Object public: RivCrossSectionGeometryGenerator(const std::vector &polyline, const cvf::Vec3d& extrusionDirection, - const RigMainGrid* grid ); + const RivCrossSectionHexGridIntf* grid ); ~RivCrossSectionGeometryGenerator(); - - - void textureCoordinates(cvf::Vec2fArray* textureCoords, - const RigResultAccessor* resultAccessor, - const cvf::ScalarMapper* mapper) const; - - // Mapping between cells and geometry - const std::vector& triangleToCellIndex() const; - const std::vector& triangleVxToCellCornerInterpolationWeights() const; - - // Generated geometry + + // Generate geometry cvf::ref generateSurface(); cvf::ref createMeshDrawable(); + // Mapping between cells and geometry + const std::vector& triangleToCellIndex() const; + const std::vector& triangleVxToCellCornerInterpolationWeights() const; + private: void calculateArrays(); + static void adjustPolyline(const std::vector& polyLine, + const cvf::Vec3d extrDir, + std::vector* adjustedPolyline); - - - void adjustPolyline(); - - cvf::cref m_mainGrid; + cvf::cref m_hexGrid; std::vector m_polyLine; cvf::Vec3d m_extrusionDirection; - std::vector m_adjustedPolyline; // Output arrays cvf::ref m_triangleVxes; cvf::ref m_cellBorderLineVxes; std::vector m_triangleToCellIdxMap; - std::vector m_triVxToCellCornerWeights; }; diff --git a/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.cpp b/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.cpp index cb4574d0a7..c15d856dd9 100644 --- a/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.cpp +++ b/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.cpp @@ -45,7 +45,6 @@ //-------------------------------------------------------------------------------------------------- RivCrossSectionPartMgr::RivCrossSectionPartMgr(const RimCrossSection* rimCrossSection) : m_rimCrossSection(rimCrossSection), - m_grid(NULL), m_defaultColor(cvf::Color3::WHITE) { CVF_ASSERT(m_rimCrossSection); @@ -113,7 +112,10 @@ void RivCrossSectionPartMgr::updateCellResultColor(size_t timeStepIndex, RimEcli timeStepIndex, cellResultColors->resultVariable()); - m_nativeCrossSectionGenerator->textureCoordinates(m_nativeCrossSectionFacesTextureCoords.p(), resultAccessor.p() ,mapper); + calculateEclipseTextureCoordinates(m_nativeCrossSectionFacesTextureCoords.p(), + m_nativeCrossSectionGenerator->triangleToCellIndex(), + resultAccessor.p(), + mapper); RivScalarMapperUtils::applyTextureResultsToPart(m_nativeCrossSectionFaces.p(), @@ -127,6 +129,45 @@ void RivCrossSectionPartMgr::updateCellResultColor(size_t timeStepIndex, RimEcli } +//-------------------------------------------------------------------------------------------------- +/// Calculates the texture coordinates in a "nearly" one dimentional texture. +/// Undefined values are coded with a y-texturecoordinate value of 1.0 instead of the normal 0.5 +//-------------------------------------------------------------------------------------------------- +void RivCrossSectionPartMgr::calculateEclipseTextureCoordinates(cvf::Vec2fArray* textureCoords, + const std::vector& triangleToCellIdxMap, + const RigResultAccessor* resultAccessor, + const cvf::ScalarMapper* mapper) const +{ + if (!resultAccessor) return; + + size_t numVertices = triangleToCellIdxMap.size()*3; + + textureCoords->resize(numVertices); + cvf::Vec2f* rawPtr = textureCoords->ptr(); + + double cellScalarValue; + cvf::Vec2f texCoord; + + int triangleCount = static_cast(triangleToCellIdxMap.size()); + +#pragma omp parallel for private(texCoord, cellScalarValue) + for (int tIdx = 0; tIdx < triangleCount; tIdx++) + { + cellScalarValue = resultAccessor->cellScalarGlobIdx(triangleToCellIdxMap[tIdx]); + texCoord = mapper->mapToTextureCoord(cellScalarValue); + if (cellScalarValue == HUGE_VAL || cellScalarValue != cellScalarValue) // a != a is true for NAN's + { + texCoord[1] = 1.0f; + } + + size_t j; + for (j = 0; j < 3; j++) + { + rawPtr[tIdx*3 + j] = texCoord; + } + } +} + const int priCrossSectionGeo = 1; const int priNncGeo = 2; const int priMesh = 3; @@ -265,14 +306,15 @@ void RivCrossSectionPartMgr::appendMeshLinePartsToModel(cvf::ModelBasicList* mod //-------------------------------------------------------------------------------------------------- void RivCrossSectionPartMgr::computeData() { - m_grid = mainGrid(); - CVF_ASSERT(m_grid.notNull()); + RigMainGrid* m_grid = mainGrid(); + CVF_ASSERT(m_grid); std::vector< std::vector > polyLine = m_rimCrossSection->polyLines(); if (polyLine.size() > 0) { cvf::Vec3d direction = extrusionDirection(polyLine[0]); - m_nativeCrossSectionGenerator = new RivCrossSectionGeometryGenerator(polyLine[0], direction, m_grid.p()); + cvf::ref eclHexGrid = new RivEclipseCrossSectionGrid(m_grid); + m_nativeCrossSectionGenerator = new RivCrossSectionGeometryGenerator(polyLine[0], direction, eclHexGrid.p()); } } diff --git a/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.h b/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.h index 736b1b13be..96c1dd4fcc 100644 --- a/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.h +++ b/ApplicationCode/ModelVisualization/RivCrossSectionPartMgr.h @@ -62,10 +62,9 @@ private: RigMainGrid* mainGrid(); cvf::Vec3d extrusionDirection(const std::vector& polyline) const; - + void calculateEclipseTextureCoordinates(cvf::Vec2fArray* textureCoords, const std::vector& triangleToCellIdxMap, const RigResultAccessor* resultAccessor, const cvf::ScalarMapper* mapper) const; private: - cvf::cref m_grid; const RimCrossSection* m_rimCrossSection; cvf::Color3f m_defaultColor;