00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: PointSet3.cxx,v $ 00005 Language: C++ 00006 Date: $Date: 2005/02/08 03:51:53 $ 00007 Version: $Revision: 1.14 $ 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 \doxygen{PointSet} class was designed to interact with the Image class. 00024 // For this reason it was found convenient to allow the points in the set to 00025 // hold values that could be computed from images. The value associated with 00026 // the point is referred as \code{PixelType} in order to make it consistent 00027 // with image terminology. Users can define the type as they please thanks to 00028 // the flexibility offered by the Generic Programming approach used in the 00029 // toolkit. The \code{PixelType} is the first template parameter of the 00030 // PointSet. 00031 // 00032 // \index{itk::PointSet!PixelType} 00033 // 00034 // Software Guide : EndLatex 00035 00036 00037 #include "itkPointSet.h" 00038 00039 int main(int, char *[]) 00040 { 00041 // Software Guide : BeginLatex 00042 // 00043 // The following code defines a particular type for a pixel type and 00044 // instantiates a PointSet class with it. 00045 // 00046 // Software Guide : EndLatex 00047 00048 // Software Guide : BeginCodeSnippet 00049 typedef unsigned short PixelType; 00050 typedef itk::PointSet< PixelType, 3 > PointSetType; 00051 // Software Guide : EndCodeSnippet 00052 00053 00054 // A point set is instantiated here 00055 PointSetType::Pointer pointSet = PointSetType::New(); 00056 00057 00058 // Software Guide : BeginLatex 00059 // 00060 // Data can be inserted into the PointSet using the \code{SetPointData()} 00061 // method. This method requires the user to provide an identifier. The data 00062 // in question will be associated to the point holding the same identifier. 00063 // It is the user's responsibility to verify the appropriate matching between 00064 // inserted data and inserted points. The following line illustrates the use 00065 // of the \code{SetPointData()} method. 00066 // 00067 // \index{itk::PointSet!SetPointData()} 00068 // 00069 // Software Guide : EndLatex 00070 00071 // Software Guide : BeginCodeSnippet 00072 unsigned int dataId = 0; 00073 PixelType value = 79; 00074 pointSet->SetPointData( dataId++, value ); 00075 // Software Guide : EndCodeSnippet 00076 00077 00078 // Software Guide : BeginLatex 00079 // 00080 // Data associated with points can be read from the PointSet using the 00081 // \code{GetPointData()} method. This method requires the user to provide 00082 // the identifier to the point and a valid pointer to a location where the 00083 // pixel data can be safely written. In case the identifier does not match 00084 // any existing identifier on the PointSet the method will return 00085 // \code{false} and the pixel value returned will be invalid. It is the 00086 // user's responsibility to check the returned boolean value before 00087 // attempting to use it. 00088 // 00089 // \index{itk::PointSet!GetPointData()} 00090 // 00091 // Software Guide : EndLatex 00092 00093 // Software Guide : BeginCodeSnippet 00094 00095 const bool found = pointSet->GetPointData( dataId, & value ); 00096 if( found ) 00097 { 00098 std::cout << "Pixel value = " << value << std::endl; 00099 } 00100 // Software Guide : EndCodeSnippet 00101 00102 00103 00104 // Software Guide : BeginLatex 00105 // 00106 // The \code{SetPointData()} and \code{GetPointData()} methods are not the 00107 // most efficient way to get access to point data. It is far more efficient 00108 // to use the Iterators provided by the \code{PointDataContainer}. 00109 // 00110 // Data associated with points is internally stored in 00111 // \code{PointDataContainer}s. In the same way as with points, the actual 00112 // container type used depend on whether the style of the PointSet is static 00113 // or dynamic. Static point sets will use an \doxygen{VectorContainer} while 00114 // dynamic point sets will use an \doxygen{MapContainer}. The type of the 00115 // data container is defined as one of the traits in the PointSet. The 00116 // following declaration illustrates how the type can be taken from the 00117 // traits and used to conveniently declare a similar type on the global 00118 // namespace. 00119 // 00120 // \index{itk::PointSet!PointDataContainer} 00121 // 00122 // Software Guide : EndLatex 00123 00124 // Software Guide : BeginCodeSnippet 00125 typedef PointSetType::PointDataContainer PointDataContainer; 00126 // Software Guide : EndCodeSnippet 00127 00128 00129 // Software Guide : BeginLatex 00130 // 00131 // Using the type it is now possible to create an instance of the data 00132 // container. This is a standard reference counted object, henceforth it 00133 // uses the \code{New()} method for creation and assigns the newly created 00134 // object to a SmartPointer. 00135 // 00136 // \index{PointDataContainer!New()} 00137 // \index{PointDataContainer!Pointer} 00138 // 00139 // Software Guide : EndLatex 00140 00141 00142 // Software Guide : BeginCodeSnippet 00143 PointDataContainer::Pointer pointData = PointDataContainer::New(); 00144 // Software Guide : EndCodeSnippet 00145 00146 00147 // Software Guide : BeginLatex 00148 // 00149 // Pixel data can be inserted in the container with the method 00150 // \code{InsertElement()}. This method requires an identified to be provided 00151 // for each point data. 00152 // 00153 // \index{PointDataContainer!InsertElement()} 00154 // \index{itk::VectorContainer!InsertElement()} 00155 // \index{itk::MapContainer!InsertElement()} 00156 // 00157 // Software Guide : EndLatex 00158 00159 // Software Guide : BeginCodeSnippet 00160 unsigned int pointId = 0; 00161 00162 PixelType value0 = 34; 00163 PixelType value1 = 67; 00164 00165 pointData->InsertElement( pointId++ , value0 ); 00166 pointData->InsertElement( pointId++ , value1 ); 00167 // Software Guide : EndCodeSnippet 00168 00169 00170 // Software Guide : BeginLatex 00171 // 00172 // Finally the PointDataContainer can be assigned to the PointSet. This will 00173 // substitute any previously existing PointDataContainer on the PointSet. The 00174 // assignment is done using the \code{SetPointData()} method. 00175 // 00176 // \index{itk::PointSet!SetPointData()} 00177 // 00178 // Software Guide : EndLatex 00179 00180 // Software Guide : BeginCodeSnippet 00181 pointSet->SetPointData( pointData ); 00182 // Software Guide : EndCodeSnippet 00183 00184 00185 // Software Guide : BeginLatex 00186 // 00187 // The PointDataContainer can be obtained from the PointSet using the 00188 // \code{GetPointData()} method. This method returns a pointer (assigned to 00189 // a SmartPointer) to the actual container owned by the PointSet. 00190 // 00191 // \index{itk::PointSet!GetPointData()} 00192 // 00193 // Software Guide : EndLatex 00194 00195 // Software Guide : BeginCodeSnippet 00196 PointDataContainer::Pointer pointData2 = pointSet->GetPointData(); 00197 // Software Guide : EndCodeSnippet 00198 00199 00200 // Software Guide : BeginLatex 00201 // 00202 // The most efficient way to sequentially visit the data associated with 00203 // points is to use the iterators provided by \code{PointDataContainer}. The 00204 // \code{Iterator} type belongs to the traits of the PointsContainer 00205 // classes. The iterator is not a reference counted class, so it is just 00206 // created directly from the traits without using SmartPointers. 00207 // 00208 // \index{PointDataContainer!Iterator} 00209 // 00210 // Software Guide : EndLatex 00211 00212 // Software Guide : BeginCodeSnippet 00213 typedef PointDataContainer::Iterator PointDataIterator; 00214 // Software Guide : EndCodeSnippet 00215 00216 00217 // Software Guide : BeginLatex 00218 // 00219 // The subsequent use of the iterator follows what you may expect from a STL 00220 // iterator. The iterator to the first point is obtained from the container 00221 // with the \code{Begin()} method and assigned to another iterator. 00222 // 00223 // \index{PointDataContainer!Begin()} 00224 // 00225 // Software Guide : EndLatex 00226 00227 // Software Guide : BeginCodeSnippet 00228 PointDataIterator pointDataIterator = pointData2->Begin(); 00229 // Software Guide : EndCodeSnippet 00230 00231 00232 // Software Guide : BeginLatex 00233 // 00234 // The \code{++} operator on the iterator can be used to advance from one 00235 // data point to the next. The actual value of the PixelType to which the 00236 // iterator is pointing can be obtained with the \code{Value()} 00237 // method. The loop for walking through all the point data can be 00238 // controlled by comparing the current iterator with the iterator returned 00239 // by the \code{End()} method of the PointsContainer. The following lines 00240 // illustrate the typical loop for walking through the point data. 00241 // 00242 // \index{PointDataContainer!End()} 00243 // \index{PointDataContainer!increment ++} 00244 // 00245 // Software Guide : EndLatex 00246 00247 // Software Guide : BeginCodeSnippet 00248 PointDataIterator end = pointData2->End(); 00249 while( pointDataIterator != end ) 00250 { 00251 PixelType p = pointDataIterator.Value(); // access the pixel data 00252 std::cout << p << std::endl; // print the pixel data 00253 ++pointDataIterator; // advance to next pixel/point 00254 } 00255 // Software Guide : EndCodeSnippet 00256 00257 00258 // Software Guide : BeginLatex 00259 // 00260 // Note that as in STL, the iterator returned by the \code{End()} method is 00261 // not a valid iterator. This is called a \emph{past-end} iterator in order 00262 // to indicate that it is the value resulting from advancing one step after 00263 // visiting the last element in the container. 00264 // 00265 // Software Guide : EndLatex 00266 00267 00268 return 0; 00269 } 00270 00271 00272