-
Notifications
You must be signed in to change notification settings - Fork 1
Fix getCameraPose() on Android (replicates upstream #19) #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,4 +45,4 @@ repomix-output.txt | |
| *.log | ||
| .env | ||
| *.tmp | ||
|
|
||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import 'dart:math' show sqrt; | ||
| import 'dart:typed_data'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:ar_flutter_plugin_2/datatypes/config_planedetection.dart'; | ||
| import 'package:ar_flutter_plugin_2/models/ar_anchor.dart'; | ||
|
|
@@ -49,9 +50,18 @@ class ARSessionManager { | |
| /// Returns the camera pose in Matrix4 format with respect to the world coordinate system of the [ARView] | ||
| Future<Matrix4?> getCameraPose() async { | ||
| try { | ||
| final serializedCameraPose = | ||
| await _channel.invokeMethod<List<dynamic>>('getCameraPose', {}); | ||
| return MatrixConverter().fromJson(serializedCameraPose!); | ||
| if (Platform.isAndroid) { | ||
| final serializedCameraPose = await _channel.invokeMethod<Map<dynamic, dynamic>>('getCameraPose', {}); | ||
| final position = serializedCameraPose!['position']; | ||
| final rotation = serializedCameraPose!['rotation']; | ||
|
|
||
| final translation = Vector3(position!['x'], position!['y'], position!['z']); | ||
| final quaternion = Quaternion(rotation!['x'], rotation!['y'], rotation!['z'], rotation!['w']); | ||
| return Matrix4.compose(translation, quaternion, Vector3.all(1.0)); | ||
| } else { | ||
| final serializedCameraPose = await _channel.invokeMethod<List<dynamic>>('getCameraPose', {}); | ||
| return MatrixConverter().fromJson(serializedCameraPose!); | ||
| } | ||
|
Comment on lines
+53
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the null assertion operator ( Additionally, values returned from platform channels for numeric coordinates can sometimes be serialized as Furthermore, using Here is a safer, more robust implementation using defensive checks, safe double conversions, and if (defaultTargetPlatform == TargetPlatform.android) {
final serializedCameraPose = await _channel.invokeMethod<Map<dynamic, dynamic>>('getCameraPose', {});
if (serializedCameraPose == null) return null;
final position = serializedCameraPose['position'] as Map?;
final rotation = serializedCameraPose['rotation'] as Map?;
if (position == null || rotation == null) return null;
final translation = Vector3(
(position['x'] as num).toDouble(),
(position['y'] as num).toDouble(),
(position['z'] as num).toDouble(),
);
final quaternion = Quaternion(
(rotation['x'] as num).toDouble(),
(rotation['y'] as num).toDouble(),
(rotation['z'] as num).toDouble(),
(rotation['w'] as num).toDouble(),
);
return Matrix4.compose(translation, quaternion, Vector3.all(1.0));
} else {
final serializedCameraPose = await _channel.invokeMethod<List<dynamic>>('getCameraPose', {});
if (serializedCameraPose == null) return null;
return MatrixConverter().fromJson(serializedCameraPose);
} |
||
| } catch (e) { | ||
| print('Error caught: ' + e.toString()); | ||
| return null; | ||
|
|
@@ -122,7 +132,7 @@ class ARSessionManager { | |
| } | ||
|
|
||
| //Show or hide planes | ||
| void showPlanes(bool showPlanes){ | ||
| void showPlanes(bool showPlanes) { | ||
| _channel.invokeMethod<void>('showPlanes', { | ||
| "showPlanes": showPlanes, | ||
| }); | ||
|
|
@@ -138,8 +148,7 @@ class ARSessionManager { | |
| if (onError != null) { | ||
| onError!(call.arguments[0]); | ||
| print(call.arguments); | ||
| } | ||
| else{ | ||
| } else { | ||
| ScaffoldMessenger.of(buildContext).showSnackBar(SnackBar( | ||
| content: Text(call.arguments[0]), | ||
| action: SnackBarAction( | ||
|
|
@@ -207,7 +216,6 @@ class ARSessionManager { | |
| }); | ||
| } | ||
|
|
||
|
|
||
| /// Dispose the AR view on the platforms to pause the scenes and disconnect the platform handlers. | ||
| /// You should call this before removing the AR view to prevent out of memory erros | ||
| dispose() async { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
Platform.isAndroidcan be replaced withdefaultTargetPlatform, this import is no longer needed and should be removed to keep the imports clean and avoid unnecessary dependencies ondart:io.