/// <summary> /// </summary> /// <param name="b"></param> /// <param name="snapshot"></param> /// <param name="serialise_individual_observables"></param> /// <param name="do_serialise_unobservables"></param> /// <param name="do_serialise_aggregated_float_array"></param> /// <returns></returns> static Offset <FState> SerialiseState(FlatBufferBuilder b, EnvironmentSnapshot snapshot, bool do_serialise_unobservables = false, bool do_serialise_aggregated_float_array = false, bool serialise_individual_observables = false) { var n = b.CreateString(snapshot.EnvironmentName); var observables_vector = _null_vector_offset; if (do_serialise_aggregated_float_array) { observables_vector = FState.CreateObservablesVector(b, snapshot.Observables); } var unobservables = _null_unobservables_offset; if (do_serialise_unobservables) { var state_unobservables = snapshot.Unobservables; if (state_unobservables != null) { var bodies = state_unobservables.Bodies; FUnobservables.StartBodiesVector(b, bodies.Length); foreach (var rig in bodies) { var vel = rig.Velocity; var ang = rig.AngularVelocity; FBody.CreateFBody(b, vel.x, vel.y, vel.z, ang.x, ang.y, ang.z); } var bodies_vector = b.EndVector(); var poses = state_unobservables.Poses; FUnobservables.StartPosesVector(b, poses.Length); foreach (var tra in poses) { var pos = tra.position; var rot = tra.rotation; FQuaternionTransform.CreateFQuaternionTransform(b, pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, rot.w); } var poses_vector = b.EndVector(); FUnobservables.StartFUnobservables(b); FUnobservables.AddPoses(b, poses_vector); FUnobservables.AddBodies(b, bodies_vector); unobservables = FUnobservables.EndFUnobservables(b); } } var description_offset = new Offset <FEnvironmentDescription>(); if (snapshot.Description != null) { description_offset = Serialise(b, snapshot); } var d = new StringOffset(); if (snapshot.DebugMessage != "") { d = b.CreateString(snapshot.DebugMessage); } var t = b.CreateString(snapshot.TerminationReason); FState.StartFState(b); FState.AddEnvironmentName(b, n); FState.AddFrameNumber(b, snapshot.FrameNumber); if (do_serialise_aggregated_float_array) { FState.AddObservables(b, observables_vector); } if (do_serialise_unobservables) { FState.AddUnobservables(b, unobservables); } FState.AddSignal(b, snapshot.Signal); FState.AddTerminated(b, snapshot.Terminated); FState.AddTerminationReason(b, t); if (snapshot.Description != null) { FState.AddEnvironmentDescription(b, description_offset); } if (snapshot.DebugMessage != "") { FState.AddExtraSerialisedMessage(b, d); } return(FState.EndFState(b)); }
public static byte[] build_states(IEnumerable <EnvironmentState> states) { var b = new FlatBufferBuilder(1); foreach (var state in states) { var n = b.CreateString(state.EnvironmentName); var observables_vector = FState.CreateObservablesVector(b, state.Observables); var observers = new Offset <FOBS> [state.Observations.Values.Count]; var k = 0; foreach (var observer in state.Observations.Values) { observers[k++] = build_observer(b, observer); } var observers_vector = FState.CreateObservationsVector(b, observers); FUnobservables.StartBodiesVector(b, state.Unobservables.Bodies.Length); foreach (var rig in state.Unobservables.Bodies) { var vel = rig.Velocity; var ang = rig.AngularVelocity; FBody.CreateFBody(b, vel.x, vel.y, vel.z, ang.x, ang.y, ang.z); } var bodies_vector = b.EndVector(); FUnobservables.StartPosesVector(b, state.Unobservables.Poses.Length); foreach (var tra in state.Unobservables.Poses) { var pos = tra.position; var rot = tra.rotation; FQuaternionTransform.CreateFQuaternionTransform(b, pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, rot.w); } var poses_vector = b.EndVector(); FUnobservables.StartFUnobservables(b); FUnobservables.AddPoses(b, poses_vector); FUnobservables.AddBodies(b, bodies_vector); var unobservables = FUnobservables.EndFUnobservables(b); var description_offset = new Offset <FEnvironmentDescription>(); if (state.Description != null) { description_offset = build_description(b, state); } var d = new StringOffset(); if (state.DebugMessage != "") { d = b.CreateString(state.DebugMessage); } var t = b.CreateString(state.TerminationReason); FState.StartFState(b); FState.AddEnvironmentName(b, n); FState.AddFrameNumber(b, state.FrameNumber); FState.AddObservables(b, observables_vector); FState.AddUnobservables(b, unobservables); FState.AddTotalEnergySpent(b, state.TotalEnergySpentSinceReset); FState.AddSignal(b, state.Signal); FState.AddTerminated(b, state.Terminated); FState.AddTerminationReason(b, t); FState.AddObservations(b, observers_vector); if (state.Description != null) { FState.AddEnvironmentDescription(b, description_offset); } if (state.DebugMessage != "") { FState.AddDebugMessage(b, d); } var offset = FState.EndFState(b); FState.FinishFStateBuffer(b, offset); } return(b.SizedByteArray()); }