///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2018- Equinor ASA // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RivPolylineGenerator.h" #include "RiaColorTools.h" #include "RiaGuiApplication.h" #include "RiaPreferences.h" #include "RiuGuiTheme.h" #include "cvfCamera.h" #include "cvfDrawableGeo.h" #include "cvfDrawableText.h" #include "cvfPrimitiveSetDirect.h" #include "cvfPrimitiveSetIndexedUInt.h" #include "cvfqtUtils.h" //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cvf::ref RivPolylineGenerator::createLineAlongPolylineDrawable( const std::vector& polyLine, bool closeLine ) { std::vector> polyLines; polyLines.push_back( polyLine ); return createLineAlongPolylineDrawable( polyLines, closeLine ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cvf::ref RivPolylineGenerator::createLineAlongPolylineDrawable( const std::vector>& polyLines, bool closeLine ) { std::vector lineIndices; std::vector vertices; for ( const std::vector& polyLine : polyLines ) { if ( polyLine.size() < 2 ) continue; size_t verticesCount = vertices.size(); for ( size_t i = 0; i < polyLine.size(); ++i ) { vertices.emplace_back( polyLine[i] ); if ( i < polyLine.size() - 1 ) { lineIndices.push_back( static_cast( verticesCount + i ) ); lineIndices.push_back( static_cast( verticesCount + i + 1 ) ); } } if ( closeLine && vertices.front() != vertices.back() ) { lineIndices.push_back( static_cast( verticesCount + polyLine.size() - 1 ) ); lineIndices.push_back( static_cast( verticesCount ) ); } } if ( vertices.empty() ) return nullptr; cvf::ref vx = new cvf::Vec3fArray; vx->assign( vertices ); cvf::ref idxes = new cvf::UIntArray; idxes->assign( lineIndices ); cvf::ref prim = new cvf::PrimitiveSetIndexedUInt( cvf::PT_LINES ); prim->setIndices( idxes.p() ); cvf::ref polylineGeo = new cvf::DrawableGeo; polylineGeo->setVertexArray( vx.p() ); polylineGeo->addPrimitiveSet( prim.p() ); return polylineGeo; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cvf::ref RivPolylineGenerator::createPointsFromPolylineDrawable( const std::vector& polyLine ) { std::vector> polyLines; polyLines.push_back( polyLine ); return createPointsFromPolylineDrawable( polyLines ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cvf::ref RivPolylineGenerator::createPointsFromPolylineDrawable( const std::vector>& polyLines ) { std::vector vertices; for ( const std::vector& polyLine : polyLines ) { for ( const auto& pl : polyLine ) { vertices.emplace_back( pl ); } } if ( vertices.empty() ) return nullptr; cvf::ref primSet = new cvf::PrimitiveSetDirect( cvf::PT_POINTS ); primSet->setStartIndex( 0 ); primSet->setIndexCount( vertices.size() ); cvf::ref geo = new cvf::DrawableGeo; cvf::ref vx = new cvf::Vec3fArray( vertices ); geo->setVertexArray( vx.p() ); geo->addPrimitiveSet( primSet.p() ); return geo; } /// //-------------------------------------------------------------------------------------------------- cvf::ref RivPolylineGenerator::createSetOfLines( const std::vector>& lines ) { std::vector lineIndices; std::vector vertices; for ( const std::vector& polyLine : lines ) { if ( polyLine.size() < 2 ) continue; size_t verticesCount = vertices.size(); for ( size_t i = 0; i < polyLine.size(); i += 2 ) { vertices.emplace_back( polyLine[i] ); vertices.emplace_back( polyLine[i + 1] ); if ( i < polyLine.size() - 1 ) { lineIndices.push_back( static_cast( verticesCount + i ) ); lineIndices.push_back( static_cast( verticesCount + i + 1 ) ); } } } if ( vertices.empty() ) return nullptr; cvf::ref vx = new cvf::Vec3fArray; vx->assign( vertices ); cvf::ref idxes = new cvf::UIntArray; idxes->assign( lineIndices ); cvf::ref prim = new cvf::PrimitiveSetIndexedUInt( cvf::PT_LINES ); prim->setIndices( idxes.p() ); cvf::ref polylineGeo = new cvf::DrawableGeo; polylineGeo->setVertexArray( vx.p() ); polylineGeo->addPrimitiveSet( prim.p() ); return polylineGeo; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cvf::ref RivPolylineGenerator::createOrientedLabel( bool negativeXDirection, const cvf::Camera* camera, const cvf::Vec3d& labelPosition, const QString& labelText ) { RiaGuiApplication* app = RiaGuiApplication::instance(); auto backgroundColor = RiaPreferences::current()->defaultViewerBackgroundColor; auto fontColor = RiuGuiTheme::getColorByVariableName( "textColor" ); auto font = app->defaultWellLabelFont(); cvf::ref drawableText = new cvf::DrawableText; drawableText->setFont( font ); drawableText->setCheckPosVisible( false ); drawableText->setDrawBorder( true ); drawableText->setDrawBackground( true ); drawableText->setVerticalAlignment( cvf::TextDrawer::BASELINE ); drawableText->setBackgroundColor( backgroundColor ); drawableText->setBorderColor( RiaColorTools::computeOffsetColor( backgroundColor, 0.3f ) ); drawableText->setTextColor( RiaColorTools::fromQColorTo3f( fontColor ) ); cvf::String cvfString = cvfqt::Utils::toString( labelText ); cvf::Vec3d finalPosition = labelPosition; if ( camera ) { cvf::Vec3d windowLabelPosition; camera->project( labelPosition, &windowLabelPosition ); auto oneCharBB = drawableText->textBoundingBox( cvf::String( "A" ), cvf::Vec3f( labelPosition ) ); if ( negativeXDirection ) { auto textBB = drawableText->textBoundingBox( cvfString, cvf::Vec3f( labelPosition ) ); windowLabelPosition.x() -= textBB.extent().x() + oneCharBB.extent().x(); } else { windowLabelPosition.x() += oneCharBB.extent().x(); } camera->unproject( windowLabelPosition, &finalPosition ); } drawableText->addText( cvfString, cvf::Vec3f( finalPosition ) ); return drawableText; }