-
Notifications
You must be signed in to change notification settings - Fork 14
/
IKRS.DivisibleOBJBuilder.js
366 lines (277 loc) · 8.86 KB
/
IKRS.DivisibleOBJBuilder.js
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* A Three.js geometry-to-OBJ converter.
*
* Thanks to AlexChet for the code snippet:
* http://stackoverflow.com/questions/11367822/exporting-a-three-js-mesh-as-an-obj-or-stl
*
*
* @author Ikaros Kappler
* @date 2014-04-25
* @version 1.0.0
**/
/**
THREE.saveGeometryToObj = function (geometry) {
var s = '';
for (i = 0; i < geometry.vertices.length; i++) {
s+= 'v '+(geometry.vertices[i].x) + ' ' +
geometry.vertices[i].y + ' '+
geometry.vertices[i].z + '\n';
}
for (i = 0; i < geometry.faces.length; i++) {
s+= 'f '+ (geometry.faces[i].a+1) + ' ' +
(geometry.faces[i].b+1) + ' '+
(geometry.faces[i].c+1);
if (geometry.faces[i].d !== undefined) {
s+= ' '+ (geometry.faces[i].d+1);
}
s+= '\n';
}
return s;
}
**/
IKRS.DivisibleOBJBuilder = function( meshes,
filename,
processListener,
maxChunkSize,
millimeterPerUnit,
exportSingleMesh
) {
IKRS.Object.call( this );
if( !filename )
filename = "extrusion.obj";
if( !millimeterPerUnit )
millimeterPerUnit = 0.5; // The default value in the BezierCanvasHandler
// It is possible to pass an array or a single gerometry
if( typeof meshes.length === "undefined" )
meshes = [ meshes ];
this.meshes = meshes;
this.filename = filename;
this.processListener = processListener;
this.maxChunkSize = maxChunkSize;
this.millimeterPerUnit = millimeterPerUnit;
this.exportSingleMesh = exportSingleMesh;
this.currentMeshIndex = 0;
this.currentVertexIndex = 0;
this.currentTriangleIndex = 0;
this.chunkResults = [];
/*
// Calculated projected chunk count
var totalTriangleCount = 0;
for( var i = 0; i < this.meshes.length; i++ ) {
totalTriangleCount += this.meshes[i].geometry.faces.length;
}
// ~3167 bytes for 10 facets (triangles)
this.projectedSize =
this.meshes.length*21 // "solid pixel\n" and "endsolid\n"
+
Math.round(totalTriangleCount/10) * 3167;
this.projectedChunkCount = Math.ceil( this.projectedSize / this.maxChunkSize );
*/
var vertexDataSize = 0;
var faceDataSize = 0;
for( var i = 0; i < meshes.length; i++ ) {
// Vertices are stored as
// "v " + <x> + " " + <y> " " + <z> + "\n"
// => 5 constant bytes + 3*float [16 characters]
vertexDataSize += meshes[i].geometry.vertices.length * (6 + 3 * 16);
// Faces are stored as
// "f " + <a> + " " + <b> + " " + " " <c>\n"
// => 5 constant bytes + 3*int (let's say at 100*100 vertices the average int length is 5).
// NOTE THAT QUAD FACES ARE NOT RESPECTED HERE.
faceDataSize += meshes[i].geometry.vertices.length * (6 + 3 * 5);
}
this.projectedSize =
this.meshes.length*10 // "o " + <name> + "\n"
+
vertexDataSize
+
faceDataSize;
this.projectedChunkCount = Math.ceil( this.projectedSize / this.maxChunkSize );
this.interrupted = false;
}
IKRS.DivisibleOBJBuilder.prototype = new IKRS.Object();
IKRS.DivisibleOBJBuilder.prototype.constructor = IKRS.DivisibleOBJBuilder;
/**
* Processes the next chunk of data.
*
* @return true if there is at least one next part, false otherwise.
**/
IKRS.DivisibleOBJBuilder.prototype.processNextChunk = function() {
if( this.interrupted )
return false;
var tmpResult = this._saveOBJ( this.currentMeshIndex,
this.currentVertexIndex,
this.currentTriangleIndex,
this.maxChunkSize,
this.millimeterPerUnit,
this.exportSingleMesh
);
this.currentMeshIndex = tmpResult.meshIndex;
this.currentVertexIndex = tmpResult.vertexIndex;
this.currentTriangleIndex = tmpResult.triangleIndex;
//window.alert( "chunks.length=" + this.chunkResults.length );
return ( tmpResult.returnCode==0 &&
this.currentMeshIndex < this.meshes.length &&
this.currentTriangleIndex < this.meshes[this.currentMeshIndex].geometry.faces.length
);
}
IKRS.DivisibleOBJBuilder.prototype.interrupt = function() {
this.interrupted = true;
}
IKRS.DivisibleOBJBuilder.prototype.isInterrupted = function() {
return this.interrupted;
}
IKRS.DivisibleOBJBuilder.prototype.getProcessedChunkCount = function() {
return this.chunkResults.length;
}
IKRS.DivisibleOBJBuilder.prototype.getProjectedChunkCount = function() {
return this.projectedChunkCount;
}
IKRS.DivisibleOBJBuilder.prototype.saveOBJResult = function() {
var objString = this.chunkResults.join( "\n\n" );
var blob = new Blob([objString], {type: 'text/plain'});
window.saveAs(blob, this.filename);
}
IKRS.DivisibleOBJBuilder.prototype._saveOBJ = function( meshIndex,
vertexIndex,
triangleIndex,
maxChunkSize,
millimeterPerUnit,
exportSingleMesh
) {
var currentChunkSize = 0;
while( meshIndex < this.meshes.length &&
currentChunkSize < maxChunkSize
) {
var currentMesh = this.meshes[ meshIndex ];
//window.alert( meshes[i].geometry );
var tmpResult = this._buildOBJ( currentMesh.geometry,
meshIndex,
vertexIndex,
triangleIndex,
currentChunkSize,
maxChunkSize,
millimeterPerUnit,
exportSingleMesh
);
this.chunkResults.push( tmpResult.value );
currentChunkSize += tmpResult.chunkSize;
vertexIndex = tmpResult.vertexIndex;
triangleIndex = tmpResult.triangleIndex;
// Next mesh?
if( triangleIndex >= currentMesh.geometry.faces.length ) {
meshIndex++;
vertexIndex = 0;
triangleIndex = 0;
}
}
return { returnCode: 0,
meshIndex: meshIndex,
vertexIndex: vertexIndex,
triangleIndex: triangleIndex
};
};
IKRS.DivisibleOBJBuilder.prototype._buildOBJ = function( geometry,
meshIndex,
vertexIndex,
triangleIndex,
currentChunkSize,
maxChunkSize,
millimeterPerUnit,
exportSingleMesh
) {
var vertices = geometry.vertices;
// Warning!
// The faces may be Face4 and not Face3!
var tris = geometry.faces;
// Use an array as StringBuffer (concatenation is extremely slow in IE6).
var obj = [];
var chunkSize = 0;
// Next mesh following?
if( triangleIndex == 0 &&
(!exportSingleMesh || (exportSingleMesh && meshIndex==0))
) {
//stl.push( "solid pixel\n" );
//chunkSize += ("solid pixel\n").length;
var objectLine = "o mesh[" + meshIndex + "]\n";
obj.push( objectLine );
chunkSize += objectLine.length;
}
// Fist print triangles
while( vertexIndex < vertices.length &&
currentChunkSize+chunkSize < maxChunkSize
) {
var tmpData =
"v " +
vertices[vertexIndex].x + " " +
vertices[vertexIndex].y + " " +
vertices[vertexIndex].z + "\n";
vertexIndex++;
obj.push( tmpData );
chunkSize += tmpData.length;
}
// Add an empty line after the vertex list
if( vertexIndex >= vertices.length ) {
obj.push( "\n\n" );
chunkSize += 2;
}
while( triangleIndex < tris.length &&
currentChunkSize+chunkSize < maxChunkSize
) {
/*
var tmpBuffer = [];
var triangle = tris[triangleIndex];
tmpBuffer.push( " facet normal " + this._stringifyVector( triangle.normal, millimeterPerUnit ) + "\n");
tmpBuffer.push(" outer loop\n");
tmpBuffer.push(" " + this._stringifyVertex( vertices[ triangle.a ], millimeterPerUnit ) );
tmpBuffer.push(" " + this._stringifyVertex( vertices[ triangle.b ], millimeterPerUnit ) );
tmpBuffer.push(" " + this._stringifyVertex( vertices[ triangle.c ], millimeterPerUnit ) );
tmpBuffer.push(" endloop \n");
tmpBuffer.push(" endfacet \n");
var tmpData = tmpBuffer.join("");
*/
var triangle = tris[triangleIndex];
// Note that OBJ indices start at 1.
var tmpData =
"f " +
(triangle.a+1) + " " +
(triangle.b+1) + " " +
(triangle.c+1) + "\n";
triangleIndex++;
obj.push( tmpData );
chunkSize += tmpData.length;
}
// Add to empty lines after face list.
if( triangleIndex >= tris.length &&
(!exportSingleMesh || (exportSingleMesh && meshIndex+1>=this.meshes.length))
) {
obj.push( "\n\n" );
chunkSize += 2;
}
// Convert array to string
var data = obj.join("");
return { returnCode: 0,
value: data,
chunkSize: chunkSize,
vertexIndex: vertexIndex,
triangleIndex: triangleIndex
};
};
/*
IKRS.DivisibleSTLBuilder.prototype._stringifyVector = function( vec,
millimeterPerUnit
) {
// var mm = value * millimeterPerUnit
return "" +
(vec.x*millimeterPerUnit) +
" " +
(vec.y*millimeterPerUnit) +
" " +
(vec.z*millimeterPerUnit);
};
IKRS.DivisibleSTLBuilder.prototype._stringifyVertex = function( vec,
millimeterPerUnit
) {
return "vertex " + this._stringifyVector(vec,millimeterPerUnit) + " \n";
};
*/