Scroom  0.14
Scroom::Utils::Segment< T > Class Template Reference

#include <linearsegment.hh>

Inheritance diagram for Scroom::Utils::Segment< T >:
Inheritance graph
Collaboration diagram for Scroom::Utils::Segment< T >:
Collaboration graph

Public Types

using value_type = T
 

Public Member Functions

 Segment ()
 
 Segment (value_type start_, value_type size_)
 
template<bool T_is_int = std::is_same<int, typename std::remove_cv<T>::type>::value>
 Segment (typename std::enable_if<!T_is_int, Segment< int > const & >::type other)
 
Segment moveTo (value_type p) const
 
bool contains (value_type p) const
 
bool contains (const Segment< value_type > &other) const
 
bool intersects (const Segment< value_type > &other) const
 
void reduceSizeToMultipleOf (value_type m)
 
Segment< value_typeintersection (const Segment< value_type > &other) const
 
void intersect (const Segment< value_type > &other)
 
Segment< value_typebefore (value_type v) const
 
Segment< value_typeafter (value_type v) const
 
value_type getStart () const
 
value_type getEnd () const
 
value_type getSize () const
 
bool isEmpty () const
 
bool operator== (const Segment< value_type > &other) const
 
bool operator!= (const Segment< value_type > &other) const
 
Segment< value_type > & operator+= (value_type n)
 
Segment< value_type > & operator-= (value_type n)
 
Segment< value_type > & operator*= (value_type n)
 
Segment< value_type > & operator/= (value_type n)
 
Segment< value_type > & operator&= (const Segment< value_type > &other)
 
void setStart (value_type n)
 
void setSize (value_type n)
 
void setEnd (value_type n)
 
template<typename U >
Segment< U > to () const
 

Private Member Functions

void normalize ()
 

Private Attributes

value_type start
 
value_type size
 

Member Typedef Documentation

◆ value_type

template<typename T >
using Scroom::Utils::Segment< T >::value_type = T

Constructor & Destructor Documentation

◆ Segment() [1/3]

template<typename T >
Scroom::Utils::Segment< T >::Segment ( )
inline
55  : start(0)
56  , size(0)
57  {
58  }

Referenced by Scroom::Utils::Segment< value_type >::moveTo().

Here is the caller graph for this function:

◆ Segment() [2/3]

template<typename T >
Scroom::Utils::Segment< T >::Segment ( value_type  start_,
value_type  size_ 
)
inline
61  : start(start_)
62  , size(size_)
63  {
64  normalize();
65  }

◆ Segment() [3/3]

template<typename T >
template<bool T_is_int = std::is_same<int, typename std::remove_cv<T>::type>::value>
Scroom::Utils::Segment< T >::Segment ( typename std::enable_if<!T_is_int, Segment< int > const & >::type  other)
inlineexplicit

Implicit conversion from Segment<int> to Segment<T>. If T!=int

70  : start(other.getStart())
71  , size(other.getSize())
72  {
73  }

Member Function Documentation

◆ after()

template<typename T >
Segment<value_type> Scroom::Utils::Segment< T >::after ( value_type  v) const
inline
121  {
122  if(v > start + size)
123  {
124  return Segment<value_type>();
125  }
126  if(v > start)
127  {
128  return Segment<value_type>(v, start + size - v);
129  }
130  return *this;
131  }

Referenced by Scroom::Utils::Rectangle< int >::below(), and Scroom::Utils::Rectangle< int >::rightOf().

Here is the caller graph for this function:

◆ before()

template<typename T >
Segment<value_type> Scroom::Utils::Segment< T >::before ( value_type  v) const
inline
108  {
109  if(v < start)
110  {
111  return Segment<value_type>();
112  }
113  if(v < start + size)
114  {
115  return Segment<value_type>(start, v - start);
116  }
117  return *this;
118  }

Referenced by Scroom::Utils::Rectangle< int >::above(), and Scroom::Utils::Rectangle< int >::leftOf().

