forked from r03ert0/meshgeometry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
meshgeometry.c
7197 lines (6561 loc) · 185 KB
/
meshgeometry.c
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//char version[]="meshgeometry, version 1, roberto toro, 12 August 2009";
//char version[]="meshgeometry, version 2, roberto toro, 28 April 2010";
//char version[]="meshgeometry, version 3, roberto toro, 1 May 2010"; // added -laplace and -taubinLM
//char version[]="meshgeometry, version 4, roberto toro, 21 May 2010"; // commands are processed as a chain
//char version[]="meshgeometry, version 5, roberto toro, 28 May 2012"; // added several commands: foldLength, volume, absgi, texture threshold, countClusters, and includes meshconvert v8
//char version[]="meshgeometry, version 6, roberto toro, 10 November 2012"; // added randomverts, help, centre, normalise, normal, verbose, off mesh format (load and save), added to github
//char version[]="meshgeometry, version 7, roberto toro, 17 Decembre 2014"; // vtk support
//char version[]="meshgeometry, version 8, roberto toro, 26 Decembre 2015";
//char version[]="meshgeometry, version 9, roberto toro, 10 June 2017"; // add gii reader
//char version[]="meshgeometry, version 10, roberto toro, 7 November 2017"; // add asc reader/writer
char version[]="meshgeometry, version 11, roberto toro, 10 December 2019"; // added more colourmaps
/*
To use:
./meshgeometry_mac -i /Applications/_Neuro/freesurfer510/subjects/bert/surf/lh.inflated -i /Applications/_Neuro/freesurfer510/subjects/bert/surf/lh.curv -drawSurface hot bert.tif lat
To compile:
source compile.sh
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <zlib.h>
#include "colormap.h"
// OpenGL libraries
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#define pi 3.14159265358979323846264338327950288419716939927510
#define EPSILON 1e-8 // small enough to avoid division overflow
#define min(a,b) (((a)<(b))?(a):(b))
#define kMAXNETRIS 100
#define kFreeSurferMesh 1
#define kFreeSurferData 2
#define kFreeSurferAnnot 3
#define kBrainVisaMesh 4
#define kFloatData 5
#define kRawFloatData 6
#define kText 7
#define kTextData 8
#define kVRMLMesh 9
#define kObjMesh 10
#define kPlyMesh 11
#define kSTLMesh 12
#define kSmeshMesh 13
#define kBinMesh 14
#define kOffMesh 15
#define kMGHData 16
#define kVTKMesh 17
#define kDPVData 18
#define kCivetObjMesh 19
#define kGiiMesh 20
#define kGiiData 21
#define kAscMesh 22
typedef struct
{
float x,y,z;
}float3D;
typedef struct
{
int a,b,c;
}int3D;
typedef struct
{
int a,b;
}int2D;
#define SIZESTACK 64
typedef struct
{
int n;
int t[SIZESTACK];
}NTriRec;
typedef struct
{
int np; // number of vertices
int nt; // number of triangles
int ddim; // data dimensions (default: 1)
float3D *p; // vertices
int3D *t; // triangles
float *data; // data
char *selection; // selection
NTriRec *NT; // neighbouring triangles
}Mesh;
float area(Mesh *m);
float volume(Mesh *m);
int smooth(Mesh *m);
int taubin(float lambda, float mu, int N, Mesh *m);
float minData(Mesh *m);
float maxData(Mesh *m);
int nonmanifold_tris(Mesh *mesh);
int g_gluInitFlag=0;
Mesh mesh;
float R;
int verbose=0;
void get_edges(Mesh *mesh, int3D **e);
void get_nonmanifold_edges(Mesh *mesh, int3D *e, int3D **nme, int *nnme);
int smoothData(Mesh *m,float l,int niter);
#pragma mark -
float dot3D(float3D a, float3D b)
{
return (float){a.x*b.x+a.y*b.y+a.z*b.z};
}
float3D cross3D(float3D a, float3D b)
{
return (float3D){a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x};
}
float3D add3D(float3D a, float3D b)
{
return (float3D){a.x+b.x,a.y+b.y,a.z+b.z};
}
float3D sub3D(float3D a, float3D b)
{
return (float3D){a.x-b.x,a.y-b.y,a.z-b.z};
}
float3D sca3D(float3D a, float t)
{
return (float3D){a.x*t,a.y*t,a.z*t};
}
float norm3D(float3D a)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
}
float norm3Dsqr(float3D a)
{
return a.x*a.x+a.y*a.y+a.z*a.z;
}
float3D normal3D(int i, Mesh *m)
{
float3D *p=m->p;
int3D *t=m->t;
float3D N;
N=cross3D(sub3D(p[t[i].b],p[t[i].a]),sub3D(p[t[i].c],p[t[i].a]));
return sca3D(N,1/norm3D(N));
}
float determinant(float3D a, float3D b, float3D c)
{
float D= a.x*(b.y*c.z-c.y*b.z)+
a.y*(b.z*c.x-c.z*b.x)+
a.z*(b.x*c.y-c.x*b.y);
return D;
}
int multMatVec(float *m, float3D v, float3D *result)
{
/*
Multiplies the vector v by the 4x4 matrix m, and puts the result into result
*/
float3D r;
r.x= m[0]*v.x +m[1]*v.y +m[2]*v.z+m[3];
r.y= m[4]*v.x +m[5]*v.y +m[6]*v.z+m[7];
r.z= m[8]*v.x +m[9]*v.y +m[10]*v.z+m[11];
result->x=r.x;
result->y=r.y;
result->z=r.z;
return 0;
}
#pragma mark -
#pragma mark [ Utilities ]
int endianness;
#define kMOTOROLA 1
#define kINTEL 2
void checkEndianness(void)
{
char b[]={1,0,0,0};
int num=*(int*)b;
if(num==16777216)
endianness=kMOTOROLA;
else
endianness=kINTEL;
}
int systemOutput(char *cmd, char *out)
{
FILE *fp;
fp=popen(cmd, "r");
if (fp==NULL)
return 0;
else
fgets(out,256,fp);
pclose(fp);
return 1;
}
void checkVersion(const char *home)
{
char cmd[1024];
char v_local[128];
char v_remote[128];
sprintf(cmd, "git --git-dir $(dirname %s)/.git log -1 --format=%%cd", home);
if(!systemOutput(cmd, v_local))
{
puts("ERROR: Failed to get local version");
return;
}
sprintf(cmd, "git --git-dir $(dirname %s)/.git log -1 --format=%%cd origin/master", home);
if(!systemOutput(cmd, v_remote))
{
puts("ERROR: Failed to get remote version");
return;
}
printf("Last update: %s", v_local);
if(strcmp(v_local, v_remote) == 0)
puts("Code is up to date.");
else
{
puts("WARNING: Local and remote versions are not the same");
printf("Local: %s", v_local);
printf("Remote: %s", v_remote);
}
return;
}
void swapint(int *n)
{
char *by=(char*)n;
char sw[4]={by[3],by[2],by[1],by[0]};
*n=*(int*)sw;
}
void swapfloat(float *n)
{
char *by=(char*)n;
char sw[4]={by[3],by[2],by[1],by[0]};
*n=*(float*)sw;
}
void swaptriangles(Mesh *m)
{
int nt=m->nt;
int3D *t=m->t;
int i;
for(i=0;i<nt;i++)
{
swapint(&t[i].a);
swapint(&t[i].b);
swapint(&t[i].c);
}
}
void swapvertices(Mesh *m)
{
int np=m->np;
float3D *p=m->p;
int i;
for(i=0;i<np;i++)
{
swapfloat(&p[i].x);
swapfloat(&p[i].y);
swapfloat(&p[i].z);
}
}
// triangle area using Heron's formula
float triangle_area(float3D p0, float3D p1, float3D p2)
{
float a,b,c; // side lengths
float s; // semiperimeter
float areaval;
a=norm3D(sub3D(p0,p1));
b=norm3D(sub3D(p1,p2));
c=norm3D(sub3D(p2,p0));
s=(a+b+c)/2.0;
if(s*(s-a)*(s-b)*(s-c)<0)
areaval=0;
else
areaval=sqrt(s*(s-a)*(s-b)*(s-c));
return areaval;
}
// Adapted from intersect_RayTriangle()
// Copyright 2001, softSurfer (www.softsurfer.com)
// This code may be freely used and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.
// Input: vector "x", triangle index "it"
// Output: *c0, *c1 = the triangle-based coordinates of the intersection (when it exists)
// Return: -1 = triangle is degenerate (a segment or point)
// 0 = disjoint (no intersect)
// 1 = intersect in unique point I1
// 2 = are in the same plane
// code from:http://geometryalgorithms.com/Archive/algorithm_0105/algorithm_0105.htm#intersect_RayTriangle()
int intersect_VectorTriangle(float3D x, int i, float *c0, float *c1, Mesh *m)
{
float3D *p=m->p;
int3D *t=m->t;
int3D T=t[i];
double xx[3];
double u[3], v[3], n[3]; // triangle vectors
double dir[3],w0[3], w[3]; // ray vectors
double r, a, b; // params to calc ray-plane intersect
double uu, uv, vv, wu, wv, D;
double ss,tt;
u[0]=p[T.b].x-p[T.a].x;
u[1]=p[T.b].y-p[T.a].y;
u[2]=p[T.b].z-p[T.a].z;
v[0]=p[T.c].x-p[T.a].x;
v[1]=p[T.c].y-p[T.a].y;
v[2]=p[T.c].z-p[T.a].z;
n[0]=u[1]*v[2]-u[2]*v[1];
n[1]=u[2]*v[0]-u[0]*v[2];
n[2]=u[0]*v[1]-u[1]*v[0];
if(sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2])<1e-10)
{
//printf("%lf\n", sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2])); // triangle is degenerate, do not deal with this case
return -1;
}
dir[0]=x.x;
dir[1]=x.y;
dir[2]=x.z;
w0[0] = -p[T.a].x;
w0[1] = -p[T.a].y;
w0[2] = -p[T.a].z;
a = n[0]*w0[0]+n[1]*w0[1]+n[2]*w0[2]; //a = dot3D(n,w0);
b = n[0]*dir[0]+n[1]*dir[1]+n[2]*dir[2]; //b = dot3D(n,dir);
if (b>-EPSILON && b<EPSILON) { // ray is parallel to triangle plane
if (a == 0.0) // ray lies in triangle plane
return 2;
else
return 0; // ray disjoint from plane
}
// get intersect point of ray with triangle plane
r = -a/b;
if (r < 0.0) // ray goes away from triangle
return 0; // => no intersect
// for a segment, also test if (r > 1.0) => no intersect
xx[0]=dir[0]*r;
xx[1]=dir[1]*r;
xx[2]=dir[2]*r; // intersect point of ray and plane
// is I inside T?
uu=u[0]*u[0]+u[1]*u[1]+u[2]*u[2];
uv=u[0]*v[0]+u[1]*v[1]+u[2]*v[2];
vv=v[0]*v[0]+v[1]*v[1]+v[2]*v[2];
w[0]=xx[0]-p[T.a].x;
w[1]=xx[1]-p[T.a].y;
w[2]=xx[2]-p[T.a].z;
wu=w[0]*u[0]+w[1]*u[1]+w[2]*u[2];
wv=w[0]*v[0]+w[1]*v[1]+w[2]*v[2];
D = uv * uv - uu * vv;
// get and test parametric coords
ss = (uv * wv - vv * wu) / D;
if(ss>-EPSILON && ss<EPSILON) ss=0;
if((1-ss)>-EPSILON && (1-ss)<EPSILON) ss=1;
tt = (uv * wu - uu * wv) / D;
if(tt>-EPSILON && tt<EPSILON) tt=0;
if((1-tt)>-EPSILON && (1-tt)<EPSILON) tt=1;
*c0=(float)ss;
*c1=(float)tt;
if (ss < 0.0 || tt < 0.0 || (ss + tt) > 1.0) // I is outside T
return 0;
return 1; // I is in T
}
void neighbours(Mesh *m)
{
// find incident triangles for every vertex
int np=m->np;
int nt=m->nt;
int3D *t=m->t;
NTriRec **NT=&(m->NT);
int i;
if(*NT)
free(*NT);
*NT=(NTriRec*)calloc(np,sizeof(NTriRec));
if(*NT==NULL)
{
puts("ERROR: Cannot create NT structure in neighbours() function");
return;
}
for(i=0;i<nt;i++)
{
((*NT)[t[i].a]).t[((*NT)[t[i].a]).n++] = i;
((*NT)[t[i].b]).t[((*NT)[t[i].b]).n++] = i;
((*NT)[t[i].c]).t[((*NT)[t[i].c]).n++] = i;
}
}
#define TINY 1.0e-10 // A small number.
#define NMAX 500000 //Maximum allowed number of function evaluations.
#define SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
float amotry(float *p, float y[], float psum[], int ndim,float (*funk)(float []), int ihi, float fac)
{
int j;
float fac1,fac2,ytry,*ptry;
ptry=(float*)calloc(ndim,sizeof(float));
fac1=(1.0-fac)/ndim;
fac2=fac1-fac;
for (j=0;j<ndim;j++)
ptry[j]=psum[j]*fac1-p[ndim*ihi+j]*fac2;
ytry=(*funk)(ptry); // Evaluate the function at the trial point.
if (ytry < y[ihi])
{
// If it's better than the highest, then replace the highest.
y[ihi]=ytry;
for (j=0;j<ndim;j++)
{
psum[j] += ptry[j]-p[ndim*ihi+j];
p[ndim*ihi+j]=ptry[j];
}
}
free(ptry);
return ytry;
}
#define GET_PSUM for(j=0;j<ndim;j++){for(sum=0.0,i=0;i<mpts;i++)sum+=p[ndim*i+j];psum[j]=sum;}
void amoeba(float *p, float y[], int ndim, float ftol,float (*funk)(float []),int *nfunk)
// Multidimensional minimization of the function funk(x) where x[0..ndim-1] is a vector in ndim
// dimensions, by the downhill simplex method of Nelder and Mead. From Numerical Recipes in C
{
int i,ihi,inhi,j,mpts=ndim+1;
float rtol,sum,swap,ysave,ytry,*psum;
psum=(float*)calloc(ndim,sizeof(float));
*nfunk=0;
GET_PSUM
for (;;)
{
int ilo=0;
ihi = y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
for (i=0;i<mpts;i++)
{
if (y[i] <= y[ilo])
ilo=i;
if (y[i] > y[ihi])
{
inhi=ihi;
ihi=i;
}
else if (y[i] > y[inhi] && i != ihi)
inhi=i;
}
rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo])+TINY);
if (rtol < ftol)
{
SWAP(y[0],y[ilo])
for (i=0;i<ndim;i++) SWAP(p[ndim*(0)+i],p[ndim*ilo+i])
break;
}
if (*nfunk >= NMAX){break;};
*nfunk += 2;
ytry=amotry(p,y,psum,ndim,funk,ihi,-1.0);
if (ytry <= y[ilo])
ytry=amotry(p,y,psum,ndim,funk,ihi,2.0);
else if (ytry >= y[inhi])
{
ysave=y[ihi];
ytry=amotry(p,y,psum,ndim,funk,ihi,0.5);
if (ytry >= ysave)
{
for (i=0;i<mpts;i++)
if (i != ilo)
{
for (j=0;j<ndim;j++)
p[ndim*i+j]=psum[j]=0.5*(p[ndim*i+j]+p[ndim*(ilo-1)+j]);
y[i]=(*funk)(psum);
}
*nfunk += ndim; // Keep track of function evaluations.
GET_PSUM // Recompute psum.
}
}
else
--(*nfunk); // Correct the evaluation count.
} // Go back for the test of doneness and the next
free(psum);
}
#pragma mark -
#pragma mark [ Format conversion ]
int getformatindex(char *path)
{
char *formats[]={"orig", "pial", "white", "mesh", "sratio",
"float", "curv", "txt", "inflated","sphere",
"sulc", "reg", "txt1", "wrl", "obj",
"ply", "stl", "smesh", "off", "bin",
"mgh", "annot", "raw", "vtk", "dpv",
"civet_obj","gii", "data_gii", "asc"};
int i,n=sizeof(formats)/sizeof(long); // number of recognised formats
int found,index;
char *extension;
for(i=strlen(path);i>=0;i--)
if(path[i]=='.')
break;
if(i==0)
{
puts("ERROR: Unable to find the format extension");
return 0;
}
extension=path+i+1;
for(i=0;i<n;i++)
{
found=(strcmp(formats[i],extension)==0);
if(found)
break;
}
index=-1;
if(i==0 || i==1 || i==2 || i==8 || i==9 ||i==11)
{
index=kFreeSurferMesh;
if(verbose)
puts("Format: FreeSurfer mesh");
}
else
if(i==21)
{
index=kFreeSurferAnnot;
if(verbose)
puts("Format: FreeSurfer annot");
}
else
if(i==3)
{
index=kBrainVisaMesh;
if(verbose)
puts("Format: BrainVisa mesh");
}
else
if(i==4 || i==6 || i==10)
{
index=kFreeSurferData;
if(verbose)
puts("Format: FreeSurfer Data");
}
else
if(i==5)
{
index=kFloatData;
if(verbose)
puts("Format: Float Data");
}
else
if(i==22)
{
index=kRawFloatData;
if(verbose)
puts("Format: Raw Float Data");
}
else
if(i==12)
{
index=kTextData;
if(verbose)
puts("Format: Text Data");
}
else
if(i==7)
{
index=kText;
if(verbose)
puts("Format: Text mesh");
}
else
if(i==13)
{
index=kVRMLMesh;
if(verbose)
puts("Format: VRML mesh");
}
else
if(i==14)
{
index=kObjMesh;
if(verbose)
puts("Format: Obj mesh");
}
else
if(i==15)
{
index=kPlyMesh;
if(verbose)
puts("Format: Ply mesh");
}
else
if(i==16)
{
index=kSTLMesh;
if(verbose)
puts("Format: STL mesh");
}
else
if(i==17)
{
index=kSmeshMesh;
if(verbose)
puts("Format: Smesh mesh");
}
else
if(i==18)
{
index=kOffMesh;
if(verbose)
puts("Format: Off mesh");
}
else
if(i==19)
{
index=kBinMesh;
if(verbose)
puts("Format: Bin mesh");
}
else
if(i==20)
{
index=kMGHData;
if(verbose)
puts("Format: MGH data");
}
else
if(i==23)
{
index=kVTKMesh;
if(verbose)
puts("Format: VTK Mesh");
}
else
if(i==24)
{
index=kDPVData;
if(verbose)
puts("Format: DPV Data");
}
else
if(i==25)
{
index=kCivetObjMesh;
if(verbose)
puts("Format: Civet Obj Mesh");
}
else
if(i==26)
{
index=kGiiMesh;
if(verbose)
puts("Format: Gii Mesh");
}
else
if(i==27)
{
index=kGiiData;
if(verbose)
puts("Format: Gii Data");
}
else
if(i==28)
{
index=kAscMesh;
if(verbose)
puts("Format: Asc Mesh");
}
return index;
}
#pragma mark -
int FreeSurfer_load_mesh(char *path, Mesh *m)
{
int *np=&(m->np);
int *nt=&(m->nt);
float3D **p=&(m->p);
int3D **t=&(m->t);
FILE *f;
int id,a,b,c;
f=fopen(path,"r");
if(f==NULL)
return 1;
// read triangle/quad identifier: 3 bytes
a=((int)(u_int8_t)fgetc(f))<<16;
b=((int)(u_int8_t)fgetc(f))<<8;
c=(u_int8_t)fgetc(f);
id=a+b+c;
if(id==16777214) // triangle mesh
{
char date[256],info[256];
fgets(date,256,f);
fgets(info,256,f);
fread(np,1,sizeof(int),f); if(endianness==kINTEL) swapint(np);
fread(nt,1,sizeof(int),f); if(endianness==kINTEL) swapint(nt);
// read vertices
*p=(float3D*)calloc(*np,sizeof(float3D));
if((*p)==NULL) puts("ERROR: Cannot allocate memory for points [FreeSurfer_load_mesh]");
else
{
fread((char*)(*p),*np,3*sizeof(float),f); if(endianness==kINTEL) swapvertices(m);
// read triangles
*t=(int3D*)calloc(*nt,sizeof(int3D));
if((*t)==NULL) puts("ERROR: Cannot allocate memory for triangles [FreeSurfer_load_mesh]");
else
{
fread((char*)(*t),*nt,3*sizeof(int),f); if(endianness==kINTEL) swaptriangles(m);
}
}
}
fclose(f);
return 0;
}
int FreeSurfer_load_data(char *path, Mesh *m)
{
int *np=&(m->np);
float **data=&(m->data);
FILE *f;
int i,j;
int id,a,b,c;
if(verbose)
puts("* FreeSurfer_load_data");
f=fopen(path,"r");
if(f==NULL)
return 1;
// read identifier: 3 bytes
a=((int)(u_int8_t)fgetc(f))<<16;
b=((int)(u_int8_t)fgetc(f))<<8;
c=(u_int8_t)fgetc(f);
id=a+b+c;
if(id==16777215) // triangle mesh
{
char byte4[4];
if(endianness==kINTEL)
for(i=0;i<4;i++) byte4[3-i]=fgetc(f);
else
fread(byte4,4,sizeof(char),f);
*np=*(int*)byte4;
if(verbose)
printf("FS #vertex_data %i\n",*np);
*data=(float*)calloc(*np,sizeof(float));
// disregard FaceCount and ValsPerVertex
fgetc(f);fgetc(f);fgetc(f);fgetc(f);
fgetc(f);fgetc(f);fgetc(f);fgetc(f);
// read vertex data
for(j=0;j<*np;j++)
{
if(endianness==kINTEL)
for(i=0;i<4;i++) byte4[3-i]=fgetc(f);
else
fread(byte4,4,sizeof(char),f);
(*data)[j]=*(float*)byte4;
}
}
if(verbose)
puts("FSData finished");
fclose(f);
return 0;
}
int FreeSurfer_load_annot(char *path, Mesh *m)
{
if(verbose)
puts("* FreeSurfer_load_annot");
FILE *f;
int i,n,l;
char *tmp;
float **data=&(m->data);
f=fopen(path,"r");
if(f==NULL)
return 0;
fread(&n,1,sizeof(int),f);
if(endianness==kINTEL)
swapint(&n);
if(m->np==0)
m->np=n;
if(n!=m->np)
{
printf("ERROR: Annotation file corrupted. points:%i annotations:%i [FreeSurfer_load_annot]\n",m->np,n);
return 1;
}
m->ddim=3;
tmp=(char*)calloc(m->np,2*sizeof(int));
*data=(float*)calloc(m->np,3*sizeof(float));
if(tmp==NULL)
{
puts("ERROR: Cannot allocate memory [FreeSurfer_load_annot]");
return 1;
}
fread(tmp,m->np,2*sizeof(int),f);
for(i=0;i<min(m->np,n);i++)
{
l=((int*)tmp)[2*i+1];
if(endianness==kINTEL)
swapint(&l);
(*data)[3*i+0]=(l&0xff);
(*data)[3*i+1]=((l>>8)&0xff);
(*data)[3*i+2]=((l>>16)&0xff);
}
free(tmp);
fclose(f);
return 0;
}
int FreeSurfer_load_mghdata(char *path, Mesh *m)
{
// path: path to source thickness file in mgh format (non-compressed version of mgz)
int *np=&(m->np);
float **data=&(m->data);
FILE *f;
int v,ndim1,ndim2,ndim3,nframes,type,dof;
int i;
f=fopen(path,"r");
if(f==NULL)
return 1;
fread(&v,1,sizeof(int),f); swapint(&v);
fread(&ndim1,1,sizeof(int),f); swapint(&ndim1);
fread(&ndim2,1,sizeof(int),f); swapint(&ndim2);
fread(&ndim3,1,sizeof(int),f); swapint(&ndim3);
fread(&nframes,1,sizeof(int),f); swapint(&nframes);
fread(&type,1,sizeof(int),f); swapint(&type);
fread(&dof,1,sizeof(int),f); swapint(&dof);
if(verbose)
{
printf("version:%i\n",v);
printf("ndim1:%i\n",ndim1);
printf("ndim2:%i\n",ndim2);
printf("ndim3:%i\n",ndim3);
printf("nframes:%i\n",nframes);
printf("type:%i\n",type);
printf("dof:%i\n\n",dof);
}
*np=ndim1*ndim2*ndim3;
*data=(float*)calloc(*np,sizeof(float));
fseek(f,64*4,SEEK_CUR);
for(i=0;i<*np;i++)
{
fread(&((*data)[i]),1,sizeof(float),f);
swapfloat(&((*data)[i]));
}
fclose(f);
return 0;
}
int FreeSurfer_save_mesh(char *path, Mesh *m)
{
int *np=&(m->np);
int *nt=&(m->nt);
float3D *p=m->p;
int3D *t=m->t;
FILE *f;
int id=16777214,a,b,c;
int NP,NT,i;
char date[6]="EMPTY",info[6]="EMPTY";
float3D ftmp;
int3D itmp;
f=fopen(path,"w");
if(f==NULL)
return 1;
// write data identifier: 3 bytes
a=id>>16;
b=(id&0xff00)>>8;
c=(id&0xff);
fputc((char)a,f);
fputc((char)b,f);
fputc((char)c,f);
// write date and info (EMPTY)
fprintf(f, "%s %s\n\n", date, info);
// write number of vertices and triangles
NP=*np;
NT=*nt;
if(endianness==kINTEL)
{
swapint(&NP);
swapint(&NT);
}
fwrite(&NP,1,sizeof(int),f);
fwrite(&NT,1,sizeof(int),f);
// write vertices and triangles
if(endianness==kINTEL)
{
for(i=0;i<*np;i++)
{
ftmp=p[i];
swapfloat(&ftmp.x);
swapfloat(&ftmp.y);
swapfloat(&ftmp.z);
fwrite(&ftmp,1,sizeof(float3D),f);
}
for(i=0;i<*nt;i++)
{
itmp=t[i];
swapint(&itmp.a);
swapint(&itmp.b);
swapint(&itmp.c);
fwrite(&itmp,1,sizeof(int3D),f);
}
}
else
{
fwrite(p,*np,sizeof(float3D),f);
fwrite(t,*nt,sizeof(int3D),f);
}
fclose(f);
return 0;
}
int FreeSurfer_save_data(char *path, Mesh *m)
{
int *np=&(m->np);
float *data=m->data;
FILE *f;
int id=16777215,a,b,c;
int FaceCount=0,ValsPerVertex=1,i,n;
float x;
f=fopen(path,"w");
if(f==NULL)
return 1;
// write data identifier: 3 bytes
a=id>>16;
b=(id&0xff00)>>8;
c=(id&0xff);
fputc((char)a,f);
fputc((char)b,f);
fputc((char)c,f);
n=*np;
if(endianness==kINTEL)
{
swapint(&n);
swapint(&FaceCount);
swapint(&ValsPerVertex);
}
fwrite(&n,1,sizeof(int),f);
fwrite(&FaceCount,1,sizeof(int),f);
fwrite(&ValsPerVertex,1,sizeof(int),f);
// write data
if(endianness==kINTEL)
{