Exemple #1
0
        public static SoftBody CreateFromVtkFile(SoftBodyWorldInfo worldInfo, string fileName)
        {
            List <Vector3> points = new List <Vector3>(0);
            List <int[]>   cells  = new List <int[]>(0);

            using (var reader = new StreamReader(fileName))
            {
                bool   readingPoints = false;
                bool   readingCells  = false;
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    string[] lineParts = line.Split(' ');
                    if (lineParts[0] == "POINTS")
                    {
                        readingPoints   = true;
                        readingCells    = false;
                        points.Capacity = int.Parse(lineParts[1]);
                    }
                    else if (lineParts[0] == "CELLS")
                    {
                        readingPoints  = false;
                        readingCells   = true;
                        cells.Capacity = int.Parse(lineParts[1]);
                    }
                    else if (lineParts[0] == "CELL_TYPES")
                    {
                        readingPoints = false;
                        readingCells  = false;
                    }
                    else if (readingPoints)
                    {
                        Vector3 point = new Vector3(
                            (float)ParseDouble(lineParts[0]),
                            (float)ParseDouble(lineParts[1]),
                            (float)ParseDouble(lineParts[2]));
                        points.Add(point);
                    }
                    else if (readingCells)
                    {
                        int   numPoints = int.Parse(lineParts[0]);
                        int[] cell      = new int[numPoints];
                        for (int i = 0; i < numPoints; i++)
                        {
                            cell[i] = int.Parse(lineParts[i + 1]);
                        }
                        cells.Add(cell);
                    }
                }
            }
            SoftBody body = new SoftBody(worldInfo, points.Count, points.ToArray(), null);

            foreach (int[] cell in cells)
            {
                body.AppendTetra(cell[0], cell[1], cell[2], cell[3]);
                body.AppendLink(cell[0], cell[1], null, true);
                body.AppendLink(cell[1], cell[2], null, true);
                body.AppendLink(cell[2], cell[0], null, true);
                body.AppendLink(cell[0], cell[3], null, true);
                body.AppendLink(cell[1], cell[3], null, true);
                body.AppendLink(cell[2], cell[3], null, true);
            }

            GenerateBoundaryFaces(body);
            body.InitializeDmInverse();
            body.TetraScratches.Resize(body.Tetras.Count);
            body.TetraScratchesTn.Resize(body.Tetras.Count);
            //Console.WriteLine($"Nodes:  {body.Nodes.Count}");
            //Console.WriteLine($"Links:  {body.Links.Count}");
            //Console.WriteLine($"Faces:  {body.Faces.Count}");
            //Console.WriteLine($"Tetras: {body.Tetras.Count}");

            return(body);
        }
Exemple #2
0
        public static SoftBody CreateFromTetGenData(SoftBodyWorldInfo worldInfo,
                                                    string ele, string face, string node, bool faceLinks, bool tetraLinks, bool facesFromTetras)
        {
            CultureInfo culture = CultureInfo.InvariantCulture;

            char[]    separator = new[] { ' ' };
            Vector3[] pos;

            using (var nodeReader = new StringReader(node))
            {
                string[] nodeHeader = nodeReader.ReadLine().Split(separator, StringSplitOptions.RemoveEmptyEntries);
                int      numNodes   = int.Parse(nodeHeader[0]);
                //int numDims = int.Parse(nodeHeader[1]);
                //int numAttrs = int.Parse(nodeHeader[2]);
                //bool hasBounds = !nodeHeader[3].Equals("0");

                pos = new Vector3[numNodes];
                for (int n = 0; n < numNodes; n++)
                {
                    string[] nodeLine = nodeReader.ReadLine().Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    pos[int.Parse(nodeLine[0])] = new Vector3(
                        double.Parse(nodeLine[1], culture),
                        double.Parse(nodeLine[2], culture),
                        double.Parse(nodeLine[3], culture));
                }
            }
            var psb = new SoftBody(worldInfo, pos.Length, pos, null);

            /*
             * if (!string.IsNullOrEmpty(face))
             * {
             *      throw new NotImplementedException();
             * }
             */
            if (!string.IsNullOrEmpty(ele))
            {
                using (var eleReader = new StringReader(ele))
                {
                    string[] eleHeader = eleReader.ReadLine().Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    int      numTetras = int.Parse(eleHeader[0]);
                    //int numCorners = int.Parse(eleHeader[1]);
                    //int numAttrs = int.Parse(eleHeader[2]);

                    for (int n = 0; n < numTetras; n++)
                    {
                        string[] eleLine = eleReader.ReadLine().Split(separator, StringSplitOptions.RemoveEmptyEntries);
                        //int index = int.Parse(eleLine[0], culture);
                        int ni0 = int.Parse(eleLine[1], culture);
                        int ni1 = int.Parse(eleLine[2], culture);
                        int ni2 = int.Parse(eleLine[3], culture);
                        int ni3 = int.Parse(eleLine[4], culture);
                        psb.AppendTetra(ni0, ni1, ni2, ni3);
                        if (tetraLinks)
                        {
                            psb.AppendLink(ni0, ni1, null, true);
                            psb.AppendLink(ni1, ni2, null, true);
                            psb.AppendLink(ni2, ni0, null, true);
                            psb.AppendLink(ni0, ni3, null, true);
                            psb.AppendLink(ni1, ni3, null, true);
                            psb.AppendLink(ni2, ni3, null, true);
                        }
                    }
                }
            }

            //Console.WriteLine("Nodes: {0}", psb.Nodes.Count);
            //Console.WriteLine("Links: {0}", psb.Links.Count);
            //Console.WriteLine("Faces: {0}", psb.Faces.Count);
            //Console.WriteLine("Tetras: {0}", psb.Tetras.Count);

            return(psb);
        }
        SoftBody CreateVolumetricSoftbody(SoftBodyWorldInfo worldInfo, BulletSharp.Math.Vector3[] vertices)
        {
            int vertexcount = vertices.Length;

            psb = new SoftBody(worldInfo, vertexcount, vertices, null);

            for (int x = 3; vertexcount > x; x += 4)
            {
                int n0 = x - 3;
                int n1 = x - 2;
                int n2 = x - 1;
                int n3 = x - 0;
                psb.AppendTetra(n0, n1, n2, n3);

                psb.AppendLink(n0, n1);
                linkdata.Add(true);
                brokenlinks.Add(false);

                psb.AppendLink(n1, n2);
                linkdata.Add(true);
                brokenlinks.Add(false);

                psb.AppendLink(n2, n0);
                linkdata.Add(true);
                brokenlinks.Add(false);

                psb.AppendLink(n0, n3);
                linkdata.Add(true);
                brokenlinks.Add(false);

                psb.AppendLink(n1, n3);
                linkdata.Add(true);
                brokenlinks.Add(false);

                psb.AppendLink(n2, n3);
                linkdata.Add(true);
                brokenlinks.Add(false);

                ValidTetras.Add(true);
            }

            instances = GetInstances(vertices);
            //List<bool> ValidCuts = CutTest3(vertices);
            for (int x = 0; instances.Count > x; x++)
            {
                //if (ValidCuts[x] == false)
                {
                    //    continue;
                }
                for (int i = 1; instances[x].Count > i; i++)
                {
                    int i1 = instances[x][i - 1];
                    int i2 = instances[x][i - 0];
                    psb.AppendLink(i1, i2);
                    linkdata.Add(false);
                    brokenlinks.Add(false);
                }
            }

            return(psb);
        }