Craniofacial identity authentication(颅面身份认证)


背景

计算机颅像重合与颅骨重建技术至今发展多年,研究内容涉及模式识别、解剖医学、计算机视觉和法医人类学等多个学科领域,毫无疑问已成为国内外研究的热点,近年来多位国内外学者致力于颅像重合与颅骨重建的研究。

颅面身份认证是一项针对颅骨重建与颅像重合的研究,在公安法医学,考古学,医学整形等诸多领域有普遍应用,传统手工颅骨重建与颅面复原耗费时间长且结果难以实现,近年来逐步兴起的计算机辅助颅骨重建与颅像重合的方法使得结果更加真切,高效。

基于深度学习预测特征点的三维颅面配准(TPS)

[1]徐亚丽. 基于深度学习的颅面特征点标定与配准[D].青岛大学,2021.DOI:10.27262/d.cnki.gqdau.2021.002285.]

首先,使用深度学习网络来训练特征点检测模型,利用训练好的网络模型预测二维坐标,最后映射回三维数据;然后,将预测得到的三维特征点作为配准的控制点应用到 TPS 配准中。

基于深度学习预测特征点的 TPS 配准一共包括以下几个步骤:首先利用基于深度图的三维面貌特征点自动标定网络预测三维目标面貌数据的 7 个特征点;利用基于信息图的三维颅骨特征点自动标定网络预测三维目标颅骨数据的 8 个特征点。其次,确定参考面貌和颅骨数据上的特征点。第三,使用目标数据上预测的特征点以 及参考数据上标记好的特征点作为控制点来进行 TPS 变换,即将参考颅面向目标颅面变换。最后,在目标颅面上寻找每个参考颅面上点的最近点,得到最终的配准结果。

颅面复原方法综述

[1]王琳,赵俊莉,段福庆,周明全.颅面复原方法综述[J].计算机工程,2019,45(12):8-18.DOI:10.19678/j.issn.1000-3428.0053279.

基于知识的颅面复原方法

基于稀疏软组织厚度的颅面复原方法

对 照 匹 配 法 ( 显式的稠密软组织复制方法)

模 板 变 形 法 ( 隐式的稠密软组织复制方法)

统计模型法

基于最小二乘拟合的颅面统计复原方法

基于后验概率最大的颅面统计复原

基于层次化颅面统计模型的复原

基于统计回归的颅面复原

.OBJ文件读取(matlab)

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
function [node_xyz, face_node ]= objread( input_file_name )

%*****************************************************************************80
%
%% OBJ_DISPLAY displays the faces of a shape defined by an OBJ file.
%
% Usage:
%
% obj_display ( 'file.obj' )
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 27 September 2008
%
% Author:
%
% John Burkardt
%
% Get sizes.
%
[ node_num, face_num, normal_num, order_max ] = obj_size ( input_file_name );
%
% Get the data.
%
[ node_xyz, face_order, face_node ] = ...
obj_read ( input_file_name, node_num, face_num, normal_num, order_max );
%
% FACE_NODE may contain polygons of different orders.
% To make the call to PATCH, we will assume all the polygons are the same order.
% To do so, we'll simply "stutter" the first node in each face list.
%
for face = 1 : face_num
face_node(face_order(face)+1:order_max,face) = face_node(1,face);
end
%
% If any node index is still less than 1, set the whole face to 1's.
% We're giving up on this presumably meaningless face, but we need to
% do it in a way that keeps MATLAB happy!
%
for face = 1 : face_num
for i = 1 : order_max
face_node(i,face) = max ( face_node(i,face), 1 );
end
end
%
% Display the shape.
% The TITLE function will interpret underscores in the title.
% We need to unescape such escape sequences!
return
end
function c = ch_cap ( c )

%*****************************************************************************80
%
%% CH_CAP capitalizes a single character.
%
% Parameters:
%
% Input, character C, the character to capitalize.
% Output, character C, the capitalized character.
%
if ( 'a' <= c && c <= 'z' )
c = c + 'A' - 'a';
end

