開発メモその2
あらかじめバイナリにしておく? [サポート事例]
Posted Date:2009/08/28(Fri) 11:05
AVS/Expressのフィールドファイルフォーマットは
データとしてアスキーとバイナリどっちも指定できますが
読み込み速度は、バイナリのほうが圧倒的に速くなります。
ソルバーからの出力をあらかじめバイナリにすることが
できればいいのですが、そうでないばあい変換用のプログラム
を作った方がいいのかどうか?変換するのも時間がかかるし
そのまま読み込んだ方が手間でない場合もあります。
データが時系列で手法を変えてアニメーションを作るとか
する場合は、もちろん変換してほうがいいですが、これも
やっぱりデータサイズなどによってはそのままでもいいかも
しれません。
カラムに並んだアスキーデータをバイナリに変換する
サンプルがあったので一応載せておきます。
エラー処理とかほとんどしてないですけど、、、
データファイル ascii.txt。
------------------------------------
value X Y Z
0.02 0.0 0.0 0.0
0.04 5.0 2.0 0.0
0.06 1.0 0.0 0.0
0.08 4.0 4.0 1.0
0.10 3.0 3.0 2.0
0.12 2.0 1.0 2.5
0.14 2.0 4.0 2.5
0.16 1.0 4.0 3.0
0.18 3.0 1.0 3.2
0.20 5.0 4.0 3.5
0.22 3.0 4.0 3.8
0.24 0.0 3.0 4.1
0.26 3.0 0.0 4.3
0.28 1.0 4.0 4.3
0.30 0.0 3.0 4.8
---------------------------------------
変換プログラム ascii_to_binary.c
---------------------------------------
/* ab.c Ascii Binary 座標値 変換*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <string.h>
/* 区切り文字列 */
#define FLDHDRDELIMS " \t=\n\r,"
/* 点の最大個数 */
#define POINTS_MAX 1000
int
main (int argc, char **argv)
{
FILE *fp_in = NULL;
FILE *fp_out = NULL;
char out_filename[512];
char fld_filename[512];
char buffer[1024];
/* 点の数 */
int points_num = 0;
/* valueと座標値の配列*/
float value[POINTS_MAX];
float coordx[POINTS_MAX];
float coordy[POINTS_MAX];
float coordz[POINTS_MAX];
size_t size = 0;
size_t write_size = 0;
char *ptr = NULL;
/* 引数チェック */
if( argc < 2 )
{
printf("command input_filename\n");
return(1);
}
/* 入力ファイルを開く */
if ((fp_in = fopen(argv[1], "r")) == NULL)
{
printf("Cannot open input file %s\n",argv[1]);
return( 0 );
}
/* 1行目を読み飛ばす */
fgets( buffer , 1023 , fp_in);
/* value x y zを読み込む*/
points_num = 0;
while( NULL != fgets( buffer , 1023 , fp_in) )
{
/* value 読み込み */
ptr = strtok(buffer, FLDHDRDELIMS);
if( ptr==NULL ){ break;}
value[points_num] = atof( ptr );
/* coord X読み込み */
ptr = strtok(NULL, FLDHDRDELIMS);
coordx[points_num] = atof( ptr );
/* coord Y読み込み */
ptr = strtok(NULL, FLDHDRDELIMS);
coordy[points_num] = atof( ptr );
/* coord Z読み込み */
ptr = strtok(NULL, FLDHDRDELIMS);
coordz[points_num] = atof( ptr );
/* デバッグ用 */
/*
printf("[%d]={%f,%f,%f,%f}\n"
,points_num
,value[points_num]
,coordx[points_num]
,coordy[points_num]
,coordz[points_num]
);
*/
points_num++;
}
fclose(fp_in);
/* バイナリデータとfieldヘッダを出力する。 */
/* バイナリファイルを開く */
sprintf( out_filename,"%s.bin",argv[1]);
if ((fp_out = fopen( out_filename, "wb")) == NULL)
{
printf("Cannot openoutput file %s\n",out_filename);
return( 0 );
}
size = (size_t)points_num;
/* Valueを出力 */
write_size = fwrite( value , sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write value array\n");
}
/* X座標値を書き込み */
write_size = fwrite( coordx, sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write coordx array\n");
}
/* Y座標値を書き込み */
write_size = fwrite( coordy, sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write coordy array\n");
}
/* Z座標値を書き込み */
write_size = fwrite( coordz, sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write coordz array\n");
}
fclose( fp_out );
/* フィールドヘッダを出力する */
sprintf( fld_filename,"%s.fld",argv[1]);
if ((fp_out = fopen( fld_filename, "w")) == NULL)
{
printf("Cannot open output file %s\n",fld_filename);
return( 0 );
}
fprintf(fp_out,"# AVS field file\n");
fprintf(fp_out,"ndim=1\n");
fprintf(fp_out,"dim1=%d\n",points_num);
fprintf(fp_out,"nspace=3\n");
fprintf(fp_out,"veclen=1\n");
fprintf(fp_out,"data=float\n");
fprintf(fp_out,"field=irregular\n");
fprintf(fp_out,"variable 1 file=./%s filetype=binary offset=0\n",out_filename);
fprintf(fp_out
,"coord 1 file=./%s filetype=binary skip=%d\n",out_filename
,points_num*sizeof(float)
);
fprintf(fp_out
,"coord 2 file=./%s filetype=binary skip=%d\n",out_filename
,points_num*sizeof(float)*2
);
fprintf(fp_out
,"coord 3 file=./%s filetype=binary skip=%d\n",out_filename
,points_num*sizeof(float)*3
);
fclose(fp_out);
return(1);
}
---------------------------------------------------
ascii_to_binary.cをコンパイルして
axcii_to_binary.exe ascii.txtと実行すると
バイナリファイルとフィールドヘッダができます。
データとしてアスキーとバイナリどっちも指定できますが
読み込み速度は、バイナリのほうが圧倒的に速くなります。
ソルバーからの出力をあらかじめバイナリにすることが
できればいいのですが、そうでないばあい変換用のプログラム
を作った方がいいのかどうか?変換するのも時間がかかるし
そのまま読み込んだ方が手間でない場合もあります。
データが時系列で手法を変えてアニメーションを作るとか
する場合は、もちろん変換してほうがいいですが、これも
やっぱりデータサイズなどによってはそのままでもいいかも
しれません。
カラムに並んだアスキーデータをバイナリに変換する
サンプルがあったので一応載せておきます。
エラー処理とかほとんどしてないですけど、、、
データファイル ascii.txt。
------------------------------------
value X Y Z
0.02 0.0 0.0 0.0
0.04 5.0 2.0 0.0
0.06 1.0 0.0 0.0
0.08 4.0 4.0 1.0
0.10 3.0 3.0 2.0
0.12 2.0 1.0 2.5
0.14 2.0 4.0 2.5
0.16 1.0 4.0 3.0
0.18 3.0 1.0 3.2
0.20 5.0 4.0 3.5
0.22 3.0 4.0 3.8
0.24 0.0 3.0 4.1
0.26 3.0 0.0 4.3
0.28 1.0 4.0 4.3
0.30 0.0 3.0 4.8
---------------------------------------
変換プログラム ascii_to_binary.c
---------------------------------------
/* ab.c Ascii Binary 座標値 変換*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <string.h>
/* 区切り文字列 */
#define FLDHDRDELIMS " \t=\n\r,"
/* 点の最大個数 */
#define POINTS_MAX 1000
int
main (int argc, char **argv)
{
FILE *fp_in = NULL;
FILE *fp_out = NULL;
char out_filename[512];
char fld_filename[512];
char buffer[1024];
/* 点の数 */
int points_num = 0;
/* valueと座標値の配列*/
float value[POINTS_MAX];
float coordx[POINTS_MAX];
float coordy[POINTS_MAX];
float coordz[POINTS_MAX];
size_t size = 0;
size_t write_size = 0;
char *ptr = NULL;
/* 引数チェック */
if( argc < 2 )
{
printf("command input_filename\n");
return(1);
}
/* 入力ファイルを開く */
if ((fp_in = fopen(argv[1], "r")) == NULL)
{
printf("Cannot open input file %s\n",argv[1]);
return( 0 );
}
/* 1行目を読み飛ばす */
fgets( buffer , 1023 , fp_in);
/* value x y zを読み込む*/
points_num = 0;
while( NULL != fgets( buffer , 1023 , fp_in) )
{
/* value 読み込み */
ptr = strtok(buffer, FLDHDRDELIMS);
if( ptr==NULL ){ break;}
value[points_num] = atof( ptr );
/* coord X読み込み */
ptr = strtok(NULL, FLDHDRDELIMS);
coordx[points_num] = atof( ptr );
/* coord Y読み込み */
ptr = strtok(NULL, FLDHDRDELIMS);
coordy[points_num] = atof( ptr );
/* coord Z読み込み */
ptr = strtok(NULL, FLDHDRDELIMS);
coordz[points_num] = atof( ptr );
/* デバッグ用 */
/*
printf("[%d]={%f,%f,%f,%f}\n"
,points_num
,value[points_num]
,coordx[points_num]
,coordy[points_num]
,coordz[points_num]
);
*/
points_num++;
}
fclose(fp_in);
/* バイナリデータとfieldヘッダを出力する。 */
/* バイナリファイルを開く */
sprintf( out_filename,"%s.bin",argv[1]);
if ((fp_out = fopen( out_filename, "wb")) == NULL)
{
printf("Cannot openoutput file %s\n",out_filename);
return( 0 );
}
size = (size_t)points_num;
/* Valueを出力 */
write_size = fwrite( value , sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write value array\n");
}
/* X座標値を書き込み */
write_size = fwrite( coordx, sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write coordx array\n");
}
/* Y座標値を書き込み */
write_size = fwrite( coordy, sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write coordy array\n");
}
/* Z座標値を書き込み */
write_size = fwrite( coordz, sizeof(float), size, fp_out);
if(write_size != size )
{
printf("can't write coordz array\n");
}
fclose( fp_out );
/* フィールドヘッダを出力する */
sprintf( fld_filename,"%s.fld",argv[1]);
if ((fp_out = fopen( fld_filename, "w")) == NULL)
{
printf("Cannot open output file %s\n",fld_filename);
return( 0 );
}
fprintf(fp_out,"# AVS field file\n");
fprintf(fp_out,"ndim=1\n");
fprintf(fp_out,"dim1=%d\n",points_num);
fprintf(fp_out,"nspace=3\n");
fprintf(fp_out,"veclen=1\n");
fprintf(fp_out,"data=float\n");
fprintf(fp_out,"field=irregular\n");
fprintf(fp_out,"variable 1 file=./%s filetype=binary offset=0\n",out_filename);
fprintf(fp_out
,"coord 1 file=./%s filetype=binary skip=%d\n",out_filename
,points_num*sizeof(float)
);
fprintf(fp_out
,"coord 2 file=./%s filetype=binary skip=%d\n",out_filename
,points_num*sizeof(float)*2
);
fprintf(fp_out
,"coord 3 file=./%s filetype=binary skip=%d\n",out_filename
,points_num*sizeof(float)*3
);
fclose(fp_out);
return(1);
}
---------------------------------------------------
ascii_to_binary.cをコンパイルして
axcii_to_binary.exe ascii.txtと実行すると
バイナリファイルとフィールドヘッダができます。

