Android 安卓阿科测量距离

Android 安卓阿科测量距离,android,distance,measure,arcore,Android,Distance,Measure,Arcore,我正在开发AR应用程序,并且正在使用VirocoreSDK。问题是如何借助ViroCore SDK测量距离?我应该把两个物体放在表面上,然后用它们的坐标来做吗?或者有其他方法来做吗?现在我正在尝试做一些类似于本文所述的事情这是我的课程,用于测量放置在AR中的两个对象之间的距离: public class ARRuler { public static final String TAG = ARRuler.class.getSimpleName(); private static final

我正在开发AR应用程序,并且正在使用VirocoreSDK。问题是如何借助ViroCore SDK测量距离?我应该把两个物体放在表面上,然后用它们的坐标来做吗?或者有其他方法来做吗?现在我正在尝试做一些类似于本文所述的事情

这是我的课程,用于测量放置在AR中的两个对象之间的距离:

public class ARRuler {

public static final String TAG = ARRuler.class.getSimpleName();

private static final long PROCESS_POINT_DELAY = 1500L;

private static final int BOX_COLOR = Color.parseColor("#FF0000");
private static final float BOX_WIDTH = 0.05f;
private static final float BOX_HEIGHT = 0.05f;
private static final float BOX_LENGTH = 0.05f;

private static final int DISTANCE_LABEL_HEIGHT = 100;
private static final int DISTANCE_LABEL_WIDTH = 100;
private static final int DISTANCE_LABEL_TEXT_COLOR = Color.parseColor("#FF0000");
private static final int DISTANCE_LABEL_TEXT_SIZE = 10;

private static final float POLYLINE_THICKNESS = 0.05f;
private static final int POLYLINE_COLOR = Color.parseColor("#FFFFFF");


private ViroViewARCore mViroViewARCore;
private ARScene mARScene;
private List<Node> mSelectedNodes;
private List<Float> mDistances;

public ARRuler(ViroViewARCore viroViewARCore, ARScene arScene) {
    this.mViroViewARCore = viroViewARCore;
    this.mARScene = arScene;
    this.mSelectedNodes = new ArrayList<>();
    this.mDistances = new ArrayList<>();
}

public void pointSelected(Vector position) {
    Log.d(TAG, "pointSelected: " + position);

    Box box = new Box(BOX_WIDTH, BOX_HEIGHT, BOX_LENGTH);

    Material material = new Material();
    material.setDiffuseColor(BOX_COLOR);
    box.setMaterials(Collections.singletonList(material));

    Node boxNode = new Node();
    boxNode.setGeometry(box);
    boxNode.setPosition(position);

    if (mSelectedNodes.size() == 0) {
        boxNode.setClickListener(new ClickListener() {
            @Override
            public void onClick(int i, Node node, Vector vector) {
                Log.d(TAG, "onClick: First point clicked");

               mSelectedNodes.add(node);
               startDelayedProcessing();
            }

            @Override
            public void onClickState(int i, Node node, ClickState clickState, Vector vector) {

            }
        });
    }

    mARScene.getRootNode().addChildNode(boxNode);
    mSelectedNodes.add(boxNode);

    startDelayedProcessing();
}

private void startDelayedProcessing() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            processSelectedPoints();
        }
    }, PROCESS_POINT_DELAY);
}

private void processSelectedPoints() {
    Log.d(TAG, "processSelectedPoints: ");

    if (mSelectedNodes.size() < 2) {
        Log.d(TAG, "processSelectedPoints: Only one point, return");
        return;
    }

    for (int i = 0; i < mSelectedNodes.size(); i++) {
        Log.d(TAG, "processSelectedPoints: Start looping...");

        if (i + 1 == mSelectedNodes.size()) {
            Log.d(TAG, "processSelectedPoints: Last pair reached, return");
            return;
        }

        Node start = mSelectedNodes.get(i);
        Node end = mSelectedNodes.get(i + 1);

        drawDistanceLabel(start, end);
        drawPolylines();
    }
}

private void drawDistanceLabel(Node start, Node end) {
    Vector startVector = start.getPositionRealtime();
    Vector endVector = end.getPositionRealtime();

    Log.d(TAG, "drawDistanceLabel: Vectors created!");
    Log.d(TAG, "drawDistanceLabel: startVector=" + startVector);
    Log.d(TAG, "drawDistanceLabel: endVector=" + endVector);

    float distance = startVector.distance(endVector);
    Log.d(TAG, "drawDistanceLabel: distance=" + distance);
    mDistances.add(distance);

    String distanceString = new DecimalFormat("#.###").format(distance);
    Log.d(TAG, "drawDistanceLabel: distanceString=" + distanceString);

    Text text = new Text(mViroViewARCore.getViroContext(), distanceString,
            DISTANCE_LABEL_WIDTH, DISTANCE_LABEL_HEIGHT);
    text.setColor(DISTANCE_LABEL_TEXT_COLOR);
    text.setFontSize(DISTANCE_LABEL_TEXT_SIZE);

    Node node = new Node();
    node.setGeometry(text);

    Vector labelPosition = startVector.midpoint(endVector);
    node.setPosition(labelPosition);

    mARScene.getRootNode().addChildNode(node);
}

