StLフォーマット

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
tui

StLフォーマット

#1

投稿記事 by tui » 6年前

visualstudioを使ってSTLデータを表示させたいのですが、なかなかうまくいきません

どこを変えたらいいのでしょうか?

コード:


#include <iostream>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;

struct vert
{
	double pnt1[3];
	double pnt2[3];
	double pnt3[3];
};

struct vert4
{
	double pnt1[4];//x,y,z,w
	double pnt2[4];
	double pnt3[4];
};

struct Triangle {
	double nornVec[3];
	double pnt1[3];
	double pnt2[3];
	double pnt3[3];
	unsigned int index;
};


class STLreader
{
	////一時的に利用する点の構造体
	//typedef struct tmp_point{
	//	double coord[3];     //座標
	//	unsigned int index;    //点に付随するユニークなインデックス
	//}tmp_point;

	////一時的に利用する辺の構造体
	//typedef struct tmp_edge{
	//	unsigned int start;
	//	unsigned int end;
	//	tmp_edge *next;
	//}tmp_edge;

	////点とその個数、一時的な利用
	//tmp_point *point_array[INT_MAX / 100000];

	//unsigned int num_points;//点の数
	//unsigned int num_edges;//辺の数

	unsigned int num_tmp_points;//一時的な変数
	unsigned int num_triangles;//三角形の数



public:
	int countTriangles(string filename)
	{
		//STLファイルのオープン
		ifstream ifs;
		char line[80];
		ifs.open(filename);
		if (ifs.fail())
			return false;
		string str;
		int count = 0;
		int a;
		getline(ifs, str);
		//int h = str.find("solid");
		if (str.find("solid")==-1){
			ifs.close();
			ifs.open(filename, ios::binary);
			if (ifs.fail())
				return false;
			ifs.read(line, 80);
			ifs.read(line, 4);
			return *(int *)line;
		}
		while (getline(ifs, str)){
			a = str.find("normal");
			if (a > 0)
				count++;
		}
		ifs.close();
		return count;
	};

	bool readASCIISTLFile(string filename, Triangle *values)
	{

		//STLファイルのオープン
		ifstream ifs(filename);
		if (ifs.fail())
			return false;

		//vector<mesh> Stldata;
		string str;
		int index = 0, i, j;
		double x = 0, y = 0, z = 0;
		//tmp_point *tmp_pnt = new tmp_point;

		////STLファイルのオープン
		//in = fopen(filename.c_str(), "r");
		//if (in == NULL)
		//	return false;
		getline(ifs, str);
		if (0<str.find("solid"))
			return false;
		//ファイルから座標値の読み込み
		printf("Tring text STL file ...");
		num_tmp_points = 0;
		num_triangles = 0;


		while (getline(ifs, str)){

			////normalが見つかるまで読み飛ばす
			//i = str.find("normal");
			//if (i > 0){
			//	for (int c = 2; c >= 0; c--){
			//		values[num_triangles].nornVec[c] = atof(str.substr(str.rfind(" ")).c_str());
			//		str.erase(str.rfind(" "));
			//		num_tmp_points++;
			//	}
			//}

			//vertexが見つかるまで読み飛ばす
			j = str.find("vertex");
			if (j<0)
				continue;
			//連続する3頂点の読み込みポリゴンを登録
			//tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
			for (int c = 2; c >= 0; c--)
			{
				values[num_triangles].pnt1[c] = atof(str.substr(str.rfind(" ")).c_str());
				str.erase(str.rfind(" "));
				num_tmp_points++;
			}
			//point_array[num_tmp_points-1] = tmp_pnt;

			//tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
			getline(ifs, str);
			for (int c = 2; c >= 0; c--)
			{
				values[num_triangles].pnt2[c] = atof(str.substr(str.rfind(" ")).c_str());
				str.erase(str.rfind(" "));
				//tmp_pnt->index = num_tmp_points;
				num_tmp_points++;
			}
			//point_array[num_tmp_points-1] = tmp_pnt;

			//tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
			getline(ifs, str);
			for (int c = 2; c >= 0; c--)
			{
				values[num_triangles].pnt3[c] = atof(str.substr(str.rfind(" ")).c_str());
				str.erase(str.rfind(" "));
				//tmp_pnt->index = num_tmp_points;
				num_tmp_points++;
			}
			values[num_triangles].nornVec[0] = getNormalVert(values[num_triangles])[0];
			values[num_triangles].nornVec[1] = getNormalVert(values[num_triangles])[1];
			values[num_triangles].nornVec[2] = getNormalVert(values[num_triangles])[2];
			
			values[num_triangles].index = num_triangles;
			num_triangles++;

		}
		ifs.close();
		//fclose(in);
		if (num_triangles > 0)
			printf("Done.\n");
		else
			printf("Failed\n");
		return(num_triangles > 0);

	}

