CTL  0.6.1
Computed Tomography Library
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
CTL::Range< T > Class Template Reference

The Range class holds start and end point of a value range (or interval) and provides a few convenience methods to operate on that range. More...

#include <range.h>

Collaboration diagram for CTL::Range< T >:
Collaboration graph
[legend]

Public Member Functions

template<typename T1 , typename T2 , typename = typename std::enable_if<std::is_convertible<T1, T>::value && std::is_convertible<T2, T>::value>::type>
 Range (T1 start, T2 end)
 Constructs a Range representing the interval [start, end]. More...
 
template<typename T1 , typename T2 , typename = typename std::enable_if<std::is_convertible<T1, T>::value && std::is_convertible<T2, T>::value>::type>
 Range (const std::pair< T1, T2 > &bounds)
 Constructs a Range representing the interval [bounds.first, bounds.second]. More...
 
T & start ()
 Returns a (modifiable) reference to the start point of this range.
 
T & end ()
 Returns a (modifiable) reference to the end point of this range.
 
const T & start () const
 Returns the start point of this range.
 
const T & end () const
 Returns the end point of this range.
 
center () const
 Returns the center point of this range. More...
 
width () const
 Returns the width of the range (i.e. end - start). More...
 
spacing (uint nbSamples) const
 Returns the spacing that two sampling points would have if this range were divided into nbSamples equidistant sampling points. More...
 
std::vector< T > linspace (uint nbSamples, bool endpoint=true) const
 Generates a vector with nbSamples points that are equally distributed across this range. More...
 
std::vector< T > expspace (uint nbSamples, bool endpoint=true) const
 Generates a vector with nbSamples points that are distributed across this range with exponentially increasing distances. More...
 
std::pair< T, T > toPair () const
 Returns the start and end point of the range as a std::pair.
 

Static Public Member Functions

static Range fromCenterAndWidth (T center, T width)
 Creates a Range object describing an interval of width width centered at center. More...
 
static std::vector< T > linspace (T from, T to, uint nbSamples, bool endpoint=true)
 Generates a vector with nbSamples points that are equally distributed across the range of [from, to]. More...
 
static std::vector< T > expspace (T from, T to, uint nbSamples, bool endpoint=true)
 Generates a vector with nbSamples points that are distributed across the range of [from, to] with exponentially increasing distances. More...
 

Private Attributes

_data [2]
 

Related Functions

(Note that these are not member functions.)

 IndexRange
 Alias for Range<unsigned int>. More...
 
 SamplingRange
 Alias for Range<float>. More...
 

Detailed Description

template<typename T>
class CTL::Range< T >

The Range class holds start and end point of a value range (or interval) and provides a few convenience methods to operate on that range.

This class holds a start and end point (of same type) that are intended to describe an interval bounded by these two values. Besides its use as a pair-like container in several places throughout the CTL, the main purpose of this class lies in its convenience methods for creation of a vector of sampling points across the specified interval. Two different sampling patterns are available:

The template type for this class needs to satisfy basic mathematical operations, such as +, -, and /.

Constructor & Destructor Documentation

◆ Range() [1/2]

template<typename T>
template<typename T1 , typename T2 , typename = typename std::enable_if<std::is_convertible<T1, T>::value && std::is_convertible<T2, T>::value>::type>
CTL::Range< T >::Range ( T1  start,
T2  end 
)
inline

Constructs a Range representing the interval [start, end].

This creates a Range object and sets the borders of the interval to start and end, respectively. Both start and end must be convertible to the type of Range.

◆ Range() [2/2]

template<typename T>
template<typename T1 , typename T2 , typename = typename std::enable_if<std::is_convertible<T1, T>::value && std::is_convertible<T2, T>::value>::type>
CTL::Range< T >::Range ( const std::pair< T1, T2 > &  bounds)
inline

Constructs a Range representing the interval [bounds.first, bounds.second].

This creates a Range object and sets the borders of the interval to bounds.first and bounds.second, respectively. The types of both elements in the pair must be convertible to the type of Range.

Member Function Documentation

◆ center()

template<typename T >
T CTL::Range< T >::center ( ) const

Returns the center point of this range.

