Skip to content

Commit

Permalink
added analog scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
cvhammond committed Jul 10, 2023
1 parent 25ddf20 commit d1de4a7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 157 deletions.
2 changes: 1 addition & 1 deletion src/c3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl C3d {
//self.data.num_frames = end_field - start_field + 1;
}
}
self.data.parse(&self.bytes.data, &self.processor)?;
self.data.parse(&self.bytes.data, &self.parameters, &self.processor)?;
Ok(self)
}
}
Expand Down
69 changes: 58 additions & 11 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use std::ops::{MulAssign, SubAssign, DivAssign};

use crate::processor::Processor;
use crate::C3dParseError;
use crate::{
parameters::{
AnalogOffset::{Signed, Unsigned},
Parameters,
},
C3dParseError,
};
use ndarray::{Array, Array2, Array3};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -85,10 +93,10 @@ impl Data {
self.num_frames = (self.last_frame as usize - self.first_frame as usize) + 1;
if self.num_frames != self.point_frames {
self.num_frames = self.point_frames;
// return Err(C3dParseError::NumFramesMismatch(
// self.point_frames,
// self.num_frames,
// ));
// return Err(C3dParseError::NumFramesMismatch(
// self.point_frames,
// self.num_frames,
// ));
}
if self.trial_end_frame - self.trial_start_frame + 1 != self.num_frames {
self.num_frames = self.trial_end_frame - self.trial_start_frame + 1;
Expand Down Expand Up @@ -145,7 +153,11 @@ impl Data {
Ok(self)
}

fn parse_points(&mut self, data_bytes: &[u8], processor: &Processor) -> Result<&mut Self, C3dParseError> {
fn parse_points(
&mut self,
data_bytes: &[u8],
processor: &Processor,
) -> Result<&mut Self, C3dParseError> {
let mut point_data: Array3<f32> =
Array::zeros((self.num_frames as usize, self.points_per_frame as usize, 3));
let mut cameras: Array3<bool> = Array::from_elem(
Expand Down Expand Up @@ -203,10 +215,20 @@ impl Data {
Ok(self)
}

fn parse_analog(&mut self, data_bytes: &Vec<u8>, processor: &Processor) -> Result<&mut Self, C3dParseError> {
fn parse_analog(
&mut self,
data_bytes: &Vec<u8>,
parameters: &Parameters,
processor: &Processor,
) -> Result<&mut Self, C3dParseError> {
let analog_samples_per_channel_per_frame = if self.analog_channels > 0 {
(self.analog_samples_per_frame / self.analog_channels) as usize
} else {
0
};
let mut analog_data: Array2<f32> = Array::zeros((
self.num_frames as usize,
self.analog_samples_per_frame as usize,
self.num_frames * self.analog_channels as usize,
analog_samples_per_channel_per_frame,
));
let mut analog_iter = analog_data.iter_mut();

Expand Down Expand Up @@ -249,14 +271,39 @@ impl Data {
}
}
}
let offset_len = match &parameters.required_parameters.analog.offset {
Signed(offset) => offset.len(),
Unsigned(offset) => offset.len(),
};
if offset_len == parameters.required_parameters.analog.scale.len()
&& offset_len == analog_data.shape()[1]
{
analog_data
.columns_mut()
.into_iter()
.enumerate()
.for_each(|(i, mut column)| {
match &parameters.required_parameters.analog.offset {
Signed(offset) => column.sub_assign(offset[i] as f32),
Unsigned(offset) => column.sub_assign(offset[i] as f32),
}
column.mul_assign(parameters.required_parameters.analog.scale[i]);
});
analog_data.div_assign(parameters.required_parameters.analog.gen_scale);
}
self.analog = analog_data;
Ok(self)
}

pub fn parse(&mut self, data_bytes: &Vec<u8>, processor: &Processor) -> Result<(), C3dParseError> {
pub fn parse(
&mut self,
data_bytes: &Vec<u8>,
parameters: &Parameters,
processor: &Processor,
) -> Result<(), C3dParseError> {
self.calc_num_frames(data_bytes)?
.parse_points(data_bytes, processor)?
.parse_analog(data_bytes, processor)?;
.parse_analog(data_bytes, parameters, processor)?;
Ok(())
}
}
Expand Down
146 changes: 3 additions & 143 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,147 +1,7 @@
use std::collections::HashSet;
use c3dio::parameters::AnalogOffset::{Signed, Unsigned};
use std::ops::{MulAssign, SubAssign};

use c3dio::data::DataFormat;
use c3dio::data::{parse_point_data_float, parse_point_data_int};
use c3dio::C3d;
fn main() {
let dec_int = C3d::load(
"tests/c3d.org-sample-files/Sample36/18124framesf.c3d",
)
.unwrap();
// step_through_data_bytes(pi, pr);
}

fn step_through_data_bytes(c3d1: C3d, c3d2: C3d) {
// let bytes_per_frame1 = c3d1.data.point_bytes_per_frame + c3d1.data.analog_bytes_per_frame;
// let bytes_per_point1 = c3d1.data.point_bytes_per_frame / c3d1.data.points_per_frame as usize;
// let bytes_per_frame2 = c3d2.data.point_bytes_per_frame + c3d2.data.analog_bytes_per_frame;
// let bytes_per_point2 = c3d2.data.point_bytes_per_frame / c3d2.data.points_per_frame as usize;
// let mut counter = 0;
// let mut top_byte_values: HashSet<u8> = HashSet::new();
// let mut bottom_byte_values: HashSet<u8> = HashSet::new();
// //for i in 0..c3d1.data.num_frames {
// for i in 0..1 {
// let start1 = i * bytes_per_frame1;
// let end1 = start1 + bytes_per_frame1;
// let start2 = i * bytes_per_frame2;
// let end2 = start2 + bytes_per_frame2;
// let frame1 = &c3d1.data_bytes[start1..end1];
// let frame2 = &c3d2.data_bytes[start2..end2];
// //for j in 0..c3d1.data.points_per_frame as usize {
// for j in 0..5 {
// let start1 = j * bytes_per_point1;
// let end1 = start1 + bytes_per_point1;
// let start2 = j * bytes_per_point2;
// let end2 = start2 + bytes_per_point2;
// let point1 = &frame1[start1..end1];
// let point2 = &frame2[start2..end2];
// let (mut x1, mut y1, mut z1, _, _) = match c3d1.data.format {
// DataFormat::Float => parse_point_data_float(point1, &c3d1.processor),
// DataFormat::Integer => parse_point_data_int(point1, &c3d1.processor),
// DataFormat::Unknown => panic!("Unknown data format"),
// };
// let (mut x2, mut y2, mut z2, _, _) = match c3d2.data.format {
// DataFormat::Float => parse_point_data_float(point2, &c3d2.processor),
// DataFormat::Integer => parse_point_data_int(point2, &c3d2.processor),
// DataFormat::Unknown => panic!("Unknown data format"),
// };
// if c3d1.data.format == DataFormat::Integer {
// x1 *= c3d1.data.scale_factor;
// y1 *= c3d1.data.scale_factor;
// z1 *= c3d1.data.scale_factor;
// }
// if c3d2.data.format == DataFormat::Integer {
// x2 *= c3d2.data.scale_factor;
// y2 *= c3d2.data.scale_factor;
// z2 *= c3d2.data.scale_factor;
// }
// println!("Frame: {}, Point: {}", i, j);
// println!("x1: {}, x2: {}", x1, x2);
// println!("y1: {}, y2: {}", y1, y2);
// println!("z1: {}, z2: {}", z1, z2);
// if x1 != x2 {
// dbg!("Point mismatch");
// dbg!("point1");
// print_bits(point1[0]);
// print_bits(point1[1]);
// dbg!("point2");
// print_bits(point2[0]);
// print_bits(point2[1]);
// dbg!(x1);
// dbg!(x2);
// top_byte_values.insert(point1[0]);
// bottom_byte_values.insert(point1[1]);
// counter += 1;
// }
// if y1 != y2 {
// dbg!("Point mismatch");
// dbg!("point1");
// print_bits(point1[2]);
// print_bits(point1[3]);
// dbg!("point2");
// print_bits(point2[2]);
// print_bits(point2[3]);
// dbg!(y1);
// dbg!(y2);
// counter += 1;
// top_byte_values.insert(point1[2]);
// bottom_byte_values.insert(point1[3]);
// }
// if z1 != z2 {
// dbg!("Point mismatch");
// dbg!("point1");
// print_bits(point1[4]);
// print_bits(point1[5]);
// dbg!("point2");
// print_bits(point2[4]);
// print_bits(point2[5]);
// dbg!(z1);
// dbg!(z2);
// counter += 1;
// top_byte_values.insert(point1[4]);
// bottom_byte_values.insert(point1[5]);
// }
// }
// }
// dbg!(counter);
// dbg!(c3d1.data.scale_factor);
// dbg!(c3d2.data.scale_factor);
// c3d1 == c3d2;
// // top_byte_values.iter().for_each(|x| print_bits(*x));
// dbg!(bottom_byte_values);
}

fn print_bits(byte: u8) {
println!("{:#010b}", byte);
}

fn print_stuff(pc_real: C3d, pc_int: C3d) {
dbg!(pc_real == pc_int);

dbg!(pc_real.data.points.len());
dbg!(pc_int.data.points.len());
dbg!((&pc_real.data.points - &pc_int.data.points).sum());
let diff = &pc_real.data.points - &pc_int.data.points;
let mut counter = 0;
let mut indices = Vec::new();
diff.iter().for_each(|x| {
if *x != 0.0 {
dbg!(x);
indices.push(counter);
}
counter += 1;
});
pc_real.data.points.iter().enumerate().for_each(|(i, x)| {
if indices.contains(&i) {
dbg!(x);
}
});
pc_int.data.points.iter().enumerate().for_each(|(i, x)| {
if indices.contains(&i) {
dbg!(x / pc_int.data.scale_factor);
}
});
dbg!(pc_real.data.scale_factor);
dbg!(pc_int.data.scale_factor);
dbg!(pc_real == pc_int);
let dec_int = C3d::load("tests/c3d.org-sample-files/Sample19/sample19.c3d").unwrap();
}
8 changes: 6 additions & 2 deletions src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ impl Parameters {
pub fn get_data(&self, group: &str, parameter: &str) -> Option<ParameterData> {
get(&self.raw_parameters, group, parameter)
}


}

impl PartialEq for Parameters {
Expand All @@ -74,7 +76,9 @@ impl AnalogOffset {
let offset = get_signed_integer_vec(raw_parameters, "ANALOG", "OFFSET");
let offset = match offset {
Some(offset) => offset,
None => return Ok(AnalogOffset::Signed(Vec::new())),
None => {
return Ok(AnalogOffset::Signed(Vec::new()))
},
};
match format {
AnalogFormat::Signed => Ok(AnalogOffset::Signed(offset)),
Expand Down Expand Up @@ -256,7 +260,7 @@ impl RequiredAnalogParameters {
used: 0,
labels: Vec::new(),
descriptions: Vec::new(),
gen_scale: 0.0,
gen_scale: 1.0,
offset: AnalogOffset::Signed(Vec::new()),
units: Vec::new(),
scale: Vec::new(),
Expand Down

0 comments on commit d1de4a7

Please sign in to comment.