Example #1
0
        public void SliceMesh(SimpleCfdMesh mesh, int vertexAmountThreshold = 65000)
        {
            Dictionary <int, Submesh> newSubmeshes = new Dictionary <int, Submesh>();
            int subKey = 0;

            foreach (var kv in mesh.Submeshes)
            {
                if (kv.Value.Vertices.Count <= vertexAmountThreshold)
                {
                    newSubmeshes.Add(subKey++, kv.Value);
                    continue;
                }

                var submesh = kv.Value;
                var slice   = (int)Math.Ceiling(submesh.Vertices.Count / (float)vertexAmountThreshold);
                var bestVertexCountThreshold = (int)(submesh.Vertices.Count / (float)slice);
                int sliceKey = 0;

                Dictionary <int, int> old2New = new Dictionary <int, int>();
                List <int>            indices = new List <int>();
                for (int i = 0, ilen = submesh.Indices.Count; i < ilen; i++)
                {
                    var idx = submesh.Indices[i];
                    if (!old2New.ContainsKey(idx))
                    {
                        old2New[idx] = old2New.Count;
                    }
                    indices.Add(old2New[idx]);

                    if (indices.Count % 3 == 0)//trick for triangles
                    {
                        if (old2New.Count >= bestVertexCountThreshold)
                        {
                            if (sliceKey < slice - 1 || old2New.Count >= vertexAmountThreshold)
                            {
                                var newSubmesh = CreateSubmeshSlice(submesh, old2New.Keys.ToList(), indices);
                                newSubmesh.Name        = submesh.Name + "_" + sliceKey;
                                newSubmesh.TimeStep    = submesh.TimeStep;
                                newSubmeshes[subKey++] = newSubmesh;

                                old2New.Clear();
                                indices.Clear();
                                sliceKey++;
                            }
                        }
                    }
                }
                if (old2New.Count > 0)
                {
                    var newSubmesh = CreateSubmeshSlice(submesh, old2New.Keys.ToList(), indices);
                    newSubmesh.Name        = submesh.Name + "_" + sliceKey;
                    newSubmesh.TimeStep    = submesh.TimeStep;
                    newSubmeshes[subKey++] = newSubmesh;

                    old2New.Clear();
                    indices.Clear();
                }
            }
            mesh.Submeshes = newSubmeshes;
        }
Example #2
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);
        }