boostを使ったxmlファイルの値の取得ができません.

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ace-k
記事: 2
登録日時: 9年前

boostを使ったxmlファイルの値の取得ができません.

#1

投稿記事 by ace-k » 9年前

はじめまして.
現在,boostを使用して,以下のxmlから座標値を取得して,4×4行列に値を格納したいのですが,その中身となる<Rotation><M_00>~<M_22>と<Center><x><y><z>を取得することができません.
たとえば,<ImagePath>や<FocalLength>は下記のプログラムで値を取得できますが,<Rotation>や<Center>のように子ノードに値が入っているとプログラムがうまく動きません(24~26行目の処理).色々調べているのですが,わからないため,解決策を教えていただけないでしょうか.

対象のXMLファイル

コード:

-<ATExport>
  -<Photo>
      <ImagePath>C:\Users\xxxx.JPG</ImagePath>
      <FocalLength>4260.0611535683593</FocalLength>
      <AspectRatio>1</AspectRatio>
     -<Rotation>
        <M_00>0.84296302804976242</M_00>
        <M_01>0.53617827316920286</M_01>
        <M_02>-0.043888412166162565</M_02>
        <M_10>0.0057364743957685725</M_10>
        <M_11>-0.090535342960440565</M_11>
        <M_12>-0.99587672155570628</M_12>
        <M_20>-0.53794091330059846</M_20>
        <M_21>0.83923549201420622</M_21>
        <M_22>-0.079393719783059272</M_22>
      </Rotation>
     -<Center>
        <x>-1.6122401371584472</x>
        <y>-3.0789121912094748</y>
        <z>-0.11422715613536941</z>
      </Center>
   </Photo>
…
-<ATExport>
プログラムの抜粋

コード:

struct Photo{
	string img_path;					// ImagePath
	float f_length;						// FocalLength
	Eigen::Matrix4f sensor_mat;			// SensorMatrix(Rotation+Center)
};

bool MyFilesIO::LoadXmlFile()
{
	using namespace boost::property_tree;
	using namespace std;

	std::vector<Photo> PhotoVector;

	ptree pt;
	read_xml( file_name, pt );

	BOOST_FOREACH ( const auto &photo, pt.get_child("ATExport") ){
		
		Photo *ph = new Photo();
		
		ph->img_path = photo.second.get<string>("ImagePath");
		ph->f_length = photo.second.get<float>("FocalLength");
		
         BOOST_FOREACH (const auto &child, pt.get_child("ATExport.Photo.Rotation")){
	        ph->sensor_mat(0,0) = child.second.get<float>("M_00");
         }

		 PhotoVector.push_back(*ph);
	}

	for(int i=0;i<PhotoVector.size();i++){
            cout << "ImagePath[" << i << "]:" << PhotoVector[i].img_path << endl; 
            cout << "FocalLength[" <<i << "]:" << PhotoVector[i].f_length << endl; 
            cout << "Sensor_mat[" << i << "]:" << PhotoVector[i].sensor_mat << endl;
	}
return true;
}

pocket
記事: 49
登録日時: 9年前

Re: boostを使ったxmlファイルの値の取得ができません.

#2

投稿記事 by pocket » 9年前

こんばんは。

私も初学者なのでアドバイスが適当か分かりませんが、参考程度に書きます。

コード:

BOOST_FOREACH (const auto &child, pt.get_child("ATExport.Photo.Rotation")){
            ph->sensor_mat(0,0) = child.second.get<float>("M_00");
}
上記のコードですが、M_00についてしか値を取得していないような気がします。
XMLには、M_00,M_02,M_10とあるので、それぞれについてsecond.get関数を呼ぶ必要があるのではないでしょうか?

アバター
a5ua
記事: 199
登録日時: 14年前

Re: boostを使ったxmlファイルの値の取得ができません.

#3

投稿記事 by a5ua » 9年前

ace-k さんが書きました:

コード:

         BOOST_FOREACH (const auto &child, pt.get_child("ATExport.Photo.Rotation")){
            ph->sensor_mat(0,0) = child.second.get<float>("M_00");
         }
コンパイルしてませんが、以下のようなコードでいかがでしょうか?

コード:

		for (int i = 0; i < 3; ++i) {
			for (int j = 0; j < 3; ++j) {
				std::ostringstream oss;
				oss << "Rotation.M_" << i << j;
				ph->sensor_mat(i, j) = photo.second.get<float>(oss.str());
			}
		}

ace-k
記事: 2
登録日時: 9年前

Re: boostを使ったxmlファイルの値の取得ができません.

#4

投稿記事 by ace-k » 9年前

pocketさん,a5uaさん

コメントありがとうございました.
自分でも色々試してみたところ,下記のサイトを参考に実装したら,値を取得できました.
http://qiita.com/todanano/items/40d82665b865030af9ab

具体的には以下のような感じでできました.

コード:

		BOOST_FOREACH ( const auto &itt, it.second ){

			if( itt.first != "Rotation" ){
				
				continue;
			}

			ph->sensor_mat(0,0) = itt.second.get<float>("M_00");
			ph->sensor_mat(1,0) = itt.second.get<float>("M_01");
			ph->sensor_mat(2,0) = itt.second.get<float>("M_02");

			ph->sensor_mat(0,1) = itt.second.get<float>("M_10");
			ph->sensor_mat(1,1) = itt.second.get<float>("M_11");
			ph->sensor_mat(2,1) = itt.second.get<float>("M_12");

			ph->sensor_mat(0,2) = itt.second.get<float>("M_20");
			ph->sensor_mat(1,2) = itt.second.get<float>("M_21");
			ph->sensor_mat(2,2) = itt.second.get<float>("M_22");

		}


閉鎖

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