Example #1
0
        public static MeshGeometry3D ToMeshGeometry3D(this DMesh3 mesh)
        {
            var vertices  = mesh.Vertices();
            var uiPoints  = new Point3DCollection(mesh.VertexCount);
            var indexes   = mesh.TriangleIndices();
            var uiIndexes = new IntCollection();

            foreach (var v in vertices)
            {
                uiPoints.Add(v.ToPoint3D());
            }

            foreach (var i in indexes)
            {
                uiIndexes.Add(i);
            }

            return(new MeshGeometry3D()
            {
                Positions = uiPoints.ToVector3Collection(),
                TriangleIndices = uiIndexes
            });
        }
        public static System.Windows.Media.Media3D.MeshGeometry3D GenerateMesh(this PointCloudTileSource source, Grid<float> grid, Extent3D distributionExtent, bool showBackFaces)
        {
            // subtract midpoint to center around (0,0,0)
            Extent3D centeringExtent = source.Extent;
            Point3D centerOfMass = source.CenterOfMass;
            double centerOfMassMinusMin = centerOfMass.Z - centeringExtent.MinZ;

            var positions = new System.Windows.Media.Media3D.Point3DCollection(grid.CellCount);
            var indices = new System.Windows.Media.Int32Collection(2 * (grid.SizeX - 1) * (grid.SizeY - 1));

            float fillVal = grid.FillVal;

            for (int x = 0; x < grid.SizeX; x++)
            {
                for (int y = 0; y < grid.SizeY; y++)
                {
                    double value = grid.Data[x, y] - centerOfMassMinusMin;

                    double xCoord = ((double)x / grid.SizeX) * distributionExtent.RangeX + distributionExtent.MinX - distributionExtent.MidpointX;
                    double yCoord = ((double)y / grid.SizeY) * distributionExtent.RangeY + distributionExtent.MinY - distributionExtent.MidpointY;

                    xCoord += (distributionExtent.MidpointX - centeringExtent.MidpointX);
                    yCoord += (distributionExtent.MidpointY - centeringExtent.MidpointY);

                    var point = new System.Windows.Media.Media3D.Point3D(xCoord, yCoord, value);
                    positions.Add(point);

                    if (x > 0 && y > 0)
                    {
                        // add two triangles
                        int currentPosition = x * grid.SizeY + y;
                        int topPosition = currentPosition - 1;
                        int leftPosition = currentPosition - grid.SizeY;
                        int topleftPosition = leftPosition - 1;

                        if (grid.Data[x - 1, y] != fillVal && grid.Data[x, y - 1] != fillVal)
                        {
                            if (grid.Data[x, y] != fillVal)
                            {
                                indices.Add(leftPosition);
                                indices.Add(topPosition);
                                indices.Add(currentPosition);

                                if (showBackFaces)
                                {
                                    indices.Add(leftPosition);
                                    indices.Add(currentPosition);
                                    indices.Add(topPosition);
                                }
                            }

                            if (grid.Data[x - 1, y - 1] != fillVal)
                            {
                                indices.Add(topleftPosition);
                                indices.Add(topPosition);
                                indices.Add(leftPosition);

                                if (showBackFaces)
                                {
                                    indices.Add(topleftPosition);
                                    indices.Add(leftPosition);
                                    indices.Add(topPosition);
                                }
                            }
                        }
                    }
                }
            }

            var normals = new System.Windows.Media.Media3D.Vector3DCollection(positions.Count);

            for (int i = 0; i < positions.Count; i++)
                normals.Add(new System.Windows.Media.Media3D.Vector3D(0, 0, 0));

            for (int i = 0; i < indices.Count; i += 3)
            {
                int index1 = indices[i];
                int index2 = indices[i + 1];
                int index3 = indices[i + 2];

                System.Windows.Media.Media3D.Vector3D side1 = positions[index1] - positions[index3];
                System.Windows.Media.Media3D.Vector3D side2 = positions[index1] - positions[index2];
                System.Windows.Media.Media3D.Vector3D normal = System.Windows.Media.Media3D.Vector3D.CrossProduct(side1, side2);

                normals[index1] += normal;
                normals[index2] += normal;
                normals[index3] += normal;
            }

            for (int i = 0; i < normals.Count; i++)
            {
                if (normals[i].Length > 0)
                {
                    var normal = normals[i];
                    normal.Normalize();

                    // the fact that this is necessary means I am doing something wrong
                    if (normal.Z < 0)
                        normal.Negate();

                    normals[i] = normal;
                }
            }

            var geometry = new System.Windows.Media.Media3D.MeshGeometry3D
            {
                Positions = positions,
                TriangleIndices = indices,
                Normals = normals
            };

            return geometry;
        }
