Modified lines:  7, 9, 21, 40
Added line:  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, 91, 92, 93, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 134, 135
Removed line:  None
Generated by diff2html.pl
© Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007
diff2html.pl is licensed under the GNU GPL.

  ../10-01/10-01-ReadModel.c     10-02-ReadModel.c
  50 lines
1656 bytes
Last modified : Mon Nov 19 19:12:51 2012

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

Generated by diff2html.pl on Mon Nov 19 19:24:05 2012
Command-line:
/home/ubuntu/scripts/diff2html_utf.pl ../10-01/10-01-ReadModel.c 10-02-ReadModel.c