private HandType ActiveHand(DepthPoint leftHand, DepthPoint rightHand)
 {
     if (leftHand.Z > rightHand.Z)
     {
         return(HandType.Left);
     }
     return(HandType.Right);
 }
    void Restock()
    {
        float now = Time.time;
        MeshSection section = new MeshSection();

        if(startMeshLink)
        {
            Vector3 startPos = startObject.transform.position;
            startPos.x =  (startPos.x+(startObject.transform.localScale.x/2));
            startPos.y =  (0);
            section.point = startPos;
            if (alwaysUp)
                section.upDir = Vector3.up;
            else
                section.upDir = transform.TransformDirection(Vector3.up);
            section.time = now;
            section.heightOverride = startingHeight;
            section.usesOverride = true;
            sections.Add(section);
            startMeshLink = false;
        }
        else
        {
            section.point = position;
            if (alwaysUp)
                section.upDir = Vector3.up;
            else
                section.upDir = transform.TransformDirection(Vector3.up);
            section.time = now;
            if(useManualHeightOverride)
            {
                section.usesOverride = true;
                section.heightOverride = manualHeightOverride;
            }
            else
            {
                section.usesOverride = false;
            }
            sections.Add(section);
        }

        xSpacing = randomizer.Next(spacingMin,spacingMax);
        position.x += xSpacing;

        DepthPoint dp1 = new DepthPoint();
        DepthPoint dp2 = new DepthPoint();

        if(sections.Count>4)
        {
            dp1.point = transform.position;
            dp2.point = transform.position;
            dp2.point.z = extrusionWidth;
            extrudeDirection = dp1.point+dp2.point;
            depthPoints.Clear();
            depthPoints.Add(dp1);
            depthPoints.Add(dp2);
            extrudeRotation = Quaternion.LookRotation(extrudeDirection,Vector3.up);
        }
        else
        {
            dp1.point = sections[0].point;
            dp2.point = sections[0].point;
            dp2.point.z = extrusionWidth;
            extrudeDirection = dp1.point-dp2.point;
            depthPoints.Clear();
            depthPoints.Add(dp1);
            depthPoints.Add(dp2);
            extrudeRotation = Quaternion.LookRotation(extrudeDirection,Vector3.up);
        }
    }
