-
Notifications
You must be signed in to change notification settings - Fork 0
/
intensity_scan.cpp
100 lines (92 loc) · 2.31 KB
/
intensity_scan.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
typedef struct {
float x,y,z,intensity;
} Point;
std::vector<Point> LoadFromPTS(char* fileName) {
std::vector<Point> pcd;
FILE* f = fopen(fileName, "r");
if (!f) {
printf("File not found: %s\n", fileName);
return pcd;
}
char buf[256];
int pointsParsed = 0;
float x,y,z,its;
int r,g,b;
float minIts = 99999;
float maxIts = -99999;
while (fgets(buf, 256, f)) {
int n = atoi(buf);
for (int i=0;i<n;i++) {
fgets(buf,256,f);
if (sscanf(buf, "%f %f %f %f %d %d %d",&x,&y,&z,&its,&r,&g,&b) == 7) {
Point p = {x,y,z,its};
pcd.push_back(p);
if (its < minIts) minIts = its;
if (its > maxIts) maxIts = its;
pointsParsed++;
} else {
printf("Error parsing %s\n",fileName);
printf("Line %d: %s\n",pointsParsed,buf);
break;
}
}
}
fclose(f);
printf("Parsed %s (%d points) min %f max %f\n",fileName,pointsParsed,minIts,maxIts);
for (int i=0;i<pointsParsed;i++)
pcd[i].intensity = (pcd[i].intensity - minIts) / (maxIts - minIts);
return pcd;
}
//void writeToPCD(std::vector<Point> *p, char* fileName) {
// FILE* f = fopen(fileName, "w");
// fprintf(f,"# .PCD v0.7 - Point Cloud Data file format\n"
// "VERSION 0.7\n"
// "FIELDS x y z intensity\n"
// "SIZE 4 4 4 4\n"
// "TYPE F F F F\n"
// "COUNT 1 1 1 1\n"
// "WIDTH %lu\n"
// "HEIGHT 1\n"
// "VIEWPOINT 0 0 0 1 0 0 0\n"
// "POINTS %lu\n"
// "DATA ascii\n",p->size(),p->size());
// for (size_t i=0;i<p->size();i++) {
// Point q = p->at(i);
// fprintf(f,"%f %f %f %f\n",q.x,q.y,q.z,q.intensity);
// }
// fclose(f);
//}
void writeToPCD(std::vector<Point> *p, char* fileName) {
FILE* f = fopen(fileName, "w");
fprintf(f,"# .PCD v0.7 - Point Cloud Data file format\n"
"VERSION 0.7\n"
"FIELDS x y z rgb\n"
"SIZE 4 4 4 4\n"
"TYPE F F F I\n"
"COUNT 1 1 1 1\n"
"WIDTH %lu\n"
"HEIGHT 1\n"
"VIEWPOINT 0 0 0 1 0 0 0\n"
"POINTS %lu\n"
"DATA ascii\n",p->size(),p->size());
for (size_t i=0;i<p->size();i++) {
Point q = p->at(i);
int r = q.intensity * 255;
int rgb = (r<<16) | (r<<8) | r;
fprintf(f,"%f %f %f %d\n",q.x,q.y,q.z,rgb);
}
fclose(f);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("%s cloud.pts/cloud.pcd [out.pcd]\n",argv[0]);
return 1;
}
std::vector<Point> p = LoadFromPTS(argv[1]);
writeToPCD(&p,argv[2]);
}