예제 #1
0
        public void Vertex_normal_data_should_be_correctly_associated_with_face_data_from_an_OBJ_file()
        {
            const string contents = @"
v 0 1 0
v -1 0 0
v 1 0 0

vn -1 0 0
vn 1 0 0
vn 0 1 0

f 1//3 2//1 3//2
f 1/0/3 2/102/1 3/14/2";

            var objFile = ObjFile.Parse(contents);
            var g       = objFile.DefaultGroup;
            var t1      = (SmoothTriangle)g.Children[0];
            var t2      = (SmoothTriangle)g.Children[1];

            t1.P1.Should().Be(objFile.Vertices[0]);
            t1.P2.Should().Be(objFile.Vertices[1]);
            t1.P3.Should().Be(objFile.Vertices[2]);

            t1.N1.Should().Be(objFile.Normals[2]);
            t1.N2.Should().Be(objFile.Normals[0]);
            t1.N3.Should().Be(objFile.Normals[1]);

            t1.Should().BeEquivalentTo(t2);
        }
예제 #2
0
        public void Parse_should_process_and_triangulate_polygonal_data_from_the_given_input()
        {
            const string contents = @"
v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0
v 0 2 0

f 1 2 3 4 5";

            var   objFile = ObjFile.Parse(contents);
            Group g       = objFile.DefaultGroup;
            var   t1      = (Triangle)g.Children[0];
            var   t2      = (Triangle)g.Children[1];
            var   t3      = (Triangle)g.Children[2];

            t1.P1.Should().Be(objFile.Vertices[0]);
            t1.P2.Should().Be(objFile.Vertices[1]);
            t1.P3.Should().Be(objFile.Vertices[2]);

            t2.P1.Should().Be(objFile.Vertices[0]);
            t2.P2.Should().Be(objFile.Vertices[2]);
            t2.P3.Should().Be(objFile.Vertices[3]);

            t3.P1.Should().Be(objFile.Vertices[0]);
            t3.P2.Should().Be(objFile.Vertices[3]);
            t3.P3.Should().Be(objFile.Vertices[4]);
        }
예제 #3
0
        public void CreatesMultipleGroups()
        {
            var file = @"
v -1 1 0 
v -1 0 0 
v 1 0 0 
v 1 1 0 
v 0 2 0

g FirstGroup
f 1 2 3
g SecondGroup
f 1 3 4
";
            var data = ObjFile.Parse(file);
            var g1   = data.Groups[0];
            var g2   = data.Groups[1];
            var t1   = (Triangle)g1.Children[0];
            var t2   = (Triangle)g2.Children[0];

            t1.P1.Should().Be(data.Vertices[1]);
            t1.P2.Should().Be(data.Vertices[2]);
            t1.P3.Should().Be(data.Vertices[3]);
            t2.P1.Should().Be(data.Vertices[1]);
            t2.P2.Should().Be(data.Vertices[3]);
            t2.P3.Should().Be(data.Vertices[4]);
        }
예제 #4
0
        public void ParsesToTrianglesWithNormals()
        {
            var file = @"
v 0 1 0
v -1 0 0
v 1 0 0

vn -1 0 0
vn 1 0 0
vn 0 1 0

f 1//3 2//1 3//2
f 1/0/3 2/102/1 3/14/2
";
            var data = ObjFile.Parse(file);
            var g    = data.DefaultGroup;
            var t1   = (SmoothTriangle)g.Children[0];
            var t2   = (SmoothTriangle)g.Children[1];

            t1.P1.Should().Be(data.Vertices[1]);
            t1.P2.Should().Be(data.Vertices[2]);
            t1.P3.Should().Be(data.Vertices[3]);
            t1.N1.Should().Be(data.Normals[3]);
            t1.N2.Should().Be(data.Normals[1]);
            t1.N3.Should().Be(data.Normals[2]);
        }
예제 #5
0
        public void TriangulatesPolygons()
        {
            var file = @"
v -1 1 0 
v -1 0 0 
v 1 0 0 
v 1 1 0 
v 0 2 0

f 1 2 3 4 5
";
            var data = ObjFile.Parse(file);
            var g    = data.DefaultGroup;
            var t1   = (Triangle)g.Children[0];
            var t2   = (Triangle)g.Children[1];
            var t3   = (Triangle)g.Children[2];

            t1.P1.Should().Be(data.Vertices[1]);
            t1.P2.Should().Be(data.Vertices[2]);
            t1.P3.Should().Be(data.Vertices[3]);
            t2.P1.Should().Be(data.Vertices[1]);
            t2.P2.Should().Be(data.Vertices[3]);
            t2.P3.Should().Be(data.Vertices[4]);
            t3.P1.Should().Be(data.Vertices[1]);
            t3.P2.Should().Be(data.Vertices[4]);
            t3.P3.Should().Be(data.Vertices[5]);
        }
