# Algorithm

> 算法模块主要封装了slam,平面检测等能力.

<br>

#### SLAM

* 需要开启SLAM/6dof能力,只需要调用一行代码

<table data-header-hidden><thead><tr><th width="625"></th><th></th></tr></thead><tbody><tr><td><strong>EnableSlamHeadTracker</strong></td><td><br></td></tr><tr><td>void EnableSlamHeadTracker()</td><td></td></tr><tr><td><p>开启6dof能力</p><p>注意:</p><p>1.开启时眼镜需要静止1秒左右时间才能计算空间位置</p><p>2.开启后,眼镜相机不可用.</p></td><td></td></tr></tbody></table>

&#x20; 相机上只要挂载了HeadTrackedPoseDriver类,并绑定了通用input事件,即可从原有的3DoF转为6DoF.

<figure><img src="/files/F5KvJgxF6UfUMkQD9VGL" alt=""><figcaption></figcaption></figure>

* 如果不再使用slam,可调用如下接口关闭

  | **DisableSlamHeadTracker**   | <p><br></p> |
  | ---------------------------- | ----------- |
  | void EnableSlamHeadTracker() |             |
  | 关闭6dof能力                     |             |
* 可调用如下接口获取接口状态(可以在Update中每一帧查询)

  <table data-header-hidden><thead><tr><th width="568"></th><th></th></tr></thead><tbody><tr><td><strong>GetSlamStatus</strong></td><td><br></td></tr><tr><td>SlamState GetSlamStatus()</td><td></td></tr><tr><td><p>获取当前Slam的状态:</p><pre class="language-c#"><code class="lang-c#">public enum SlamState
  {
          //正在初始化
          FFVINS_INITIALIZING = 0,
          //初始化成功
          FFVINS_TRACKING_SUCCESS = 1,
          //失败
          FFVINS_TRACKING_FAIL = 2,
  }
  </code></pre></td><td></td></tr></tbody></table>

<br>

***

#### 平面检测

> 运行Sample中的PlaneDetection即可查看平面检测sample code. 在编辑器中,使用宏编写了测试数据.

<figure><img src="/files/Fo84SemrMJ1n4HUeGZ0Z" alt=""><figcaption></figcaption></figure>

* 打开平面检测,需要调用如下接口

  <table data-header-hidden><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>EnablePlaneDetection</strong></td><td><br></td></tr><tr><td>void EnablePlaneDetection()</td><td></td></tr><tr><td><p>开启平面检测能力.注意:调用该代码前,需要先调用EnableSlamHeadTracker()示例:</p><pre class="language-c#"><code class="lang-c#">AlgorithmApi.EnableSlamHeadTracker();
  AlgorithmApi.EnablePlaneDetection();
  </code></pre></td><td></td></tr></tbody></table>
* 使用如下接口获取平面检测结果,如果返回值>0,则检测到平面

  <table data-header-hidden><thead><tr><th width="501"></th><th></th></tr></thead><tbody><tr><td><strong>GetPlaneInfo</strong></td><td><br></td></tr><tr><td>int GetPlaneInfo(XRPlaneInfo[] info)</td><td></td></tr><tr><td><p>获取平面检测结果.</p><p>info:传入的数据长度取决于需要最多获知多少个平面,例如XRPlaneInfo[] info= new XRPlaneInfo[3]</p></td><td></td></tr></tbody></table>
* 对平面检测结果的数据进行简单的处理

  | **ConvertPlanePosition**                       | <p><br></p> |
  | ---------------------------------------------- | ----------- |
  | Vector3 ConvertPlanePosition(XRPlaneInfo info) |             |
  | 将检测到的平面数据传入,获取平面位置信息                           |             |

  | **ConvertPlaneRotation**                          | <p><br></p> |
  | ------------------------------------------------- | ----------- |
  | Quaternion ConvertPlaneRotation(XRPlaneInfo info) |             |
  | 将检测到的平面数据传入,获取平面旋转信息                              |             |
* 关闭平面检测

  | **DisablePlaneDetection**     | <p><br></p> |
  | ----------------------------- | ----------- |
  | void DisablePlaneDetection(); |             |
  | 关闭平面检测能力.                     |             |
* 在sample code中,内置了一套可以渲染出平面的接口.以便debug使用.

  <table data-header-hidden><thead><tr><th width="542"></th><th></th></tr></thead><tbody><tr><td><strong>CreatePlaneMesh</strong></td><td><br></td></tr><tr><td>GameObject CreatePlaneMesh(XRPlaneInfo onePlaneMeshInfo, GameObject obj, bool includeMaterial = false, bool invertYZ = false)</td><td></td></tr><tr><td>平面模型重建,将检测到的平面数据传入,该方法就会创建一个GameObject并且添加对应的Mesh</td><td></td></tr></tbody></table>

<table data-header-hidden><thead><tr><th width="571"></th><th></th></tr></thead><tbody><tr><td><strong>SetUnityCameraToPhysicalCameraFOV</strong></td><td><br></td></tr><tr><td>Void SetUnityCameraToPhysicalCameraFOV(Camera camera)</td><td></td></tr><tr><td>将unity的渲染相机参数设置成眼镜的实体相机参数</td><td></td></tr></tbody></table>

<table data-header-hidden><thead><tr><th width="576"></th><th></th></tr></thead><tbody><tr><td><strong>GetAzimuth</strong></td><td><br></td></tr><tr><td>float GetAzimuth()</td><td></td></tr><tr><td>获取带磁力计的偏航角</td><td></td></tr></tbody></table>

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rayneo.gitbook.io/rayneo-devdoc/x-xi-lie/unity-kai-fa/x2-api-jiu-ban-sdk-zhi-chi/algorithm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
