@@ -199,9 +199,9 @@ do_parse!(
199
199
sp >>
200
200
pos: read_vec3 >>
201
201
sp >>
202
- row0: read_vec3 >>
202
+ row0: read_vec3 >>
203
203
sp >>
204
- row1: read_vec3 >>
204
+ row1: read_vec3 >>
205
205
sp >>
206
206
row2: read_vec3 >>
207
207
sp >>
@@ -220,32 +220,71 @@ do_parse!(
220
220
221
221
named ! ( line_cmd<CommandType >,
222
222
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
+ } )
225
232
)
226
233
)
227
234
) ;
228
235
229
236
named ! ( tri_cmd<CommandType >,
230
237
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
+ } )
233
249
)
234
250
)
235
251
) ;
236
252
237
253
named ! ( quad_cmd<CommandType >,
238
254
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
+ } )
241
268
)
242
269
)
243
270
) ;
244
271
245
272
named ! ( opt_line_cmd<CommandType >,
246
273
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
+ } )
249
288
)
250
289
)
251
290
) ;
@@ -472,6 +511,53 @@ pub struct SubFileRefCmd {
472
511
pub file : SubFileRef
473
512
}
474
513
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
+
475
561
/// Types of commands contained in a LDraw file.
476
562
#[ derive( Debug , PartialEq ) ]
477
563
pub enum CommandType {
@@ -480,13 +566,13 @@ pub enum CommandType {
480
566
/// [Line Type 1](https://www.ldraw.org/article/218.html#lt1) sub-file reference.
481
567
SubFileRef ( SubFileRefCmd ) ,
482
568
/// [Line Type 2](https://www.ldraw.org/article/218.html#lt2) segment.
483
- Line ( ) ,
569
+ Line ( LineCmd ) ,
484
570
/// [Line Type 3](https://www.ldraw.org/article/218.html#lt3) triangle.
485
- Triangle ( ) ,
571
+ Triangle ( TriangleCmd ) ,
486
572
/// [Line Type 4](https://www.ldraw.org/article/218.html#lt4) quadrilateral.
487
- Quad ( ) ,
573
+ Quad ( QuadCmd ) ,
488
574
/// [Line Type 5](https://www.ldraw.org/article/218.html#lt5) optional line.
489
- OptLine ( )
575
+ OptLine ( OptLineCmd )
490
576
}
491
577
492
578
/// Resolver trait for file references.
@@ -610,11 +696,53 @@ mod tests {
610
696
}
611
697
612
698
#[ test]
613
- fn test_read_line_cmd ( ) {
699
+ fn test_read_cmd ( ) {
614
700
let res = CommandType :: Command ( Cmd { id : 0 , content : "this doesn't matter" . to_string ( ) } ) ;
615
701
assert_eq ! ( read_line( b"0 this doesn't matter" ) , Ok ( ( & b"" [ ..] , res) ) ) ;
616
702
}
617
703
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
+
618
746
#[ test]
619
747
fn test_read_line_subfileref ( ) {
620
748
let res = CommandType :: SubFileRef ( SubFileRefCmd {
0 commit comments