2016年3月6日日曜日

UE4.x CSVのテスト1

CSVのテスト

// CSVファイルを読み込んでobj形式で出力するテスト
// TestCSV1(TEXT("Content/ThirdPerson/TextFiles/Mesh1.csv"));
void TestCSV1(const FString& path)
{
 // CSVデータ 9列
 // Vertex,Index,POSITION 0,POSITION 1,POSITION 2,TEXCOORD0 0,TEXCOORD0 1,TEXCOORD1 0,TEXCOORD1 1
 struct CSVData
 {
  int32 Vertex;
  int32 Index;
  FVector Pos;
  FVector2D Tex0;
  FVector2D Tex1;
 };

 // CSVデータの格納用
 TArray<CSVData> csvDaraArray;
 // テキストファイルの読み込み
 TArray<FString> lineArray;
 bool done = FFileHelper::LoadANSITextFileToStrings(*(FPaths::GameDir() + path), NULL, lineArray);
 if(!done) {
  UE_LOG(LogTemp, Error, TEXT("TestCSV1 %s Failed"), *path);
  return;
 }
 // 配列に格納
 CSVData csvData;
 for(int32 i = 0; i < lineArray.Num(); i++)
 {
  if(i == 0) continue; // 先頭スキップ
  TArray<FString> csvValues;
  int32 numElement = lineArray[i].ParseIntoArray(&csvValues, TEXT(","), true);

  if(9 != csvValues.Num()) {
   continue;
  }
  csvData.Vertex = FCString::Atoi(*csvValues[0]);
  csvData.Index = FCString::Atoi(*csvValues[1]);
  csvData.Pos.X = FCString::Atof(*csvValues[2]);
  csvData.Pos.Y = FCString::Atof(*csvValues[3]);
  csvData.Pos.Z = FCString::Atof(*csvValues[4]);
  csvData.Tex0.X = FCString::Atof(*csvValues[5]);
  csvData.Tex0.Y = FCString::Atof(*csvValues[6]);
  csvData.Tex1.X = FCString::Atof(*csvValues[7]);
  csvData.Tex1.Y = FCString::Atof(*csvValues[8]);

  csvDaraArray.Add(csvData);
 }

 UE_LOG(LogTemp, Display, TEXT("csvDaraArray num %d"), csvDaraArray.Num());

 TMap<int32, CSVData> indexCSVDataMap; // 頂点インデックスと頂点座標のマップ
 for(int32 i = 0; i < csvDaraArray.Num(); i++)
 {
  int32 idx = csvDaraArray[i].Index;

  CSVData* pData = indexCSVDataMap.Find(idx);
  if(!pData)
  {
   indexCSVDataMap.Add(idx, csvDaraArray[i]);
  }
 }
#ifdef _WIN64
 // Wavefront obj形式で出力するテスト
 char buf[512];

 // Group
 sprintf(buf, "g Mesh1\n");
 ::OutputDebugStringA(buf);
 // Pos
 for(int32 i = 0; i < indexCSVDataMap.Num(); i++)
 {
  sprintf(buf, "v %f %f %f\n", indexCSVDataMap[i].Pos.X, indexCSVDataMap[i].Pos.Y, indexCSVDataMap[i].Pos.Z);
  ::OutputDebugStringA(buf);
 }
 // Tex0
 for(int32 i = 0; i < indexCSVDataMap.Num(); i++)
 {
  sprintf(buf, "vt %f %f\n", indexCSVDataMap[i].Tex0.X, indexCSVDataMap[i].Tex0.Y);
  ::OutputDebugStringA(buf);
 }
 // Tex1
 //for(int32 i = 0; i < indexCSVDataMap.Num(); i++)
 //{
 // sprintf(buf, "vt %f %f\n", indexCSVDataMap[i].Tex1.X, indexCSVDataMap[i].Tex1.Y);
 // ::OutputDebugStringA(buf);
 //}
 // face
 for(int32 i = 0; i < csvDaraArray.Num(); i += 3)
 {
  int32 i0 = csvDaraArray[i + 0].Index + 1;
  int32 i1 = csvDaraArray[i + 1].Index + 1;
  int32 i2 = csvDaraArray[i + 2].Index + 1;

  sprintf(buf, "f %d/%d// %d/%d// %d/%d//\n", i0, i0, i1, i1, i2, i2);
  ::OutputDebugStringA(buf);
 }
#endif
}

