public void Evaluate(int SpreadMax) { if (!bEnable[0]) { return; } // Get the current state of our contacts. // Nice, this seems to be a threadsafe read. ReadOnlyContactCollection contacts = contactTarget.GetState(); IDout.SliceCount = contactSize.SliceCount = contactRotation.SliceCount = contactCoord.SliceCount = contacts.Count; int i = 0; foreach (Contact c in contacts) { IDout[i] = c.Id; Vector2D pos = new Vector2D(c.X, c.Y); pos.x = (bNormalizeValues[0]) ? pos.x / 512 - 1 : pos.x; pos.y = (bNormalizeValues[0]) ? (2 - pos.y / 384) - 1 : pos.y; double rotation = 1 - c.Orientation / (2 * Math.PI); contactSize[i] = new Vector2D(c.Bounds.Width / 1024.0, c.Bounds.Height / 768.0); contactCoord[i] = pos; contactRotation[i] = rotation; i++; } }
private Contact FindReturned(ReadOnlyContactCollection contacts, ContactData gone) { foreach (Contact c in contacts) { // Not a tag, or is already associated with a player. if (!c.IsTagRecognized || GetPlayerIdByContact(c) != -1) { continue; } for (int i = 0; i < 4; i++) { if (playerTags[i] == gone.Contact.Id) { if (Vector2.Distance(new Vector2(c.CenterX, c.CenterY), gone.LastLocation) < 50) { playerTags[i] = c.Id; return(c); } return(null); } } } return(null); }
public static OSCMessage aliveMessage(ReadOnlyContactCollection contacts) { OSCMessage message = new OSCMessage("/tuio/2Dblb"); message.Append("alive"); for (int i = 0; i < contacts.Count; i++) { message.Append(contacts[i].Id); } return message; }
/// <summary> /// Called just before DoUpdate if contacts need to be processed. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> /// <param name="contacts">The collection of contacts that are currently active.</param> protected abstract void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts);
public void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts) { TimeSpan now = gameTime.TotalRealTime; // First process tags that are leaving. Queue<ContactData> q = leavingTags; ContactData current; leavingTags = new Queue<ContactData>(); while (q.Count > 0) { current = q.Dequeue(); if (current.LastSeen + new TimeSpan(0, 0, 0, 1) < now) { if (ContactRemoved != null) ContactRemoved(this, current); } else { Contact returned = FindReturned(contacts, current); if (returned != null) { current.Update(returned, now); activeContacts.Add(current); } else { leavingTags.Enqueue(current); } } } // Now get current contacts. foreach (Contact c in contacts) { bool found = false; // Search the collection of active contacts to see if we already know about this one. foreach (ContactData d in activeContacts) { if (c.Id == d.Contact.Id) { d.Update(c, now); found = true; break; } } // Not found? It's a new contact, add it and notify event handlers. if (!found) { ContactData d = new ContactData(c, now); activeContacts.Add(d); if (ContactAdded != null) ContactAdded(this, d); } } // Now check to see if there are any that need removing - these will have an outdated LastSeen. int length = activeContacts.Count; for (int i = 0; i < length; i++) { ContactData d = activeContacts[i]; if (!d.LastSeen.Equals(now)) { activeContacts.RemoveAt(i); i--; length--; if (d.Contact.IsTagRecognized) { leavingTags.Enqueue(d); } else { if (ContactRemoved != null) ContactRemoved(this, d); } } } // Finally, check to see if anything in pendingPressed can be moved to pressed. length = pendingPressed.Count; for (int i = 0; i < length; i++) { TouchableContactPair p = pendingPressed[i]; if ((p.Contact.LastSeen - p.Contact.TimeAdded).TotalMilliseconds > 100 && p.Touchable.InRegion(p.Contact.Contact)) { pressed.Add(p); pendingPressed.RemoveAt(i); i--; length--; int player = WhichPlayer(p.Contact); if (player != -1) { p.Touchable.Controller.Press(p.Contact, player); } } } }
private Contact FindReturned(ReadOnlyContactCollection contacts, ContactData gone) { foreach (Contact c in contacts) { // Not a tag, or is already associated with a player. if (!c.IsTagRecognized || GetPlayerIdByContact(c) != -1) { continue; } for (int i = 0; i < 4; i++) { if (playerTags[i] == gone.Contact.Id) { if (Vector2.Distance(new Vector2(c.CenterX, c.CenterY), gone.LastLocation) < 50) { playerTags[i] = c.Id; return c; } return null; } } } return null; }
internal static Exception ContactIsNotInCollection(Contact contact, ReadOnlyContactCollection collection) { return new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ContactIsNotInCollectionException, contact.Id, collection)); }
/// <summary> /// Main application Logic /// We retrieve the current Contacts and send TUIO messages accordingly /// You can specify via appConfig if you want a Finger Contact to be sent as /tuio/2Dcur or /tuio/2Dblb /// Sending it as /tuio/2Dblb conserves the Contact size, rotation, angular velocity and angular acceleration /// </summary> protected override void Update(GameTime gameTime) { if (isApplicationActivated || isApplicationPreviewed) { _contactManipulationData.Clear(); // Want to identify all the contacts added or removed since the last update. List<Contact> addedContacts = new List<Contact>(); List<Contact> removedContacts = new List<Contact>(); List<Contact> changedContacts = new List<Contact>(); List<Contact> aliveContacts = new List<Contact>(); List<Contact> addedFingers = new List<Contact>(); List<Contact> removedFingers = new List<Contact>(); List<Contact> changedFingers = new List<Contact>(); List<Contact> aliveFingers = new List<Contact>(); List<Contact> addedTags = new List<Contact>(); List<Contact> removedTags = new List<Contact>(); List<Contact> changedTags = new List<Contact>(); List<Contact> aliveTags = new List<Contact>(); List<Contact> addedBlobs = new List<Contact>(); List<Contact> removedBlobs = new List<Contact>(); List<Contact> changedBlobs= new List<Contact>(); List<Contact> aliveBlobs = new List<Contact>(); ReadOnlyContactCollection currentContacts = contactTarget.GetState(); // Write all unactive previous Contacts into the according removed-Lists if (previousContacts != null) { foreach (Contact contact in previousContacts) { Contact c = null; currentContacts.TryGetContactFromId(contact.Id, out c); if (c == null) { removedContacts.Add(contact); if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { removedFingers.Add(contact); } if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { removedBlobs.Add(contact); } else if (contact.IsTagRecognized) { removedTags.Add(contact); } else { removedBlobs.Add(contact); } } } // Throw away unused Manipulation Processors cleanManipulationProcessorList(removedContacts); foreach (Contact contact in currentContacts) { aliveContacts.Add(contact); // Put the Contact into the according List if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { aliveFingers.Add(contact); } if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { aliveBlobs.Add(contact); } else if (contact.IsTagRecognized) { aliveTags.Add(contact); } else { aliveBlobs.Add(contact); } Contact c = null; previousContacts.TryGetContactFromId(contact.Id, out c); if (c != null) { if (c.ToString() != contact.ToString()) { changedContacts.Add(contact); // Invoke the processing of a Contact's Manipulation Processor processContactManipulator(contact, currentContacts, previousContacts); // Put the Contact into the according List if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { changedFingers.Add(contact); } if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { changedBlobs.Add(contact); } else if (contact.IsTagRecognized) { changedTags.Add(contact); } else { changedBlobs.Add(contact); } } } else { addedContacts.Add(contact); // Add a Manipulation Processor to each contact // This is done for extracting the contact velocity directly from Surface SDK addManipulationProcessor(contact); if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == false || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { addedFingers.Add(contact); } if (contact.IsFingerRecognized && (Properties.Settings.Default.SendFingersAsBlobs == true || Properties.Settings.Default.SendFingersAlsoAsBlobs == true)) { addedBlobs.Add(contact); } else if (contact.IsTagRecognized) { addedTags.Add(contact); } else { addedBlobs.Add(contact); } } } // Send the TUIO messages //sendTUIO_2DCur(changedFingers, previousContacts); //sendTUIO_2DObj(changedTags, previousContacts); //sendTUIO_2DBlb(changedBlobs, previousContacts); if (addedFingers.Count != 0 || changedFingers.Count != 0 || removedFingers.Count != 0) { sendTUIO_2DCur(aliveFingers, previousContacts); } if (addedTags.Count != 0 || changedTags.Count != 0 || removedTags.Count != 0) { sendTUIO_2DObj(aliveTags, previousContacts); } } else { foreach (Contact c in currentContacts) { addedContacts.Add(c); if (c.IsFingerRecognized) { addedFingers.Add(c); } if (c.IsTagRecognized) { addedTags.Add(c); } } } previousContacts = currentContacts; foreach (Contact c in changedContacts) { updateHistoryData(c.Id, c.FrameTimestamp); } foreach (Contact c in removedContacts) { cleanHistory(c.Id); } } base.Update(gameTime); }
/// <summary> /// Sends a /tuio/2Dobj message /// The remote Host is specified in the appConfig /// The message is built according to TUIO 1.1 specifications (see http://www.tuio.org/?specification) /// </summary> /// <param name="contacts">Current acitve Contacts</param> /// <param name="previousContacts">Contacts from the previous frame</param> public void sendTUIO_2DObj(List<Contact> contacts, ReadOnlyContactCollection previousContacts) { //if (contacts.Count == 0) return; InteractiveSurface surface = Microsoft.Surface.Core.InteractiveSurface.DefaultInteractiveSurface; double width = surface.Right - surface.Left; double height = surface.Bottom - surface.Top; OSCBundle objectBundle = new OSCBundle(); OSCMessage sourceMessage = TUIO_2DObj.sourceMessage(); objectBundle.Append(sourceMessage); OSCMessage aliveMessage = TUIO_2DObj.aliveMessage(contacts); objectBundle.Append(aliveMessage); for (int i = 0; i < contacts.Count; i++) { Contact c = contacts[i]; double x = (c.CenterX-surface.Left)/width; double y = (c.CenterY-surface.Top)/height; float angularVelocity = 0.0f; float angularAcceleration = 0.0f; if( previousContacts.Contains( c.Id )) { computeAngularVelocity(c, previousContacts.GetContactFromId(c.Id), out angularVelocity, out angularAcceleration); } float X = 0.0f; float Y = 0.0f; getVelocity(c.Id, out X, out Y, surface); float motionAcceleration = 0.0f; getMotionAcceleration(c.Id, out motionAcceleration, c.FrameTimestamp); if (c.Tag.Type == TagType.Byte) { OSCMessage setMessage = TUIO_2DObj.setMessage(c.Id, (int)c.Tag.Byte.Value, (float)x, (float)y, (float)c.Orientation, X, Y, angularVelocity, motionAcceleration, angularAcceleration); objectBundle.Append(setMessage); } else if (c.Tag.Type == TagType.Identity) { OSCMessage setMessage = TUIO_2DObj.setMessage(c.Id, (int)c.Tag.Identity.Value, (float)x, (float)y, (float)c.Orientation, X, Y, angularVelocity, motionAcceleration, angularAcceleration); objectBundle.Append(setMessage); } } OSCMessage frameMessage = TUIO_2DObj.frameMessage(_Frame); objectBundle.Append(frameMessage); _Frame++; _OSCSender.Send(objectBundle); }
/// <summary> /// Sends a /tuio/2Dblb message /// The remote Host is specified in the appConfig /// The message is built according to TUIO 1.1 specifications (see http://www.tuio.org/?specification) /// </summary> /// <param name="contacts">Current active contacts</param> /// <param name="previousContacts">Contacts from the previous frame</param> public void sendTUIO_2DBlb(List<Contact> contacts, ReadOnlyContactCollection previousContacts) { //if (contacts.Count == 0) return; InteractiveSurface surface = Microsoft.Surface.Core.InteractiveSurface.DefaultInteractiveSurface; double width = surface.Right - surface.Left; double height = surface.Bottom - surface.Top; OSCBundle blobBundle = new OSCBundle(); OSCMessage sourceMessage = TUIO_2DBlb.sourceMessage(); blobBundle.Append(sourceMessage); OSCMessage aliveMessage = TUIO_2DBlb.aliveMessage(contacts); blobBundle.Append(aliveMessage); for (int i = 0; i < contacts.Count; i++) { Contact c = contacts[i]; double x = (c.CenterX - surface.Left) / width; double y = (c.CenterY - surface.Top) / height; double w = c.MajorAxis/surface.Width; double h = c.MinorAxis/surface.Height; double f = c.PhysicalArea / (surface.Width * surface.Height); float angularVelocity = 0.0f; float angularAcceleration = 0.0f; if (previousContacts.Contains(c.Id)) { computeAngularVelocity(c, previousContacts.GetContactFromId(c.Id), out angularVelocity, out angularAcceleration); } float X = 0.0f; float Y = 0.0f; getVelocity(c.Id, out X, out Y, surface); float motionAcceleration = 0.0f; getMotionAcceleration(c.Id, out motionAcceleration, c.FrameTimestamp); OSCMessage setMessage = TUIO_2DBlb.setMessage(c.Id, (float)x, (float)y, (float)c.Orientation, (float)w, (float)h, (float)(w * h), X, Y, angularVelocity, motionAcceleration, angularAcceleration); blobBundle.Append(setMessage); } OSCMessage frameMessage = TUIO_2DBlb.frameMessage(_Frame); blobBundle.Append(frameMessage); _Frame++; _OSCSender.Send(blobBundle); }
public void processContactManipulator(Contact c, ReadOnlyContactCollection currentContacts, ReadOnlyContactCollection previousContacts) { List<Manipulator> removedManipulators = new List<Manipulator>(); if (previousContacts != null) { // Find all the contacts that were removed since the last check. foreach (Contact contact in previousContacts) { // Create a temporary variable for the following method call. Contact tempContact; // Determine if the Contact object from the previous list is in the current list. if (!currentContacts.TryGetContactFromId(contact.Id, out tempContact)) { // The contact was not found in the list of current contacts, so it has been removed. // Copy the Contact object information to a new Manipulator object and add // the Manipulator object to the removedManipulators list. removedManipulators.Add(new Manipulator(contact.Id, contact.X, contact.Y)); } } } List<Manipulator> currentManipulator = new List<Manipulator>(); currentManipulator.Add(new Manipulator(c.Id, c.X, c.Y)); if (_contactProcessors.ContainsKey(c.Id)) { _contactProcessors[c.Id].ProcessManipulators(currentManipulator, removedManipulators); } }
public void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts) { TimeSpan now = gameTime.TotalRealTime; // First process tags that are leaving. Queue <ContactData> q = leavingTags; ContactData current; leavingTags = new Queue <ContactData>(); while (q.Count > 0) { current = q.Dequeue(); if (current.LastSeen + new TimeSpan(0, 0, 0, 1) < now) { if (ContactRemoved != null) { ContactRemoved(this, current); } } else { Contact returned = FindReturned(contacts, current); if (returned != null) { current.Update(returned, now); activeContacts.Add(current); } else { leavingTags.Enqueue(current); } } } // Now get current contacts. foreach (Contact c in contacts) { bool found = false; // Search the collection of active contacts to see if we already know about this one. foreach (ContactData d in activeContacts) { if (c.Id == d.Contact.Id) { d.Update(c, now); found = true; break; } } // Not found? It's a new contact, add it and notify event handlers. if (!found) { ContactData d = new ContactData(c, now); activeContacts.Add(d); if (ContactAdded != null) { ContactAdded(this, d); } } } // Now check to see if there are any that need removing - these will have an outdated LastSeen. int length = activeContacts.Count; for (int i = 0; i < length; i++) { ContactData d = activeContacts[i]; if (!d.LastSeen.Equals(now)) { activeContacts.RemoveAt(i); i--; length--; if (d.Contact.IsTagRecognized) { leavingTags.Enqueue(d); } else { if (ContactRemoved != null) { ContactRemoved(this, d); } } } } // Finally, check to see if anything in pendingPressed can be moved to pressed. length = pendingPressed.Count; for (int i = 0; i < length; i++) { TouchableContactPair p = pendingPressed[i]; if ((p.Contact.LastSeen - p.Contact.TimeAdded).TotalMilliseconds > 100 && p.Touchable.InRegion(p.Contact.Contact)) { pressed.Add(p); pendingPressed.RemoveAt(i); i--; length--; int player = WhichPlayer(p.Contact); if (player != -1) { p.Touchable.Controller.Press(p.Contact, player); } } } }
public void Frame() { if (openCOVER != null) { // Want to identify all the contacts added or removed since the last update. List<Contact> addedContacts = new List<Contact>(); List<Contact> removedContacts = new List<Contact>(); List<Contact> changedContacts = new List<Contact>(); // Get a list of the current contacts ReadOnlyContactCollection currentContacts = contactTarget.GetState(); // Compare the contacts in the current list to the list saved from the last update if (previousContacts != null) { foreach (Contact contact in previousContacts) { Contact c = null; currentContacts.TryGetContactFromId(contact.Id, out c); if (c == null) { removedContacts.Add(contact); if(contact.IsFingerRecognized) removedManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY)); } } foreach (Contact contact in currentContacts) { Contact c = null; previousContacts.TryGetContactFromId(contact.Id, out c); if (c != null) { changedContacts.Add(contact); if (contact.IsFingerRecognized) currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY)); } else { addedContacts.Add(contact); if (contact.IsFingerRecognized) currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY)); } } } else { foreach (Contact c in currentContacts) { addedContacts.Add(c); if (c.IsFingerRecognized) currentManipulators.Add(new Manipulator(c.Id, c.CenterX, c.CenterY)); } } manipulationProcessor.ProcessManipulators(currentManipulators, removedManipulators); currentManipulators.Clear(); removedManipulators.Clear(); previousContacts = currentContacts; // Hit test and assign all new contacts foreach (Contact c in addedContacts) { openCOVER.addedContact(c); } // Update the captors of all the pre-existing contacts foreach (Contact c in changedContacts) { openCOVER.changedContact(c); } // Clean up all old contacts foreach (Contact co in removedContacts) { openCOVER.removedContact(co); } openCOVER.frame(); } }
public void Frame() { if (openCOVER != null) { // Want to identify all the contacts added or removed since the last update. List <Contact> addedContacts = new List <Contact>(); List <Contact> removedContacts = new List <Contact>(); List <Contact> changedContacts = new List <Contact>(); // Get a list of the current contacts ReadOnlyContactCollection currentContacts = contactTarget.GetState(); // Compare the contacts in the current list to the list saved from the last update if (previousContacts != null) { foreach (Contact contact in previousContacts) { Contact c = null; currentContacts.TryGetContactFromId(contact.Id, out c); if (c == null) { removedContacts.Add(contact); if (contact.IsFingerRecognized) { removedManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY)); } } } foreach (Contact contact in currentContacts) { Contact c = null; previousContacts.TryGetContactFromId(contact.Id, out c); if (c != null) { changedContacts.Add(contact); if (contact.IsFingerRecognized) { currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY)); } } else { addedContacts.Add(contact); if (contact.IsFingerRecognized) { currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY)); } } } } else { foreach (Contact c in currentContacts) { addedContacts.Add(c); if (c.IsFingerRecognized) { currentManipulators.Add(new Manipulator(c.Id, c.CenterX, c.CenterY)); } } } manipulationProcessor.ProcessManipulators(currentManipulators, removedManipulators); currentManipulators.Clear(); removedManipulators.Clear(); previousContacts = currentContacts; // Hit test and assign all new contacts foreach (Contact c in addedContacts) { openCOVER.addedContact(c); } // Update the captors of all the pre-existing contacts foreach (Contact c in changedContacts) { openCOVER.changedContact(c); } // Clean up all old contacts foreach (Contact co in removedContacts) { openCOVER.removedContact(co); } openCOVER.frame(); } }
/// <summary> /// Sends a /tuio/2Dcur message /// The remote Host is specified in the appConfig /// The message is built according to TUIO 1.1 specifications (see http://www.tuio.org/?specification) /// </summary> /// <param name="contacts">Current active contacts</param> /// <param name="previousContacts">Contacts from the previous frame</param> public void sendTUIO_2DCur(List<Contact> contacts, ReadOnlyContactCollection previousContacts) { //if (contacts.Count == 0) return; InteractiveSurface surface = Microsoft.Surface.Core.InteractiveSurface.DefaultInteractiveSurface; double width = surface.Right - surface.Left; double height = surface.Bottom - surface.Top; OSCBundle cursorBundle = new OSCBundle(); OSCMessage sourceMessage = TUIO_2DCur.sourceMessage(); cursorBundle.Append(sourceMessage); OSCMessage aliveMessage = TUIO_2DCur.aliveMessage(contacts); cursorBundle.Append(aliveMessage); for (int i = 0; i < contacts.Count; i++) { Contact c = contacts[i]; double x = (c.CenterX - surface.Left) / width; double y = (c.CenterY - surface.Top) / height; float X = 0.0f; float Y = 0.0f; getVelocity(c.Id, out X, out Y, surface); float motionAcceleration = 0.0f; getMotionAcceleration(c.Id, out motionAcceleration, c.FrameTimestamp); OSCMessage setMessage = TUIO_2DCur.setMessage(c.Id, (float)x, (float)y, X, Y, motionAcceleration); cursorBundle.Append(setMessage); } OSCMessage frameMessage = TUIO_2DCur.frameMessage(_Frame); cursorBundle.Append(frameMessage); _Frame++; _OSCSender.Send(cursorBundle); }
protected override void ProcessContacts(GameTime gameTime, ReadOnlyContactCollection contacts) { contactParser.ProcessContacts(gameTime, contacts); }