Added better error checking and tests for appropriate failures.

This commit is contained in:
Patrick Marsee 2025-01-20 13:50:07 -05:00
parent 600fd0c276
commit fbf1a9214b
5 changed files with 444 additions and 143 deletions

View file

@ -14,6 +14,12 @@ fn new_looking_at(direction: Vector3, up: Vector3, _positive_z_forward: bool) ->
Basis::from_cols(x, y, z)
}
/**
* A simple segment with one alignment sampler and one elevation sampler.
* This necessarily has exactly 2 endpoints and 1 sub-segment.
* Despite the sub-segment being unambiguous, it shall still fail if the incorrect index is used.
* This is also true of its float state.
*/
pub struct SimpleSegment<A: AlignmentSampler, E: ElevationSampler> {
alignment: A,
elevation: E,
@ -25,11 +31,12 @@ pub struct SimpleSegment<A: AlignmentSampler, E: ElevationSampler> {
// cache some stuff
//endpoint0: Option<Transform3D>,
//endpoint1: Option<Transform3D>,
//length: Option<f32>,
length: f32,
}
impl<A: AlignmentSampler, E: ElevationSampler> SimpleSegment<A, E> {
pub fn new(alignment: A, elevation: E) -> Self {
let length = elevation.length();
Self {
alignment,
elevation,
@ -37,27 +44,25 @@ impl<A: AlignmentSampler, E: ElevationSampler> SimpleSegment<A, E> {
upper_connection: Option::None,
//endpoint0: None,
//endpoint1: None,
//length: None,
length,
}
}
}
impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A, E> {
fn get_length(&self, _index: usize) -> Result<f32, rail_segment::Error> {
/*match self.length {
Some(length) => Ok(length),
None => {
let result = self.elevation.length();
self.length = Some(result);
Ok(result)
},
}*/
Ok(self.elevation.length())
fn get_length(&self, index: usize) -> Result<f32, rail_segment::Error> {
if index == 0 {
Ok(self.length)
} else {
Err(rail_segment::Error::IndexError(index))
}
}
fn sample(&self, _index: usize, offset: f32) -> Result<Vector3, rail_segment::Error> {
if offset < 0.0 || offset > self.get_length(1).unwrap() {
return Err(rail_segment::Error::OffsetError);
fn sample(&self, index: usize, offset: f32) -> Result<Vector3, rail_segment::Error> {
if index != 0 {
return Err(rail_segment::Error::IndexError(index));
} else if offset < 0.0 || offset > self.length {
return Err(rail_segment::Error::OffsetError(offset));
}
let top_down_t = self.elevation.to_top_down_normalized(offset / self.elevation.length());
let xz = self.alignment.sample_normalized(top_down_t);
@ -65,9 +70,11 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
Ok(Vector3::new(xz.x, y, xz.y))
}
fn sample_with_rotation(&self, _index: usize, offset: f32) -> Result<Transform3D, rail_segment::Error> {
if offset < 0.0 || offset > self.get_length(1).unwrap() {
return Err(rail_segment::Error::OffsetError);
fn sample_with_rotation(&self, index: usize, offset: f32) -> Result<Transform3D, rail_segment::Error> {
if index != 0 {
return Err(rail_segment::Error::IndexError(index));
} else if offset < 0.0 || offset > self.length {
return Err(rail_segment::Error::OffsetError(offset));
}
let top_down_t = self.elevation.to_top_down_normalized(offset / self.elevation.length());
let direction_xz = self.alignment.tangent_normalized(top_down_t);
@ -79,9 +86,11 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
Ok(Transform3D::new(basis, Vector3::new(xz.x, y, xz.y)))
}
fn sample_with_rotation_reverse(&self, _index: usize, offset: f32) -> Result<Transform3D, rail_segment::Error> {
if offset < 0.0 || offset > self.get_length(1).unwrap() {
return Err(rail_segment::Error::OffsetError);
fn sample_with_rotation_reverse(&self, index: usize, offset: f32) -> Result<Transform3D, rail_segment::Error> {
if index != 0 {
return Err(rail_segment::Error::IndexError(index));
} else if offset < 0.0 || offset > self.length {
return Err(rail_segment::Error::OffsetError(offset));
}
let top_down_t = self.elevation.to_top_down_normalized(offset / self.elevation.length());
let direction_zx = self.alignment.tangent_normalized(top_down_t);
@ -93,9 +102,11 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
Ok(Transform3D::new(basis, Vector3::new(xz.x, y, xz.y)))
}
fn sample_up_vector(&self, _index: usize, offset: f32) -> Result<Vector3, rail_segment::Error> {
if offset < 0.0 || offset > self.get_length(1).unwrap() {
return Err(rail_segment::Error::OffsetError);
fn sample_up_vector(&self, index: usize, offset: f32) -> Result<Vector3, rail_segment::Error> {
if index != 0 {
return Err(rail_segment::Error::IndexError(index));
} else if offset < 0.0 || offset > self.length {
return Err(rail_segment::Error::OffsetError(offset));
}
let top_down_t = self.elevation.to_top_down_normalized(offset / self.elevation.length());
let direction_xz = self.alignment.tangent_normalized(top_down_t);
@ -105,35 +116,19 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
Ok(direction.cross(normal).normalized())
}
fn segment_from_endpoint(&self, _endpoint: usize) -> Result<usize, rail_segment::Error> {
Ok(1)
fn segment_from_endpoint(&self, endpoint: usize) -> Result<usize, rail_segment::Error> {
if endpoint > 1 {
return Err(rail_segment::Error::EndpointError(endpoint));
} else {
Ok(0)
}
}
fn get_endpoint_transform(&self, endpoint: usize) -> Result<Transform3D, rail_segment::Error> {
match endpoint {
/*0 => {
match self.endpoint0 {
Some(result) => Ok(result),
None => {
let result = self.sample_with_rotation_reverse(0, 0.0).unwrap();
self.endpoint0 = Some(result);
Ok(result)
},
}
},
1 => {
match self.endpoint1 {
Some(result) => Ok(result),
None => {
let result = self.sample_with_rotation(0, self.get_length(1).unwrap()).unwrap();
self.endpoint1 = Some(result);
Ok(result)
},
}
},*/
0 => self.sample_with_rotation_reverse(0, 0.0),
1 => self.sample_with_rotation(0, self.get_length(1).unwrap()),
_ => Err(rail_segment::Error::EndpointError),
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
@ -141,7 +136,7 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
match endpoint {
0 => Ok(false),
1 => Ok(true),
_ => Err(rail_segment::Error::EndpointError),
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
@ -155,17 +150,37 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
if let Some(ret) = &self.lower_connection {
Ok(ret)
} else {
Err(rail_segment::Error::NeighborError)
Err(rail_segment::Error::ConnectionError(endpoint))
}
},
1 => {
if let Some(ret) = &self.upper_connection {
Ok(ret)
} else {
Err(rail_segment::Error::NeighborError)
Err(rail_segment::Error::ConnectionError(endpoint))
}
},
_ => Err(rail_segment::Error::EndpointError),
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
fn get_connection(&self, endpoint: usize) -> Result<RailConnector, rail_segment::Error> {
match endpoint {
0 => {
if let Some(ret) = self.lower_connection {
Ok(ret)
} else {
Err(rail_segment::Error::ConnectionError(endpoint))
}
},
1 => {
if let Some(ret) = self.upper_connection {
Ok(ret)
} else {
Err(rail_segment::Error::ConnectionError(endpoint))
}
},
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
@ -183,7 +198,7 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
None => Ok(false),
}
},
_ => Err(rail_segment::Error::EndpointError),
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
@ -201,7 +216,7 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
self.upper_connection = Option::Some(connector);
Ok(true)
},
_ => Err(rail_segment::Error::EndpointError),
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
@ -215,7 +230,7 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
self.upper_connection = Option::None;
Ok(true)
},
_ => Err(rail_segment::Error::EndpointError),
_ => Err(rail_segment::Error::EndpointError(endpoint)),
}
}
@ -228,8 +243,8 @@ impl<A: AlignmentSampler, E: ElevationSampler> RailSegment for SimpleSegment<A,
0.0
}
fn set_state(&mut self, _: f32) {
// This does nothing for simple segments.
fn set_state(&mut self, state: f32) -> Result<f32, rail_segment::Error> {
Err(rail_segment::Error::StateError(state))
}
}
@ -264,8 +279,14 @@ mod tests {
let alignment = TangentAlignmentSampler::new(Vector2::new(1.0, 1.0), Vector2::new(10.0, 13.0));
let elevation = TangentElevationSampler::new(1.0, 9.0, 15.0);
let test_segment = SimpleSegment::new(alignment, elevation);
assert_eq!(test_segment.get_length(1).unwrap(), 17.0,
"Segment length was incorrect.");
// test success
assert!(match test_segment.get_length(0) { Ok(17.0)=>true, _=>false, },
"Segment length was incorrect.");
// test failure
assert!(match test_segment.get_length(1) { Err(rail_segment::Error::IndexError(1))=>true, _=>false, },
"Got length of non-existant sub-segment.");
}
#[test]
@ -273,12 +294,22 @@ mod tests {
let alignment = TangentAlignmentSampler::new(Vector2::new(1.0, 1.0), Vector2::new(10.0, 13.0));
let elevation = TangentElevationSampler::new(1.0, 9.0, 15.0);
let test_segment = SimpleSegment::new(alignment, elevation);
assert_eq!(test_segment.sample(1, 0.0).unwrap(), Vector3::new(1.0, 1.0, 1.0),
// test successes
assert_eq!(test_segment.sample(0, 0.0).unwrap(), Vector3::new(1.0, 1.0, 1.0),
"Segment begins at the wrong location.");
assert_eq!(test_segment.sample(1, 17.0).unwrap(), Vector3::new(10.0, 9.0, 13.0),
assert_eq!(test_segment.sample(0, 17.0).unwrap(), Vector3::new(10.0, 9.0, 13.0),
"Segment ends at the wrong location.");
assert_eq!(test_segment.sample(1, 8.5).unwrap(), Vector3::new(5.5, 5.0, 7.0),
assert_eq!(test_segment.sample(0, 8.5).unwrap(), Vector3::new(5.5, 5.0, 7.0),
"Segment passes through the wrong location.");
// test failures
assert!(match test_segment.sample(1, 0.0) { Err(rail_segment::Error::IndexError(1))=>true, _=>false, },
"Sampled non-existant sub-segment.");
assert!(match test_segment.sample(0, -1.0) { Err(rail_segment::Error::OffsetError(-1.0))=>true, _=>false, },
"Sampled segment with negative offset.");
assert!(match test_segment.sample(0, 18.0) { Err(rail_segment::Error::OffsetError(18.0))=>true, _=>false, },
"Sampled segment with excess offset.");
}
#[test]
@ -289,8 +320,10 @@ mod tests {
let direction_forward = Vector3::new(-9.0 / 17.0, -8.0 / 17.0, -12.0 / 17.0);
let direction_right = Vector3::new(-0.8, 0.0, 0.6);
let direction_up = direction_forward.cross(direction_right);
let transform1 = test_segment.sample_with_rotation(1, 0.0).unwrap();
let transform1 = test_segment.sample_with_rotation(0, 0.0).unwrap();
let epsilon = 1e-6;
// test successes
assert_eq!(transform1.origin, Vector3::new(1.0, 1.0, 1.0),
"Segment begins at the wrong location.");
compare_vectors(transform1.basis.col_c(), direction_forward, epsilon,
@ -299,16 +332,16 @@ mod tests {
"Segment begins with the wrong right direction.");
compare_vectors(transform1.basis.col_b(), direction_up, epsilon,
"Segment begins with the wrong up direction.");
let transform2 = test_segment.sample_with_rotation(1, 17.0).unwrap();
let transform2 = test_segment.sample_with_rotation(0, 17.0).unwrap();
assert_eq!(transform2.origin, Vector3::new(10.0, 9.0, 13.0),
"Segment ends at the wrong location.");
compare_vectors(transform2.basis.col_c(), direction_forward, epsilon,
"Segment ends facing the wrong direction.");
compare_vectors(transform2.basis.col_a(), direction_right, epsilon,
"Segment ends with the wrong righ direction.");
"Segment ends with the wrong right direction.");
compare_vectors(transform2.basis.col_b(), direction_up, epsilon,
"Segment ends with the wrong up direction.");
let transform3 = test_segment.sample_with_rotation(1, 8.5).unwrap();
let transform3 = test_segment.sample_with_rotation(0, 8.5).unwrap();
assert_eq!(transform3.origin, Vector3::new(5.5, 5.0, 7.0),
"Segment passes through the wrong location.");
compare_vectors(transform3.basis.col_c(), direction_forward, epsilon,
@ -317,6 +350,14 @@ mod tests {
"Segment passes through with the wrong righ direction.");
compare_vectors(transform3.basis.col_b(), direction_up, epsilon,
"Segment passes through with the wrong up direction.");
// test failures
assert!(match test_segment.sample_with_rotation(1, 0.0) { Err(rail_segment::Error::IndexError(1))=>true, _=>false, },
"Sampled non-existant sub-segment.");
assert!(match test_segment.sample_with_rotation(0, -1.0) { Err(rail_segment::Error::OffsetError(-1.0))=>true, _=>false, },
"Sampled segment with negative offset.");
assert!(match test_segment.sample_with_rotation(0, 18.0) { Err(rail_segment::Error::OffsetError(18.0))=>true, _=>false, },
"Sampled segment with excess offset.");
}
#[test]
@ -327,8 +368,10 @@ mod tests {
let direction_forward = Vector3::new(9.0 / 17.0, 8.0 / 17.0, 12.0 / 17.0);
let direction_right = Vector3::new(0.8, 0.0, -0.6);
let direction_up = direction_forward.cross(direction_right);
let transform1 = test_segment.sample_with_rotation_reverse(1, 0.0).unwrap();
let transform1 = test_segment.sample_with_rotation_reverse(0, 0.0).unwrap();
let epsilon = 1e-6;
// test successes
assert_eq!(transform1.origin, Vector3::new(1.0, 1.0, 1.0),
"Segment begins at the wrong location.");
compare_vectors(transform1.basis.col_c(), direction_forward, epsilon,
@ -337,16 +380,16 @@ mod tests {
"Segment begins with the wrong right direction.");
compare_vectors(transform1.basis.col_b(), direction_up, epsilon,
"Segment begins with the wrong up direction.");
let transform2 = test_segment.sample_with_rotation_reverse(1, 17.0).unwrap();
let transform2 = test_segment.sample_with_rotation_reverse(0, 17.0).unwrap();
assert_eq!(transform2.origin, Vector3::new(10.0, 9.0, 13.0),
"Segment ends at the wrong location.");
compare_vectors(transform2.basis.col_c(), direction_forward, epsilon,
"Segment ends facing the wrong direction.");
compare_vectors(transform2.basis.col_a(), direction_right, epsilon,
"Segment ends with the wrong righ direction.");
"Segment ends with the wrong right direction.");
compare_vectors(transform2.basis.col_b(), direction_up, epsilon,
"Segment ends with the wrong up direction.");
let transform3 = test_segment.sample_with_rotation_reverse(1, 8.5).unwrap();
let transform3 = test_segment.sample_with_rotation_reverse(0, 8.5).unwrap();
assert_eq!(transform3.origin, Vector3::new(5.5, 5.0, 7.0),
"Segment passes through the wrong location.");
compare_vectors(transform3.basis.col_c(), direction_forward, epsilon,
@ -355,6 +398,14 @@ mod tests {
"Segment passes through with the wrong righ direction.");
compare_vectors(transform3.basis.col_b(), direction_up, epsilon,
"Segment passes through with the wrong up direction.");
// test failures
assert!(match test_segment.sample_with_rotation_reverse(1, 0.0) { Err(rail_segment::Error::IndexError(1))=>true, _=>false, },
"Sampled non-existant sub-segment.");
assert!(match test_segment.sample_with_rotation_reverse(0, -1.0) { Err(rail_segment::Error::OffsetError(-1.0))=>true, _=>false, },
"Sampled segment with negative offset.");
assert!(match test_segment.sample_with_rotation_reverse(0, 18.0) { Err(rail_segment::Error::OffsetError(18.0))=>true, _=>false, },
"Sampled segment with excess offset.");
}
#[test]
@ -365,15 +416,102 @@ mod tests {
let direction_forward = Vector3::new(-9.0 / 17.0, -8.0 / 17.0, -12.0 / 17.0);
let direction_right = Vector3::new(-0.8, 0.0, 0.6);
let direction_up = direction_forward.cross(direction_right);
let up1 = test_segment.sample_up_vector(1, 0.0).unwrap();
let up1 = test_segment.sample_up_vector(0, 0.0).unwrap();
let epsilon = 1e-6;
// test successes
compare_vectors(up1, direction_up, epsilon,
"Segment begins with the wrong up direction.");
let up2 = test_segment.sample_up_vector(1, 17.0).unwrap();
let up2 = test_segment.sample_up_vector(0, 17.0).unwrap();
compare_vectors(up2, direction_up, epsilon,
"Segment ends with the wrong up direction.");
let up3 = test_segment.sample_up_vector(1, 8.5).unwrap();
let up3 = test_segment.sample_up_vector(0, 8.5).unwrap();
compare_vectors(up3, direction_up, epsilon,
"Segment passes through with the wrong up direction.");
// test failures
assert!(match test_segment.sample_up_vector(1, 0.0) { Err(rail_segment::Error::IndexError(1))=>true, _=>false, },
"Sampled non-existant sub-segment.");
assert!(match test_segment.sample_up_vector(0, -1.0) { Err(rail_segment::Error::OffsetError(-1.0))=>true, _=>false, },
"Sampled segment with negative offset.");
assert!(match test_segment.sample_up_vector(0, 18.0) { Err(rail_segment::Error::OffsetError(18.0))=>true, _=>false, },
"Sampled segment with excess offset.");
}
#[test]
fn test_segment_from_endpoint() {
let alignment = TangentAlignmentSampler::new(Vector2::new(1.0, 1.0), Vector2::new(10.0, 13.0));
let elevation = TangentElevationSampler::new(1.0, 9.0, 15.0);
let test_segment = SimpleSegment::new(alignment, elevation);
// test successes
assert_eq!(test_segment.segment_from_endpoint(0).unwrap(), 0, "Endpoint 0 points to incorrect internal segment.");
assert_eq!(test_segment.segment_from_endpoint(1).unwrap(), 0, "Endpoint 1 points to incorrect internal segment.");
// test failures
assert!(match test_segment.segment_from_endpoint(2) { Err(rail_segment::Error::EndpointError(2))=>true, _=>false, },
"Endpoint 2 points to an internal segment.");
}
#[test]
fn test_get_endpoint_transform() {
let alignment = TangentAlignmentSampler::new(Vector2::new(1.0, 1.0), Vector2::new(10.0, 13.0));
let elevation = TangentElevationSampler::new(1.0, 9.0, 15.0);
let test_segment = SimpleSegment::new(alignment, elevation);
let direction_forward = Vector3::new(9.0 / 17.0, 8.0 / 17.0, 12.0 / 17.0);
let direction_right = Vector3::new(0.8, 0.0, -0.6);
let direction_up = direction_forward.cross(direction_right);
// test successes
let transform1 = test_segment.get_endpoint_transform(0).unwrap();
let epsilon = 1e-6;
assert_eq!(transform1.origin, Vector3::new(1.0, 1.0, 1.0),
"Segment begins at the wrong location.");
compare_vectors(transform1.basis.col_c(), direction_forward, epsilon,
"Segment begins facing the wrong direction.");
compare_vectors(transform1.basis.col_a(), direction_right, epsilon,
"Segment begins with the wrong right direction.");
compare_vectors(transform1.basis.col_b(), direction_up, epsilon,
"Segment begins with the wrong up direction.");
let transform2 = test_segment.sample_with_rotation(0, 17.0).unwrap();
assert_eq!(transform2.origin, Vector3::new(10.0, 9.0, 13.0),
"Segment ends at the wrong location.");
compare_vectors(transform2.basis.col_c(), -direction_forward, epsilon,
"Segment ends facing the wrong direction.");
compare_vectors(transform2.basis.col_a(), -direction_right, epsilon,
"Segment ends with the wrong right direction.");
compare_vectors(transform2.basis.col_b(), direction_up, epsilon,
"Segment ends with the wrong up direction.");
// test failures
assert!(match test_segment.get_endpoint_transform(2) { Err(rail_segment::Error::EndpointError(2))=>true, _=>false, },
"Endpoint 2 has a transform.");
}
#[test]
fn test_is_endpoint_upper() {
let alignment = TangentAlignmentSampler::new(Vector2::new(1.0, 1.0), Vector2::new(10.0, 13.0));
let elevation = TangentElevationSampler::new(1.0, 9.0, 15.0);
let test_segment = SimpleSegment::new(alignment, elevation);
// test successes
assert_eq!(test_segment.is_endpoint_upper(0).unwrap(), false, "Endpoint 0 is upper.");
assert_eq!(test_segment.is_endpoint_upper(1).unwrap(), true, "Endpoint 1 is lower.");
// test failures
assert!(match test_segment.is_endpoint_upper(2) { Err(rail_segment::Error::EndpointError(2))=>true, _=>false, },
"Endpoint 2 should not be upper or lower.");
}
#[test]
fn test_num_endpoints() {
// By far the most critical and intricate test.
let alignment = TangentAlignmentSampler::new(Vector2::new(1.0, 1.0), Vector2::new(10.0, 13.0));
let elevation = TangentElevationSampler::new(1.0, 9.0, 15.0);
let test_segment = SimpleSegment::new(alignment, elevation);
assert_eq!(test_segment.num_endpoints(), 2, "Incorrect number of endpoints.");
}
// For now, I'm not going to write tests for most of the connection methods since they're basically all getters and setters.
}