UE4.x XMLのテスト1

プロジェクト名.Build.cs
PublicDependencyModuleNames に XmlParser を追加してビルド
プロジェクト設定/パッケージ化の Additional Non-Asset Directories to Packageに
Content/ThirdPerson/TextFiles を追加した
XMLのパーステスト

#include "XmlParser.h" // <- 追加

// OpenStreetMapのxmlファイルを読み込んでパースするテスト
// TestXml1(TEXT("Content/ThirdPerson/TextFiles/map_0001.osm"));
void TestXml1(const FString& path)
{
 UE_LOG(LogTemp, Display, TEXT("FPaths::GameDir() %s"), *FPaths::GameDir());

 FXmlFile xmlFile;
 xmlFile.LoadFile(FPaths::GameDir() + path);

 FXmlNode* pRoot = xmlFile.GetRootNode();
 if(!pRoot)
 {
  UE_LOG(LogTemp, Error, TEXT("%s RootNode not found"), *path);
  return;
 }

 UE_LOG(LogTemp, Display, TEXT("RootNode Tag %s"), *(pRoot->GetTag()));

 // ルートの子ノードの配列を取得
 const TArray<FXmlNode*>& childNode = pRoot->GetChildrenNodes();
 for(int i = 0; i < childNode.Num(); i++)
 {
  const FString& tag = childNode[i]->GetTag();
  UE_LOG(LogTemp, Display, TEXT("\tChildNode %d Tag %s"), i, *tag);

  if(tag == TEXT("node"))
  {
   UE_LOG(LogTemp, Display, TEXT("\t\tnode content %s"), *(childNode[i]->GetContent()));

   // ノードの属性の配列を取得
   const TArray<FXmlAttribute>& attrib = childNode[i]->GetAttributes();
   for(auto a : attrib)
   {
    const FString& tag = a.GetTag();
    const FString& value = a.GetValue();

    UE_LOG(LogTemp, Display, TEXT("\t\t\t%s = %s"), *tag, *value);
   }
  }
 }
}



2016年3月5日土曜日

UE4.x Jsonのテスト1

*.Build.cs の PublicDependencyModuleNames に Json を追加してビルドした

Jsonのパーステスト

#include "Json.h"  // 追加

void TestJson1() {

 FString jsonData = 
 "{\
  \"objArray\" : [\
   { \"pos\": [1.23, 2.33, 1.2345], \"rot\": [ 0.0, 0.0, 0.0] },\
   { \"pos\": [10.23, 20.33, 10.2345], \"rot\": [ 3.0, 6.0, 9.0] },\
   { \"pos\": [20.23, 40.33, 20.2345], \"rot\": [ 10.0, 20.0, 30.0] }\
  ]\
 }";

 // jsonDataをデシリアイズする
 TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
 TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(jsonData);
 bool result = FJsonSerializer::Deserialize(JsonReader, JsonObject);

 // objArrayを取得
 // フィールド名から配列を取得
 const TArray< TSharedPtr<FJsonValue> >& objArray = JsonObject->GetArrayField(TEXT("objArray"));
 for(int32 i = 0; i < objArray.Num(); i++)
 {
  // 配列内のJSONオブジェクトをパース
  // FJsonObjectに変換
  const TSharedPtr<FJsonObject>& Obj = objArray[i]->AsObject();

  const TArray< TSharedPtr<FJsonValue> >& pos = Obj->GetArrayField(TEXT("pos"));
  for(int32 i = 0; i < pos.Num(); i++)
  {
   UE_LOG(LogTemp, Display, TEXT("pos %d %lf"), i, pos[i]->AsNumber());
  }

  const TArray< TSharedPtr<FJsonValue> >& rot = Obj->GetArrayField(TEXT("rot"));
  for(int32 i = 0; i < rot.Num(); i++)
  {
   UE_LOG(LogTemp, Display, TEXT("rot %d %lf"), i, rot[i]->AsNumber());
  }
 }
}