-
Notifications
You must be signed in to change notification settings - Fork 3
/
PlyFile.cpp
127 lines (119 loc) · 3.96 KB
/
PlyFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*********************************************************************************
*Copyright(C) 2013-2016 Qingan Yan (yanqinganssg (at) gmail.com or yanqingan (at) whu.edu.cn)
*Author: Qingan Yan
*Version: v15.2
*Date: Sep-10-2015
*Description: This program is used to display point clouds from .out and .ply file format,
which is the output of Bundler.
*Notice: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
*For more information, please view my homepage at https://yanqingan.github.io
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <QGLWidget>
#include "PlyFile.h"
PlyFile::PlyFile()
{
m_pVertices = NULL;
m_pColors = NULL;
m_nVertex = 0;
}
PlyFile::~PlyFile()
{
if(m_pVertices)
{
free(m_pVertices);
m_pVertices = NULL;
}
if(m_pColors)
{
free(m_pColors);
m_pColors = NULL;
}
}
/* mainly use c language */
int PlyFile::Load(const char* filename)
{
/* whether is a ply file */
char* pCh = strstr(filename, ".ply"); // return the position
if(pCh != NULL)
{
FILE* file = fopen(filename, "r");
fseek(file, 0, SEEK_END); // change the position to read(take care: SEEK_END)
long fileSize = ftell(file); // tell the byte size of the pointer from the beginning
try
{
m_pVertices = (float*)malloc(fileSize * sizeof(float));
m_pColors = (float*)malloc(fileSize * sizeof(float));
}
catch(char*)
{
printf("Exception in malloc vertex buffer of PlyFile::load()!\n");
return -1;
}
if(m_pVertices == NULL)
return -1;
fseek(file, 0, SEEK_SET); // this is SEEK_SET
if(file)
{
int i = 0;
char buf[1000];
fgets(buf, 300, file); // read (300 - 1) bytes or a line
/* read header */
while(strncmp("element vertex", buf, strlen("element vertex")) != 0) // string comparsion, if equal return 0
{
fgets(buf, 300, file); // read again
}
strcpy(buf, buf + strlen("element vertex")); // copy string from the position of strlen bytes
sscanf(buf, "%i", &m_nVertex); // read a format data from a string
fseek(file, 0, SEEK_SET); // go to the beginning
while(strncmp("end_header", buf, strlen("end_header")) != 0) // find the end of header
{
fgets(buf, 300, file);
}
/* read vertices and colors */
i = 0;
for(int iter = 0; iter < m_nVertex; iter++)
{
fgets(buf, 300, file);
int r, g, b;
sscanf(buf, "%f %f %f %d %d %d", &m_pVertices[i], &m_pVertices[i + 1]
, &m_pVertices[i + 2], &r, &g, &b); // the first three is positions, latter three is colors
m_pColors[i] = (float)r / 255;
m_pColors[i + 1] = (float)g / 255;
m_pColors[i + 2] = (float)b / 255;
i += 3;
}
fclose(file);
}
else
{
printf("File can not be opened!\n");
}
}
else
{
printf("File does not have a .ply extension!");
}
return 0;
}
void PlyFile::Draw()
{
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3, GL_FLOAT, 0, m_pColors);
glVertexPointer(3, GL_FLOAT, 0, m_pVertices);
//glDrawArrays(GL_POINTS, 0, vertexNumber); // another way
glBegin(GL_POINTS);
for(int i = 0; i < m_nVertex; i++)
{
glArrayElement(i);
}
glEnd();
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}