Here is the caller graph for this function:

◆ contains() [1/2]

template<typename T >
bool Scroom::Utils::Segment< T >::contains ( const Segment< value_type > &  other) const
inline
80  {
81  return getStart() <= other.getStart() && other.getEnd() <= getEnd();
82  }

◆ contains() [2/2]

template<typename T >
bool Scroom::Utils::Segment< T >::contains ( value_type  p) const
inline
77 { return start <= p && p < (start + size); }

Referenced by Scroom::Utils::Rectangle< int >::contains().

Here is the caller graph for this function:

◆ getEnd()

◆ getSize()

◆ getStart()

◆ intersect()

template<typename T >
void Scroom::Utils::Segment< T >::intersect ( const Segment< value_type > &  other)
inline
105 { *this = intersection(other); }

Referenced by Scroom::Utils::Segment< value_type >::operator&=().

Here is the caller graph for this function:

◆ intersection()

template<typename T >
Segment<value_type> Scroom::Utils::Segment< T >::intersection ( const Segment< value_type > &  other) const
inline
92  {
93  const value_type newStart = std::max(getStart(), other.getStart());
94  const value_type newEnd = std::min(getEnd(), other.getEnd());
95  const value_type newSize = newEnd - newStart;
96 
97  if(newSize >= 0)
98  {
99  return Segment<value_type>(newStart, newSize);
100  }
101 
102  return Segment<value_type>(); // empty segment
103  }

Referenced by Scroom::Utils::Rectangle< int >::above(), Scroom::Utils::Rectangle< int >::below(), Scroom::Utils::Segment< value_type >::intersect(), Scroom::Utils::Rectangle< int >::intersection(), Scroom::Utils::Rectangle< int >::leftOf(), and Scroom::Utils::Rectangle< int >::rightOf().

Here is the caller graph for this function:

◆ intersects()

template<typename T >
bool Scroom::Utils::Segment< T >::intersects ( const Segment< value_type > &  other) const
inline
85  {
86  return getStart() < other.getEnd() && other.getStart() < getEnd() && !isEmpty() && !other.isEmpty();
87  }

Referenced by Scroom::Utils::Rectangle< int >::intersects().

Here is the caller graph for this function:

◆ isEmpty()

template<typename T >
bool Scroom::Utils::Segment< T >::isEmpty ( ) const
inline

◆ moveTo()

template<typename T >
Segment Scroom::Utils::Segment< T >::moveTo ( value_type  p) const
inline
75 { return Segment(p, size); }

Referenced by Scroom::Utils::Rectangle< int >::moveTo().

Here is the caller graph for this function:

◆ normalize()

template<typename T >
void Scroom::Utils::Segment< T >::normalize ( )
inlineprivate
209  {
210  if(size < 0)
211  {
212  start += size;
213  size = -size;
214  }
215  }

Referenced by Scroom::Utils::Segment< value_type >::Segment(), and Scroom::Utils::Segment< value_type >::setEnd().

Here is the caller graph for this function:

◆ operator!=()

template<typename T >
bool Scroom::Utils::Segment< T >::operator!= ( const Segment< value_type > &  other) const
inline
156 { return !(*this == other); }

◆ operator&=()

template<typename T >
Segment<value_type>& Scroom::Utils::Segment< T >::operator&= ( const Segment< value_type > &  other)
inline
181  {
182  intersect(other);
183  return *this;
184  }

◆ operator*=()

template<typename T >
Segment<value_type>& Scroom::Utils::Segment< T >::operator*= ( value_type  n)
inline
169  {
170  start *= n;
171  size *= n;
172  return *this;
173  }

◆ operator+=()

template<typename T >
Segment<value_type>& Scroom::Utils::Segment< T >::operator+= ( value_type  n)
inline
159  {
160  start += n;
161  return *this;
162  }

◆ operator-=()

