Public Types | Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
o2scl::contour Class Reference

Calculate contour lines from a two-dimensional data set. More...

#include <contour.h>

Public Types

typedef boost::numeric::ublas::vector< double > ubvector
 
typedef boost::numeric::ublas::vector< int > ubvector_int
 
typedef boost::numeric::ublas::matrix< double > ubmatrix
 

Public Member Functions

Basic usage
template<class vec_t , class mat_t >
void set_data (size_t sizex, size_t sizey, const vec_t &x_fun, const vec_t &y_fun, const mat_t &udata)
 Set the data. More...
 
template<class mat_t >
void set_data (const uniform_grid< double > &ugx, const uniform_grid< double > &ugy, const mat_t &udata)
 Set the data. More...
 
template<class vec_t >
void set_levels (size_t nlevels, vec_t &ulevels)
 Set the contour levels. More...
 
void calc_contours (std::vector< contour_line > &clines)
 Calculate the contours. More...
 
Regrid function
void regrid_data (size_t xfact, size_t yfact, size_t interp_type=o2scl::itp_cspline)
 Regrid the data. More...
 
Obtain internal data
void get_data (size_t &sizex, size_t &sizey, ubvector *&x_fun, ubvector *&y_fun, ubmatrix *&udata)
 Get the data. More...
 
void get_edges (std::vector< edge_crossings > &x_edges, std::vector< edge_crossings > &y_edges)
 Return the edges for each contour level. More...
 
void print_edges_yhoriz (edge_crossings &xedges, edge_crossings &yedges)
 Print out the edges to cout.
 
void print_edges_xhoriz (edge_crossings &xedges, edge_crossings &yedges)
 Print out the edges to cout.
 

Public Attributes

int verbose
 Verbosity parameter (default 0) More...
 
double lev_adjust
 (default $ 10^{-8} $)
 
bool debug_next_point
 If true, debug the functions which determine the next point functions (default false)p.
 

Static Public Attributes

Edge status
static const int empty =0
 
static const int edge =1
 
static const int contourp =2
 
static const int endpoint =3
 

Protected Member Functions

int find_next_point_y_direct (int j, int k, int &jnext, int &knext, int &dir_next, int nsw, edge_crossings &xedges, edge_crossings &yedges)
 Find next point starting from a point on a right edge.
 
int find_next_point_x_direct (int j, int k, int &jnext, int &knext, int &dir_next, int nsw, edge_crossings &xedges, edge_crossings &yedges)
 Find next point starting from a point on a bottom edge.
 
void find_intersections (size_t ilev, double &level, edge_crossings &xedges, edge_crossings &yedges)
 Find all of the intersections of the edges with the contour level.
 
void edges_in_y_direct (double level, interp< ubvector > &si, edge_crossings &yedges)
 Interpolate all right edge crossings.
 
void edges_in_x_direct (double level, interp< ubvector > &si, edge_crossings &xedges)
 Interpolate all bottom edge crossings.
 
void process_line (int j, int k, int dir, std::vector< double > &x, std::vector< double > &y, bool first, edge_crossings &xedges, edge_crossings &yedges)
 Create a contour line from a starting edge.
 
void check_data ()
 Check to ensure the x- and y-arrays are monotonic.
 

Protected Attributes

std::vector< edge_crossingsyed
 Right edge list.
 
std::vector< edge_crossingsxed
 Bottom edge list.
 
User-specified data
int nx
 
int ny
 
ubvector xfun
 
ubvector yfun
 
ubmatrix data
 
User-specified contour levels
int nlev
 
ubvector levels
 
bool levels_set
 

Static Protected Attributes

Edge direction
static const int dxdir =0
 
static const int dydir =1
 
Edge found or not found
static const int efound =1
 
static const int enot_found =0
 

Private Member Functions

 contour (const contour &)
 
contouroperator= (const contour &)
 

Detailed Description

Basic Usage

The contours are generated as a series of x- and y-coordinates, defining a line. If the contour is closed, then the first and the last set of coordinates will be equal.

The convention used by this class is that the first (row) index of the matrix enumerates the x coordinate and that the second (column) index enumerates the y coordinate. See the discussion in the User's guide in the section called Rows and columns vs. x and y.

The data is copied by set_data(), so changing the data will not change the contours unless set_data() is called again. The functions set_levels() and calc_contours() can be called several times for the same data without calling set_data() again.

Note that in order to simplify the algorithm for computing contour lines, the calc_contours() function will sometimes adjust the user-specified contour levels slightly in order to ensure that no contour line passes exactly through any data point on the grid. The contours are adjusted by multiplying the original contour level by 1 plus a small number ( $ 10^{-8} $ by default), which is specified in lev_adjust.

