parsingrust.obj

How would I parse this section of an OBJ file?


I am trying to parse this

    f 3//2 8//2 4//2
    f 7//3 6//3 8//3
    f 2//4 8//4 6//4
    f 1//5 4//5 2//5
    f 5//6 2//6 6//6
    f 5//1 7//1 3//1
    f 3//2 7//2 8//2
    f 7//3 5//3 6//3
    f 2//4 4//4 8//4
    f 1//5 3//5 4//5
    f 5//6 1//6 2//6

in Rust, but I'm not sure how. I want it to be that it is split so that the numbers before the double slashes get put into the x,y,z components of a Vec4, and the number after gets put in the w component (i.e. f 3//2 8//2 4//2 -> Vec4::new(3,8,4,2);)


Solution

  • This should really be solved with regex. Something like f (\d+)//(\d+) (\d+)//(\d+) (\d+)//(\d+) should do the trick where capture groups 1, 3, 4 are 0, 1, 2 in your vector and 2, 3, 5 should be asserted to all be the same then go into the the last place.

    But parsing strings in rust is always fun, so here's my crack at it. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=52441e70033a3956f5914c5288202386