Example #3
0
        public static System.Windows.Media.Media3D.MeshGeometry3D GenerateMesh(this PointCloudTileSource source, Grid <float> grid, Extent3D distributionExtent, bool showBackFaces)
        {
            // subtract midpoint to center around (0,0,0)
            Extent3D centeringExtent      = source.Extent;
            Point3D  centerOfMass         = source.CenterOfMass;
            double   centerOfMassMinusMin = centerOfMass.Z - centeringExtent.MinZ;

            var positions = new System.Windows.Media.Media3D.Point3DCollection(grid.CellCount);
            var indices   = new System.Windows.Media.Int32Collection(2 * (grid.SizeX - 1) * (grid.SizeY - 1));

            float fillVal = grid.FillVal;

            for (int x = 0; x < grid.SizeX; x++)
            {
                for (int y = 0; y < grid.SizeY; y++)
                {
                    double value = grid.Data[x, y] - centerOfMassMinusMin;

                    double xCoord = ((double)x / grid.SizeX) * distributionExtent.RangeX + distributionExtent.MinX - distributionExtent.MidpointX;
                    double yCoord = ((double)y / grid.SizeY) * distributionExtent.RangeY + distributionExtent.MinY - distributionExtent.MidpointY;

                    xCoord += (distributionExtent.MidpointX - centeringExtent.MidpointX);
                    yCoord += (distributionExtent.MidpointY - centeringExtent.MidpointY);

                    var point = new System.Windows.Media.Media3D.Point3D(xCoord, yCoord, value);
                    positions.Add(point);

                    if (x > 0 && y > 0)
                    {
                        // add two triangles
                        int currentPosition = x * grid.SizeY + y;
                        int topPosition     = currentPosition - 1;
                        int leftPosition    = currentPosition - grid.SizeY;
                        int topleftPosition = leftPosition - 1;

                        if (grid.Data[x - 1, y] != fillVal && grid.Data[x, y - 1] != fillVal)
                        {
                            if (grid.Data[x, y] != fillVal)
                            {
                                indices.Add(leftPosition);
                                indices.Add(topPosition);
                                indices.Add(currentPosition);

                                if (showBackFaces)
                                {
                                    indices.Add(leftPosition);
                                    indices.Add(currentPosition);
                                    indices.Add(topPosition);
                                }
                            }

                            if (grid.Data[x - 1, y - 1] != fillVal)
                            {
                                indices.Add(topleftPosition);
                                indices.Add(topPosition);
                                indices.Add(leftPosition);

                                if (showBackFaces)
                                {
                                    indices.Add(topleftPosition);
                                    indices.Add(leftPosition);
                                    indices.Add(topPosition);
                                }
                            }
                        }
                    }
                }
            }

            var normals = new System.Windows.Media.Media3D.Vector3DCollection(positions.Count);

            for (int i = 0; i < positions.Count; i++)
            {
                normals.Add(new System.Windows.Media.Media3D.Vector3D(0, 0, 0));
            }

            for (int i = 0; i < indices.Count; i += 3)
            {
                int index1 = indices[i];
                int index2 = indices[i + 1];
                int index3 = indices[i + 2];

                System.Windows.Media.Media3D.Vector3D side1  = positions[index1] - positions[index3];
                System.Windows.Media.Media3D.Vector3D side2  = positions[index1] - positions[index2];
                System.Windows.Media.Media3D.Vector3D normal = System.Windows.Media.Media3D.Vector3D.CrossProduct(side1, side2);

                normals[index1] += normal;
                normals[index2] += normal;
                normals[index3] += normal;
            }

            for (int i = 0; i < normals.Count; i++)
            {
                if (normals[i].Length > 0)
                {
                    var normal = normals[i];
                    normal.Normalize();

                    // the fact that this is necessary means I am doing something wrong
                    if (normal.Z < 0)
                    {
                        normal.Negate();
                    }

                    normals[i] = normal;
                }
            }

            var geometry = new System.Windows.Media.Media3D.MeshGeometry3D
            {
                Positions       = positions,
                TriangleIndices = indices,
                Normals         = normals
            };

            return(geometry);
        }
