// Update is called once per frame void Update() { if (!mJointGenerator || !mElbowObject || !mHandObject || !mWebcamCamera) { return; } // work out the best joint... IList <TJoint> Joints = mJointGenerator.mJoints; TJoint joint = new TJoint(); // if no joints, make up dummy one if (Joints.Count == 0) { return; } mJointLooper = (mJointLooper + 1) % Joints.Count; joint = Joints [mJointLooper]; // use camera to make 3D positions Vector3 MiddleScreen = new Vector3(joint.mMiddle.x * mWebcamCamera.pixelWidth, joint.mMiddle.y * mWebcamCamera.pixelHeight, MiddleDepth); Vector3 EndScreen = new Vector3(joint.mEnd.x * mWebcamCamera.pixelWidth, joint.mEnd.y * mWebcamCamera.pixelHeight, EndDepth); Vector3 Middle = mWebcamCamera.ScreenToWorldPoint(MiddleScreen); Vector3 End = mWebcamCamera.ScreenToWorldPoint(EndScreen); mElbowObject.position = Middle; mHandObject.position = End; }
public void Read(BinaryReader reader) { m_olJoints = new List <TJoint>(); m_olBitmaps = new List <Bitmap>(); m_nDrawMode = reader.ReadInt32(); m_sTension = reader.ReadSingle(); m_nCurveWidth = reader.ReadInt32(); m_nJointCount = reader.ReadInt32(); int nCount = reader.ReadInt32(); for (int i = 0; i < nCount; i++) { TJoint joint = new TJoint(); joint.Read(reader); m_olJoints.Add(joint); } m_nPoseCount = reader.ReadInt32(); m_sAlpha = 1.0f - (m_nPoseCount / 255.0f); m_nBitmapCount = reader.ReadInt32(); for (int f = 0; f < m_nBitmapCount; f++) { Bitmap bmp = FileFormat.LoadBitmap(reader); m_olBitmaps.Add(bmp); } }
/// <summary> /// Draws a bone line between two joints /// </summary> /// <param name="skeleton">skeleton to draw bones from</param> /// <param name="drawingContext">drawing context to draw to</param> /// <param name="jointType0">joint to start drawing from</param> /// <param name="jointType1">joint to end drawing at</param> public static void DrawBone(TSkeleton skeleton, DrawingContext drawingContext, TJointType jointType0, TJointType jointType1) { TJoint joint0 = skeleton.Joints[(int)jointType0]; TJoint joint1 = skeleton.Joints[(int)jointType1]; // If we can't find either of these joints, exit if (joint0.TrackingState == TJointTrackingState.NotTracked || joint1.TrackingState == TJointTrackingState.NotTracked) { return; } // Don't draw if both points are inferred if (joint0.TrackingState == TJointTrackingState.Inferred && joint1.TrackingState == TJointTrackingState.Inferred) { return; } // We assume all drawn bones are inferred unless BOTH joints are tracked Pen drawPen = inferredBonePen; if (joint0.TrackingState == TJointTrackingState.Tracked && joint1.TrackingState == TJointTrackingState.Tracked) { drawPen = trackedBonePen; } drawingContext.DrawLine(drawPen, SkeletonPointToScreen(joint0.Position), SkeletonPointToScreen(joint1.Position)); }
private void NavigateByBothHands(TJoint lastLeftHandFrame, TJoint lastRightHandFrame, List <Frame> frames) { var distance = CalculateDistance(lastLeftHandFrame, lastRightHandFrame); ZoomSum += distance - LastHandsDistance; if (ZoomSum > Configuration.minimalHandDistanceChanged) { var transitionVector = new Vector3() { X = ZoomSum }; //LogString.Log("Hand: " + Kinect.Toolbox.Utils.Extensions.Print(jointPos.Position)); Raise(() => RaiseEvent(frames, transitionVector, -1, ControlType.BOTH_HANDS)); ZoomSum = 0; } else if (ZoomSum < -Configuration.minimalHandDistanceChanged) { var transitionVector = new Vector3() { X = ZoomSum }; //LogString.Log("Hand: " + Kinect.Toolbox.Utils.Extensions.Print(jointPos.Position)); Raise(() => RaiseEvent(frames, transitionVector, -1, ControlType.BOTH_HANDS)); ZoomSum = 0; } LastHandsDistance = distance; }
private void CompareJoint(TJoint joint1, TJoint joint2) { Assert.True(joint1.JointType == joint2.JointType); CompareSkeletonPoints(joint1.Position, joint2.Position); Assert.True(joint1.TrackingState == joint2.TrackingState); }
public void UpdateSettInfo() { if (nMain.currObj != null) { TJoint jointcomp = nMain.currObj.GetComponent <TJoint>(); AXInp.GetComponent <UnityEngine.UI.InputField>().text = jointcomp.comp.anchor.x.ToString(); AYInp.GetComponent <UnityEngine.UI.InputField>().text = jointcomp.comp.anchor.y.ToString(); } }
private float CalculateDistance(TJoint lastLeftHandFrame, TJoint lastRightHandFrame) { var transitionLeftX = lastLeftHandFrame.Position.X - LeftLastPosition.X; var transitionLeftY = lastLeftHandFrame.Position.Y - LeftLastPosition.Y; var transitionRightX = lastRightHandFrame.Position.X - RightLastPosition.X; var transitionRightY = lastRightHandFrame.Position.Y - RightLastPosition.Y; var distance = (float)Math.Sqrt((transitionRightX - transitionLeftX) * (transitionRightX - transitionLeftX) + (transitionRightY - transitionLeftY) * (transitionRightY - transitionLeftY)); return(distance); }
private void NavigateByLeftHand(TJoint lastLeftHandFrame, List <Frame> frames) { var transitionX = lastLeftHandFrame.Position.X - LeftLastPosition.X; var transitionY = lastLeftHandFrame.Position.Y - LeftLastPosition.Y; var transitionVector = new Vector3() { X = transitionX, Y = transitionY }; Raise(() => RaiseEvent(frames, transitionVector, 0, ControlType.LEFT_HAND)); LeftLastPosition = lastLeftHandFrame.Position; }
private void NavigateByRightHand(TJoint lastRightHandFrame, List <Frame> frames) { var transitionX = lastRightHandFrame.Position.X - RightLastPosition.X; var transitionY = lastRightHandFrame.Position.Y - RightLastPosition.Y; var transitionVector = new Vector3() { X = -transitionX, Y = -transitionY }; Raise(() => RaiseEvent(frames, transitionVector, 0, ControlType.RIGHT_HAND)); RightLastPosition = lastRightHandFrame.Position; }
// Update is called once per frame void Update() { // yield return new WaitForEndOfFrame(); if (mMaskTexture == null) { return; } if (mJointCalculator == null) { mJointCalculator = new TJointCalculator(); } if (mDebugJoint) { TJoint joint = new TJoint(); joint.mStart = new float2(0, 0); joint.mMiddle = new float2(0.2f, 0.2f); joint.mEnd = new float2(0.4f, 0.5f); mJoints = new List <TJoint> (); mJoints.Add(joint); } else { mJoints = mJointCalculator.CalculateJoints(ref mDebug, mMaskTexture, mRayTexture, mRayMaterial, mSecondJointTexture, mSecondJointMaterial, mMaxColumnTest, mReadBackFormat, mMinJointLength); if (mBestJointOnly) { // filter out best int Best = 0; for (int i = 1; i < mJoints.Count; i++) { if (mJoints[i].Length() > mJoints[Best].Length()) { Best = i; } } if (mJoints.Count > 0) { TJoint joint = mJoints[Best]; mJoints.Clear(); mJoints.Add(joint); } } } }
private TSkeleton GetSkeleton() { var joints = new TJoint[10]; for (int i = 0; i < 10; i++) { joints[i] = GetJoint(); } return(new TSkeleton() { ClippedEdges = TFrameEdges.Left, Position = GetSkeletonPoint(), TrackingId = new Random().Next(), TrackingState = TSkeletonTrackingState.Tracked, Joints = joints }); }
public void Read(BinaryReader reader) { int f; TJoint pChild; int nCount; m_olChildren = new List <TJoint>(); m_nDrawAs = reader.ReadInt32(); m_nDrawWidth = reader.ReadInt32(); m_nLineWidth = reader.ReadInt32(); m_bShowLine = reader.ReadBoolean(); byte[] color = new byte[] { reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte() }; m_nColor = Functions.Helpers.ODDCOLORtoCOLOR(color); byte[] inColor = new byte[] { reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte() }; m_nInColor = Functions.Helpers.ODDCOLORtoCOLOR(inColor); m_bFill = reader.ReadBoolean(); m_nX = reader.ReadInt32(); m_nY = reader.ReadInt32(); m_nState = reader.ReadInt32(); m_nLength = reader.ReadInt32(); m_nIndex = reader.ReadInt32(); m_sAngleToParent = reader.ReadSingle(); m_nBitmap = reader.ReadInt32(); m_nBMPXoffs = reader.ReadInt32(); m_nBMPYoofs = reader.ReadInt32(); m_sBitmapRotation = reader.ReadSingle(); m_nBitmapAlpha = reader.ReadByte(); nCount = reader.ReadInt32(); for (f = 0; f < nCount; f++) { pChild = new TJoint(); pChild.m_pParent = this; pChild.Read(reader); m_olChildren.Add(pChild); } }
public static void DrawJoint(TJoint joint, Rect ScreenRect) { Vector2 Start = new Vector2(joint.mStart.x, joint.mStart.y); Vector2 Middle = new Vector2(joint.mMiddle.x, joint.mMiddle.y); Vector2 RayEnd = new Vector2(joint.mRayEnd.x, joint.mRayEnd.y); Vector2 End = new Vector2(joint.mEnd.x, joint.mEnd.y); Vector2 Left = End + new Vector2(joint.mRadLeft.x, joint.mRadLeft.y); Vector2 Right = End + new Vector2(joint.mRadRight.x, joint.mRadRight.y); float Radius = joint.mEndRadius; FitToRect(ref Start, ScreenRect); FitToRect(ref Middle, ScreenRect); FitToRect(ref RayEnd, ScreenRect); FitToRect(ref End, ScreenRect); FitToRect(ref Left, ScreenRect); FitToRect(ref Right, ScreenRect); FitToRect(ref Radius, ScreenRect); GuiHelper.DrawLine(Start, Middle, Color.red); GuiHelper.DrawLine(Middle, RayEnd, Color.green); GuiHelper.DrawCircle(End, Radius, Color.magenta); GuiHelper.DrawLine(End, Left, Color.blue); GuiHelper.DrawLine(End, Right, Color.blue); }
public static void DrawJoint(TJoint joint, int ScreenSectionX, int ScreenSectionY) { Rect rect = GetScreenRect(ScreenSectionX, ScreenSectionY); DrawJoint(joint, rect); }
List <TJoint> CalculateJoints(ref string DebugOut, Color32[] SecondJointPixels, Texture SecondJointTexture, Texture MaskTexture, int MaxJointLength, float AngleDegMin, float AngleDegMax, int MaxColumnTest, int MinJointLength, Material ParamsMaterial) { //int RayPad = ParamsMaterial.GetInt ("RayPad"); int MaxJoints = 400; List <TJoint> Joints = new List <TJoint> (); // gr: verify w/h agains pixels size? int PixelCount = SecondJointPixels.Length; if (PixelCount % SecondJointTexture.width != 0) { return(Joints); } // should match mSecondJointTexture.height int PixelsWidth = SecondJointTexture.width; int PixelsHeight = PixelCount / PixelsWidth; MaxColumnTest = Mathf.Min(SecondJointTexture.width, MaxColumnTest); List <int> Columns = new List <int> (); for (int c = 0; c < MaxColumnTest; c++) { float xf = (c / (float)MaxColumnTest) * SecondJointTexture.width; int x = Mathf.FloorToInt(xf); // don't add dupes if (Columns.Count > 0) { if (Columns [Columns.Count - 1] == x) { continue; } } Columns.Add(x); } DebugOut += SecondJointPixels [0 + (0 * PixelsWidth)] + "\n"; DebugOut += SecondJointPixels [0 + ((PixelsHeight / 2) * PixelsWidth)] + "\n"; DebugOut += SecondJointPixels [(10) + ((PixelsHeight - 1) * PixelsWidth)] + "\n"; // gr: could probbaly be more cache friendly by approaching this row-by-row... for (int xi = 0; xi < Columns.Count; xi++) { int x = Columns[xi]; Color32 ColPixel = SecondJointPixels[x]; // height is the same for each angle so we can skip that quick float Height = (ColPixel.a / 255.0f) * PixelsHeight; if (Height < 1) { continue; } // get the longest joint for this X float BestJointLength = 0.0f; int BestJointAng = -1; for (int angstep = 0; angstep < PixelsHeight; angstep++) { int p = x + (angstep * PixelsWidth); Color32 Pixel = SecondJointPixels[p]; int pixr = Pixel.r; // float AngleDeg = Mathf.Lerp( AngleDegMin, AngleDegMax, (float)angstep / (float)PixelsHeight ); float JointLength = (pixr / 255.0f) * MaxJointLength; if (JointLength < BestJointLength) { continue; } BestJointLength = JointLength; BestJointAng = angstep; } if (BestJointAng < 0) { continue; } if (BestJointLength < MinJointLength) { continue; } { int angstep = BestJointAng; int p = x + (angstep * PixelsWidth); Color32 Pixel = SecondJointPixels[p]; float AngleDeg = Mathf.Lerp(AngleDegMin, AngleDegMax, (float)angstep / (float)PixelsHeight); float JointLength = ((float)Pixel.r / 255.0f) * MaxJointLength; float PanLength = ((float)Pixel.g / 255.0f) * MaxJointLength; float RadiusLength = ((float)Pixel.b / 255.0f) * MaxJointLength; RadiusLength = Mathf.Max(1, RadiusLength); // gr: something wrong in this calc? half seems to look right // texture->texture scale // gr: maybe just UVscalar is wrong? JointLength /= MaskTexture.width / SecondJointTexture.width; RadiusLength /= MaskTexture.width / SecondJointTexture.width; PanLength /= MaskTexture.width / SecondJointTexture.width; float AngleRad = radians(AngleDeg); Vector2 AngleVector = new Vector2(Mathf.Sin(AngleRad), Mathf.Cos(AngleRad)); AngleVector.Normalize(); Vector2 LeftVector = new Vector2(AngleVector.y, -AngleVector.x); Vector2 RightVector = new Vector2(-AngleVector.y, AngleVector.x); // in order to center the circle, we want to move it back a bit. we know it's already <PAD> back, but we might want to go further in either direction // basically pad needs to become radius //float Diff = RadiusLength - RayPad; float Diff = 0; AngleVector *= JointLength + Diff; Vector2 PanVector = new Vector2(LeftVector.x, LeftVector.y); PanVector *= PanLength; LeftVector *= RadiusLength - PanLength; RightVector *= RadiusLength + PanLength; float2 UvScalar = new float2(1.0f / SecondJointTexture.width, 1.0f / SecondJointTexture.height); TJoint joint = new TJoint(); joint.mStart = new float2(x * UvScalar.x, 0); joint.mMiddle = new float2(x * UvScalar.x, Height * UvScalar.y); joint.mRayEnd = new float2(joint.mMiddle.x + (AngleVector.x * UvScalar.x), joint.mMiddle.y + (AngleVector.y * UvScalar.y)); joint.mEnd = new float2(joint.mRayEnd.x + (PanVector.x * UvScalar.x), joint.mRayEnd.y + (PanVector.y * UvScalar.y)); joint.mEndRadius = RadiusLength * UvScalar.x; joint.mRadLeft = new float2(LeftVector.x * UvScalar.x, LeftVector.y * UvScalar.y); joint.mRadRight = new float2(RightVector.x * UvScalar.x, RightVector.y * UvScalar.y); Joints.Add(joint); if (Joints.Count >= MaxJoints) { return(Joints); } } } return(Joints); }