-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathexternalmodelmanagementexample.dart
627 lines (569 loc) · 20.4 KB
/
externalmodelmanagementexample.dart
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
import 'dart:convert';
import 'package:ar_flutter_plugin/managers/ar_location_manager.dart';
import 'package:ar_flutter_plugin/managers/ar_session_manager.dart';
import 'package:ar_flutter_plugin/managers/ar_object_manager.dart';
import 'package:ar_flutter_plugin/managers/ar_anchor_manager.dart';
import 'package:ar_flutter_plugin/models/ar_anchor.dart';
import 'package:flutter/material.dart';
import 'package:ar_flutter_plugin/ar_flutter_plugin.dart';
import 'package:ar_flutter_plugin/datatypes/config_planedetection.dart';
import 'package:ar_flutter_plugin/datatypes/node_types.dart';
import 'package:ar_flutter_plugin/datatypes/hittest_result_types.dart';
import 'package:ar_flutter_plugin/models/ar_node.dart';
import 'package:ar_flutter_plugin/models/ar_hittest_result.dart';
import 'package:vector_math/vector_math_64.dart' as VectorMath;
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:geolocator/geolocator.dart';
class ExternalModelManagementWidget extends StatefulWidget {
ExternalModelManagementWidget({Key? key}) : super(key: key);
@override
_ExternalModelManagementWidgetState createState() =>
_ExternalModelManagementWidgetState();
}
class _ExternalModelManagementWidgetState
extends State<ExternalModelManagementWidget> {
// Firebase stuff
bool _initialized = false;
bool _error = false;
FirebaseManager firebaseManager = FirebaseManager();
Map<String, Map> anchorsInDownloadProgress = Map<String, Map>();
ARSessionManager? arSessionManager;
ARObjectManager? arObjectManager;
ARAnchorManager? arAnchorManager;
ARLocationManager? arLocationManager;
List<ARNode> nodes = [];
List<ARAnchor> anchors = [];
String lastUploadedAnchor = "";
AvailableModel selectedModel = AvailableModel(
"Duck",
"https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Duck/glTF-Binary/Duck.glb",
"");
bool readyToUpload = false;
bool readyToDownload = true;
bool modelChoiceActive = false;
@override
void initState() {
firebaseManager.initializeFlutterFire().then((value) => setState(() {
_initialized = value;
_error = !value;
}));
super.initState();
}
@override
void dispose() {
super.dispose();
arSessionManager!.dispose();
}
@override
Widget build(BuildContext context) {
// Show error message if initialization failed
if (_error) {
return Scaffold(
appBar: AppBar(
title: const Text('External Model Management'),
),
body: Container(
child: Center(
child: Column(
children: [
Text("Firebase initialization failed"),
ElevatedButton(
child: Text("Retry"), onPressed: () => {initState()})
],
))));
}
// Show a loader until FlutterFire is initialized
if (!_initialized) {
return Scaffold(
appBar: AppBar(
title: const Text('External Model Management'),
),
body: Container(
child: Center(
child: Column(children: [
CircularProgressIndicator(),
Text("Initializing Firebase")
]))));
}
return Scaffold(
appBar: AppBar(
title: const Text('External Model Management'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.pets),
onPressed: () {
setState(() {
modelChoiceActive = !modelChoiceActive;
});
},
),
]),
body: Container(
child: Stack(children: [
ARView(
onARViewCreated: onARViewCreated,
planeDetectionConfig: PlaneDetectionConfig.horizontalAndVertical,
),
Align(
alignment: FractionalOffset.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: onRemoveEverything,
child: Text("Remove Everything")),
]),
),
Align(
alignment: FractionalOffset.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Visibility(
visible: readyToUpload,
child: ElevatedButton(
onPressed: onUploadButtonPressed,
child: Text("Upload"))),
Visibility(
visible: readyToDownload,
child: ElevatedButton(
onPressed: onDownloadButtonPressed,
child: Text("Download"))),
]),
),
Align(
alignment: FractionalOffset.centerLeft,
child: Visibility(
visible: modelChoiceActive,
child: ModelSelectionWidget(
onTap: onModelSelected,
firebaseManager: this.firebaseManager)))
])));
}
void onARViewCreated(
ARSessionManager arSessionManager,
ARObjectManager arObjectManager,
ARAnchorManager arAnchorManager,
ARLocationManager arLocationManager) {
this.arSessionManager = arSessionManager;
this.arObjectManager = arObjectManager;
this.arAnchorManager = arAnchorManager;
this.arLocationManager = arLocationManager;
this.arSessionManager!.onInitialize(
showFeaturePoints: false,
showPlanes: true,
customPlaneTexturePath: "Images/triangle.png",
showWorldOrigin: true,
);
this.arObjectManager!.onInitialize();
this.arAnchorManager!.initGoogleCloudAnchorMode();
this.arSessionManager!.onPlaneOrPointTap = onPlaneOrPointTapped;
this.arObjectManager!.onNodeTap = onNodeTapped;
this.arAnchorManager!.onAnchorUploaded = onAnchorUploaded;
this.arAnchorManager!.onAnchorDownloaded = onAnchorDownloaded;
this
.arLocationManager!
.startLocationUpdates()
.then((value) => null)
.onError((error, stackTrace) {
switch (error.toString()) {
case 'Location services disabled':
{
showAlertDialog(
context,
"Action Required",
"To use cloud anchor functionality, please enable your location services",
"Settings",
this.arLocationManager!.openLocationServicesSettings,
"Cancel");
break;
}
case 'Location permissions denied':
{
showAlertDialog(
context,
"Action Required",
"To use cloud anchor functionality, please allow the app to access your device's location",
"Retry",
this.arLocationManager!.startLocationUpdates,
"Cancel");
break;
}
case 'Location permissions permanently denied':
{
showAlertDialog(
context,
"Action Required",
"To use cloud anchor functionality, please allow the app to access your device's location",
"Settings",
this.arLocationManager!.openAppPermissionSettings,
"Cancel");
break;
}
default:
{
this.arSessionManager!.onError(error.toString());
break;
}
}
this.arSessionManager!.onError(error.toString());
});
}
void onModelSelected(AvailableModel model) {
this.selectedModel = model;
this.arSessionManager!.onError(model.name + " selected");
setState(() {
modelChoiceActive = false;
});
}
Future<void> onRemoveEverything() async {
anchors.forEach((anchor) {
this.arAnchorManager!.removeAnchor(anchor);
});
anchors = [];
if (lastUploadedAnchor != "") {
setState(() {
readyToDownload = true;
readyToUpload = false;
});
} else {
setState(() {
readyToDownload = true;
readyToUpload = false;
});
}
}
Future<void> onNodeTapped(List<String> nodeNames) async {
var foregroundNode = nodes.firstWhere((element) => element.name == nodeNames.first);
this.arSessionManager!.onError(foregroundNode.data!["onTapText"]);
}
Future<void> onPlaneOrPointTapped(
List<ARHitTestResult> hitTestResults) async {
var singleHitTestResult = hitTestResults.firstWhere(
(hitTestResult) => hitTestResult.type == ARHitTestResultType.plane);
if (singleHitTestResult != null) {
var newAnchor = ARPlaneAnchor(
transformation: singleHitTestResult.worldTransform, ttl: 2);
bool? didAddAnchor = await this.arAnchorManager!.addAnchor(newAnchor);
if (didAddAnchor!) {
this.anchors.add(newAnchor);
// Add note to anchor
var newNode = ARNode(
type: NodeType.webGLB,
uri: this.selectedModel.uri,
scale: VectorMath.Vector3(0.2, 0.2, 0.2),
position: VectorMath.Vector3(0.0, 0.0, 0.0),
rotation: VectorMath.Vector4(1.0, 0.0, 0.0, 0.0),
data: {"onTapText": "I am a " + this.selectedModel.name});
bool? didAddNodeToAnchor =
await this.arObjectManager!.addNode(newNode, planeAnchor: newAnchor);
if (didAddNodeToAnchor!) {
this.nodes.add(newNode);
setState(() {
readyToUpload = true;
});
} else {
this.arSessionManager!.onError("Adding Node to Anchor failed");
}
} else {
this.arSessionManager!.onError("Adding Anchor failed");
}
}
}
Future<void> onUploadButtonPressed() async {
this.arAnchorManager!.uploadAnchor(this.anchors.last);
setState(() {
readyToUpload = false;
});
}
onAnchorUploaded(ARAnchor anchor) {
// Upload anchor information to firebase
firebaseManager.uploadAnchor(anchor,
currentLocation: this.arLocationManager!.currentLocation);
// Upload child nodes to firebase
if (anchor is ARPlaneAnchor) {
anchor.childNodes.forEach((nodeName) => firebaseManager.uploadObject(
nodes.firstWhere((element) => element.name == nodeName)));
}
setState(() {
readyToDownload = true;
readyToUpload = false;
});
this.arSessionManager!.onError("Upload successful");
}
ARAnchor onAnchorDownloaded(Map<String,dynamic> serializedAnchor) {
final anchor = ARPlaneAnchor.fromJson(anchorsInDownloadProgress[serializedAnchor["cloudanchorid"]] as Map<String,dynamic>);
anchorsInDownloadProgress.remove(anchor.cloudanchorid);
this.anchors.add(anchor);
// Download nodes attached to this anchor
firebaseManager.getObjectsFromAnchor(anchor, (snapshot) {
snapshot.docs.forEach((objectDoc) {
ARNode object = ARNode.fromMap(objectDoc.data() as Map<String, dynamic>);
arObjectManager!.addNode(object, planeAnchor: anchor);
this.nodes.add(object);
});
});
return anchor;
}
Future<void> onDownloadButtonPressed() async {
//this.arAnchorManager.downloadAnchor(lastUploadedAnchor);
//firebaseManager.downloadLatestAnchor((snapshot) {
// final cloudAnchorId = snapshot.docs.first.get("cloudanchorid");
// anchorsInDownloadProgress[cloudAnchorId] = snapshot.docs.first.data();
// arAnchorManager.downloadAnchor(cloudAnchorId);
//});
// Get anchors within a radius of 100m of the current device's location
if (this.arLocationManager!.currentLocation != null) {
firebaseManager.downloadAnchorsByLocation((snapshot) {
final cloudAnchorId = snapshot.get("cloudanchorid");
anchorsInDownloadProgress[cloudAnchorId] = snapshot.data() as Map<String, dynamic>;
arAnchorManager!.downloadAnchor(cloudAnchorId);
}, this.arLocationManager!.currentLocation, 0.1);
setState(() {
readyToDownload = false;
});
} else {
this
.arSessionManager!
.onError("Location updates not running, can't download anchors");
}
}
void showAlertDialog(BuildContext context, String title, String content,
String buttonText, Function buttonFunction, String cancelButtonText) {
// set up the buttons
Widget cancelButton = ElevatedButton(
child: Text(cancelButtonText),
onPressed: () {
Navigator.of(context).pop();
},
);
Widget actionButton = ElevatedButton(
child: Text(buttonText),
onPressed: () {
buttonFunction();
Navigator.of(context).pop();
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text(title),
content: Text(content),
actions: [
cancelButton,
actionButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}
// Class for managing interaction with Firebase (in your own app, this can be put in a separate file to keep everything clean and tidy)
typedef FirebaseListener = void Function(QuerySnapshot snapshot);
typedef FirebaseDocumentStreamListener = void Function(
DocumentSnapshot snapshot);
class FirebaseManager {
FirebaseFirestore? firestore;
Geoflutterfire? geo;
CollectionReference? anchorCollection;
CollectionReference? objectCollection;
CollectionReference? modelCollection;
// Firebase initialization function
Future<bool> initializeFlutterFire() async {
try {
// Wait for Firebase to initialize
await Firebase.initializeApp();
geo = Geoflutterfire();
firestore = FirebaseFirestore.instance;
anchorCollection = FirebaseFirestore.instance.collection('anchors');
objectCollection = FirebaseFirestore.instance.collection('objects');
modelCollection = FirebaseFirestore.instance.collection('models');
return true;
} catch (e) {
return false;
}
}
void uploadAnchor(ARAnchor anchor, {Position? currentLocation}) {
if (firestore == null) return;
var serializedAnchor = anchor.toJson();
var expirationTime = DateTime.now().millisecondsSinceEpoch / 1000 +
serializedAnchor["ttl"] * 24 * 60 * 60;
serializedAnchor["expirationTime"] = expirationTime;
// Add location
if (currentLocation != null) {
GeoFirePoint myLocation = geo!.point(
latitude: currentLocation.latitude,
longitude: currentLocation.longitude);
serializedAnchor["position"] = myLocation.data;
}
anchorCollection!
.add(serializedAnchor)
.then((value) =>
print("Successfully added anchor: " + serializedAnchor["name"]))
.catchError((error) => print("Failed to add anchor: $error"));
}
void uploadObject(ARNode node) {
if (firestore == null) return;
var serializedNode = node.toMap();
objectCollection!
.add(serializedNode)
.then((value) =>
print("Successfully added object: " + serializedNode["name"]))
.catchError((error) => print("Failed to add object: $error"));
}
void downloadLatestAnchor(FirebaseListener listener) {
anchorCollection!
.orderBy("expirationTime", descending: false)
.limitToLast(1)
.get()
.then((value) => listener(value))
.catchError(
(error) => (error) => print("Failed to download anchor: $error"));
}
void downloadAnchorsByLocation(FirebaseDocumentStreamListener listener,
Position location, double radius) {
GeoFirePoint center =
geo!.point(latitude: location.latitude, longitude: location.longitude);
Stream<List<DocumentSnapshot>> stream = geo!
.collection(collectionRef: anchorCollection!)
.within(center: center, radius: radius, field: 'position');
stream.listen((List<DocumentSnapshot> documentList) {
documentList.forEach((element) {
listener(element);
});
});
}
void downloadAnchorsByChannel() {}
void getObjectsFromAnchor(ARPlaneAnchor anchor, FirebaseListener listener) {
objectCollection!
.where("name", whereIn: anchor.childNodes)
.get()
.then((value) => listener(value))
.catchError((error) => print("Failed to download objects: $error"));
}
void deleteExpiredDatabaseEntries() {
WriteBatch batch = FirebaseFirestore.instance.batch();
anchorCollection!
.where("expirationTime",
isLessThan: DateTime.now().millisecondsSinceEpoch / 1000)
.get()
.then((anchorSnapshot) => anchorSnapshot.docs.forEach((anchorDoc) {
// Delete all objects attached to the expired anchor
objectCollection!
.where("name", arrayContainsAny: anchorDoc.get("childNodes"))
.get()
.then((objectSnapshot) => objectSnapshot.docs.forEach(
(objectDoc) => batch.delete(objectDoc.reference)));
// Delete the expired anchor
batch.delete(anchorDoc.reference);
}));
batch.commit();
}
void downloadAvailableModels(FirebaseListener listener) {
modelCollection!
.get()
.then((value) => listener(value))
.catchError((error) => print("Failed to download objects: $error"));
}
}
class AvailableModel {
String name;
String uri;
String image;
AvailableModel(this.name, this.uri, this.image);
}
class ModelSelectionWidget extends StatefulWidget {
final Function onTap;
final FirebaseManager firebaseManager;
ModelSelectionWidget({required this.onTap, required this.firebaseManager});
@override
_ModelSelectionWidgetState createState() => _ModelSelectionWidgetState();
}
class _ModelSelectionWidgetState extends State<ModelSelectionWidget> {
List<AvailableModel> models = [];
String? selected;
@override
void initState() {
super.initState();
widget.firebaseManager.downloadAvailableModels((snapshot) {
snapshot.docs.forEach((element) {
setState(() {
models.add(AvailableModel(element.get("name"), element.get("uri"),
element.get("image").first["downloadURL"]));
});
});
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
DecoratedBox(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
style: BorderStyle.solid,
width: 4.0,
),
borderRadius: BorderRadius.all(Radius.circular(5)),
shape: BoxShape.rectangle,
boxShadow: const <BoxShadow>[
BoxShadow(
color: Color(0x66000000),
blurRadius: 10.0,
spreadRadius: 4.0,
)
],
),
child: Text('Choose a Model',
style: DefaultTextStyle.of(context)
.style
.apply(fontSizeFactor: 2.0)),
),
Container(
height: MediaQuery.of(context).size.width * 0.65,
child: ListView.builder(
itemCount: models.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
widget.onTap(models[index]);
},
child: Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
child: Column(
children: [
Padding(
padding: EdgeInsets.all(20),
child: Image.network(models[index].image)),
Text(
models[index].name,
style: DefaultTextStyle.of(context)
.style
.apply(fontSizeFactor: 2.0),
)
],
),
),
);
},
),
)
]);
}
}