-
Notifications
You must be signed in to change notification settings - Fork 23
/
LWOWriter.cpp
340 lines (284 loc) · 9.19 KB
/
LWOWriter.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/***********************************************************************
LWOWriter - Utility to convert a stream of depth and color frames into
a sequence of 3D triangle meshes in Lightwave Object file format.
Copyright (c) 2012-2015 Oliver Kreylos
This file is part of the Kinect 3D Video Capture Project (Kinect).
The Kinect 3D Video Capture Project 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; either
version 2 of the License, or (at your option) any later version.
The Kinect 3D Video Capture Project is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Kinect 3D Video Capture Project; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <Misc/SizedTypes.h>
#include <Misc/FileNameExtensions.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Math/Constants.h>
#include <Geometry/Point.h>
#include <Geometry/Box.h>
#include <Geometry/ProjectiveTransformation.h>
#include <Images/RGBImage.h>
#include <Images/WriteImageFile.h>
#include <Kinect/FileFrameSource.h>
#include <Kinect/Projector.h>
#include "IFFChunkWriter.h"
void writeFrames(const Kinect::FrameSource::IntrinsicParameters& ip,const Kinect::FrameBuffer& color,const Kinect::MeshBuffer& mesh,const char* lwoFileName)
{
/* Create the texture file name: */
std::string textureFileName(lwoFileName,Misc::getExtension(lwoFileName));
textureFileName.append("-color.png");
/* Write the color frame as a texture image: */
{
Images::RGBImage texImage(color.getSize(0),color.getSize(1));
Images::RGBImage::Color* tiPtr=texImage.modifyPixels();
const unsigned char* cfPtr=color.getTypedBuffer<unsigned char>();
for(int y=0;y<color.getSize(1);++y)
for(int x=0;x<color.getSize(0);++x,++tiPtr,cfPtr+=3)
*tiPtr=Images::RGBImage::Color(cfPtr);
Images::writeImageFile(texImage,textureFileName.c_str());
}
/* Open the LWO file: */
IO::FilePtr lwoFile=IO::openFile(lwoFileName,IO::File::WriteOnly);
lwoFile->setEndianness(Misc::BigEndian);
/* Create the LWO file structure via the FORM chunk: */
{
IFFChunkWriter form(lwoFile,"FORM");
form.write<char>("LWO2",4);
/* Create the TAGS chunk: */
{
IFFChunkWriter tags(&form,"TAGS");
tags.writeString("ColorImage");
tags.writeChunk();
}
/* Create the LAYR chunk: */
{
IFFChunkWriter layr(&form,"LAYR");
layr.write<Misc::UInt16>(0U);
layr.write<Misc::UInt16>(0x0U);
for(int i=0;i<3;++i)
layr.write<Misc::Float32>(0.0f);
layr.writeString("DepthImage");
layr.writeChunk();
}
/* Create an index map for all vertices to omit unused vertices: */
unsigned int* indices=new unsigned int[mesh.numVertices];
for(unsigned int i=0;i<mesh.numVertices;++i)
indices[i]=~0x0U;
unsigned int numUsedVertices=0;
/* Create the PNTS, BBOX and VMAP chunks in one go: */
{
typedef Kinect::FrameSource::IntrinsicParameters::PTransform PTransform;
typedef PTransform::Point Point;
typedef Geometry::Box<Point::Scalar,3> Box;
IFFChunkWriter bbox(&form,"BBOX");
IFFChunkWriter pnts(&form,"PNTS");
IFFChunkWriter vmap(&form,"VMAP");
/* Write the VMAP header: */
vmap.write<char>("TXUV",4);
vmap.write<Misc::UInt16>(2U);
vmap.writeString("ColorImageUV");
/* Process all triangle vertices: */
Box pBox=Box::empty;
const Kinect::MeshBuffer::Vertex* vertices=mesh.getVertices();
const Kinect::MeshBuffer::Index* tiPtr=mesh.getTriangleIndices();
for(unsigned int i=0;i<mesh.numTriangles*3;++i,++tiPtr)
{
/* Check if the triangle vertex doesn't already have an index: */
if(indices[*tiPtr]==~0x0U)
{
/* Assign an index to the triangle vertex: */
indices[*tiPtr]=numUsedVertices;
/* Transform the mesh vertex to camera space using the depth projection matrix: */
Point dp(vertices[*tiPtr].position.getXyzw());
Point cp=ip.depthProjection.transform(dp);
/* Transform the depth-space point to texture space using the color projection matrix: */
Point tp=ip.colorProjection.transform(dp);
/* Add the point to the bounding box: */
pBox.addPoint(cp);
/* Store the point and its texture coordinates: */
pnts.writePoint(cp);
vmap.writeVarIndex(numUsedVertices);
for(int i=0;i<2;++i)
vmap.write<Misc::Float32>(tp[i]);
++numUsedVertices;
}
}
/* Write the bounding box: */
bbox.writeBox(pBox);
/* Write the BBOX, PNTS, and VMAP chunks: */
bbox.writeChunk();
pnts.writeChunk();
vmap.writeChunk();
}
/* Create the POLS chunk: */
{
IFFChunkWriter pols(&form,"POLS");
pols.write<char>("FACE",4);
const Kinect::MeshBuffer::Index* tiPtr=mesh.getTriangleIndices();
for(unsigned int triangleIndex=0;triangleIndex<mesh.numTriangles;++triangleIndex,tiPtr+=3)
{
pols.write<Misc::UInt16>(3U);
for(int i=0;i<3;++i)
pols.writeVarIndex(indices[tiPtr[2-i]]);
}
pols.writeChunk();
}
/* Delete the vertex index map: */
delete[] indices;
/* Create the PTAG chunk: */
{
IFFChunkWriter ptag(&form,"PTAG");
ptag.write<char>("SURF",4);
for(unsigned int triangleIndex=0;triangleIndex<mesh.numTriangles;++triangleIndex)
{
ptag.writeVarIndex(triangleIndex);
ptag.write<Misc::UInt16>(0U);
}
ptag.writeChunk();
}
/* Create the CLIP chunk: */
{
IFFChunkWriter clip(&form,"CLIP");
clip.write<Misc::UInt32>(1U);
/* Create the STIL chunk: */
{
IFFChunkWriter stil(&clip,"STIL",true);
stil.writeString(textureFileName.c_str());
stil.writeChunk();
}
clip.writeChunk();
}
/* Create the SURF chunk: */
{
IFFChunkWriter surf(&form,"SURF");
surf.writeString("ColorImage");
surf.writeString("");
/* Create the SIDE subchunk: */
{
IFFChunkWriter side(&surf,"SIDE",true);
side.write<Misc::UInt16>(3U);
side.writeChunk();
}
/* Create the SMAN subchunk: */
{
IFFChunkWriter sman(&surf,"SMAN",true);
sman.write<Misc::Float32>(Math::rad(90.0f));
sman.writeChunk();
}
/* Create the COLR subchunk: */
{
IFFChunkWriter colr(&surf,"COLR",true);
// colr.writeColor(1.0f,1.0f,1.0f);
colr.writeColor(0.5f,0.6f,0.8f);
colr.writeVarIndex(0U);
colr.writeChunk();
}
/* Create the DIFF subchunk: */
{
IFFChunkWriter diff(&surf,"DIFF",true);
diff.write<Misc::Float32>(1.0f);
diff.writeVarIndex(0U);
diff.writeChunk();
}
/* Create the LUMI subchunk: */
{
IFFChunkWriter lumi(&surf,"LUMI",true);
lumi.write<Misc::Float32>(0.0f);
lumi.writeVarIndex(0U);
lumi.writeChunk();
}
/* Create the BLOK subchunk: */
{
IFFChunkWriter blok(&surf,"BLOK",true);
/* Create the IMAP subchunk: */
{
IFFChunkWriter imap(&blok,"IMAP",true);
imap.writeString("1");
/* Create the CHAN subchunk: */
{
IFFChunkWriter chan(&imap,"CHAN",true);
chan.write<char>("COLR",4);
chan.writeChunk();
}
imap.writeChunk();
}
/* Create the PROJ subchunk: */
{
IFFChunkWriter proj(&blok,"PROJ",true);
proj.write<Misc::UInt16>(5U);
proj.writeChunk();
}
/* Create the IMAG subchunk: */
{
IFFChunkWriter imag(&blok,"IMAG",true);
imag.writeVarIndex(1U);
imag.writeChunk();
}
/* Create the VMAP subchunk: */
{
IFFChunkWriter vmap(&blok,"VMAP",true);
vmap.writeString("ColorImageUV");
vmap.writeChunk();
}
blok.writeChunk();
}
/* Write the SURF chunk: */
surf.writeChunk();
}
/* Write the FORM chunk: */
form.writeChunk();
}
}
int main(int argc,char* argv[])
{
/* Open the requested 3D video stream: */
std::string colorFileName=argv[1];
colorFileName.append(".color");
std::string depthFileName=argv[1];
depthFileName.append(".depth");
Kinect::FileFrameSource frameSource(colorFileName.c_str(),depthFileName.c_str());
/* Get the 3D video stream's intrinsic parameters: */
Kinect::FrameSource::IntrinsicParameters ip=frameSource.getIntrinsicParameters();
/* Create a 3D video projector: */
Kinect::Projector projector(frameSource);
projector.setFilterDepthFrames(true,false);
/* Read pairs of frames from the source and export them to a sequence of Lightwave Object files: */
unsigned int minIndex=argc>3?atoi(argv[3]):0;
unsigned int maxIndex=argc>4?atoi(argv[4]):Math::Constants<unsigned int>::max;
unsigned int frameIndex=0;
std::cout<<"Processing frame 0"<<std::flush;
while(true)
{
std::cout<<"\b\b\b\b\b\b"<<std::setw(6)<<frameIndex<<std::flush;
/* Read the next pair of frames: */
Kinect::FrameBuffer color=frameSource.readNextColorFrame();
Kinect::FrameBuffer depth=frameSource.readNextDepthFrame();
/* Bail out if either frame is invalid: */
if(color.timeStamp==Math::Constants<double>::max||depth.timeStamp==Math::Constants<double>::max)
break;
if(frameIndex>=minIndex&&frameIndex<maxIndex)
{
/* Convert the depth frame to a mesh: */
Kinect::MeshBuffer mesh;
projector.processDepthFrame(depth,mesh);
/* Export the frame pair to an LWO file: */
char lwoFileName[1024];
snprintf(lwoFileName,sizeof(lwoFileName),argv[2],frameIndex);
writeFrames(ip,color,mesh,lwoFileName);
}
++frameIndex;
}
std::cout<<std::endl;
return 0;
}