public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    geometry Cylinder {
        bottom TRUE
        height 20.0
        radius 45.2
        side TRUE
        top FALSE
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            var cylinder = (scene.Root.Children[0] as ShapeNode).Geometry as CylinderNode;

            Assert.AreEqual(true, cylinder.Bottom.Value);
            Assert.AreEqual(20f, cylinder.Height.Value);
            Assert.AreEqual(45.2f, cylinder.Radius.Value);
            Assert.AreEqual(true, cylinder.Side.Value);
            Assert.AreEqual(false, cylinder.Top.Value);
        }
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0.1 0.05 0.03
        }
        texture NULL
        textureTransform TextureTransform {
        }
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            AssertExt.AreEqual(new ShapeNode {
                Appearance = new AppearanceNode {
                    Material = new MaterialNode {
                        DiffuseColor = new SFColor(0.1f, 0.05f, 0.03f)
                    },
                    TextureTransform = new TextureTransformNode()
                }
            }, scene.Root.Children[0]);
        }
Exemple #3
0
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0.05 0.03 0.01
            ambientIntensity 0
            specularColor 0.07 0.12 0.12
            shininess 0.06615
            transparency 0
        }
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            AssertExt.AreEqual(new ShapeNode {
                Appearance = new AppearanceNode {
                    Material = new MaterialNode {
                        DiffuseColor     = new SFColor(0.05f, 0.03f, 0.01f),
                        AmbientIntensity = 0,
                        SpecularColor    = new SFColor(0.07f, 0.12f, 0.12f),
                        Shininess        = 0.06615f,
                        Transparency     = 0,
                    }
                }
            }, scene.Root.Children[0]);
        }
Exemple #4
0
        public BaseNode[] createVrmlFromString(string vrmlSyntax)
        {
            Vrml97Tokenizer tokenizer = new Vrml97Tokenizer(new StringReader(vrmlSyntax));
            VrmlParser      parser    = new VrmlParser(tokenizer);
            MFNode          node      = new MFNode();

            parser.Parse(node);
            return(null);
        }
 private VrmlScene LoadScene(string name)
 {
     using (var stream = GetType().Assembly.GetManifestResourceStream(this.GetType(), name)) {
         var tokenizer = new Vrml97Tokenizer(stream);
         var parser    = new VrmlParser(tokenizer);
         var scene     = new VrmlScene();
         parser.Parse(scene);
         return(scene);
     }
 }
Exemple #6
0
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    geometry Sphere {
        radius 2
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            var sphere = (scene.Root.Children[0] as ShapeNode).Geometry as SphereNode;

            Assert.AreEqual(2f, sphere.Radius.Value);
        }
Exemple #7
0
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    geometry Box {
        size 4 5 6
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            var box = (scene.Root.Children[0] as ShapeNode).Geometry as BoxNode;

            Assert.AreEqual(4f, box.Size.X);
            Assert.AreEqual(5f, box.Size.Y);
            Assert.AreEqual(6f, box.Size.Z);
        }
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    appearance Appearance {
        textureTransform TextureTransform {
        }
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            AssertExt.AreEqual(new ShapeNode {
                Appearance = new AppearanceNode {
                    TextureTransform = new TextureTransformNode {
                    }
                }
            }, scene.Root.Children[0]);
        }
