00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: PointSet1.cxx,v $ 00005 Language: C++ 00006 Date: $Date: 2005-02-08 03:51:53 $ 00007 Version: $Revision: 1.16 $ 00008 00009 Copyright (c) Insight Software Consortium. All rights reserved. 00010 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 #if defined(_MSC_VER) 00018 #pragma warning ( disable : 4786 ) 00019 #endif 00020 00021 // Software Guide : BeginLatex 00022 // 00023 // The \code{itk::PointSet} is a basic class intended to represent geometry 00024 // in the form of a set of points in n-dimensional space. It is the base 00025 // class for the \code{itk::Mesh} providing the methods necessary to 00026 // manipulate sets of point. Points can have values associated with 00027 // them. The type of such values is defined by a template parameter of the 00028 // \code{itk::PointSet} class (i.e., \code{TPixelType}. Two basic 00029 // interaction styles of PointSets are available in ITK. These styles are 00030 // referred to as \emph{static} and \emph{dynamic}. The first style is used 00031 // when the number of points in the set is known in advance and is not 00032 // expected to change as a consequence of the manipulations performed on the 00033 // set. The dynamic style, on the other hand, is intended to support 00034 // insertion and removal of points in an efficient manner. Distinguishing 00035 // between the two styles is meant to facilitate the fine tuning of a 00036 // \code{PointSet}'s behavior while optimizing performance and memory 00037 // management. 00038 // 00039 // \index{itk::PointSet} 00040 // \index{itk::PointSet!Static} 00041 // \index{itk::PointSet!Dynamic} 00042 // 00043 // In order to use the PointSet class, its header file should be included. 00044 // 00045 // Software Guide : EndLatex 00046 00047 // Software Guide : BeginCodeSnippet 00048 #include "itkPointSet.h" 00049 // Software Guide : EndCodeSnippet 00050 00051 int main(int, char *[]) 00052 { 00053 // Software Guide : BeginLatex 00054 // 00055 // Then we must decide what type of value to associate with the 00056 // points. This is generally called the \code{PixelType} in order to make the 00057 // terminology consistent with the \code{itk::Image}. The PointSet is also 00058 // templated over the dimension of the space in which the points are 00059 // represented. The following declaration illustrates a typical 00060 // instantiation of the PointSet class. 00061 // 00062 // \index{itk::PointSet!Instantiation} 00063 // 00064 // Software Guide : EndLatex 00065 00066 // Software Guide : BeginCodeSnippet 00067 typedef itk::PointSet< unsigned short, 3 > PointSetType; 00068 // Software Guide : EndCodeSnippet 00069 00070 00071 // Software Guide : BeginLatex 00072 // 00073 // A \code{PointSet} object is created by invoking the \code{New()} method 00074 // on its type. The resulting object must be assigned to a 00075 // \code{SmartPointer}. The PointSet is then reference-counted and can be 00076 // shared by multiple objects. The memory allocated for the PointSet will 00077 // be released when the number of references to the object is reduced to 00078 // zero. This simply means that the user does not need to be concerned 00079 // with invoking the \code{Delete()} method on this class. In fact, the 00080 // \code{Delete()} method should \textbf{never} be called directly within 00081 // any of the reference-counted ITK classes. 00082 // 00083 // \index{itk::PointSet!New()} 00084 // \index{itk::PointSet!Pointer} 00085 // 00086 // Software Guide : EndLatex 00087 00088 // Software Guide : BeginCodeSnippet 00089 PointSetType::Pointer pointsSet = PointSetType::New(); 00090 // Software Guide : EndCodeSnippet 00091 00092 00093 // Software Guide : BeginLatex 00094 // 00095 // Following the principles of Generic Programming, the \code{PointSet} class has a 00096 // set of associated defined types to ensure that interacting objects can be 00097 // declared with compatible types. This set of type definitions is 00098 // commonly known as a set of \emph{traits}. Among them we can find the 00099 // \code{PointType} type, for example. This is the type used by the point set to 00100 // represent points in space. The following declaration takes the point 00101 // type as defined in the \code{PointSet} traits and renames it to be conveniently 00102 // used in the global namespace. 00103 // 00104 // \index{itk::PointSet!PointType} 00105 // 00106 // Software Guide : EndLatex 00107 00108 // Software Guide : BeginCodeSnippet 00109 typedef PointSetType::PointType PointType; 00110 // Software Guide : EndCodeSnippet 00111 00112 00113 // Software Guide : BeginLatex 00114 // 00115 // The \code{PointType} can now be used to declare point objects to be 00116 // inserted in the \code{PointSet}. Points are fairly small objects, so 00117 // it is inconvenient to manage them with reference counting and smart 00118 // pointers. They are simply instantiated as typical C++ classes. The Point 00119 // class inherits the \code{[]} operator from the \code{itk::Array} class. 00120 // This makes it possible to access its components using index notation. For 00121 // efficiency's sake no bounds checking is performed during index access. It is 00122 // the user's responsibility to ensure that the index used is in the range 00123 // $\{0,Dimension-1\}$. Each of the components in the point is associated 00124 // with space coordinates. The following code illustrates how to instantiate 00125 // a point and initialize its components. 00126 // 00127 // Software Guide : EndLatex 00128 00129 // Software Guide : BeginCodeSnippet 00130 PointType p0; 00131 p0[0] = -1.0; // x coordinate 00132 p0[1] = -1.0; // y coordinate 00133 p0[2] = 0.0; // z coordinate 00134 // Software Guide : EndCodeSnippet 00135 00136 00137 PointType p1; 00138 00139 p1[0] = 1.0; // Point 1 = { 1,-1,0 } 00140 p1[1] = -1.0; 00141 p1[2] = 0.0; 00142 00143 00144 PointType p2; // Point 2 = { 1,1,0 } 00145 p2[0] = 1.0; 00146 p2[1] = 1.0; 00147 p2[2] = 0.0; 00148 00149 // Software Guide : BeginLatex 00150 // 00151 // Points are inserted in the PointSet by using the \code{SetPoint()} method. 00152 // This method requires the user to provide a unique identifier for the 00153 // point. The identifier is typically an unsigned integer that will enumerate 00154 // the points as they are being inserted. The following code shows how three 00155 // points are inserted into the PointSet. 00156 // 00157 // \index{itk::PointSet!SetPoint()} 00158 // 00159 // Software Guide : EndLatex 00160 00161 // Software Guide : BeginCodeSnippet 00162 pointsSet->SetPoint( 0, p0 ); 00163 pointsSet->SetPoint( 1, p1 ); 00164 pointsSet->SetPoint( 2, p2 ); 00165 // Software Guide : EndCodeSnippet 00166 00167 00168 // Software Guide : BeginLatex 00169 // 00170 // It is possible to query the PointSet in order to determine how many points 00171 // have been inserted into it. This is done with the \code{GetNumberOfPoints()} 00172 // method as illustrated below. 00173 // 00174 // \index{itk::PointSet!GetNumberOfPoints()} 00175 // 00176 // Software Guide : EndLatex 00177 00178 // Software Guide : BeginCodeSnippet 00179 const unsigned int numberOfPoints = pointsSet->GetNumberOfPoints(); 00180 std::cout << numberOfPoints << std::endl; 00181 // Software Guide : EndCodeSnippet 00182 00183 00184 // Software Guide : BeginLatex 00185 // 00186 // Points can be read from the PointSet by using the \code{GetPoint()} method 00187 // and the integer identifier. The point is stored in a pointer provided by 00188 // the user. If the identifier provided does not match an 00189 // existing point, the method will return \code{false} and the contents of the 00190 // point will be invalid. The following code illustrates point access 00191 // using defensive programming. 00192 // 00193 // \index{itk::PointSet!GetPoint()} 00194 // 00195 // Software Guide : EndLatex 00196 00197 // Software Guide : BeginCodeSnippet 00198 PointType pp; 00199 bool pointExists = pointsSet->GetPoint( 1, & pp ); 00200 00201 if( pointExists ) 00202 { 00203 std::cout << "Point is = " << pp << std::endl; 00204 } 00205 // Software Guide : EndCodeSnippet 00206 00207 00208 // Software Guide : BeginLatex 00209 // 00210 // \code{GetPoint()} and \code{SetPoint()} are not the most efficient methods 00211 // to access points in the PointSet. It is preferable to get direct access 00212 // to the internal point container defined by the \emph{traits} and use 00213 // iterators to walk sequentially over the list of points (as shown in 00214 // the following example). 00215 // 00216 // Software Guide : EndLatex 00217 00218 return 0; 00219 } 00220 00221