-
Notifications
You must be signed in to change notification settings - Fork 27
/
DetectionResult.cs
160 lines (142 loc) · 5.29 KB
/
DetectionResult.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System.Collections.Generic;
using UnityEngine;
namespace MBaske.Sensors.Grid
{
/// <summary>
/// A <see cref="DetectionResult"/> is generated by an
/// <see cref="IDetector"/> and encoded by an <see cref="IEncoder"/>.
/// </summary>
public class DetectionResult
{
/// <summary>
/// Wrapper for a <see cref="IDetectable"/> object and its
/// associated normalized points list.
/// Note that this refers to any object implementing
/// <see cref="IDetectable"/>. It is not to be confused
/// with <see cref="DetectableGameObject"/> which is a
/// specific implementation of <see cref="IDetectable"/>.
/// </summary>
public class Item
{
/// <summary>
/// Detectable object.
/// </summary>
public IDetectable Detectable;
/// <summary>
/// List of normalized points, 0/0/0 to 1/1/1.
/// Will be converted to grid positions by <see cref="IEncoder"/>.
/// </summary>
public List<Vector3> NormPoints = new List<Vector3>();
/// <summary>
/// Whether this <see cref="Item"/> contains any points.
/// </summary>
public bool HasPoints => NormPoints.Count > 0;
}
/// <summary>
/// List of detectable tags.
/// </summary>
public IList<string> DetectableTags { get; private set; }
private readonly Stack<Item> m_ItemPool;
private readonly IList<IDetectable> m_Detectables;
private readonly IDictionary<string, IList<Item>> m_ItemsByTag;
/// <summary>
/// Creates a <see cref="DetectionResult"/> instance.
/// </summary>
/// <param name="detectableTags">List of detectable tags</param>
/// <param name="initCapacity">Initial capacity</param>
public DetectionResult(IList<string> detectableTags, int initCapacity)
{
DetectableTags = new List<string>(detectableTags);
int n = DetectableTags.Count;
m_Detectables = new List<IDetectable>(initCapacity);
m_ItemsByTag = new Dictionary<string, IList<Item>>(n);
m_ItemPool = new Stack<Item>(initCapacity);
for (int i = 0; i < n; i++)
{
m_ItemsByTag.Add(DetectableTags[i], new List<Item>(initCapacity));
}
}
/// <summary>
/// Clears the <see cref="DetectionResult"/>.
/// </summary>
public void Clear()
{
m_Detectables.Clear();
foreach (var list in m_ItemsByTag.Values)
{
foreach (var item in list)
{
item.NormPoints.Clear();
m_ItemPool.Push(item);
}
list.Clear();
}
}
/// <summary>
/// Adds an <see cref="IDetectable"/> and its associated normalized points.
/// </summary>
/// <param name="detectable">Detectable object</param>
/// <param name="normPoints">Normalized points list</param>
public void Add(IDetectable detectable, IList<Vector3> normPoints)
{
Item item = m_ItemPool.Count > 0 ? m_ItemPool.Pop() : new Item();
item.Detectable = detectable;
item.NormPoints.AddRange(normPoints);
m_ItemsByTag[detectable.Tag].Add(item);
m_Detectables.Add(detectable);
}
/// <summary>
/// Tries to retrieve <see cref="Item"/> instances associated
/// with a specific tag.
/// </summary>
/// <param name="tag">The specified tag</param>
/// <param name="items">List of items (output)</param>
/// <returns>Whether any items were found</returns>
public bool TryGetItems(string tag, out IList<Item> items)
{
if (m_ItemsByTag.TryGetValue(tag, out items) && items.Count > 0)
{
return true;
}
items = null;
return false;
}
/// <summary>
/// Whether the <see cref="DetectionResult"/> contains
/// a specific <see cref="IDetectable"/> object.
/// </summary>
/// <param name="detectable">Detectable object</param>
/// <returns>True if <see cref="IDetectable"/> was found</returns>
public bool Contains(IDetectable detectable)
{
return m_Detectables.Contains(detectable);
}
/// <summary>
/// Returns the <see cref="Item"/> instance count associated
/// with a specific tag.
/// </summary>
/// <param name="tag">The specified tag</param>
/// <returns>Number of items</returns>
public int Count(string tag)
{
if (m_ItemsByTag.TryGetValue(tag, out IList<Item> items))
{
return items.Count;
}
return 0;
}
/// <summary>
/// Returns the total <see cref="Item"/> instance count.
/// </summary>
/// <returns>Number of items</returns>
public int Count()
{
int sum = 0;
foreach (var list in m_ItemsByTag.Values)
{
sum += list.Count;
}
return sum;
}
}
}