Skip to content

Commit 7313e28

Browse files
committed
Update Track mapping with the new ontology
1 parent f6cd448 commit 7313e28

6 files changed

Lines changed: 446 additions & 324 deletions

File tree

AGXUnity/IO/OpenPLX/Errors.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public enum AgxUnityOpenPLXErrors
2222
TraitNotImportable = 15,
2323
NonPrincipalAxis = 16,
2424
MultipleDistanceDistortions = 17,
25-
NonSymmetricSweepPattern = 18,
25+
MissingTrackSystem = 18,
26+
DuplicateTrackConnection = 19,
27+
InvalidLinkDescription = 20,
28+
InsufficientTrackWheels = 21,
29+
MissingTrackWheelBody = 22,
2630
}
2731

2832
public class BaseError : openplx.Error
@@ -281,12 +285,52 @@ public MultipleDistanceDistortionsError( openplx.Core.Object source )
281285
protected override string createErrorMessage() => $"AGXUnity only supports a single distance distortion";
282286
}
283287

284-
public class NonSymmetricSweepPatternError : BaseError
288+
public class MissingTrackSystemError : BaseError
285289
{
286-
public NonSymmetricSweepPatternError( openplx.Math.Vec2 FoV )
287-
: base( FoV, AgxUnityOpenPLXErrors.NonSymmetricSweepPattern )
290+
public MissingTrackSystemError( openplx.Vehicles.TrackSystem.Connections.Base source )
291+
: base( source, AgxUnityOpenPLXErrors.MissingTrackSystem )
288292
{ }
289293

290-
protected override string createErrorMessage() => $"AGXUnity only supports symmetrical sweep patterns";
294+
protected override string createErrorMessage() => $"Track connection does not specify a track system";
295+
}
296+
297+
public class DuplicateTrackConnectionError : BaseError
298+
{
299+
private openplx.Vehicles.TrackSystem.Base m_system;
300+
301+
public DuplicateTrackConnectionError( openplx.Vehicles.TrackSystem.Connections.Base source, openplx.Vehicles.TrackSystem.Base system )
302+
: base( source, AgxUnityOpenPLXErrors.DuplicateTrackConnection )
303+
{
304+
m_system = system;
305+
}
306+
307+
protected override string createErrorMessage() => $"Track system '{m_system.getName()}' is specified in multiple track connections";
308+
}
309+
310+
public class InvalidLinkDescriptionError : BaseError
311+
{
312+
public InvalidLinkDescriptionError( openplx.Vehicles.TrackSystem.Components.Tracks.LinkDescription.Base source )
313+
: base( source, AgxUnityOpenPLXErrors.InvalidLinkDescription )
314+
{ }
315+
316+
protected override string createErrorMessage() => $"Link description needs to have non-zero, positive width and height";
317+
}
318+
319+
public class InsufficientTrackWheelsError : BaseError
320+
{
321+
public InsufficientTrackWheelsError( openplx.Vehicles.TrackSystem.Base source )
322+
: base( source, AgxUnityOpenPLXErrors.InsufficientTrackWheels )
323+
{ }
324+
325+
protected override string createErrorMessage() => $"Track system needs to have at least two track wheels";
326+
}
327+
328+
public class MissingTrackWheelBodyError : BaseError
329+
{
330+
public MissingTrackWheelBodyError( openplx.Vehicles.TrackSystem.Components.TrackWheels.Base source )
331+
: base( source, AgxUnityOpenPLXErrors.MissingTrackWheelBody )
332+
{ }
333+
334+
protected override string createErrorMessage() => $"Track wheel's body was not properly mapped to a RigidBody";
291335
}
292336
}

AGXUnity/IO/OpenPLX/InteractionMapper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ public void MapFrictionModel( ContactMaterial cm, openplx.Physics.Interactions.D
507507
fm = Data.DefaultFriction;
508508
}
509509

510+
if ( friction.HasTrait<openplx.Vehicles.TrackSystem.SurfaceContact.Traits.TrackFriction>() )
511+
fm.TrackFrictionModel = true;
512+
510513
var solveType = FrictionModel.ESolveType.Direct;
511514
var solveTypeAnnotation = friction.findAnnotations("agx_friction_solve_type");
512515
var approximateConeAnnotation = friction.findAnnotations("agx_approximate_cone_friction");

