2011-06-22 15:28:38 +00:00
|
|
|
/**
|
2011-11-24 17:32:51 +00:00
|
|
|
* The physical length library. Made for nanometer scale.
|
2011-06-22 15:28:38 +00:00
|
|
|
* @file length.h
|
|
|
|
*/
|
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
/* sorry it is not styled correctly, i'll work on it further */
|
2011-06-22 15:28:38 +00:00
|
|
|
|
2011-09-19 13:00:40 +00:00
|
|
|
#ifndef LENGTH_H_INCLUDED
|
|
|
|
#define LENGTH_H_INCLUDED 1
|
2011-06-22 15:28:38 +00:00
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
/* type to be used by length units by default */
|
|
|
|
typedef int DEF_LENGTH_VALUE;
|
2011-11-09 13:25:49 +00:00
|
|
|
|
2011-11-17 17:47:27 +00:00
|
|
|
/**
|
2011-11-24 17:32:51 +00:00
|
|
|
* Length template class
|
2011-11-17 17:47:27 +00:00
|
|
|
* @param T actual type holding a value (be aware of precision and range!)
|
|
|
|
* @param P power of length unit: 1 - length, 2 - area, 3 - volume, -1 - lin. density etc...
|
|
|
|
* This class check length dimension in compile time. In runtime it behaves
|
|
|
|
* exactly like contained type t (which should be numeric type, like int or double)
|
|
|
|
* This class can be replaced with its contained type or simple stub.
|
|
|
|
* Check rules:
|
|
|
|
* - comparisons (< = etc.), addition, subtraction require values of same dimension
|
|
|
|
* e. g. length with length, area with area etc.
|
|
|
|
* - multiplication and division result have appropriate dimension (powers
|
|
|
|
* added and subtracted respectively)
|
|
|
|
* - sqrt and cbrt have appropriate dimensions (P/2 and P/3).
|
|
|
|
* Limitations:
|
|
|
|
* - functions which should not be applied to dimensioned values are not implemeted:
|
|
|
|
* they include algebraic (exp, log...), trigo (sin, cos...), hyperbolic (sinh, cosh..)
|
|
|
|
* - pow function is not implemented as it is require dimension check in runtime
|
|
|
|
* you should use multiplication, division, sqrt and cbrt functions instead.
|
|
|
|
* - sqrt and cbrt result type should be instantiated before they used
|
|
|
|
* Be aware when using them in complex formulae, e. g.
|
|
|
|
* LENGTH< double, 1 > len = cbrt(vol) - is ok, but
|
2011-11-24 17:32:51 +00:00
|
|
|
* LENGTH< double, 2 > vol = sqrt(area*area*area*area)/length - will fail
|
2011-11-17 17:47:27 +00:00
|
|
|
* if LENGTH<..., 4> is not instantiated
|
|
|
|
* - non-integer power values do not supported
|
|
|
|
* they should be implemented carefully using natural fractions, not floats, to be exact
|
|
|
|
* but they are very rare so you should not worry about.
|
|
|
|
* e. g. linear electric noise density should be in mV/sqrt(m)
|
|
|
|
* - automatic numeric type casts are not performed. You even have to manually
|
|
|
|
* cast LENGTH< short > to LENGTH< int > or LENGTH< float >
|
|
|
|
* to LENGTH< double >. Anyway it is not such trouble as progremmer should be
|
|
|
|
* very careful when mixing numeric types and avoid automatic casts.
|
|
|
|
*
|
|
|
|
*/
|
2011-06-22 15:28:38 +00:00
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
template < typename T = DEF_LENGTH_VALUE, int P = 1 > class LENGTH;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Length units contained in this class
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T> class LENGTH_UNITS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For internal needs
|
|
|
|
*/
|
|
|
|
template < typename T, int P > struct LENGTH_TRAITS
|
|
|
|
{
|
|
|
|
typedef LENGTH<T, P> flat;
|
|
|
|
};
|
|
|
|
|
|
|
|
template < typename T > struct LENGTH_TRAITS< T, 0 >
|
|
|
|
{
|
|
|
|
/* length dimension to power 0 is just a number, so LENGTH<T, 0> should be automatically converted to T */
|
|
|
|
typedef T flat;
|
|
|
|
};
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template < typename T, int P > class LENGTH
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
|
|
|
friend class LENGTH_UNITS< T >;
|
|
|
|
friend class LENGTH_TRAITS< T, P >;
|
|
|
|
template < typename Y, int R > friend class LENGTH;
|
2011-09-19 13:00:40 +00:00
|
|
|
protected:
|
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
T m_U;
|
|
|
|
LENGTH( T units ) : m_U( units )
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static T RawValue( const LENGTH<T, P> &x )
|
|
|
|
{
|
|
|
|
return x.m_U;
|
|
|
|
}
|
|
|
|
static T RawValue( const T& x )
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
2011-06-22 15:28:38 +00:00
|
|
|
public:
|
2011-09-20 10:08:17 +00:00
|
|
|
typedef T value_type;
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
dimension = P
|
|
|
|
};
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH( const LENGTH <T, P> &orig ) : m_U( orig.m_U )
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH( void ) : m_U()
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
|
2011-09-20 10:08:17 +00:00
|
|
|
static LENGTH<T, P> zero ( void )
|
|
|
|
{
|
|
|
|
return T(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
LENGTH<T, P> & operator = ( const LENGTH<T, P> & y )
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
this->m_U = y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-10-14 14:13:35 +00:00
|
|
|
template <typename Y> operator LENGTH< Y, P > ( void )
|
2011-11-24 17:32:51 +00:00
|
|
|
{
|
|
|
|
return this->m_U;
|
|
|
|
}
|
|
|
|
/*************************/
|
|
|
|
/* comparisons and tests */
|
|
|
|
/*************************/
|
2011-09-20 10:08:17 +00:00
|
|
|
bool operator ==( const LENGTH < T, P > y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U == y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
bool operator !=( const LENGTH < T, P > y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U != y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
bool operator <( const LENGTH < T, P > y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U < y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
bool operator >=( const LENGTH < T, P > y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U >= y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
bool operator >( const LENGTH < T, P > y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U > y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
bool operator <=( const LENGTH < T, P > y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U <= y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
bool operator !( void ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return !m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
/*************************/
|
|
|
|
/* basic arithmetic */
|
|
|
|
/*************************/
|
2011-09-20 10:08:17 +00:00
|
|
|
LENGTH< T, P > operator - ( void ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return LENGTH<T, P>(-this->m_U);
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH< T, P > operator - ( const LENGTH< T, P > y ) const
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U - y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH< T, P > operator + ( const LENGTH< T, P > y ) const
|
2011-11-15 18:26:06 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U + y.m_U;
|
2011-11-15 18:26:06 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
template < int R >
|
|
|
|
typename LENGTH_TRAITS< T, P + R >::flat operator * ( const LENGTH<T, R> &y ) const
|
2011-11-15 18:26:06 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U * y.m_U;
|
2011-11-15 18:26:06 +00:00
|
|
|
}
|
2011-09-20 10:08:17 +00:00
|
|
|
LENGTH< T, P > operator * ( const T & y) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U * y;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
LENGTH< T, P > friend operator * ( const T &y, const LENGTH<T, P> &x )
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return x.m_U * y;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
|
|
|
|
template < int R >
|
|
|
|
typename LENGTH_TRAITS< T, P - R >::flat operator / ( const LENGTH<T, R> &y ) const
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U / y.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
|
|
|
LENGTH< T, P > operator / ( const T &y ) const
|
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U / y;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-15 18:26:06 +00:00
|
|
|
LENGTH< T, -P > friend operator / ( const T &y, const LENGTH< T, P > &x )
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return y / x.m_U;
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-15 18:26:06 +00:00
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
friend LENGTH< T, P > sqrt( LENGTH< T, P*2 > y )
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return sqrt( y.m_U );
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
friend LENGTH< T, P > cbrt( LENGTH< T, P*3 > y )
|
2011-09-20 10:08:17 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return cbrt( y.m_U );
|
2011-09-20 10:08:17 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
/*************************/
|
|
|
|
/* assignment arithmetic */
|
|
|
|
/*************************/
|
|
|
|
LENGTH< T, P >& operator -= ( const LENGTH< T, P > y )
|
2011-11-11 17:44:20 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U -= y.m_U;
|
2011-11-11 17:44:20 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH< T, P >& operator += ( const LENGTH< T, P > y )
|
2011-11-11 17:44:20 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U += y.m_U;
|
2011-11-11 17:44:20 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH< T, P >& operator *= ( const T y )
|
2011-11-11 17:44:20 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U *= y;
|
2011-11-11 17:44:20 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
LENGTH< T, P >& operator /= ( const T y )
|
2011-11-11 17:44:20 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
return m_U /= y;
|
2011-11-11 17:44:20 +00:00
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
/*************************/
|
|
|
|
/* more arithmetic */
|
|
|
|
/*************************/
|
2011-09-19 13:00:40 +00:00
|
|
|
};
|
2011-06-22 15:28:38 +00:00
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
/**
|
2011-09-20 10:08:17 +00:00
|
|
|
* Units of length
|
|
|
|
*
|
|
|
|
* How to use them:
|
|
|
|
* there are several functions, named LENGTH_UNITS< T >::METRE, which return
|
|
|
|
* named unit (1 meter in example) which have type LENGTH< T, P >.
|
|
|
|
* to get specific length you should use a multiplication:
|
|
|
|
* 3*LENGTH_UNITS::metre() gives 3 metres
|
|
|
|
* 0.01*LENGTH_UNITS::metre() gives 0.01 inch
|
|
|
|
* to get numeric value of length in specific units you should use a division
|
|
|
|
* length/LENGTH_UNITS::metre() gives number of metres in length
|
|
|
|
* legnth/LENGTH_UNITS::foot() gives number of feet in length
|
|
|
|
*/
|
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
template < typename T = DEF_LENGTH_VALUE > class LENGTH_UNITS {
|
2011-09-19 13:00:40 +00:00
|
|
|
protected:
|
|
|
|
enum
|
2011-06-22 15:28:38 +00:00
|
|
|
{
|
2011-11-24 17:32:51 +00:00
|
|
|
METRE = 1000000000, /* The ONLY constant connecting length to the real world */
|
|
|
|
|
2011-09-19 13:00:40 +00:00
|
|
|
INCH = METRE / 10000 * 254
|
|
|
|
};
|
2011-11-24 17:32:51 +00:00
|
|
|
public:
|
|
|
|
static LENGTH< T, 1 > metre( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( METRE );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > decimetre( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( METRE / 10 );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > centimetre( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( METRE / 100 );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > millimetre( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( METRE / 1000 );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > micrometre( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( METRE / 1000000 );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > foot( void ) { /* do not think this will ever need */
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( INCH * 12 );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > inch( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( INCH );
|
|
|
|
}
|
2011-11-24 17:32:51 +00:00
|
|
|
static LENGTH< T, 1 > mil( void ) {
|
2011-09-20 10:08:17 +00:00
|
|
|
return T( INCH / 1000 );
|
|
|
|
}
|
2011-09-19 13:00:40 +00:00
|
|
|
};
|
2011-06-22 15:28:38 +00:00
|
|
|
|
2011-09-20 10:08:17 +00:00
|
|
|
/**
|
2011-11-24 17:32:51 +00:00
|
|
|
* shortcut to get units of given length type
|
2011-09-20 10:08:17 +00:00
|
|
|
*/
|
|
|
|
template < typename T, int D > class LENGTH_UNITS< LENGTH< T, D > >: public LENGTH_UNITS< T >
|
|
|
|
{
|
2011-09-19 13:00:40 +00:00
|
|
|
};
|
2011-06-22 16:33:59 +00:00
|
|
|
|
2011-11-24 17:32:51 +00:00
|
|
|
#endif
|