otbImageReference.h 4.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21 22
#ifndef otbImageReference_h
#define otbImageReference_h
23 24 25 26 27 28 29 30 31 32 33 34

#include "itkPoint.h"

namespace otb
{
namespace ogr
{
/**\ingroup gGeometry
 * \class ImageReference
 * \todo See how mix it with the \c otb::ogr::DataSource wrapper as it was
 * with \c VectorData.
 * \since OTB v 3.14.0
35 36
 *
 * \ingroup OTBGdalAdapters
37 38 39 40 41 42 43 44
 */
template <typename TPrecision> class ImageReference
{
public:
  typedef TPrecision PrecisionType;
  enum { Dimension = 2 };
  /**\name Standard ITK typedefs */
  //@{
45

46 47
  bool GetDebug() const
    {
48
    return m_Holder.GetDebug();
49 50 51 52 53 54 55 56 57
    }
  //@}
  /**\name Template-parameters typedefs */
  //@{
  typedef itk::Vector<PrecisionType, 2> SpacingType;
  typedef itk::Point<PrecisionType, 2>  OriginType;
  typedef itk::Point<PrecisionType, 2>  PointType;
  //@}

58 59
  inline const char * GetNameOfClass()
  {
60
    return "ImageReference";
61 62
  }

63 64 65 66 67
  /** Default constructor.
   * \post <tt>m_Spacing = {1,1}</tt>
   * \post <tt>m_Origin = {0,0}</tt>
   */
  ImageReference(itk::Object const& holder)
68
    : m_Holder(holder)
69 70 71 72 73 74 75 76 77
    {
    m_Spacing.Fill(1);
    m_Origin.Fill(0);
    }
  /** Init constructor.
   * \post <tt>m_Spacing = spacing</tt>
   * \post <tt>m_Origin = origin</tt>
   */
  ImageReference(SpacingType const& spacing, OriginType const& origin, itk::Object const& holder)
78
    : m_Holder(holder) , m_Spacing(spacing), m_Origin(origin)
79 80 81 82 83
    {
    }

  void Modified() const
    {
84
    m_Holder.Modified();
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }

  /**\name Origin property
   * Represents the origin of the geometries in the image coordinates system.
   */
  //@{
  itkGetConstReferenceMacro(Origin, OriginType);     //!< Origin getter.
  itkSetMacro(Origin, OriginType);                   //!< Origin setter.
  void SetOrigin(const TPrecision origin[Dimension]) //!< Origin setter.
    {
    const OriginType p(origin);
    this->SetOrigin(p);
    }
  //@}

  /**\name Spacing property
   * Spacing of the geometries to put in the corresponding image coordinates.
   */
  //@{
  itkGetConstReferenceMacro(Spacing, SpacingType);         //!< Spacing getter.
  void SetSpacing(const SpacingType& spacing)              //!< Spacing setter.
    {
    // itkDebugMacro("setting Spacing to " << spacing);
    if (this->m_Spacing != spacing)
      {
      this->m_Spacing = spacing;
      this->Modified();
      }
    }
  void SetSpacing(const TPrecision spacing[Dimension]) //!< Spacing setter.
    {
    const SpacingType s(spacing);
    this->SetSpacing(s);
    }
  //@}

  /**
   * Projects a point from the Data Source coordinates system to the image
   * coordinates system.
   * \param[in] point  point in Data Source coordinates system
   * \param[out] physicalPoint  point in the image coordinates system.
   * \throw None
   */
  void TransformPointToPhysicalPoint(const PointType& point, PointType& physicalPoint) const
    {
    for (size_t i=0; i!=Dimension; ++i)
      physicalPoint[i] = point[i] * m_Spacing[i] + m_Origin[i];
    }

  /**
   * Projects a point from the Data Source coordinates system to the image
   * coordinates system.
   * \param[in] point  point in Data Source coordinates system
   * \return the point projected in the image coordinates system.
   * \throw None
   */
141
  PointType TransformPointToPhysicalPoint(const PointType& point) const
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    {
    // why no loop on VDimension ?
    PointType physicalPoint;
    for (size_t i=0; i!=Dimension; ++i)
      physicalPoint[i] = point[i] * m_Spacing[i] + m_Origin[i];
    return physicalPoint;
    }

  /**
   * Assignment operator.
   * \param[in] rhs source data to be copied.
   * \throw None
   * \internal
   * Required because of the hack to provide a \c GetDebug() function which is
   * required by \c itkTypeMacro.
   */
  ImageReference& operator=(ImageReference const& rhs)
    {
    m_Spacing = rhs.m_Spacing;
    m_Origin  = rhs.m_Origin;
    return *this;
    }

private:
166
  itk::Object     const& m_Holder;
167 168 169 170 171 172 173 174 175
  SpacingType            m_Spacing;
  OriginType             m_Origin;
};

} // ogr namespace

} // end namespace otb

#ifndef OTB_MANUAL_INSTANTIATION
176
// #include "otbImageReference.hxx"
177 178
#endif

179
#endif // otbImageReference_h