| 
 | Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. | 
| ../10-02/10-02-ReadModel.c | 11-02-ReadModel.c | |||
|---|---|---|---|---|
| 137 lines 4755 bytes Last modified : Mon Nov 19 19:12:10 2012 | 137 lines 4759 bytes Last modified : Mon Nov 19 12:28:59 2012 | |||
| 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 | // 計算機序論2・実習 (筑波大学工学システム学類) | 4 | // 計算機序論2・実習 (筑波大学工学システム学類) | |
| 5 | 5 | |||
| 6 | // 2012/11/19a kameda[at]iit.tsukuba.ac.jp | 6 | // 2012/11/19a kameda[at]iit.tsukuba.ac.jp | |
| 7 | // 10.02. モデルの読込 | 7 | // 11.02. プログラム統合 | |
| 8 | 8 | |||
| 9 | #include "ic2-ModelHeaders.h" | 9 | #include "ic2-CommonHeaders.h" | |
| 10 | 10 | |||
| 11 | // *********************************************************************** | 11 | // *********************************************************************** | |
| 12 | // ReadModel ************************************************************* | 12 | // ReadModel ************************************************************* | |
| 13 | 13 | |||
| 14 | // +++-------------------------------------------------- | 14 | // +++-------------------------------------------------- | |
| 15 | // 3DCGモデルを記述したファイルを読み込む | 15 | // 3DCGモデルを記述したファイルを読み込む | |
| 16 | // +++-------------------------------------------------- | 16 | // +++-------------------------------------------------- | |
| 17 | 17 | |||
| 18 | // ----------------------------------------------------- | 18 | // ----------------------------------------------------- | |
| 19 | // 三角形パッチ構造体(ic2PATCH)リストの表示 | 19 | // 三角形パッチ構造体(ic2PATCH)リストの表示 | |
| 20 | // ----------------------------------------------------- | 20 | // ----------------------------------------------------- | |
| 21 | int ic2_PrintPATCHList (struct ic2PATCH *firstpatchptr) { | 21 | int ic2_PrintPATCHList (struct ic2PATCH *firstpatchptr) { | |
| 22 | struct ic2PATCH *p = NULL; | 22 | struct ic2PATCH *p = NULL; | |
| 23 | int np = 0; | 23 | int np = 0; | |
| 24 | 24 | |||
| 25 | for (p = firstpatchptr; p != NULL; p = p->next) { | 25 | for (p = firstpatchptr; p != NULL; p = p->next) { | |
| 26 | printf("PATCH %2d: " | 26 | printf("PATCH %2d: " | |
| 27 | "(%5.2f, %5.2f, %5.2f), (%5.2f, %5.2f, %5.2f), (%5.2f, %5.2f, %5.2f), " | 27 | "(%5.2f, %5.2f, %5.2f), (%5.2f, %5.2f, %5.2f), (%5.2f, %5.2f, %5.2f), " | |
| 28 | "rgb=[%4.2f, %4.2f, %4.2f] \n", | 28 | "rgb=[%4.2f, %4.2f, %4.2f] \n", | |
| 29 | np, | 29 | np, | |
| 30 | p->s.x, p->s.y, p->s.z, | 30 | p->s.x, p->s.y, p->s.z, | |
| 31 | p->t.x, p->t.y, p->t.z, | 31 | p->t.x, p->t.y, p->t.z, | |
| 32 | p->u.x, p->u.y, p->u.z, | 32 | p->u.x, p->u.y, p->u.z, | |
| 33 | p->c.r, p->c.g, p->c.b); | 33 | p->c.r, p->c.g, p->c.b); | |
| 34 | np++; | 34 | np++; | |
| 35 | } | 35 | } | |
| 36 | 36 | |||
| 37 | return np; | 37 | return np; | |
| 38 | } | 38 | } | |
| 39 | 39 | |||
| 40 | 40 | |||
| 41 | // +---------------------------------------------------- | 41 | // +---------------------------------------------------- | |
| 42 | // 三角形パッチ1つ分のメモリ確保と読込 | 42 | // 三角形パッチ1つ分のメモリ確保と読込 | |
| 43 | // +---------------------------------------------------- | 43 | // +---------------------------------------------------- | |
| 44 | static int ic2_InsertPATCH (struct ic2PATCH **firstpatchptr, char *onelinedata) { | 44 | static int ic2_InsertPATCH (struct ic2PATCH **firstpatchptr, char *onelinedata) { | |
| 45 | // (1) 文字列へのポインタは存在するか | 45 | // (1) 文字列へのポインタは存在するか | |
| 46 | // (2) メモリ確保/下請け | 46 | // (2) メモリ確保/下請け | |
| 47 | // (3) 値の読み込み | 47 | // (3) 値の読み込み | |
| 48 | // (4) ic2PATCHリスト構造への組み込み | 48 | // (4) ic2PATCHリスト構造への組み込み | |
| 49 | struct ic2PATCH *newpatch = NULL; // 三角形パッチ構造体ヘのポインタ | 49 | struct ic2PATCH *newpatch = NULL; // 三角形パッチ構造体ヘのポインタ | |
| 50 | int number_of_element = 0; // 読み込めた項目数 | 50 | int number_of_element = 0; // 読み込めた項目数 | |
| 51 | 51 | |||
| 52 | // (1) 文字列へのポインタは存在するか | 52 | // (1) 文字列へのポインタは存在するか | |
| 53 | if (onelinedata == NULL) return -1; | 53 | if (onelinedata == NULL) return -1; | |
| 54 | 54 | |||
| 55 | // (2) メモリ確保 | 55 | // (2) メモリ確保 | |
| 56 | newpatch = (struct ic2PATCH *)calloc(1, sizeof(struct ic2PATCH)); | 56 | newpatch = (struct ic2PATCH *)calloc(1, sizeof(struct ic2PATCH)); | |
| 57 | if (newpatch == NULL) { | 57 | if (newpatch == NULL) { | |
| 58 | printf("ic2_InsertPATCH: Memory allocation failed.\n"); | 58 | printf("ic2_InsertPATCH: Memory allocation failed.\n"); | |
| 59 | return -2; | 59 | return -2; | |
| 60 | } | 60 | } | |
| 61 | 61 | |||
| 62 | // (3) 値の読み込み | 62 | // (3) 値の読み込み | |
| 63 | number_of_element = | 63 | number_of_element = | |
| 64 | sscanf(onelinedata, "%f %f %f %f %f %f %f %f %f %f %f %f", | 64 | sscanf(onelinedata, "%f %f %f %f %f %f %f %f %f %f %f %f", | |
| 65 | &newpatch->s.x, &newpatch->s.y, &newpatch->s.z, | 65 | &newpatch->s.x, &newpatch->s.y, &newpatch->s.z, | |
| 66 | &newpatch->t.x, &newpatch->t.y, &newpatch->t.z, | 66 | &newpatch->t.x, &newpatch->t.y, &newpatch->t.z, | |
| 67 | &newpatch->u.x, &newpatch->u.y, &newpatch->u.z, | 67 | &newpatch->u.x, &newpatch->u.y, &newpatch->u.z, | |
| 68 | &newpatch->c.r, &newpatch->c.g, &newpatch->c.b); | 68 | &newpatch->c.r, &newpatch->c.g, &newpatch->c.b); | |
| 69 | if (number_of_element != 12) { | 69 | if (number_of_element != 12) { | |
| 70 | printf("ic2_InsertPATCH: format error (%d elements found)\n", number_of_element); | 70 | printf("ic2_InsertPATCH: format error (%d elements found)\n", number_of_element); | |
| 71 | printf("ic2_InsertPATCH: \"%s\"\n", onelinedata); | 71 | printf("ic2_InsertPATCH: \"%s\"\n", onelinedata); | |
| 72 | free(newpatch); | 72 | free(newpatch); | |
| 73 | return -3; | 73 | return -3; | |
| 74 | } | 74 | } | |
| 75 | 75 | |||
| 76 | // (4) パッチに関する追加属性の事前計算 | 76 | // (4) パッチに関する追加属性の事前計算 | |
| 77 | 77 | |||
| 78 | // (5) ic2PATCHリスト構造への組み込み | 78 | // (5) ic2PATCHリスト構造への組み込み | |
| 79 | // *newpatch を 三角形パッチ集合の先頭に挿入 | 79 | // *newpatch を 三角形パッチ集合の先頭に挿入 | |
| 80 | newpatch->next = *firstpatchptr; | 80 | newpatch->next = *firstpatchptr; | |
| 81 | *firstpatchptr = newpatch; | 81 | *firstpatchptr = newpatch; | |
| 82 | return 0; | 82 | return 0; | |
| 83 | } | 83 | } | |
| 84 | 84 | |||
| 85 | // +---------------------------------------------------- | 85 | // +---------------------------------------------------- | |
| 86 | // 3DCGモデルファイルのオープン・読込・クローズ | 86 | // 3DCGモデルファイルのオープン・読込・クローズ | |
| 87 | // +---------------------------------------------------- | 87 | // +---------------------------------------------------- | |
| 88 | int ic2_ReadModel(char *filename, struct ic2PATCH **firstpatchptr) { | 88 | int ic2_ReadModel(char *filename, struct ic2PATCH **firstpatchptr) { | |
| 89 | FILE *filetoopen = NULL; // FILE構造体へのポインタ | 89 | FILE *filetoopen = NULL; // FILE構造体へのポインタ | |
| 90 | int patchnumber = 0; // パッチ数 | 90 | int patchnumber = 0; // パッチ数 | |
| 91 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 91 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
| 92 | char firstword[256]; // コメント行かどうかの判定用 | 92 | char firstword[256]; // コメント行かどうかの判定用 | |
| 93 | int linenumber = 0; // ファイル中の行番号 | 93 | int linenumber = 0; // ファイル中の行番号 | |
| 94 | 94 | |||
| 95 | // Foolproof | 95 | // Foolproof | |
| 96 | if (filename == NULL) { | 96 | if (filename == NULL) { | |
| 97 | printf("Error: You need to specify file name to open.\n"); | 97 | printf("Error: You need to specify file name to open.\n"); | |
| 98 | return -1; | 98 | return -1; | |
| 99 | } | 99 | } | |
| 100 | 100 | |||
| 101 | // ファイルのオープン | 101 | // ファイルのオープン | |
| 102 | filetoopen = fopen(filename, "r"); | 102 | filetoopen = fopen(filename, "r"); | |
| 103 | if (filetoopen == NULL) { | 103 | if (filetoopen == NULL) { | |
| 104 | printf("Error: Failed to open/read \"%s\".\n", filename); | 104 | printf("Error: Failed to open/read \"%s\".\n", filename); | |
| 105 | return -2; | 105 | return -2; | |
| 106 | } | 106 | } | |
| 107 | // ファイル名を表示 | 107 | // ファイル名を表示 | |
| 108 | printf("Reading model from \"%s\"\n", filename); | 108 | printf("Reading model from \"%s\"\n", filename); | |
| 109 | 109 | |||
| 110 | // 1行ずつ読込して登録 | 110 | // 1行ずつ読込して登録 | |
| 111 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | 111 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | |
| 112 | linenumber++; | 112 | linenumber++; | |
| 113 | 113 | |||
| 114 | // もし行内に1文字もなければ(1単語もなければ)次行へ | 114 | // もし行内に1文字もなければ(1単語もなければ)次行へ | |
| 115 | if (sscanf(oneline, "%256s", firstword) < 1) | 115 | if (sscanf(oneline, "%256s", firstword) < 1) | |
| 116 | continue; | 116 | continue; | |
| 117 | // もし先頭が#で始まっていれば次行へ | 117 | // もし先頭が#で始まっていれば次行へ | |
| 118 | if (firstword[0] == '#') | 118 | if (firstword[0] == '#') | |
| 119 | continue; | 119 | continue; | |
| 120 | if (ic2_InsertPATCH(firstpatchptr, oneline)) { | 120 | if (ic2_InsertPATCH(firstpatchptr, oneline)) { | |
| 121 | printf("Model reading is interrupted.\n"); | 121 | printf("Model reading is interrupted.\n"); | |
| 122 | break; | 122 | break; | |
| 123 | } | 123 | } | |
| 124 | patchnumber++; | 124 | patchnumber++; | |
| 125 | } | 125 | } | |
| 126 | 126 | |||
| 127 | // ファイルのクローズ | 127 | // ファイルのクローズ | |
| 128 | if (fclose(filetoopen) != 0) { | 128 | if (fclose(filetoopen) != 0) { | |
| 129 | printf("Error: Failed to close \"%s\".\n", filename); | 129 | printf("Error: Failed to close \"%s\".\n", filename); | |
| 130 | // error, but we get data anyway... | 130 | // error, but we get data anyway... | |
| 131 | } | 131 | } | |
| 132 | 132 | |||
| 133 | printf("Finish reading the model (%d patches).\n", patchnumber); | 133 | printf("Finish reading the model (%d patches).\n", patchnumber); | |
| 134 | ic2_PrintPATCHList (*firstpatchptr); | 134 | ic2_PrintPATCHList (*firstpatchptr); | |
| 135 | 135 | |||
| 136 | return patchnumber; | 136 | return patchnumber; | |
| 137 | } | 137 | } |