	bool readBinarySTLFile(string STL_file, Triangle *values)
	{
		char line[81];
		float *coord;
		int num;
		FILE *in;
		errno_t error;

		ifstream ifs(STL_file);
		if (ifs.fail())
			return false;
		//ifstream ifs(STL_file, ios_base::binary);
		//ofstream ofs("result.txt");//結果を書き込む用

		//if (!ifs)
		//{
		//	// エラー処理
		//	ofs << "Can't open file " << endl;
		//	ifs.close();
		//	return false;
		//}

		if ((error = fopen_s(&in,STL_file.c_str(), "rb")) != 0)
		{
			// エラー処理
			//ofs << "Can't open file " << endl;
			fclose(in);
			return false;
		}

		//最初の84ビット分を読み飛ばす
		if (fread(line, 1, 80, in) != 80)
			return false;

		printf("Tring binary STL file ...");
		fread(line, 1, 4, in);
		num = *(int*)line;

		//ofs << "number of triangles: " << num<< endl;
		int j = 0;
		//ofs << "solid model" << endl;
		while (fread(line, 1, 50, in) == 50)
		{
			coord = (float *)line;
			//values[j].nornVec[0] = coord[0];
			//values[j].nornVec[1] = coord[1];
			//values[j].nornVec[2] = coord[2];
			
			values[j].pnt1[0] = coord[3];
			values[j].pnt1[1] = coord[4];
			values[j].pnt1[2] = coord[5];

			values[j].pnt2[0] = coord[6];
			values[j].pnt2[1] = coord[7];
			values[j].pnt2[2] = coord[8];

			values[j].pnt3[0] = coord[9];
			values[j].pnt3[1] = coord[10];
			values[j].pnt3[2] = coord[11];
			
			values[j].nornVec[0] = getNormalVert(values[j])[0];
			values[j].nornVec[1] = getNormalVert(values[j])[1];
			values[j].nornVec[2] = getNormalVert(values[j])[2];
			values[j].index = j;
			j++;

		}

		//ofs << "endsolid model" << endl;
		fclose(in);
		//ofs.close();
		if (num > 0)
			printf("Done.\n");
		else
			printf("Failed\n");
		return(num > 0);

	}


	void cross(double vec0[], double vec1[], double vec2[])
	{
		vec2[0] = vec0[1] * vec1[2] - vec0[2] * vec1[1];
		vec2[1] = vec0[2] * vec1[0] - vec0[0] * vec1[2];
		vec2[2] = vec0[0] * vec1[1] - vec0[1] * vec1[0];
	}

	void normVec(double vec[])
	{
		double norm;
		norm = sqrt((vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]));
		vec[0] /= norm; vec[1] /= norm; vec[2] /= norm;
	}

	double* getNormalVert(Triangle vertex)
	{
		double normalVec[3] = {};
		double vec1[3] = {	vertex.pnt2[0] - vertex.pnt1[0],
							vertex.pnt2[1] - vertex.pnt1[1],
							vertex.pnt2[2] - vertex.pnt1[2] };
		double vec2[3] = {	vertex.pnt3[0] - vertex.pnt2[0],
							vertex.pnt3[1] - vertex.pnt2[1],
							vertex.pnt3[2] - vertex.pnt2[2] };
		cross(vec1, vec2, normalVec);
		normVec(normalVec);//正規化
		return normalVec;
	}


};


int main()
{
	int countTriangles(string cube);

	bool readASCIISTLFile(string cubeASCIISTL, Triangle *values);

	bool readBinarySTLFile(string cubebinaryfile, Triangle *values);


	return 0;
}




アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: StLフォーマット

#2

投稿記事 by みけCAT » 6年前

長いのでまだ全部読んでいませんが、とりあえずmain関数内に戻り値を返す以外の処理が全く書かれていないので表示されるわけがないと思います。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
かめのこのこのこ
記事: 15
登録日時: 7年前

Re: StLフォーマット

#3

投稿記事 by かめのこのこのこ » 6年前

tui さんのコードの一部(295~299行目)ですが、

コード:

    int countTriangles(string cube);
 
    bool readASCIISTLFile(string cubeASCIISTL, Triangle *values);
 
    bool readBinarySTLFile(string cubebinaryfile, Triangle *values);
これらはすべて、関数の呼び出しではなく、関数の宣言しか行われていません。
(関数の宣言 => http://unageanu.hatenablog.com/entry/20 ... 1188035176

次に、クラス内に定義された関数の呼び方の一例を示しておくので、これをヒントにしてみてください。

コード:

#include <iostream>

using namespace std;

class Foo {
    int Value = 0;
public:
    // 変数 Value の値を読み出す 
    int GetValue() {
        return Value;
    }

    // 変数 Value に新しい値を書き込む
    void SetValue(int NewValue) {
        Value = NewValue;
    }
};

int main() {
    // クラス Foo のインスタンス foo を生成 
    Foo foo;

    // foo の中の変数 Value に 10 を書き込む
    foo.SetValue(10);
    // foo の中の変数 Value を読み取る
    int fooValue = foo.GetValue();

    // 標準出力に fooValue の値を出力
    cout << fooValue << endl;

    return 0;
}

/*
* 出力:
* 10
*/
オフトピック
クラス内に定義された関数の呼び出し方がわからなかったという前提で返信を書いたが、ひょっとしてクラスというものから説明するべきだったか?

返信

“C言語何でも質問掲示板” へ戻る