This is computed (using operations of this range's value type) as: (start + end) / 2.

Example:

auto range = Range<float>(0.0f, 2.5f);
qInfo() << range.center(); // output: 1.25
auto range_int = Range<int>(0, 5);
qInfo() << range_int.center(); // output: 2 (Note: computations in 'int')

◆ expspace() [1/2]

template<typename T >
std::vector< T > CTL::Range< T >::expspace ( uint  nbSamples,
bool  endpoint = true 
) const

Generates a vector with nbSamples points that are distributed across this range with exponentially increasing distances.

The start and end point of this range must be (strictly) larger than zero when using this method; throws an std::domain_error otherwise.

If nbSamples > 1 and endpoint = true, the last sample is end(). Otherwise, it is not included in the returned result.

Example:

auto range = Range<float>(1.0f, 10.0f);
qInfo() << range.expspace(5); // output: std::vector(1, 1.77828, 3.16228, 5.62341, 10)
qInfo() << range.expspace(5, false); // output: std::vector(1, 1.58489, 2.51189, 3.98107, 6.30957)

◆ expspace() [2/2]

template<typename T>
std::vector< T > CTL::Range< T >::expspace ( from,
to,
uint  nbSamples,
bool  endpoint = true 
)
static

Generates a vector with nbSamples points that are distributed across the range of [from, to] with exponentially increasing distances.

from and to must be (strictly) larger than zero; throws an std::domain_error otherwise.

If nbSamples > 1 and endpoint = true, the last sample is to. Otherwise, it is not included in the returned result.

Same as Range<T>(from, to).expspace(nbSamples, endpoint).

Example:

qInfo() << Range<float>::expspace(1.0f, 100.0f, 6); // output: std::vector(1, 2.51189, 6.30957, 15.8489, 39.8107, 100)
qInfo() << Range<float>::expspace(1.0f, 100.0f, 6, false); // output: std::vector(1, 2.15443, 4.64159, 10, 21.54435, 46.41589)

◆ fromCenterAndWidth()

template<typename T>
Range< T > CTL::Range< T >::fromCenterAndWidth ( center,
width 
)
static

Creates a Range object describing an interval of width width centered at center.

The resulting Range has the following start and end points:

  • start: (center - width / 2)
  • end: (center + width / 2)

Mathematical operations are performed with the type of the Range.

Example:

auto range = Range<float>::fromCenterAndWidth(2.5f, 5.0f);
qInfo() << range.start() << "," << range.end(); // output: 0 , 5
// be careful with integer types and odd 'width'!
auto range_int = Range<int>::fromCenterAndWidth(3, 5);
qInfo() << range_int.start() << "," << range_int.end(); // output: 1 , 5 (Note: actual width is now 4; center remains as requested)

◆ linspace() [1/2]

template<typename T >
std::vector< T > CTL::Range< T >::linspace ( uint  nbSamples,
bool  endpoint = true 
) const

Generates a vector with nbSamples points that are equally distributed across this range.

If nbSamples > 1 and endpoint = true, the last sample is end(). Otherwise, it is not included in the returned result.

Example:

auto range = Range<float>(0.0f, 2.5f);
qInfo() << range.linspace(5); // output: std::vector(0, 0.625, 1.25, 1.875, 2.5)
qInfo() << range.linspace(5, false); // output: std::vector(0, 0.5, 1, 1.5, 2)

◆ linspace() [2/2]

template<typename T>
std::vector< T > CTL::Range< T >::linspace ( from,
to,
uint  nbSamples,
bool  endpoint = true 
)
static

Generates a vector with nbSamples points that are equally distributed across the range of [from, to].

If nbSamples > 1 and endpoint = true, the last sample is to. Otherwise, it is not included in the returned result.

Same as Range<T>(from, to).linspace(nbSamples, endpoint).

Example:

qInfo() << Range<float>::linspace(0.0f, 10.0f, 6); // output: std::vector(0, 2, 4, 6, 8, 10)
qInfo() << Range<float>::linspace(0.0f, 10.0f, 6, false); // output: std::vector(0, 1.66667, 3.33333, 5, 6.66667, 8.33333)

◆ spacing()

template<typename T >
T CTL::Range< T >::spacing ( uint  nbSamples) const

Returns the spacing that two sampling points would have if this range were divided into nbSamples equidistant sampling points.

Returns zero for nbSamples <= 1.

Example:

auto range = Range<float>(0.0f, 10.0f);
qInfo() << range.spacing(5); // output: 2.5

◆ width()

template<typename T >
T CTL::Range< T >::width ( ) const

Returns the width of the range (i.e. end - start).

Same as end() - start().

Friends And Related Function Documentation

◆ IndexRange()

template<typename T>
IndexRange
related

Alias for Range<unsigned int>.

This Range type is typically used to describe ranges/intervals of indices.

◆ SamplingRange()

template<typename T>
SamplingRange
related

Alias for Range<float>.

This Range type is typically used to describe ranges/intervals of sampling points.


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