ITK Getting Started tutorial (10) creating a point set

1. General

itk::PointSet is a base class that represents geometry in the form of point sets in n-dimensional space. It is the base class of the necessary methods to provide the set of operation points for itk::Mesh. Points have values associated with them, and the types of these values are defined by the template parameters of the itk::PointSet class (such as TPixelType). There are two basic interaction modes of PointSets in ITK, static and dynamic. The condition of using the static method is that the number of points in the set is known in advance, and subsequent operations on the point set will not change the number of points. On the other hand, using dynamic method can support efficient interpolation and deletion of points. The difference between the two methods means that it is convenient to fine tune the behavior of the PointSet during performance optimization and memory management.

2. Process

In order to use the PointSet pointset class, you must include its header file# include "itkPointSet.h"

Then we must determine the data type related to the point. In order to be consistent with the terminology of itk::Image, this data type is usually called PixelType. The pointset is also templated on the dimension of the space, and the points will be expressed in the space. The following declaration illustrates a typical example of the pointset pointset class. typedef itk::PointSet< unsigned short, 3 > PointSetType;

The PointSet pointset object is created by calling the new () method on its type. The object must be assigned to a smart pointer. The PointSet reference is then counted and shared with multiple objects. When the count of reference objects decreases to zero, the memory allocated to the PointSet pointset is released. This means that there is no need to call the Delete() method on this class. In fact, never call the Delete() method directly on all reference counting classes in the ITK. PointSetType::Pointer pointsSet = PointSetType::New();

Following the basic principles of generic programming, PointSet has a series of related definition types to ensure that interactive objects with compatible types are declared. These type definitions are well known for a series of features. Pointtype, one of the properties of PointSet class, can be used by point set to represent points in space. The following declaration uses the PointSet pointset feature pointtype and renames it for use in the global namespace. typedef PointSetType::PointType PointType;

You can now use PointType to declare the midpoint object inserted into the PointSet. Points are relatively small objects, so they are not easy to manage using reference counts and smart pointers. Like typical C + + classes, they can be simply instantiated. The point class inherits the [] operator from itk::Array, which makes it possible to use index symbols to access its elements. For efficiency reasons, the boundary check is not performed, and the user ensures that the i index value is within the range of {0, Dimension − 1}. Each element in a point is associated with a spatial coordinate.

The following code illustrates how to instantiate a point and initialize its elements.

PointType p0;
p0[0] = -1.0; // x coordinate
p0[1] = -1.0; // y coordinate
p0[2] = 0.0; // z coordinate

Use the SetPoint() method to insert points into the PointSet. This method requires the user to provide a unique identifier of the point. This identifier is usually an unsigned integer that enumerates all points with an identifier when inserting points into a point set. The following code shows how to insert three points into a point set:

pointsSet->SetPoint( 0, p0 );
pointsSet->SetPoint( 1, p1 );
pointsSet->SetPoint( 2, p2 );

You can query the PointSet to determine the number of points that have been inserted into the PointSet. The code for using the GetNumberOfPoints() method is described below.

const unsigned int numberOfPoints = pointsSet->GetNumberOfPoints();
std::cout << numberOfPoints << std::endl;

You can read points from a PointSet pointset by using the GetPoint() method and an integer identifier. The point is stored in a pointer provided by the user. If the provided identifier does not match the existing point, this method will return false and the content in the point is invalid.

PointType pp;
bool pointExists = pointsSet->GetPoint( 1, & pp );
if( pointExists )
{
std::cout << "Point is = " << pp << std::endl;
}

GetPoint() and SetPoint() are not the most efficient ways to access points in a PointSet. It is more direct and efficient to directly access the internal point container defined by the property and use iterators to access the list of points sequentially.

3. Code

 //  In order to use the PointSet class, you should include its header file.
#include "itkPointSet.h"

int main(int, char *[])
{
	//  Then we must decide what type of value is associated with the point. This is often referred to as PixelType to align the terminology with itk::Image. PointSet s are also templated in the spatial dimension representing points.
	// The following declaration illustrates a typical instantiation of the PointSet class.
	using PointSetType = itk::PointSet<unsigned short, 3>;

	//A PointSet object can be created by calling the New() method of its type. The generated object must be assigned to SmartPointer.
	//The PointSet is then referenced and counted and can be shared by multiple objects. When the reference to the object is reduced to zero, the memory allocated for the PointSet is freed.
	//This simply means that the user does not need to care about calling the Delete() method on this class. In fact, the Delete() method should not be called directly in any ITK class that references counting.
	//  
	PointSetType::Pointer pointsSet = PointSetType::New();
	// Software Guide : EndCodeSnippet


	// Following the basic principles of generic programming, the PointSet has a series of related definition types, which can ensure the declaration of interactive objects with compatible types.
	// These type definitions are well known for a series of features` PointType, one of the characteristics of PointSet class, can be used by point set to represent points in space.
	// The following declaration uses the point set 'PointSet' feature 'pointtype' and renames it for use in the global namespace` typedef PointSetType::PointType PointType;`
	//
	using PointType = PointSetType::PointType;
	// Now you can use 'PointType' to declare the midpoint object inserted into the point set 'PointSet'. Points are relatively small objects, so they are not easy to manage using reference counts and smart pointers.
	// Like typical C + + classes, they can be simply instantiated. The point class inherits the ` [] 'operator from ` itk::Array', which makes it possible to access its elements using index symbols.
	// For efficiency reasons, boundary checking is not performed, and the user ensures that the 'i' index value is within the range of ` {0, Dimension − 1} '. Each element in a point is associated with a spatial coordinate.
	PointType p0;
	p0[0] = -1.0; //  x coordinate
	p0[1] = -1.0; //  y coordinate
	p0[2] = 0.0;  //  z coordinate



	PointType p1;

	p1[0] = 1.0; // Point 1 = { 1,-1,0 }
	p1[1] = -1.0;
	p1[2] = 0.0;


	PointType p2; // Point 2 = { 1,1,0 }
	p2[0] = 1.0;
	p2[1] = 1.0;
	p2[2] = 0.0;

	// Use the 'SetPoint()' method to insert points into the point set 'PointSet'. This method requires the user to provide a unique identifier of the point.
	// This identifier is usually an unsigned integer that enumerates all points with an identifier when inserting points into a point set. The following code shows how to insert three points into a point set:
	pointsSet->SetPoint(0, p0);
	pointsSet->SetPoint(1, p1);
	pointsSet->SetPoint(2, p2);
	// Software Guide : EndCodeSnippet


	//  You can use the 'GetNumberOfPoints()' method to query the number of points that have been inserted into the 'PointSet'.
	const unsigned int numberOfPoints = pointsSet->GetNumberOfPoints();
	std::cout << numberOfPoints << std::endl;
	// You can read points from the point set PointSet by using the 'GetPoint()' method and integer identifier. The point is stored in a pointer provided by the user.
	// If the provided identifier does not match the existing point, this method will return 'false' and the content in the point is invalid.
	PointType pp;
	bool pointExists = pointsSet->GetPoint(1, &pp);

	if (pointExists)
	{
		std::cout << "Point is = " << pp << std::endl;
	}
	// `GetPoint() ` and 'SetPoint() ` are not the most effective ways to access points in the point set' PointSet '.
	// It is more direct and efficient to directly access the internal point container defined by the property and use iterators to access the list of points sequentially.

	return EXIT_SUCCESS;
}

4. Results

Posted by project3 on Thu, 04 Nov 2021 00:29:52 -0700