private void drawPolylines() {
    Log.d(TAG, "drawPolylines: ");

    Polyline polyline = new Polyline(POLYLINE_THICKNESS);
    polyline.setPoints(getVectors());

    Material material = new Material();
    material.setDiffuseColor(POLYLINE_COLOR);
    polyline.setMaterials(Collections.singletonList(material));

    Node node = new Node();
    node.setGeometry(polyline);

    mARScene.getRootNode().addChildNode(node);
}

private List<Vector> getVectors() {
    Log.d(TAG, "getVectors: ");

    List<Vector> vectors = new ArrayList<>();

    for (Node node : mSelectedNodes) {
        vectors.add(node.getPositionRealtime());
    }

    return vectors;
}
公共类ARRuler{
公共静态最终字符串标记=ARRuler.class.getSimpleName();
专用静态最终长过程\点\延迟=1500L;
私有静态最终整型框_COLOR=COLOR.parseColor(“#FF0000”);
专用静态最终浮箱_宽度=0.05f;
专用静态最终浮箱高度=0.05f;
专用静态最终浮箱长度=0.05f;
专用静态最终整数距离\标签\高度=100;
专用静态最终整数距离\标签\宽度=100;
私有静态最终整数距离_标签_文本_颜色=COLOR.parseColor(“#FF0000”);
专用静态最终整数距离\标签\文本\大小=10;
专用静态最终浮动多段线_厚度=0.05f;
私有静态最终整数多段线_COLOR=COLOR.parseColor(“#FFFFFF”);
私有ViroViewARCore mViroViewARCore;
私人纵火现场;
私有列表mSelectedNodes;
私人名单;
公共ARRuler(ViroViewARCore ViroViewARCore,ARScene-ARScene){
this.mViroViewARCore=viroViewARCore;
this.mARScene=arScene;
this.mSelectedNodes=new ArrayList();
this.mdistance=new ArrayList();
}
已选择公共无效点(矢量位置){
Log.d(标记,“点选定:”+位置);
箱盒=新箱盒(箱盒宽度、箱盒高度、箱盒长度);
材料=新材料();
材料。setDiffuseColor(方框颜色);
box.setMaterials(Collections.singletonList(material));
Node boxNode=新节点();
setGeometry(box);
boxNode.setPosition(位置);
if(mSelectedNodes.size()==0){
setClickListener(新建ClickListener(){
@凌驾
公共void onClick(int i、节点、向量){
d(标记“onClick:First point clicked”);
mSelectedNodes.add(节点);
startDelayedProcessing();
}
@凌驾
public void onClickState(int i、节点节点、ClickState ClickState、向量向量){
}
});
}
mARScene.getRootNode().addChildNode(boxNode);
mSelectedNodes.add(boxNode);
startDelayedProcessing();
}
私有void startDelayedProcessing(){
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
processSelectedPoints();
}
},处理点(延迟);
}
私有void processSelectedPoints(){
Log.d(标记“processSelectedPoints:”);
if(mSelectedNodes.size()<2){
d(标记“processSelectedPoints:仅一个点,返回”);
回来
}
对于(int i=0;i
}

它会在单击的位置绘制红色框,添加两个框后,它会绘制多段线和文本标签,并在这些框之间绘制距离。要开始使用它,只需将其复制到项目中,并传递
ViroViewARCore
ARScene
对象。在此之后,只需在
节点中调用
ARRuler.pointSelected(selectedPosition)
。单击Listener()
回调


请随意提问。

这是我的课程,用于测量放置在AR中的两个对象之间的距离:

public class ARRuler {

public static final String TAG = ARRuler.class.getSimpleName();

private static final long PROCESS_POINT_DELAY = 1500L;

private static final int BOX_COLOR = Color.parseColor("#FF0000");
private static final float BOX_WIDTH = 0.05f;
private static final float BOX_HEIGHT = 0.05f;
private static final float BOX_LENGTH = 0.05f;

private static final int DISTANCE_LABEL_HEIGHT = 100;
private static final int DISTANCE_LABEL_WIDTH = 100;
private static final int DISTANCE_LABEL_TEXT_COLOR = Color.parseColor("#FF0000");
private static final int DISTANCE_LABEL_TEXT_SIZE = 10;

private static final float POLYLINE_THICKNESS = 0.05f;
private static final int POLYLINE_COLOR = Color.parseColor("#FFFFFF");


private ViroViewARCore mViroViewARCore;
private ARScene mARScene;
private List<Node> mSelectedNodes;
private List<Float> mDistances;

public ARRuler(ViroViewARCore viroViewARCore, ARScene arScene) {
    this.mViroViewARCore = viroViewARCore;
    this.mARScene = arScene;
    this.mSelectedNodes = new ArrayList<>();
    this.mDistances = new ArrayList<>();
}

public void pointSelected(Vector position) {
    Log.d(TAG, "pointSelected: " + position);

    Box box = new Box(BOX_WIDTH, BOX_HEIGHT, BOX_LENGTH);

    Material material = new Material();
    material.setDiffuseColor(BOX_COLOR);
    box.setMaterials(Collections.singletonList(material));

    Node boxNode = new Node();
    boxNode.setGeometry(box);
    boxNode.setPosition(position);

    if (mSelectedNodes.size() == 0) {
        boxNode.setClickListener(new ClickListener() {
            @Override
            public void onClick(int i, Node node, Vector vector) {
                Log.d(TAG, "onClick: First point clicked");

               mSelectedNodes.add(node);
               startDelayedProcessing();
            }

            @Override
            public void onClickState(int i, Node node, ClickState clickState, Vector vector) {

            }
        });
    }

    mARScene.getRootNode().addChildNode(boxNode);
    mSelectedNodes.add(boxNode);

    startDelayedProcessing();
}

private void startDelayedProcessing() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            processSelectedPoints();
        }
    }, PROCESS_POINT_DELAY);
}

