AGXDynamics  2.35.0.3
TrackUtils.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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 <agxVehicle/Track.h>
20 
21 #include <agxCollide/Box.h>
22 
23 namespace agxVehicle
24 {
25  namespace utils
26  {
27  inline void createGeometryBox( const TrackNode& node )
28  {
31  geometry->setLocalTransform( transform );
32  node.getRigidBody()->add( geometry );
33  }
34 
41  template<typename ContainerT>
42  IndexRangeContainer splitNonconsecutive( const ContainerT& container )
43  {
44  if ( container.size() < 2 )
45  return IndexRangeContainer{ IndexRange( 0, container.size() ) };
46 
47  agxAssert( std::is_sorted( container.begin(), container.end() ) );
48 
49  IndexRangeContainer result{ IndexRange( 0, 1 ) };
50  for ( agx::UInt i = 1; i < container.size(); ++i ) {
51  if ( container[ i ] == container[ i - 1 ] + typename ContainerT::value_type( 1 ) )
52  ++result.back().end();
53  else
54  result.push_back( agx::IndexRange( i, i + 1 ) );
55  }
56 
57  return result;
58  }
59 
69  template<typename ContainerT>
70  IndexRangeContainer splitNonconsecutive( const Track& track, const ContainerT& container )
71  {
72  auto result = splitNonconsecutive( container );
73 
74  agxAssert( !result.empty() );
75  if ( result.size() == 1 && result.front().empty() )
76  return IndexRangeContainer{};
77 
78  // Transform ranges from indices to container values. E.g.,
79  // 2 4 7 [9 10 11 12] 18
80  // index: 3 ... 6
81  // range original: begin = 3, end = 7
82  // range transformed: begin = 9, end = 13
83  for ( agx::UInt i = 0; i < result.size(); ++i ) {
84  auto& range = result[ i ];
85  const auto size = range.size();
86  range.begin() = container[ range.begin() ];
87  range.end() = range.begin() + size;
88  }
89 
90  if ( result.size() < 2 )
91  return result;
92 
93  // Including the last range in the first if the first node belongs
94  // to the first range and the last node to the last.
95  const auto numNodes = track.nodes().size();
96  if ( result.front().begin() == agx::UInt( 0 ) && result.back().end() == numNodes ) {
97  result.front().begin() = result.back().begin();
98  result.pop_back();
99  }
100 
101  return result;
102  }
103 
107  inline agx::UInt prevIndex( agx::UInt index, agx::UInt size )
108  {
109  agxAssert( size > 0 && index != agx::InvalidIndex );
110  return index == 0 ? size - 1 : index - 1;
111  }
112 
116  inline agx::UInt nextIndex( agx::UInt index, agx::UInt size )
117  {
118  agxAssert( size > 0 && index != agx::InvalidIndex );
119  return ( index + 1 ) % size;
120  }
121 
128  inline TrackNodeRange createRangeExcludingNodeIndex( const Track& track, agx::UInt nodeIndex, agx::Int signedNumNodes )
129  {
130  const auto totalNumNodes = track.nodes().size();
131  if ( totalNumNodes < 1 )
132  return track.nodes();
133 
134  const agx::UInt numNodes = signedNumNodes < 0 ?
135  (agx::UInt)-signedNumNodes :
136  (agx::UInt)signedNumNodes;
137  agx::UInt beginIndex = agx::InvalidIndex;
138  agx::UInt endIndex = agx::InvalidIndex;
139  if ( signedNumNodes < 0 ) {
140  beginIndex = nodeIndex > numNodes ?
141  nodeIndex - numNodes :
142  nodeIndex + totalNumNodes - numNodes;
143  endIndex = nodeIndex;
144  }
145  else {
146  beginIndex = ( nodeIndex + 1 ) % totalNumNodes;
147  endIndex = ( nodeIndex + numNodes + 1 ) % totalNumNodes;
148  }
149 
150  return TrackNodeRange( track.getIterator( beginIndex ), track.getIterator( endIndex ) );
151  }
152  }
153 }
agxCollide::Box
A box shape for geometric intersection tests.
Definition: Box.h:32
agxVehicle::TrackNodeRange
agxSDK::LinkedSegmentRange< TrackNode > TrackNodeRange
Node range object for tracks.
Definition: TrackNodeRange.h:28
agxSDK::LinkedSegment::getHalfExtents
const agx::Vec3 & getHalfExtents() const
agxData::transform
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
agxVehicle::utils::createGeometryBox
void createGeometryBox(const TrackNode &node)
Definition: TrackUtils.h:27
Track.h
agx::Container::size
size_t size() const
Definition: Container.h:134
agx::AffineMatrix4x4T< Real >::translate
static AffineMatrix4x4T< Real > translate(const Vec3T< Real > &dv)
Return a matrix which translates according to dv.
Definition: AffineMatrix4x4.h:657
agxVehicle::utils::prevIndex
agx::UInt prevIndex(agx::UInt index, agx::UInt size)
Definition: TrackUtils.h:107
agxVehicle::Track::nodes
NodeRange nodes() const
agxVehicle::TrackNode
Node/segment/shoe object in agxVehicle::Track.
Definition: TrackNode.h:46
agx::AffineMatrix4x4T< Real >
agx::Int
int32_t Int
Definition: Integer.h:35
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agxVehicle::Track
Assembly object representing a continuous track with a given number of shoes (nodes).
Definition: agxVehicle/Track.h:36
agxSDK::LinkedSegmentRange::size
agx::UInt size() const
Definition: LinkedSegmentRange.h:203
agx::VectorPOD< IndexRange >
agx::IndexRangeT< agx::UInt >
agxVehicle::utils::splitNonconsecutive
IndexRangeContainer splitNonconsecutive(const ContainerT &container)
Given a sorted container this function will split the content into ranges where the elements are cons...
Definition: TrackUtils.h:42
agx::ref_ptr< Geometry >
agxCollide::Geometry
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
agxSDK::LinkedSegment::getRigidBody
agx::RigidBody * getRigidBody() const
agxVehicle
This namespace contains classes related to vehicles dynamics.
Definition: AckermannSteering.h:22
agx::RigidBody::add
bool add(agxCollide::Geometry *geometry, const agx::AffineMatrix4x4 &localTransform, bool incrementalMassCalculation=false)
Connect a geometry to this rigid body.
agxVehicle::Track::getIterator
Iterator getIterator(agx::UInt index) const
Definition: agxVehicle/Track.h:404
agx::UInt
uint32_t UInt
Definition: Integer.h:34
agxVehicle::utils::nextIndex
agx::UInt nextIndex(agx::UInt index, agx::UInt size)
Definition: TrackUtils.h:116
agx::IndexRangeT::end
T & end()
Definition: IndexRange.h:217
Box.h
agx::InvalidIndex
const AGXCORE_EXPORT InvalidIndexStruct InvalidIndex
agxSDK::LinkedSegmentRange
Segment range object for linked structure.
Definition: LinkedSegmentRange.h:27
agxVehicle::utils::createRangeExcludingNodeIndex
TrackNodeRange createRangeExcludingNodeIndex(const Track &track, agx::UInt nodeIndex, agx::Int signedNumNodes)
Creates a node range where nodeIndex is excluded and number of nodes to include is signed.
Definition: TrackUtils.h:128
agxVehicle::IndexRange
agx::IndexRangeT< agx::UInt > IndexRange
Definition: TrackNode.h:35
agx::Vec3T::z
T & z()
Definition: Vec3Template.h:511