Mercurial > hg > orthanc-stone
view Framework/Radiography/RadiographyLayer.cpp @ 547:0f43e479b49c ct-pet-dose-struct
Removed old rtstruct demo (correct one on its way) + Many warning fixes (mostly 64-bit ?) + reformat + small fixes + indent (breaking long lines)
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 02 Apr 2019 11:54:28 +0200 |
parents | 77e0eb83ff63 |
children | fd9b9d993fc7 42dadae61fa9 |
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-2019 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 "RadiographyLayer.h" #include <Core/OrthancException.h> namespace OrthancStone { static double Square(double x) { return x * x; } RadiographyLayer::Geometry::Geometry() : hasCrop_(false), panX_(0), panY_(0), angle_(0), resizeable_(false), pixelSpacingX_(1), pixelSpacingY_(1) { } void RadiographyLayer::Geometry::GetCrop(unsigned int &x, unsigned int &y, unsigned int &width, unsigned int &height) const { if (!hasCrop_) throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); // you should probably use RadiographyLayer::GetCrop() or at least call HasCrop() before x = cropX_; y = cropY_; width = cropWidth_; height = cropHeight_; } void RadiographyLayer::UpdateTransform() { transform_ = AffineTransform2D::CreateScaling(geometry_.GetPixelSpacingX(), geometry_.GetPixelSpacingY()); double centerX, centerY; GetCenter(centerX, centerY); transform_ = AffineTransform2D::Combine( AffineTransform2D::CreateOffset(geometry_.GetPanX() + centerX, geometry_.GetPanY() + centerY), AffineTransform2D::CreateRotation(geometry_.GetAngle()), AffineTransform2D::CreateOffset(-centerX, -centerY), transform_); transformInverse_ = AffineTransform2D::Invert(transform_); } void RadiographyLayer::AddToExtent(Extent2D& extent, double x, double y) const { GetTransform().Apply(x, y); extent.AddPoint(x, y); } bool RadiographyLayer::Contains(double x, double y) const { GetTransformInverse().Apply(x, y); unsigned int cropX, cropY, cropWidth, cropHeight; GetCrop(cropX, cropY, cropWidth, cropHeight); return (x >= cropX && x <= cropX + cropWidth && y >= cropY && y <= cropY + cropHeight); } void RadiographyLayer::DrawBorders(CairoContext& context, double zoom) { if (GetControlPointCount() < 3 ) return; cairo_t* cr = context.GetObject(); cairo_set_line_width(cr, 2.0 / zoom); ControlPoint cp; GetControlPoint(cp, 0); cairo_move_to(cr, cp.x, cp.y); for (size_t i = 0; i < GetControlPointCount(); i++) { GetControlPoint(cp, i); cairo_line_to(cr, cp.x, cp.y); } cairo_close_path(cr); cairo_stroke(cr); } RadiographyLayer::RadiographyLayer(MessageBroker& broker, const RadiographyScene& scene) : IObservable(broker), index_(0), hasSize_(false), width_(0), height_(0), prefferedPhotometricDisplayMode_(PhotometricDisplayMode_Default), scene_(scene) { UpdateTransform(); } void RadiographyLayer::ResetCrop() { geometry_.ResetCrop(); UpdateTransform(); } void RadiographyLayer::SetPreferredPhotomotricDisplayMode(PhotometricDisplayMode prefferedPhotometricDisplayMode) { prefferedPhotometricDisplayMode_ = prefferedPhotometricDisplayMode; EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } void RadiographyLayer::SetCrop(unsigned int x, unsigned int y, unsigned int width, unsigned int height) { if (!hasSize_) { throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); } if (x + width > width_ || y + height > height_) { throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); } geometry_.SetCrop(x, y, width, height); UpdateTransform(); EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } void RadiographyLayer::SetGeometry(const Geometry& geometry) { geometry_ = geometry; if (hasSize_) { UpdateTransform(); } EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } void RadiographyLayer::GetCrop(unsigned int& x, unsigned int& y, unsigned int& width, unsigned int& height) const { if (GetGeometry().HasCrop()) { GetGeometry().GetCrop(x, y, width, height); } else { x = 0; y = 0; width = width_; height = height_; } } void RadiographyLayer::SetAngle(double angle) { geometry_.SetAngle(angle); UpdateTransform(); EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } void RadiographyLayer::SetSize(unsigned int width, unsigned int height) { if (hasSize_ && (width != width_ || height != height_)) { throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); } hasSize_ = true; width_ = width; height_ = height; UpdateTransform(); EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } Extent2D RadiographyLayer::GetExtent() const { Extent2D extent; unsigned int x, y, width, height; GetCrop(x, y, width, height); double dx = static_cast<double>(x); double dy = static_cast<double>(y); double dwidth = static_cast<double>(width); double dheight = static_cast<double>(height); AddToExtent(extent, dx, dy); AddToExtent(extent, dx + dwidth, dy); AddToExtent(extent, dx, dy + dheight); AddToExtent(extent, dx + dwidth, dy + dheight); return extent; } bool RadiographyLayer::GetPixel(unsigned int& imageX, unsigned int& imageY, double sceneX, double sceneY) const { if (width_ == 0 || height_ == 0) { return false; } else { GetTransformInverse().Apply(sceneX, sceneY); int x = static_cast<int>(std::floor(sceneX)); int y = static_cast<int>(std::floor(sceneY)); if (x < 0) { imageX = 0; } else if (x >= static_cast<int>(width_)) { imageX = width_; } else { imageX = static_cast<unsigned int>(x); } if (y < 0) { imageY = 0; } else if (y >= static_cast<int>(height_)) { imageY = height_; } else { imageY = static_cast<unsigned int>(y); } return true; } } void RadiographyLayer::SetPan(double x, double y) { geometry_.SetPan(x, y); UpdateTransform(); EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } void RadiographyLayer::SetPixelSpacing(double x, double y) { geometry_.SetPixelSpacing(x, y); UpdateTransform(); EmitMessage(RadiographyLayer::LayerEditedMessage(*this)); } void RadiographyLayer::GetCenter(double& centerX, double& centerY) const { centerX = static_cast<double>(width_) / 2.0; centerY = static_cast<double>(height_) / 2.0; GetTransform().Apply(centerX, centerY); } size_t RadiographyLayer::GetControlPointCount() const {return 4;} void RadiographyLayer::GetControlPoint(ControlPoint& cpScene /* out in scene coordinates */, size_t index) const { unsigned int cropX, cropY, cropWidth, cropHeight; GetCrop(cropX, cropY, cropWidth, cropHeight); ControlPoint cp; switch (index) { case ControlPoint_TopLeftCorner: cp = ControlPoint(cropX, cropY, ControlPoint_TopLeftCorner); break; case ControlPoint_TopRightCorner: cp = ControlPoint(cropX + cropWidth, cropY, ControlPoint_TopRightCorner); break; case ControlPoint_BottomLeftCorner: cp = ControlPoint(cropX, cropY + cropHeight, ControlPoint_BottomLeftCorner); break; case ControlPoint_BottomRightCorner: cp = ControlPoint(cropX + cropWidth, cropY + cropHeight, ControlPoint_BottomRightCorner); break; default: throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); } // transforms image coordinates into scene coordinates GetTransform().Apply(cp.x, cp.y); cpScene = cp; } bool RadiographyLayer::LookupControlPoint(ControlPoint& cpScene /* out */, double x, double y, double zoom, double viewportDistance) const { double threshold = Square(viewportDistance / zoom); for (size_t i = 0; i < GetControlPointCount(); i++) { ControlPoint cp; GetControlPoint(cp, i); double d = Square(cp.x - x) + Square(cp.y - y); if (d <= threshold) { cpScene = cp; return true; } } return false; } }