public override Vector4 getEdgeData(Point3 cube, int edgeId)
        {
            var offsetPos = cube - offset;

            if (offsetPos.X < 0 || offsetPos.Y < 0 || offsetPos.Z < 0 ||
                offsetPos.X >= b.Dimensions.X || offsetPos.Y >= b.Dimensions.Y || offsetPos.Z >= b.Dimensions.Z)
            {
                return(a.getEdgeData(cube, edgeId));
            }

            if (!a.HasEdgeData(cube, edgeId))
            {
                return(b.getEdgeData(offsetPos, edgeId));
            }

            if (!b.HasEdgeData(offsetPos, edgeId))
            {
                return(a.getEdgeData(cube, edgeId));
            }

            var da = a.getEdgeData(cube, edgeId).W;
            var db = b.getEdgeData(offsetPos, edgeId).W;

            float diff;

            if (GetSign(cube))
            {
                diff = da - db; // 0 is not air, so if diff > 0 then b is closer
            }
            else
            {
                diff = db - da; // 0 is air, so if diff > 0 then b is closer
            }
            return(diff > 0 ? b.getEdgeData(offsetPos, edgeId) : a.getEdgeData(cube, edgeId));
        }
Ejemplo n.º 2
0
        public override Vector4 getEdgeData(Point3 cube, int edgeId)
        {
            var signs = a.GetEdgeSigns(cube, edgeId);

            if (signs[0] != signs[1])
            {
                return(a.getEdgeData(cube, edgeId));
            }



            signs = b.GetEdgeSigns(cube - _offset, edgeId);
            if (signs[0] != signs[1])
            {
                return(b.getEdgeData(cube - _offset, edgeId));
            }


            throw new InvalidOperationException("No crossing edge here!");
        }
