From 94f184b1beb863ac8a1a7ec5b71d19228bcd5b33 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Sat, 9 Aug 2014 22:23:41 +0200 Subject: [PATCH] Added RivTernaryScalarMapper and unit test --- .../ModelVisualization/CMakeLists_files.cmake | 2 + .../CMakeLists.txt | 5 + .../RivTernaryScalarMapper-Test.cpp | 49 +++++++++ .../RivTernaryScalarMapper.cpp | 102 ++++++++++++++++++ .../RivTernaryScalarMapper.h | 47 ++++++++ 5 files changed, 205 insertions(+) create mode 100644 ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/RivTernaryScalarMapper-Test.cpp create mode 100644 ApplicationCode/ModelVisualization/RivTernaryScalarMapper.cpp create mode 100644 ApplicationCode/ModelVisualization/RivTernaryScalarMapper.h diff --git a/ApplicationCode/ModelVisualization/CMakeLists_files.cmake b/ApplicationCode/ModelVisualization/CMakeLists_files.cmake index 9ca5bb10e4..ee8779f9c2 100644 --- a/ApplicationCode/ModelVisualization/CMakeLists_files.cmake +++ b/ApplicationCode/ModelVisualization/CMakeLists_files.cmake @@ -24,6 +24,7 @@ ${CEE_CURRENT_LIST_DIR}RivWellPipesPartMgr.h ${CEE_CURRENT_LIST_DIR}RivWellHeadPartMgr.h ${CEE_CURRENT_LIST_DIR}RivResultToTextureMapper.h ${CEE_CURRENT_LIST_DIR}RivTextureCoordsCreator.h +${CEE_CURRENT_LIST_DIR}RivTernaryScalarMapper.h ) set (SOURCE_GROUP_SOURCE_FILES @@ -45,6 +46,7 @@ ${CEE_CURRENT_LIST_DIR}RivWellPathCollectionPartMgr.cpp ${CEE_CURRENT_LIST_DIR}RivWellPipesPartMgr.cpp ${CEE_CURRENT_LIST_DIR}RivWellHeadPartMgr.cpp ${CEE_CURRENT_LIST_DIR}RivTextureCoordsCreator.cpp +${CEE_CURRENT_LIST_DIR}RivTernaryScalarMapper.cpp ) list(APPEND CODE_HEADER_FILES diff --git a/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/CMakeLists.txt b/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/CMakeLists.txt index 0b0bb00ce8..97e839bfc1 100644 --- a/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/CMakeLists.txt +++ b/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/CMakeLists.txt @@ -19,6 +19,7 @@ include_directories( set( MODEL_VISUALIZATION_CPP_SOURCES ../RivPipeGeometryGenerator.cpp + ../RivTernaryScalarMapper.cpp ) @@ -29,6 +30,7 @@ set( CPP_SOURCES set( UNIT_TEST_CPP_SOURCES main.cpp RivPipeGeometryGenerator-Test.cpp + RivTernaryScalarMapper-Test.cpp ) @@ -37,10 +39,13 @@ set( LINK_LIBRARIES LibRender LibGeometry LibCore + LibGuiQt CommonCode ${OPENGL_LIBRARIES} + ${QT_LIBRARIES} + ) diff --git a/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/RivTernaryScalarMapper-Test.cpp b/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/RivTernaryScalarMapper-Test.cpp new file mode 100644 index 0000000000..f39cedfb77 --- /dev/null +++ b/ApplicationCode/ModelVisualization/ModelVisualization_UnitTests/RivTernaryScalarMapper-Test.cpp @@ -0,0 +1,49 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2011-2012 Statoil ASA, Ceetron AS +// +// 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 "gtest/gtest.h" + +// #include "cvfLibCore.h" +// #include "cvfLibViewing.h" +// #include "cvfLibRender.h" +// #include "cvfLibGeometry.h" +// +// #include "RivPipeGeometryGenerator.h" + +#include "RivTernaryScalarMapper.h" +#include "cvfTextureImage.h" + +#include +#include "cvfqtUtils.h" + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +TEST(TernaryScalarMapperTest, BasicFunctions) +{ + cvf::ref scalarMapper = new RivTernaryScalarMapper(cvf::Color3f::GRAY, 0.8f); + + cvf::ref texImage = new cvf::TextureImage; + scalarMapper->updateTexture(texImage.p()); + + QImage img = cvfqt::Utils::toQImage(*(texImage.p())); + + img.save("c:/tmp/test.bmp"); + +} + diff --git a/ApplicationCode/ModelVisualization/RivTernaryScalarMapper.cpp b/ApplicationCode/ModelVisualization/RivTernaryScalarMapper.cpp new file mode 100644 index 0000000000..0f0f1ed9cf --- /dev/null +++ b/ApplicationCode/ModelVisualization/RivTernaryScalarMapper.cpp @@ -0,0 +1,102 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) Statoil ASA, Ceetron Solutions AS +// +// 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 "RivTernaryScalarMapper.h" +#include "cvfTextureImage.h" + + + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RivTernaryScalarMapper::RivTernaryScalarMapper(const cvf::Color3f& undefScalarColor, float opacityLevel) +: m_undefScalarColor(undefScalarColor), + m_opacityLevel(opacityLevel), + m_textureSize(128, 256) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +cvf::Vec2f RivTernaryScalarMapper::mapToTextureCoord(double soil, double swat, bool isTransparent) +{ + cvf::Vec2f texCoord; + + return texCoord; +} + +//-------------------------------------------------------------------------------------------------- +/// F * +/// * * +/// * * +/// * * Texture in this region is assigned the given opacity level +/// * * +/// D *********** E +/// C * +/// * * +/// * * Texture in this region is opaque +/// * * +/// * * +/// A *********** B +//-------------------------------------------------------------------------------------------------- +bool RivTernaryScalarMapper::updateTexture(cvf::TextureImage* image) +{ + CVF_ASSERT(image); + image->allocate(m_textureSize.x(), m_textureSize.y()); + + // For now fill with white so we can see any errors more easily + image->fill(cvf::Color4ub(cvf::Color3::WHITE)); + + + + cvf::uint halfTextureHeight = m_textureSize.y() / 2; + + // Create texture + + float xStride = static_cast(1.0f / m_textureSize.x()); + float yStride = static_cast(1.0f / halfTextureHeight); + + float sgas_red = 0.0f; + for (int yPos = 0; yPos < halfTextureHeight; yPos++) + { + float soil_green = 0.0f; + for (int xPos = 0; xPos < m_textureSize.x() - yPos; xPos++) + { + float swat_blue = 1.0f - sgas_red - soil_green; + + cvf::Color3f floatCol(sgas_red, soil_green, swat_blue); + + cvf::ubyte rByteCol = floatCol.rByte(); + cvf::ubyte gByteCol = floatCol.gByte(); + cvf::ubyte bByteCol = floatCol.bByte(); + + const cvf::Color4ub clr(rByteCol, gByteCol, bByteCol, 255); + image->setPixel(xPos, yPos, clr); + + // Set opacity + const cvf::Color4ub clrOpacity(rByteCol, gByteCol, bByteCol, 255 * m_opacityLevel); + image->setPixel(xPos, yPos + halfTextureHeight, clrOpacity); + + soil_green += xStride; + } + sgas_red += yStride; + } + + return true; +} diff --git a/ApplicationCode/ModelVisualization/RivTernaryScalarMapper.h b/ApplicationCode/ModelVisualization/RivTernaryScalarMapper.h new file mode 100644 index 0000000000..8da5bf112e --- /dev/null +++ b/ApplicationCode/ModelVisualization/RivTernaryScalarMapper.h @@ -0,0 +1,47 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) Statoil ASA, Ceetron Solutions AS +// +// 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. +// +///////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "cvfBase.h" +#include "cvfObject.h" +#include "cvfVector2.h" +#include "cvfColor3.h" + +namespace cvf +{ + class TextureImage; +} + +//================================================================================================== +/// +//================================================================================================== +class RivTernaryScalarMapper : public cvf::Object +{ +public: + RivTernaryScalarMapper(const cvf::Color3f& undefScalarColor, float opacityLevel); + + cvf::Vec2f mapToTextureCoord(double soil, double swat, bool isTransparent); + bool updateTexture(cvf::TextureImage* image); + +private: + cvf::Color3f m_undefScalarColor; + float m_opacityLevel; + cvf::Vec2ui m_textureSize; +}; +