Geometry — 2D

Typedefs

typedef CGAL::Arrangement_2<Traits_2> Arrangement_2
typedef Arrangement_2::Ccb_halfedge_const_circulator Ccb_halfedge_const_circulator
typedef Arrangement_2::Face_const_iterator Face_const_iterator
class Arrangement2
#include <arrangement2.h>

Arrangement2.

Class to handle 2D arrangements

Public Functions

Arrangement2() = default

Constructor.

Initializes an empty arrangement

std::vector<Polygon2> getBoundedFaces()

Get the bounded faces of the arrangement

Returns:

A vector of bounded faces

std::vector<Polygon2> getUnboundedFaces()

Get the unbounded faces of the arrangement

Returns:

A vector of unbounded faces

void insert(const Polygon2 &polygon)

Inserts a polygon into the arrangement

Parameters:

polygon – The polygon to insert

void insert(const Polyline2 &polyline)

Inserts a polyline into the arrangement

Parameters:

polyline – The polyline to insert

Private Functions

std::vector<Polygon2> getFaces(uint8_t type)

Get the faces of the arrangement.

Parameters:

type – 0 for bounded faces, 1 for unbounded faces, 2 for all faces

Returns:

A vector of faces

Private Members

Arrangement_2 m_arrangement

The arrangement.

Defines

INT_POINT_MULTIPLIER
class Polygon2 : public Polygon_2
#include <polygon2.h>

A class for storing and manipulating polygons in 2D space.

Public Functions

void add_hole(const Polygon2 &hole)
std::pair<Point_2, Point_2> bounding_box() const

Calculates the bounding box of the polygon.

Returns:

A pair of points representing the minimum and maximum corners of the bounding box.

std::vector<Polygon2> diff(const Polygon2 &other) const

Calculates the difference of the polygon with another polygon.

Parameters:

other – The other polygon.

Returns:

A vector of polygons representing the difference.

std::vector<Polygon2> diff(const std::vector<Polygon2> &other) const
std::pair<std::vector<Polygon2>, std::vector<Polyline2>> do_clip(const std::vector<Polyline2> &polylines) const
std::vector<Polygon2> do_union(const Polygon2 &other) const
std::vector<Polygon2> do_union(const std::vector<Polygon2> &others) const
double double_area() const
void draw()
std::vector<Polygon2> holes() const
bool inside(const Point_2 &point) const

Checks if a point is inside the polygon.

Parameters:

point – The point to check.

Returns:

True if the point is inside the polygon, false otherwise.

std::vector<Polygon2> intersect(const Polygon2 &other) const

Calculates the intersection of the polygon with another polygon.

Parameters:

other – The other polygon.

Returns:

A vector of polygons representing the intersection.

std::vector<Polygon2> intersect(const std::vector<Polygon2> &others) const

Calculates the intersection of the polygon with a vector of polygons.

Parameters:

others – The vector of polygons.

Returns:

A vector of polygons representing the intersection.

bool is_ccw() const

Checks if the points in the polygon are in counter-clockwise order.

Returns:

True if the points are in counter-clockwise order, false otherwise.

std::vector<Polygon2> offset(double distance) const
bool on_boundary(const Point_2 &point) const

Checks if a point is on the boundary of the polygon.

Parameters:

point – The point to check.

Returns:

True if the point is on the boundary of the polygon, false otherwise.

operator std::vector<glm::vec2>() const

Converts the polygon to a vector of 2D vectors.

Returns:

A vector of 2D vectors representing the points of the polygon.

Polygon2() = default

Default constructor.

Creates an empty Polygon2 object.

Polygon2(const Polygon_2 &polygon)
Polygon2(const std::vector<glm::dvec2> &points)

Constructor.

Creates a Polygon2 object from a sequence of points with double precision.

Parameters:

points – The points defining the polygon as double precision.

Pre:

There must be at least 3 points in the sequence.

Pre:

The the first and last point in the sequence must be the same.

Polygon2(const std::vector<glm::vec2> &points)

Constructor.

Creates a Polygon2 object from a sequence of points.

Parameters:

points – The points defining the polygon.

Pre:

There must be at least 3 points in the sequence.

Pre:

The the first and last point in the sequence must be the same.

Polygon2(const std::vector<Point_2> &points)

Constructor.

Creates a Polygon2 object from a sequence of points with single precision.

Parameters:

points – The points defining the polygon as single precision.

Pre:

There must be at least 3 points in the sequence.

Pre:

The the first and last point in the sequence must be the same.

