LCOV - code coverage report
Current view: top level - usr/include/irrlicht - dimension2d.h (source / functions) Hit Total Coverage
Test: report Lines: 9 9 100.0 %
Date: 2015-07-11 18:23:49 Functions: 7 8 87.5 %

          Line data    Source code
       1             : // Copyright (C) 2002-2012 Nikolaus Gebhardt
       2             : // This file is part of the "Irrlicht Engine".
       3             : // For conditions of distribution and use, see copyright notice in irrlicht.h
       4             : 
       5             : #ifndef __IRR_DIMENSION2D_H_INCLUDED__
       6             : #define __IRR_DIMENSION2D_H_INCLUDED__
       7             : 
       8             : #include "irrTypes.h"
       9             : #include "irrMath.h" // for irr::core::equals()
      10             : 
      11             : namespace irr
      12             : {
      13             : namespace core
      14             : {
      15             :         template <class T>
      16             :         class vector2d;
      17             : 
      18             :         //! Specifies a 2 dimensional size.
      19             :         template <class T>
      20             :         class dimension2d
      21             :         {
      22             :                 public:
      23             :                         //! Default constructor for empty dimension
      24        1452 :                         dimension2d() : Width(0), Height(0) {}
      25             :                         //! Constructor with width and height
      26       12081 :                         dimension2d(const T& width, const T& height)
      27       12081 :                                 : Width(width), Height(height) {}
      28             : 
      29             :                         dimension2d(const vector2d<T>& other); // Defined in vector2d.h
      30             : 
      31             :                         //! Use this constructor only where you are sure that the conversion is valid.
      32             :                         template <class U>
      33       11660 :                         explicit dimension2d(const dimension2d<U>& other) :
      34       11660 :                                 Width((T)other.Width), Height((T)other.Height) { }
      35             : 
      36             :                         template <class U>
      37             :                         dimension2d<T>& operator=(const dimension2d<U>& other)
      38             :                         {
      39             :                                 Width = (T) other.Width;
      40             :                                 Height = (T) other.Height;
      41             :                                 return *this;
      42             :                         }
      43             : 
      44             : 
      45             :                         //! Equality operator
      46             :                         bool operator==(const dimension2d<T>& other) const
      47             :                         {
      48             :                                 return core::equals(Width, other.Width) &&
      49             :                                                 core::equals(Height, other.Height);
      50             :                         }
      51             : 
      52             :                         //! Inequality operator
      53             :                         bool operator!=(const dimension2d<T>& other) const
      54             :                         {
      55             :                                 return ! (*this == other);
      56             :                         }
      57             : 
      58             :                         bool operator==(const vector2d<T>& other) const;  // Defined in vector2d.h
      59             : 
      60             :                         bool operator!=(const vector2d<T>& other) const
      61             :                         {
      62             :                                 return !(*this == other);
      63             :                         }
      64             : 
      65             :                         //! Set to new values
      66           1 :                         dimension2d<T>& set(const T& width, const T& height)
      67             :                         {
      68           1 :                                 Width = width;
      69           1 :                                 Height = height;
      70           1 :                                 return *this;
      71             :                         }
      72             : 
      73             :                         //! Divide width and height by scalar
      74             :                         dimension2d<T>& operator/=(const T& scale)
      75             :                         {
      76             :                                 Width /= scale;
      77             :                                 Height /= scale;
      78             :                                 return *this;
      79             :                         }
      80             : 
      81             :                         //! Divide width and height by scalar
      82             :                         dimension2d<T> operator/(const T& scale) const
      83             :                         {
      84             :                                 return dimension2d<T>(Width/scale, Height/scale);
      85             :                         }
      86             : 
      87             :                         //! Multiply width and height by scalar
      88             :                         dimension2d<T>& operator*=(const T& scale)
      89             :                         {
      90             :                                 Width *= scale;
      91             :                                 Height *= scale;
      92             :                                 return *this;
      93             :                         }
      94             : 
      95             :                         //! Multiply width and height by scalar
      96             :                         dimension2d<T> operator*(const T& scale) const
      97             :                         {
      98             :                                 return dimension2d<T>(Width*scale, Height*scale);
      99             :                         }
     100             : 
     101             :                         //! Add another dimension to this one.
     102             :                         dimension2d<T>& operator+=(const dimension2d<T>& other)
     103             :                         {
     104             :                                 Width += other.Width;
     105             :                                 Height += other.Height;
     106             :                                 return *this;
     107             :                         }
     108             : 
     109             :                         //! Add two dimensions
     110             :                         dimension2d<T> operator+(const dimension2d<T>& other) const
     111             :                         {
     112             :                                 return dimension2d<T>(Width+other.Width, Height+other.Height);
     113             :                         }
     114             : 
     115             :                         //! Subtract a dimension from this one
     116             :                         dimension2d<T>& operator-=(const dimension2d<T>& other)
     117             :                         {
     118             :                                 Width -= other.Width;
     119             :                                 Height -= other.Height;
     120             :                                 return *this;
     121             :                         }
     122             : 
     123             :                         //! Subtract one dimension from another
     124             :                         dimension2d<T> operator-(const dimension2d<T>& other) const
     125             :                         {
     126             :                                 return dimension2d<T>(Width-other.Width, Height-other.Height);
     127             :                         }
     128             : 
     129             :                         //! Get area
     130             :                         T getArea() const
     131             :                         {
     132             :                                 return Width*Height;
     133             :                         }
     134             : 
     135             :                         //! Get the optimal size according to some properties
     136             :                         /** This is a function often used for texture dimension
     137             :                         calculations. The function returns the next larger or
     138             :                         smaller dimension which is a power-of-two dimension
     139             :                         (2^n,2^m) and/or square (Width=Height).
     140             :                         \param requirePowerOfTwo Forces the result to use only
     141             :                         powers of two as values.
     142             :                         \param requireSquare Makes width==height in the result
     143             :                         \param larger Choose whether the result is larger or
     144             :                         smaller than the current dimension. If one dimension
     145             :                         need not be changed it is kept with any value of larger.
     146             :                         \param maxValue Maximum texturesize. if value > 0 size is
     147             :                         clamped to maxValue
     148             :                         \return The optimal dimension under the given
     149             :                         constraints. */
     150             :                         dimension2d<T> getOptimalSize(
     151             :                                         bool requirePowerOfTwo=true,
     152             :                                         bool requireSquare=false,
     153             :                                         bool larger=true,
     154             :                                         u32 maxValue = 0) const
     155             :                         {
     156             :                                 u32 i=1;
     157             :                                 u32 j=1;
     158             :                                 if (requirePowerOfTwo)
     159             :                                 {
     160             :                                         while (i<(u32)Width)
     161             :                                                 i<<=1;
     162             :                                         if (!larger && i!=1 && i!=(u32)Width)
     163             :                                                 i>>=1;
     164             :                                         while (j<(u32)Height)
     165             :                                                 j<<=1;
     166             :                                         if (!larger && j!=1 && j!=(u32)Height)
     167             :                                                 j>>=1;
     168             :                                 }
     169             :                                 else
     170             :                                 {
     171             :                                         i=(u32)Width;
     172             :                                         j=(u32)Height;
     173             :                                 }
     174             : 
     175             :                                 if (requireSquare)
     176             :                                 {
     177             :                                         if ((larger && (i>j)) || (!larger && (i<j)))
     178             :                                                 j=i;
     179             :                                         else
     180             :                                                 i=j;
     181             :                                 }
     182             : 
     183             :                                 if ( maxValue > 0 && i > maxValue)
     184             :                                         i = maxValue;
     185             : 
     186             :                                 if ( maxValue > 0 && j > maxValue)
     187             :                                         j = maxValue;
     188             : 
     189             :                                 return dimension2d<T>((T)i,(T)j);
     190             :                         }
     191             : 
     192             :                         //! Get the interpolated dimension
     193             :                         /** \param other Other dimension to interpolate with.
     194             :                         \param d Value between 0.0f and 1.0f.
     195             :                         \return Interpolated dimension. */
     196             :                         dimension2d<T> getInterpolated(const dimension2d<T>& other, f32 d) const
     197             :                         {
     198             :                                 f32 inv = (1.0f - d);
     199             :                                 return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));
     200             :                         }
     201             : 
     202             : 
     203             :                         //! Width of the dimension.
     204             :                         T Width;
     205             :                         //! Height of the dimension.
     206             :                         T Height;
     207             :         };
     208             : 
     209             :         //! Typedef for an f32 dimension.
     210             :         typedef dimension2d<f32> dimension2df;
     211             :         //! Typedef for an unsigned integer dimension.
     212             :         typedef dimension2d<u32> dimension2du;
     213             : 
     214             :         //! Typedef for an integer dimension.
     215             :         /** There are few cases where negative dimensions make sense. Please consider using
     216             :                 dimension2du instead. */
     217             :         typedef dimension2d<s32> dimension2di;
     218             : 
     219             : 
     220             : } // end namespace core
     221             : } // end namespace irr
     222             : 
     223             : #endif
     224             : 

Generated by: LCOV version 1.11