Commit 8f97c099 authored by Jan Kremer's avatar Jan Kremer

Made HDF5 import more robust and added DataScaleFactor to display data of very different scale.

parent 5b15db3f
No preview for this file type
No preview for this file type
......@@ -3,10 +3,66 @@
#include "HDF5Library.h"
#include "Runtime/Core/Public/Misc/Paths.h"
#include "Runtime/Core/Public/GenericPlatform/GenericPlatformFile.h"
#include "H5Cpp.h"
using namespace H5;
bool UHDF5Library::ImportData(TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W)
DataSet UHDF5Library::getDatasetByName(Group & group, char * name)
{
DataSet dataset;
try {
dataset = group.openDataSet(name);
}
catch (...)
{
FString adjustedName = FString(name);
if (islower(*name))
{
adjustedName = adjustedName.ToUpper();
}
else
{
adjustedName = adjustedName.ToLower();
}
char* adjustedNameAsCharArray = TCHAR_TO_ANSI(*adjustedName);
dataset = group.openDataSet(adjustedNameAsCharArray);
}
return dataset;
}
Group UHDF5Library::getGroupByName(H5File & file, FString name)
{
for (int i = 0; i < 1000; ++i)
{
Group group;
FString Number;
FString Cat;
char* CatAsCharArray = NULL;
try {
Number = FString::FromInt(i);
Cat = name + Number;
CatAsCharArray = TCHAR_TO_ANSI(*Cat);
group = file.openGroup(CatAsCharArray);
return group;
}
catch (...)
{
FString adjustedName = FString(CatAsCharArray).ToLower();
char* adjustedNameAsCharArray = TCHAR_TO_ANSI(*adjustedName);
try {
group = file.openGroup(adjustedNameAsCharArray);
return group;
}
catch (...)
{
}
}
}
return Group();
}
bool UHDF5Library::ImportData(FString FileName, TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W)
{
/*
* Turn off the auto-printing when failure occurs so that we can
......@@ -20,22 +76,26 @@ bool UHDF5Library::ImportData(TArray<float>& X, TArray<float>& Y, TArray<float>&
FString relpath = FPaths::ProjectDir();
TCHAR *dirname = TEXT("Resources");
relpath.PathAppend(dirname, _tcslen(dirname));
//TCHAR *dirname = TEXT("Resources");
//relpath.PathAppend(dirname, _tcslen(dirname));
relpath += FString("/Resources/");
//TCHAR *filename = TEXT("abc64x64x64.h5part");
//relpath.PathAppend(filename, _tcslen(filename));
TCHAR *filename = TEXT("data.h5part");
relpath.PathAppend(filename, _tcslen(filename));
relpath += FileName;
FString abspath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*relpath);
char* pathAsCharArray = TCHAR_TO_ANSI(*abspath);
H5File file(pathAsCharArray, H5F_ACC_RDONLY);
Group group = file.openGroup("Step#150");
Group group = getGroupByName(file, "Step#");
/*
* Get dataspace of one dataset to get the dimension. (Dimensions must be equal for all sets.)
*/
DataSet dataset = group.openDataSet("X"); // TODO if "X" does not exist, search for "x". do the same for the other 5 dimensions.
DataSet dataset = getDatasetByName(group, "X");
DataSpace dataspace = dataset.getSpace();
int rank = dataspace.getSimpleExtentNdims();
hsize_t dims_out[1];
......@@ -58,35 +118,35 @@ bool UHDF5Library::ImportData(TArray<float>& X, TArray<float>& Y, TArray<float>&
/*
* Read Y
*/
dataset = group.openDataSet("Y");
dataset = getDatasetByName(group, "Y");
dataset.read(data_out, PredType::NATIVE_FLOAT);
Y = TArray<float>(data_out, dims_out[0]);
/*
* Read Z
*/
dataset = group.openDataSet("Z");
dataset = getDatasetByName(group, "Z");
dataset.read(data_out, PredType::NATIVE_FLOAT);
Z = TArray<float>(data_out, dims_out[0]);
/*
* Read U
*/
dataset = group.openDataSet("u");
dataset = getDatasetByName(group, "U");
dataset.read(data_out, PredType::NATIVE_FLOAT);
U = TArray<float>(data_out, dims_out[0]);
/*
* Read V
*/
dataset = group.openDataSet("v");
dataset = getDatasetByName(group, "V");
dataset.read(data_out, PredType::NATIVE_FLOAT);
V = TArray<float>(data_out, dims_out[0]);
/*
* Read W
*/
dataset = group.openDataSet("w");
dataset = getDatasetByName(group, "W");
dataset.read(data_out, PredType::NATIVE_FLOAT);
W = TArray<float>(data_out, dims_out[0]);
......
......@@ -4,17 +4,23 @@
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "H5Cpp.h"
#include "HDF5Library.generated.h"
using namespace H5;
/**
*
*
*/
UCLASS()
class IMMERSIVEANALYTICS_API UHDF5Library : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = HDF5)
static bool ImportData(TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W);
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Test HDF5", Keywords = "TEST HDF5"), Category = HDF5)
static bool ImportData(FString FileName, TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W);
private:
static DataSet getDatasetByName(Group & group, char * name);
static Group getGroupByName(H5File & file, FString name);
};
......@@ -29,7 +29,7 @@ void APlot3D::BeginPlay()
void APlot3D::InitPoints()
{
UHDF5Library::ImportData(X, Y, Z, U, V, W);
UHDF5Library::ImportData(FileName, X, Y, Z, U, V, W);
int32 Num;
if (OverridePointCount)
......@@ -44,10 +44,16 @@ void APlot3D::InitPoints()
IsBrushed = TArray<bool>();
IsBrushed.Init(false, Num);
float XSum = 0;
for (int32 i = 0; i < Num; i++)
{
Length.Emplace(FVector(U[i], V[i], W[i]).Size());
XSum += X[i];
}
float XMean = XSum / Num;
DataScaleFactor = 0.5 / XMean;
SetActorScale3D(FVector(DataScaleFactor, DataScaleFactor, DataScaleFactor));
int32 MaxIndex, MinIndex;
UKismetMathLibrary::MaxOfFloatArray(Length, MaxIndex, MaxLength);
......
......@@ -109,9 +109,15 @@ public:
UPROPERTY(Category = Plot3D, VisibleDefaultsOnly, BlueprintReadOnly)
float MaxW;
UPROPERTY(EditDefaultsOnly, Category = Plot3D)
UPROPERTY(Category = Plot3D, VisibleDefaultsOnly, BlueprintReadOnly)
float DataScaleFactor;
UPROPERTY(EditAnywhere, Category = Plot3D)
UCurveVector* TransferFunction;
UPROPERTY(EditAnywhere, Category = Plot3D)
FString FileName;
UPROPERTY(Category = Plot3D, VisibleAnywhere, BlueprintReadOnly)
UPointCloud *PointCloud;
......
......@@ -193,7 +193,15 @@ void UPlotCanvas::DrawHistogramPlot(int32 bins)
for (int i = 0; i < PawnBase->Plot3D->Points.Num(); ++i)
{
float CurX = (*XArray)[i];
int32 MyIndex = (CurX - XMin) / Diff * (bins - 1);
int32 MyIndex;
if (Diff == 0)
{
MyIndex = 0;
}
else
{
MyIndex = (CurX - XMin) / Diff * (bins - 1);
}
++XCount[MyIndex];
if (PawnBase->Plot3D->IsBrushed[i])
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment