STL ファイル バイナリ形式読み込み
このページには, STL ファイル バイナリ形式読み込みプログラムを載せています.
さらに、次の公開データを使う
- http://www.capture3d.com/products-ATOS-download.html で公開されている atos-beetle-500000_stl.zip をダウンロードして使用
さらに、公開データを用いてSQLite 3 のテーブル stl を生成します。
◆ 作成するSQLite 3 データベース: stldb
CSVファイル生成手順
◆ ソースコードのダウンロードとコンパイル手順
cd /tmp
rm -f readstl.c
wget https://www.kkaneko.jp/rinkou/od/readstl.c
g++ -o a.out readstl.c
◆ 上記の手順でダウンロードが始まるソースコード
#include<stdio.h>
const int BUFSIZE = 10000;
int main(int arcg, char** argv)
{
FILE *fp;
char *fname = argv[1];
unsigned char buf[BUFSIZE];
unsigned int i, size;
unsigned int num;
float nx;
float ny;
float nz;
float x1;
float y1;
float z1;
float x2;
float y2;
float z2;
float x3;
float y3;
float z3;
fp = fopen( fname, "rb" );
if( fp == NULL ){
printf( "%s \n", fname );
return -1;
}
/* header */
size = fread( buf, sizeof( unsigned char ), 80, fp );
/* num */
size = fread( buf, sizeof( unsigned char ), 4, fp );
num = *( (unsigned int*) buf );
printf( "seq, nx, ny, nz, x1, y1, z1, x2, y2, z2, x3, y3, z3\n", nx );
/* body */
i = 1;
while(1) {
size = fread( buf, sizeof( unsigned char ), 50, fp );
if ( size < 50 ) {
break;
}
nx = *( (float*) (&(buf[0])) );
ny = *( (float*) (&(buf[4])) );
nz = *( (float*) (&(buf[8])) );
x1 = *( (float*) (&(buf[12])) );
y1 = *( (float*) (&(buf[16])) );
z1 = *( (float*) (&(buf[20])) );
x2 = *( (float*) (&(buf[24])) );
y2 = *( (float*) (&(buf[28])) );
z2 = *( (float*) (&(buf[32])) );
x3 = *( (float*) (&(buf[36])) );
y3 = *( (float*) (&(buf[40])) );
z3 = *( (float*) (&(buf[44])) );
printf( "%d, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n", i, nx, ny, nz, x1, y1, z1, x2, y2, z2, x3, y3, z3 );
i++;
}
/* printf( "%d\n", num ); */
fclose( fp );
return 0;
}
◆ 実行手順と実行結果の例
./a.out beetle_500000.stl > beetle_500000.stl.csv

◆ 確認表示
R プログラム (scatterplot3d を使用)
X <- read.table("/tmp/beetle_500000.stl.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE);
library(scatterplot3d)
plot3d( x = X$x1, y = X$y1, z = X$z1 )


R プログラム (rgl を使用)
X <- read.table("/tmp/beetle_500000.stl.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE);
library(rgl)
plot3d( x = X$x1, y = X$y1, z = X$z1 )


SQLite 3 データベースの生成
SQLite 3
あらかじめ決めておく事項
このページでは,SQLite 3 データベースの生成を行う. 生成するSQLite 3 データベースのデータベース名を決めておくこと. このページでは,次のように書く.
- データベース名: stldb
データベース名は,自由に決めてよいが,半角文字(つまり英字と英記号)を使い,スペースを含まないこと,
テーブルの作成
- テーブル定義
- T(seq, nx, ny, nz, x1, y1, z1, x2, y2, z2, x3, y3, z3)
- stl(seq, name, nx, ny, nz, x1, y1, z1, x2, y2, z2, x3, y3, z3)
◆ bash プログラム
#!/bin/bash rm -f /tmp/stldb # cat >/tmp/a.$$.sql <<-SQL drop table T; SQL cat /tmp/a.$$.sql | sqlite3 /tmp/stldb # cat >/tmp/a.$$.sql <<-SQL drop table stl; SQL cat /tmp/a.$$.sql | sqlite3 /tmp/stldb # cat >/tmp/a.$$.sql <<-SQL create table T ( seq integer primary key not null, nx real, ny real, nz real, x1 real, y1 real, z1 real, x2 real, y2 real, z2 real, x3 real, y3 real, z3 REAL ); SQL cat /tmp/a.$$.sql | sqlite3 /tmp/stldb # cat >/tmp/a.$$.sql <<-SQL create table stl ( name INTEGER not null, seq TEXT not null, nx real, ny real, nz real, x1 real, y1 real, z1 real, x2 real, y2 real, z2 real, x3 real, y3 real, z3 REAL ); SQL cat /tmp/a.$$.sql | sqlite3 /tmp/stldb
- テーブルの作成
◆ bash プログラム
#!/bin/bash cat >/tmp/a.$$.sql <<-SQL .mode csv .import /tmp/a.$$.csv T insert into stl select 'beetle_500000' as name, T.seq as seq, T.nx as nx, T.ny as ny, T.nz as nz, T.x1 as x1, T.y1 as y1, T.z1 as z1, T.x2 as x2, T.y2 as y2, T.z2 as z2, T.x3 as x3, T.y3 as y3, T.z3 as z3 from T; drop table T; vacuum; SQL # cd /tmp rm -f readstl.c wget https://www.kkaneko.jp/rinkou/od/readstl.c g++ -o a.out readstl.c # # cd /tmp wget http://www.capture3d.com/Downloads/atos-beetle-1000000_stl.zip unzip atos-beetle-500000_stl.zip # ./a.out /tmp/beetle_500000.stl > /tmp/beetle_500000.stl.csv tail -n +2 /tmp/beetle_500000.stl.csv > /tmp/a.$$.csv cat /tmp/a.$$.sql | sqlite3 /tmp/stldb # echo 'select * from T limit 10;' | sqlite3 /tmp/stldb # echo 'select * from stl limit 10;' | sqlite3 /tmp/stldb
- (オプション) データベースの再構成
◆ bash プログラム
#!/bin/bash cd /tmp ls -la stldb # dump cat >/tmp/a.$$.sql <<-SQL .output /tmp/stldb.sql .dump .exit SQL # cat /tmp/a.$$.sql | sqlite3 /tmp/stldb # rm /tmp/stldb # resutor cat >/tmp/a.$$.sql <<-SQL .read /tmp/stldb.sql .exit SQL # cat /tmp/a.$$.sql | sqlite3 /tmp/stldb # cd /tmp ls -la stldb
SQLiteman で確認
- R で使ってみる
◆ bash プログラム
#!/bin/bash cat >/tmp/a.$$.r <<-RCOMMAND library(RSQLite) library(sqldf) library(rgl) dbname="/tmp/stldb" driver=dbDriver("SQLite") conn=dbConnect(driver,dbname) # T <- dbGetQuery(conn, "SELECT * from stl"); # D <- sqldf("select x1, y1, z1 from T") plot3d( x = D\$x1, y = D\$y1, z = D\$z1 ) Sys.sleep(100) RCOMMAND cat /tmp/a.$$.r | r