Linear interpolation is used to decide whether or not a line segment from the grid and a contour cross. This choice is intentional, since (in addition to making the algorithm much simpler) it is the user (and not this contour class) which is likely best able to refine the data. In case a simple refinement scheme is desired, the method regrid_data() is provided which refines the internally stored data for any interpolation type.

Since linear interpolation is used, the contour calculation implicitly assumes that there is not more than one intersection of any contour level with any line segment. For contours which do not close inside the region of interest, the results will always end at either the minimum or maximum values of the x or y grid points (no extrapolation is ever done). Note also that the points defining the contour are not necessarily equally spaced. Two neighboring points will never be farther apart than the distance across opposite corners of one cell in the grid.

The Algorithm:

This works by viewing the data as defining a square two-dimensional grid. The function calc_contours() exhaustively enumerates every line segment in the grid which involves a level crossing and then organizes the points defined by the intersection of a line segment with a level curve into a full contour line.

Idea for Future:
Copy constructor
Idea for Future:
Improve the algorithm to ensure that no contour line ends on an internal point. I am not sure the best way to do this, but it could be done recursively just by trying all possible links instead of just using the one that minimizes the distance.
Idea for Future:
Rewrite the code which adjusts the contour levels to ensure contours don't go through the data to adjust the internal copy of the data instead? This should be more accurate because we're perturbing one point instead of perturbing the entire line.
Idea for Future:
Change nx and ny to size_t?
Idea for Future:
It would be nice to have a function which creates a set of closed regions to fill which represent the data. However, this likely requires a completely new algorithm, because it's not easy to simply close the contours already generated by the calc_contours() function. There are, for example, several cases which are difficult to handle, such as filling a region in between several closed contours

Definition at line 240 of file contour.h.

Member Function Documentation

◆ calc_contours()

void o2scl::contour::calc_contours ( std::vector< contour_line > &  clines)
Note
There may be zero or more than one contour line for each level, so the size of clines is not necessarily equal to the number of levels specified in set_levels().

◆ get_data()

void o2scl::contour::get_data ( size_t &  sizex,
size_t &  sizey,
ubvector *&  x_fun,
ubvector *&  y_fun,
ubmatrix *&  udata 
)
inline

This is useful to see how the data has changed after a call to regrid_data().

Idea for Future:
There is probably a better way than returning pointers to the internal data.

Definition at line 387 of file contour.h.

◆ get_edges()

void o2scl::contour::get_edges ( std::vector< edge_crossings > &  x_edges,
std::vector< edge_crossings > &  y_edges 
)
inline

The size of y_edges and x_edges will both be equal to the number of levels set by set_levels().

Definition at line 405 of file contour.h.

◆ regrid_data()

void o2scl::contour::regrid_data ( size_t  xfact,
size_t  yfact,
size_t  interp_type = o2scl::itp_cspline 
)

Use interpolation to refine the data set. This can be called before calc_contours() in order to attempt make the contour levels smoother by providing a smaller grid size. If the original number of data points is $ (\mathrm{nx},\mathrm{ny}) $, then the new number of data points is

\[ (\mathrm{xfact}~(\mathrm{nx}-1)+1, \mathrm{yfact}~(\mathrm{ny}-1)+1) \]

The parameters xfact and yfact must both be larger than zero and they cannot both be 1.

◆ set_data() [1/2]

template<class vec_t , class mat_t >
void o2scl::contour::set_data ( size_t  sizex,
size_t  sizey,
const vec_t &  x_fun,
const vec_t &  y_fun,
const mat_t &  udata 
)
inline

The types vec_t and mat_t can be any types which have operator[] and operator[][] for array and matrix indexing.

Note that this method copies all of the user-specified data to local storage so that changes in the data after calling this function will not be reflected in the contours that are generated.

Definition at line 266 of file contour.h.

◆ set_data() [2/2]

template<class mat_t >
void o2scl::contour::set_data ( const uniform_grid< double > &  ugx,
const uniform_grid< double > &  ugy,
const mat_t &  udata 
)
inline

The type mat_t can be any type which has operator[][] for matrix indexing.

Note that this method copies all of the user-specified data to local storage so that changes in the data after calling this function will not be reflected in the contours that are generated.

Definition at line 302 of file contour.h.

◆ set_levels()

template<class vec_t >
void o2scl::contour::set_levels ( size_t  nlevels,
vec_t &  ulevels 
)
inline

This is separate from the function calc_contours() so that the user can compute the contours for different data sets using the same levels.

Definition at line 337 of file contour.h.

Member Data Documentation

◆ verbose

int o2scl::contour::verbose

If verbose is greater than 0, then adjustments to the contour levels will be output and the contour lines will be output as they are built up from the intersections. If verbose is greater than 1, then all edges will be output. If verbose is greater than 2, a keypress will be required after each contour line is constructed.

Definition at line 431 of file contour.h.


The documentation for this class was generated from the following file:

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).