Exemplo n.º 1
0
            protected bool LoadHeader(VtkModel model)
            {
                //1st line - file version and identifier: # vtk DataFile Version x.x
                var line   = GetLine().Trim();
                var tokens = line.Split(' ');

                Debug.WriteLine(string.Format("first line tokens count (should be 5): {0}", tokens));
                var v = tokens.Last();

                Debug.Assert(v.Contains('.'));
                var toks   = v.Split('.');
                int majorV = int.Parse(toks[0]);
                int minorV = int.Parse(toks[1]);

                Debug.WriteLine(string.Format("file version: {0}.{1}", majorV, minorV));
                model.MajorVersion = majorV;
                model.MinorVersion = minorV;

                //2nd line - header: no more than 256 char, for data description
                line = GetLine();
                Debug.Assert(line.Length <= 255);
                Debug.WriteLine(string.Format("file description: {0}", line));
                model.Description = line;

                //3rd line - file format: either ASCII or BINARY
                line = GetLine().Trim();
                Debug.Assert(line == "ASCII" || line == "BINARY");
                return(true);
            }
Exemplo n.º 2
0
            protected void LoadDataset(VtkModel model)
            {
                _curToken = GetVtkToken();
                switch (_curToken)
                {
                //                case VtkToken.StructuredPoint:break;
                //                case VtkToken.StructuredGrid:break;
                case VtkToken.UnstructuredGrid:
                    var usGrid = new VtkUnstructuredGrid();
                    LoadUnstructuredGrid(usGrid);
                    model.DataSet = usGrid;
                    break;

                case VtkToken.Polydata:
                    var polydata = new VtkPolyData();
                    LoadPolydata(polydata);
                    model.DataSet = polydata;
                    break;

                //                case VtkToken.RectilinearGrid:break;
                //                case VtkToken.Field:break;
                case VtkToken.UnknowMetadata:
                    SkipMetadata();
                    break;

                default:
                    throw new NotImplementedException(String.Format("unsupported vtk token: {0}.", _curToken));
                    break;
                }
            }
Exemplo n.º 3
0
            protected void LoadPointData(VtkModel model)
            {
                var n = GetCount();

                Debug.WriteLine(string.Format("POINT_DATA size: {0}", n));
                model.PointDatas = new Dictionary <string, VtkDataAttribute>();
                LoadDataAttribs(model.PointDatas, n);
            }
Exemplo n.º 4
0
            public VtkModel Load()
            {
                var model = new VtkModel();

                if (!LoadHeader(model))
                {
                    return(null);
                }
                while (!EndOfFile)
                {
                    if (_curToken == VtkToken.None)
                    {
                        _curToken = GetVtkToken();
                    }
                    switch (_curToken)
                    {
                    case VtkToken.Dataset:
                        LoadDataset(model);
                        break;

                    case VtkToken.CellData:
                        LoadCellData(model);
                        break;

                    case VtkToken.PointData:
                        LoadPointData(model);
                        break;

                    case VtkToken.UnknowMetadata:
                        SkipMetadata();
                        break;

                    default:
                        throw new NotImplementedException(string.Format("unsupported vtk token:{0}.", _curToken));
                        break;
                    }
                }
                return(model);
            }
