Exemple #1
0
        /// <summary>
        /// Duplicate of the inlined version!!!!!! DANGEROUS
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="signs"></param>
        /// <param name="cube"></param>
        /// <returns></returns>
        public static Vector3 calculateQefPoint(AbstractHermiteGrid grid, bool[] signs, Point3 cube)
        {
            //TODO: this can be optimized probably!
            var changingEdges = grid.GetAllEdgeIds().Where(e =>
            {
                var ids = grid.GetEdgeVertexIds(e);
                return(signs[ids[0]] != signs[ids[1]]);
            }).ToArray();


            var positions = changingEdges.Select(e => grid.GetEdgeIntersectionCubeLocal(cube, e)).ToArray();
            var normals   = changingEdges.Select(e => grid.GetEdgeNormal(cube, e)).ToArray();

            var meanIntersectionPoint = positions.Aggregate((a, b) => a + b) * (1f / positions.Length);
            var leastsquares          = QEFCalculator.CalculateCubeQEF(normals, positions, meanIntersectionPoint);

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

            if (qefPoint[0] < 0 || qefPoint[1] < 0 || qefPoint[2] < 0 ||
                qefPoint[0] > 1 || qefPoint[1] > 1 || qefPoint[2] > 1)
            {
                qefPoint = 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");
            }
            return(qefPoint);
        }
Exemple #2
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());
            });
        }