Commit e1ce11bd authored by Jørgen Lind's avatar Jørgen Lind
Browse files

Make InMemory test a unittest

parent 615e3dd9
......@@ -18,13 +18,10 @@
#ifndef VOLUMEINDEXER_H
#define VOLUMEINDEXER_H
#include <OpenVDS/openvds_export.h>
#include <OpenVDS/Vector.h>
#include <OpenVDS/VolumeData.h>
#include <OpenVDS/openvds_export.h>
#include <cstdint>
#include "CoordinateTransformer.h"
#include <OpenVDS/CoordinateTransformer.h>
namespace OpenVDS
{
......
......@@ -16,130 +16,36 @@
****************************************************************************/
#include <OpenVDS/OpenVDS.h>
#include <OpenVDS/VolumeDataLayoutDescriptor.h>
#include <OpenVDS/VolumeDataAxisDescriptor.h>
#include <OpenVDS/VolumeDataChannelDescriptor.h>
#include <OpenVDS/KnownMetadata.h>
#include <OpenVDS/GlobalMetadataCommon.h>
#include <OpenVDS/VolumeDataLayout.h>
#include <OpenVDS/VolumeDataAccess.h>
#include <VDS/SimplexNoiceKernel.h>
#include <cstdlib>
#include <fmt/format.h>
#include <gtest/gtest.h>
#include <array>
#include "../utils/GenerateVDS.h"
inline bool ends_with(std::string const &value, std::string const &ending)
template<typename T>
size_t byteSize(const std::vector<T> vec)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
return sizeof(T) * vec.size();
}
TEST(IOTests, InMemory)
{
OpenVDS::Error error;
OpenVDS::InMemoryOpenOptions options;
auto brickSize = OpenVDS::VolumeDataLayoutDescriptor::BrickSize_64;
int negativeMargin = 0;
int positiveMargin = 0;
int brickSize2DMultiplier = 4;
auto lodLevels = OpenVDS::VolumeDataLayoutDescriptor::LODLevels_None;
auto layoutOptions = OpenVDS::VolumeDataLayoutDescriptor::Options_None;
OpenVDS::VolumeDataLayoutDescriptor layoutDescriptor(brickSize, negativeMargin, positiveMargin, brickSize2DMultiplier, lodLevels, layoutOptions);
std::vector<OpenVDS::VolumeDataAxisDescriptor> axisDescriptors;
axisDescriptors.push_back(OpenVDS::VolumeDataAxisDescriptor(1126, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_SAMPLE, "ms", 0.0f, 4.f));
axisDescriptors.emplace_back(605, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_CROSSLINE, "", 1932.f, 2536.f);
axisDescriptors.emplace_back(385, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_INLINE, "", 9985.f, 10369.f);
std::vector<OpenVDS::VolumeDataChannelDescriptor> channelDescriptors;
channelDescriptors.emplace_back(OpenVDS::VolumeDataChannelDescriptor::Format_R32, OpenVDS::VolumeDataChannelDescriptor::Components_1, AMPLITUDE_ATTRIBUTE_NAME, "", -0.10919982194900513, 0.1099749207496643);
OpenVDS::MetadataContainer metadataContainer;
std::unique_ptr<OpenVDS::VDS, decltype(&OpenVDS::Close)> handle(OpenVDS::Create(options, layoutDescriptor, axisDescriptors, channelDescriptors, metadataContainer, error), &OpenVDS::Close);
std::unique_ptr<OpenVDS::VDS, decltype(&OpenVDS::Close)> handle(generateSimpleInMemory3DVDS(60,60,60), &OpenVDS::Close);
ASSERT_TRUE(handle);
OpenVDS::VolumeDataLayout *layout = OpenVDS::GetLayout(handle.get());
ASSERT_TRUE(layout);
OpenVDS::VolumeDataAccessManager *accessManager = OpenVDS::GetDataAccessManager(handle.get());
ASSERT_TRUE(accessManager);
OpenVDS::VolumeDataPageAccessor *pageAccessor = accessManager->CreateVolumeDataPageAccessor(layout, OpenVDS::Dimensions_012, 0, 0, 100, OpenVDS::VolumeDataAccessManager::AccessMode_Create);
ASSERT_TRUE(pageAccessor);
int32_t chunkCount = int32_t(pageAccessor->GetChunkCount());
//float value_min
for (int i = 0; i < chunkCount; i++)
{
OpenVDS::VolumeDataPage *page = pageAccessor->CreatePage(i);
OpenVDS::VolumeIndexer3D outputIndexer(page, 0, 0, OpenVDS::Dimensions_012, layout);
OpenVDS::FloatVector3 frequency(0.6f, 2.f, 4.f);
int pitch[OpenVDS::Dimensionality_Max];
void *buffer = page->GetWritableBuffer(pitch);
OpenVDS::CalculateNoise3D(buffer, OpenVDS::VolumeDataChannelDescriptor::Format_R32, &outputIndexer, frequency, 0.001f, 0.f, false, 345);
page->Release();
}
pageAccessor->Commit();
pageAccessor->SetMaxPages(0);
accessManager->FlushUploadQueue();
///////////////////////////////////////////////////////
int32_t output_width = 2000;
int32_t output_height = 2000;
std::string file_name = "output.bmp";
if (!ends_with(file_name, ".bmp"))
file_name = file_name + ".bmp";
int32_t axis_mapper[3] = { 2, 1, 0};
std::unique_ptr<FILE, decltype(&fclose)> file(fopen(file_name.c_str(), "wb"), &fclose);
if (!file)
{
fmt::print(stderr, "Failed to open file: {}\n", file_name);
ASSERT_TRUE(false);
}
int sampleCount[3];
sampleCount[0] = layout->GetDimensionNumSamples(axis_mapper[0]);
sampleCount[1] = layout->GetDimensionNumSamples(axis_mapper[1]);
sampleCount[2] = layout->GetDimensionNumSamples(axis_mapper[2]);
fmt::print(stdout, "Found data set with sample count [{}, {}, {}]\n", sampleCount[0], sampleCount[1], sampleCount[2]);
float x_sample_shift = float(sampleCount[1]) / output_width;
float y_sample_shift = float(sampleCount[2]) / output_height;
std::vector<std::array<float, OpenVDS::Dimensionality_Max>> samples;
samples.resize(size_t(output_width) * size_t(output_height));
int32_t axis_position = 300;
axis_position = std::max(0, axis_position);
axis_position = std::min(sampleCount[0], axis_position);
fill3DVDSWithNoice(handle.get());
for (int y = 0; y < output_height; y++)
{
float y_pos = y * y_sample_shift;
for (int x = 0; x < output_width; x++)
{
float x_pos = x * x_sample_shift;
auto &pos = samples[size_t(y) * size_t(output_width) + size_t(x)];
pos[size_t(axis_mapper[0])] = axis_position;
pos[size_t(axis_mapper[1])] = x_pos;
pos[size_t(axis_mapper[2])] = y_pos;
}
}
auto layout = OpenVDS::GetLayout(handle.get());
auto accessManager = OpenVDS::GetDataAccessManager(handle.get());
int32_t minPos[OpenVDS::Dimensionality_Max] = {15, 15, 15};
int32_t maxPos[OpenVDS::Dimensionality_Max] = {55, 55, 55};
std::vector<float> data;
data.resize(size_t(output_width) * size_t(output_height));
int64_t request = accessManager->RequestVolumeSamples(data.data(), layout, OpenVDS::Dimensions_012, 0, 0, reinterpret_cast<const float (*)[OpenVDS::Dimensionality_Max]>(samples.data()), samples.size(), OpenVDS::InterpolationMethod::Linear);
data.resize((maxPos[0] - minPos[0]) * (maxPos[1] - minPos[1]) * (maxPos[2] - minPos[2]));
int64_t request = accessManager->RequestVolumeSubset(data.data(), layout, OpenVDS::Dimensions_012, 0, 0,minPos, maxPos, OpenVDS::VolumeDataChannelDescriptor::Format_R32);
bool finished = accessManager->WaitForCompletion(request);
if (!finished)
{
......@@ -149,52 +55,24 @@ TEST(IOTests, InMemory)
float minValue = layout->GetChannelValueRangeMin(0);
float maxValue = layout->GetChannelValueRangeMax(0);
float intScale = layout->GetChannelIntegerScale(0);
float intLayout = layout->GetChannelIntegerOffset(0);
OpenVDS::QuantizingValueConverterWithNoValue<uint8_t, float, false> converter(minValue, maxValue, intScale, intLayout, 0.f, 0.f);
std::vector<std::array<uint8_t, 3>> fileData;
fileData.resize(size_t(output_width) * size_t(output_height));
for (int y = 0; y < output_height; y++)
{
for (int x = 0; x < output_width; x++)
{
float range = maxValue - minValue;
uint8_t value = converter.ConvertValue(data[size_t(y * output_width + x)]);
auto &color = fileData[size_t(y * output_width + x)];
color[0] = value;
color[1] = value;
color[2] = value;
}
}
std::vector<uint32_t> histogram;
histogram.resize(1000);
uint32_t filesize = 54 + (sizeof(*fileData.data()) * uint32_t(fileData.size()));
uint8_t bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
uint8_t bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
uint8_t bmppad[3] = {0,0,0};
float bucketSize = range / 1000.f;
bmpfileheader[ 2] = (uint8_t)(filesize );
bmpfileheader[ 3] = (uint8_t)(filesize>> 8);
bmpfileheader[ 4] = (uint8_t)(filesize>>16);
bmpfileheader[ 5] = (uint8_t)(filesize>>24);
bmpinfoheader[ 4] = (uint8_t)(output_width );
bmpinfoheader[ 5] = (uint8_t)(output_width>> 8);
bmpinfoheader[ 6] = (uint8_t)(output_width>>16);
bmpinfoheader[ 7] = (uint8_t)(output_width>>24);
bmpinfoheader[ 8] = (uint8_t)(output_height );
bmpinfoheader[ 9] = (uint8_t)(output_height>> 8);
bmpinfoheader[10] = (uint8_t)(output_height>>16);
bmpinfoheader[11] = (uint8_t)(output_height>>24);
fwrite(bmpfileheader,1,14,file.get());
fwrite(bmpinfoheader,1,40,file.get());
for (int32_t i = 0; i < output_height; i++)
for (auto f : data)
{
ASSERT_GE(f, minValue);
ASSERT_LE(f, maxValue);
histogram[int((f - minValue) / bucketSize)]++;
}
int32_t limit = data.size() / 100;
for (auto &h : histogram)
{
fwrite(reinterpret_cast<const uint8_t *>(fileData.data()) +(output_width*(output_height-i-1)*3),3,size_t(output_width),file.get());
fwrite(bmppad,1,(-3 * output_width) & 3, file.get());
ASSERT_LT(h, limit);
}
file.reset();
fmt::print(stdout, "File written to: {}\n", file_name);
}
#include <OpenVDS/OpenVDS.h>
#include <OpenVDS/VolumeDataLayoutDescriptor.h>
#include <OpenVDS/VolumeDataAxisDescriptor.h>
#include <OpenVDS/VolumeDataChannelDescriptor.h>
#include <OpenVDS/KnownMetadata.h>
#include <OpenVDS/GlobalMetadataCommon.h>
#include <OpenVDS/VolumeDataLayout.h>
#include <OpenVDS/VolumeDataAccess.h>
#include <VDS/SimplexNoiceKernel.h>
static OpenVDS::VDS *generateSimpleInMemory3DVDS(int32_t samplesX = 100, int32_t samplesY = 100, int32_t samplesZ = 100)
{
auto brickSize = OpenVDS::VolumeDataLayoutDescriptor::BrickSize_32;
int negativeMargin = 4;
int positiveMargin = 4;
int brickSize2DMultiplier = 4;
auto lodLevels = OpenVDS::VolumeDataLayoutDescriptor::LODLevels_None;
auto layoutOptions = OpenVDS::VolumeDataLayoutDescriptor::Options_None;
OpenVDS::VolumeDataLayoutDescriptor layoutDescriptor(brickSize, negativeMargin, positiveMargin, brickSize2DMultiplier, lodLevels, layoutOptions);
std::vector<OpenVDS::VolumeDataAxisDescriptor> axisDescriptors;
axisDescriptors.push_back(OpenVDS::VolumeDataAxisDescriptor(100, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_SAMPLE, "ms", 0.0f, 4.f));
axisDescriptors.emplace_back(100, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_CROSSLINE, "", 1932.f, 2536.f);
axisDescriptors.emplace_back(100, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_INLINE, "", 9985.f, 10369.f);
std::vector<OpenVDS::VolumeDataChannelDescriptor> channelDescriptors;
channelDescriptors.emplace_back(OpenVDS::VolumeDataChannelDescriptor::Format_R32, OpenVDS::VolumeDataChannelDescriptor::Components_1, AMPLITUDE_ATTRIBUTE_NAME, "", -0.10919982194900513, 0.1099749207496643);
OpenVDS::InMemoryOpenOptions options;
OpenVDS::MetadataContainer metadataContainer;
OpenVDS::Error error;
return OpenVDS::Create(options, layoutDescriptor, axisDescriptors, channelDescriptors, metadataContainer, error);
}
static void fill3DVDSWithNoice(OpenVDS::VDS *vds, int32_t channel = 0, const OpenVDS::FloatVector3 &frequency = OpenVDS::FloatVector3(0.6f, 2.f, 4.f))
{
OpenVDS::VolumeDataLayout *layout = OpenVDS::GetLayout(vds);
ASSERT_TRUE(layout);
OpenVDS::VolumeDataAccessManager *accessManager = OpenVDS::GetDataAccessManager(vds);
ASSERT_TRUE(accessManager);
OpenVDS::VolumeDataPageAccessor *pageAccessor = accessManager->CreateVolumeDataPageAccessor(layout, OpenVDS::Dimensions_012, channel, 0, 100, OpenVDS::VolumeDataAccessManager::AccessMode_Create);
ASSERT_TRUE(pageAccessor);
int32_t chunkCount = int32_t(pageAccessor->GetChunkCount());
for (int i = 0; i < chunkCount; i++)
{
OpenVDS::VolumeDataPage *page = pageAccessor->CreatePage(i);
OpenVDS::VolumeIndexer3D outputIndexer(page, 0, 0, OpenVDS::Dimensions_012, layout);
int pitch[OpenVDS::Dimensionality_Max];
void *buffer = page->GetWritableBuffer(pitch);
OpenVDS::CalculateNoise3D(buffer, OpenVDS::VolumeDataChannelDescriptor::Format_R32, &outputIndexer, frequency, 0.001f, 0.f, false, 345);
page->Release();
}
pageAccessor->Commit();
pageAccessor->SetMaxPages(0);
accessManager->FlushUploadQueue();
accessManager->DestroyVolumeDataPageAccessor(pageAccessor);
}
#include <cstdio>
#include <cstdint>
void writeBMP(int out_width, int out_height, FILE *file, const void *data, size_t size)
{
uint32_t filesize = 54 + size;
uint8_t bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
uint8_t bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
uint8_t bmppad[3] = {0,0,0};
bmpfileheader[ 2] = (uint8_t)(filesize );
bmpfileheader[ 3] = (uint8_t)(filesize>> 8);
bmpfileheader[ 4] = (uint8_t)(filesize>>16);
bmpfileheader[ 5] = (uint8_t)(filesize>>24);
bmpinfoheader[ 4] = (uint8_t)(out_width );
bmpinfoheader[ 5] = (uint8_t)(out_width>> 8);
bmpinfoheader[ 6] = (uint8_t)(out_width>>16);
bmpinfoheader[ 7] = (uint8_t)(out_width>>24);
bmpinfoheader[ 8] = (uint8_t)(out_height );
bmpinfoheader[ 9] = (uint8_t)(out_height>> 8);
bmpinfoheader[10] = (uint8_t)(out_height>>16);
bmpinfoheader[11] = (uint8_t)(out_height>>24);
fwrite(bmpfileheader,1,14,file);
fwrite(bmpinfoheader,1,40,file);
for (int32_t i = 0; i < out_height; i++)
{
fwrite(reinterpret_cast<const uint8_t *>(data) + (out_width*(out_height-i-1)*3),3,size_t(out_width),file);
fwrite(bmppad,1,(-3 * out_width) & 3, file);
}
fflush(file);
}
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