PointSet1.cxx 7.88 KB
Newer Older
1 2
/*=========================================================================

Patrick Imbo's avatar
nomsg  
Patrick Imbo committed
3
  Program:   ORFEO Toolbox
4
  Language:  C++
Patrick Imbo's avatar
nomsg  
Patrick Imbo committed
5 6 7 8 9 10 11 12 13
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.

  Some parts of this code are derived from ITK. See ITKCopyright.txt
  for details.
14 15


16 17
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 19 20
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

//  Software Guide : BeginLatex
//
//  The \code{itk::PointSet} is a basic class intended to represent geometry
//  in the form of a set of points in n-dimensional space. It is the base
//  class for the \code{itk::Mesh} providing the methods necessary to
//  manipulate sets of point. Points can have values associated with
//  them. The type of such values is defined by a template parameter of the
//  \code{itk::PointSet} class (i.e., \code{TPixelType}. Two basic
//  interaction styles of PointSets are available in ITK. These styles are
//  referred to as \emph{static} and \emph{dynamic}. The first style is used
//  when the number of points in the set is known in advance and is not
//  expected to change as a consequence of the manipulations performed on the
//  set. The dynamic style, on the other hand, is intended to support
//  insertion and removal of points in an efficient manner. Distinguishing
//  between the two styles is meant to facilitate the fine tuning of a
//  \code{PointSet}'s behavior while optimizing performance and memory
//  management.
//
//  \index{itk::PointSet}
//  \index{itk::PointSet!Static}
//  \index{itk::PointSet!Dynamic}
//
//  In order to use the PointSet class, its header file should be included.
//
47
//  Software Guide : EndLatex
48 49 50 51 52 53 54 55 56 57 58

// Software Guide : BeginCodeSnippet
#include "itkPointSet.h"
// Software Guide : EndCodeSnippet

int main(int, char *[])
{
  //  Software Guide : BeginLatex
  //
  //  Then we must decide what type of value to associate with the
  //  points. This is generally called the \code{PixelType} in order to make the
59
  //  terminology consistent with the \code{itk::Image}. The PointSet is also
60 61 62 63 64 65
  //  templated over the dimension of the space in which the points are
  //  represented. The following declaration illustrates a typical
  //  instantiation of the PointSet class.
  //
  //  \index{itk::PointSet!Instantiation}
  //
66
  //  Software Guide : EndLatex
67 68

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
69
  typedef itk::PointSet<unsigned short, 2> PointSetType;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  A \code{PointSet} object is created by invoking the \code{New()} method
  //  on its type.  The resulting object must be assigned to a
  //  \code{SmartPointer}.  The PointSet is then reference-counted and can be
  //  shared by multiple objects. The memory allocated for the PointSet will
  //  be released when the number of references to the object is reduced to
  //  zero. This simply means that the user does not need to be concerned
  //  with invoking the \code{Delete()} method on this class.  In fact, the
  //  \code{Delete()} method should \textbf{never} be called directly within
  //  any of the reference-counted ITK classes.
  //
  //  \index{itk::PointSet!New()}
  //  \index{itk::PointSet!Pointer}
  //
87
  //  Software Guide : EndLatex
88 89

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
90
  PointSetType::Pointer pointsSet = PointSetType::New();
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  Following the principles of Generic Programming, the \code{PointSet} class has a
  //  set of associated defined types to ensure that interacting objects can be
  //  declared with compatible types. This set of type definitions is
  //  commonly known as a set of \emph{traits}.  Among them we can find the
  //  \code{PointType} type, for example.  This is the type used by the point set to
  //  represent points in space.  The following declaration takes the point
  //  type as defined in the \code{PointSet} traits and renames it to be conveniently
  //  used in the global namespace.
  //
  //  \index{itk::PointSet!PointType}
  //
106
  //  Software Guide : EndLatex
107 108

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
109
  typedef PointSetType::PointType PointType;
110 111 112 113 114 115 116 117 118 119 120 121
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  The \code{PointType} can now be used to declare point objects to be
  //  inserted in the \code{PointSet}. Points are fairly small objects, so
  //  it is inconvenient to manage them with reference counting and smart
  //  pointers. They are simply instantiated as typical C++ classes. The Point
  //  class inherits the \code{[]} operator from the \code{itk::Array} class.
  //  This makes it possible to access its components using index notation. For
  //  efficiency's sake no bounds checking is performed during index access. It is
  //  the user's responsibility to ensure that the index used is in the range
OTB Bot's avatar
STYLE  
OTB Bot committed
122
  //  $\{0, Dimension-1\}$. Each of the components in the point is associated
123 124 125
  //  with space coordinates. The following code illustrates how to instantiate
  //  a point and initialize its components.
  //
126
  //  Software Guide : EndLatex
127 128 129 130 131 132 133 134 135

  // Software Guide : BeginCodeSnippet
  PointType p0;
  p0[0] = -1.0;     //  x coordinate
  p0[1] = -1.0;     //  y coordinate
  // Software Guide : EndCodeSnippet

  PointType p1;

OTB Bot's avatar
STYLE  
OTB Bot committed
136
  p1[0] =  1.0; // Point 1 = { 1, -1 }
137
  p1[1] = -1.0;
138

OTB Bot's avatar
STYLE  
OTB Bot committed
139
  PointType p2; // Point 2 = { 1, 1 }
140 141
  p2[0] =  1.0;
  p2[1] =  1.0;
142 143 144 145 146 147 148 149 150 151 152

  //  Software Guide : BeginLatex
  //
  //  Points are inserted in the PointSet by using the \code{SetPoint()} method.
  //  This method requires the user to provide a unique identifier for the
  //  point. The identifier is typically an unsigned integer that will enumerate
  //  the points as they are being inserted. The following code shows how three
  //  points are inserted into the PointSet.
  //
  //  \index{itk::PointSet!SetPoint()}
  //
153
  //  Software Guide : EndLatex
154 155

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
156 157 158
  pointsSet->SetPoint(0, p0);
  pointsSet->SetPoint(1, p1);
  pointsSet->SetPoint(2, p2);
159 160 161 162 163 164 165 166 167 168
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  // It is possible to query the PointSet in order to determine how many points
  // have been inserted into it. This is done with the \code{GetNumberOfPoints()}
  // method as illustrated below.
  //
  //  \index{itk::PointSet!GetNumberOfPoints()}
  //
169
  //  Software Guide : EndLatex
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  // Software Guide : BeginCodeSnippet
  const unsigned int numberOfPoints = pointsSet->GetNumberOfPoints();
  std::cout << numberOfPoints << std::endl;
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  // Points can be read from the PointSet by using the \code{GetPoint()} method
  // and the integer identifier. The point is stored in a pointer provided by
  // the user. If the identifier provided does not match an
  // existing point, the method will return \code{false} and the contents of the
  // point will be invalid. The following code illustrates point access
  // using defensive programming.
  //
  //  \index{itk::PointSet!GetPoint()}
  //
187
  //  Software Guide : EndLatex
188 189 190

  // Software Guide : BeginCodeSnippet
  PointType pp;
OTB Bot's avatar
STYLE  
OTB Bot committed
191
  bool      pointExists =  pointsSet->GetPoint(1, &pp);
192

OTB Bot's avatar
STYLE  
OTB Bot committed
193 194
  if (pointExists)
    {
195
    std::cout << "Point is = " << pp << std::endl;
OTB Bot's avatar
STYLE  
OTB Bot committed
196
    }
197 198 199 200 201 202 203 204 205 206
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  // \code{GetPoint()} and \code{SetPoint()} are not the most efficient methods
  // to access points in the PointSet. It is preferable to get direct access
  // to the internal point container defined by the \emph{traits} and use
  // iterators to walk sequentially over the list of points (as shown in
  // the following example).
  //
207
  //  Software Guide : EndLatex
208

209
  return EXIT_SUCCESS;
210
}