|
Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. |
../109-3/109-3-ReadLine.c | 109-4-ReadModel.c | |||
---|---|---|---|---|
78 lines 2197 bytes Last modified : Mon Nov 7 11:16:16 2011 |
191 lines 6221 bytes Last modified : Mon Nov 7 22:21:22 2011 |
|||
1 | // Keisanki Joron 2 (Introduction to Computing II) | 1 | // Keisanki Joron 2 (Introduction to Computing II) | |
2 | // Dept. of Engineering Systems, University of Tsukuba | 2 | // Dept. of Engineering Systems, University of Tsukuba | |
3 | // [UTF-8 / Unix] | 3 | // [UTF-8 / Unix] | |
4 | 4 | |||
5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | 5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | |
6 | // 9.3 行毎の読み込み | 6 | // 9.4 モデルの読み込み | |
7 | 7 | |||
8 | #include <stdio.h> | 8 | #include <stdio.h> | |
9 | #include <stdlib.h> // calloc() | |||
10 | ||||
11 | // +---------------------------------------------------- | |||
12 | // 1つの点のための構造体 | |||
13 | // +---------------------------------------------------- | |||
14 | struct ic2POINT { | |||
15 | float x; | |||
16 | float y; | |||
17 | float z; | |||
18 | }; | |||
19 | ||||
20 | // +---------------------------------------------------- | |||
21 | // 1つの色のための構造体 | |||
22 | // +---------------------------------------------------- | |||
23 | struct ic2COLOR { | |||
24 | float r; | |||
25 | float g; | |||
26 | float b; | |||
27 | }; | |||
28 | ||||
29 | // +---------------------------------------------------- | |||
30 | // 1つの三角形パッチのための構造体 | |||
31 | // 次の三角形パッチへのポインタを持つ。 | |||
32 | // v(st) × v(su) [外積]がこの面の法線ベクトルを成す(右ネジ式) | |||
33 | // +---------------------------------------------------- | |||
34 | struct ic2PATCH { | |||
35 | struct ic2POINT s; // 頂点s | |||
36 | struct ic2POINT t; // 頂点t | |||
37 | struct ic2POINT u; // 頂点u | |||
38 | struct ic2COLOR c; // 色の強度 (通常は0.0 - 1.0) | |||
39 | struct ic2PATCH *next; // 次の三角形パッチへのポインタ | |||
40 | }; | |||
41 | ||||
42 | // +++-------------------------------------------------- | |||
43 | // 新しい三角形パッチ構造体(ic2PATCH)のメモリ確保と初期化 | |||
44 | // +++-------------------------------------------------- | |||
45 | static struct ic2PATCH *ic2_NewPATCH (void) { | |||
46 | struct ic2PATCH *newpatch = NULL; | |||
47 | ||||
48 | newpatch = (struct ic2PATCH *)calloc(1, sizeof(struct ic2PATCH)); | |||
49 | return (newpatch); | |||
50 | } | |||
51 | ||||
52 | // +++-------------------------------------------------- | |||
53 | // 三角形パッチ構造体(ic2PATCH)1つ分の読み込み | |||
54 | // +++-------------------------------------------------- | |||
55 | static int ic2_InsertPATCH (struct ic2PATCH **firstpatchptr, char *onelinedata) { | |||
56 | // (1) 文字列へのポインタは存在するか | |||
57 | // (2) メモリ確保/下請け | |||
58 | // (3) 値の読み込み | |||
59 | // (4) ic2PATCHリスト構造への組み込み | |||
60 | struct ic2PATCH *newpatch = NULL; // 三角形パッチ構造体ヘのポインタ | |||
61 | int number_of_element = 0; // 読み込めた項目数 | |||
62 | ||||
63 | // (1) 文字列へのポインタは存在するか | |||
64 | if (onelinedata == NULL) return 1; | |||
65 | ||||
66 | // (2) メモリ確保/下請け | |||
67 | if ((newpatch = ic2_NewPATCH()) == NULL) { | |||
68 | printf("ic2_InsertPATCH: Memory allocation failed.\n"); | |||
69 | return 2; | |||
70 | } | |||
71 | ||||
72 | // (3) 値の読み込み | |||
73 | number_of_element = | |||
74 | sscanf(onelinedata, "%f %f %f %f %f %f %f %f %f %f %f %f", | |||
75 | &newpatch->s.x, &newpatch->s.y, &newpatch->s.z, | |||
76 | &newpatch->t.x, &newpatch->t.y, &newpatch->t.z, | |||
77 | &newpatch->u.x, &newpatch->u.y, &newpatch->u.z, | |||
78 | &newpatch->c.r, &newpatch->c.g, &newpatch->c.b); | |||
79 | if (number_of_element != 12) { | |||
80 | printf("ic2_InsertPATCH: format error (%d elements found)\n", number_of_element); | |||
81 | printf("ic2_InsertPATCH: \"%s\"\n", onelinedata); | |||
82 | free(newpatch); | |||
83 | return 3; | |||
84 | } | |||
85 | ||||
86 | // (4) ic2PATCHリスト構造への組み込み | |||
87 | // *newpatch を 三角形パッチ集合の先頭に挿入 | |||
88 | newpatch->next = *firstpatchptr; | |||
89 | *firstpatchptr = newpatch; | |||
90 | return 0; | |||
91 | } | |||
9 | 92 | |||
10 | // +---------------------------------------------------- | 93 | // +---------------------------------------------------- | |
11 | // モデルのファイルからの読込 | 94 | // モデルのファイルからの読込 | |
12 | // +---------------------------------------------------- | 95 | // +---------------------------------------------------- | |
13 | int ic2_ReadModel(char *filename) { | 96 | // 返値:負 ... エラー | |
97 | // 返値:0ないし正値 ... 読み込みに成功したパッチ数 | |||
98 | int ic2_ReadModel(char *filename, struct ic2PATCH **firstpatchptr) { | |||
14 | FILE *filetoopen = NULL; // A pointer to FILE structure | 99 | FILE *filetoopen = NULL; // A pointer to FILE structure | |
15 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 100 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
16 | char firstword[256]; // コメント行かどうかの判定用 | 101 | char firstword[256]; // コメント行かどうかの判定用 | |
17 | int linenumber = 0; // ファイル中の行番号 | 102 | int linenumber = 0; // ファイル中の行番号 | |
18 | int patchnumber = 0; // パッチ数 | 103 | int patchnumber = 0; // パッチ数 | |
19 | 104 | |||
20 | // We need at least one option to indicate a file to open | 105 | // We need at least one option to indicate a file to open | |
21 | if (filename == NULL) { | 106 | if (filename == NULL) { | |
22 | printf("Error: You need to specify a one file to open.\n"); | 107 | printf("Error: You need to specify a one file to open.\n"); | |
23 | return -1; | 108 | return -1; | |
24 | } | 109 | } | |
25 | 110 | |||
26 | // Try to open it | 111 | // Try to open it | |
27 | filetoopen = fopen(filename, "r"); | 112 | filetoopen = fopen(filename, "r"); | |
28 | if (filetoopen == NULL) { | 113 | if (filetoopen == NULL) { | |
29 | printf("Error: Failed to open/read \"%s\".\n", filename); | 114 | printf("Error: Failed to open/read \"%s\".\n", filename); | |
30 | return -2; | 115 | return -2; | |
31 | } | 116 | } | |
32 | printf("Reading model from \"%s\"\n", filename); | 117 | printf("Reading model from \"%s\"\n", filename); | |
33 | 118 | |||
34 | // 1行ずつ読込 | 119 | // 1行ずつ読込 | |
35 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | 120 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | |
36 | linenumber++; | 121 | linenumber++; | |
37 | 122 | |||
38 | // もし行内に1文字もなければ(1単語もなければ)次行へ | 123 | // もし行内に1文字もなければ(1単語もなければ)次行へ | |
39 | if (sscanf(oneline, "%256s", firstword) < 1) | 124 | if (sscanf(oneline, "%256s", firstword) < 1) | |
40 | continue; | 125 | continue; | |
41 | // もし先頭が#で始まっていれば次行へ | 126 | // もし先頭が#で始まっていれば次行へ | |
42 | if (firstword[0] == '#') | 127 | if (firstword[0] == '#') | |
43 | continue; | 128 | continue; | |
44 | // 他のエラートラップ | 129 | // 他のエラートラップ | |
45 | if (0) { | 130 | if (0) { | |
46 | printf("Skip(line=%d): %s\n", linenumber, oneline); | 131 | printf("Skip(line=%d): %s\n", linenumber, oneline); | |
47 | continue; | 132 | continue; | |
48 | } | 133 | } | |
49 | 134 | |||
50 | printf("Valid: %d: \"%s\"\n", linenumber, oneline); | 135 | if (ic2_InsertPATCH(firstpatchptr, oneline)) { | |
51 | patchnumber++; | 136 | printf("Model reading is interrupted.\n"); | |
137 | break; | |||
138 | } | |||
139 | patchnumber++; | |||
52 | } | 140 | } | |
53 | 141 | |||
54 | // And close it | 142 | // And close it | |
55 | if (fclose(filetoopen) != 0) { | 143 | if (fclose(filetoopen) != 0) { | |
56 | printf("Error: Failed to close \"%s\".\n", filename); | 144 | printf("Error: Failed to close \"%s\".\n", filename); | |
57 | // error, but we get data anyway... | 145 | // error, but we get data anyway... | |
58 | } | 146 | } | |
59 | 147 | |||
60 | printf("Finish reading the model (%d patches).\n", patchnumber); | 148 | printf("Finish reading the model (%d patches).\n", patchnumber); | |
61 | return patchnumber; | 149 | return patchnumber; | |
62 | } | 150 | } | |
63 | 151 | |||
64 | int main(int argc, char *argv[]){ | 152 | // ----------------------------------------------------- | |
153 | // 三角形パッチ構造体(ic2PATCH)リストの表示 | |||
154 | // ----------------------------------------------------- | |||
155 | int ic2_PrintPATCHList (struct ic2PATCH *firstpatchptr) { | |||
156 | struct ic2PATCH *p; | |||
157 | int np = 0; | |||
158 | ||||
159 | for (p = firstpatchptr; p != NULL; p = p->next) { | |||
160 | np++; | |||
161 | printf("PATCH: (%g, %g, %g), (%g, %g, %g), (%g, %g, %g), rgb=[%g, %g, %g]\n", | |||
162 | p->s.x, p->s.y, p->s.z, | |||
163 | p->t.x, p->t.y, p->t.z, | |||
164 | p->u.x, p->u.y, p->u.z, | |||
165 | p->c.r, p->c.g, p->c.b); | |||
166 | } | |||
167 | ||||
168 | return np; | |||
169 | } | |||
65 | 170 | |||
171 | // -------------------------------------------------------------- | |||
172 | int main (int argc, char *argv[]) { | |||
173 | struct ic2PATCH *firstpatchptr = NULL; | |||
174 | int numberpatches = 0; | |||
175 | ||||
176 | // model ファイルの読み込み | |||
66 | if (argc <= 1) { | 177 | if (argc <= 1) { | |
67 | printf("Error: no model file is specified.\n"); | 178 | printf("Error: no model file is specified.\n"); | |
68 | return 1; | 179 | return 1; | |
69 | } | 180 | } | |
70 | 181 | numberpatches = ic2_ReadModel(argv[1], &firstpatchptr); | ||
71 | if (ic2_ReadModel(argv[1]) < 0) { | 182 | if (numberpatches < 0) { | |
72 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | 183 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | |
73 | return 1; | 184 | return 1; | |
74 | } | 185 | } | |
186 | printf("Number of Patches in the model = %d \n", numberpatches); | |||
187 | ic2_PrintPATCHList(firstpatchptr); | |||
75 | 188 | |||
76 | return 0; | 189 | return 0; | |
77 | } | 190 | } | |
78 | 191 |