forked from mtysgithub/Qualcomm.Vuforia.UnityExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordManagerImpl.cs
345 lines (323 loc) · 13.3 KB
/
WordManagerImpl.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using UnityEngine;
public class WordManagerImpl : WordManager
{
private readonly Dictionary<int, WordAbstractBehaviour> mActiveWordBehaviours = new Dictionary<int, WordAbstractBehaviour>();
private bool mAutomaticTemplate;
private readonly List<Word> mLostWords = new List<Word>();
private int mMaxInstances = 1;
private readonly List<WordResult> mNewWords = new List<WordResult>();
private readonly Dictionary<int, WordResult> mTrackedWords = new Dictionary<int, WordResult>();
private readonly List<Word> mWaitingQueue = new List<Word>();
private readonly Dictionary<string, List<WordAbstractBehaviour>> mWordBehaviours = new Dictionary<string, List<WordAbstractBehaviour>>();
private readonly List<WordAbstractBehaviour> mWordBehavioursMarkedForDeletion = new List<WordAbstractBehaviour>();
private WordPrefabCreationMode mWordPrefabCreationMode;
private const string TEMPLATE_IDENTIFIER = "Template_ID";
private WordAbstractBehaviour AssociateWordBehaviour(WordResult wordResult)
{
List<WordAbstractBehaviour> list;
string key = wordResult.Word.StringValue.ToLowerInvariant();
if (this.mWordBehaviours.ContainsKey(key))
{
list = this.mWordBehaviours[key];
}
else if (this.mWordBehaviours.ContainsKey("Template_ID"))
{
list = this.mWordBehaviours["Template_ID"];
}
else
{
Debug.Log("No prefab available for string value " + key);
return null;
}
foreach (WordAbstractBehaviour behaviour in list)
{
if (behaviour.Trackable == null)
{
return this.AssociateWordBehaviour(wordResult, behaviour);
}
}
if (list.Count < this.mMaxInstances)
{
WordAbstractBehaviour item = InstantiateWordBehaviour(list.First<WordAbstractBehaviour>());
list.Add(item);
return this.AssociateWordBehaviour(wordResult, item);
}
return null;
}
private WordAbstractBehaviour AssociateWordBehaviour(WordResult wordResult, WordAbstractBehaviour wordBehaviourTemplate)
{
if (this.mActiveWordBehaviours.Count >= this.mMaxInstances)
{
return null;
}
Word word = wordResult.Word;
WordAbstractBehaviour behaviour = wordBehaviourTemplate;
IEditorWordBehaviour behaviour2 = behaviour;
behaviour2.SetNameForTrackable(word.StringValue);
behaviour2.InitializeWord(word);
this.mActiveWordBehaviours.Add(word.ID, behaviour);
return behaviour;
}
private void AssociateWordResultsWithBehaviours()
{
List<Word> list = new List<Word>(this.mWaitingQueue);
foreach (Word word in list)
{
if (this.mTrackedWords.ContainsKey(word.ID))
{
WordResult wordResult = this.mTrackedWords[word.ID];
if (this.AssociateWordBehaviour(wordResult) != null)
{
this.mWaitingQueue.Remove(word);
}
}
else
{
this.mWaitingQueue.Remove(word);
}
}
foreach (WordResult result2 in this.mNewWords)
{
if (this.AssociateWordBehaviour(result2) == null)
{
this.mWaitingQueue.Add(result2.Word);
}
}
}
private static WordAbstractBehaviour CreateWordBehaviour()
{
GameObject gameObject = new GameObject("Word-AutoTemplate");
WordAbstractBehaviour behaviour = BehaviourComponentFactory.Instance.AddWordBehaviour(gameObject);
Debug.Log("Creating Word Behaviour");
return behaviour;
}
public override void DestroyWordBehaviour(WordAbstractBehaviour behaviour, [Optional, DefaultParameterValue(true)] bool destroyGameObject)
{
foreach (string str in this.mWordBehaviours.Keys.ToArray<string>())
{
if (this.mWordBehaviours[str].Contains(behaviour))
{
this.mWordBehaviours[str].Remove(behaviour);
if (this.mWordBehaviours[str].Count == 0)
{
this.mWordBehaviours.Remove(str);
}
if (destroyGameObject)
{
UnityEngine.Object.Destroy(behaviour.gameObject);
this.mWordBehavioursMarkedForDeletion.Add(behaviour);
}
else
{
IEditorWordBehaviour behaviour2 = behaviour;
behaviour2.UnregisterTrackable();
}
}
}
}
public override IEnumerable<WordResult> GetActiveWordResults()
{
return this.mTrackedWords.Values;
}
public override IEnumerable<Word> GetLostWords()
{
return this.mLostWords;
}
public override IEnumerable<WordResult> GetNewWords()
{
return this.mNewWords;
}
public override IEnumerable<WordAbstractBehaviour> GetTrackableBehaviours()
{
List<WordAbstractBehaviour> list = new List<WordAbstractBehaviour>();
foreach (List<WordAbstractBehaviour> list2 in this.mWordBehaviours.Values)
{
list.AddRange(list2);
}
return list;
}
internal void InitializeWordBehaviourTemplates()
{
if (this.mWordPrefabCreationMode == WordPrefabCreationMode.DUPLICATE)
{
List<WordAbstractBehaviour> list = this.mWordBehavioursMarkedForDeletion.ToList<WordAbstractBehaviour>();
if (this.mAutomaticTemplate && this.mWordBehaviours.ContainsKey("Template_ID"))
{
foreach (WordAbstractBehaviour behaviour in this.mWordBehaviours["Template_ID"])
{
list.Add(behaviour);
UnityEngine.Object.Destroy(behaviour.gameObject);
}
this.mWordBehaviours.Remove("Template_ID");
}
WordAbstractBehaviour[] behaviourArray = (WordAbstractBehaviour[]) UnityEngine.Object.FindObjectsOfType(typeof(WordAbstractBehaviour));
foreach (WordAbstractBehaviour behaviour2 in behaviourArray)
{
if (!list.Contains(behaviour2))
{
IEditorWordBehaviour behaviour3 = behaviour2;
string key = behaviour3.IsTemplateMode ? "Template_ID" : behaviour3.SpecificWord.ToLowerInvariant();
if (!this.mWordBehaviours.ContainsKey(key))
{
this.mWordBehaviours[key] = new List<WordAbstractBehaviour> { behaviour2 };
if (key == "Template_ID")
{
this.mAutomaticTemplate = false;
}
}
}
}
if (!this.mWordBehaviours.ContainsKey("Template_ID"))
{
WordAbstractBehaviour behaviour4 = CreateWordBehaviour();
this.mWordBehaviours.Add("Template_ID", new List<WordAbstractBehaviour> { behaviour4 });
this.mAutomaticTemplate = true;
}
}
this.mWordBehavioursMarkedForDeletion.Clear();
}
internal void InitializeWordBehaviourTemplates(WordPrefabCreationMode wordPrefabCreationMode, int maxInstances)
{
this.mWordPrefabCreationMode = wordPrefabCreationMode;
this.mMaxInstances = maxInstances;
this.InitializeWordBehaviourTemplates();
}
private static WordAbstractBehaviour InstantiateWordBehaviour(WordAbstractBehaviour input)
{
GameObject obj2 = UnityEngine.Object.Instantiate(input.gameObject) as GameObject;
return obj2.GetComponent<WordAbstractBehaviour>();
}
internal void RemoveDestroyedTrackables()
{
foreach (List<WordAbstractBehaviour> list in this.mWordBehaviours.Values)
{
for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i] == null)
{
list.RemoveAt(i);
}
}
}
foreach (string str in this.mWordBehaviours.Keys.ToArray<string>())
{
if (this.mWordBehaviours[str].Count == 0)
{
this.mWordBehaviours.Remove(str);
}
}
}
internal void SetWordBehavioursToNotFound()
{
foreach (WordAbstractBehaviour behaviour in this.mActiveWordBehaviours.Values)
{
behaviour.OnTrackerUpdate(TrackableBehaviour.Status.NOT_FOUND);
}
}
public override bool TryGetWordBehaviour(Word word, out WordAbstractBehaviour behaviour)
{
return this.mActiveWordBehaviours.TryGetValue(word.ID, out behaviour);
}
private void UnregisterLostWords()
{
foreach (Word word in this.mLostWords)
{
if (this.mActiveWordBehaviours.ContainsKey(word.ID))
{
WordAbstractBehaviour behaviour = this.mActiveWordBehaviours[word.ID];
behaviour.OnTrackerUpdate(TrackableBehaviour.Status.NOT_FOUND);
((IEditorTrackableBehaviour) behaviour).UnregisterTrackable();
this.mActiveWordBehaviours.Remove(word.ID);
}
}
}
private void UpdateWordBehaviourPoses()
{
foreach (KeyValuePair<int, WordAbstractBehaviour> pair in this.mActiveWordBehaviours)
{
if (this.mTrackedWords.ContainsKey(pair.Key))
{
WordResult result = this.mTrackedWords[pair.Key];
Vector3 position = result.Position;
Quaternion orientation = result.Orientation;
Vector2 size = result.Word.Size;
pair.Value.transform.rotation = orientation;
Vector3 vector3 = (Vector3) (pair.Value.transform.rotation * new Vector3(-size.x * 0.5f, 0f, -size.y * 0.5f));
pair.Value.transform.position = position + vector3;
pair.Value.OnTrackerUpdate(result.CurrentStatus);
}
}
}
private void UpdateWordResultPoses(Camera arCamera, IEnumerable<QCARManagerImpl.WordResultData> wordResults)
{
QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour) UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour));
if (behaviour == null)
{
Debug.LogError("QCAR Behaviour could not be found");
}
else
{
Rect viewportRectangle = behaviour.GetViewportRectangle();
bool videoBackGroundMirrored = behaviour.VideoBackGroundMirrored;
CameraDevice.VideoModeData videoMode = behaviour.GetVideoMode();
foreach (QCARManagerImpl.WordResultData data2 in wordResults)
{
WordResultImpl impl = (WordResultImpl) this.mTrackedWords[data2.id];
Vector3 position = arCamera.transform.TransformPoint(data2.pose.position);
Quaternion orientation = data2.pose.orientation;
Quaternion quaternion2 = (arCamera.transform.rotation * orientation) * Quaternion.AngleAxis(270f, Vector3.left);
impl.SetPose(position, quaternion2);
impl.SetStatus(data2.status);
OrientedBoundingBox cameraFrameObb = new OrientedBoundingBox(data2.orientedBoundingBox.center, data2.orientedBoundingBox.halfExtents, data2.orientedBoundingBox.rotation);
impl.SetObb(QCARRuntimeUtilities.CameraFrameToScreenSpaceCoordinates(cameraFrameObb, viewportRectangle, videoBackGroundMirrored, videoMode));
}
if (this.mWordPrefabCreationMode == WordPrefabCreationMode.DUPLICATE)
{
this.UpdateWordBehaviourPoses();
}
}
}
private void UpdateWords(IEnumerable<QCARManagerImpl.WordData> newWordData, IEnumerable<QCARManagerImpl.WordResultData> wordResults)
{
this.mNewWords.Clear();
this.mLostWords.Clear();
foreach (QCARManagerImpl.WordData data in newWordData)
{
if (!this.mTrackedWords.ContainsKey(data.id))
{
string text = Marshal.PtrToStringUni(data.stringValue);
WordImpl word = new WordImpl(data.id, text, data.size);
WordResultImpl impl2 = new WordResultImpl(word);
this.mTrackedWords.Add(data.id, impl2);
this.mNewWords.Add(impl2);
}
}
List<int> list = new List<int>();
foreach (QCARManagerImpl.WordResultData data2 in wordResults)
{
list.Add(data2.id);
}
foreach (int num in this.mTrackedWords.Keys.ToList<int>())
{
if (!list.Contains(num))
{
this.mLostWords.Add(this.mTrackedWords[num].Word);
this.mTrackedWords.Remove(num);
}
}
if (this.mWordPrefabCreationMode == WordPrefabCreationMode.DUPLICATE)
{
this.UnregisterLostWords();
this.AssociateWordResultsWithBehaviours();
}
}
internal void UpdateWords(Camera arCamera, QCARManagerImpl.WordData[] newWordData, QCARManagerImpl.WordResultData[] wordResults)
{
this.UpdateWords(newWordData, wordResults);
this.UpdateWordResultPoses(arCamera, wordResults);
}
}