Skip to content

Commit

Permalink
Finished Definition of Gridded Data PDU (#35)
Browse files Browse the repository at this point in the history
* Changed sizes of Designator and EE PDUs

* [WIP] Implementing DataRepresentationType and other necessary type information for GriddedDataPdu

* Finished implementation of Gridded Data PDU
  • Loading branch information
crhowell3 authored Aug 4, 2024
1 parent c320438 commit 7ecd811
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/common/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8805,16 +8805,16 @@ impl GriddedDataFieldNumber {
#[derive(Copy, Clone, Debug, Default, FromPrimitive, PartialEq)]
pub enum GriddedDataCoordinateSystem {
#[default]
RighthandedCartesian = 0,
LefthandedCartesian = 1,
RightHandedCartesian = 0,
LeftHandedCartesian = 1,
LatitudeLongitudeHeight = 2,
LatitudeLongitudeDepth = 3,
}

impl GriddedDataCoordinateSystem {
#[must_use]
pub fn decode(buf: &mut BytesMut) -> Self {
Self::from_u8(buf.get_u8()).unwrap_or(Self::default())
Self::from_u16(buf.get_u16()).unwrap_or(Self::default())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distributed_emissions/designator_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Default for DesignatorPdu {
pdu_header: PduHeader::default(
PduType::Designator,
ProtocolFamily::DistributedEmissionRegeneration,
56,
88,
),
designating_entity_id: EntityId::default(1),
code_name: 0,
Expand Down Expand Up @@ -165,7 +165,7 @@ mod tests {
let pdu_header = PduHeader::default(
PduType::Designator,
ProtocolFamily::DistributedEmissionRegeneration,
448 / 8,
88,
);

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion src/distributed_emissions/electromagnetic_emissions_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Default for ElectromagneticEmissionsPdu {
pdu_header: PduHeader::default(
PduType::ElectromagneticEmission,
ProtocolFamily::DistributedEmissionRegeneration,
56,
28,
),
emitting_entity_id: EntityId::default(1),
event_id: EventId::default(1),
Expand Down
137 changes: 137 additions & 0 deletions src/synthetic_environment/data_types/grid_axis_descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// open-dis-rust - Rust implementation of the IEEE-1278.1 Distributed Interactive Simulation
// Copyright (C) 2023 Cameron Howell
//
// Licensed under the BSD-2-Clause License

use bytes::{Buf, BufMut, BytesMut};

use crate::common::enums::GridAxisDescriptorAxisType;

#[derive(Clone, Debug, PartialEq)]
pub enum GridAxisType {
FixedSpacing {
number_of_points_on_x_axis: u16,
initial_index: u16,
},
VariableSpacing {
number_of_points_on_x_axis: u16,
initial_index: u16,
coordinate_scale_x: f64,
coordinate_offset_x: f64,
x_values: Vec<u16>,
padding: Vec<u8>,
},
}

#[derive(Clone, Debug)]
pub struct GridAxisDescriptor {
pub domain_initial: f64,
pub domain_final: f64,
pub domain_points: u16,
pub interleaf_factor: u8,
pub axis_type: GridAxisDescriptorAxisType,
pub data: GridAxisType,
}

impl GridAxisDescriptor {
#[must_use]
pub fn new(
domain_initial: f64,
domain_final: f64,
domain_points: u16,
interleaf_factor: u8,
axis_type: GridAxisDescriptorAxisType,
data: GridAxisType,
) -> Self {
GridAxisDescriptor {
domain_initial,
domain_final,
domain_points,
interleaf_factor,
axis_type,
data,
}
}

pub fn serialize(&self, buf: &mut BytesMut) {
buf.put_f64(self.domain_initial);
buf.put_f64(self.domain_final);
buf.put_u16(self.domain_points);
buf.put_u8(self.interleaf_factor);
buf.put_u8(self.axis_type as u8);
match &self.data {
GridAxisType::FixedSpacing {
number_of_points_on_x_axis,
initial_index,
} => {
buf.put_u16(*number_of_points_on_x_axis);
buf.put_u16(*initial_index);
}
GridAxisType::VariableSpacing {
number_of_points_on_x_axis,
initial_index,
coordinate_scale_x,
coordinate_offset_x,
x_values,
padding,
} => {
buf.put_u16(*number_of_points_on_x_axis);
buf.put_u16(*initial_index);
buf.put_f64(*coordinate_scale_x);
buf.put_f64(*coordinate_offset_x);
for x in x_values {
buf.put_u16(*x);
}
for p in padding {
buf.put_u8(*p);
}
}
}
}

pub fn decode(buf: &mut BytesMut) -> Self {
let domain_initial = buf.get_f64();
let domain_final = buf.get_f64();
let domain_points = buf.get_u16();
let interleaf_factor = buf.get_u8();
let axis_type = GridAxisDescriptorAxisType::decode(buf);
let data = match axis_type {
GridAxisDescriptorAxisType::RegularAxis => GridAxisType::FixedSpacing {
number_of_points_on_x_axis: buf.get_u16(),
initial_index: buf.get_u16(),
},
GridAxisDescriptorAxisType::IrregularAxis => {
let number_of_points_on_x_axis = buf.get_u16();
let initial_index = buf.get_u16();
let coordinate_scale_x = buf.get_f64();
let coordinate_offset_x = buf.get_f64();
let mut x_values: Vec<u16> = vec![];
for _ in 0..number_of_points_on_x_axis {
x_values.push(buf.get_u16());
}
let mut padding: Vec<u8> = vec![];
while buf.has_remaining() {
padding.push(buf.get_u8());
}

GridAxisType::VariableSpacing {
number_of_points_on_x_axis,
initial_index,
coordinate_scale_x,
coordinate_offset_x,
x_values,
padding,
}
}
};

GridAxisDescriptor {
domain_initial,
domain_final,
domain_points,
interleaf_factor,
axis_type,
data,
}
}
}
164 changes: 133 additions & 31 deletions src/synthetic_environment/data_types/grid_data_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,160 @@

use bytes::{Buf, BufMut, BytesMut};

#[derive(Clone, Debug, Default)]
use crate::common::enums::{GriddedDataDataRepresentation, GriddedDataSampleType};

#[derive(Clone, Debug, PartialEq)]
pub enum DataRepresentationType {
Type0 {
number_of_octets: u16,
data_values: Vec<u8>,
padding: Vec<u8>,
},
Type1 {
field_scale: f32,
field_offset: f32,
number_of_values: u16,
data_values: Vec<u16>,
padding: Vec<u8>,
},
Type2 {
number_of_values: u16,
padding: u16,
data_values: Vec<f32>,
},
}

#[derive(Clone, Debug)]
pub struct GridDataRecord {
pub sample_type: u16,
pub data_representation: u16,
pub number_of_octets: u16,
pub data_values: Vec<u8>,
pub padding: u8,
pub sample_type: GriddedDataSampleType,
pub data_representation: GriddedDataDataRepresentation,
pub data: DataRepresentationType,
}

impl GridDataRecord {
#[must_use]
pub fn new(
sample_type: u16,
data_representation: u16,
number_of_octets: u16,
data_values: Vec<u8>,
padding: u8,
sample_type: GriddedDataSampleType,
data_representation: GriddedDataDataRepresentation,
data: DataRepresentationType,
) -> Self {
GridDataRecord {
sample_type,
data_representation,
number_of_octets,
data_values,
padding,
data,
}
}

pub fn serialize(&self, buf: &mut BytesMut) {
buf.put_u16(self.sample_type);
buf.put_u16(self.data_representation);
buf.put_u16(self.number_of_octets);
for i in 0..self.data_values.len() {
buf.put_u8(self.data_values[i]);
buf.put_u16(self.sample_type as u16);
buf.put_u16(self.data_representation as u16);
match &self.data {
DataRepresentationType::Type0 {
number_of_octets,
data_values,
padding,
} => {
buf.put_u16(*number_of_octets);
for i in data_values {
buf.put_u8(*i);
}
for p in padding {
buf.put_u8(*p);
}
}
DataRepresentationType::Type1 {
field_scale,
field_offset,
number_of_values,
data_values,
padding,
} => {
buf.put_f32(*field_scale);
buf.put_f32(*field_offset);
buf.put_u16(*number_of_values);
for i in data_values {
buf.put_u16(*i);
}
for p in padding {
buf.put_u8(*p);
}
}
DataRepresentationType::Type2 {
number_of_values,
padding,
data_values,
} => {
buf.put_u16(*number_of_values);
buf.put_u16(*padding);
for i in data_values {
buf.put_f32(*i);
}
}
}
buf.put_u8(self.padding);
}

pub fn decode(buf: &mut BytesMut) -> GridDataRecord {
let sample_type = buf.get_u16();
let data_representation = buf.get_u16();
let number_of_octets = buf.get_u16();
let mut data_values: Vec<u8> = vec![];
for _i in 0..number_of_octets {
data_values.push(buf.get_u8());
}
let padding = buf.get_u8();
let sample_type = GriddedDataSampleType::decode(buf);
let data_representation = GriddedDataDataRepresentation::decode(buf);
let data = match data_representation {
GriddedDataDataRepresentation::Type0 => {
let number_of_octets = buf.get_u16();
let mut data_values: Vec<u8> = vec![];
for _ in 0..number_of_octets {
data_values.push(buf.get_u8());
}
let mut padding: Vec<u8> = vec![];
while buf.has_remaining() {
padding.push(buf.get_u8());
}

DataRepresentationType::Type0 {
number_of_octets,
data_values,
padding,
}
}
GriddedDataDataRepresentation::Type1 => {
let field_scale = buf.get_f32();
let field_offset = buf.get_f32();
let number_of_values = buf.get_u16();
let mut data_values: Vec<u16> = vec![];
for _ in 0..number_of_values {
data_values.push(buf.get_u16());
}
let mut padding: Vec<u8> = vec![];
while buf.has_remaining() {
padding.push(buf.get_u8());
}

DataRepresentationType::Type1 {
field_scale,
field_offset,
number_of_values,
data_values,
padding,
}
}
GriddedDataDataRepresentation::Type2 => {
let number_of_values = buf.get_u16();
let padding = buf.get_u16();
let mut data_values: Vec<f32> = vec![];
for _ in 0..number_of_values {
data_values.push(buf.get_f32());
}

DataRepresentationType::Type2 {
number_of_values,
padding,
data_values,
}
}
};

GridDataRecord {
sample_type,
data_representation,
number_of_octets,
data_values,
padding,
data,
}
}
}
Loading

0 comments on commit 7ecd811

Please sign in to comment.