1 | // -----------------------------------------------------------------------
|
---|
2 | // <copyright file="UserActivityRecord.cs" company="Microsoft">
|
---|
3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
4 | // </copyright>
|
---|
5 | // -----------------------------------------------------------------------
|
---|
6 |
|
---|
7 | namespace Microsoft.Samples.Kinect.Webserver.Sensor
|
---|
8 | {
|
---|
9 | using System;
|
---|
10 |
|
---|
11 | using Microsoft.Kinect;
|
---|
12 |
|
---|
13 | /// <summary>
|
---|
14 | /// Represents activity state and metrics for a single user.
|
---|
15 | /// </summary>
|
---|
16 | internal class UserActivityRecord
|
---|
17 | {
|
---|
18 | // Activity level, above which a user is considered to be in "active" state.
|
---|
19 | private const double ActivityMetricThreshold = 0.1;
|
---|
20 |
|
---|
21 | private double activityLevel;
|
---|
22 |
|
---|
23 | public UserActivityRecord(SkeletonPoint position, int updateId, long timestamp)
|
---|
24 | {
|
---|
25 | this.ActivityLevel = 0.0;
|
---|
26 | this.LastPosition = position;
|
---|
27 | this.LastUpdateId = updateId;
|
---|
28 | this.IsActive = false;
|
---|
29 | this.StateTransitionTimestamp = timestamp;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// User activity level metric being tracked.
|
---|
34 | /// </summary>
|
---|
35 | /// <remarks>
|
---|
36 | /// Value is always in [0.0, 1.0] interval.
|
---|
37 | /// </remarks>
|
---|
38 | public double ActivityLevel
|
---|
39 | {
|
---|
40 | get
|
---|
41 | {
|
---|
42 | return this.activityLevel;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private set
|
---|
46 | {
|
---|
47 | this.activityLevel = Math.Max(0.0, Math.Min(1.0, value));
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Id of last update that touched this activity record.
|
---|
53 | /// </summary>
|
---|
54 | public int LastUpdateId { get; private set; }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// True if user activity is currently larger than the activity threshold.
|
---|
58 | /// </summary>
|
---|
59 | public bool IsActive { get; private set; }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Time when IsActive state last changed from true to false or vice versa.
|
---|
63 | /// </summary>
|
---|
64 | public long StateTransitionTimestamp { get; private set; }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Last position where user was observed.
|
---|
68 | /// </summary>
|
---|
69 | public SkeletonPoint LastPosition { get; private set; }
|
---|
70 |
|
---|
71 | public void Update(SkeletonPoint position, int updateId, long timestamp)
|
---|
72 | {
|
---|
73 | // Movement magnitude gets scaled by this amount in order to get the current activity metric
|
---|
74 | const double DeltaScalingFactor = 10.0;
|
---|
75 |
|
---|
76 | // Controls how quickly new values of the metric displace old values. 1.0 means that new values
|
---|
77 | // for metric immediately replace old values, while smaller decay amounts mean that old metric
|
---|
78 | // values influence the metric for a longer amount of time (i.e.: decay more slowly).
|
---|
79 | const double ActivityDecay = 0.1;
|
---|
80 |
|
---|
81 | var delta = new SkeletonPoint
|
---|
82 | {
|
---|
83 | X = position.X - this.LastPosition.X,
|
---|
84 | Y = position.Y - this.LastPosition.Y,
|
---|
85 | Z = position.Z - this.LastPosition.Z
|
---|
86 | };
|
---|
87 |
|
---|
88 | double deltaLengthSquared = (delta.X * delta.X) + (delta.Y * delta.Y) + (delta.Z * delta.Z);
|
---|
89 | double newMetric = DeltaScalingFactor * Math.Sqrt(deltaLengthSquared);
|
---|
90 |
|
---|
91 | this.ActivityLevel = ((1.0 - ActivityDecay) * this.ActivityLevel) + (ActivityDecay * newMetric);
|
---|
92 |
|
---|
93 | bool newIsActive = this.ActivityLevel >= ActivityMetricThreshold;
|
---|
94 |
|
---|
95 | if (newIsActive != this.IsActive)
|
---|
96 | {
|
---|
97 | this.IsActive = newIsActive;
|
---|
98 | this.StateTransitionTimestamp = timestamp;
|
---|
99 | }
|
---|
100 |
|
---|
101 | this.LastPosition = position;
|
---|
102 | this.LastUpdateId = updateId;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|