source: other-projects/playing-in-the-street/summer-2013/trunk/Microsoft.Samples.Kinect.Webserver/Sensor/UserActivityMeter.cs@ 28896

Last change on this file since 28896 was 28896, checked in by davidb, 10 years ago

Core Web Server that connects to the Kinect device

File size: 3.8 KB
Line 
1// -----------------------------------------------------------------------
2// <copyright file="UserActivityMeter.cs" company="Microsoft">
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// </copyright>
5// -----------------------------------------------------------------------
6
7namespace Microsoft.Samples.Kinect.Webserver.Sensor
8{
9 using System.Collections.Generic;
10 using Microsoft.Kinect;
11
12 /// <summary>
13 /// Helper class used to measure user activity.
14 /// </summary>
15 internal class UserActivityMeter
16 {
17 private readonly Dictionary<int, UserActivityRecord> activityRecords = new Dictionary<int, UserActivityRecord>();
18 private int totalUpdatesSoFar;
19
20 /// <summary>
21 /// Clears all user activity metrics.
22 /// </summary>
23 public void Clear()
24 {
25 this.activityRecords.Clear();
26 }
27
28 /// <summary>
29 /// Update user activity metrics with data from a collection of skeletons.
30 /// </summary>
31 /// <param name="skeletons">
32 /// Collection of skeletons to use to update activity metrics.
33 /// </param>
34 /// <param name="timestamp">
35 /// Time when skeleton array was received for processing.
36 /// </param>
37 /// <remarks>
38 /// UserActivityMeter assumes that this method is called regularly, e.g.: once
39 /// per skeleton frame received by application, so if a user whose activity was
40 /// previously measured is now absent, activity record will be removed.
41 /// </remarks>
42 public void Update(ICollection<Skeleton> skeletons, long timestamp)
43 {
44 foreach (var skeleton in skeletons)
45 {
46 UserActivityRecord record;
47
48 if (this.activityRecords.TryGetValue(skeleton.TrackingId, out record))
49 {
50 record.Update(skeleton.Position, this.totalUpdatesSoFar, timestamp);
51 }
52 else
53 {
54 record = new UserActivityRecord(skeleton.Position, this.totalUpdatesSoFar, timestamp);
55 this.activityRecords[skeleton.TrackingId] = record;
56 }
57 }
58
59 // Remove activity records corresponding to users that are no longer being tracked
60 var idsToRemove = new List<int>();
61 foreach (var record in this.activityRecords)
62 {
63 if (record.Value.LastUpdateId != this.totalUpdatesSoFar)
64 {
65 idsToRemove.Add(record.Key);
66 }
67 }
68
69 foreach (var id in idsToRemove)
70 {
71 this.activityRecords.Remove(id);
72 }
73
74 ++this.totalUpdatesSoFar;
75 }
76
77 /// <summary>
78 /// Gets the activity record associated with the specified user.
79 /// </summary>
80 /// <param name="userTrackingId">
81 /// Skeleton tracking Id of user associated with the activity record to
82 /// retrieve.
83 /// </param>
84 /// <param name="record">
85 /// [out] When this method returns, contains the record associated with the
86 /// specified user tracking Id, if the appropriate activity record is found.
87 /// This parameter is passed uninitialized.
88 /// </param>
89 /// <returns>
90 /// <code>true</code> if the UserActivityMeter contains an activity record
91 /// for the specified user tracking Id; otherwise, <code>false</code>.
92 /// </returns>
93 public bool TryGetActivityRecord(int userTrackingId, out UserActivityRecord record)
94 {
95 return this.activityRecords.TryGetValue(userTrackingId, out record);
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.