return
end
function truefalse = ch_eqi ( c1, c2 )

%*****************************************************************************80
%
%% CH_EQI is a case insensitive comparison of two characters for equality.
%
% Example:
%
% CH_EQI ( 'A', 'a' ) is TRUE.
%
%
% Parameters:
%
% Input, character C1, C2, the characters to compare.
%
% Output, logical TRUEFALSE, is TRUE (1) if the characters are equal.
%
FALSE = 0;
TRUE = 1;

if ( ch_cap ( c1 ) == ch_cap ( c2 ) )
truefalse = TRUE;
else
truefalse = FALSE;
end

return
end
function value = ch_index ( s, c )

%*****************************************************************************80
%
%% CH_INDEX is the first occurrence of a character in a string.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 01 May 2004
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, string S, the string to be searched.
%
% Input, character C, the character to be searched for.
%
% Output, integer VALUE, the location of the first occurrence of C
% in the string, or 0 if C does not occur.
%
value = 0;

for i = 1 : length ( s )

if ( s(i:i) == c )
value = i;
return
end

end

return
end
function value = ch_is_control ( ch )

%*****************************************************************************80
%
%% CH_IS_CONTROL is TRUE if a character is a control character.
%
% Discussion:
%
% A "control character" has ASCII code <= 31 or 127 <= ASCII code.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 27 September 2008
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, character CH, the character to be tested.
%
% Output, integer CH_IS_CONTROL, TRUE if the character is a control
% character, and FALSE otherwise.
%
if ( ch <= 31 || 127 <= ch )
value = 1;
else
value = 0;
end

return
end
function truefalse = ch_is_digit ( c )

%*****************************************************************************80
%
% CH_IS_DIGIT returns TRUE if the character C is a digit.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Parameters:
%
% Input, character C, a character.
%
% Output, integer TRUEFALSE, is TRUE (1) if C is a digit, FALSE (0) otherwise.
%
TRUE = 1;
FALSE = 0;

if ( '0' <= c && c <= '9' )
truefalse = TRUE;
else
truefalse = FALSE;
end

return
end
function digit = ch_to_digit ( c )

%*****************************************************************************80
%
%% CH_TO_DIGIT returns the integer value of a base 10 digit.
%
% Example:
%
% C DIGIT
% --- -----
% '0' 0
% '1' 1
% ... ...
% '9' 9
% ' ' 0
% 'X' -1
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 22 November 2003
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, character C, the decimal digit, '0' through '9' or blank
% are legal.
%
% Output, integer DIGIT, the corresponding integer value. If C was
% 'illegal', then DIGIT is -1.
%
if ( '0' <= c && c <= '9' )

digit = c - '0';

elseif ( c == ' ' )

digit = 0;

else

digit = -1;

end

return
end
function [ node_xyz, face_order, face_node, normal_vector, vertex_normal ] = ...
obj_read ( input_file_name, node_num, face_num, normal_num, order_max )

%*****************************************************************************80
%
%% OBJ_READ reads graphics information from a Wavefront OBJ file.
%
% Discussion:
%
% It is intended that the information read from the file can
% either start a whole new graphics object, or simply be added
% to a current graphics object via the '<<' command.
%
% This is controlled by whether the input values have been zeroed
% out or not. This routine simply tacks on the information it
% finds to the current graphics object.
%
% Example:
%
% # magnolia.obj
%
% v -3.269770 -39.572201 0.876128
% v -3.263720 -39.507999 2.160890
% ...
% v 0.000000 -9.988540 0.000000
% vn 1.0 0.0 0.0
% ...
% vn 0.0 1.0 0.0
%
% f 8 9 11 10
% f 12 13 15 14
% ...
% f 788 806 774
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
%
% Parameters:
%
% Input, string INPUT_FILE_NAME, the name of the input file.
% Input, integer NODE_NUM, the number of points.
% Input, integer FACE_NUM, the number of faces.
% Input, integer NORMAL_NUM, the number of normal vectors.
% Input, integer ORDER_MAX, the maximum number of vertices per face.
% Output, real NODE_XYZ(3,NODE_NUM), the coordinates of points.
% Output, integer FACE_ORDER(FACE_NUM), the number of vertices per face.
% Output, integer FACE_NODE(ORDER_MAX,FACE_NUM), the nodes making faces.
% Output, real NORMAL_VECTOR(3,NORMAL_NUM), normal vectors.
% Output, integer VERTEX_NORMAL(ORDER_MAX,FACE_NUM), the indices of normal
% vectors per vertex.
%
face = 0;
node = 0;
normal = 0;
text_num = 0;

