public NodeFactory() { builtin["Anchor"] = new AnchorNode(); builtin["Appearance"] = new AppearanceNode(); builtin["Background"] = new BackgroundNode(); builtin["Box"] = new BoxNode(); builtin["Color"] = new ColorNode(); builtin["Cone"] = new ConeNode(); builtin["Coordinate"] = new CoordinateNode(); builtin["CoordinateInterpolator"] = new CoordinateInterpolatorNode(); builtin["Cylinder"] = new CylinderNode(); builtin["DirectionalLight"] = new DirectionalLightNode(); builtin["Extrusion"] = new ExtrusionNode(); builtin["Group"] = new GroupNode(); builtin["Collision"] = new CollisionNode(); builtin["Switch"] = new SwitchNode(); builtin["IndexedFaceSet"] = new IndexedFaceSetNode(); builtin["IndexedLineSet"] = new IndexedLineSetNode(); builtin["Material"] = new MaterialNode(); builtin["NavigationInfo"] = new NavigationInfoNode(); builtin["OrientationInterpolator"] = new OrientationInterpolatorNode(); builtin["Normal"] = new NormalNode(); builtin["PixelTexture"] = new PixelTextureNode(); builtin["PointLight"] = new PointLightNode(); builtin["PositionInterpolator"] = new PositionInterpolatorNode(); builtin["ScalarInterpolator"] = new ScalarInterpolationNode(); builtin["Shape"] = new ShapeNode(); builtin["Sphere"] = new SphereNode(); builtin["TextureCoordinate"] = new TextureCoordinateNode(); builtin["TimeSensor"] = new TimeSensorNode(); builtin["Transform"] = new TransformNode(); builtin["Viewpoint"] = new ViewpointNode(); builtin["WorldInfo"] = new WorldInfoNode(); }
public static INamedDirectedGraph <ICoordinateNode, IWeightedNamedDirectedEdge <ICoordinateNode> > NamedDirectedFromInput(StreamReader input) { var graph = new NamedDirectedGraph <ICoordinateNode, IWeightedNamedDirectedEdge <ICoordinateNode> >(); while (!input.EndOfStream) { var line = input.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } var parts = line.Split(' '); if (parts.Length != 5) { Debug.WriteLine("Line did not have proper amount of parts: " + parts); continue; } var nodeFromX = parts[0].ToInt(-1); var nodeFromY = parts[1].ToInt(-1); var nodeToX = parts[3].ToInt(-1); var nodeToY = parts[4].ToInt(-1); var edgeName = parts[2]; var nodeFrom = new CoordinateNode(new Point(nodeFromX, nodeFromY)); var nodeTo = new CoordinateNode(new Point(nodeToX, nodeToY)); var weight = Point.DistanceBetween(nodeFrom.Point, nodeTo.Point); var edge = new WeightedNamedDirectedEdge <ICoordinateNode>(edgeName, nodeFrom, nodeTo, weight); graph.AddNode(nodeFrom).AddNode(nodeTo).AddEdge(edge); } return(graph); }
/// <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); }