HOME

TheInfoList



OR:

In
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great deal ...
, marching squares is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
that generates
contours Contour may refer to: * Contour (linguistics), a phonetic sound * Pitch contour * Contour (camera system), a 3D digital camera system * Contour, the KDE Plasma 4 interface for tablet devices * Contour line, a curve along which the function h ...
for a two-dimensional
scalar field In mathematics and physics, a scalar field is a function associating a single number to every point in a space – possibly physical space. The scalar may either be a pure mathematical number ( dimensionless) or a scalar physical quantit ...
(rectangular
array An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
of individual numerical values). A similar method can be used to contour 2D triangle meshes. The contours can be of two kinds: * ''Isolines'' – lines following a single data level, or ''isovalue''. * ''Isobands '' – filled areas between isolines. Typical applications include the contour lines on
topographic map In modern mapping, a topographic map or topographic sheet is a type of map characterized by large-scale detail and quantitative representation of relief features, usually using contour lines (connecting points of equal elevation), but histori ...
s or the generation of isobars for
weather map A weather map, also known as synoptic weather chart, displays various meteorological features across a particular area at a particular point in time and has various symbols which all have specific meanings. Such maps have been in use since the mi ...
s. Marching squares takes a similar approach to the 3D
marching cubes Marching cubes is a computer graphics algorithm, published in the 1987 SIGGRAPH proceedings by Lorensen and Cline, for extracting a polygonal mesh of an isosurface from a three-dimensional discrete scalar field (the elements of which are someti ...
algorithm: * Process each cell in the grid independently. * Calculate a cell index using comparisons of the contour level(s) with the data values at the cell corners. * Use a pre-built
lookup table In computer science, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation. The process is termed as "direct addressing" and LUTs differ from hash tables in a way that, to retrieve a value v w ...
, keyed on the cell index, to describe the output geometry for the cell. * Apply
linear interpolation In mathematics, linear interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points. Linear interpolation between two known points If the two known po ...
along the boundaries of the cell to calculate the exact contour position.


Basic algorithm

Here are the steps of the algorithm: Apply a threshold to the 2D field to make a
binary Binary may refer to: Science and technology Mathematics * Binary number, a representation of numbers using only two digits (0 and 1) * Binary function, a function that takes two arguments * Binary operation, a mathematical operation that ta ...
image containing: * 1 where the data value is ''above'' the isovalue * 0 where the data value is ''below'' the isovalue Every 2x2 block of pixels in the binary image forms a contouring cell, so the whole image is represented by a grid of such cells (shown in green in the picture below). Note that this contouring grid is one cell smaller in each direction than the original 2D field. For each cell in the contouring grid: # Compose the 4
bit The bit is the most basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented a ...
s at the corners of the cell to build a binary index: walk around the cell in a
clockwise Two-dimensional rotation can occur in two possible directions. Clockwise motion (abbreviated CW) proceeds in the same direction as a clock's hands: from the top to the right, then down and then to the left, and back up to the top. The opposite ...
direction appending the
bit The bit is the most basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented a ...
to the index, using
bitwise OR In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic ope ...
and left-shift, from
most significant bit In computing, bit numbering is the convention used to identify the bit positions in a binary number. Bit significance and indexing In computing, the least significant bit (LSB) is the bit position in a binary integer representing the binary ...
at the top left, to
least significant bit In computing, bit numbering is the convention used to identify the bit positions in a binary number. Bit significance and indexing In computing, the least significant bit (LSB) is the bit position in a binary integer representing the binary ...
at the bottom left. The resulting 4-bit index can have 16 possible values in the range 0–15. # Use the cell index to access a pre-built
lookup table In computer science, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation. The process is termed as "direct addressing" and LUTs differ from hash tables in a way that, to retrieve a value v w ...
with 16 entries listing the edges needed to represent the cell (shown in the lower right part of the picture below). # Apply
linear interpolation In mathematics, linear interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points. Linear interpolation between two known points If the two known po ...
between the original field data values to find the exact position of the contour line along the edges of the cell.


Disambiguation of saddle points

The contour is ambiguous at
saddle points In mathematics, a saddle point or minimax point is a point on the surface of the graph of a function where the slopes (derivatives) in orthogonal directions are all zero (a critical point), but which is not a local extremum of the function ...
. It is possible to resolve the ambiguity by using the
average In ordinary language, an average is a single number taken as representative of a list of numbers, usually the sum of the numbers divided by how many numbers are in the list (the arithmetic mean). For example, the average of the numbers 2, 3, 4, 7, ...
data value for the center of the cell to choose between different connections of the interpolated points (four images in bottom-right corner):


Isobands

A similar algorithm can be created for filled contour bands within upper and lower threshold values:


Contouring triangle meshes

The same basic algorithm can be applied to triangular meshes, which consist of connected triangles with data assigned to the vertices. For example, a scattered set of data points could be connected with a
Delaunay triangulation In mathematics and computational geometry, a Delaunay triangulation (also known as a Delone triangulation) for a given set P of discrete points in a general position is a triangulation DT(P) such that no point in P is inside the circumcircle ...
to allow the data field to be contoured. A triangular cell is always '' planar'', because it is a '' 2-simplex'' (i.e. specified by n+1 vertices in an n-dimensional space). There is always a unique linear interpolant across a triangle, and no possibility of an ambiguous saddle.


Isolines

The analysis for
isolines A contour line (also isoline, isopleth, or isarithm) of a function of two variables is a curve along which the function has a constant value, so that the curve joins points of equal value. It is a plane section of the three-dimensional gra ...
over triangles is especially simple: there are 3 binary digits, so there are 8 possibilities:


Isobands

The analysis for isobands over triangles requires 3 ternary trits, so there are 27 possibilities:


Dimensions and spaces

The ''data space'' for the Marching Squares algorithm is 2D, because the vertices assigned a data value are connected to their neighbors in a 2D
topological In mathematics, topology (from the Greek words , and ) is concerned with the properties of a geometric object that are preserved under continuous deformations, such as stretching, twisting, crumpling, and bending; that is, without closing ...
grid, but the spatial coordinates assigned to the vertices can be in 2D, 3D or higher dimensions. For example, a triangular mesh may represent a 2D data surface embedded in 3D space, where spatial positions of the vertices and interpolated points along a contour will all have 3 coordinates. Note that the case of squares is ambiguous again, because a
quadrilateral In geometry a quadrilateral is a four-sided polygon, having four edges (sides) and four corners (vertices). The word is derived from the Latin words ''quadri'', a variant of four, and ''latus'', meaning "side". It is also called a tetragon, ...
embedded in 3-dimensional space is not necessarily planar, so there is a choice of geometrical interpolation scheme to draw the banded surfaces in 3D.


Performance considerations

The algorithm is
embarrassingly parallel In parallel computing, an embarrassingly parallel workload or problem (also called embarrassingly parallelizable, perfectly parallel, delightfully parallel or pleasingly parallel) is one where little or no effort is needed to separate the problem ...
, because all cells are processed independently. It is easy to write a
parallel algorithm In computer science, a parallel algorithm, as opposed to a traditional serial algorithm, is an algorithm which can do multiple operations in a given time. It has been a tradition of computer science to describe serial algorithms in abstract machine ...
assuming: * Shared read-only input scalar field. * Shared append-only geometry output stream. A naive implementation of Marching Squares that processes every cell independently will perform every
linear interpolation In mathematics, linear interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points. Linear interpolation between two known points If the two known po ...
twice (isoline) or four times (isoband). Similarly, the output will contain 2 copies of the 2D vertices for disjoint lines (isoline) or 4 copies for polygons (isobands). nder the assumptions that: the grid is large, so that most cells are internal; and a full contiguous set of isobands is being created. It is possible to reduce the computational overhead by caching the results of interpolation. For example, a single-threaded serial version would only need to cache interpolated results for one row of the input grid. It is also possible to reduce the size of the output by using indexed geometric primitives, ''i.e.'' create an
array An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
of 2D vertices and specify lines or polygons with
short integer In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are ...
offsets into the array.


References

* * * * * * {{cite journal, first1=Marina P. , last1=Cipolletti , first2=Claudio A. , last2=Delrieux , first3=Gerardo M. E. , last3=Perillo , first4=M. Cintia , last4=Piccolo , title=Superresolution border segmentation and measurement in remote sensing images , journal=Comp. Geosci. , year=2012 , volume=40 , pages=87–97 , doi=10.1016/j.cageo.2011.07.015, bibcode=2012CG.....40...87C


External links


Marching Square Matlab algorithm
– An easy to understand open-source marching square algorithm.
implementation
in Java
Marching Squares code
in Java. Given a 2D data set and thresholds, returns GeneralPath[] for easy plotting.
Meandering Triangles
explanation and sample Python implementation.
Marching Squares code in C
– A single header library for marching squares that can export triangle meshes for easy rendering. Computer graphics algorithms