@@ -4,32 +4,60 @@ function save(this,filename,encoding)
4
4
% this - GIfTI object
5
5
% filename - name of GIfTI file that will be created
6
6
% encoding - optional argument to specify encoding format, among
7
- % ASCII, Base64Binary, GZipBase64Binary, ExternalFileBinary
7
+ % ASCII, Base64Binary, GZipBase64Binary, ExternalFileBinary,
8
+ % Collada (.dae)
8
9
% __________________________________________________________________________
9
10
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
10
11
11
12
% Guillaume Flandin
12
- % $Id: save.m 2076 2008-09-10 12:34:08Z guillaume $
13
+ % $Id: save.m 3290 2009-07-27 18:11:52Z guillaume $
13
14
14
15
error(nargchk(1 ,3 ,nargin ));
15
16
16
- % Open file for writing
17
+ % Check filename and file format
17
18
% --------------------------------------------------------------------------
19
+ ext = ' .gii' ;
18
20
if nargin == 1
19
21
filename = ' untitled.gii' ;
20
22
else
23
+ if nargin == 3 && strcmpi(encoding ,' collada' )
24
+ ext = ' .dae' ;
25
+ end
21
26
[p ,f ,e ] = fileparts(filename );
22
- if ~ismember(lower(e ),{' .gii ' })
23
- e = ' .gii ' ;
27
+ if ~ismember(lower(e ),{ext })
28
+ e = ext ;
24
29
end
25
30
filename = fullfile(p ,[f e ]);
26
31
end
27
32
33
+ % Open file for writing
34
+ % --------------------------------------------------------------------------
28
35
fid = fopen(filename ,' wt' );
29
36
if fid == - 1
30
37
error(' Unable to write file %s : permission denied.' ,filename );
31
38
end
32
39
40
+ % Write file
41
+ % --------------------------------------------------------------------------
42
+ switch ext
43
+ case ' .gii'
44
+ if nargin < 3 , encoding = ' GZipBase64Binary' ; end
45
+ fid = save_gii(fid ,this ,encoding );
46
+ case ' .dae'
47
+ fid = save_dae(fid ,this );
48
+ otherwise
49
+ error(' Unknown file format.' );
50
+ end
51
+
52
+ % Close file
53
+ % --------------------------------------------------------------------------
54
+ fclose(fid );
55
+
56
+ % ==========================================================================
57
+ % function fid = save_gii(fid,this,encoding)
58
+ % ==========================================================================
59
+ function fid = save_gii(fid ,this ,encoding )
60
+
33
61
% Defaults for DataArray's attributes
34
62
% --------------------------------------------------------------------------
35
63
[unused ,unused ,mach ] = fopen(fid );
@@ -40,11 +68,7 @@ function save(this,filename,encoding)
40
68
else
41
69
error(' [GIFTI] Unknown byte order "%s ".' ,mach );
42
70
end
43
- if nargin > 2
44
- def.Encoding = encoding ;
45
- else
46
- def.Encoding = ' GZipBase64Binary' ;
47
- end
71
+ def.Encoding = encoding ;
48
72
def.Intent = ' NIFTI_INTENT_NONE' ;
49
73
def.DataType = ' NIFTI_TYPE_FLOAT32' ;
50
74
def.ExternalFileName = ' ' ;
@@ -214,4 +238,144 @@ function save(this,filename,encoding)
214
238
end
215
239
216
240
fprintf(fid ,' </GIFTI>\n ' );
217
- fclose(fid );
241
+
242
+ % ==========================================================================
243
+ % function fid = save_dae(fid,this)
244
+ % ==========================================================================
245
+ function fid = save_dae(fid ,this )
246
+
247
+ o = inline(' blanks(x*3)' );
248
+
249
+ % Split the mesh into connected components
250
+ % --------------------------------------------------------------------------
251
+ s = struct(this );
252
+ try
253
+ C = spm_mesh_label(s .faces );
254
+ d = [];
255
+ for i= 1 : numel(unique(C ))
256
+ d(i ).faces = s .faces(C == i ,: );
257
+ u = unique(d(i ).faces);
258
+ d(i ).vertices = s .vertices(u ,: );
259
+ a = 1 : max(d(i ).faces(: ));
260
+ a(u ) = 1 : size(d(i ).vertices,1 );
261
+ % a = sparse(1,double(u),1:1:size(d(i).vertices,1));
262
+ d(i ).faces = a(d(i ).faces);
263
+ end
264
+ s = d ;
265
+ end
266
+
267
+ % Prolog & root of the Collada XML file
268
+ % --------------------------------------------------------------------------
269
+ fprintf(fid ,' <?xml version="1.0"?>\n ' );
270
+ fprintf(fid ,' <COLLADA xmlns="http://www.collada.org/2008/03/COLLADASchema" version="1.5.0">\n ' );
271
+
272
+ % Assets
273
+ % --------------------------------------------------------------------------
274
+ fprintf(fid ,' %s <asset>\n ' ,o(1 ));
275
+ fprintf(fid ,' %s <contributor>\n ' ,o(2 ));
276
+ fprintf(fid ,' %s <author_website>%s </author_website>\n ' ,o(3 ),...
277
+ ' http://www.fil.ion.ucl.ac.uk/spm/' );
278
+ fprintf(fid ,' %s <authoring_tool>%s </authoring_tool>\n ' ,o(3 ),spm(' Ver' ));
279
+ fprintf(fid ,' %s </contributor>\n ' ,o(2 ));
280
+ fprintf(fid ,' %s <created>%s </created>\n ' ,o(2 ),datestr(now ,' yyyy-mm-ddTHH:MM:SSZ' ));
281
+ fprintf(fid ,' %s <modified>%s </modified>\n ' ,o(2 ),datestr(now ,' yyyy-mm-ddTHH:MM:SSZ' ));
282
+ fprintf(fid ,' %s <unit name="millimeter" meter="0.001"/>\n ' ,o(2 ));
283
+ fprintf(fid ,' %s <up_axis>Z_UP</up_axis>\n ' ,o(2 ));
284
+ fprintf(fid ,' %s </asset>\n ' ,o(1 ));
285
+
286
+ % Image, Materials, Effects
287
+ % --------------------------------------------------------------------------
288
+ % fprintf(fid,'%s<library_images/>\n',o(1));
289
+
290
+ fprintf(fid ,' %s <library_materials>\n ' ,o(1 ));
291
+ for i= 1 : numel(s )
292
+ fprintf(fid ,' %s <material id="material%d " name="material%d ">\n ' ,o(2 ),i ,i );
293
+ fprintf(fid ,' %s <instance_effect url="#material%d -effect"/>\n ' ,o(3 ),i );
294
+ fprintf(fid ,' %s </material>\n ' ,o(2 ));
295
+ end
296
+ fprintf(fid ,' %s </library_materials>\n ' ,o(1 ));
297
+
298
+ fprintf(fid ,' %s <library_effects>\n ' ,o(1 ));
299
+ for i= 1 : numel(s )
300
+ fprintf(fid ,' %s <effect id="material%d -effect" name="material%d -effect">\n ' ,o(2 ),i ,i );
301
+ fprintf(fid ,' %s <profile_COMMON>\n ' ,o(3 ));
302
+ fprintf(fid ,' %s <technique sid="COMMON">\n ' ,o(4 ));
303
+ fprintf(fid ,' %s <lambert>\n ' ,o(5 ));
304
+ fprintf(fid ,' %s <emission>\n ' ,o(6 ));
305
+ fprintf(fid ,' %s <color>%f %f %f %d </color>\n ' ,o(7 ),[0 0 0 1 ]);
306
+ fprintf(fid ,' %s </emission>\n ' ,o(6 ));
307
+ fprintf(fid ,' %s <ambient>\n ' ,o(6 ));
308
+ fprintf(fid ,' %s <color>%f %f %f %d </color>\n ' ,o(7 ),[0 0 0 1 ]);
309
+ fprintf(fid ,' %s </ambient>\n ' ,o(6 ));
310
+ fprintf(fid ,' %s <diffuse>\n ' ,o(6 ));
311
+ fprintf(fid ,' %s <color>%f %f %f %d </color>\n ' ,o(7 ),[0.5 0.5 0.5 1 ]);
312
+ fprintf(fid ,' %s </diffuse>\n ' ,o(6 ));
313
+ fprintf(fid ,' %s <transparent>\n ' ,o(6 ));
314
+ fprintf(fid ,' %s <color>%d %d %d %d </color>\n ' ,o(7 ),[1 1 1 1 ]);
315
+ fprintf(fid ,' %s </transparent>\n ' ,o(6 ));
316
+ fprintf(fid ,' %s <transparency>\n ' ,o(6 ));
317
+ fprintf(fid ,' %s <float>%f </float>\n ' ,o(7 ),0 );
318
+ fprintf(fid ,' %s </transparency>\n ' ,o(6 ));
319
+ fprintf(fid ,' %s </lambert>\n ' ,o(5 ));
320
+ fprintf(fid ,' %s </technique>\n ' ,o(4 ));
321
+ fprintf(fid ,' %s </profile_COMMON>\n ' ,o(3 ));
322
+ fprintf(fid ,' %s </effect>\n ' ,o(2 ));
323
+ end
324
+ fprintf(fid ,' %s </library_effects>\n ' ,o(1 ));
325
+
326
+ % Geometry
327
+ % --------------------------------------------------------------------------
328
+ fprintf(fid ,' %s <library_geometries>\n ' ,o(1 ));
329
+ for i= 1 : numel(s )
330
+ fprintf(fid ,' %s <geometry id="shape%d " name="shape%d ">\n ' ,o(2 ),i ,i );
331
+ fprintf(fid ,' %s <mesh>\n ' ,o(3 ));
332
+ fprintf(fid ,' %s <source id="shape%d -positions">\n ' ,o(4 ),i );
333
+ fprintf(fid ,' %s <float_array id="shape%d -positions-array" count="%d ">' ,o(5 ),i ,numel(s(i ).vertices));
334
+ fprintf(fid ,' %f ' ,repmat(s(i ).vertices' ,1 ,[]));
335
+ fprintf(fid ,' </float_array>\n ' );
336
+ fprintf(fid ,' %s <technique_common>\n ' ,o(5 ));
337
+ fprintf(fid ,' %s <accessor count="%d " offset="0" source="#shape%d -positions-array" stride="3">\n ' ,o(6 ),size(s(i ).vertices,1 ),i );
338
+ fprintf(fid ,' %s <param name="X" type="float" />\n ' ,o(7 ));
339
+ fprintf(fid ,' %s <param name="Y" type="float" />\n ' ,o(7 ));
340
+ fprintf(fid ,' %s <param name="Z" type="float" />\n ' ,o(7 ));
341
+ fprintf(fid ,' %s </accessor>\n ' ,o(6 ));
342
+ fprintf(fid ,' %s </technique_common>\n ' ,o(5 ));
343
+ fprintf(fid ,' %s </source>\n ' ,o(4 ));
344
+ fprintf(fid ,' %s <vertices id="shape%d -vertices">\n ' ,o(4 ),i );
345
+ fprintf(fid ,' %s <input semantic="POSITION" source="#shape%d -positions"/>\n ' ,o(5 ),i );
346
+ fprintf(fid ,' %s </vertices>\n ' ,o(4 ));
347
+ fprintf(fid ,' %s <triangles material="material%d " count="%d ">\n ' ,o(4 ),i ,size(s(i ).faces,1 ));
348
+ fprintf(fid ,' %s <input semantic="VERTEX" source="#shape%d -vertices" offset="0"/>\n ' ,o(5 ),i );
349
+ fprintf(fid ,' %s <p>' ,o(5 ));
350
+ fprintf(fid ,' %d ' ,repmat(s(i ).faces' ,1 ,[])-1 );
351
+ fprintf(fid ,' </p>\n ' );
352
+ fprintf(fid ,' %s </triangles>\n ' ,o(4 ));
353
+ fprintf(fid ,' %s </mesh>\n ' ,o(3 ));
354
+ fprintf(fid ,' %s </geometry>\n ' ,o(2 ));
355
+ end
356
+ fprintf(fid ,' %s </library_geometries>\n ' ,o(1 ));
357
+
358
+ % Scene
359
+ % --------------------------------------------------------------------------
360
+ fprintf(fid ,' %s <library_visual_scenes>\n ' ,o(1 ));
361
+ fprintf(fid ,' %s <visual_scene id="VisualSceneNode" name="SceneNode">\n ' ,o(2 ));
362
+ for i= 1 : numel(s )
363
+ fprintf(fid ,' %s <node id="node%d ">\n ' ,o(3 ),i );
364
+ fprintf(fid ,' %s <instance_geometry url="#shape%d ">\n ' ,o(4 ),i );
365
+ fprintf(fid ,' %s <bind_material>\n ' ,o(5 ));
366
+ fprintf(fid ,' %s <technique_common>\n ' ,o(6 ));
367
+ fprintf(fid ,' %s <instance_material symbol="material%d " target="#material%d "/>\n ' ,o(7 ),i ,i );
368
+ fprintf(fid ,' %s </technique_common>\n ' ,o(6 ));
369
+ fprintf(fid ,' %s </bind_material>\n ' ,o(5 ));
370
+ fprintf(fid ,' %s </instance_geometry>\n ' ,o(4 ));
371
+ fprintf(fid ,' %s </node>\n ' ,o(3 ));
372
+ end
373
+ fprintf(fid ,' %s </visual_scene>\n ' ,o(2 ));
374
+ fprintf(fid ,' %s </library_visual_scenes>\n ' ,o(1 ));
375
+ fprintf(fid ,' %s <scene>\n ' ,o(1 ));
376
+ fprintf(fid ,' %s <instance_visual_scene url="#VisualSceneNode" />\n ' ,o(2 ));
377
+ fprintf(fid ,' %s </scene>\n ' ,o(1 ));
378
+
379
+ % End of XML
380
+ % --------------------------------------------------------------------------
381
+ fprintf(fid ,' </COLLADA>\n ' );
0 commit comments