void reverse()

Reverses the order of the points in the polygon.

void scale(glm::dvec2 scale)

Scales the polygon by a factor about the origin.

void set_holes(const std::vector<Polygon2> &holes)
void simplify(double tolerance = 0.001)

Simplifies the polygon by removing nearly co-linear points.

Parameters:

tolerance – The maximum distance a point can be from the line segment formed by its neighboring points to be considered nearly co-linear. Defaults to 0.001.

Polyline2 to_polyline() const
void translate(glm::dvec2 translation)

Translates the polygon by a vector.

Public Static Functions

static std::pair<std::vector<Polygon2>, std::vector<Polyline2>> Clip(const std::vector<Polygon2> &polygons, const std::vector<Polyline2> &polylines)
static std::vector<Polygon2> Difference(const std::vector<Polygon2> &polygons, const std::vector<Polygon2> &others)

Calculates the union of the polygon with another polygon.

Parameters:

other – The other polygon.

Returns:

A vector of polygons representing the union.

static std::vector<Polygon2> Intersection(const std::vector<Polygon2> &polygons, const std::vector<Polygon2> &others)

Calculates the difference of the polygon with a vector of polygons.

Parameters:

others – The vector of polygons.

Returns:

A vector of polygons representing the difference.

static std::vector<Polygon2> Offset(const std::vector<Polygon2> &polygons, double distance)
static std::vector<Polygon2> Union(const std::vector<Polygon2> &polygons, const std::vector<Polygon2> &others)

Private Members

std::vector<Polygon2> m_holes
class Polyline2 : public std::vector<Segment_2>
#include <polyline2.h>

A class for storing and manipulating polylines.

A polyline is a sequence of line segments, defined by a sequence of points.

Public Functions

void append(const Polyline2 &polyline)
double length() const
std::vector<Point_2> points() const

Returns the points of the polyline.

Returns:

The points of the polyline.

Polyline2() = default

Default constructor.

Creates an empty Polyline2 object.

Polyline2(const std::vector<glm::dvec2> &points)

Constructor.

Creates a Polyline2 object from a sequence of points with double precision.

See also

Polygon2 instead.

Parameters:

points – The points defining the polyline as double precision.

Pre:

There must be at least 2 points in the sequence.

Pre:

The the first and last point must NOT be the same. If they are the same,

Polyline2(const std::vector<glm::vec2> &points)

Constructor.

Creates a Polyline2 object from a sequence of points with single precision.

See also

Polygon2 instead.

Parameters:

points – The points defining the polyline as single precision.

Pre:

There must be at least 2 points in the sequence.

Pre:

The the first and last point must NOT be the same. If they are the same,

Polyline2(const std::vector<Point_2> &points)

Constructor.

Creates a Polyline2 object from a sequence of points.

See also

Polygon2 instead.

Parameters:

points – The points defining the polyline.

Pre:

There must be at least 2 points in the sequence.

Pre:

The the first and last point must NOT be the same. If they are the same,

void prepend(const Polyline2 &polyline)
void reverse()

Reverses the order of the points in the polyline.

std::vector<Segment_2> segments() const
void simplify(double tolerance = 0.001)

Remove co-linear points from the polyline.

Parameters:

tolerance – The tolerance for co-linearity.

void translate(const Point_2 &translation)

Private Static Functions

static double Distance(const Point_2 &a, const Point_2 &b)
class MarchingSquares
#include <marching_squares.h>

A class to compute iso-lines from a 2D scalar field using the marching squares algorithm.

This class provides a method to compute iso-lines from a 2D scalar field using the marching squares algorithm. An arbitrary function can be evaluated at each point in the 2D scalar field to determine the iso-value at that point by passing a function pointer.

Public Types

typedef std::pair<std::vector<Polygon2>, std::vector<Polyline2>> CrossSection

This class returns a CrossSection which is a pair of vectors of polygons and polylines.

Note

The polygons represent the closed regions of the iso-lines and the polylines represent open iso-lines.

Public Static Functions

static std::pair<std::vector<glm::dvec2>, std::vector<glm::ivec2>> ComputeIsoLinesDistribution(std::function<double(double, double, double)> &geometry_evaluator, std::function<std::unordered_map<uint8_t, float>(double, double, double)> &distribution_evaluator, size_t channel, double z, const glm::dvec2 &min, const glm::dvec2 &max, const glm::dvec2 &cell_size, double iso_value = 0.0, double epsilon = 1e-12)
static std::pair<std::vector<glm::dvec2>, std::vector<glm::ivec2>> ComputeIsoLinesGeometry(std::function<double(double, double, double)> &evaluator, double z, const glm::dvec2 &min, const glm::dvec2 &max, const glm::dvec2 &cell_size, double iso_value = 0.0, double epsilon = 1e-12)

