[37] | 1 | #include <iostream> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <strings.h> |
---|
| 4 | |
---|
| 5 | #ifdef LINUX |
---|
| 6 | #include <string.h> |
---|
| 7 | #endif |
---|
| 8 | |
---|
| 9 | // xyz2ply by Sean Anderson, 1999. |
---|
| 10 | // Based loosely on cyra2ply, by Lucas Periera |
---|
| 11 | // Converts a NRC xyz laser range image file to a ply file. |
---|
| 12 | |
---|
| 13 | // Compile with: CC -o xyz2ply xyz2ply.cc |
---|
| 14 | |
---|
| 15 | void |
---|
| 16 | printUsage(const char * name) |
---|
| 17 | { |
---|
| 18 | std::cerr << "Convert NRC xyz laser range scan to a ply file.\n" |
---|
| 19 | << "Usage:\n" |
---|
| 20 | << " " << name |
---|
| 21 | << " -h | [-r] [headersize [rows [colunms]]] < file.xyz > file.ply\n" |
---|
| 22 | << "where:\n" |
---|
| 23 | << " -h Shows this help\n" |
---|
| 24 | << " -r Reverses the order of points in a row\n" |
---|
| 25 | << " headersize Override the size of the header to be headersize\n" |
---|
| 26 | << " rows Override the number of rows\n" |
---|
| 27 | << " columns Override the number of columns\n"; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | int |
---|
| 31 | main(int argc, char * argv[]) |
---|
| 32 | { |
---|
| 33 | int cols = -1; |
---|
| 34 | int rows = -1; |
---|
| 35 | int headersize = -1; |
---|
| 36 | bool reverse = false; |
---|
| 37 | |
---|
| 38 | for (int c = 1; c < argc; c++) |
---|
| 39 | { |
---|
| 40 | if (!strcmp(argv[c], "-h")) |
---|
| 41 | { |
---|
| 42 | printUsage(argv[0]); |
---|
| 43 | return 1; |
---|
| 44 | } |
---|
| 45 | else if (!strcmp(argv[c], "-r")) |
---|
| 46 | { |
---|
| 47 | reverse = true; |
---|
| 48 | } |
---|
| 49 | else if (headersize == -1) |
---|
| 50 | { |
---|
| 51 | headersize = atoi(argv[c]); |
---|
| 52 | } |
---|
| 53 | else if (cols == -1) |
---|
| 54 | { |
---|
| 55 | cols = atoi(argv[c]); |
---|
| 56 | } |
---|
| 57 | else if (rows == -1) |
---|
| 58 | { |
---|
| 59 | rows = atoi(argv[c]); |
---|
| 60 | } |
---|
| 61 | else |
---|
| 62 | { |
---|
| 63 | std::cerr << "Too many arguments!\n"; |
---|
| 64 | printUsage(argv[0]); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // If header size not given, try to find in the file. |
---|
| 69 | if (headersize == -1) |
---|
| 70 | { |
---|
| 71 | std::cin.seekg(80, std::ios::beg); |
---|
| 72 | if (!std::cin) |
---|
| 73 | { |
---|
| 74 | std::cerr << "Header too short\n"; |
---|
| 75 | return 1; |
---|
| 76 | } |
---|
| 77 | std::cin.read((char *) &headersize, sizeof(headersize)); |
---|
| 78 | if (!std::cin) |
---|
| 79 | { |
---|
| 80 | std::cerr << "Couldn't read header size\n"; |
---|
| 81 | return 1; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if (cols == -1) |
---|
| 86 | { |
---|
| 87 | std::cin.seekg(244, std::ios::beg); |
---|
| 88 | if (!std::cin) |
---|
| 89 | { |
---|
| 90 | std::cerr << "Header too short\n"; |
---|
| 91 | return 1; |
---|
| 92 | } |
---|
| 93 | std::cin.read((char *) &cols, sizeof(cols)); |
---|
| 94 | if (!std::cin) |
---|
| 95 | { |
---|
| 96 | std::cerr << "Couldn't read number of columns\n"; |
---|
| 97 | return 1; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if (rows == -1) |
---|
| 102 | { |
---|
| 103 | std::cin.seekg(248, std::ios::beg); |
---|
| 104 | if (!std::cin) |
---|
| 105 | { |
---|
| 106 | std::cerr << "Header too short\n"; |
---|
| 107 | return 1; |
---|
| 108 | } |
---|
| 109 | std::cin.read((char *) &rows, sizeof(rows)); |
---|
| 110 | if (!std::cin) |
---|
| 111 | { |
---|
| 112 | std::cerr << "Couldn't read number of rows\n"; |
---|
| 113 | return 1; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | std::cin.seekg(headersize, std::ios::beg); |
---|
| 119 | if (!std::cin) |
---|
| 120 | { |
---|
| 121 | std::cerr << "File too short; couldn't seek to start of xyz data\n"; |
---|
| 122 | return 1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | typedef float float3[3]; |
---|
| 126 | |
---|
| 127 | float3 * pts = new float3 [rows * cols]; |
---|
| 128 | int * grid = new int[rows * cols]; |
---|
| 129 | int * pgrid = grid; |
---|
| 130 | int index = 0; |
---|
| 131 | |
---|
| 132 | int row, col; |
---|
| 133 | for (row = 0; row < rows; row++) |
---|
| 134 | { |
---|
| 135 | for (col = 0; col < cols; col++, pgrid++) |
---|
| 136 | { |
---|
| 137 | std::cin.read((char *) &pts[index], sizeof(pts[index])); |
---|
| 138 | |
---|
| 139 | if (!std::cin) |
---|
| 140 | { |
---|
| 141 | std::cerr << "File too short\n"; |
---|
| 142 | return 1; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if (pts[index][0] == -1 && pts[index][1] == -1 && pts[index][2] == -1) |
---|
| 146 | { |
---|
| 147 | *pgrid = -1; |
---|
| 148 | } |
---|
| 149 | else |
---|
| 150 | { |
---|
| 151 | *pgrid = index; |
---|
| 152 | index++; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | const int numVerts = index; |
---|
| 158 | |
---|
| 159 | std::cout << "ply\n" |
---|
| 160 | << "format ascii 1.0\n" |
---|
| 161 | << "obj_info is_cyberware_data 0\n" |
---|
| 162 | << "obj_info is_mesh 0\n" |
---|
| 163 | << "obj_info is_warped 0\n" |
---|
| 164 | << "obj_info is_interlaced 0\n" |
---|
| 165 | << "obj_info num_cols " << cols << std::endl |
---|
| 166 | << "obj_info num_rows " << rows << std::endl |
---|
| 167 | << "element vertex " << numVerts << std::endl |
---|
| 168 | << "property float x\n" |
---|
| 169 | << "property float y\n" |
---|
| 170 | << "property float z\n" |
---|
| 171 | << "element range_grid " << rows * cols << std::endl |
---|
| 172 | << "property list uchar int vertex_indices\n" |
---|
| 173 | << "end_header\n"; |
---|
| 174 | |
---|
| 175 | for (index = 0; index < numVerts; index++) |
---|
| 176 | { |
---|
| 177 | std::cout << pts[index][0] << " " |
---|
| 178 | << pts[index][1] << " " |
---|
| 179 | << pts[index][2] << std::endl; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | pgrid = grid; |
---|
| 183 | for (row = 0; row < rows; row++) |
---|
| 184 | { |
---|
| 185 | if (reverse) |
---|
| 186 | { |
---|
| 187 | pgrid = grid + (1 + row) * cols - 1; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | for (col = 0; col < cols; col++) |
---|
| 191 | { |
---|
| 192 | if (*pgrid < 0) |
---|
| 193 | { |
---|
| 194 | std::cout << "0\n"; |
---|
| 195 | } |
---|
| 196 | else |
---|
| 197 | { |
---|
| 198 | std::cout << "1 " << *pgrid << std::endl; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | if (reverse) |
---|
| 202 | { |
---|
| 203 | pgrid--; |
---|
| 204 | } |
---|
| 205 | else |
---|
| 206 | { |
---|
| 207 | pgrid++; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | } |
---|