while writing this article,I was learning how to output the .ply file with c++ and met some problems. Here I share my solution.
Introduction:
RPly is a library that lets applications read and write PLY files. The PLY file format is widely used to store geometric information, such as 3D models, but is general enough to be useful for other purposes.
Construction of .ply file:
Sample:
ply
format ascii 1.0 { ascii/二进制,格式版本数 }
comment made by name { 注释关键词说明,像其他行一样 }
obj_info This is My First Ply File
element vertex 8 { 定义“vertex”(顶点)元素,在文件中有8个 }
property float32 x { 顶点包含浮点坐标“x”}
property float32 y { y 坐标同样是一个顶点属性 }
property float32 z { z 也是坐标 }
element face 6 { 在文件里有6个“face”(面片) }
property list uint8 int32 vertex_index { “vertex_indices”(顶点素引)是一列整数 }
end_header { 划定头部结尾 }
0 0 0 { 顶点列表的开始 }
0 0 1
0 1 1
0 1 0
1 0 0
1 0 1
1 1 1
1 1 0
4 0 1 2 3 { 面片列表开始 }
4 7 6 5 4
4 0 4 5 1
4 1 5 6 2
4 2 6 7 3
4 3 7 4 0
Usage:(only for writing)
(1)The 1st Step(Here is my list of file)
The header file ,"rply.h"and"rplyfile.h", is from official document. The source file ,"rply.c", too.
(2)The 2nd Step(Code)
My main function is in "myWriter.cpp".
Here is "myWriter.cpp".
#include "rply.h"
#include
int main() {
p_ply oply;//声明输出文件对象
float x[8] = { 0,1,0,0,1,1,0,1 };
float y[8] = { 0,0,1,0,1,0,1,1 };
float z[8] = { 0,0,0,0,0,1,1,1 };
float r = 255;
float g = 0;
float b = 0;
oply = ply_create("myFirstPly2.ply", PLY_ASCII, NULL, 0, NULL);
/*(文件路径/文件名,编码方式,报错,输入数据,我这边用不上就没有研究这个)*/
if(!ply_add_element(oply, "vertex", 8))return 1;/*生成element部分*/
if (!ply_add_scalar_property(oply, "x", PLY_FLOAT32))return 1;/*生成property部分*/
if (!ply_add_scalar_property(oply, "y", PLY_FLOAT32))return 1;
if (!ply_add_scalar_property(oply, "z", PLY_FLOAT32))return 1;
if (!ply_add_scalar_property(oply, "red", PLY_UCHAR))return 1;
if (!ply_add_scalar_property(oply, "green", PLY_UCHAR))return 1;
if (!ply_add_scalar_property(oply, "blue", PLY_UCHAR))return 1;
if(!ply_add_comment(oply, "Object: author Swithun"))return 1;/*生成comment部分*/
if(! ply_add_obj_info(oply, "This is My First Ply File"))return 1;/*生成project information部分*/
if(!ply_write_header(oply))return 1;/*将上面添加的header内容写入ply文件的header部分*/
for (int i = 0; i < 8; i++) {
if (!ply_write(oply, x[i]))return 1;/*写入点云数据*/
if (!ply_write(oply, y[i]))return 1;
if (!ply_write(oply, z[i]))return 1;
if (!ply_write(oply, r))return 1;
if (!ply_write(oply, g))return 1;
if (!ply_write(oply, b))return 1;
}
if(!ply_close(oply))return 1;/*close file*/
return 0;
}
ply
format ascii 1.0
comment Object: author Swithun
obj_info This is My First Ply File
element vertex 8
property float32 x
property float32 y
property float32 z
property uchar red
property uchar green
property uchar blue
end_header
0 0 0 255 0 0
1 0 0 255 0 0
0 1 0 255 0 0
0 0 0 255 0 0
1 1 0 255 0 0
1 0 1 255 0 0
0 1 1 255 0 0
1 1 1 255 0 0
Open it with MeshLab:
eight small red points(maybe you cannot see it).So I don't display,Ahhh.