Compute the iso-lines of a 2D scalar field using the marching squares algorithm.

Warning

The vertices and edges are not connected, so they need to be stitched into polylines using the StitchIsoLinesIntoPolylines method.

Parameters:
  • evaluator – A function pointer to the function that evaluates the scalar field at a given point. This function must take in three arguments: x, y, and z and return a double. The reason that the z argument is included is to allow for the evaluation of a 3D scalar field slice. This avoids the need extract a slice from a 3D scalar field first.

  • z – The z-value of the 2D slice to evaluate the scalar field at.

  • min – The minimum x and y values of the 2D slice.

  • max – The maximum x and y values of the 2D slice.

  • cell_size – The size of the cells in the 2D slice.

  • iso_value – The iso-value to compute the iso-lines at.

  • epsilon – The epsilon value to use when comparing floating point numbers. If you have problems with the iso-lines not connecting during stitching try increasing this value.

Returns:

A pair of vectors of vertices and edges that represent the unconnected iso-lines.

static CrossSection ComputeStitchedIsoLinesDistribution(std::function<double(double, double, double)> &geometry_evaluator, std::function<std::unordered_map<uint8_t, float>(double, double, double)> &distribution_evaluator, size_t channel, double z, const glm::dvec2 &min, const glm::dvec2 &max, const glm::dvec2 &cell_size, double iso_value = 0.0, double epsilon = 1e-12)
static CrossSection ComputeStitchedIsoLinesGeometry(std::function<double(double, double, double)> &evaluator, double z, const glm::dvec2 &min, const glm::dvec2 &max, const glm::dvec2 &cell_size, double iso_value = 0.0, double epsilon = 1e-12)

A convenience method to compute the iso-lines of a 2D scalar field using the marching squares algorithm and then stitch them into polylines using a depth-first search.

See also

ComputeIsoLines and StitchIsoLinesIntoPolylines

Parameters:
  • evaluator – A function pointer to the function that evaluates the scalar field at a given point. This function must take in three arguments: x, y, and z and return a double. The reason that the z argument is included is to allow for the evaluation of a 3D scalar field slice. This avoids the need extract a slice from a 3D scalar field first.

  • z – The z-value of the 2D slice to evaluate the scalar field at.

  • min – The minimum x and y values of the 2D slice.

  • max – The maximum x and y values of the 2D slice.

  • cell_size – The size of the cells in the 2D slice.

  • iso_value – The iso-value to compute the iso-lines at.

  • epsilon – The epsilon value to use when comparing floating point numbers. If you have problems with the iso-lines not connecting during stitching try increasing this value.

Returns:

A pair of vectors of polygons and polylines that represent the CrossSection.

static CrossSection StitchIsoLinesIntoPolylines(const std::vector<glm::dvec2> &vertices, const std::vector<glm::ivec2> &edges)

Stitch the iso-lines into polylines/.

This method takes the vertices and edges from the ComputeIsoLines method and stitches them into polylines. It does this by performing a depth-first search (DFS) over the edges to determine the connected components of the iso-lines. Special care is taken to allow for cycles graph in the case the contours form closed polygons.

Parameters:
  • vertices – The vertices of the iso-lines.

  • edges – The edges of the iso-lines.

Returns:

A pair of vectors of polygons and polylines that represent the CrossSection.

Private Static Functions

static glm::dvec2 Interpolate(const glm::dvec2 &p1, const glm::dvec2 &p2, double val1, double val2, double iso_value = 0.0, double epsilon = 1e-12)

Linearly interpolate between two points.

Parameters:
  • p1 – The first point.

  • p2 – The second point.

  • val1 – The value at the first point.

  • val2 – The value at the second point.

  • iso_value – The iso-value to interpolate at.

Returns:

The interpolated point.

static size_t VectorInsert(std::vector<glm::dvec2> &vertices, const glm::dvec2 &vertex, double epsilon = 1e-12)

Insert a vertex into a vector of vertices if it does not already exist.

Parameters:
  • vertices – The vector of vertices to insert the vertex into.

  • vertex – The vertex to insert.

  • epsilon – The epsilon value to use when comparing floating point numbers.

Returns:

The index of the vertex in the vector.