예제 #6
0
        public void IgnoreUnrecognizedLines()
        {
            var gibberish = @"
There was a young lady named Bright
who traveled much faster than light. 
She set out one day in a relative way,
and came back the previous night.";
            var data      = ObjFile.Parse(gibberish);

            data.VertexCount().Should().Be(0);
        }
예제 #7
0
        public void Parse_should_silently_ignore_any_unrecognized_statements()
        {
            const string gibberish = @"
There was a young lady named Bright
who traveled much faster than light.
She set out one day
in a relative way,
and came back the previous night.";

            var objFile = ObjFile.Parse(gibberish);

            objFile.IgnoredLineCount.Should().Be(5);
        }
예제 #8
0
        public void Vertex_normal_data_should_be_correctly_imported_from_an_OBJ_file()
        {
            const string contents = @"
vn 0 0 1
vn 0.707 0 -0.707
vn 1 2 3";

            var objFile = ObjFile.Parse(contents);

            objFile.Normals.Should()
            .HaveCount(3)
            .And.ContainInOrder(new Vector(0, 0, 1), new Vector(0.707, 0, -0.707), new Vector(1, 2, 3));
        }
예제 #9
0
        public void ParsesNormalRecords()
        {
            var file = @"
vn 0 0 1
vn 0.707 0 -0.707
vn 1 2 3 
";
            var data = ObjFile.Parse(file);

            data.Normals[1].Should().Be(new Normal(0, 0, 1));
            data.Normals[2].Should().Be(new Normal(0.707f, 0, -0.707f));
            data.Normals[3].Should().Be(new Normal(1, 2, 3));
        }
예제 #10
0
        public void ParsesVertexRecords()
        {
            var file = @"
v -1 1 0
v -1.0000 0.5000 0.0000
v 1 0 0
v 1 1 0 
";
            var data = ObjFile.Parse(file);

            data.Vertices[1].Should().Be(new Point(-1, 1, 0));
            data.Vertices[2].Should().Be(new Point(-1, 0.5f, 0));
            data.Vertices[3].Should().Be(new Point(1, 0, 0));
            data.Vertices[4].Should().Be(new Point(1, 1, 0));
        }
예제 #11
0
        public void Parse_should_process_vertex_data_from_the_given_input()
        {
            const string contents = @"
v -1 1 0
v -1.0000 0.5000 0.0000
v 1 0 0
v 1 1 0";

            var objFile = ObjFile.Parse(contents);

            objFile.Vertices.Should()
            .HaveCount(4)
            .And.ContainInOrder(
                new Point(-1, 1, 0),
                new Point(-1, 0.5, 0),
                new Point(1, 0, 0),
                new Point(1, 1, 0));
        }
예제 #12
0
        public void Parse_should_recognize_a_group_statement_and_add_subsequent_triangles_to_the_named_group()
        {
            const string contents = @"
v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0

g FirstGroup
f 1 2 3
g SecondGroup
f 1 3 4";

            using var stream = new MemoryStream();
            using var writer = new StreamWriter(stream, Encoding.ASCII, leaveOpen: true)
                  {
                      AutoFlush = true
                  };
            writer.Write(contents);
            stream.Position = 0;

            var   objFile = ObjFile.Parse(stream);
            Group group1  = objFile.FindGroupByName("FirstGroup") ?? throw new InvalidOperationException();
            Group group2  = objFile.FindGroupByName("SecondGroup") ?? throw new InvalidOperationException();

            var t1 = (Triangle)group1.Children[0];
            var t2 = (Triangle)group2.Children[0];

            t1.P1.Should().Be(objFile.Vertices[0]);
            t1.P2.Should().Be(objFile.Vertices[1]);
            t1.P3.Should().Be(objFile.Vertices[2]);

            t2.P1.Should().Be(objFile.Vertices[0]);
            t2.P2.Should().Be(objFile.Vertices[2]);
            t2.P3.Should().Be(objFile.Vertices[3]);
        }
예제 #13
0
        public void ParsesToTriangles()
        {
            var file = @"
v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0

f 1 2 3
f 1 3 4
";
            var data = ObjFile.Parse(file);
            var g    = data.DefaultGroup;
            var t1   = (Triangle)g.Children[0];
            var t2   = (Triangle)g.Children[1];

            t1.P1.Should().Be(data.Vertices[1]);
            t1.P2.Should().Be(data.Vertices[2]);
            t1.P3.Should().Be(data.Vertices[3]);
            t2.P1.Should().Be(data.Vertices[1]);
            t2.P1.Should().Be(data.Vertices[1]);
            t2.P2.Should().Be(data.Vertices[3]);
            t2.P3.Should().Be(data.Vertices[4]);
        }