我为我的长篇文章提前道歉.
我的目的是为Project Tango Yellowstone设备创建一个网格化应用程序,以创建建筑内部的3D地图.我打算利用最近版本的tango-examples-c代码中添加的实验网格API.
我使用point-cloud-jni-example(turing)作为起点,到目前为止已完成以下操作:
>在point_cloud_app.cc中设置config_experimental_enable_scene_reconstruction tango配置参数(参见docs)
// Enable scene reconstruction
ret = TangoConfig_setBool(tango_config_,
config_experimental_enable_scene_reconstruction", true);
if (ret != TANGO_SUCCESS) {
LOGE("PointCloudApp: config_experimental_enable_scene_reconstruction() Failed"
"with error code: %d", ret);
return ret;
}
>在TangoJNINative.java中添加了extractMesh本机方法
// Extracts the full mesh from the scene reconstruction.
public static native float extractMesh();
>将匹配的extractMesh函数添加到jni_interface.cc
JNIEXPORT void JNICALL
Java_com_projecttango_experiments_nativepointcloud_TangoJNINative_extractMesh(
jnienv*, jobject) {
app.ExtractMesh();
}
>在point_cloud_app.cc中添加了ExtractMesh方法
void PointCloudApp::ExtractMesh() {
// see line 1245 of tango_client_api.h
mesh_ptr = new TangoMesh_Experimental();
TangoService_Experimental_extractMesh(mesh_ptr);
mesh = *mesh_ptr;
LOGE("PointCloudApp: num_vertices: %d", mesh.num_vertices);
float float1, float2, float3;
float1 = mesh.vertices[1][0];
float2 = mesh.vertices[1][1];
float3 = float1 + float2; // these lines show I can use the vertex data
LOGE("PointCloudApp: First vertex, x: %f", mesh.vertices[1][0]); // this line causes app to crash; printing the vertex data seems to be the problem
}
>将TangoMesh_Experimental声明添加到point_cloud_app.h
// see line 1131 of tango_client_api.h
TangoMesh_Experimental* mesh_ptr;
TangoMesh_Experimental mesh;
>添加了一个额外的按钮来调用extractMesh本机方法. (没有显示这个,因为它非常简单)
作为参考,这里是API的TangoMesh_Experimental Struct:
// A mesh, described by vertices and face indices, with optional per-vertex
// normals and colors.
typedef struct TangoMesh_Experimental {
// Index into a three-dimensional fixed grid.
int32_t index[3];
// Array of vertices. Each vertex is an {x, y, z} coordinate triplet, in
// meters.
float (*vertices)[3];
// Array of faces. Each face is an index triplet into the vertices array.
uint32_t (*faces)[3];
// Array of per-vertex normals. Each normal is a normalized {x, y, z} vector.
float (*normals)[3];
// Array of per-vertex colors. Each color is a 4-tuple of 8-bit {R, G, B, A}
// values.
uint8_t (*colors)[4];
// Number of vertices, describing the size of the vertices array.
uint32_t num_vertices;
// Number of faces, describing the size of the faces array.
uint32_t num_faces;
// If true, each vertex will have an associated normal. In that case, the
// size of the normals array will be equal to num_vertices. Otherwise, the
// size of the normals array will be 0.
bool has_normals;
// If true, each vertex will have an associated color. In that case, the size
// of the colors array will be equal to num_vertices. Otherwise, the size of
// the colors array will be 0.
bool has_colors;
} TangoMesh_Experimental;
我目前对这个结构的理解是:
> float中的三个指针(*顶点)[3];指向三个内存块开头的地址,用于网格顶点的x,y和z坐标(法线和颜色颜色也是如此).特定顶点由在三个数组中的特定索引处找到的x,y和z分量组成.
>类似地,uint32_t(* faces)[3]数组有三个指针指向三个内存块的开头,但是这里有一组特定的三个元素,而是包含指示哪三个顶点的索引号(来自顶点数组(每个都有)三个坐标))弥补那张脸.
当前状态是我能够提取网格,并将其中的一些打印到控制台,然后崩溃没有错误
PointCloudApp: PointCloudApp: num_vertices: 8044
如果我省略了我在point_cloud_app.cc(上面的#4)中添加的最后一行,则应用程序不会崩溃.我能够访问顶点数据并使用它执行某些操作,但使用LOGE打印它会导致10次崩溃.偶尔,它会正确打印值而不会崩溃.顶点数据是否有洞或无效值?
我已经尝试将j_I中的test_float返回给java,但是当我尝试这样做时它又崩溃了.
建议?
解决方法:
顶点是一个动态点数组,其中每个点都是一个浮点数[3].试试这个例子:
for (int i = 0; i < mesh.num_vertices; ++i) {
printf("%d: x=%f y=%f z=%f\n", i, mesh.vertices[i][0],
mesh.vertices[i][1], mesh.vertices[i][2]);
}
如果你看一下内存布局,那就是x0 y0 z0 x1 y1 z1等,每个都是浮点数.