Exemple #9
0
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    geometry Cone {
        bottomRadius 10.0
        height 20.0
        side TRUE
        bottom FALSE
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            var cone = (scene.Root.Children[0] as ShapeNode).Geometry as ConeNode;

            Assert.AreEqual(10f, cone.BottomRadius.Value);
            Assert.AreEqual(20f, cone.Height.Value);
            Assert.AreEqual(true, cone.Side.Value);
            Assert.AreEqual(false, cone.Bottom.Value);
        }
        public void ParseTest()
        {
            var parser = new VrmlParser(new Vrml97Tokenizer(new StringReader(@"
#VRML V2.0 utf8
Shape {
    geometry Sphere {
        radius 2
    }
    appearance Appearance {
        material NULL
        texture NULL
        textureTransform NULL
    }
}")));
            var scene  = new VrmlScene();

            parser.Parse(scene);

            var shape = scene.Root.Children[0] as ShapeNode;

            Assert.IsNotNull(shape.Geometry);
            Assert.IsNotNull(shape.Appearance);
        }
Exemple #11
0
        /// <summary>
        /// Reads a VRML file at a specified path, parses the information, and returns the contained models.
        /// </summary>
        /// <param name="filePath">A full path to a valid VRML file.</param>
        /// <returns>The extracted models.</returns>
        public List <Mesh> Read(string filePath)
        {
            string fullPath = Environment.ExpandEnvironmentVariables(filePath);

            Logger.Info("Parsing .wrl file at path \"" + fullPath + "\"");

            FileInfo fileInfo = new FileInfo(fullPath);

            if (!fileInfo.Exists)
            {
                Logger.Error("File \"" + fullPath + "\" does not exist!");
                return(null);
            }

            // parse the Vrml file
            VrmlScene scene;

            using (var stream = new StreamReader(filePath))
            {
                VrmlParser parser = new VrmlParser(new VrmlTokenizer(stream));
                scene = parser.Parse();
            }

            List <Mesh>            meshes             = new List <Mesh>();
            MeshBuilder            builder            = new MeshBuilder();
            Dictionary <int, uint> indexToVertexIndex = new Dictionary <int, uint>();
            List <uint>            triangleIndices    = new List <uint>();

            // Do a depth first search on the VRML scene to find all meshes
            Stack <GroupingNode> toVisit = new Stack <GroupingNode>();

            toVisit.Push(scene.Root);

            HashSet <Node> visited = new HashSet <Node>(toVisit);

            string currentDescription = "DefaultName";

            while (toVisit.Count > 0)
            {
                GroupingNode node = toVisit.Pop();

                // keep track of the most recent description in the heirarchy to name child meshes using
                if (node is AnchorNode)
                {
                    currentDescription = (node as AnchorNode).Description;
                }

                foreach (Node child in node.Children)
                {
                    // If the child node may have children nodes we must visit it if we have not already
                    if (child is GroupingNode)
                    {
                        if (!visited.Contains(child))
                        {
                            toVisit.Push(child as GroupingNode);
                            visited.Add(child);
                        }
                    }
                    // If the node is a mesh extract it
                    if (child is ShapeNode)
                    {
                        ShapeNode shape = child as ShapeNode;

                        if (shape.Geometry.Node is IndexedFaceSetNode)
                        {
                            IndexedFaceSetNode indexFaceSet = shape.Geometry.Node as IndexedFaceSetNode;
                            CoordinateNode     coords       = indexFaceSet.Coord.Node as CoordinateNode;

                            // Must have at least one index given to be a mesh
                            if (indexFaceSet.CoordIndex.Length == 0)
                            {
                                continue;
                            }

                            // The indicies in the mesh we are building may not match those in the file if
                            // not all points from the coords are used, so maintain a mapping from original
                            // index in the file to the corresponding index in the mesh currently being built.
                            builder.Clear();
                            indexToVertexIndex.Clear();
                            triangleIndices.Clear();

                            foreach (SFInt32 index in indexFaceSet.CoordIndex)
                            {
                                if (index < 0)
                                {
                                    builder.AddTriangle(new Triangle(triangleIndices[0], triangleIndices[1], triangleIndices[2]));
                                    triangleIndices.Clear();
                                }
                                else
                                {
                                    uint i;
                                    if (!indexToVertexIndex.TryGetValue(index, out i))
                                    {
                                        SFVec3f pos = coords.Point.GetValue(index);

                                        i = (uint)builder.VertexCount;
                                        indexToVertexIndex.Add(index.Value, i);
                                        builder.AddVertex(new Vector3(pos.X, pos.Y, pos.Z));
                                    }
                                    triangleIndices.Add(i);
                                }
                            }

                            // Generate the mesh and calculate the vertex normals
                            Mesh mesh = builder.CreateMesh(currentDescription);
                            meshes.Add(mesh);
                            Logger.Info($"Found Mesh: {mesh}");
                        }
                    }
                }
            }

            Logger.Info("Finished parsing file");
            return(meshes);
        }