view Framework/Toolbox/GeometryToolbox.h @ 173:6b0411ac843a wasm

fix captain rt-struct
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 Mar 2018 17:58:55 +0100
parents 316324f42848
children 15d92d93738b
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-2018 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/>.
 **/


#pragma once

#include "LinearAlgebra.h"

namespace OrthancStone
{
  namespace GeometryToolbox
  {
    void ProjectPointOntoPlane(Vector& result,
                               const Vector& point,
                               const Vector& planeNormal,
                               const Vector& planeOrigin);

    bool IsParallel(const Vector& u,
                    const Vector& v);

    bool IsParallelOrOpposite(bool& isOpposite,
                              const Vector& u,
                              const Vector& v);

    bool IntersectTwoPlanes(Vector& p,
                            Vector& direction,
                            const Vector& origin1,
                            const Vector& normal1,
                            const Vector& origin2,
                            const Vector& normal2);

    bool ClipLineToRectangle(double& x1,  // Coordinates of the clipped line (out)
                             double& y1,
                             double& x2,
                             double& y2,
                             const double ax,  // Two points defining the line (in)
                             const double ay,
                             const double bx,
                             const double by,
                             const double& xmin,   // Coordinates of the rectangle (in)
                             const double& ymin,
                             const double& xmax,
                             const double& ymax);

    void GetPixelSpacing(double& spacingX, 
                         double& spacingY,
                         const Orthanc::DicomMap& dicom);

    inline double ProjectAlongNormal(const Vector& point,
                                     const Vector& normal)
    {
      return boost::numeric::ublas::inner_prod(point, normal);
    }

    Matrix CreateRotationMatrixAlongX(double a);

    Matrix CreateRotationMatrixAlongY(double a);

    Matrix CreateRotationMatrixAlongZ(double a);

    Matrix CreateTranslationMatrix(double dx,
                                   double dy,
                                   double dz);

    Matrix CreateScalingMatrix(double sx,
                               double sy,
                               double sz);
    
    bool IntersectPlaneAndSegment(Vector& p,
                                  const Vector& normal,
                                  double d,
                                  const Vector& edgeFrom,
                                  const Vector& edgeTo);

    bool IntersectPlaneAndLine(Vector& p,
                               const Vector& normal,
                               double d,
                               const Vector& origin,
                               const Vector& direction);

    inline float ComputeBilinearInterpolationInternal(float x,
                                                      float y,
                                                      float f00,    // source(x, y)
                                                      float f01,    // source(x + 1, y)
                                                      float f10,    // source(x, y + 1)
                                                      float f11);   // source(x + 1, y + 1)

    inline float ComputeBilinearInterpolation(float x,
                                              float y,
                                              float f00,    // source(x, y)
                                              float f01,    // source(x + 1, y)
                                              float f10,    // source(x, y + 1)
                                              float f11);   // source(x + 1, y + 1)

    inline float ComputeTrilinearInterpolation(float x,
                                               float y,
                                               float z,
                                               float f000,   // source(x, y, z)
                                               float f001,   // source(x + 1, y, z)
                                               float f010,   // source(x, y + 1, z)
                                               float f011,   // source(x + 1, y + 1, z)
                                               float f100,   // source(x, y, z + 1)
                                               float f101,   // source(x + 1, y, z + 1)
                                               float f110,   // source(x, y + 1, z + 1)
                                               float f111);  // source(x + 1, y + 1, z + 1)
  };
}


float OrthancStone::GeometryToolbox::ComputeBilinearInterpolationInternal(float x,
                                                                          float y,
                                                                          float f00,
                                                                          float f01,
                                                                          float f10,
                                                                          float f11)
{
  // This function only works on fractional parts
  assert(x >= 0 && y >= 0 && x < 1 && y < 1);

  // https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_square
  return f00 * (1 - x) * (1 - y) + f01 * x * (1 - y) + f10 * (1 - x) * y + f11 * x * y;
}


float OrthancStone::GeometryToolbox::ComputeBilinearInterpolation(float x,
                                                                  float y,
                                                                  float f00,
                                                                  float f01,
                                                                  float f10,
                                                                  float f11)
{
  assert(x >= 0 && y >= 0);

  // Compute the fractional part of (x,y)
  float xx = x - std::floor(x);
  float yy = y - std::floor(y);

  return ComputeBilinearInterpolationInternal(xx, yy, f00, f01, f10, f11);
}


float OrthancStone::GeometryToolbox::ComputeTrilinearInterpolation(float x,
                                                                   float y,
                                                                   float z,
                                                                   float f000,
                                                                   float f001,
                                                                   float f010,
                                                                   float f011,
                                                                   float f100,
                                                                   float f101,
                                                                   float f110,
                                                                   float f111)
{
  assert(x >= 0 && y >= 0 && z >= 0);

  float xx = x - std::floor(x);
  float yy = y - std::floor(y);
  float zz = z - std::floor(z);

  // "In practice, a trilinear interpolation is identical to two
  // bilinear interpolation combined with a linear interpolation"
  // https://en.wikipedia.org/wiki/Trilinear_interpolation#Method
  float a = ComputeBilinearInterpolationInternal(xx, yy, f000, f001, f010, f011);
  float b = ComputeBilinearInterpolationInternal(xx, yy, f100, f101, f110, f111);

  return (1 - zz) * a + zz * b;
}