mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Created texture and texture coord calculations
This commit is contained in:
parent
94f184b1be
commit
3dbf7cb866
@ -18,18 +18,12 @@
|
||||
|
||||
#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 "cvfqtUtils.h"
|
||||
|
||||
#include <QImage>
|
||||
#include "cvfqtUtils.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@ -43,7 +37,66 @@ TEST(TernaryScalarMapperTest, BasicFunctions)
|
||||
|
||||
QImage img = cvfqt::Utils::toQImage(*(texImage.p()));
|
||||
|
||||
img.save("c:/tmp/test.bmp");
|
||||
|
||||
img.save("c:/tmp/test.png");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(TernaryScalarMapperTest, TextureMapping)
|
||||
{
|
||||
cvf::ref<RivTernaryScalarMapper> scalarMapper = new RivTernaryScalarMapper(cvf::Color3f::GRAY, 0.8f);
|
||||
|
||||
// Without opacity
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(0.0, 0.0, false);
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.y());
|
||||
}
|
||||
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(1.0, 0.0, false);
|
||||
EXPECT_DOUBLE_EQ(1.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.y());
|
||||
}
|
||||
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(0.0, 1.0, false);
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.5, texCoord.y());
|
||||
}
|
||||
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(3.0, 3.0, false);
|
||||
EXPECT_DOUBLE_EQ(1.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.y());
|
||||
}
|
||||
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(-1.0, -1.0, false);
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.y());
|
||||
}
|
||||
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(0.5, 3.0, false);
|
||||
EXPECT_DOUBLE_EQ(0.5, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.25, texCoord.y());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Opacity
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(0.0, 0.0, true);
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(0.5, texCoord.y());
|
||||
}
|
||||
|
||||
{
|
||||
cvf::Vec2f texCoord = scalarMapper->mapToTextureCoord(0.0, 1.0, true);
|
||||
EXPECT_DOUBLE_EQ(0.0, texCoord.x());
|
||||
EXPECT_DOUBLE_EQ(1.0, texCoord.y());
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ RivTernaryScalarMapper::RivTernaryScalarMapper(const cvf::Color3f& undefScalarCo
|
||||
m_opacityLevel(opacityLevel),
|
||||
m_textureSize(128, 256)
|
||||
{
|
||||
setTernaryRanges(0.0, 1.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -36,8 +37,19 @@ RivTernaryScalarMapper::RivTernaryScalarMapper(const cvf::Color3f& undefScalarCo
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
cvf::Vec2f RivTernaryScalarMapper::mapToTextureCoord(double soil, double swat, bool isTransparent)
|
||||
{
|
||||
cvf::Vec2f texCoord;
|
||||
double soilNormalized = (soil - m_rangeMinSoil) * m_soilFactor;
|
||||
soilNormalized = cvf::Math::clamp(soilNormalized, 0.0, 1.0);
|
||||
|
||||
double swatNormalized = (swat - m_rangeMinSwat) * m_swatFactor;
|
||||
swatNormalized = cvf::Math::clamp(swatNormalized, 0.0, 1.0 - soilNormalized);
|
||||
swatNormalized /= 2.0;
|
||||
|
||||
if (isTransparent)
|
||||
{
|
||||
swatNormalized += 0.5;
|
||||
}
|
||||
|
||||
cvf::Vec2f texCoord(static_cast<float>(soilNormalized), static_cast<float>(swatNormalized));
|
||||
return texCoord;
|
||||
}
|
||||
|
||||
@ -73,10 +85,10 @@ bool RivTernaryScalarMapper::updateTexture(cvf::TextureImage* image)
|
||||
float yStride = static_cast<float>(1.0f / halfTextureHeight);
|
||||
|
||||
float sgas_red = 0.0f;
|
||||
for (int yPos = 0; yPos < halfTextureHeight; yPos++)
|
||||
for (cvf::uint yPos = 0; yPos < halfTextureHeight; yPos++)
|
||||
{
|
||||
float soil_green = 0.0f;
|
||||
for (int xPos = 0; xPos < m_textureSize.x() - yPos; xPos++)
|
||||
for (cvf::uint xPos = 0; xPos < m_textureSize.x() - yPos; xPos++)
|
||||
{
|
||||
float swat_blue = 1.0f - sgas_red - soil_green;
|
||||
|
||||
@ -90,7 +102,7 @@ bool RivTernaryScalarMapper::updateTexture(cvf::TextureImage* image)
|
||||
image->setPixel(xPos, yPos, clr);
|
||||
|
||||
// Set opacity
|
||||
const cvf::Color4ub clrOpacity(rByteCol, gByteCol, bByteCol, 255 * m_opacityLevel);
|
||||
const cvf::Color4ub clrOpacity(rByteCol, gByteCol, bByteCol, static_cast<cvf::ubyte>(255 * m_opacityLevel));
|
||||
image->setPixel(xPos, yPos + halfTextureHeight, clrOpacity);
|
||||
|
||||
soil_green += xStride;
|
||||
@ -100,3 +112,17 @@ bool RivTernaryScalarMapper::updateTexture(cvf::TextureImage* image)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RivTernaryScalarMapper::setTernaryRanges(double soilLower, double soilUpper, double swatLower, double swatUpper)
|
||||
{
|
||||
m_rangeMinSoil = soilLower;
|
||||
m_rangeMaxSoil = soilUpper;
|
||||
m_soilFactor = 1.0 / (soilUpper - soilLower);
|
||||
|
||||
m_rangeMinSwat = swatLower;
|
||||
m_rangeMaxSwat = swatUpper;
|
||||
m_swatFactor = 1.0 / (swatUpper - swatLower);
|
||||
}
|
||||
|
@ -36,6 +36,8 @@ class RivTernaryScalarMapper : public cvf::Object
|
||||
public:
|
||||
RivTernaryScalarMapper(const cvf::Color3f& undefScalarColor, float opacityLevel);
|
||||
|
||||
void setTernaryRanges(double soilLower, double soilUpper, double swatLower, double swatUpper);
|
||||
|
||||
cvf::Vec2f mapToTextureCoord(double soil, double swat, bool isTransparent);
|
||||
bool updateTexture(cvf::TextureImage* image);
|
||||
|
||||
@ -43,5 +45,13 @@ private:
|
||||
cvf::Color3f m_undefScalarColor;
|
||||
float m_opacityLevel;
|
||||
cvf::Vec2ui m_textureSize;
|
||||
|
||||
double m_rangeMaxSoil;
|
||||
double m_rangeMinSoil;
|
||||
double m_soilFactor;
|
||||
|
||||
double m_rangeMaxSwat;
|
||||
double m_rangeMinSwat;
|
||||
double m_swatFactor;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user