template<typename T >
Segment<value_type>& Scroom::Utils::Segment< T >::operator-= ( value_type  n)
inline
164  {
165  start -= n;
166  return *this;
167  }

◆ operator/=()

template<typename T >
Segment<value_type>& Scroom::Utils::Segment< T >::operator/= ( value_type  n)
inline
175  {
176  start /= n;
177  size /= n;
178  return *this;
179  }

◆ operator==()

template<typename T >
bool Scroom::Utils::Segment< T >::operator== ( const Segment< value_type > &  other) const
inline
142  {
143  if(isEmpty() != other.isEmpty())
144  {
145  return false;
146  }
147 
148  if(isEmpty())
149  {
150  return true;
151  }
152 
153  return areEqual(start, other.start) && areEqual(size, other.size);
154  }

◆ reduceSizeToMultipleOf()

template<typename T >
void Scroom::Utils::Segment< T >::reduceSizeToMultipleOf ( value_type  m)
inline
89 { size -= size % m; }

Referenced by Scroom::Utils::Rectangle< int >::reduceSizeToMultipleOf().

Here is the caller graph for this function:

◆ setEnd()

template<typename T >
void Scroom::Utils::Segment< T >::setEnd ( value_type  n)
inline
193  {
194  size = n - start;
195  normalize();
196  }

Referenced by Scroom::Utils::Rectangle< int >::setBottom(), Scroom::Utils::Rectangle< int >::setRight(), and Scroom::Utils::Segment< value_type >::setStart().

Here is the caller graph for this function:

◆ setSize()

template<typename T >
void Scroom::Utils::Segment< T >::setSize ( value_type  n)
inline
191 { size = n; }

Referenced by Scroom::Utils::Rectangle< int >::setSize().

Here is the caller graph for this function:

◆ setStart()

template<typename T >
void Scroom::Utils::Segment< T >::setStart ( value_type  n)
inline
186  {
187  const auto end = getEnd();
188  start = n;
189  setEnd(end);
190  }

Referenced by Scroom::Utils::Rectangle< int >::setLeft(), and Scroom::Utils::Rectangle< int >::setTop().

Here is the caller graph for this function:

◆ to()

template<typename T >
template<typename U >
Segment<U> Scroom::Utils::Segment< T >::to ( ) const
inline
200  {
201  return {
202  static_cast<U>(start),
203  static_cast<U>(size),
204  };
205  }

Member Data Documentation

◆ size

◆ start


The documentation for this class was generated from the following file:
Scroom::Utils::isZero
bool isZero(T v)
Scroom::Utils::Segment::getEnd
value_type getEnd() const
Definition: linearsegment.hh:135
Scroom::Utils::Segment::normalize
void normalize()
Definition: linearsegment.hh:208
Scroom::Utils::Segment::value_type
T value_type
Definition: linearsegment.hh:52
Scroom::Utils::Segment::Segment
Segment()
Definition: linearsegment.hh:54
Scroom::Utils::areEqual
bool areEqual(T a, T b)
Definition: linearsegment.hh:38
Scroom::Utils::Segment::size
value_type size
Definition: linearsegment.hh:219
Scroom::Utils::Segment::start
value_type start
Definition: linearsegment.hh:218
Scroom::Utils::Segment::intersect
void intersect(const Segment< value_type > &other)
Definition: linearsegment.hh:105
Scroom::Utils::Segment::intersection
Segment< value_type > intersection(const Segment< value_type > &other) const
Definition: linearsegment.hh:91
Scroom::Utils::Segment::setEnd
void setEnd(value_type n)
Definition: linearsegment.hh:192
Scroom::Utils::Segment::getStart
value_type getStart() const
Definition: linearsegment.hh:133
Scroom::Utils::Segment::isEmpty
bool isEmpty() const
Definition: linearsegment.hh:139
Scroom::Utils::Segment::getSize
value_type getSize() const
Definition: linearsegment.hh:137