public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jObject = JObject.Load(reader); Model.LeapMotion.Hand modelHand = new Model.LeapMotion.Hand(); string localPositionString = jObject["LocalPosition"].ToString().TrimStart('<').TrimEnd('>'); string[] localPositions = localPositionString.Split(','); modelHand.LocalPosition = new Vector3( float.Parse(localPositions[0]), float.Parse(localPositions[1]), float.Parse(localPositions[2]) ); IList <Finger> fingers = new List <Finger>(); serializer.Converters.Add(new FingerTypeConverter()); foreach (var child in jObject["Fingers"]) { Finger finger = child.ToObject <Finger>(serializer); fingers.Add(finger); } modelHand.Fingers = fingers; modelHand.Side = (ESideType)Enum.Parse(typeof(ESideType), jObject["Side"].ToString()); modelHand.BodyType = (Body.EBodyType)Enum.Parse(typeof(Body.EBodyType), jObject["BodyType"].ToString()); IList <Point> points = new List <Point>(); serializer.Converters.Add(new PointTypeConverter()); foreach (var child in jObject["Points"]) { Point point = child.ToObject <Point>(serializer); points.Add(point); } modelHand.Points = points; string certroidString = jObject["Centroid"].ToString().TrimStart('<').TrimEnd('>'); string[] certroids = certroidString.Split(','); modelHand.Centroid = new Vector3( float.Parse(certroids[0]), float.Parse(certroids[1]), float.Parse(certroids[2]) ); float w = 0.0F; // There is now float w in logging data. So set the default value of 0.0 modelHand.Rotation = new Quaternion(modelHand.Centroid, w); return(modelHand); }
/** * Method to handle frame events */ public void OnFrame(object sender, FrameEventArgs args) { // Get the most recent frame and report some basic information Frame frame = args.frame; Logger.Debug("Frame available - frame id: {0}, hands: {1}", frame.Id, frame.Hands.Count); // Create Mocap Frame to send to server later VinteR.Model.MocapFrame mocapFrame = new VinteR.Model.MocapFrame(adapter.Config.Name, adapter.Config.AdapterType); foreach (Leap.Hand hand in frame.Hands) { Logger.Debug("Hand id: {0}, fingers: {1}", hand.Id, hand.Fingers.Count); // Get the Arm bone // Arm arm = hand.Arm; // Logger.Debug("Arm direction: {0}, wrist position: {1}, elbow position: {2}", arm.Direction, arm.WristPosition, arm.ElbowPosition); // Get fingers and bones IList <Model.LeapMotion.Finger> fingers = new List <Model.LeapMotion.Finger>(); foreach (Leap.Finger finger in hand.Fingers) { Model.LeapMotion.Finger modelFinger = new Model.LeapMotion.Finger(getFingerType(finger.Type.ToString())); IList <Model.LeapMotion.FingerBone> fingerBones = new List <Model.LeapMotion.FingerBone>(); Logger.Debug("Finger {0}: length: {1}mm, width: {2}mm, fingertipPosition: {3}", finger.Type.ToString(), finger.Length, finger.Width, finger.TipPosition); // Get finger bones Bone bone; for (int b = 0; b < 4; b++) { bone = finger.Bone((Bone.BoneType)b); Model.LeapMotion.FingerBone modelBone = new Model.LeapMotion.FingerBone(getFingerBoneType(bone.Type.ToString())); modelBone.LocalStartPosition = new System.Numerics.Vector3(bone.PrevJoint.x, bone.PrevJoint.y, bone.PrevJoint.z); modelBone.LocalEndPosition = new System.Numerics.Vector3(bone.NextJoint.x, bone.NextJoint.y, bone.NextJoint.z); fingerBones.Add(modelBone); Logger.Debug("Finger Bone: {0}, start: {1}, end: {2}", bone.Type.ToString(), bone.PrevJoint, bone.NextJoint); } modelFinger.Bones = fingerBones; fingers.Add(modelFinger); } // Get the hand's normal vector and direction Vector normal = hand.PalmNormal; Vector direction = hand.Direction; float pitch = direction.Pitch * 180.0f / (float)Math.PI; float roll = normal.Roll * 180.0f / (float)Math.PI; float yaw = direction.Yaw * 180.0f / (float)Math.PI; // Create Model hand Model.LeapMotion.Hand modelHand = new Model.LeapMotion.Hand(); modelHand.LocalRotation = System.Numerics.Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll); modelHand.LocalPosition = new System.Numerics.Vector3(direction.x, direction.y, direction.z); modelHand.Side = getHandSideType(hand); modelHand.Fingers = fingers; modelHand.Name = hand.IsLeft ? NameHandLeft : NameHandRight; // Create hand body to add to mocapFrame VinteR.Model.Body modelBody = modelHand; mocapFrame.AddBody(ref modelBody); } adapter.OnFrameAvailable(mocapFrame); }