Ejemplo n.º 3
0
        private static void createQEFVertices(List <Vector3> vertices, AbstractHermiteGrid grid, Dictionary <Point3, int> vIndex)
        {
            var cubeSigns = new bool[8];

            var edgeVertexIds = grid.GetAllEdgeIds().Select(e => grid.GetEdgeVertexIds(e)).ToArray();
            var edgeOffsets   = grid.GetAllEdgeIds().Select(e => grid.GetEdgeOffsets(e)).ToArray();

            int changingEdgeCount = 0;

            int[]     changingEdges = new int[12];
            Vector3[] positions     = new Vector3[12];
            Vector3[] normals       = new Vector3[12];

            grid.ForEachCube(curr =>
            {
                grid.GetCubeSigns(curr, cubeSigns);
                bool allTrue  = true;
                bool allFalse = true;
                for (int i = 0; i < 8; i++)
                {
                    var sign = cubeSigns[i];
                    allTrue  = sign && allTrue;
                    allFalse = !sign && allFalse;
                }
                if (allTrue || allFalse)
                {
                    return;                         // no sign changes
                }
                //if ( cubeSigns.All( v => v ) || !cubeSigns.Any( v => v ) ) return; // no sign changes

                changingEdgeCount = 0;
                for (int i = 0; i < edgeVertexIds.Length; i++)
                {
                    var ids = edgeVertexIds[i];
                    if (cubeSigns[ids[0]] == cubeSigns[ids[1]])
                    {
                        continue;
                    }

                    changingEdges[changingEdgeCount] = i;
                    changingEdgeCount++;
                }

                for (int i = 0; i < changingEdgeCount; i++)
                {
                    var iEdgeId      = changingEdges[i];
                    var iEdgeOffsets = edgeOffsets[iEdgeId];
                    var iEdgeData    = grid.getEdgeData(curr, iEdgeId);
                    positions[i]     = Vector3.Lerp(iEdgeOffsets[0], iEdgeOffsets[1], iEdgeData.W);
                    normals[i]       = iEdgeData.TakeXYZ();
                }


                var meanIntersectionPoint = new Vector3();
                for (int i = 0; i < changingEdgeCount; i++)
                {
                    meanIntersectionPoint = meanIntersectionPoint + positions[i];
                }
                meanIntersectionPoint = meanIntersectionPoint * (1f / changingEdgeCount);

                var leastsquares = QEFCalculator.CalculateCubeQEF(normals, positions, changingEdgeCount, meanIntersectionPoint);

                var qefPoint1 = new Vector3();
                qefPoint1     = new Vector3(leastsquares[0], leastsquares[1], leastsquares[2]);

                if (qefPoint1[0] < 0 || qefPoint1[1] < 0 || qefPoint1[2] < 0 ||
                    qefPoint1[0] > 1 || qefPoint1[1] > 1 || qefPoint1[2] > 1)
                {
                    qefPoint1 = meanIntersectionPoint;     // I found someone online who does this too: http://ngildea.blogspot.be/2014/11/implementing-dual-contouring.html
                    //TODO: should probably fix the QEF, maybe by removing singular values

                    //ERROR!
                    //throw new InvalidOperationException("QEF returned solution outside of cube");
                }

                vIndex[curr] = vertices.Count;
                vertices.Add(qefPoint1 + (Vector3)curr.ToVector3());
            });
        }
        public static HermiteDataGrid CopyGrid(AbstractHermiteGrid grid)
        {
            var    ret         = new HermiteDataGrid();
            Point3 storageSize = grid.Dimensions + new Point3(1, 1, 1);

            ret.cells = new Array3D <Vertex>(storageSize);

            for (int x = 0; x < storageSize.X; x++)
            {
                for (int y = 0; y < storageSize.Y; y++)
                {
                    for (int z = 0; z < storageSize.Z; z++)
                    {
                        var p = new Point3(x, y, z);
                        ret.cells[p] = new Vertex()
                        {
                            Sign     = grid.GetSign(p),
                            Material = grid.GetMaterial(p)

                                       /*EdgeData = ret.dirs.Select(dir =>
                                        *  {
                                        *      var edgeId = grid.GetEdgeId(p, p + dir);
                                        *      return grid.HasEdgeData(p, edgeId) ? grid.getEdgeData(p, edgeId) : new Vector4();
                                        *  }).ToArray()*/
                        };
                    }
                }
            }

            //ret.ForEachGridPoint(p =>
            //    {
            //        ret.cells[p] = new Vertex()
            //            {
            //                Sign = grid.GetSign(p),
            //                /*EdgeData = ret.dirs.Select(dir =>
            //                    {
            //                        var edgeId = grid.GetEdgeId(p, p + dir);
            //                        return grid.HasEdgeData(p, edgeId) ? grid.getEdgeData(p, edgeId) : new Vector4();
            //                    }).ToArray()*/
            //            };
            //    });


            var dirs     = ret.dirs;
            var dirEdges = dirs.Select(i => ret.GetEdgeId(new Point3(), i)).ToArray();

            ret.ForEachGridPoint(p =>
            {
                var gridPointP      = ret.cells.GetFast(p.X, p.Y, p.Z);
                gridPointP.EdgeData = new Vector4[3];
                for (int i = 0; i < 3; i++)
                {
                    var dir    = dirs[i];
                    var edgeId = dirEdges[i];

                    Point3 endPoint = p + dir;
                    // Optimization: direclty read from already constructed data
                    if (!ret.cells.InArray(endPoint))
                    {
                        continue;
                    }
                    if (gridPointP.Sign == ret.cells.GetFast(endPoint.X, endPoint.Y, endPoint.Z).Sign)
                    {
                        continue;
                    }
                    if (!grid.HasEdgeData(p, edgeId))
                    {
                        // This can normally not happen, since we check if there is a sign difference by looking at the already evaluated density points.
                        //  If this would be true there is some problem with the manual determining of the existence of an edge.
                        //throw new InvalidOperationException();
                        continue;
                    }
                    gridPointP.EdgeData[i] = grid.getEdgeData(p, edgeId);
                }

                /*val.EdgeData = ret.dirs.Select( dir =>
                 *  {
                 *
                 *  } ).ToArray();*/

                ret.cells[p] = gridPointP;
            });

            return(ret);
        }