Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
SupportUSD_Exporter.h
1// Copyright (c) 2013 - 2019 Marc-Michael Horstmann;
2// Copyright (c) 2020 - 2024 Trend Verlag;
3//
4// trax track library
5// AD 2024
6//
7// "the resolution of all the fruitless searches"
8//
9// Peter Gabriel
10
11
12#pragma once
13
14#include "SupportUSD.h"
15#include "SupportUSD_Tokens.h"
16
17#include "dim/support/DimSupportStream.h"
18
19#include "spat/VectorBundle.h"
20#include "spat/VectorBundle2.h"
21#include "spat/Frame.h"
22
23#include <stack>
24
25namespace trax{
26
33 class USDExporter{
34 public:
35 USDExporter( pxr::UsdStageRefPtr stage, const pxr::SdfPath& atPath, bool bWriteTrackMesh = false );
36 ~USDExporter();
37
45 void Prepare( TrackSystem& tracksystem );
46
47
49 void Write( const TrackSystem& tracksystem );
50
51
52 inline pxr::UsdStageRefPtr GetStage() const noexcept{
53 return m_Stage;
54 }
55 private:
56 void Prepare( TrackCollection& collection );
57 void Prepare( ConnectorCollection& collection );
58 void Prepare( TrackBuilder& track );
59 void Prepare( Connector& connector );
60
61 void Write( const TrackCollection& collection );
62 void Write( const ConnectorCollection& collection );
63 void Write( const TrackBuilder& track );
64 void Write( const Connector& connector );
65 void Write( const Switch& connector );
66 void Write( const ThreeWaySwitch& connector );
67 void Write( const SingleSlipSwitch& connector );
68 void Write( const DoubleSlipSwitch& connector );
69 void Write( const Curve& curve, const std::string& pathName = "Curve" );
70 void Write( const Line& line, const std::string& pathName = "Curve" );
71 void Write( const LineP& line, const std::string& pathName = "Curve" );
72 void Write( const Arc& arc, const std::string& pathName = "Curve" );
73 void Write( const ArcP& arcP, const std::string& pathName = "Curve" );
74 void Write( const Helix& helix, const std::string& pathName = "Curve" );
75 void Write( const HelixP& helixP, const std::string& pathName = "Curve" );
76 void Write( const Cubic& cubic, const std::string& pathName = "Curve" );
77 void Write( const Spline& spline, const std::string& pathName = "Curve" );
78 void Write( const Clothoid&, const std::string& pathName = "Curve" );
79 void Write( const Rotator&, const std::string& pathName = "Curve" );
80 void Write( const RotatorChain&, const std::string& pathName = "Curve" );
81 void Write( const PolygonalChain&, const std::string& pathName = "Curve" );
82 void Write( const SampledCurve& curve, const std::string& pathName = "Curve" );
83 void Write( const RoadwayTwist& twist, const std::string& pathName = "Twist" );
84 void Write( const ConstantTwist& twist, const std::string& pathName = "Twist" );
85 void Write( const LinearTwist& twist, const std::string& pathName = "Twist" );
86 void Write( const PiecewiseTwist& twist, const std::string& pathName = "Twist" );
87 void Write( const DirectionalTwist& twist, const std::string& pathName = "Twist" );
88 void Write( const PiecewiseDirectionalTwist& twist, const std::string& pathName = "Twist" );
89 void Write( const CombinedTwist& twist, const std::string& pathName = "Twist" );
90 void Write( const Section& section );
91
92 pxr::UsdStageRefPtr m_Stage;
93 std::stack<pxr::SdfPath> m_PathStack;
94 bool m_bWriteTrackMesh;
95
96 void Write( pxr::UsdPrim& toPrim, const spat::Frame<dim::Length,dim::One>& frame );
97
98 template<typename D>
99 void Write( const common::Interval<D>& interval, const pxr::TfToken& typeName = UsdTraxTokens->Interval, const std::string& pathName = "Interval" ){
100 m_PathStack.push( m_PathStack.top().AppendPath( pxr::SdfPath{ pathName } ) );
101 if( pxr::UsdPrim primInterval = m_Stage->DefinePrim( m_PathStack.top(), typeName ); primInterval )
102 {
103 CreateAttribute( primInterval, UsdTraxTokens->Near, interval.Near() );
104 CreateAttribute( primInterval, UsdTraxTokens->Far, interval.Far() );
105 }
106 m_PathStack.pop();
107 }
108
109 template<typename D>
110 void Write( const spat::Position<D>& pos, const pxr::TfToken& typeName, const std::string& pathName )
111 {
112 m_PathStack.push( m_PathStack.top().AppendPath( pxr::SdfPath{ pathName } ) );
113 if( pxr::UsdPrim prim = m_Stage->DefinePrim( m_PathStack.top(), typeName ); prim )
114 {
115 CreateAttribute( prim, UsdTraxTokens->X, pos.x );
116 CreateAttribute( prim, UsdTraxTokens->Y, pos.y );
117 CreateAttribute( prim, UsdTraxTokens->Z, pos.z );
118 }
119 m_PathStack.pop();
120 }
121
122 template<typename D>
123 void Write( const spat::Vector<D>& T, const pxr::TfToken& typeName, const std::string& pathName )
124 {
125 m_PathStack.push( m_PathStack.top().AppendPath( pxr::SdfPath{ pathName } ) );
126 if( pxr::UsdPrim prim = m_Stage->DefinePrim( m_PathStack.top(), typeName ); prim )
127 {
128 CreateAttribute( prim, UsdTraxTokens->Dx, T.dx );
129 CreateAttribute( prim, UsdTraxTokens->Dy, T.dy );
130 CreateAttribute( prim, UsdTraxTokens->Dz, T.dz );
131 }
132 m_PathStack.pop();
133 }
134
135 void Write( const spat::VectorBundle<dim::Length,dim::One>& bundle, const pxr::TfToken& typeName, const std::string& pathName );
136 void Write( const spat::VectorBundle2<dim::Length,dim::One>& bundle2, const pxr::TfToken& typeName, const std::string& pathName );
137 void Write( const spat::Frame<dim::Length,dim::One>& frame, const pxr::TfToken& typeName, const std::string& pathName );
138 void Write( const CurveSample& curveSample, const pxr::TfToken& typeName, const std::string& pathName );
139 void Write( const CubicData& cubicData, const pxr::TfToken& typeName, const std::string& pathName );
140
141 template<typename D> inline
142 void CreateAttribute( pxr::UsdPrim& toPrim, const pxr::TfToken& typeName, D d ){
143 if( pxr::UsdAttribute attribute = toPrim.CreateAttribute( typeName, pxr::SdfValueTypeNames->String ); attribute )
144 attribute.Set( to_string( d ) );
145 }
146
147 template<class TraxType>
148 static pxr::SdfPath Name( const TraxType& object, const char* pDefault )
149 {
150 if( std::string Name = object.Reference( "Name" ); Name.empty() )
151 return pxr::SdfPath{ pDefault + std::to_string( object.ID() ) };
152 else
153 return pxr::SdfPath{ pxr::SdfPath{ Name }.GetName() };
154 }
155 };
156
157}
void Prepare(TrackSystem &tracksystem)
Prepare the stage for writing the track system.
void Write(const TrackSystem &tracksystem)
Write the track system to the stage.
Namespace of all the trax track libraries classes and methods.
Definition Collection.h:17
dclspc pxr::TfStaticData< UsdTraxTokenTypes > UsdTraxTokens
An interval describes the area between two numbers. It is understood to contain the near one and exlu...
Definition Interval.h:42
constexpr Valtype Near() const noexcept
Definition Interval.h:381
constexpr Valtype Far() const noexcept
Definition Interval.h:386
A Frame ("TNBFrame") describes a location in 3d space and an orientation using a right handed coordin...
Definition Frame.h:52
Valtype y
cartesian y coordinate.
Definition Position.h:50
Valtype z
cartesian z coordinate.
Definition Position.h:51
Valtype x
cartesian x coordinate.
Definition Position.h:49
Valtype dy
cartesian y component.
Definition Vector.h:52
Valtype dx
cartesian x component.
Definition Vector.h:51
Valtype dz
cartesian z component.
Definition Vector.h:53
A plane arc.
Definition Curve.h:602
A plane arc with owned parameters.
Definition Curve.h:669
Curve with linear increasing curvature. Also called 'Euler Spiral'.
Definition Curve.h:1393
Combines the effect of two twists by adding them.
Definition RoadwayTwist.h:468
Collection to hold a set of Connector objects.
Definition ConnectorCollection.h:36
A Connector rearranges the connections between several track ends.
Definition Connector.h:226
A twist that stays constant over the whole track range.
Definition RoadwayTwist.h:265
Cubic polynom curve.
Definition Curve.h:1067
Curves implement this interface that then can get attached to a track to define the tracks geometry.
Definition Curve.h:198
A twist that rotates the frame arount T in a way that B is nearest a certain direction.
Definition RoadwayTwist.h:372
A double slip switch is made from eight tracks that form a crossing with the possibility to divert th...
Definition Switch.h:527
A three dimensional spiral.
Definition Curve.h:812
A three dimensional spiral with owned parameters.
Definition Curve.h:903
A straight line.
Definition Curve.h:498
A straight line with owned parameters.
Definition Curve.h:511
A twist that varies linearly between two values.
Definition RoadwayTwist.h:290
A twist that works like a DirectionalTwist but interpolates (linearly) two neighbouring attractors ac...
Definition RoadwayTwist.h:419
A twist with values at control points and either none or linear interpolation for the points in betwe...
Definition RoadwayTwist.h:333
A series of samples of points and tangents that make up a curve.
Definition Curve.h:1744
A RoadwayTwist is used to define the actual rotating angle around the tangent at a curve to get the c...
Definition RoadwayTwist.h:81
A series of Rotator curves that continue each other.
Definition Curve.h:1631
Curve with evenly (with respect to arc length) rotating tangent vector.
Definition Curve.h:1568
A curve given by Fi,ki,ti,si samples of a real curveT.
Definition Curve.h:1856
A profile of a track.
Definition Section.h:45
A single slip switch is made from five tracks that form a crossing with the possibility to divert the...
Definition Switch.h:439
\ brief Cubic Hermite Spline.
Definition Curve.h:1162
A Switch rearranges the connections between three track ends.
Definition Switch.h:186
A switch that is connecting one narrow end to three outgoing track ends.
Definition Switch.h:326
Interface for making and shaping tracks.
Definition Track.h:831
Collection to hold a set of Track objects.
Definition TrackCollection.h:48
A track system is the container for tracks.
Definition TrackSystem.h:56