Example #4
0
        private void ProcessingThread()
        {
            // Start AcquireFrame/ReleaseFrame loop
            while (senseManager.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                // Acquire the color image data
                PXCMCapture.Sample  sample = senseManager.QuerySample();
                Bitmap              colorBitmap;
                PXCMImage.ImageData colorData;
                sample.color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24, out colorData);
                colorBitmap = colorData.ToBitmap(0, sample.color.info.width, sample.color.info.height);

                // Get face data
                if (faceData != null)
                {
                    faceData.Update();
                    numFacesDetected = faceData.QueryNumberOfDetectedFaces();

                    if (numFacesDetected > 0)
                    {
                        // Get the first face detected (index 0)
                        PXCMFaceData.Face face = faceData.QueryFaceByIndex(0);
                        face.QueryExpressions();
                        PXCMFaceData.PoseData poseData = face.QueryPose();
                        //  PXCMPoint3DF32 outHeadPosition = new PXCMPoint3DF32(); //F200 has added confidence into struct
                        PXCMFaceData.HeadPosition outHeadPosition = new PXCMFaceData.HeadPosition();

                        //processing the head pose data to find the head center position
                        poseData.QueryHeadPosition(out outHeadPosition);
                        System.Windows.Media.Media3D.Point3DCollection points = new System.Windows.Media.Media3D.Point3DCollection();
                        points.Add(new System.Windows.Media.Media3D.Point3D(outHeadPosition.headCenter.x,
                                                                            outHeadPosition.headCenter.y, outHeadPosition.headCenter.z));

                        Console.WriteLine("head center position: " + points);
                        // poseData.QueryHeadPosition(out outHeadPosition);
                        PXCMFaceData.PoseEulerAngles outPoseEulerAngles = new PXCMFaceData.PoseEulerAngles();
                        poseData.QueryPoseAngles(out outPoseEulerAngles);
                        roll  = (int)outPoseEulerAngles.roll;
                        pitch = (int)outPoseEulerAngles.pitch;
                        yaw   = (int)outPoseEulerAngles.yaw;
                        // PXCMFaceData.LandmarkType.LANDMARK_EYE_LEFT_CENTER what can I do with this?
                        if (pitch + 12 > 10)
                        {
                            headUp = true;
                        }
                        else
                        {
                            headUp = false;
                        }
                        if (pitch < -10)
                        {
                            headDown = true;
                        }
                        else
                        {
                            headDown = false;
                        }
                        if (roll > 5)
                        {
                            headTiltLeft = true;
                        }
                        else
                        {
                            headTiltLeft = false;
                        }
                        if (roll < -5)
                        {
                            headTiltRight = true;
                        }
                        else
                        {
                            headTiltRight = false;
                        }
                        if (yaw > 5)
                        {
                            headTurnLeft = true;
                        }
                        else
                        {
                            headTurnLeft = false;
                        }
                        if (yaw < -5)
                        {
                            headTurnRight = true;
                        }
                        else
                        {
                            headTurnRight = false;
                        }

                        //Console.WriteLine("Rotation: " + outPoseEulerAngles.roll + " " + outPoseEulerAngles.pitch + " " + outPoseEulerAngles.yaw);
                        PXCMFaceData.ExpressionsData edata = face.QueryExpressions();
                        // retrieve the expression information
                        PXCMFaceData.ExpressionsData.FaceExpressionResult smileScore;
                        PXCMFaceData.ExpressionsData.FaceExpressionResult eyesUpScore;
                        PXCMFaceData.ExpressionsData.FaceExpressionResult eyesDownScore;
                        PXCMFaceData.ExpressionsData.FaceExpressionResult eyesTurnLeftScore;
                        PXCMFaceData.ExpressionsData.FaceExpressionResult eyesTurnRightScore;
                        PXCMFaceData.ExpressionsData.FaceExpressionResult headTiltedLeftScore;
                        PXCMFaceData.ExpressionsData.FaceExpressionResult headTurnedLeftScore;
                        // PXCMFaceData.ExpressionsData.FaceExpressionResult headUpScore;
                        //PXCMFaceData.ExpressionsData.FaceExpressionResult headDownScore;
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_SMILE, out smileScore);
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_UP, out eyesUpScore);
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_DOWN, out eyesDownScore);
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_TURN_LEFT, out eyesTurnLeftScore);
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_TURN_RIGHT, out eyesTurnRightScore);
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_HEAD_TILT_LEFT, out headTiltedLeftScore);
                        edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_HEAD_TURN_LEFT, out headTurnedLeftScore);
                        // edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_HEAD_UP, out headUpScore);
                        //edata.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_HEAD_DOWN, out headDownScore);
                        smile  = smileScore.intensity;
                        eyesUp = eyesUpScore.intensity;
                        if (eyesUp == 100)
                        {
                            eyeIsUp = true;
                        }
                        else
                        {
                            eyeIsUp = false;
                        }
                        eyesDown = eyesDownScore.intensity;
                        if (eyesDown == 100)
                        {
                            eyeIsDown = true;
                        }
                        else
                        {
                            eyeIsDown = false;
                        }

                        eyesTurnLeft  = eyesTurnLeftScore.intensity;
                        eyesTurnRight = eyesTurnRightScore.intensity;
                        //  headTiltLeft = headTiltedLeftScore.intensity;
                        // headTurnLeft= headTurnedLeftScore.intensity;
                        // headUp = headUpScore.intensity;
                        //headDown = headDownScore.intensity;
                        PXCMCapture.Device device = senseManager.captureManager.device;
                        device.SetIVCAMAccuracy(PXCMCapture.Device.IVCAMAccuracy.IVCAM_ACCURACY_FINEST);
                        // eyeIsUP= CheckFaceExpression(edata, FaceExpression.EXPRESSION_EYES_UP, 15);
                        if ((headTiltLeft | headTurnLeft) & headUp & (eyesTurnLeft == 100) & (!eyeIsDown))
                        {
                            looksForward = true;
                        }

                        else if ((headTiltRight | headTurnRight) & headUp & (eyesTurnRight == 100) & (!eyeIsDown))
                        {
                            looksForward = true;
                        }


                        /* else if (headTiltRight & (headDown|headUp) & (!headTurnRight) & (eyesTurnRight==100))
                         *   looksForward = true;
                         * else if (headTiltLeft & (headDown|headUp) &  (!headTurnLeft) & (eyesTurnLeft == 100))
                         *   looksForward = true;
                         * */
                        else
                        {
                            looksForward = eyeIsUp;
                        }
                        //  headTiltLeftThreshold = CheckFaceExpression(edata, FaceExpression.EXPRESSION_HEAD_TILT_LEFT, 15);

                        //csv mona
                        // var csv = new StringBuilder();
                        // outputs 10:00 PM
                        //    var newLine = string.Format("{0},{1},{2},{3},{4}{5}", DateTime.Now.ToString("dd-MM-yyyy-hh:mm:ss:fff"), roll, pitch, yaw, eyesUp, Environment.NewLine);
                        //     csv.Append(newLine);
                        // string pathString = System.IO.Path.Combine(filePath, fileName);

                        //   File.AppendAllText(pathString, csv.ToString());



                        // Retrieve face location data
                        PXCMFaceData.DetectionData faceDetectionData = face.QueryDetection();
                        if (faceDetectionData != null)
                        {
                            PXCMRectI32 faceRectangle;
                            faceDetectionData.QueryBoundingRect(out faceRectangle);
                            faceRectangleHeight = faceRectangle.h;
                            faceRectangleWidth  = faceRectangle.w;
                            faceRectangleX      = faceRectangle.x;
                            faceRectangleY      = faceRectangle.y;
                        }


                        // Process face recognition data
                        if (face != null)
                        {
                            // Retrieve the recognition data instance
                            recognitionData = face.QueryRecognition();

                            // Set the user ID and process register/unregister logic
                            if (recognitionData.IsRegistered())
                            {
                                userId = Convert.ToString(recognitionData.QueryUserID());

                                if (doUnregister)
                                {
                                    recognitionData.UnregisterUser();
                                    doUnregister = false;
                                }
                            }
                            else
                            {
                                if (doRegister)
                                {
                                    recognitionData.RegisterUser();

                                    // Capture a jpg image of registered user
                                    colorBitmap.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                                    doRegister = false;
                                }
                                else
                                {
                                    userId = "Unrecognized";
                                }
                            }
                        }
                    }
                    else
                    {
                        userId = "No users in view";
                    }
                }

                // Display the color stream and other UI elements
                UpdateUI(colorBitmap);

                // Release resources
                colorBitmap.Dispose();
                sample.color.ReleaseAccess(colorData);
                sample.color.Dispose();

                // Release the frame
                senseManager.ReleaseFrame();
            }
        }