Mercurial > hg > orthanc-stone
view UnitTestsSources/VolumeRenderingTests.cpp @ 1780:b7c9fd1e9fb0
unit test VolumeRendering.FitTexture
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 May 2021 11:39:11 +0200 |
parents | 9ab251c03eda |
children | bf4b15b059ea |
line wrap: on
line source
/** * Stone of Orthanc * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics * Department, University Hospital of Liege, Belgium * Copyright (C) 2017-2021 Osimis S.A., Belgium * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * This program 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 * Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. **/ #include "../OrthancStone/Sources/Scene2D/CairoCompositor.h" #include "../OrthancStone/Sources/Scene2D/CopyStyleConfigurator.h" #include "../OrthancStone/Sources/Scene2D/ColorTextureSceneLayer.h" #include "../OrthancStone/Sources/Volumes/DicomVolumeImageMPRSlicer.h" #include "../OrthancStone/Sources/Volumes/DicomVolumeImageReslicer.h" #include <Images/ImageProcessing.h> #include <Images/ImageTraits.h> #include <OrthancException.h> #include <gtest/gtest.h> static float GetPixelValue(const Orthanc::ImageAccessor& image, unsigned int x, unsigned int y) { switch (image.GetFormat()) { case Orthanc::PixelFormat_Grayscale8: return Orthanc::ImageTraits<Orthanc::PixelFormat_Grayscale8>::GetFloatPixel(image, x, y); case Orthanc::PixelFormat_Float32: return Orthanc::ImageTraits<Orthanc::PixelFormat_Float32>::GetFloatPixel(image, x, y); case Orthanc::PixelFormat_RGB24: { Orthanc::PixelTraits<Orthanc::PixelFormat_RGB24>::PixelType pixel; Orthanc::ImageTraits<Orthanc::PixelFormat_RGB24>::GetPixel(pixel, image, x, y); return pixel.red_; } case Orthanc::PixelFormat_BGRA32: { Orthanc::PixelTraits<Orthanc::PixelFormat_BGRA32>::PixelType pixel; Orthanc::ImageTraits<Orthanc::PixelFormat_BGRA32>::GetPixel(pixel, image, x, y); return pixel.red_; } default: throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } } static bool IsConstImage(float value, const Orthanc::ImageAccessor& image) { for (unsigned int y = 0; y < image.GetHeight(); y++) { for (unsigned int x = 0; x < image.GetWidth(); x++) { if (!OrthancStone::LinearAlgebra::IsNear(value, GetPixelValue(image, x, y))) { return false; } } } return true; } static bool IsConstRegion(float value, const Orthanc::ImageAccessor& image, unsigned int x, unsigned int y, unsigned int width, unsigned int height) { Orthanc::ImageAccessor region; image.GetRegion(region, x, y, width, height); return IsConstImage(value, region); } static void Assign3x3Pattern(Orthanc::ImageAccessor& image) { if (image.GetFormat() == Orthanc::PixelFormat_Grayscale8 && image.GetWidth() == 3 && image.GetHeight() == 3) { unsigned int v = 0; for (unsigned int y = 0; y < image.GetHeight(); y++) { uint8_t *p = reinterpret_cast<uint8_t*>(image.GetRow(y)); for (unsigned int x = 0; x < image.GetWidth(); x++, p++) { *p = v; v += 25; } } } else { throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); } } static Orthanc::ImageAccessor* Render(const OrthancStone::Scene2D& scene, unsigned int width, unsigned int height) { OrthancStone::CairoCompositor compositor(width, height); compositor.Refresh(scene); Orthanc::ImageAccessor rendered; compositor.GetCanvas().GetReadOnlyAccessor(rendered); return Orthanc::Image::Clone(rendered); } // Render the scene using the identity viewpoint (default) static Orthanc::ImageAccessor* Render(OrthancStone::ISceneLayer* layer, unsigned int width, unsigned int height) { OrthancStone::Scene2D scene; scene.SetLayer(0, layer); return Render(scene, width, height); } enum SlicerType { SlicerType_MPR = 0, SlicerType_Reslicer = 1 }; static OrthancStone::TextureBaseSceneLayer* SliceVolume(boost::shared_ptr<OrthancStone::DicomVolumeImage> volume, const OrthancStone::CoordinateSystem3D& volumeCoordinates, const OrthancStone::CoordinateSystem3D& cuttingPlane, SlicerType type) { Orthanc::DicomMap dicom; dicom.SetValue(Orthanc::DICOM_TAG_STUDY_INSTANCE_UID, "study", false); dicom.SetValue(Orthanc::DICOM_TAG_SERIES_INSTANCE_UID, "series", false); dicom.SetValue(Orthanc::DICOM_TAG_SOP_INSTANCE_UID, "sop", false); volume->SetDicomParameters(OrthancStone::DicomInstanceParameters(dicom)); std::unique_ptr<OrthancStone::IVolumeSlicer> slicer; switch (type) { case SlicerType_MPR: slicer.reset(new OrthancStone::DicomVolumeImageMPRSlicer(volume)); break; case SlicerType_Reslicer: slicer.reset(new OrthancStone::DicomVolumeImageReslicer(volume)); break; default: throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); } std::unique_ptr<OrthancStone::IVolumeSlicer::IExtractedSlice> slice(slicer->ExtractSlice(cuttingPlane)); if (slice->IsValid()) { OrthancStone::CopyStyleConfigurator configurator; return dynamic_cast<OrthancStone::TextureBaseSceneLayer*>(slice->CreateSceneLayer(&configurator, cuttingPlane)); } else { return NULL; } } static OrthancStone::TextureBaseSceneLayer* Slice3x3x1Pattern(const OrthancStone::CoordinateSystem3D& volumeCoordinates, const OrthancStone::CoordinateSystem3D& cuttingPlane, SlicerType type) { OrthancStone::VolumeImageGeometry geometry; geometry.SetSizeInVoxels(3, 3, 1); geometry.SetAxialGeometry(volumeCoordinates); boost::shared_ptr<OrthancStone::DicomVolumeImage> volume(new OrthancStone::DicomVolumeImage); volume->Initialize(geometry, Orthanc::PixelFormat_Grayscale8, false); { OrthancStone::ImageBuffer3D::SliceWriter writer(volume->GetPixelData(), OrthancStone::VolumeProjection_Axial, 0); Assign3x3Pattern(writer.GetAccessor()); } OrthancStone::Vector v = volume->GetGeometry().GetVoxelDimensions(OrthancStone::VolumeProjection_Axial); if (!OrthancStone::LinearAlgebra::IsNear(1, v[0]) || !OrthancStone::LinearAlgebra::IsNear(1, v[1]) || !OrthancStone::LinearAlgebra::IsNear(1, v[2])) { throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); } return SliceVolume(volume, volumeCoordinates, cuttingPlane, type); } TEST(VolumeRendering, Axial) { OrthancStone::CoordinateSystem3D axial(OrthancStone::LinearAlgebra::CreateVector(-0.5, -0.5, 0), OrthancStone::LinearAlgebra::CreateVector(1, 0, 0), OrthancStone::LinearAlgebra::CreateVector(0, 1, 0)); for (unsigned int mode = 0; mode < 2; mode++) { OrthancStone::CoordinateSystem3D cuttingPlane; std::unique_ptr<OrthancStone::TextureBaseSceneLayer> layer(Slice3x3x1Pattern(axial, cuttingPlane, static_cast<SlicerType>(mode))); ASSERT_TRUE(layer.get() != NULL); ASSERT_EQ(OrthancStone::ISceneLayer::Type_FloatTexture, layer->GetType()); OrthancStone::Extent2D box; layer->GetBoundingBox(box); ASSERT_FLOAT_EQ(-1.0f, box.GetX1()); ASSERT_FLOAT_EQ(-1.0f, box.GetY1()); ASSERT_FLOAT_EQ(2.0f, box.GetX2()); ASSERT_FLOAT_EQ(2.0f, box.GetY2()); { const Orthanc::ImageAccessor& texture = dynamic_cast<OrthancStone::TextureBaseSceneLayer&>(*layer).GetTexture(); ASSERT_EQ(3u, texture.GetWidth()); ASSERT_EQ(3u, texture.GetHeight()); ASSERT_FLOAT_EQ(0, GetPixelValue(texture, 0, 0)); ASSERT_FLOAT_EQ(25, GetPixelValue(texture, 1, 0)); ASSERT_FLOAT_EQ(50, GetPixelValue(texture, 2, 0)); ASSERT_FLOAT_EQ(75, GetPixelValue(texture, 0, 1)); ASSERT_FLOAT_EQ(100, GetPixelValue(texture, 1, 1)); ASSERT_FLOAT_EQ(125, GetPixelValue(texture, 2, 1)); ASSERT_FLOAT_EQ(150, GetPixelValue(texture, 0, 2)); ASSERT_FLOAT_EQ(175, GetPixelValue(texture, 1, 2)); ASSERT_FLOAT_EQ(200, GetPixelValue(texture, 2, 2)); } std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(layer.release(), 5, 5)); ASSERT_EQ(5u, rendered->GetWidth()); ASSERT_EQ(5u, rendered->GetHeight()); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 2, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 3, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 4, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 2, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 3, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 4, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 2)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 2)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 2, 2)); ASSERT_FLOAT_EQ(25, GetPixelValue(*rendered, 3, 2)); ASSERT_FLOAT_EQ(50, GetPixelValue(*rendered, 4, 2)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 3)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 3)); ASSERT_FLOAT_EQ(75, GetPixelValue(*rendered, 2, 3)); ASSERT_FLOAT_EQ(100, GetPixelValue(*rendered, 3, 3)); ASSERT_FLOAT_EQ(125, GetPixelValue(*rendered, 4, 3)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 4)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 4)); ASSERT_FLOAT_EQ(150, GetPixelValue(*rendered, 2, 4)); ASSERT_FLOAT_EQ(175, GetPixelValue(*rendered, 3, 4)); ASSERT_FLOAT_EQ(200, GetPixelValue(*rendered, 4, 4)); } } TEST(VolumeRendering, TextureCorners) { // The origin of a 2D texture is the coordinate of the BORDER of the // top-left pixel, *not* the center of the top-left pixel (as in // DICOM 3D convention) Orthanc::Image pixel(Orthanc::PixelFormat_RGB24, 1, 1, false); Orthanc::ImageProcessing::Set(pixel, 255, 0, 0, 255); { std::unique_ptr<OrthancStone::ColorTextureSceneLayer> layer(new OrthancStone::ColorTextureSceneLayer(pixel)); layer->SetOrigin(0, 0); std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(layer.release(), 2, 2)); ASSERT_EQ(2u, rendered->GetWidth()); ASSERT_EQ(2u, rendered->GetHeight()); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 1)); ASSERT_FLOAT_EQ(255, GetPixelValue(*rendered, 1, 1)); } { std::unique_ptr<OrthancStone::ColorTextureSceneLayer> layer(new OrthancStone::ColorTextureSceneLayer(pixel)); layer->SetOrigin(-0.01, 0); std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(layer.release(), 2, 2)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 0)); ASSERT_FLOAT_EQ(255, GetPixelValue(*rendered, 0, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 1)); } { std::unique_ptr<OrthancStone::ColorTextureSceneLayer> layer(new OrthancStone::ColorTextureSceneLayer(pixel)); layer->SetOrigin(-0.01, -0.01); std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(layer.release(), 2, 2)); ASSERT_FLOAT_EQ(255, GetPixelValue(*rendered, 0, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 1)); } { std::unique_ptr<OrthancStone::ColorTextureSceneLayer> layer(new OrthancStone::ColorTextureSceneLayer(pixel)); layer->SetOrigin(0, -0.01); std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(layer.release(), 2, 2)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 0)); ASSERT_FLOAT_EQ(255, GetPixelValue(*rendered, 1, 0)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 0, 1)); ASSERT_FLOAT_EQ(0, GetPixelValue(*rendered, 1, 1)); } } TEST(VolumeRendering, FitTexture) { Orthanc::Image pixel(Orthanc::PixelFormat_RGB24, 1, 1, false); Orthanc::ImageProcessing::Set(pixel, 255, 0, 0, 255); { std::unique_ptr<OrthancStone::ColorTextureSceneLayer> layer(new OrthancStone::ColorTextureSceneLayer(pixel)); layer->SetOrigin(-42.0f, 35.0f); layer->SetPixelSpacing(2, 3); OrthancStone::Scene2D scene; scene.SetLayer(0, layer.release()); scene.FitContent(30, 30); std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(scene, 30, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 5, 30)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 5, 0, 20, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 25, 0, 5, 30)); rendered.reset(Render(scene, 40, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 10, 30)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 10, 0, 20, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 30, 0, 5, 30)); scene.FitContent(40, 30); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 10, 30)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 10, 0, 20, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 30, 0, 5, 30)); rendered.reset(Render(scene, 30, 36)); // The scene has not been fitted ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 30, 3)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 3, 36)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 5, 3, 20, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 25, 0, 5, 36)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 33, 30, 3)); scene.FitContent(30, 36); // Refit rendered.reset(Render(scene, 30, 36)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 3, 36)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 3, 0, 24, 36)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 27, 0, 3, 36)); } { std::unique_ptr<OrthancStone::ColorTextureSceneLayer> layer(new OrthancStone::ColorTextureSceneLayer(pixel)); layer->SetOrigin(42.0f, -35.0f); layer->SetPixelSpacing(3, 2); OrthancStone::Scene2D scene; scene.SetLayer(0, layer.release()); scene.FitContent(30, 30); std::unique_ptr<Orthanc::ImageAccessor> rendered(Render(scene, 30, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 30, 5)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 0, 5, 30, 20)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 25, 30, 5)); rendered.reset(Render(scene, 30, 40)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 30, 10)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 0, 10, 30, 20)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 30, 30, 5)); scene.FitContent(30, 40); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 30, 10)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 0, 10, 30, 20)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 30, 30, 5)); rendered.reset(Render(scene, 36, 30)); // The scene has not been fitted ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 3, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 36, 3)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 3, 5, 30, 20)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 25, 36, 5)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 33, 0, 3, 30)); scene.FitContent(36, 30); // Refit rendered.reset(Render(scene, 36, 30)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 0, 36, 3)); ASSERT_TRUE(IsConstRegion(255.0f, *rendered, 0, 3, 36, 24)); ASSERT_TRUE(IsConstRegion(0.0f, *rendered, 0, 27, 36, 3)); } }