AGXUnity/IO/OpenPLX/MappingUtils.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,58 @@ public static bool HasTrait<T>( this openplx.Core.Object obj )
7474
string traitName = typeof( T ).FullName.Substring( 8 );
7575
return obj.hasTrait( traitName );
7676
}
77+
78+
public static bool TryGetBoolAnnotation( this openplx.Core.Object obj, string name, out bool value )
79+
{
80+
value = true; // This will be overwritten
81+
var annots = obj.findAnnotations(name);
82+
if ( annots.IsEmpty )
83+
return false;
84+
85+
foreach ( var annot in annots ) {
86+
if ( annot.isTrue() ) {
87+
value = true;
88+
return true;
89+
}
90+
if ( annot.isFalse() ) {
91+
value = false;
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
98+
public static bool TryGetRealAnnotation( this openplx.Core.Object obj, string name, out float value )
99+
{
100+
value = 0.0f; // This will be overwritten
101+
var annots = obj.findAnnotations(name);
102+
if ( annots.IsEmpty )
103+
return false;
104+
105+
foreach ( var annot in annots ) {
106+
if ( annot.isNumber() ) {
107+
value = (float)annot.asReal();
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
113+
114+
public static bool TryGetStringAnnotation( this openplx.Core.Object obj, string name, out string value )
115+
{
116+
value = ""; // This will be overwritten
117+
var annots = obj.findAnnotations(name);
118+
if ( annots.IsEmpty )
119+
return false;
120+
121+
foreach ( var annot in annots ) {
122+
if ( annot.isString() ) {
123+
value = annot.asString();
124+
return true;
125+
}
126+
}
127+
return false;
128+
}
77129
}
78130
public static class Utils
79131
{
@@ -98,6 +150,11 @@ public static T ReportUnimplemented<T>( openplx.Core.Object obj, openplx.ErrorRe
98150
return null;
99151
}
100152

153+
public static void ReportUnimplemented( openplx.Core.Object obj, openplx.ErrorReporter err )
154+
{
155+
err.reportError( new UnimplementedError( obj ) );
156+
}
157+
101158
public static void AddChild( GameObject parent, GameObject child, openplx.ErrorReporter err, openplx.Core.Object obj )
102159
{
103160
if ( child != null )

AGXUnity/IO/OpenPLX/OpenPLXRoot.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,26 @@ protected override bool Initialize()
115115
var errorReporter = new ErrorReporter();
116116
var mapper = new agxopenplx.OpenPlxDriveTrainMapper( errorReporter, map );
117117

118-
agxPowerLine.PowerLineRef powerline = new agxPowerLine.PowerLineRef(new agxPowerLine.PowerLine());
119-
mapper.mapDriveTrainIntoPowerLine( Native as openplx.Physics.System, powerline );
120-
121118
RuntimeMapped = new Dictionary<string, agx.Referenced>();
122-
Simulation.Instance.Native.add( powerline.get() );
123-
foreach ( var (obj, constraint) in mapper.getMappedConstraints() ) {
124-
Simulation.Instance.Native.add( constraint.get() );
125-
RuntimeMapped.Add( obj.getName(), constraint.get() );
126-
}
127-
foreach ( var obj in powerline.getUnits() ) {
128-
var objName = obj.getName();
129-
if ( objName.StartsWith( PrunedNativeName + "." ) )
130-
RuntimeMapped.Add( objName, obj.get() );
131-
}
132-
foreach ( var obj in powerline.getConnectors() ) {
133-
var objName = obj.getName();
134-
if ( objName.StartsWith( PrunedNativeName + "." ) )
135-
RuntimeMapped.Add( objName, obj.get() );
119+
120+
agxPowerLine.PowerLineRef powerline = mapper.mapDriveTrainIntoPowerLine( Native as openplx.Physics.System);
121+
// TODO: Fix null return from this method
122+
if ( powerline != null && powerline.get() != null ) {
123+
Simulation.Instance.Native.add( powerline.get() );
124+
foreach ( var (obj, constraint) in mapper.getMappedConstraints() ) {
125+
Simulation.Instance.Native.add( constraint.get() );
126+
RuntimeMapped.Add( obj.getName(), constraint.get() );
127+
}
128+
foreach ( var obj in powerline.getUnits() ) {
129+
var objName = obj.getName();
130+
if ( objName.StartsWith( PrunedNativeName + "." ) )
131+
RuntimeMapped.Add( objName, obj.get() );
132+
}
133+
foreach ( var obj in powerline.getConnectors() ) {
134+
var objName = obj.getName();
135+
if ( objName.StartsWith( PrunedNativeName + "." ) )
136+
RuntimeMapped.Add( objName, obj.get() );
137+
}
136138
}
137139

138140
return base.Initialize();

AGXUnity/IO/OpenPLX/OpenPLXUnityMapper.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class OpenPLXUnityMapper
4949
Options = options;
5050

5151
InteractionMapper = new InteractionMapper( Data );
52-
VehicleMapper = new VehicleMapper( Data );
52+
VehicleMapper = new VehicleMapper( Data, InteractionMapper );
5353
SensorMapper = new SensorMapper( Data );
5454
}
5555

@@ -851,6 +851,8 @@ GameObject MapSystem( openplx.Physics3D.System system )
851851
{
852852
var s = MapSystemPass1( system );
853853
MapSystemPass2( system );
854+
// This method performs actual mapping of previously discovered tracks and is not recursive
855+
VehicleMapper.MapTrackSystems();
854856
MapSystemPass3( system );
855857
MapSystemPass4( system );
856858
MapSystemPass5( system );
@@ -884,14 +886,8 @@ GameObject MapSystemPass1( openplx.Physics3D.System system )
884886
Data.MaterialCache[ geometry.material() ] = Data.DefaultMaterial;
885887
}
886888
}
887-
foreach ( var trackSystem in system.getNonReferenceValues<openplx.Vehicles.Tracks.System>() ) {
888-
if ( trackSystem.belt().link_description() is openplx.Vehicles.Tracks.BoxLinkDescription desc ) {
889-
var mat = desc.contact_geometry().material();
890-
if ( !Data.MaterialCache.ContainsKey( mat ) ) {
891-
Data.MaterialCache[ mat ] = InteractionMapper.MapMaterial( mat );
892-
}
893-
}
894-
}
889+
890+
VehicleMapper.MapSystemPass1( system );
895891

896892
return s;
897893
}
@@ -919,6 +915,8 @@ void MapSystemPass2( openplx.Physics3D.System system )
919915

920916
foreach ( var terr in system.getNonReferenceValues<openplx.Terrain.Terrain>() )
921917
Utils.AddChild( s, MapTerrain( terr ), Data.ErrorReporter, terr );
918+
919+
VehicleMapper.MapSystemPass2( system );
922920
}
923921

924922
void MapSystemPass3( openplx.Physics3D.System system )
@@ -928,9 +926,6 @@ void MapSystemPass3( openplx.Physics3D.System system )
928926
foreach ( var subSystem in system.getNonReferenceValues<openplx.Physics3D.System>() )
929927
MapSystemPass3( subSystem );
930928

931-
foreach ( var trackSystem in system.getNonReferenceValues<openplx.Vehicles.Tracks.System>() )
932-
VehicleMapper.MapTrackSystem( trackSystem );
933-
934929
foreach ( var mateConnector in system.getNonReferenceValues<openplx.Physics3D.Interactions.MateConnector>() )
935930
InteractionMapper.MapMateConnector( mateConnector );
936931

@@ -946,8 +941,7 @@ void MapSystemPass4( openplx.Physics3D.System system )
946941
foreach ( var subSystem in system.getNonReferenceValues<openplx.Physics3D.System>() )
947942
MapSystemPass4( subSystem );
948943

949-
if ( system is openplx.Vehicles.Suspensions.SingleMate.Base suspension )
950-
VehicleMapper.MapSingleMateSuspensionOnto( suspension, s );
944+
VehicleMapper.MapSystemPass4( system );
951945

952946
foreach ( var lidar in system.getNonReferenceValues<openplx.Sensors.LidarLogic>() )
953947
Utils.AddChild( s, SensorMapper.MapLidar( lidar ), Data.ErrorReporter, lidar );
@@ -985,11 +979,7 @@ void MapSystemPass5( openplx.Physics3D.System system )
985979
foreach ( var subSystem in system.getNonReferenceValues<openplx.Physics3D.System>() )
986980
MapSystemPass5( subSystem );
987981

988-
if ( system is openplx.Vehicles.Steering.Kinematic.Base steering )
989-
VehicleMapper.MapSteeringOnto( steering, s );
990-
991-
foreach ( var wheel in system.getNonReferenceValues<openplx.Vehicles.Wheels.ElasticWheel>() )
992-
VehicleMapper.MapElasticWheel( wheel );
982+
VehicleMapper.MapSystemPass5( system );
993983
}
994984
}
995985
}

0 commit comments

Comments
 (0)