-
Notifications
You must be signed in to change notification settings - Fork 0
/
inout.c
2141 lines (1822 loc) · 73 KB
/
inout.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
/*************************************************************
*
* inout.c - Input and output routines for triangle meshes
*
* Mark J. Stock, [email protected]
*
*
* rocktools - Tools for creating and manipulating triangular meshes
* Copyright (C) 1999,2002-4,6,13-15,20 Mark J. Stock
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*********************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "structs.h"
tri_pointer read_input(char[MAX_FN_LEN],int,tri_pointer);
tri_pointer read_raw(char[MAX_FN_LEN],tri_pointer);
tri_pointer read_tin(char[MAX_FN_LEN],tri_pointer);
tri_pointer read_obj(char[MAX_FN_LEN],int,tri_pointer);
tri_pointer read_msh(char[MAX_FN_LEN],tri_pointer);
int write_output(tri_pointer, char[4], int, int, char**);
int write_raw(tri_pointer, int);
int write_tin(tri_pointer, int);
int write_obj(tri_pointer, int, int, char**);
int write_seg(tri_pointer, int, char**);
int write_rad(tri_pointer, int);
int write_pov(tri_pointer, int);
int write_rib(tri_pointer, int);
int write_wrl(tri_pointer, int, int, char**);
int get_tri(FILE*,int,tri_pointer);
int write_tri(FILE*,int,tri_pointer);
/*
* Determine file type and run appropriate read routine
*/
tri_pointer read_input(char infile[MAX_FN_LEN],int invert,tri_pointer tri_head) {
//int dummy;
char extension[4]; /* filename extension if infile */
tri_pointer new_tri_head; /* the pointer to the first triangle */
/* Determine the input file format from the .XXX extension, and read it */
strncpy(extension,infile+strlen(infile)-3,3);
if (strncmp(extension, "raw", 3) == 0)
new_tri_head = read_raw(infile,tri_head);
else if (strncmp(extension, "obj", 3) == 0)
new_tri_head = read_obj(infile,invert,tri_head);
else if (strncmp(extension, "tin", 3) == 0)
new_tri_head = read_tin(infile,tri_head);
else if (strncmp(extension, "msh", 3) == 0)
new_tri_head = read_msh(infile,tri_head);
else {
fprintf(stderr,"Input filename extension is assumed to be (%s)\n",extension);
fprintf(stderr,"This is either not a supported input file format, or you\n");
fprintf(stderr," need to use a proper extension (last 3 characters in filename).\n");
exit(0);
}
// make sure all tris are oriented in the same direction
//dummy = fix_orientation(new_tri_head);
//fprintf(stderr,"Flipped %d normals\n",dummy);
// determine the adjacent triangles for each triangle
#ifdef CONN
(void) set_adjacent_tris(new_tri_head);
#endif
return new_tri_head;
}
/*
* Read in a Wavefront (.obj) file
*
* For now, assume that the Wavefront file was written in short
* form (meaning that every node only appears once, and many
* triangles may reference the same node
*/
//#pragma GCC push_options
//#pragma GCC optimize ("O0")
tri_pointer __attribute__((optimize("O0"))) read_obj(char filename[MAX_FN_LEN],int invert,tri_pointer tri_head) {
int inode = 0;
int inorm = 0;
int itext = 0;
int num_tri = 0;
int num_nodes = 0;
int num_norms = 0;
int num_texts = 0;
char onechar[2],anotherchar[2];
char twochar[3];
char sbuf[256];
char xs[64],ys[64],zs[64];
VEC *loc = NULL;
VEC *normal = NULL;
UV *texture = NULL;
VEC test,nmin,nmax;
node_ptr new_node = NULL;
norm_ptr new_norm = NULL;
text_ptr new_text = NULL;
tri_pointer new_tri = NULL;
BIN nodebin;
NBIN normbin;
TBIN textbin;
FILE *fp;
// fprintf(stderr,"Try to read %s\n",filename); fflush(stderr);
// open the .obj file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Prescanning %s",filename);
fflush(stderr);
nmax.x = -9.999e+9;
nmax.y = -9.999e+9;
nmax.z = -9.999e+9;
nmin.x = 9.999e+9;
nmin.y = 9.999e+9;
nmin.z = 9.999e+9;
// read the input file and update statistics
while (fread(&onechar,sizeof(char),1,fp) == 1) {
// only read nodes, we're putting them into bins on this pass
if (onechar[0] == 'v') {
fread(&anotherchar,sizeof(char),1,fp);
// fprintf(stdout," (%c)\n",anotherchar); fflush(stdout);
if (isspace(anotherchar[0])) {
// read a vertex location
fscanf(fp,"%s %s %s",xs,ys,zs);
//fprintf(stderr," %s %s %s\n",xs,ys,zs); fflush(stderr);
test.x = atof(xs);
test.y = atof(ys);
test.z = atof(zs);
if (test.x > nmax.x) nmax.x = test.x;
if (test.y > nmax.y) nmax.y = test.y;
if (test.z > nmax.z) nmax.z = test.z;
if (test.x < nmin.x) nmin.x = test.x;
if (test.y < nmin.y) nmin.y = test.y;
if (test.z < nmin.z) nmin.z = test.z;
//fprintf(stderr,"%d %g %g %g\n",i,test.x,test.y,test.z); fflush(stderr);
if (inode/DOTPER == (inode+DPMO)/DOTPER) fprintf(stderr,".");
inode++;
} else if (anotherchar[0] == 'n') {
// it's a vertex normal
inorm++;
} else if (anotherchar[0] == 't') {
// it's a texture coordinate
itext++;
} else {
// if its not identifiable, skip it, do not scale, do not write
}
} else {
// if its not identifiable, skip it, do not scale, do not write
}
fscanf(fp,"%[^\n]",sbuf); // read line beyond first char
fscanf(fp,"%[\n]",twochar); // read newline
}
fprintf(stderr,"\n");
fclose(fp);
// now, we know how many nodes there will be, size and malloc the array
loc = (VEC*)malloc(inode*sizeof(VEC));
// fprintf(stderr,"Found %d vertexes\n",i);
if (inorm > 0) normal = (VEC*)malloc(inorm*sizeof(VEC));
if (itext > 0) texture = (UV*)malloc(itext*sizeof(UV));
// Initialize bin structures
// fprintf(stderr,"%g %g %g %g %g %g\n",nmin.x,nmin.y,nmin.z,nmax.x,nmax.y,nmax.z);
//nodebin.dx = (nmax.x-nmin.x)/(BIN_COUNT-1);
//nodebin.start = nmin.x - 0.5*nodebin.dx;
//for (i=0;i<BIN_COUNT;i++) nodebin.b[i] = NULL;
(void) prepare_node_bin (&nodebin,nmin,nmax);
(void) prepare_norm_bin (&normbin);
(void) prepare_texture_bin (&textbin);
// open the .obj file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Opening file %s...",filename);
fflush(stderr);
// node counter variable!
inode = 0;
// node normal counter variable!
inorm = 0;
// node texture counter variable!
itext = 0;
// line count variable
int nlines = 0;
// read the input file and update statistics
while (fread(&onechar,sizeof(char),1,fp) == 1) {
nlines++;
// fprintf(stdout,"(%c)",onechar); fflush(stdout);
if (onechar[0] == '#') {
// read a comment line
// fprintf(stdout,"#%s\n",sbuf); // write comment
} else if (onechar[0] == 'v') {
fread(&anotherchar,sizeof(char),1,fp);
// fprintf(stdout," (%c)\n",anotherchar); fflush(stdout);
if (isspace(anotherchar[0])) {
// read a vertex location
fscanf(fp,"%s %s %s",xs,ys,zs);
// fprintf(stderr,"%d (%s) (%s) (%s)\n",i,xs,ys,zs); fflush(stderr);
loc[inode].x = atof(xs);
loc[inode].y = atof(ys);
loc[inode].z = atof(zs);
inode++;
} else if (anotherchar[0] == 'n') {
// read a vertex normal
fscanf(fp,"%s %s %s",xs,ys,zs);
// fprintf(stderr,"n %d %s %s %s\n",k,xs,ys,zs); fflush(stderr);
normal[inorm].x = atof(xs);
normal[inorm].y = atof(ys);
normal[inorm].z = atof(zs);
inorm++;
} else if (anotherchar[0] == 't') {
// read a vertex texture coordinate
fscanf(fp,"%s %s",xs,ys);
// fprintf(stderr,"n %d %s %s %s\n",k,xs,ys,zs); fflush(stderr);
texture[itext].x = atof(xs);
texture[itext].y = atof(ys);
itext++;
} else {
// if its not identifiable, skip it, do not scale, do not write
}
} else if (onechar[0] == 'f') {
// read a triangle line
new_tri = alloc_new_tri();
new_tri->index = num_tri;
//fprintf(stdout,"t");
//fprintf(stderr,"\nline %d, tri %d\n",nlines,new_tri->index);
volatile int node_index[3] = {0,0,0};
volatile int uv_index[3] = {0,0,0};
volatile int norm_index[3] = {0,0,0};
// read each set of values, like "39//123" or "5" or "1192/2524"
for (int nn=0; nn<3; nn++) {
char newval[32];
fscanf(fp,"%s",newval);
//if (nlines==247973) fprintf(stderr,"\n key is %s\n",newval);
// can't use sscanf, n//n isn't matched
//int i1,i2,i3;
//sscanf(newval,"%d/%d/%d",&i1,&i2,&i3);
//if (nlines==247973) fprintf(stderr," tokens are %d %d %d\n",i1,i2,i3);
// can't use strtok either, because n//n returns val/val and not val/null/val
//char* token = strtok(newval,"/");
//if (nlines==247973) fprintf(stderr," token is %s\n",token);
//token = strtok(NULL,"/");
//if (nlines==247973) fprintf(stderr," token is %s\n",token);
//token = strtok(NULL,"/");
//if (nlines==247973) fprintf(stderr," token is %s\n",token);
// look for a slash
int i;
for (i=0; i<strlen(newval); i++) {
if (newval[i] == '/' || newval[i] == '\0') break;
}
//if (nlines==247973) fprintf(stderr," i is %d\n",i);
char sub1[10];
strncpy(sub1,newval,i);
//if (nlines==247973) fprintf(stderr," sub is %s\n",sub1);
sub1[i] = '\0';
if (i>0) node_index[nn] = atoi(sub1);
//if (nlines==247973) fprintf(stderr," node %d is %d\n",0,node_index[0]);
//if (nlines==247973) fprintf(stderr," node %d is %d\n",nn,node_index[nn]);
// should we continue?
if (newval[i] == '/') {
int j;
for (j=i+1; j<strlen(newval); j++) {
if (newval[j] == '/' || newval[j] == '\0') break;
}
char sub2[10];
strncpy(sub2,newval+i+1,j);
sub2[j-i-1] = '\0';
if (j-i > 1) uv_index[nn] = atoi(sub2);
//if (nlines==247973) fprintf(stderr," node %d is %d\n",nn,node_index[nn]);
//if (nlines==247973) fprintf(stderr," nodeA %d is %d\n",0,node_index[0]);
// should we continue?
if (newval[j] == '/') {
int k;
for (k=j+1; k<strlen(newval); k++) {
if (newval[k] == '/' || newval[k] == '\0') break;
}
//if (nlines==247973) fprintf(stderr," nodeB %d is %d\n",0,node_index[0]);
char sub3[10];
strncpy(sub3,newval+j+1,k);
//if (nlines==247973) fprintf(stderr," nodeC %d is %d\n",0,node_index[0]);
sub3[k-j-1] = '\0';
//if (nlines==247973) fprintf(stderr," nodeD %d is %d\n",0,node_index[0]);
//if (nlines==247973) fprintf(stderr," norm %d is %d\n",nn,norm_index[nn]);
if (k-j > 1) norm_index[nn] = atoi(sub3);
//if (nlines==247973) fprintf(stderr," norm %d is %d\n",nn,norm_index[nn]);
//if (nlines==247973) fprintf(stderr," nodeE %d is %d\n",0,node_index[0]);
//if (nlines==247973) fprintf(stderr," node %d is %d\n",nn,node_index[nn]);
}
}
}
for (int j=0; j<3; j++) {
// flip the normals here, by reversing the node order
int targetj = j;
if (invert) targetj = 2-j;
if (node_index[j] < 1) {
fprintf(stderr,"ERROR (read_obj): node index in file is <1 on line %d\nQuitting.\n",nlines);
fprintf(stderr," nodes %d %d %d\n",node_index[0],node_index[1],node_index[2]);
fprintf(stderr," texts %d %d %d\n",uv_index[0],uv_index[1],uv_index[2]);
fprintf(stderr," norms %d %d %d\n",norm_index[0],norm_index[1],norm_index[2]);
exit(1);
}
new_node = add_to_nodes_list(new_tri,&num_nodes,j,&loc[node_index[j]-1],&nodebin);
new_tri->node[targetj] = new_node;
}
// set the node's normals here, if they were read in
for (int j=0; j<3; j++) {
int targetj = j;
if (invert) targetj = 2-j;
if (norm_index[j] < 1) {
new_tri->norm[targetj] = NULL;
} else {
new_norm = add_to_norms_list(&num_norms,&normal[norm_index[j]-1],&normbin);
new_tri->norm[targetj] = new_norm;
}
}
// set the node's texture coord here, if they were read in
for (int j=0; j<3; j++) {
int targetj = j;
if (invert) targetj = 2-j;
if (uv_index[j] < 1) {
new_tri->texture[targetj] = NULL;
} else {
new_text = add_to_textures_list(&num_texts,&texture[uv_index[j]-1],&textbin);
new_tri->texture[targetj] = new_text;
}
}
// set the adjacent triangle and midpoint pointers to NULL
for (int j=0; j<3; j++) {
new_tri->adjacent[j] = NULL;
new_tri->midpoint[j] = NULL;
}
num_tri++;
// add it on as the new head of the list
if (tri_head) {
new_tri->next_tri = tri_head;
tri_head = new_tri;
} else {
tri_head = new_tri;
tri_head->next_tri = NULL;
}
// fprintf(stderr," tri head is %d \n",tri_head->index);
//fscanf(fp,"%[\n]",twochar); /* read newline */
if (num_tri/DOTPER == (num_tri+DPMO)/DOTPER)
fprintf(stderr,".");
}
// skip the rest of the line, do not scale, do not write
fscanf(fp,"%[^\n]",sbuf); // read line beyond first char
fscanf(fp,"%[\n]",twochar); // read newline
}
fclose(fp);
fprintf(stderr,"\n %d tris\n",num_tri);
fprintf(stderr," %d nodes\n",num_nodes);
if (num_texts > 0) fprintf(stderr," %d texture coords\n",num_texts);
if (num_norms > 0) fprintf(stderr," %d normals\n",num_norms);
return(tri_head);
}
//#pragma GCC pop_options
/*
* Read in a RAW file
*
* This subroutine assumes that the nodes of all triangles are
* ordered in a counter-clockwise pattern when viewed from the
* top (outside).
*/
tri_pointer read_raw (char filename[MAX_FN_LEN],tri_pointer tri_head) {
int i,j,icnt;
int num_tri = 0;
int num_nodes = 0;
int num_norms = 0;
char twochar[2];
char sbuf[512];
char d[18][32];
VEC location,normal,nmin,nmax;
double temp[18];
node_ptr new_node;
norm_ptr new_norm;
//text_ptr new_text;
tri_pointer new_tri = NULL;
BIN nodebin;
NBIN normbin;
FILE *fp;
// open the .raw file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Prescanning %s",filename);
fflush(stderr);
// important, icnt is the node counter variable!
icnt = 0;
nmax.x = -9.e+9;
nmax.y = -9.e+9;
nmax.z = -9.e+9;
nmin.x = 9.e+9;
nmin.y = 9.e+9;
nmin.z = 9.e+9;
// read the input file and update statistics
// read a line from the input file
while (fscanf(fp,"%[^\n]",sbuf) != EOF) {
sscanf(sbuf,"%s %s %s %s %s %s %s %s %s",d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],d[8]);
if (!(int)isdigit(d[0][0]) && d[0][0] != '+' && d[0][0] != '-') {
// read a comment line, or some other descriptor
fscanf(fp,"%[\n]",twochar); // read up to newline
// fprintf(stdout,"%s\n",sbuf); // write comment
} else {
// read a triangle line
for (i=0; i<9; i++) temp[i] = atof(d[i]);
update_minmax (&temp[0], &nmin, &nmax);
update_minmax (&temp[3], &nmin, &nmax);
update_minmax (&temp[6], &nmin, &nmax);
if (icnt/DOTPER == (icnt+DPMO)/DOTPER) fprintf(stderr,".");
icnt++;
fscanf(fp,"%[^\n]",sbuf); // read up to newline
fscanf(fp,"%[\n]",twochar); // read newline
}
}
fprintf(stderr,"\n");
fclose(fp);
//fprintf(stderr,"found %d tris\n",icnt);
// Initialize bin structure
// fprintf(stderr,"%g %g %g %g %g %g\n",nmin.x,nmin.y,nmin.z,nmax.x,nmax.y,nmax.z);
//nodebin.dx = (nmax.x-nmin.x)/(BIN_COUNT-1);
//nodebin.start = nmin.x - 0.5*nodebin.dx;
//for (i=0;i<BIN_COUNT;i++) nodebin.b[i] = NULL;
(void) prepare_node_bin (&nodebin,nmin,nmax);
(void) prepare_norm_bin (&normbin);
// open the file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Opening file %s...",filename);
fflush(stderr);
// read a line from the input file
while (fscanf(fp,"%[^\n]",sbuf) != EOF) {
// read a triangle line
sscanf(sbuf,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],d[8],d[9],d[10],d[11],d[12],d[13],d[14],d[15],d[16],d[17]);
if (!(int)isdigit(d[0][0]) && d[0][0] != '+' && d[0][0] != '-') {
// read a comment line, or some other descriptor
fscanf(fp,"%[\n]",twochar); // read up to newline
// fprintf(stdout,"%s\n",sbuf); // write comment
} else {
for (i=0; i<18; i++) temp[i] = atof(d[i]);
// and make the tri
new_tri = alloc_new_tri();
new_tri->index = num_tri;
num_tri++;
//fprintf(stderr,"tri %d\n",num_tri); fflush(stderr);
// if we have 9 or more numbers, then we have a triangle
// fprintf(stderr,"loc %g %g %g\n",location.x,location.y,location.z); fflush(stderr);
//new_node = add_to_nodes_list(new_tri,&num_nodes,j,&location,&nodebin);
location.x = temp[0];
location.y = temp[1];
location.z = temp[2];
new_node = add_to_nodes_list(new_tri,&num_nodes,0,&location,&nodebin);
new_tri->node[0] = new_node;
//fprintf(stderr,"%d %g %g %g\n",num_tri,location.x,location.y,location.z); fflush(stderr);
location.x = temp[3];
location.y = temp[4];
location.z = temp[5];
new_node = add_to_nodes_list(new_tri,&num_nodes,1,&location,&nodebin);
new_tri->node[1] = new_node;
//fprintf(stderr,"%d %g %g %g\n",num_tri,location.x,location.y,location.z); fflush(stderr);
location.x = temp[6];
location.y = temp[7];
location.z = temp[8];
new_node = add_to_nodes_list(new_tri,&num_nodes,2,&location,&nodebin);
new_tri->node[2] = new_node;
//fprintf(stderr,"%d %g %g %g\n",num_tri,location.x,location.y,location.z); fflush(stderr);
/* set the node's normals here, if they were read in */
// currently not supported (j<=9 in here)
if ((int)isdigit(d[9][0]) || d[9][0] == '+' || d[9][0] == '-') {
normal.x = temp[9];
normal.y = temp[10];
normal.z = temp[11];
new_norm = add_to_norms_list(&num_norms,&normal,&normbin);
new_tri->norm[0] = new_norm;
normal.x = temp[12];
normal.y = temp[13];
normal.z = temp[14];
new_norm = add_to_norms_list(&num_norms,&normal,&normbin);
new_tri->norm[1] = new_norm;
normal.x = temp[15];
normal.y = temp[16];
normal.z = temp[17];
new_norm = add_to_norms_list(&num_norms,&normal,&normbin);
new_tri->norm[2] = new_norm;
} else {
new_tri->norm[0] = NULL;
new_tri->norm[1] = NULL;
new_tri->norm[2] = NULL;
}
//if (fabs(new_tri->norm[2].x) < 1.e-5 &&
// fabs(new_tri->norm[2].y) < 1.e-5 &&
// fabs(new_tri->norm[2].z) < 1.e-5 ) {
// fprintf(stderr,"Found zero norm! j=%d\n",j);
// for (i=0; i<18; i++) fprintf(stderr," %d %g\n",i,temp[i]);
// exit(0);
//}
/* set the adjacent triangle and midpoint pointers to NULL */
for (j=0; j<3; j++) {
new_tri->adjacent[j] = NULL;
new_tri->midpoint[j] = NULL;
}
/* add it on as the new head of the list */
if (tri_head) {
new_tri->next_tri = tri_head;
tri_head = new_tri;
} else {
tri_head = new_tri;
tri_head->next_tri = NULL;
}
fscanf(fp,"%[^\n]",sbuf); // read line beyond first char
fscanf(fp,"%[\n]",twochar); // read newline
if (num_tri/DOTPER == (num_tri+DPMO)/DOTPER) fprintf(stderr,".");
}
}
fclose(fp);
fprintf(stderr,"%d tris\n",num_tri);
return(tri_head);
}
/*
* Read in a TIN file
*
* This subroutine assumes that the nodes of all triangles are
* ordered in a counter-clockwise pattern when viewed from the
* top (outside).
*/
tri_pointer read_tin (char filename[MAX_FN_LEN],tri_pointer tri_head) {
int i,j,icnt,err;
int num_tri = 0;
int num_nodes = 0;
int num_norms = 0;
char onechar;
char twochar[2];
char sbuf[256];
char xs[20];
VEC location,normal,nmin,nmax;
double temp[9];
node_ptr new_node;
norm_ptr new_norm;
//tri_pointer tri_head = NULL;
tri_pointer new_tri = NULL;
BIN nodebin;
NBIN normbin;
//long int fpos;
FILE *fp;
// open the file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Prescanning %s",filename);
fflush(stderr);
// important, icnt is the node counter variable!
icnt = 0;
nmax.x = -9.e+9;
nmax.y = -9.e+9;
nmax.z = -9.e+9;
nmin.x = 9.e+9;
nmin.y = 9.e+9;
nmin.z = 9.e+9;
// read the input file and update statistics
while (fread(&onechar,sizeof(char),1,fp) == 1) {
if (onechar == '#') {
/* read a comment line */
fscanf(fp,"%[^\n]",sbuf); /* read comment beyond '#' */
fscanf(fp,"%[\n]",twochar); /* read newline */
// fprintf(stdout,"#%s\n",sbuf); /* write comment */
// if this is a data line, read it
} else if (onechar == 't') {
/* read a triangle line */
// read three nodes
for (j=0; j<9; j++) {
//fpos = ftell (fp);
err = fscanf(fp,"%s",xs);
// check this value for non-math character
if (err < 1 || xs[0] < 43 || xs[0] == 47 || xs[0] > 57) {
// if it is, fill the rest of the data with zeros
for (i=j; i<9; i++) temp[i] = 0.;
// do not rewind the file
//fseek (fp, fpos, SEEK_SET);
// and break out of this loop
break;
} else {
// if it's a math character, save it
temp[j] = atof(xs);
//fprintf(stderr," %g",temp[j]);
}
}
//fprintf(stderr,"\nj %d\n",j);
// if we have a complete triangle
if (j>=9) {
// fprintf(stdout,"loc %g %g %g\n",location.x,location.y,location.z); exit(0);
update_minmax (&temp[0], &nmin, &nmax);
update_minmax (&temp[3], &nmin, &nmax);
update_minmax (&temp[6], &nmin, &nmax);
if (icnt/DOTPER == (icnt+DPMO)/DOTPER) fprintf(stderr,".");
icnt++;
}
fscanf(fp,"%[^\n]",sbuf); // read line beyond first char
fscanf(fp,"%[\n]",twochar); /* read newline */
} else {
fscanf(fp,"%[^\n]",sbuf); // read line beyond first char
fscanf(fp,"%[\n]",twochar); /* read newline */
}
}
fprintf(stderr,"\n");
fclose(fp);
//fprintf(stderr,"found %d tris\n",icnt);
// Initialize bin structure
// fprintf(stderr,"%g %g %g %g %g %g\n",nmin.x,nmin.y,nmin.z,nmax.x,nmax.y,nmax.z);
//nodebin.dx = (nmax.x-nmin.x)/(BIN_COUNT-1);
//nodebin.start = nmin.x - 0.5*nodebin.dx;
//for (i=0;i<BIN_COUNT;i++) nodebin.b[i] = NULL;
(void) prepare_node_bin (&nodebin,nmin,nmax);
(void) prepare_norm_bin (&normbin);
// open the file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Opening file %s...",filename);
fflush(stderr);
// read the input file and update statistics
while (fread(&onechar,sizeof(char),1,fp) == 1) {
if (onechar == '#') {
/* read a comment line */
fscanf(fp,"%[^\n]",sbuf); /* read comment beyond '#' */
fscanf(fp,"%[\n]",twochar); /* read newline */
// fprintf(stdout,"#%s\n",sbuf); // write comment
// if this is a data line, read it
} else if (onechar == 'n' || onechar == 't') {
/* read a triangle line */
// read three nodes or normals or both
for (j=0; j<9; j++) {
//fpos = ftell (fp);
err = fscanf(fp,"%s",xs);
// check this value for non-math character
if (err < 1 || xs[0] < 43 || xs[0] == 47 || xs[0] > 57) {
// if it is, fill the rest of the data with zeros
for (i=j; i<9; i++) temp[i] = 0.;
// do not rewind the file
//fseek (fp, fpos, SEEK_SET);
// and break out of this loop
break;
} else {
// if it's a math character, save it
temp[j] = atof(xs);
}
}
// if there weren't even 9 numbers, then we have no triangle or
// normal on this line
if (j<9) continue;
// set the normals
if (onechar == 'n') {
// if there is an active tri, save the normals
if (new_tri) {
normal.x = temp[0];
normal.y = temp[1];
normal.z = temp[2];
new_norm = add_to_norms_list(&num_norms,&normal,&normbin);
new_tri->norm[0] = new_norm;
normal.x = temp[3];
normal.y = temp[4];
normal.z = temp[5];
new_norm = add_to_norms_list(&num_norms,&normal,&normbin);
new_tri->norm[1] = new_norm;
normal.x = temp[6];
normal.y = temp[7];
normal.z = temp[8];
new_norm = add_to_norms_list(&num_norms,&normal,&normbin);
new_tri->norm[2] = new_norm;
}
// set the triangle
} else {
// and make the tri
new_tri = alloc_new_tri();
new_tri->index = num_tri;
num_tri++;
//fprintf(stderr,"tri %d\n",num_tri); fflush(stderr);
// if we have 9 or more numbers, then we have a triangle
// fprintf(stderr,"loc %g %g %g\n",location.x,location.y,location.z); fflush(stderr);
//new_node = add_to_nodes_list(new_tri,&num_nodes,j,&location,&nodebin);
location.x = temp[0];
location.y = temp[1];
location.z = temp[2];
new_node = add_to_nodes_list(new_tri,&num_nodes,0,&location,&nodebin);
new_tri->node[0] = new_node;
//fprintf(stderr,"%d %g %g %g\n",num_tri,location.x,location.y,location.z); fflush(stderr);
location.x = temp[3];
location.y = temp[4];
location.z = temp[5];
new_node = add_to_nodes_list(new_tri,&num_nodes,1,&location,&nodebin);
new_tri->node[1] = new_node;
//fprintf(stderr,"%d %g %g %g\n",num_tri,location.x,location.y,location.z); fflush(stderr);
location.x = temp[6];
location.y = temp[7];
location.z = temp[8];
new_node = add_to_nodes_list(new_tri,&num_nodes,2,&location,&nodebin);
new_tri->node[2] = new_node;
//fprintf(stderr,"%d %g %g %g\n",num_tri,location.x,location.y,location.z); fflush(stderr);
/* set the adjacent triangle and midpoint pointers to NULL */
for (j=0; j<3; j++) {
new_tri->adjacent[j] = NULL;
new_tri->midpoint[j] = NULL;
}
/* add it on as the new head of the list */
if (tri_head) {
new_tri->next_tri = tri_head;
tri_head = new_tri;
} else {
tri_head = new_tri;
tri_head->next_tri = NULL;
}
} // end setting the triangle
fscanf(fp,"%[\n]",twochar); /* read newline */
if (num_tri/DOTPER == (num_tri+DPMO)/DOTPER) fprintf(stderr,".");
} else {
fscanf(fp,"%[^\n]",sbuf); // read line beyond first char
fscanf(fp,"%[\n]",twochar); /* read newline */
}
}
fclose(fp);
fprintf(stderr,"%d tris\n",num_tri);
return(tri_head);
}
/*
* Read in a GMSH Mesh (.msh) file
*/
tri_pointer read_msh (char filename[MAX_FN_LEN],tri_pointer tri_head) {
int i,j;
int num_tri = 0;
int num_nodes = 0;
int nodespace,num_elems;
int index,node_index[3];
//char onechar,anotherchar;
char twochar[2];
char sbuf[128];
//char is[20],xs[30],ys[30],zs[30];
char t[10][24]; // string tokens
node_ptr *node_arry; // vertex indexes
node_ptr new_node = NULL;
//tri_pointer tri_head = NULL;
tri_pointer new_tri = NULL;
FILE *fp;
// open the .msh file for reading
fp = fopen(filename,"r");
if (fp==NULL) {
fprintf(stderr,"Could not open input file %s\n",filename);
fflush(stderr);
exit(0);
}
fprintf(stderr,"Opening file %s...",filename);
fflush(stderr);
// first line is "$NOD"
fscanf(fp,"%[^\n]",sbuf); // read comment beyond '#'
fscanf(fp,"%[\n]",twochar); // read newline
// second line is the number of nodes
fscanf(fp,"%s",t[0]);
num_nodes = atoi(t[0]);
fscanf(fp,"%[^\n]",sbuf); // read comment beyond '#'
fscanf(fp,"%[\n]",twochar); // read newline
// now, we know how many nodes there will be: size and malloc the array
nodespace = num_nodes+1000;
node_arry = (node_ptr*)malloc(nodespace*sizeof(node_ptr));
// and read all of the nodes
for (i=0; i<num_nodes; i++) {
fscanf(fp,"%s %s %s %s",t[0],t[1],t[2],t[3]);
index = atoi(t[0]);
// actually create the node instead of calling add_to_nodes_list
new_node = (NODE *)malloc(sizeof(NODE));
new_node->index = i;
new_node->loc.x = atof(t[1]);
new_node->loc.y = atof(t[2]);
new_node->loc.z = atof(t[3]);
#ifdef CONN
new_node->num_conn = 0;
new_node->max_conn = 0;
#endif
new_node->next_node = node_head;
node_head = new_node;
// add it to our array for easy indexing
node_arry[index] = new_node;
fscanf(fp,"%[^\n]",sbuf); // read comment beyond '#'
fscanf(fp,"%[\n]",twochar); // read newline
}
// read lines with "$ENDNOD" and "$ELM"
fscanf(fp,"%[^\n]",sbuf); // read comment beyond '#'
fscanf(fp,"%[\n]",twochar); // read newline
fscanf(fp,"%[^\n]",sbuf); // read comment beyond '#'
fscanf(fp,"%[\n]",twochar); // read newline
// then, read the number of elements
fscanf(fp,"%s",t[0]);
num_elems = atoi(t[0]);
fscanf(fp,"%[^\n]",sbuf); // read comment beyond '#'
fscanf(fp,"%[\n]",twochar); // read newline
// and read all of the elements
for (i=0; i<num_elems; i++) {
fscanf(fp,"%s %s %s %s %s",t[0],t[1],t[2],t[3],t[4]);
// if 2nd number is 1, it's a line; is 2, it's a tri
if (atoi(t[1]) == 2) {
// read a triangle line
new_tri = alloc_new_tri();
new_tri->index = num_tri;
fscanf(fp,"%s %s %s",t[0],t[1],t[2]);
node_index[0] = atoi(t[0]);
node_index[1] = atoi(t[1]);
node_index[2] = atoi(t[2]);
for (j=0; j<3; j++) {
if (node_index[j] < 1) {
fprintf(stderr,"ERROR (read_msh): node index in file is <1\nQuitting.");
exit(1);
}
new_tri->node[j] = node_arry[node_index[j]];
#ifdef CONN
add_conn_tri(new_tri->node[j], new_tri, j);
//new_tri->node[j]->conn_tri[new_tri->node[j]->num_conn] = new_tri;
//new_tri->node[j]->conn_tri_node[new_tri->node[j]->num_conn] = j;
//new_tri->node[j]->num_conn++;
#endif
}
// set the adjacent triangle and midpoint pointers to NULL
for (j=0; j<3; j++) {
new_tri->adjacent[j] = NULL;
new_tri->midpoint[j] = NULL;
}
num_tri++;
// add it on as the new head of the list
if (tri_head) {
new_tri->next_tri = tri_head;
tri_head = new_tri;
} else {
tri_head = new_tri;
tri_head->next_tri = NULL;
}