Ejemplo n.º 1
0
        public Submesh CreateSubmeshSlice(Submesh submesh, List <int> vertexIndices, List <int> indices)
        {
            var newsb = new Submesh();

            newsb.Name      = submesh.Name;
            newsb.IndexType = submesh.IndexType;
            newsb.Indices.AddRange(indices);

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

            foreach (var k in vertexIndices)
            {
                var v = submesh.Vertices[k];
                newsb.Vertices.Add(v);
                min = Vector3.Min(min, v);
                max = Vector3.Max(max, v);
            }
            newsb.Max = max;
            newsb.Min = min;

            //normals
            if (submesh.Normals != null && submesh.Normals.Count > 0)
            {
                foreach (var k in vertexIndices)
                {
                    newsb.Normals.Add(submesh.Normals[k]);
                }
            }
            //texture coords
            if (submesh.Texcoords != null && submesh.Texcoords.Count > 0)
            {
                foreach (var k in vertexIndices)
                {
                    newsb.Texcoords.Add(submesh.Texcoords[k]);
                }
            }
            //colors
            if (submesh.Colors != null && submesh.Colors.Count > 0)
            {
                foreach (var k in vertexIndices)
                {
                    newsb.Colors.Add(submesh.Colors[k]);
                }
            }
            //scalar attribs
            if (submesh.ScalarAttribs != null && submesh.ScalarAttribs.Count > 0)
            {
                foreach (var kv in submesh.ScalarAttribs)
                {
                    List <List <float[]> > attribs = new List <List <float[]> >();
                    foreach (var timeStep in kv.Value)
                    {
                        var newts = new List <float[]>();
                        foreach (var k in vertexIndices)
                        {
                            newts.Add(timeStep[k]);
                        }
                        attribs.Add(newts);
                    }
                    newsb.ScalarAttribs[kv.Key] = attribs;
                }
            }
            //vector attribs
            if (submesh.VectorAttribs != null && submesh.VectorAttribs.Count > 0)
            {
                foreach (var kv in submesh.VectorAttribs)
                {
                    List <List <float[]> > attribs = new List <List <float[]> >();
                    foreach (var timeStep in kv.Value)
                    {
                        var newts = new List <float[]>();
                        foreach (var k in vertexIndices)
                        {
                            newts.Add(timeStep[k]);
                        }
                        attribs.Add(newts);
                    }
                    newsb.VectorAttribs[kv.Key] = attribs;
                }
            }

            return(newsb);
        }
Ejemplo n.º 2
0
        private void LoadV1(BinaryReader reader)
        {
            //name
            Name = reader.ReadString();
            //min
            Min.X = reader.ReadSingle();
            Min.Y = reader.ReadSingle();
            Min.Z = reader.ReadSingle();
            //max
            Max.X = reader.ReadSingle();
            Max.Y = reader.ReadSingle();
            Max.Z = reader.ReadSingle();

            //submesh
            var smCount = reader.ReadInt32();

            for (int smi = 0; smi < smCount; smi++)
            {
                var sm = new Submesh();
                Submeshes[smi] = sm;
                sm.Name        = reader.ReadString();
                //min
                sm.Min.X = reader.ReadSingle();
                sm.Min.Y = reader.ReadSingle();
                sm.Min.Z = reader.ReadSingle();
                //max
                sm.Max.X = reader.ReadSingle();
                sm.Max.Y = reader.ReadSingle();
                sm.Max.Z = reader.ReadSingle();
                //vertices
                var vCount = reader.ReadInt32();
                if (vCount > 0)
                {
                    for (int i = 0; i < vCount; i++)
                    {
                        var v = new Vector3();
                        v.X = reader.ReadSingle();
                        v.Y = reader.ReadSingle();
                        v.Z = reader.ReadSingle();
                        sm.Vertices.Add(v);
                    }
                }
                //normals
                var nCount = reader.ReadInt32();
                if (nCount > 0)
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        var n = new Vector3();
                        n.X = reader.ReadSingle();
                        n.Y = reader.ReadSingle();
                        n.Z = reader.ReadSingle();
                        sm.Normals.Add(n);
                    }
                }
                //tex coords
                var tCount = reader.ReadInt32();
                if (tCount > 0)
                {
                    for (int i = 0; i < tCount; i++)
                    {
                        var t = new Vector4();
                        t.X = reader.ReadSingle();
                        t.Y = reader.ReadSingle();
                        t.Z = reader.ReadSingle();
                        t.W = reader.ReadSingle();
                        sm.Texcoords.Add(t);
                    }
                }
                //colors
                var cCount = reader.ReadInt32();
                if (cCount > 0)
                {
                    for (int i = 0; i < cCount; i++)
                    {
                        var t = new Vector4();
                        t.X = reader.ReadSingle();
                        t.Y = reader.ReadSingle();
                        t.Z = reader.ReadSingle();
                        t.W = reader.ReadSingle();
                        sm.Colors.Add(t);
                    }
                }

                //index type
                sm.IndexType = reader.ReadInt32();
                //indices
                var iCount = reader.ReadInt32();
                if (iCount > 0)
                {
                    for (int i = 0; i < iCount; i++)
                    {
                        sm.Indices.Add(reader.ReadInt32());
                    }
                }
                //scalars
                var rsCount = reader.ReadInt32();
                if (rsCount > 0)
                {
                    for (int i = 0; i < rsCount; i++)
                    {
                        var key             = reader.ReadString();
                        var timeSeriesCount = reader.ReadInt32();
                        if (timeSeriesCount > 0)
                        {
                            var val = new List <List <float[]> >(timeSeriesCount);
                            for (int j = 0; j < timeSeriesCount; j++)
                            {
                                var attribCount     = reader.ReadInt32();
                                var attribCompCount = reader.ReadInt32();
                                if (attribCount > 0 && attribCompCount > 0)
                                {
                                    var attribList = new List <float[]>(attribCount);

                                    for (int k = 0; k < attribCount; k++)
                                    {
                                        var comps = new float[attribCompCount];
                                        for (int l = 0; l < attribCompCount; l++)
                                        {
                                            comps[l] = reader.ReadSingle();
                                        }
                                    }
                                    val.Add(attribList);
                                }
                            }
                            sm.ScalarAttribs[key] = val;
                        }
                    }
                }
                //vectors
                var rvCount = reader.ReadInt32();
                if (rvCount > 0)
                {
                    for (int i = 0; i < rvCount; i++)
                    {
                        var key             = reader.ReadString();
                        var timeSeriesCount = reader.ReadInt32();
                        if (timeSeriesCount > 0)
                        {
                            var val = new List <List <float[]> >(timeSeriesCount);
                            for (int j = 0; j < timeSeriesCount; j++)
                            {
                                var attribCount     = reader.ReadInt32();
                                var attribCompCount = reader.ReadInt32();
                                if (attribCount > 0 && attribCompCount > 0)
                                {
                                    var attribList = new List <float[]>(attribCount);

                                    for (int k = 0; k < attribCount; k++)
                                    {
                                        var comps = new float[attribCompCount];
                                        for (int l = 0; l < attribCompCount; l++)
                                        {
                                            comps[l] = reader.ReadSingle();
                                        }
                                    }
                                    val.Add(attribList);
                                }
                            }
                            sm.VectorAttribs[key] = val;
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
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);
        }