Blog Theme
Blog Latest Entries
- カラーマップの凡例表示 (05/23 14:34)
- データの一部だけ可視化する (04/27 13:19)
- 文字列ラベルを表示する (10/20 16:25)
- マーカーの形状をいろいろ変えてみる (09/24 18:37)
- reset_minmaxモジュール (06/04 19:14)
- 初期フォルダを変えてみる。 (12/11 11:06)
- 表示/非表示の切り替え (11/04 16:49)
- テスト用のサンプルデータを作る (10/15 12:55)
- テクスチャ画像を少し編集してみる (10/06 10:11)
- コーディングフォントは? (09/25 18:01)
- 操作説明資料を作ってみる (09/16 18:48)
- 等数値ラインのレベル値指定 (09/10 18:25)
- AVS/Expressの初期サイズを調整してみる (09/04 17:58)
- あらかじめバイナリにしておく? (08/28 11:05)
- AVSのX形式画像? (08/05 17:49)
- thresholdとclampモジュール (07/31 18:02)
- 表示方法を変えてみる (07/09 17:01)
- 必要な要素だけ表示してみる (06/25 16:17)
- フォントを指定する (06/16 11:42)
- 要素の縮小表示 (06/08 14:30)
このページへのアクセス数
Total:125PV
| 今日 | : | 1 |
|
| 1日前 | : | 0 |
|
| 2日前 | : | 0 |
|
| 3日前 | : | 0 |
|
| 4日前 | : | 1 |
|
| 5日前 | : | 1 |
|
| 6日前 | : | 0 |
|
| 7日前 | : | 0 |
|
| 8日前 | : | 0 |
|
| 9日前 | : | 0 |
|