private void processSelectedPoints() {
    Log.d(TAG, "processSelectedPoints: ");

    if (mSelectedNodes.size() < 2) {
        Log.d(TAG, "processSelectedPoints: Only one point, return");
        return;
    }

    for (int i = 0; i < mSelectedNodes.size(); i++) {
        Log.d(TAG, "processSelectedPoints: Start looping...");

        if (i + 1 == mSelectedNodes.size()) {
            Log.d(TAG, "processSelectedPoints: Last pair reached, return");
            return;
        }

        Node start = mSelectedNodes.get(i);
        Node end = mSelectedNodes.get(i + 1);

        drawDistanceLabel(start, end);
        drawPolylines();
    }
}

private void drawDistanceLabel(Node start, Node end) {
    Vector startVector = start.getPositionRealtime();
    Vector endVector = end.getPositionRealtime();

    Log.d(TAG, "drawDistanceLabel: Vectors created!");
    Log.d(TAG, "drawDistanceLabel: startVector=" + startVector);
    Log.d(TAG, "drawDistanceLabel: endVector=" + endVector);

    float distance = startVector.distance(endVector);
    Log.d(TAG, "drawDistanceLabel: distance=" + distance);
    mDistances.add(distance);

    String distanceString = new DecimalFormat("#.###").format(distance);
    Log.d(TAG, "drawDistanceLabel: distanceString=" + distanceString);

    Text text = new Text(mViroViewARCore.getViroContext(), distanceString,
            DISTANCE_LABEL_WIDTH, DISTANCE_LABEL_HEIGHT);
    text.setColor(DISTANCE_LABEL_TEXT_COLOR);
    text.setFontSize(DISTANCE_LABEL_TEXT_SIZE);

    Node node = new Node();
    node.setGeometry(text);

    Vector labelPosition = startVector.midpoint(endVector);
    node.setPosition(labelPosition);

    mARScene.getRootNode().addChildNode(node);
}

private void drawPolylines() {
    Log.d(TAG, "drawPolylines: ");

    Polyline polyline = new Polyline(POLYLINE_THICKNESS);
    polyline.setPoints(getVectors());

    Material material = new Material();
    material.setDiffuseColor(POLYLINE_COLOR);
    polyline.setMaterials(Collections.singletonList(material));

    Node node = new Node();
    node.setGeometry(polyline);

    mARScene.getRootNode().addChildNode(node);
}

private List<Vector> getVectors() {
    Log.d(TAG, "getVectors: ");

    List<Vector> vectors = new ArrayList<>();

    for (Node node : mSelectedNodes) {
        vectors.add(node.getPositionRealtime());
    }

    return vectors;
}
公共类ARRuler{
公共静态最终字符串标记=ARRuler.class.getSimpleName();
专用静态最终长过程\点\延迟=1500L;
私有静态最终整型框_COLOR=COLOR.parseColor(“#FF0000”);
专用静态最终浮箱_宽度=0.05f;
专用静态最终浮箱高度=0.05f;
专用静态最终浮箱长度=0.05f;
专用静态最终整数距离\标签\高度=100;
私有静态最终整数域