dip::Polygon struct

A polygon with floating-point vertices.

Derived classes

struct dip::ConvexHull
A convex hull is a convex polygon. It can be constructed from a simple dip::Polygon, and is guaranteed clockwise. more...

Aliases

using Vertices = std::vector<VertexFloat>
Type used to store the vertices.

Functions

auto Area() const -> dip::dfloat
Computes the (signed) area of the polygon. The default, clockwise polygons have a positive area.
auto Augment(dip::dfloat distance = 1.0) -> dip::Polygon&
Adds vertices along each edge of the polygon such that the distance between two consecutive vertices is never more than distance.
auto BendingEnergy() const -> dip::dfloat
Computes the bending energy of a polygon. more...
auto BoundingBox() const -> dip::BoundingBoxFloat
Returns the bounding box of the polygon.
auto Centroid() const -> dip::VertexFloat
Computes the centroid of the polygon.
auto Contains(dip::VertexFloat point) const -> bool
Tests if the point is contained in the polygon. more...
auto ConvexHull() const -> dip::ConvexHull
Returns the convex hull of the polygon. The polygon must be simple.
auto CovarianceMatrixSolid(dip::VertexFloat const& g) const -> dip::CovarianceMatrix
Returns the covariance matrix for the solid object represented by the polygon, using centroid g.
auto CovarianceMatrixSolid() const -> dip::CovarianceMatrix
Returns the covariance matrix for the solid object represented by the polygon.
auto CovarianceMatrixVertices(dip::VertexFloat const& g) const -> dip::CovarianceMatrix
Returns the covariance matrix for the vertices of the polygon, using centroid g.
auto CovarianceMatrixVertices() const -> dip::CovarianceMatrix
Returns the covariance matrix for the vertices of the polygon.
auto EllipseVariance() const -> dip::dfloat
Compares a polygon to the ellipse with the same covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse. more...
auto EllipseVariance(dip::VertexFloat const& g, dip::CovarianceMatrix const& C) const -> dip::dfloat
Compares a polygon to the ellipse described by the given centroid and covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse.
auto FitCircle() const -> dip::CircleParameters
Fits a circle to the polygon vertices. more...
auto FitEllipse() const -> dip::EllipseParameters
Fits an ellipse to the polygon vertices. more...
auto FractalDimension(dip::dfloat length = 0) const -> dip::dfloat
Computes the fractal dimension of a polygon. more...
auto IsClockWise() const -> bool
Determine the orientation of the polygon. more...
auto Length() const -> dip::dfloat
Computes the length of the polygon (i.e. perimeter). If the polygon represents a pixelated object, this function will overestimate the object’s perimeter. In this case, use dip::ChainCode::Length instead.
auto Perimeter() const -> dip::dfloat
An alias for dip::Polygon::Length.
auto RadiusStatistics() const -> dip::RadiusValues
Returns statistics on the radii of the polygon. The radii are the distances between the centroid and each of the vertices.
auto RadiusStatistics(dip::VertexFloat const& g) const -> dip::RadiusValues
Returns statistics on the radii of the polygon. The radii are the distances between the given centroid and each of the vertices.
auto Reverse() -> dip::Polygon&
Reverses the orientation of the polygon, converting a clockwise polygon into a counter-clockwise one and vice versa.
auto Rotate(dip::dfloat angle) -> dip::Polygon&
Rotates the polygon around the origin by angle, which is positive for clockwise rotation.
auto Scale(dip::dfloat scale) -> dip::Polygon&
Scales the polygon isotropically by multiplying each vertex coordinate by scale.
auto Scale(dip::dfloat scaleX, dip::dfloat scaleY) -> dip::Polygon&
Scales the polygon anisotropically by multiplying each vertex coordinate by scaleX and scaleY.
auto Simplify(dip::dfloat tolerance = 0.5) -> dip::Polygon&
Simplifies the polygon using the Douglas-Peucker algorithm. more...
auto Smooth(dip::dfloat sigma = 1.0) -> dip::Polygon&
Locally averages the location of vertices of a polygon so it becomes smoother. more...
auto Translate(dip::VertexFloat shift) -> dip::Polygon&
Translates the polygon by shift.

Variables

dip::Polygon::Vertices vertices
The vertices.

Alias documentation

using Vertices = std::vector<VertexFloat>

Type used to store the vertices.

Function documentation

dip::BoundingBoxFloat BoundingBox( ) const

Returns the bounding box of the polygon.

bool IsClockWise( ) const

Determine the orientation of the polygon.

This is a fast algorithm that assumes that the polygon is simple. Non-simple polygons do not have a single orientation anyway.

If the polygon is constructed from a chain code, this function should always return true.

dip::dfloat Area( ) const

Computes the (signed) area of the polygon. The default, clockwise polygons have a positive area.

dip::VertexFloat Centroid( ) const

Computes the centroid of the polygon.

dip::CovarianceMatrix CovarianceMatrixVertices( dip::VertexFloat const& g) const

Returns the covariance matrix for the vertices of the polygon, using centroid g.

dip::CovarianceMatrix CovarianceMatrixVertices( ) const

Returns the covariance matrix for the vertices of the polygon.

dip::CovarianceMatrix CovarianceMatrixSolid( dip::VertexFloat const& g) const

Returns the covariance matrix for the solid object represented by the polygon, using centroid g.

dip::CovarianceMatrix CovarianceMatrixSolid( ) const

Returns the covariance matrix for the solid object represented by the polygon.

