is there any way to draw the line btween AR nodes while moving with camera using ar_flutter_plugin

145 Views Asked by At

i want to draw the line bwt the ar nodes while moving with the camera , i have pose the nodes and calculating the area based on 4 nodes on anchor plane as shown in image.

nodes on aranchors

i want to add function based on the any of the manager given in the plugin

In this example, the vertices list contains the 3D coordinates of the four vertices of the irregular quadrilateral. The calculateArea function takes the list of vertices, converts them to vectors, calculates cross products, and sums up the areas of the triangles formed by the cross products.

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:flutter/services.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:vector_math/vector_math_64.dart';
import 'dart:math';

import '../../controllers/room_controller.dart';

class ObjectsOnPlanesWidget extends StatefulWidget {
  ObjectsOnPlanesWidget({Key? key}) : super(key: key);
  @override
  _ObjectsOnPlanesWidgetState createState() => _ObjectsOnPlanesWidgetState();
}

class _ObjectsOnPlanesWidgetState extends State<ObjectsOnPlanesWidget> {
  ARSessionManager? arSessionManager;
  ARObjectManager? arObjectManager;
  ARAnchorManager? arAnchorManager;

  List<ARNode> nodes = [];
  List<ARAnchor> anchors = [];

  //late RoomValues areaValues;




  bool drawingLine = false;
  // late var totalArea;
  final RoomController _roomController = Get.put(RoomController());

  //RoomModel dimension = _roomController.

  @override
  void initState() {
    super.initState();
    //final RoomController _roomController = Get.put(RoomController()); // Initialize the controller
  }

  @override
  void dispose() {
    super.dispose();
    arSessionManager!.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // RoomValues areaValue = RoomValues( // need to pass this in result
    //     length: _roomController.totalArea.value,
    //     width: _roomController.length.value,
    //     area: _roomController.width.value);
    return Scaffold(
        appBar: AppBar(
          title: const Text('Anchors & Objects on Planes'),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              _roomController.totalArea.value;
              _roomController.length.value;
              _roomController.width.value;
              Get.back(
                 // result: _roomController.totalArea.value
              ); // Navigate back to the previous screen
            },
          ),
        ),
        body: Container(
            child: Stack(children: [
          ARView(
            onARViewCreated: onARViewCreated,
            planeDetectionConfig: PlaneDetectionConfig.horizontal,
          ),
          // CustomPaint(
          //   painter: LinePainter(nodes: nodes),),
          Align(
            alignment: FractionalOffset.bottomCenter,
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  ElevatedButton(
                      onPressed: onRemoveEverything,
                      child: const Text("Remove Everything")),
                ]),
          )
        ])));
  }

  Future<void> onARViewCreated(
      ARSessionManager arSessionManager,
      ARObjectManager arObjectManager,
      ARAnchorManager arAnchorManager,
      ARLocationManager arLocationManager) async {
    this.arSessionManager = arSessionManager;
    this.arObjectManager = arObjectManager;
    this.arAnchorManager = arAnchorManager;

    this.arSessionManager!.onInitialize(
          showFeaturePoints: false,
          showPlanes: true,
          customPlaneTexturePath: "assets/images/tilePhoto.png",
          showWorldOrigin: true,
        );
    this.arObjectManager!.onInitialize();
    this.arSessionManager!.onPlaneOrPointTap = onPlaneOrPointTapped;
    this.arObjectManager!.onNodeTap = onNodeTapped;
    this.arObjectManager!.onPanStart = onPanStart;
    // if (nodes.length == 4) {
    //   double totalArea1 = await calculateAreaBetweenNodes();
    //   print("area1 init2 ... ${totalArea1.toString()}");
    //   _roomController.setTotalArea(totalArea1);
    // }
  }

  bool handlePans() {
    return true;
  }

  Future<void>onPanStart(String nodeName)async{

    Future<Matrix4?>? camerapose = this.arSessionManager?.getCameraPose();

    print("new node tapped with camerapose ... ${camerapose }");

  }

  Future<void> onRemoveEverything() async {
    nodes.forEach((node) {
      this.arObjectManager?.removeNode(node);
    });
    anchors.forEach((anchor) {
      this.arAnchorManager!.removeAnchor(anchor);
    });
    nodes = [];
    anchors = [];
  }

  Future<void> onNodeTapped(List<String> nodes) async {
    var number = nodes.length;
    this.arSessionManager!.onError("Tapped $number node(s)");
  }

  Future<void> onPlaneOrPointTapped(
      List<ARHitTestResult> hitTestResults) async {
    var singleHitTestResult = hitTestResults.firstWhere(
        (hitTestResult) => hitTestResult.type == ARHitTestResultType.plane);

    if (singleHitTestResult != null) {
      var newNode = ARNode(
          type: NodeType.webGLB,
          //localGLTF2,
          uri:
              "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Box/glTF-Binary/Box.glb",
          //"models/chicken/Chicken_01.gltf",
          scale: Vector3(0.05, 0.05, 0.05),
          transformation: singleHitTestResult.worldTransform);
      bool? didAddWebNode = await this.arObjectManager?.addNode(
            newNode,
          );
      if (didAddWebNode!) {
        this.nodes.add(newNode);
        print("new node added with position... ${newNode.position}");
        print("new node added with transform... ${newNode.transform}");
        if (nodes.length == 4) {
          //double totalArea = await
          await calculateAreaBetweenNodes();
          //print("area after function init ... ${totalArea.toString()}");
          //_roomController.setTotalArea(totalArea);

        }
      }
    }
  }
0

There are 0 best solutions below