public void ToProtoBuf(MocapFrame frame, out Model.Gen.MocapFrame output) { // create mapping from MocapFrame to Gen.MocapFrame output = new Model.Gen.MocapFrame() { AdapterType = frame.AdapterType, ElapsedMillis = frame.ElapsedMillis, Gesture = frame.Gesture ?? "", // set default value otherwise serialization breaks Latency = frame.Latency, SourceId = frame.SourceId }; foreach (var body in frame.Bodies) { var protoBody = new Model.Gen.MocapFrame.Types.Body() { BodyType = body.GetBodyTypeProto(), Rotation = body.Rotation.ToProto(), SideType = body.GetSideTypeProto(), Centroid = body.Centroid.ToProto(), Name = body.Name ?? string.Empty }; foreach (var point in body.Points) { var protoPoint = new Model.Gen.MocapFrame.Types.Body.Types.Point() { Name = point.Name ?? "", State = point.State ?? "", Position = point.Position.ToProto() }; protoBody.Points.Add(protoPoint); } output.Bodies.Add(protoBody); } }
/** * 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); }