dip::dfloat Length( ) const

Computes the length of the polygon (i.e. perimeter). If the polygon represents a pixelated object, this function will overestimate the object’s perimeter. In this case, use dip::ChainCode::Length instead.

dip::dfloat Perimeter( ) const

An alias for dip::Polygon::Length.

dip::RadiusValues RadiusStatistics( ) const

Returns statistics on the radii of the polygon. The radii are the distances between the centroid and each of the vertices.

dip::RadiusValues RadiusStatistics( dip::VertexFloat const& g) const

Returns statistics on the radii of the polygon. The radii are the distances between the given centroid and each of the vertices.

dip::dfloat EllipseVariance( ) const

Compares a polygon to the ellipse with the same covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse.

dip::dfloat EllipseVariance( dip::VertexFloat const& g, dip::CovarianceMatrix const& C) const

Compares a polygon to the ellipse described by the given centroid and covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse.

dip::dfloat FractalDimension( dip::dfloat length = 0) const

Computes the fractal dimension of a polygon.

Fractal dimension is defined as the slope of the polygon length as a function of scale, in a log-log plot. Scale is obtained by smoothing the polygon using dip::Polygon::Smooth. Therefore, it is important that the polygon be densely sampled, use dip::Polygon::Augment if necessary.

length is the length of the polygon (see dip::Polygon::Length). It determines the range of scales used to compute the fractal dimension, so a rough estimate is sufficient. If zero is given as length (the default value), then it is computed.

dip::dfloat BendingEnergy( ) const

Computes the bending energy of a polygon.

The bending energy is the integral along the contour of the square of the curvature. We approximate curvature by, at each vertex, taking the difference in angle between the two edges, and dividing by half the length of the two edges (this is the portion of the boundary associated to the edge).

Note that this approximation is poor when the points are far apart. dip::Polygon::Augment should be used to obtain a densely sampled polygon. It is also beneficial to sufficiently smooth the polygon so it better approximates a smooth curve around the object being measured, see dip::Polygon::Smooth.

dip::CircleParameters FitCircle( ) const

Fits a circle to the polygon vertices.

The circle equation,

\[ (x-c_x)^2 + (y-c_y)^2 = r^2 \quad , \]

can be linearized,

\begin{align} \begin{split} a x + b y + c - x^2 - y^2 &= 0 \\ a &= 2 c_x \\ b &= 2 c_y \\ c &= r^2 - c_x^2 - c_y^2 \end{split} \end{align}

We find the least-squares solution to the problem of fitting the vertex coordinates to this linear equation. This always succeeds, but will not be meaningful if the polygon is not close to a circle.

dip::EllipseParameters FitEllipse( ) const

Fits an ellipse to the polygon vertices.

We find the least-squares solution to the fit of the polygon vertices to the general equation for an ellipse,

\[ a x^2 + b xy + c y^2 + d x + e y - 1 = 0 \]

From the fitted parameters we can compute the ellipse parameters. If \(b^2-4ac >= 0\) , the fit does not correspond to an ellipse, and the function will return a default-initialized dip::EllipseParameters struct (all the values in it are zero). But even if the fit succeeds, if the polygon is not close to an ellipse, the result might not be meaningful. Use dip::CovarianceMatrix::Ellipse for an ellipse fit that is always meaningful.

dip::Polygon& Simplify( dip::dfloat tolerance = 0.5)

Simplifies the polygon using the Douglas-Peucker algorithm.

For a polygon derived from a chain code, setting tolerance to 0.5 leads to a maximum-length digital straight segment representation of the object.

dip::Polygon& Augment( dip::dfloat distance = 1.0)

Adds vertices along each edge of the polygon such that the distance between two consecutive vertices is never more than distance.

dip::Polygon& Smooth( dip::dfloat sigma = 1.0)

Locally averages the location of vertices of a polygon so it becomes smoother.

Uses a Gaussian filter with parameter sigma, which is not interpreted as a physical distance between vertices, but as a distance in number of vertices. That is, the neighboring vertex is at a distance of 1, the next one over at a distance of 2, etc. Therefore, it is important that vertices are approximately equally spaced. dip::Polygon::Augment modifies any polygon to satisfy that requirement.

A polygon derived from the chain code of an object without high curvature, when smoothed with a sigma of 2, will fairly well approximate the original smooth boundary. For objects with higher curvature (including very small objects), choose a smaller sigma.

dip::Polygon& Reverse( )

Reverses the orientation of the polygon, converting a clockwise polygon into a counter-clockwise one and vice versa.

dip::Polygon& Rotate( dip::dfloat angle)

Rotates the polygon around the origin by angle, which is positive for clockwise rotation.

dip::Polygon& Scale( dip::dfloat scale)

Scales the polygon isotropically by multiplying each vertex coordinate by scale.

dip::Polygon& Scale( dip::dfloat scaleX, dip::dfloat scaleY)

Scales the polygon anisotropically by multiplying each vertex coordinate by scaleX and scaleY.

dip::Polygon& Translate( dip::VertexFloat shift)

Translates the polygon by shift.

dip::ConvexHull ConvexHull( ) const

Returns the convex hull of the polygon. The polygon must be simple.

bool Contains( dip::VertexFloat point) const

Tests if the point is contained in the polygon.

If the point lies within numerical precision to the boundary of the polygon, the algorithm will also return true.

Note that, when testing for mutliple points, it likely is more efficient to render the polygon (dip::DrawPolygon2D) and test points by indexing into that image.

Variable documentation