Commit b3384a78 authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

further work on registration, fixed GlReduction, implemented SAD and SSD into SimilarityMeasure

parent 2dd574cb
......@@ -257,6 +257,7 @@ namespace campvis {
// deactivate Shader and FBO
_shader->deactivate();
_fbo->detachTexture(GL_COLOR_ATTACHMENT0);
_fbo->detachAll();
_fbo->deactivate();
LGL_ERROR;
......
......@@ -59,8 +59,8 @@ namespace campvis {
}
void ImageMappingInformation::updateMatrices() {
_textureToWolrdTransformation = tgt::mat4::createTranslation(_offset) * tgt::mat4::createScale(_voxelSize * _size);
if (! _textureToWolrdTransformation.invert(_worldToTextureTransformation))
_textureToWorldTransformation = tgt::mat4::createTranslation(_offset) * tgt::mat4::createScale(_voxelSize * _size);
if (! _textureToWorldTransformation.invert(_worldToTextureTransformation))
tgtAssert(false, "Could not invert texture-to-world matrix. That should not happen!");
_voxelToWorldTransformation = tgt::mat4::createTranslation(_offset) * tgt::mat4::createScale(_voxelSize);
......@@ -69,7 +69,7 @@ namespace campvis {
}
const tgt::mat4& ImageMappingInformation::getTextureToWorldMatrix() const {
return _textureToWolrdTransformation;
return _textureToWorldTransformation;
}
const tgt::mat4& ImageMappingInformation::getWorldToTextureMatrix() const {
......
......@@ -71,7 +71,7 @@ namespace campvis {
/**
* Gets the transformation matrix from texture to world coordinates.
* \return _textureToWolrdTransformation
* \return _textureToWorldTransformation
*/
const tgt::mat4& getTextureToWorldMatrix() const;
......@@ -117,7 +117,7 @@ namespace campvis {
LinearMapping<float> _realWorldValueMapping; ///< Linear mapping for mapping element values to real world values
tgt::mat4 _textureToWolrdTransformation; ///< Transformation matrix from texture to world coordinates
tgt::mat4 _textureToWorldTransformation; ///< Transformation matrix from texture to world coordinates
tgt::mat4 _worldToTextureTransformation; ///< Transformation matrix from world to texture coordinates
tgt::mat4 _voxelToWorldTransformation; ///< Transformation matrix from voxel to world coordinates
......
......@@ -249,6 +249,7 @@ namespace campvis {
shader->setUniform(texParamsUniform + "._voxelSize", _parent->getMappingInformation().getVoxelSize());
shader->setUniform(texParamsUniform + "._voxelSizeRCP", tgt::vec3(1.f) / _parent->getMappingInformation().getVoxelSize());
shader->setUniform(texParamsUniform + "._textureToWorldMatrix", _parent->getMappingInformation().getTextureToWorldMatrix());
shader->setUniform(texParamsUniform + "._worldToTextureMatrix", _parent->getMappingInformation().getWorldToTextureMatrix());
shader->setUniform(texParamsUniform + "._realWorldMapping", tgt::vec2(_parent->getMappingInformation().getRealWorldMapping()._shift, _parent->getMappingInformation().getRealWorldMapping()._scale));
break;
......@@ -292,5 +293,9 @@ namespace campvis {
return WeaklyTypedPointer(WeaklyTypedPointer::baseType(_texture->getDataType()), _texture->getNumChannels(), _texture->getPixelData());
}
void ImageRepresentationGL::unbind() const {
_texture->unbind();
}
}
\ No newline at end of file
......@@ -118,6 +118,12 @@ namespace campvis {
const std::string& texUniform = "_texture",
const std::string& texParamsUniform = "_textureParams") const;
/**
* Unbinds the currently bound image from the current texture unit.
* \note This is not necessarily this texture but the texture of the same type bound to this texture unit!
*/
void unbind() const;
/**
* Download the OpenGL texture from the GPU memory to the texture cpu buffer.
* \return _texture
......
......@@ -45,6 +45,13 @@ void main() {
vec4 c = texelFetch(_texture, texel + ivec2(0, 1), 0);
vec4 d = texelFetch(_texture, texel + ivec2(1, 1), 0);
out_Color = max(a, max(b, max(c, d)));
//vec4 a = texture(_texture, tmp);
//vec4 b = texture(_texture, texel + vec2(_textureParams._sizeRCP.x, 0));
//vec4 c = texture(_texture, texel + vec2(0, _textureParams._sizeRCP.y));
//vec4 d = texture(_texture, texel + vec2(_textureParams._sizeRCP.x, _textureParams._sizeRCP.y));
//out_Color = max(a, max(b, max(c, d)));
out_Color = a+b+c+d;
//out_Color = vec4(texel, 0.0, 1.0);
//out_Color = vec4(ex_TexCoord, 1.0);
}
......@@ -73,6 +73,17 @@ vec4 getElement3DNormalized(in sampler3D tex, in TextureParameters3D texParams,
return texture(tex, texCoords);
}
/**
* Transforms texture coordinates for texture \a tex to world coordinates using the texture's
* texture-to-world matrix.
* \param texParams TextureParameters3D struct with texture for lookup
* \param texCoords texture coordinates
* \return \a texCoords transformes to woorld coordinates.
*/
vec4 textureToWorld(in TextureParameters3D texParams, in vec4 texCoords) {
return texParams._textureToWorldMatrix * texCoords;
}
/**
* Transforms texture coordinates for texture \a tex to world coordinates using the texture's
* texture-to-world matrix.
......@@ -81,7 +92,18 @@ vec4 getElement3DNormalized(in sampler3D tex, in TextureParameters3D texParams,
* \return \a texCoords transformes to woorld coordinates.
*/
vec4 textureToWorld(in TextureParameters3D texParams, in vec3 texCoords) {
return texParams._textureToWorldMatrix * vec4(texCoords, 1.0);
return textureToWorld(texParams, vec4(texCoords, 1.0));
}
/**
* Transforms world coordinates for texture \a tex to texture coordinates using the texture's
* world-to-texture matrix.
* \param texParams TextureParameters3D struct with texture for lookup
* \param worldCoords world coordinates
* \return \a texCoords transformes to texture coordinates.
*/
vec4 worldToTexture(in TextureParameters3D texParams, in vec4 worldCoords) {
return texParams._worldToTextureMatrix * worldCoords;
}
/**
......@@ -92,7 +114,7 @@ vec4 textureToWorld(in TextureParameters3D texParams, in vec3 texCoords) {
* \return \a texCoords transformes to texture coordinates.
*/
vec4 worldToTexture(in TextureParameters3D texParams, in vec3 worldCoords) {
return texParams._worldToTextureMatrix * vec4(worldCoords, 1.0);
return worldToTexture(texParams, vec4(worldCoords, 1.0));
}
float applyRealWorldMapping(in TextureParameters3D tex, in float value) {
......
......@@ -53,7 +53,7 @@ namespace campvis {
, _fbo(0)
, _renderQuad(0)
{
_shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "core/glsl/glsl/glreduction.frag", "", false);
_shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "core/glsl/tools/glreduction.frag", "", false);
if (_shader == 0) {
LERROR("Could not load Shader for OpenGL reduction. Reduction will not work!");
return;
......@@ -84,9 +84,25 @@ namespace campvis {
return 0.f;
}
tgtAssert(image->getNumChannels() == 1, "Reduction of images with more than one channel currently not implemented! Somebody was too lazy (or stressed - deadline was close) to do that...");
return reduce(repGl->getTexture());
}
float GlReduction::reduce(const tgt::Texture* texture) {
tgtAssert(texture != 0, "Image must not be 0!");
if (_shader == 0) {
LERROR("Could not load Shader for OpenGL reduction. Reduction will not work!");
return 0.f;
}
if (texture == 0) {
LERROR("Empty texture received - nothing to reduce!");
return 0.f;
}
const tgt::svec3& size = image->getSize();
//tgtAssert(texture->getNumChannels() == 1, "Reduction of images with more than one channel currently not implemented! Somebody was too lazy (or stressed - deadline was close) to do that...");
tgtAssert(texture->getDimensions().z == 1, "Reduction of 3D images not yet implemented! Somebody was too lazy (or stressed - deadline was close) to do that...");
std::vector<float> readBackBuffer;
const tgt::ivec3& size = texture->getDimensions();
tgt::vec2 texCoordMultiplier(1.f);
tgt::ivec2 currentSize = size.xy();
reduceSizes(currentSize, texCoordMultiplier);
......@@ -99,8 +115,8 @@ namespace campvis {
// create temporary textures
for (size_t i = 0; i < 2; ++i) {
_tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RED, GL_R32F, GL_FLOAT, tgt::Texture::NEAREST);
//_tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RGBA, GL_RGBA8, GL_FLOAT, tgt::Texture::NEAREST);
//_tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RED, GL_R32F, GL_FLOAT, tgt::Texture::NEAREST);
_tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RGBA, GL_RGBA32F, GL_FLOAT, tgt::Texture::NEAREST);
_tempTextures[i]->uploadTexture();
_tempTextures[i]->setWrapping(tgt::Texture::CLAMP);
}
......@@ -114,9 +130,17 @@ namespace campvis {
// perform first reduction step outside:
_shader->activate();
repGl->bind(_shader, inputUnit);
_fbo->attachTexture(_tempTextures[_readTex]);
_shader->setIgnoreUniformLocationError(true);
inputUnit.activate();
texture->bind();
_shader->setUniform("_texture", inputUnit.getUnitNumber());
_shader->setUniform("_textureParams._size", tgt::vec2(size.xy()));
_shader->setUniform("_textureParams._sizeRCP", tgt::vec2(1.f) / tgt::vec2(size.xy()));
_shader->setUniform("_textureParams._numChannels", static_cast<int>(texture->getNumChannels()));
_shader->setIgnoreUniformLocationError(false);
glViewport(0, 0, currentSize.x, currentSize.y);
_shader->setUniform("_texCoordsMultiplier", texCoordMultiplier);
QuadRdr.renderQuad();
......@@ -144,8 +168,6 @@ namespace campvis {
// read back stuff
GLenum readBackFormat = _tempTextures[_readTex]->getFormat();
size_t channels = _tempTextures[_readTex]->getNumChannels();
std::vector<float> readBackBuffer;
readBackBuffer.resize(currentSize.x * currentSize.y * channels);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(0, 0, currentSize.x, currentSize.y, readBackFormat, GL_FLOAT, &readBackBuffer.front());
......@@ -173,7 +195,7 @@ namespace campvis {
currentSize.y = DIV_CEIL(currentSize.y, 2);
texCoordMultiplier.y /= 2.f;
}
}
}
\ No newline at end of file
......@@ -60,6 +60,8 @@ namespace campvis {
float reduce(const ImageData* image);
float reduce(const tgt::Texture* texture);
private:
/**
......
......@@ -206,6 +206,7 @@ int Texture::calcBpp(GLint internalformat) {
case GL_RGB10:
case GL_R11F_G11F_B10F:
case GL_RGB9_E5:
case GL_R32F:
case GL_RGBA:
case GL_RGBA8:
case GL_RGBA8_SNORM:
......
......@@ -141,6 +141,14 @@ public:
glBindTexture(type_ , id_);
}
/**
* unbind the current texture from the active texture unit and target.
*/
void unbind() const
{
glBindTexture(type_, 0);
}
/**
* Enable texturing on the active texture unit.
*/
......
......@@ -40,6 +40,7 @@
#include "core/classification/simpletransferfunction.h"
#include "core/tools/glreduction.h"
#include "core/tools/quadrenderer.h"
namespace campvis {
......@@ -48,17 +49,19 @@ namespace campvis {
SimilarityMeasure::SimilarityMeasure()
: VisualizationProcessor(0)
, p_referenceId("ReferenceId", "Reference Image", "", DataNameProperty::READ, AbstractProcessor::INVALID_PROPERTIES)
, p_movingId("MovingId", "Moving Image", "", DataNameProperty::READ)
, p_translation("Translation", "Moving Image Translation", tgt::vec3(0.f), tgt::vec3(-1000.f), tgt::vec3(1000.f))
, p_rotation("Rotation", "Moving Image Rotation", tgt::vec3(0.f), tgt::vec3(-90.f), tgt::vec3(90.f))
, p_viewportSize("ViewportSize", "Viewport Size", tgt::ivec2(1), tgt::ivec2(1), tgt::ivec2(1000))
, p_referenceId("ReferenceId", "Reference Image", "", DataNameProperty::READ, AbstractProcessor::VALID)
, p_movingId("MovingId", "Moving Image", "", DataNameProperty::READ, AbstractProcessor::VALID)
, p_translation("Translation", "Moving Image Translation", tgt::vec3(0.f), tgt::vec3(-1000.f), tgt::vec3(1000.f), tgt::vec3(1.f), tgt::vec3(1.f))
, p_rotation("Rotation", "Moving Image Rotation", tgt::vec3(0.f), tgt::vec3(-tgt::PIf), tgt::vec3(tgt::PIf), tgt::vec3(.1f), tgt::vec3(2.f))
, p_viewportSize("ViewportSize", "Viewport Size", tgt::ivec2(1), tgt::ivec2(1), tgt::ivec2(1000), tgt::ivec2(1), AbstractProcessor::VALID)
, p_compute("ComputeSimilarity", "Compute Similarity")
, _shader(0)
{
addProperty(&p_referenceId);
addProperty(&p_movingId);
addProperty(&p_translation);
addProperty(&p_rotation);
addProperty(&p_compute);
_viewportSizeProperty = &p_viewportSize;
}
......@@ -84,26 +87,65 @@ namespace campvis {
ImageRepresentationGL::ScopedRepresentation movingImage(data, p_movingId.getValue());
if (referenceImage != 0 && movingImage != 0) {
FramebufferActivationGuard fag(this);
createAndAttachColorTexture();
createAndAttachDepthTexture();
const tgt::svec3& size = referenceImage->getSize();
p_viewportSize.setValue(size.xy());
// reserve texture units
tgt::TextureUnit referenceUnit, movingUnit;
referenceUnit.activate();
// create temporary texture for result
tgt::Texture* similarityTex = new tgt::Texture(0, tgt::ivec3(p_viewportSize.getValue(), 1), GL_RGBA, GL_RGBA32F, GL_FLOAT, tgt::Texture::NEAREST);
similarityTex->uploadTexture();
similarityTex->setWrapping(tgt::Texture::CLAMP);
// activate FBO and attach texture
_fbo->activate();
const tgt::ivec2& windowSize = p_viewportSize.getValue();
glViewport(0, 0, static_cast<GLsizei>(windowSize.x), static_cast<GLsizei>(windowSize.y));
_fbo->attachTexture(similarityTex);
//_fbo->attachTexture(&similarityTex);
LGL_ERROR;
// bind input images
_shader->activate();
tgt::TextureUnit firstColorUnit, firstDepthUnit, secondColorUnit, secondDepthUnit;
referenceImage->bind(_shader, referenceUnit, "_referenceTexture", "_referenceTextureParams");
movingImage->bind(_shader, movingUnit, "_movingTexture", "_movingTextureParams");
tgt::mat4 registrationMatrix = tgt::mat4::createTranslation(p_translation.getValue())
* tgt::mat4::createRotationZ(p_rotation.getValue().z)
* tgt::mat4::createRotationY(p_rotation.getValue().y)
* tgt::mat4::createRotationX(p_rotation.getValue().x);
const tgt::mat4& w2t = movingImage->getParent()->getMappingInformation().getWorldToTextureMatrix();
const tgt::mat4& t2w = referenceImage->getParent()->getMappingInformation().getTextureToWorldMatrix();
registrationMatrix = w2t * registrationMatrix * t2w;
tgt::mat4 registrationInverse;
if (! registrationMatrix.invert(registrationInverse))
tgtAssert(false, "Could not invert registration matrix. This should not happen!");
// render quad to compute similarity measure by shader
//_shader->setUniform("_registrationMatrix", registrationMatrix);
_shader->setUniform("_registrationInverse", registrationInverse);
QuadRdr.renderQuad();
_shader->deactivate();
// firstImage->bind(_shader, firstColorUnit, firstDepthUnit, "_firstColor", "_firstDepth", "_firstTexParams");
// secondImage->bind(_shader, secondColorUnit, secondDepthUnit, "_secondColor", "_secondDepth", "_secondTexParams");
// _shader->setUniform("_compositingMethod", p_compositingMethod.getOptionValue());
// _shader->setUniform("_alpha", p_alphaValue.getValue());
//
// decorateRenderProlog(data, _shader);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// QuadRdr.renderQuad();
// detach texture and reduce it
data.addData("All glory to the HYPNOTOAD!", new RenderData(_fbo));
_fbo->detachAll();
_fbo->deactivate();
// reduce the juice
GlReduction reducer;
float similarity = reducer.reduce(similarityTex);
LDEBUG("Similarity Measure: " << similarity);
_shader->deactivate();
tgt::TextureUnit::setZeroUnit();
LGL_ERROR;
}
else {
LERROR("No suitable input image found.");
......
......@@ -38,6 +38,7 @@
#include "core/pipeline/abstractprocessordecorator.h"
#include "core/pipeline/visualizationprocessor.h"
#include "core/properties/datanameproperty.h"
#include "core/properties/buttonproperty.h"
#include "core/properties/floatingpointproperty.h"
#include "core/properties/genericproperty.h"
#include "core/properties/optionproperty.h"
......@@ -90,6 +91,8 @@ namespace campvis {
Vec3Property p_translation; ///< Moving image translation
Vec3Property p_rotation; ///< Moving image rotation
ButtonProperty p_compute;
protected:
/// \see AbstractProcessor::updateProperties
void updateProperties(DataContainer& dc);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment