AGX Dynamics  2.37.2.0
LinkedSegmentIterator.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agxSDK/LinkedSegment.h>
20 
21 namespace agxSDK
22 {
27  template<typename T>
29  {
30  public:
35 
40 
46  static LinkedSegmentIterator incremented( const LinkedSegmentIterator& it, agx::UInt numSegments );
47 
48  public:
49 
55 
63 
64 
69  LinkedSegmentIterator( T* segment );
70 
76 
82  LinkedSegmentIterator& operator= ( const LinkedSegmentIterator& other );
84 
85 
88  agx::Bool operator== ( const LinkedSegmentIterator& other ) const;
89 
93  agx::Bool operator!= ( const LinkedSegmentIterator& other ) const;
94 
101 
108 
115 
122 
126  LinkedSegmentIterator operator+ ( agx::UInt numSegments ) const;
127 
131  LinkedSegmentIterator operator- ( agx::UInt numSegments ) const;
132 
136  agx::UInt operator- ( const LinkedSegmentIterator& other ) const;
137 
141  T* operator* () const;
142 
143  T* operator-> () const;
144 
148  T* get() const;
149 
155 
161 
165  LinkedSegmentIterator next() const;
166 
170  LinkedSegmentIterator prev() const;
171 
175  agx::Bool equals( const LinkedSegmentIterator& other ) const;
176 
180  agx::UInt index() const;
181 
185  agx::UInt findNextIndex( agx::UInt index ) const;
186 
190  agx::UInt findPrevIndex( agx::UInt index ) const;
191 
195  agx::Bool isBoundaryless() const;
196 
200  agx::Bool isEnd() const;
201 
202  private:
206  static agx::UInt modSize( agx::UInt index, const LinkedSegmentContainer& segments );
207 
208  private:
209  const LinkedSegmentContainer* containerPtr() const;
210 
211  template<typename U>
212  friend class LinkedSegmentRange;
213  void setBoundarylessGivenEndOfRange( const LinkedSegmentIterator& end );
214 
215  private:
216  agx::UInt m_index;
217  agx::Bool m_boundaryless;
218  const LinkedSegmentContainer* m_segments;
219  };
220 
221  template<typename T>
223  {
224  return LinkedSegmentIterator( 0u, false, segments );
225  }
226 
227  template<typename T>
229  {
230  return LinkedSegmentIterator( segments.size(), false, segments );
231  }
232 
233  template<typename T>
235  agx::UInt numSegments )
236  {
237  return LinkedSegmentIterator( it.m_index + numSegments, true, *it.m_segments );
238  }
239 
240 
241 
242  template<typename T>
244  : m_index(agx::InvalidIndex)
245  , m_boundaryless(false)
246  , m_segments(nullptr)
247  {
248  }
249 
250 
251 
252  template<typename T>
254  agx::Bool boundaryless,
255  const LinkedSegmentContainer& segments )
256  : m_index( boundaryless ?
257  modSize( index, segments ) :
258  std::min<agx::UInt>( index, segments.size() ) ),
259  m_boundaryless( boundaryless ),
260  m_segments( &segments )
261  {
262  }
263 
264  template<typename T>
266  : m_index( segment->getIndex() )
267  , m_boundaryless( false )
268  , m_segments( &segment->getLinkedStructure()->getSegmentsContainer() )
269  {
270  }
271 
272  template<typename T>
274  : m_index( other.m_index ), m_boundaryless( other.m_boundaryless ), m_segments( other.m_segments )
275  {
276  }
277 
278 
279 
280  template<typename T>
282  {
283  this->m_index = other.m_index;
284  this->m_boundaryless = other.m_boundaryless;
285  this->m_segments = other.m_segments;
286 
287  return *this;
288  }
289 
290 
291 
292  template<typename T>
294  {
295  return m_index == other.m_index && containerPtr() == other.containerPtr();
296  }
297 
298  template<typename T>
300  {
301  return !( *this == other );
302  }
303 
304  template<typename T>
306  {
307  m_index = findNextIndex( m_index );
308  return *this;
309  }
310 
311  template<typename T>
313  {
314  LinkedSegmentIterator it( *this );
315  ++(*this);
316  return it;
317  }
318 
319  template<typename T>
321  {
322  m_index = findPrevIndex( m_index );
323  return *this;
324  }
325 
326  template<typename T>
328  {
329  LinkedSegmentIterator it( *this );
330  --(*this);
331  return it;
332  }
333 
334  template<typename T>
336  {
337  const auto newIndex = m_index + numSegments;
338  if ( m_boundaryless )
339  return LinkedSegmentIterator( modSize( newIndex, *m_segments ), true, *m_segments );
340  return LinkedSegmentIterator( std::min<agx::UInt>( newIndex, m_segments->size() ), false, *m_segments );
341  }
342 
343  template<typename T>
345  {
346  if ( numSegments > m_index )
347  return m_boundaryless ?
348  LinkedSegmentIterator( m_segments->size() + m_index - numSegments, true, *m_segments ) :
349  LinkedSegmentIterator::begin( *m_segments );
350  return LinkedSegmentIterator( m_index - numSegments, m_boundaryless, *m_segments );
351  }
352 
353  template<typename T>
355  {
356  agxAssert( containerPtr() == other.containerPtr() );
357  return other.m_index <= m_index ?
358  m_index - other.m_index :
359  m_index + ( m_segments->size() - other.m_index );
360  }
361 
362  template<typename T>
364  {
365  return get();
366  }
367 
368  template<typename T>
370  {
371  return get();
372  }
373 
374  template<typename T>
376  {
377  return (*m_segments)[ m_index ]->template as<T>();
378  }
379 
380  template<typename T>
382  {
383  return ++(*this);
384  }
385 
386  template<typename T>
388  {
389  return --(*this);
390  }
391 
392  template<typename T>
394  {
395  LinkedSegmentIterator copy( *this );
396  return ++copy;
397  }
398 
399  template<typename T>
401  {
402  LinkedSegmentIterator copy( *this );
403  return --copy;
404  }
405 
406  template<typename T>
408  {
409  return (*this) == other;
410  }
411 
412  template<typename T>
414  {
415  return m_index;
416  }
417 
418  template<typename T>
420  {
421  agxAssert( m_boundaryless || index < m_segments->size() );
422  return m_boundaryless ?
423  modSize( index + 1, *m_segments ) :
424  index + 1;
425  }
426 
427  template<typename T>
429  {
430  agxAssert( m_boundaryless || index > 0 );
431  return m_boundaryless &&
432  index == 0 ?
433  m_segments->size() - 1 :
434  index - 1;
435  }
436 
437  template<typename T>
439  {
440  return m_boundaryless;
441  }
442 
443 
444  template <typename T>
446  {
447  return m_index == agx::InvalidIndex || m_segments == nullptr || m_index == m_segments->size();
448  }
449 
450  template<typename T>
452  {
453  return segments.empty() ?
454  0ul :
455  ( index % segments.size() );
456  }
457 
458  template<typename T>
460  {
461  return m_segments;
462  }
463 
464  template<typename T>
466  {
467  m_boundaryless = m_index > end.m_index;
468  }
469 }
470 
472 namespace std
473 {
474  template<typename T>
475  struct iterator_traits<agxSDK::LinkedSegmentIterator<T>>
476  {
477  using difference_type = agx::UInt;
478  using value_type = T*;
479  using iterator_category = std::random_access_iterator_tag;
480  };
481 }
483 
LinkedSegmentIterator & operator--()
Decrement this iterator by one.
LinkedSegmentIterator operator+(agx::UInt numSegments) const
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:861
bool Bool
Definition: Integer.h:48
Segment range object for linked structure.
agx::UInt findPrevIndex(agx::UInt index) const
LinkedSegmentIterator()
Create an iterator that doesn&#39;t point anywhere.
STL namespace.
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
LinkedSegmentIterator & inc()
Increments this by one (same as ++it).
LinkedSegmentIterator & dec()
Decrements this by one (same as –it).
static LinkedSegmentIterator begin(const agxSDK::LinkedSegmentContainer &segments)
agx::Bool operator==(const LinkedSegmentIterator &other) const
LinkedSegmentIterator & operator++()
Increment this iterator by one.
static LinkedSegmentIterator incremented(const LinkedSegmentIterator &it, agx::UInt numSegments)
uint32_t UInt
Definition: Integer.h:34
LinkedSegmentIterator operator-(agx::UInt numSegments) const
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::Bool operator!=(const LinkedSegmentIterator &other) const
#define agxAssert(expr)
Definition: debug.h:143
Optionally unbounded linked structure segment iterator object to iterate segments in a SegmentStructu...
static LinkedSegmentIterator end(const agxSDK::LinkedSegmentContainer &segments)
size_t size() const
Definition: Container.h:134
LinkedSegmentIterator prev() const
agx::UInt findNextIndex(agx::UInt index) const
agx::Bool equals(const LinkedSegmentIterator &other) const
LinkedSegmentIterator next() const
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
bool empty() const
Definition: Container.h:136
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97