face_node = zeros ( order_max, face_num );
face_order = zeros ( face_num, 1 );
node_xyz = zeros ( 3, node_num );
normal_vector = zeros ( 3, normal_num );
vertex_normal = zeros ( order_max, face_num );
%
% If no file input, try to get one from the user.
%
if ( nargin < 1 )
input_file_name = input ( 'Enter the name of the ASCII OBJ file.' );
if ( isempty ( input_file_name ) )
return
end
end
%
% Open the file.
%
input_file_unit = fopen ( input_file_name, 'r' );

if ( input_file_unit < 0 )
fprintf ( 1, '\n' );
fprintf ( 1, 'OBJ_READ - Fatal error!\n' );
fprintf ( 1, ' Could not open the file "%s".\n', input_file_name );
error ( 'OBJ_READ - Fatal error!' );
end
%
% Read a line of text from the file.
%
while ( 1 )

text = fgetl ( input_file_unit );

if ( text == -1 )
break
end

text_num = text_num + 1;
%
% Replace any control characters (in particular, TAB's) by blanks.
%
s_control_blank ( text );

done = 1;
word_index = 0;
%
% Read a word from the line.
%
[ word, done ] = word_next_read ( text, done );
%
% If no more words in this line, read a new line.
%
if ( done )
continue
end
%
% If this word begins with '#' or '$', then it's a comment. Read a new line.
%
if ( word(1) == '#' || word(1) == '$' )
continue
end

word_index = word_index + 1;

if ( word_index == 1 )
word_one = word;
end
%
% BEVEL
% Bevel interpolation.
%
if ( s_eqi ( word_one, 'BEVEL' ) )
%
% BMAT
% Basis matrix.
%
elseif ( s_eqi ( word_one, 'BMAT' ) )
%
% C_INTERP
% Color interpolation.
%
elseif ( s_eqi ( word_one, 'C_INTERP' ) )
%
% CON
% Connectivity between free form surfaces.
%
elseif ( s_eqi ( word_one, 'CON' ) )
%
% CSTYPE
% Curve or surface type.
%
elseif ( s_eqi ( word_one, 'CSTYPE' ) )
%
% CTECH
% Curve approximation technique.
%
elseif ( s_eqi ( word_one, 'CTECH' ) )
%
% CURV
% Curve.
%
elseif ( s_eqi ( word_one, 'CURV' ) )
%
% CURV2
% 2D curve.
%
elseif ( s_eqi ( word_one, 'CURV2' ) )
%
% D_INTERP
% Dissolve interpolation.
%
elseif ( s_eqi ( word_one, 'D_INTERP' ) )
%
% DEG
% Degree.
%
elseif ( s_eqi ( word_one, 'DEG' ) )
%
% END
% End statement.
%
elseif ( s_eqi ( word_one, 'END' ) )
%
% F V1 V2 V3 ...
% or
% F V1/VT1/VN1 V2/VT2/VN2 ...
% or
% F V1//VN1 V2//VN2 ...
%
% Face.
% A face is defined by the vertices.
% Optionally, slashes may be used to include the texture vertex
% and vertex normal indices.
%
elseif ( s_eqi ( word_one, 'F' ) )

face = face + 1;

vertex = 0;

while ( 1 )

[ word, done ] = word_next_read ( text, done );

if ( done )
break
end

vertex = vertex + 1;
order_max = max ( order_max, vertex );
%
% Locate the slash characters in the word, if any.
%
i1 = ch_index ( word, '/' );
if ( 0 < i1 )
i2 = ch_index ( word(i1+1), '/' ) + i1;
else
i2 = 0;
end
%
% Read the vertex index.
%
itemp = s_to_i4 ( word );

face_node(vertex,face) = itemp;
face_order(face) = face_order(face) + 1;
%
% If there are two slashes, then read the data following the second one.
%
if ( 0 < i2 )

itemp = s_to_i4 ( word(i2+1) );

vertex_normal(vertex,face) = itemp;

end

end
%
% G
% Group name.
%
elseif ( s_eqi ( word_one, 'G' ) )
%
% HOLE
% Inner trimming loop.
%
elseif ( s_eqi ( word_one, 'HOLE' ) )
%
% L
% A line, described by a sequence of vertex indices.
% Are the vertex indices 0 based or 1 based?
%
elseif ( s_eqi ( word_one, 'L' ) )
%
% LOD
% Level of detail.
%
elseif ( s_eqi ( word_one, 'LOD' ) )
%
% MG
% Merging group.
%
elseif ( s_eqi ( word_one, 'MG' ) )
%
% MTLLIB
% Material library.
%
elseif ( s_eqi ( word_one, 'MTLLIB' ) )
%
% O
% Object name.
%
elseif ( s_eqi ( word_one, 'O' ) )
%
% P
% Point.
%
elseif ( s_eqi ( word_one, 'P' ) )
%
% PARM
% Parameter values.
%
elseif ( s_eqi ( word_one, 'PARM' ) )
%
% S
% Smoothing group.
%
elseif ( s_eqi ( word_one, 'S' ) )
%
% SCRV
% Special curve.
%
elseif ( s_eqi ( word_one, 'SCRV' ) )
%
% SHADOW_OBJ
% Shadow casting.
%
elseif ( s_eqi ( word_one, 'SHADOW_OBJ' ) )
%
% SP
% Special point.
%
elseif ( s_eqi ( word_one, 'SP' ) )
%
% STECH
% Surface approximation technique.
%
elseif ( s_eqi ( word_one, 'STECH' ) )
%
% STEP
% Stepsize.
%
elseif ( s_eqi ( word_one, 'STEP' ) )
%
% SURF
% Surface.
%
elseif ( s_eqi ( word_one, 'SURF' ) )
%
% TRACE_OBJ
% Ray tracing.
%
elseif ( s_eqi ( word_one, 'TRACE_OBJ' ) )
%
% TRIM
% Outer trimming loop.
%
elseif ( s_eqi ( word_one, 'TRIM' ) )
%
% USEMTL
% Material name.
%
elseif ( s_eqi ( word_one, 'USEMTL' ) )
%
% V X Y Z
% Geometric vertex.
%
elseif ( s_eqi ( word_one, 'V' ) )

node = node + 1;

for i = 1 : 3
[ word, done ] = word_next_read ( text, done );
temp = s_to_r8 ( word );
node_xyz(i,node) = temp;
end
%
% VN
% Vertex normals.
%
elseif ( s_eqi ( word_one, 'VN' ) )

normal = normal + 1;

for i = 1 : 3
[ word, done ] = word_next_read ( text, done );
temp = s_to_r8 ( word );
normal_vector(i,normal) = temp;
end
%
% VT
% Vertex texture.
%
elseif ( s_eqi ( word_one, 'VT' ) )
%
% VP
% Parameter space vertices.
%
elseif ( s_eqi ( word_one, 'VP' ) )
%
% Unrecognized keyword.
%
else

end

end

fclose ( input_file_unit );

return
end
function [ node_num, face_num, normal_num, order_max ] = obj_size ( ...
input_file_name )

%*****************************************************************************80
%
%% OBJ_SIZE determines sizes of graphics objects in an Alias OBJ file.
%
% Discussion:
%
% The only items of interest to this routine are vertices,
% faces, and normal vectors.
%
% Example:
%
% # magnolia.obj
%
% v -3.269770 -39.572201 0.876128
% v -3.263720 -39.507999 2.160890
% ...
% v 0.000000 -9.988540 0.000000
%
% vn 1.0 0.0 0.0
% ...
% vn 0.0 1.0 0.0
%
% f 8 9 11 10
% f 12 13 15 14
% ...
% f 788 806 774
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 26 September 2008
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, string INPUT_FILE_NAME, the input file name.
%
% Output, integer NODE_NUM, the number of points.
%
% Output, integer FACE_NUM, the number of faces.
%
% Output, integer NORMAL_NUM, the number of normal vectors.
%
% Output, integer ORDER_MAX, the maximum face order.
%
face_num = 0;
node_num = 0;
normal_num = 0;
order_max = 0;
text_num = 0;
%
% If no file input, try to get one from the user.
%
if ( nargin < 1 )
input_file_name = input ( 'Enter the name of the ASCII OBJ file.' );
if ( isempty ( input_file_name ) )
return
end
end
%
% Open the file.
%
input_file_unit = fopen ( input_file_name, 'r' );

if ( input_file_unit < 0 )
fprintf ( 1, '\n' );
fprintf ( 1, 'OBJ_SIZE - Fatal error!\n' );
fprintf ( 1, ' Could not open the file "%s".\n', input_file_name );
error ( 'OBJ_SIZE - Fatal error!' );
end
%
% Read a line of text from the file.
%
while ( 1 )

text = fgetl ( input_file_unit );

if ( text == -1 )
break
end

text_num = text_num + 1;
%
% Replace any control characters (in particular, TABs) by blanks.
%
s_control_blank ( text );

done = 1;
word_index = 0;
%
% Read a word from the line.
%
[ word, done ] = word_next_read ( text, done );
%
% If no more words in this line, read a new line.
%
if ( done )
continue
end
%
% If this word begins with '#' or '$', then it is a comment. Read a new line.
%
if ( word(1) == '#' || word(1) == '$' )
continue
end

word_index = word_index + 1;

if ( word_index == 1 )
word_one = word;
end
%
% F V1 V2 V3 ...
% or
% F V1/VT1/VN1 V2/VT2/VN2 ...
% or
% F V1//VN1 V2//VN2 ...
%
% Face.
% A face is defined by the vertices.
% Optionally, slashes may be used to include the texture vertex
% and vertex normal indices.
%
if ( s_eqi ( word_one, 'F' ) )

face_num = face_num + 1;

vertex = 0;

while ( 1 )

[ word, done ] = word_next_read ( text, done );

if ( done )
break
end

vertex = vertex + 1;
order_max = max ( order_max, vertex );
%
% Locate the slash characters in the word, if any.
%
i1 = ch_index ( word, '/' );
if ( 0 < i1 )
i2 = ch_index ( word(i1+1), '/' ) + i1;
else
i2 = 0;
end
%
% Read the vertex index.
%
s_to_i4 ( word );
%
% If there are two slashes, then read the data following the second one.
%
if ( 0 < i2 )
s_to_i4 ( word(i2+1) );
end

end
%
% V X Y Z W
% Geometric vertex.
%
elseif ( s_eqi ( word_one, 'V' ) )

node_num = node_num + 1;
continue
%
% VN
% Vertex normals.
%
elseif ( s_eqi ( word_one, 'VN' ) )

normal_num = normal_num + 1;
continue

end

end

fclose ( input_file_unit );

return
end
function s = s_control_blank ( s )

%*****************************************************************************80
%
%% S_CONTROL_BLANK replaces control characters with blanks.
%
% Discussion:
%
% A "control character" has ASCII code <= 31 or 127 <= ASCII code.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 27 September 2008
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input/output, string S, the string to be transformed.
%
s_length = s_len_trim ( s );

for i = 1 : s_length
if ( ch_is_control ( s(i) ) )
s(i) = ' ';
end
end

return
end
function value = s_eqi ( s1, s2 )

%*****************************************************************************80
%
%% S_EQI is a case insensitive comparison of two strings for equality.
%
% Example:
%
% S_EQI ( 'Anjana', 'ANJANA' ) is TRUE.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 30 April 2004
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, string S1, S2, the strings to compare.
%
% Output, logical VALUE, is TRUE if the strings are equal.
%
FALSE = 0;
TRUE = 1;

len1 = length ( s1 );
len2 = length ( s2 );
lenc = min ( len1, len2 );

for i = 1 : lenc

c1 = ch_cap ( s1(i) );
c2 = ch_cap ( s2(i) );

if ( c1 ~= c2 )
value = FALSE;
return
end

end

for i = lenc + 1 : len1
if ( s1(i) ~= ' ' )
value = FALSE;
return
end
end

for i = lenc + 1 : len2
if ( s2(i) ~= ' ' )
value = FALSE;
return
end
end

value = TRUE;

return
end
function len = s_len_trim ( s )

%*****************************************************************************80
%
%% S_LEN_TRIM returns the length of a character string to the last nonblank.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 14 June 2003
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, string S, the string to be measured.
%
% Output, integer LEN, the length of the string up to the last nonblank.
%
len = length ( s );

while ( 0 < len )
if ( s(len) ~= ' ' )
return
end
len = len - 1;
end

return
end
function ival = s_to_i4 ( s )

%*****************************************************************************80
%
%% S_TO_I4 reads an integer value from a string.
%
% Parameters:
%
% Input, string S, a string to be examined.
%
% Output, integer IVAL, the integer value read from the string.
%
sgn = 1;
state = 0;
ival = 0;

i = 0;

while ( i < s_len_trim ( s ) )

i = i + 1;
c = s(i);

if ( state == 0 )

if ( c == ' ' )

elseif ( c == '-' )
state = 1;
sgn = -1;
elseif ( c == '+' )
state = 1;
sgn = +1;
elseif ( '0' <= c && c <= '9' )
state = 2;
ival = c - '0';
else
fprintf ( '\n' );
fprintf ( 'S_TO_I4 - Fatal error!\n' );
fprintf ( ' Illegal character %c while in state %d.\n', c, state );
return;
end
%
% Have read the sign, now expecting the first digit.
%
elseif ( state == 1 )

if ( c == ' ' )

elseif ( '0' <= c && c <= '9' )
state = 2;
ival = c - '0';
else
fprintf ( '\n' );
fprintf ( 'S_TO_I4 - Fatal error!\n' );
fprintf ( ' Illegal character %c while in state %d.\n', c, state );
return
end
%
% Have read at least one digit, expecting more.
%
elseif ( state == 2 )

if ( '0' <= c && c <= '9' )
ival = 10 * ival + c - '0';
else
ival = sgn * ival;
return;
end

end

end
%
% If we read all the characters in the string, see if we're OK.
%
if ( state ~= 2 )
fprintf ( '\n' );
fprintf ( 'S_TO_I4 - Fatal error!\n' );
fprintf ( ' Did not read enough information to define an integer!\n' );
return;
end

ival = sgn * ival;

return
end
function [ r, lchar, ierror ] = s_to_r8 ( s )

%*****************************************************************************80
%
%% S_TO_R8 reads an R8 from a string.
%
% Discussion:
%
% This routine will read as many characters as possible until it reaches
% the end of the string, or encounters a character which cannot be
% part of the real number.
%
% Legal input is:
%
% 1 blanks,
% 2 '+' or '-' sign,
% 2.5 spaces
% 3 integer part,
% 4 decimal point,
% 5 fraction part,
% 6 'E' or 'e' or 'D' or 'd', exponent marker,
% 7 exponent sign,
% 8 exponent integer part,
% 9 exponent decimal point,
% 10 exponent fraction part,
% 11 blanks,
% 12 final comma or semicolon.
%
% with most quantities optional.
%
% Example:
%
% S R
%
% '1' 1.0
% ' 1 ' 1.0
% '1A' 1.0
% '12,34,56' 12.0
% ' 34 7' 34.0
% '-1E2ABCD' -100.0
% '-1X2ABCD' -1.0
% ' 2E-1' 0.2
% '23.45' 23.45
% '-4.2E+2' -420.0
% '17d2' 1700.0
% '-14e-2' -0.14
% 'e2' 100.0
% '-12.73e-9.23' -12.73 * 10.0**(-9.23)
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 22 November 2003
%
% Author:
%
% John Burkardt
%
% Parameters:
%
% Input, string S, the string containing the
% data to be read. Reading will begin at position 1 and
% terminate at the end of the string, or when no more
% characters can be read to form a legal real. Blanks,
% commas, or other nonnumeric data will, in particular,
% cause the conversion to halt.
%
% Output, real R, the value that was read from the string.
%
% Output, integer LCHAR, the number of characters of S that were used to form R.
%
% Output, integer IERROR, is 0 if no error occurred.
%
s_length = s_len_trim ( s );
ierror = 0;
lchar = -1;
isgn = 1;
rtop = 0.0;
rbot = 1.0;
jsgn = 1;
jtop = 0;
jbot = 1;
ihave = 1;
iterm = 0;

while ( 1 )

lchar = lchar + 1;
c = s(lchar+1);
%
% Blank character.
%
if ( c == ' ' )

if ( ihave == 2 )

elseif ( ihave == 6 || ihave == 7 )
iterm = 1;
elseif ( 1 < ihave )
ihave = 11;
end
%
% Comma.
%
elseif ( c == ',' || c == ';' )

if ( ihave ~= 1 )
iterm = 1;
ihave = 12;
lchar = lchar + 1;
end
%
% Minus sign.
%
elseif ( c == '-' )

if ( ihave == 1 );
ihave = 2;
isgn = -1;
elseif ( ihave == 6 )
ihave = 7;
jsgn = -1;
else
iterm = 1;
end
%
% Plus sign.
%
elseif ( c == '+' )

if ( ihave == 1 )
ihave = 2;
elseif ( ihave == 6 )
ihave = 7;
else
iterm = 1;
end
%
% Decimal point.
%
elseif ( c == '.' )

if ( ihave < 4 )
ihave = 4;
elseif ( 6 <= ihave && ihave <= 8 )
ihave = 9;
else
iterm = 1;
end
%
% Exponent marker.
%
elseif ( ch_eqi ( c, 'E' ) || ch_eqi ( c, 'D' ) )

if ( ihave < 6 )
ihave = 6;
else
iterm = 1;
end
%
% Digit.
%
elseif ( ihave < 11 && ch_is_digit ( c ) )

if ( ihave <= 2 )
ihave = 3;
elseif ( ihave == 4 )
ihave = 5;
elseif ( ihave == 6 || ihave == 7 )
ihave = 8;
elseif ( ihave == 9 )
ihave = 10;
end

d = ch_to_digit ( c );

if ( ihave == 3 )
rtop = 10.0 * rtop + d;
elseif ( ihave == 5 )
rtop = 10.0 * rtop + d;
rbot = 10.0 * rbot;
elseif ( ihave == 8 )
jtop = 10 * jtop + d;
elseif ( ihave == 10 )
jtop = 10 * jtop + d;
jbot = 10 * jbot;
end
%
% Anything else is regarded as a terminator.
%
else
iterm = 1;
end
%
% If we haven't seen a terminator, and we haven't examined the
% entire string, go get the next character.
%
if ( iterm == 1 || s_length <= lchar + 1 )
break;
end

end
%
% If we haven't seen a terminator, and we have examined the
% entire string, then we're done, and LCHAR is equal to S_LENGTH.
%
if ( iterm ~= 1 && lchar + 1 == s_length )
lchar = s_length;
end
%
% Number seems to have terminated. Have we got a legal number?
% Not if we terminated in states 1, 2, 6 or 7!
%
if ( ihave == 1 || ihave == 2 || ihave == 6 || ihave == 7 )
fprintf ( 1, '\n' );
fprintf ( 1, 'S_TO_R8 - Fatal error!\n' );
fprintf ( 1, ' IHAVE = %d\n', ihave );
error ( 'S_TO_R8 - Fatal error!' );
end
%
% Number seems OK. Form it.
%
if ( jtop == 0 )
rexp = 1.0;
else

if ( jbot == 1 )
rexp = 10.0^( jsgn * jtop );
else
rexp = jsgn * jtop;
rexp = rexp / jbot;
rexp = 10.0^rexp;
end

end

r = isgn * rexp * rtop / rbot;

return
end
function [ word, done ] = word_next_read ( s, done )

%*****************************************************************************80
%
%% WORD_NEXT_READ "reads" words from a string, one at a time.
%
% Special cases:
%
% The following characters are considered to be a single word,
% whether surrounded by spaces or not:
%
% " ( ) { } [ ]
%
% Also, if there is a trailing comma on the word, it is stripped off.
% This is to facilitate the reading of lists.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Parameters:
%
% Input, string S, a string, presumably containing words
% separated by spaces.
%
% Input, logical DONE.
% TRUE, if we are beginning a new string;
% FALSE, if we are continuing to process the current string.
%
% Output, string WORD.
% If DONE is FALSE, then WORD contains the "next" word read.
% If DONE is TRUE, then WORD is blank, because there was no more to read.
%
% Output, logical DONE.
% FALSE if another word was read,
% TRUE if no more words could be read.
%
persistent lenc;
persistent next;

tab = char ( 9 );
%
% We "remember" LENC and NEXT from the previous call.
%
% An input value of DONE = TRUE signals a new line of text to examine.
%
if ( done )

next = 1;
done = 0;
lenc = s_len_trim ( s );

if ( lenc <= 0 )
done = 1;
word = ' ';
return
end

end
%
% Beginning at index NEXT, search the string for the next nonblank,
% which signals the beginning of a word.
%
ilo = next;
%
% ...S(NEXT:) is blank. Return with WORD = ' ' and DONE = TRUE.
%
while ( 1 )

if ( lenc < ilo )
word = ' ';
done = 1;
next = lenc + 1;
return
end
%
% If the current character is blank, skip to the next one.
%
if ( s(ilo) ~= ' ' && s(ilo) ~= tab )
break
end

ilo = ilo + 1;

end
%
% ILO is the index of the next nonblank character in the string.
%
% If this initial nonblank is a special character,
% then that's the whole word as far as we're concerned,
% so return immediately.
%
if ( s(ilo) == '"' || ...
s(ilo) == '(' || ...
s(ilo) == ')' || ...
s(ilo) == '{' || ...
s(ilo) == '}' || ...
s(ilo) == '[' || ...
s(ilo) == ']' )

word = s(ilo);
next = ilo + 1;
return

end
%
% Now search for the last contiguous character that is not a
% blank, TAB, or special character.
%
next = ilo + 1;

while ( next <= lenc )

if ( s(next) == ' ' )
break;
elseif ( s(next) == tab )
break;
elseif ( s(next) == '"' )
break;
elseif ( s(next) == '(' )
break;
elseif ( s(next) == ')' )
break;
elseif ( s(next) == '{' )
break;
elseif ( s(next) == '}' )
break;
elseif ( s(next) == '[' )
break;
elseif ( s(next) == ']' )
break;
end

next = next + 1;

end

if ( s(next-1) == ',' )
word = s(ilo:next-2);
else
word = s(ilo:next-1);
end

return
end
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
function objPlot(v, f, name)
%STLPLOT is an easy way to plot an STL object
%V is the Nx3 array of vertices
%F is the Mx3 array of faces
%NAME is the name of the object, that will be displayed as a title

figure;
object.vertices = v;
size(object.vertices)
object.faces = f;
size(object.faces)

patch(object,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);

% Add a camera light, and tone down the specular highlighting
camlight('headlight');
material('dull');

% Fix the axes scaling, and set a nice view angle
axis('image');
view([-135 35]);
grid on;
title(name);
1
2
3
4
5
[n,f] = objread('zhangzifeng1_obj.obj');
%记得需要转置
n = n';
f = f';
objPlot(n,f,'mymodel');

example