PointSet1.cxx 7.88 KB
 Jordi Inglada committed Apr 10, 2006 1 2 /*=========================================================================  Patrick Imbo committed Jun 29, 2006 3  Program: ORFEO Toolbox  Jordi Inglada committed Apr 10, 2006 4  Language: C++  Patrick Imbo committed Jun 29, 2006 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.  Jordi Inglada committed Apr 10, 2006 14 15   Emmanuel Christophe committed Dec 06, 2008 16 17  This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR  Jordi Inglada committed Apr 10, 2006 18 19 20  PURPOSE. See the above copyright notices for more information. =========================================================================*/  Emmanuel Christophe committed May 07, 2011 21   Jordi Inglada committed Apr 10, 2006 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. //  Emmanuel Christophe committed Dec 06, 2008 47 // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 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  Emmanuel Christophe committed Dec 06, 2008 59  // terminology consistent with the \code{itk::Image}. The PointSet is also  Jordi Inglada committed Apr 10, 2006 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} //  Emmanuel Christophe committed Dec 06, 2008 66  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 67 68  // Software Guide : BeginCodeSnippet  OTB Bot committed Apr 01, 2010 69  typedef itk::PointSet PointSetType;  Jordi Inglada committed Apr 10, 2006 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} //  Emmanuel Christophe committed Dec 06, 2008 87  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 88 89  // Software Guide : BeginCodeSnippet  OTB Bot committed Apr 01, 2010 90  PointSetType::Pointer pointsSet = PointSetType::New();  Jordi Inglada committed Apr 10, 2006 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} //  Emmanuel Christophe committed Dec 06, 2008 106  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 107 108  // Software Guide : BeginCodeSnippet  OTB Bot committed Apr 01, 2010 109  typedef PointSetType::PointType PointType;  Jordi Inglada committed Apr 10, 2006 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 committed Jan 31, 2011 122  // $\{0, Dimension-1\}$. Each of the components in the point is associated  Jordi Inglada committed Apr 10, 2006 123 124 125  // with space coordinates. The following code illustrates how to instantiate // a point and initialize its components. //  Emmanuel Christophe committed Dec 06, 2008 126  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 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 committed Jan 31, 2011 136  p1[0] = 1.0; // Point 1 = { 1, -1 }  Emmanuel Christophe committed Dec 06, 2008 137  p1[1] = -1.0;  Jordi Inglada committed Apr 10, 2006 138   OTB Bot committed Jan 31, 2011 139  PointType p2; // Point 2 = { 1, 1 }  Emmanuel Christophe committed Dec 06, 2008 140 141  p2[0] = 1.0; p2[1] = 1.0;  Jordi Inglada committed Apr 10, 2006 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()} //  Emmanuel Christophe committed Dec 06, 2008 153  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 154 155  // Software Guide : BeginCodeSnippet  OTB Bot committed Apr 01, 2010 156 157 158  pointsSet->SetPoint(0, p0); pointsSet->SetPoint(1, p1); pointsSet->SetPoint(2, p2);  Jordi Inglada committed Apr 10, 2006 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()} //  Emmanuel Christophe committed Dec 06, 2008 169  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 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()} //  Emmanuel Christophe committed Dec 06, 2008 187  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 188 189 190  // Software Guide : BeginCodeSnippet PointType pp;  OTB Bot committed Apr 01, 2010 191  bool pointExists = pointsSet->GetPoint(1, &pp);  Jordi Inglada committed Apr 10, 2006 192   OTB Bot committed Apr 01, 2010 193 194  if (pointExists) {  Jordi Inglada committed Apr 10, 2006 195  std::cout << "Point is = " << pp << std::endl;  OTB Bot committed Apr 01, 2010 196  }  Jordi Inglada committed Apr 10, 2006 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). //  Emmanuel Christophe committed Dec 06, 2008 207  // Software Guide : EndLatex  Jordi Inglada committed Apr 10, 2006 208   Emmanuel Christophe committed Nov 17, 2008 209  return EXIT_SUCCESS;  Jordi Inglada committed Apr 10, 2006 210 }