-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeArtTemperature.cs
91 lines (76 loc) · 2.77 KB
/
WeArtTemperature.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* WEART - Temperature component
* https://www.weart.it/
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace WeArt.Core
{
/// <summary>
/// The temperature applied to the thermal actuator on the actuation point.
/// The minimum value indicates the coldest temperature, the maximum indicates the hottest.
/// </summary>
[Serializable]
public struct Temperature : ICloneable
{
/// <summary>
/// The default temperature is the ambient one, with the actuator off
/// </summary>
public static Temperature Default = new Temperature
{
Value = WeArtConstants.defaultTemperature,
Active = false
};
internal float _value;
internal bool _active;
/// <summary>
/// The temperature value, normalized between 0 (cold) and 1 (hot)
/// </summary>
public float Value
{
get => _value;
set => _value = Math.Clamp(value, WeArtConstants.minTemperature, WeArtConstants.maxTemperature);
}
/// <summary>
/// Indicates whether the temperature feeling is applied or not
/// </summary>
public bool Active
{
get => _active;
set => _active = value;
}
/// <summary>
/// True if the object is a <see cref="Temperature"/> instance with the same activation status and value
/// </summary>
/// <param name="obj">The object to check equality with</param>
/// <returns>The equality check result</returns>
public override bool Equals(object obj)
{
return obj is Temperature temperature &&
ApproximateFloatComparer.Instance.Equals(Value, temperature.Value) &&
Active == temperature.Active;
}
/// <summary>Basic <see cref="GetHashCode"/> implementation</summary>
/// <returns>The hashcode of this object</returns>
public override int GetHashCode() => base.GetHashCode();
/// <summary>Clones this object</summary>
/// <returns>A clone of this object</returns>
public object Clone() => this;
/// <summary>
/// Calculates the mean of multiple temperatures
/// </summary>
/// <param name="temperatures">A collection or set of temperatures</param>
/// <returns>The mean temperature</returns>
public static Temperature Mean(IEnumerable<Temperature> temperatures)
{
var actives = temperatures.Where(t => t.Active);
var total = actives.Sum(t => t.Value);
return new Temperature
{
Active = actives.Count() > 0,
Value = total / actives.Count()
};
}
}
}