Skip to content

Commit aae85ec

Browse files
committed
Basic implementation of line types 2 to 5
1 parent bfd2309 commit aae85ec

File tree

1 file changed

+143
-15
lines changed

1 file changed

+143
-15
lines changed

src/lib.rs

Lines changed: 143 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ do_parse!(
199199
sp >>
200200
pos: read_vec3 >>
201201
sp >>
202-
row0: read_vec3 >>
202+
row0: read_vec3 >>
203203
sp >>
204-
row1: read_vec3 >>
204+
row1: read_vec3 >>
205205
sp >>
206206
row2: read_vec3 >>
207207
sp >>
@@ -220,32 +220,71 @@ do_parse!(
220220

221221
named!(line_cmd<CommandType>,
222222
do_parse!(
223-
content: take_not_cr_or_lf >> (
224-
CommandType::Command(Cmd{ id: 2, content: std::str::from_utf8(content).unwrap().to_string() })
223+
color: color_id >>
224+
sp >>
225+
v1: read_vec3 >>
226+
sp >>
227+
v2: read_vec3 >> (
228+
CommandType::Line(LineCmd{
229+
color: color,
230+
vertices: [v1, v2]
231+
})
225232
)
226233
)
227234
);
228235

229236
named!(tri_cmd<CommandType>,
230237
do_parse!(
231-
content: take_not_cr_or_lf >> (
232-
CommandType::Command(Cmd{ id: 3, content: std::str::from_utf8(content).unwrap().to_string() })
238+
color: color_id >>
239+
sp >>
240+
v1: read_vec3 >>
241+
sp >>
242+
v2: read_vec3 >>
243+
sp >>
244+
v3: read_vec3 >> (
245+
CommandType::Triangle(TriangleCmd{
246+
color: color,
247+
vertices: [v1, v2, v3]
248+
})
233249
)
234250
)
235251
);
236252

237253
named!(quad_cmd<CommandType>,
238254
do_parse!(
239-
content: take_not_cr_or_lf >> (
240-
CommandType::Command(Cmd{ id: 4, content: std::str::from_utf8(content).unwrap().to_string() })
255+
color: color_id >>
256+
sp >>
257+
v1: read_vec3 >>
258+
sp >>
259+
v2: read_vec3 >>
260+
sp >>
261+
v3: read_vec3 >>
262+
sp >>
263+
v4: read_vec3 >> (
264+
CommandType::Quad(QuadCmd{
265+
color: color,
266+
vertices: [v1, v2, v3, v4]
267+
})
241268
)
242269
)
243270
);
244271

245272
named!(opt_line_cmd<CommandType>,
246273
do_parse!(
247-
content: take_not_cr_or_lf >> (
248-
CommandType::Command(Cmd{ id: 5, content: std::str::from_utf8(content).unwrap().to_string() })
274+
color: color_id >>
275+
sp >>
276+
v1: read_vec3 >>
277+
sp >>
278+
v2: read_vec3 >>
279+
sp >>
280+
v3: read_vec3 >>
281+
sp >>
282+
v4: read_vec3 >> (
283+
CommandType::OptLine(OptLineCmd{
284+
color: color,
285+
vertices: [v1, v2],
286+
control_points: [v3, v4]
287+
})
249288
)
250289
)
251290
);
@@ -472,6 +511,53 @@ pub struct SubFileRefCmd {
472511
pub file: SubFileRef
473512
}
474513

514+
/// Line Type 2 LDraw command to draw a segment between 2 vertices.
515+
///
516+
/// [Specification](https://www.ldraw.org/article/218.html#lt2)
517+
#[derive(Debug, PartialEq)]
518+
pub struct LineCmd {
519+
/// Color code of the primitive.
520+
pub color: i32,
521+
/// Vertices of the segment.
522+
pub vertices: [Vec3; 2]
523+
}
524+
525+
/// Line Type 3 LDraw command to draw a triangle between 3 vertices.
526+
///
527+
/// [Specification](https://www.ldraw.org/article/218.html#lt3)
528+
#[derive(Debug, PartialEq)]
529+
pub struct TriangleCmd {
530+
/// Color code of the primitive.
531+
pub color: i32,
532+
/// Vertices of the triangle.
533+
pub vertices: [Vec3; 3]
534+
}
535+
536+
/// Line Type 4 LDraw command to draw a quad between 4 vertices.
537+
///
538+
/// [Specification](https://www.ldraw.org/article/218.html#lt4)
539+
#[derive(Debug, PartialEq)]
540+
pub struct QuadCmd {
541+
/// Color code of the primitive.
542+
pub color: i32,
543+
/// Vertices of the quad.
544+
pub vertices: [Vec3; 4]
545+
}
546+
547+
/// Line Type 5 LDraw command to draw an optional segment between two vertices,
548+
/// aided by 2 control points.
549+
///
550+
/// [Specification](https://www.ldraw.org/article/218.html#lt5)
551+
#[derive(Debug, PartialEq)]
552+
pub struct OptLineCmd {
553+
/// Color code of the primitive.
554+
pub color: i32,
555+
/// Vertices of the segment.
556+
pub vertices: [Vec3; 2],
557+
/// Control points of the segment.
558+
pub control_points: [Vec3; 2]
559+
}
560+
475561
/// Types of commands contained in a LDraw file.
476562
#[derive(Debug, PartialEq)]
477563
pub enum CommandType {
@@ -480,13 +566,13 @@ pub enum CommandType {
480566
/// [Line Type 1](https://www.ldraw.org/article/218.html#lt1) sub-file reference.
481567
SubFileRef(SubFileRefCmd),
482568
/// [Line Type 2](https://www.ldraw.org/article/218.html#lt2) segment.
483-
Line(),
569+
Line(LineCmd),
484570
/// [Line Type 3](https://www.ldraw.org/article/218.html#lt3) triangle.
485-
Triangle(),
571+
Triangle(TriangleCmd),
486572
/// [Line Type 4](https://www.ldraw.org/article/218.html#lt4) quadrilateral.
487-
Quad(),
573+
Quad(QuadCmd),
488574
/// [Line Type 5](https://www.ldraw.org/article/218.html#lt5) optional line.
489-
OptLine()
575+
OptLine(OptLineCmd)
490576
}
491577

492578
/// Resolver trait for file references.
@@ -610,11 +696,53 @@ mod tests {
610696
}
611697

612698
#[test]
613-
fn test_read_line_cmd() {
699+
fn test_read_cmd() {
614700
let res = CommandType::Command(Cmd{ id: 0, content: "this doesn't matter".to_string() });
615701
assert_eq!(read_line(b"0 this doesn't matter"), Ok((&b""[..], res)));
616702
}
617703

704+
#[test]
705+
fn test_read_line_cmd() {
706+
let res = CommandType::Line(LineCmd{ color: 16, vertices: [
707+
Vec3{ x: 1.0, y: 1.0, z: 0.0 },
708+
Vec3{ x: 0.9239, y: 1.0, z: 0.3827 }
709+
] });
710+
assert_eq!(read_line(b"2 16 1 1 0 0.9239 1 0.3827"), Ok((&b""[..], res)));
711+
}
712+
713+
#[test]
714+
fn test_read_tri_cmd() {
715+
let res = CommandType::Triangle(TriangleCmd{ color: 16, vertices: [
716+
Vec3{ x: 1.0, y: 1.0, z: 0.0 },
717+
Vec3{ x: 0.9239, y: 1.0, z: 0.3827 },
718+
Vec3{ x: 0.9239, y: 0.0, z: 0.3827 }
719+
] });
720+
assert_eq!(read_line(b"3 16 1 1 0 0.9239 1 0.3827 0.9239 0 0.3827"), Ok((&b""[..], res)));
721+
}
722+
723+
#[test]
724+
fn test_read_quad_cmd() {
725+
let res = CommandType::Quad(QuadCmd{ color: 16, vertices: [
726+
Vec3{ x: 1.0, y: 1.0, z: 0.0 },
727+
Vec3{ x: 0.9239, y: 1.0, z: 0.3827 },
728+
Vec3{ x: 0.9239, y: 0.0, z: 0.3827 },
729+
Vec3{ x: 1.0, y: 0.0, z: 0.0 }
730+
] });
731+
assert_eq!(read_line(b"4 16 1 1 0 0.9239 1 0.3827 0.9239 0 0.3827 1 0 0"), Ok((&b""[..], res)));
732+
}
733+
734+
#[test]
735+
fn test_read_opt_line_cmd() {
736+
let res = CommandType::OptLine(OptLineCmd{ color: 16, vertices: [
737+
Vec3{ x: 1.0, y: 1.0, z: 0.0 },
738+
Vec3{ x: 0.9239, y: 1.0, z: 0.3827 }
739+
], control_points: [
740+
Vec3{ x: 0.9239, y: 0.0, z: 0.3827 },
741+
Vec3{ x: 1.0, y: 0.0, z: 0.0 }
742+
] });
743+
assert_eq!(read_line(b"5 16 1 1 0 0.9239 1 0.3827 0.9239 0 0.3827 1 0 0"), Ok((&b""[..], res)));
744+
}
745+
618746
#[test]
619747
fn test_read_line_subfileref() {
620748
let res = CommandType::SubFileRef(SubFileRefCmd{

0 commit comments

Comments
 (0)