Exemplo n.º 5
0
        public SimpleCfdMesh VtkToCfdMesh(VtkModel model)
        {
            var mesh = new SimpleCfdMesh();

            mesh.Name = model.Description;

            Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            var vCount  = model.DataSet.Points.Count;
            var subMesh = new Submesh();

            subMesh.Name      = mesh.Name;
            mesh.Submeshes[0] = subMesh;

            subMesh.Vertices.AddRange(model.DataSet.Points);
            for (int i = 0; i < vCount; i++)
            {
                var v = model.DataSet.Points[i];
                min = Vector3.Min(min, v);
                max = Vector3.Max(max, v);
            }
            mesh.Max = max;
            mesh.Min = min;

            subMesh.Max = max;
            subMesh.Min = min;

            #region Point Data
            if (model.PointDatas != null && model.PointDatas.Count > 0)
            {
                foreach (var kv in model.PointDatas)
                {
                    var key = kv.Key;
                    var val = kv.Value;
                    if (val is Scalars)
                    {
                        var            cs         = val as Scalars;
                        List <float[]> realScalar = new List <float[]>(cs.Values.Count);
                        for (int i = 0, len = cs.Values.Count; i < len; i++)
                        {
                            realScalar.Add(cs.Values[i]);
                        }
                        if (!subMesh.ScalarAttribs.ContainsKey(key))
                        {
                            subMesh.ScalarAttribs[key] = new List <List <float[]> >();
                        }
                        subMesh.ScalarAttribs[key].Add(realScalar);
                    }
                    else if (val is VectorsOrNormals)
                    {
                        var            vn         = val as VectorsOrNormals;
                        List <float[]> realVector = new List <float[]>(vn.Values.Count);
                        for (int i = 0, len = vn.Values.Count; i < len; i++)
                        {
                            var item = vn.Values[i];
                            realVector.Add(new float[] { item.X, item.Y, item.Z });
                        }
                        if (!subMesh.VectorAttribs.ContainsKey(key))
                        {
                            subMesh.VectorAttribs[key] = new List <List <float[]> >();
                        }
                        subMesh.VectorAttribs[key].Add(realVector);
                    }
                    else if (val is TextureCoordinates)
                    {
                        //TODO
                    }
                    else if (val is FieldData)
                    {
                        var fieldData = val as FieldData;
                        if (fieldData.Arrays != null && fieldData.Arrays.Count > 0)
                        {
                            foreach (var pair in fieldData.Arrays)
                            {
                                var pk = pair.Key;
                                var pv = pair.Value;
                                //taken field data array as scalars
                                if (pv.Tuples != null && pv.Tuples.Count > 0)
                                {
                                    var scalars = new List <float[]>();
                                    for (int i = 0, len = pv.Tuples.Count; i < len; i++)
                                    {
                                        scalars.Add(pv.Tuples[i]);
                                    }
                                    var k = key + "_" + pk;
                                    if (!subMesh.ScalarAttribs.ContainsKey(k))
                                    {
                                        subMesh.ScalarAttribs[k] = new List <List <float[]> >();
                                    }
                                    subMesh.ScalarAttribs[k].Add(scalars);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException("not implemented now for " + val.GetType());
                    }
                }
            }
            #endregion

            #region PolyData
            if (model.DataSet is VtkPolyData)
            {
                var items            = (model.DataSet as VtkPolyData).Items;
                var cellAttrib2Point = new Dictionary <int, int>();//key-vertex idx, val-cell idx
                foreach (var kv in items)
                {
                    switch (kv.Key)
                    {
                    case "Polygons":
                    case "POLYGONS":
                        subMesh.IndexType = 1;
                        var polygons = kv.Value;
                        for (int k = 0, kc = polygons.Indices.Count; k < kc; k++)
                        {
                            var idxs = polygons.Indices[k];
                            cellAttrib2Point[idxs[0]] = k;
                            cellAttrib2Point[idxs[1]] = k;
                            for (int i = 1, idLen = idxs.Length - 1; i < idLen; i++)
                            {
                                subMesh.Indices.Add(idxs[0]);
                                subMesh.Indices.Add(idxs[i]);
                                subMesh.Indices.Add(idxs[i + 1]);

                                cellAttrib2Point[idxs[i + 1]] = k;
                            }
                        }
                        break;

                    case "TriangleStrips":
                    case "TRIANGLE_STRIPS":
                        subMesh.IndexType = 1;
                        var triStrips = kv.Value;
                        for (int k = 0, kc = triStrips.Indices.Count; k < kc; k++)
                        {
                            var idxs = triStrips.Indices[k];
                            if (idxs.Length < 3)
                            {
                                continue;
                            }
                            cellAttrib2Point[idxs[0]] = k;
                            cellAttrib2Point[idxs[1]] = k;
                            for (int i = 1, j = 0, end = idxs.Length - 1; i < end; i++, j++)
                            {
                                if (j % 2 == 0)
                                {
                                    subMesh.Indices.Add(idxs[i - 1]);
                                    subMesh.Indices.Add(idxs[i]);
                                    subMesh.Indices.Add(idxs[i + 1]);
                                }
                                else
                                {
                                    subMesh.Indices.Add(idxs[i - 1]);
                                    subMesh.Indices.Add(idxs[i + 1]);
                                    subMesh.Indices.Add(idxs[i]);
                                }

                                cellAttrib2Point[idxs[i + 1]] = k;
                            }
                        }
                        break;

                    default:
                        throw new NotImplementedException("implement process for VERTICES, LINES, NORMALS, later...");
                        break;
                    }
                }

                //deal with cell data
                #region Cell Data
                if (model.CellDatas != null && model.CellDatas.Count > 0)
                {
                    var vtxCount = subMesh.Vertices.Count;
                    foreach (var kv in model.CellDatas)
                    {
                        var key = kv.Key;
                        var val = kv.Value;
                        if (val is Scalars)
                        {
                            var            cs         = val as Scalars;
                            List <float[]> realScalar = new List <float[]>(vtxCount);
                            for (int i = 0; i < vtxCount; i++)
                            {
                                realScalar.Add(cs.Values[cellAttrib2Point[i]]);
                            }
                            if (!subMesh.ScalarAttribs.ContainsKey(key))
                            {
                                subMesh.ScalarAttribs[key] = new List <List <float[]> >();
                            }
                            subMesh.ScalarAttribs[key].Add(realScalar);
                        }
                        else if (val is VectorsOrNormals)
                        {
                            var            vn         = val as VectorsOrNormals;
                            List <float[]> realVector = new List <float[]>(vtxCount);
                            for (int i = 0; i < vtxCount; i++)
                            {
                                var item = vn.Values[cellAttrib2Point[i]];
                                realVector.Add(new float[] { item.X, item.Y, item.Z });
                            }
                            if (!subMesh.VectorAttribs.ContainsKey(key))
                            {
                                subMesh.VectorAttribs[key] = new List <List <float[]> >();
                            }
                            subMesh.VectorAttribs[key].Add(realVector);
                        }
                        else if (val is FieldData)
                        {
                            var fieldData = val as FieldData;
                            if (fieldData.Arrays != null && fieldData.Arrays.Count > 0)
                            {
                                foreach (var pair in fieldData.Arrays)
                                {
                                    var pk = pair.Key;
                                    var pv = pair.Value;
                                    //taken field data array as scalars
                                    if (pv.Tuples != null && pv.Tuples.Count > 0)
                                    {
                                        var scalars = new List <float[]>(vtxCount);
                                        for (int i = 0; i < vtxCount; i++)
                                        {
                                            scalars.Add(pv.Tuples[cellAttrib2Point[i]]);
                                        }
                                        var k = key + "_" + pk;
                                        if (!subMesh.ScalarAttribs.ContainsKey(k))
                                        {
                                            subMesh.ScalarAttribs[k] = new List <List <float[]> >();
                                        }
                                        subMesh.ScalarAttribs[k].Add(scalars);
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("not implemented now for " + val.GetType());
                        }
                    }
                }
                #endregion
            }
            #endregion

            #region Cells
            if (model.DataSet.Cells != null && model.DataSet.Cells.Count > 0)
            {
                subMesh.IndexType = 1;
                var cellAttrib2Point = new Dictionary <int, int>();//key-vertex idx, val-cell idx

                for (int i = 0, cl = model.DataSet.Cells.Count; i < cl; i++)
                {
                    var cell = model.DataSet.Cells[i];
                    foreach (var index in cell.Indices)
                    {
                        cellAttrib2Point[index] = i;
                    }

                    if (cell.Type == VtkCellType.Hexahedron)
                    {
                        subMesh.Indices.Add(cell.Indices[0]);
                        subMesh.Indices.Add(cell.Indices[1]);
                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[0]);

                        subMesh.Indices.Add(cell.Indices[0 + 4]);
                        subMesh.Indices.Add(cell.Indices[1 + 4]);
                        subMesh.Indices.Add(cell.Indices[2 + 4]);
                        subMesh.Indices.Add(cell.Indices[2 + 4]);
                        subMesh.Indices.Add(cell.Indices[3 + 4]);
                        subMesh.Indices.Add(cell.Indices[0 + 4]);

                        subMesh.Indices.Add(cell.Indices[0]);
                        subMesh.Indices.Add(cell.Indices[1]);
                        subMesh.Indices.Add(cell.Indices[5]);
                        subMesh.Indices.Add(cell.Indices[5]);
                        subMesh.Indices.Add(cell.Indices[4]);
                        subMesh.Indices.Add(cell.Indices[0]);

                        subMesh.Indices.Add(cell.Indices[1]);
                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[6]);
                        subMesh.Indices.Add(cell.Indices[6]);
                        subMesh.Indices.Add(cell.Indices[5]);
                        subMesh.Indices.Add(cell.Indices[1]);

                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[7]);
                        subMesh.Indices.Add(cell.Indices[7]);
                        subMesh.Indices.Add(cell.Indices[6]);
                        subMesh.Indices.Add(cell.Indices[2]);

                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[0]);
                        subMesh.Indices.Add(cell.Indices[4]);
                        subMesh.Indices.Add(cell.Indices[4]);
                        subMesh.Indices.Add(cell.Indices[7]);
                        subMesh.Indices.Add(cell.Indices[3]);
                    }
                    else if (cell.Type == VtkCellType.Voxel)
                    {
                        subMesh.Indices.Add(cell.Indices[0]);
                        subMesh.Indices.Add(cell.Indices[1]);
                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[0]);

                        subMesh.Indices.Add(cell.Indices[0 + 4]);
                        subMesh.Indices.Add(cell.Indices[1 + 4]);
                        subMesh.Indices.Add(cell.Indices[3 + 4]);
                        subMesh.Indices.Add(cell.Indices[3 + 4]);
                        subMesh.Indices.Add(cell.Indices[2 + 4]);
                        subMesh.Indices.Add(cell.Indices[0 + 4]);

                        subMesh.Indices.Add(cell.Indices[0]);
                        subMesh.Indices.Add(cell.Indices[1]);
                        subMesh.Indices.Add(cell.Indices[5]);
                        subMesh.Indices.Add(cell.Indices[5]);
                        subMesh.Indices.Add(cell.Indices[4]);
                        subMesh.Indices.Add(cell.Indices[0]);

                        subMesh.Indices.Add(cell.Indices[1]);
                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[7]);
                        subMesh.Indices.Add(cell.Indices[7]);
                        subMesh.Indices.Add(cell.Indices[5]);
                        subMesh.Indices.Add(cell.Indices[1]);

                        subMesh.Indices.Add(cell.Indices[3]);
                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[6]);
                        subMesh.Indices.Add(cell.Indices[6]);
                        subMesh.Indices.Add(cell.Indices[7]);
                        subMesh.Indices.Add(cell.Indices[3]);

                        subMesh.Indices.Add(cell.Indices[2]);
                        subMesh.Indices.Add(cell.Indices[0]);
                        subMesh.Indices.Add(cell.Indices[4]);
                        subMesh.Indices.Add(cell.Indices[4]);
                        subMesh.Indices.Add(cell.Indices[6]);
                        subMesh.Indices.Add(cell.Indices[2]);
                    }
                    else
                    {
                        throw new NotImplementedException("implement process for " + cell.Type);
                    }
                }

                //deal with cell data
                #region Cell Data
                if (model.CellDatas != null && model.CellDatas.Count > 0)
                {
                    var vtxCount = subMesh.Vertices.Count;
                    foreach (var kv in model.CellDatas)
                    {
                        var key = kv.Key;
                        var val = kv.Value;
                        if (val is Scalars)
                        {
                            var            cs         = val as Scalars;
                            List <float[]> realScalar = new List <float[]>(vtxCount);
                            for (int i = 0; i < vtxCount; i++)
                            {
                                realScalar.Add(cs.Values[cellAttrib2Point[i]]);
                            }
                            if (!subMesh.ScalarAttribs.ContainsKey(key))
                            {
                                subMesh.ScalarAttribs[key] = new List <List <float[]> >();
                            }
                            subMesh.ScalarAttribs[key].Add(realScalar);
                        }
                        else if (val is VectorsOrNormals)
                        {
                            var            vn         = val as VectorsOrNormals;
                            List <float[]> realVector = new List <float[]>(vtxCount);
                            for (int i = 0; i < vtxCount; i++)
                            {
                                var item = vn.Values[cellAttrib2Point[i]];
                                realVector.Add(new float[] { item.X, item.Y, item.Z });
                            }
                            if (!subMesh.VectorAttribs.ContainsKey(key))
                            {
                                subMesh.VectorAttribs[key] = new List <List <float[]> >();
                            }
                            subMesh.VectorAttribs[key].Add(realVector);
                        }
                        else if (val is FieldData)
                        {
                            var fieldData = val as FieldData;
                            if (fieldData.Arrays != null && fieldData.Arrays.Count > 0)
                            {
                                foreach (var pair in fieldData.Arrays)
                                {
                                    var pk = pair.Key;
                                    var pv = pair.Value;
                                    //taken field data array as scalars
                                    if (pv.Tuples != null && pv.Tuples.Count > 0)
                                    {
                                        var scalars = new List <float[]>(vtxCount);
                                        for (int i = 0; i < vtxCount; i++)
                                        {
                                            scalars.Add(pv.Tuples[cellAttrib2Point[i]]);
                                        }
                                        var k = key + "_" + pk;
                                        if (!subMesh.ScalarAttribs.ContainsKey(k))
                                        {
                                            subMesh.ScalarAttribs[k] = new List <List <float[]> >();
                                        }
                                        subMesh.ScalarAttribs[k].Add(scalars);
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("not implemented now for " + val.GetType());
                        }
                    }
                }
                #endregion
            }
            #endregion

            return(mesh);
        }