Example #3
0
        /// <summary>
        /// Factory method for creating and reusing Trackable references from native handles.
        /// </summary>
        /// <param name="nativeHandle">A native handle to a plane that has been acquired.  RELEASE
        /// WILL BE HANDLED BY THIS METHOD.</param>
        /// <returns>A reference to the Trackable. May return <c>null</c> on error or if Trackable
        /// is not handled.</returns>
        public Trackable TrackableFactory(IntPtr nativeHandle)
        {
            if (nativeHandle == IntPtr.Zero)
            {
                return(null);
            }

            Trackable result;

            if (_trackableDict.TryGetValue(nativeHandle, out result))
            {
                // Release aquired handle and return cached result.
                _nativeSession.TrackableApi.Release(nativeHandle);
                return(result);
            }

            // This block needs to construct classes marked Obsolete since those versions are always
            // the most derived type.
#pragma warning disable 618 // Obsolete warning
            ApiTrackableType trackableType = _nativeSession.TrackableApi.GetType(nativeHandle);
            if (trackableType == ApiTrackableType.Plane)
            {
                result = new TrackedPlane(nativeHandle, _nativeSession);
            }
            else if (trackableType == ApiTrackableType.Point)
            {
                result = new TrackedPoint(nativeHandle, _nativeSession);
            }
            else if (trackableType == ApiTrackableType.InstantPlacementPoint)
            {
                result = new InstantPlacementPoint(nativeHandle, _nativeSession);
            }
            else if (trackableType == ApiTrackableType.AugmentedImage)
            {
                result = new AugmentedImage(nativeHandle, _nativeSession);
            }
            else if (trackableType == ApiTrackableType.AugmentedFace)
            {
                result = new AugmentedFace(nativeHandle, _nativeSession);
            }
            else if (trackableType == ApiTrackableType.DepthPoint)
            {
                result = new DepthPoint(nativeHandle, _nativeSession);
            }
            else if (ExperimentManager.Instance.IsManagingTrackableType((int)trackableType))
            {
                result =
                    ExperimentManager.Instance.TrackableFactory((int)trackableType, nativeHandle);
            }
            else
            {
                Debug.LogWarning(
                    "TrackableFactory::No constructor for requested trackable type.");
            }
#pragma warning restore 618

            if (result != null)
            {
                _trackableDict.Add(nativeHandle, result);
            }

            return(result);
        }
        private void DrawHand(HandType activeHand, HandType handType, HandState handState, DepthPoint handPosition, DrawingContext drawingContext)
        {
            var xInMillimeters = handPosition.X;
            var yInMillimeters = handPosition.Y;

            if (_calibrating)
            {
                if (handType == HandType.Right && _calibratingTopLeft)
                {
                    ScreenTopLeft = new Point(xInMillimeters, yInMillimeters);
                    _zLeft        = handPosition.Z;
                }
                else if (handType == HandType.Left && _calibratingBottomRight)
                {
                    ScreenBottomRight = new Point(xInMillimeters, yInMillimeters);
                    _zRight           = handPosition.Z;
                }
            }

            if (_calibrating)
            {
                drawingContext.DrawEllipse(_handOpenBrush, null, handPosition.GetPoint(), HandSize, HandSize);
            }
            else if (_doneCalibrating)
            {
                if (handType == activeHand)
                {
                    var x = PixelHelper.ToPixelsX(xInMillimeters, this);
                    var y = PixelHelper.ToPixelsY(yInMillimeters, this);

                    drawingContext.DrawEllipse(_handOpenBrush, null, handPosition.GetPoint(), HandSize, HandSize);

                    Task.Run(() =>
                    {
                        avgX.Add(x);
                        avgY.Add(y);

                        if (avgX.Count > MouseSensitivity)
                        {
                            avgX.RemoveAt(0);
                            avgY.RemoveAt(0);
                        }

                        var xFinal = avgX.Average();
                        var yFinal = avgY.Average();

                        Dispatcher.BeginInvoke(new Action(() => SetCursorPos(Convert.ToInt32(xFinal), Convert.ToInt32(yFinal))));
                    });

                    if (handState == HandState.Closed)
                    {
                        LeftDown();
                    }
                    else
                    {
                        LeftRelease();
                    }
                }
                else
                {
                    drawingContext.DrawEllipse(_handOpenBrush, null, handPosition.GetPoint(), HandSize, HandSize);
                }
            }
            else if (handType == HandType.Right)
            {
                drawingContext.DrawEllipse(_handOpenBrush, null, handPosition.GetPoint(), HandSize, HandSize);
            }
        }
        /// <summary>
        /// Handles the body frame data arriving from the sensor
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            bool dataReceived = false;

            using (var bodyFrame = e.FrameReference.AcquireFrame())
            {
                if (bodyFrame != null)
                {
                    if (_bodies == null)
                    {
                        _bodies = new Body[bodyFrame.BodyCount];
                    }

                    // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
                    // As long as those body objects are not disposed and not set to null in the array,
                    // those body objects will be re-used.
                    bodyFrame.GetAndRefreshBodyData(_bodies);
                    dataReceived = true;
                }
            }

            if (dataReceived)
            {
                using (var dc = _drawingGroup.Open())
                {
                    // Draw a transparent background to set the render size
                    dc.DrawRectangle(Brushes.Transparent, null, new Rect(0.0, 0.0, _displayWidth, _displayHeight));

                    foreach (var body in _bodies)
                    {
                        if (body.IsTracked)
                        {
                            DrawClippedEdges(body, dc);

                            var joints = body.Joints;

                            // convert the joint points to depth (display) space
                            var jointPoints = new Dictionary <JointType, DepthPoint>();

                            foreach (var jointType in joints.Keys)
                            {
                                // sometimes the depth(Z) of an inferred joint may show as negative
                                // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
                                var position = joints[jointType].Position;
                                if (position.Z < 0)
                                {
                                    position.Z = InferredZPositionClamp;
                                }

                                var depthSpacePoint = _coordinateMapper.MapCameraPointToDepthSpace(position);
                                jointPoints[jointType] = new DepthPoint(depthSpacePoint.X, depthSpacePoint.Y, (position.Z * 1000));
                            }

                            var activeHand = ActiveHand(jointPoints[JointType.HandTipLeft],
                                                        jointPoints[JointType.HandTipRight]);
                            DrawHand(activeHand, HandType.Left, body.HandLeftState, jointPoints[JointType.HandTipLeft], dc);
                            DrawHand(activeHand, HandType.Right, body.HandRightState, jointPoints[JointType.HandTipRight], dc);
                        }
                    }

                    // prevent drawing outside of our render area
                    _drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, _displayWidth, _displayHeight));
                }
            }
        }
    void Restock()
    {
        float       now     = Time.time;
        MeshSection section = new MeshSection();

        if (startMeshLink)
        {
            Vector3 startPos = startObject.transform.position;
            startPos.x    = (startPos.x + (startObject.transform.localScale.x / 2));
            startPos.y    = (0);
            section.point = startPos;
            if (alwaysUp)
            {
                section.upDir = Vector3.up;
            }
            else
            {
                section.upDir = transform.TransformDirection(Vector3.up);
            }
            section.time           = now;
            section.heightOverride = startingHeight;
            section.usesOverride   = true;
            sections.Add(section);
            startMeshLink = false;
        }
        else
        {
            section.point = position;
            if (alwaysUp)
            {
                section.upDir = Vector3.up;
            }
            else
            {
                section.upDir = transform.TransformDirection(Vector3.up);
            }
            section.time = now;
            if (useManualHeightOverride)
            {
                section.usesOverride   = true;
                section.heightOverride = manualHeightOverride;
            }
            else
            {
                section.usesOverride = false;
            }
            sections.Add(section);
        }


        xSpacing    = randomizer.Next(spacingMin, spacingMax);
        position.x += xSpacing;


        DepthPoint dp1 = new DepthPoint();
        DepthPoint dp2 = new DepthPoint();

        if (sections.Count > 4)
        {
            dp1.point        = transform.position;
            dp2.point        = transform.position;
            dp2.point.z      = extrusionWidth;
            extrudeDirection = dp1.point + dp2.point;
            depthPoints.Clear();
            depthPoints.Add(dp1);
            depthPoints.Add(dp2);
            extrudeRotation = Quaternion.LookRotation(extrudeDirection, Vector3.up);
        }
        else
        {
            dp1.point        = sections[0].point;
            dp2.point        = sections[0].point;
            dp2.point.z      = extrusionWidth;
            extrudeDirection = dp1.point - dp2.point;
            depthPoints.Clear();
            depthPoints.Add(dp1);
            depthPoints.Add(dp2);
            extrudeRotation = Quaternion.LookRotation(extrudeDirection, Vector3.up);
        }
    }