GenerateVDS.h 6.38 KB
Newer Older
1
2
3
4
5
6
7
8
#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>
Jørgen Lind's avatar
Jørgen Lind committed
9
#include <OpenVDS/SimplexNoiseKernel.h>
10
11
12
13

#include <random>

template<typename T>
Jørgen Lind's avatar
Jørgen Lind committed
14
inline void getRangeForFormat1(float &min, float &max)
15
16
17
18
19
20
{
  min = std::numeric_limits<T>::min();
  max = std::numeric_limits<T>::max();
}

template<>
Jørgen Lind's avatar
Jørgen Lind committed
21
inline void getRangeForFormat1<float>(float &min, float &max)
22
23
24
25
26
{
  min = -1.f;
  max = 1.f;
}
template<>
Jørgen Lind's avatar
Jørgen Lind committed
27
inline void getRangeForFormat1<double>(float &min, float &max)
28
29
30
31
{
  min = -1.f;
  max = 1.f;
}
Jørgen Lind's avatar
Jørgen Lind committed
32
33

static void getRangeForFormat(OpenVDS::VolumeDataChannelDescriptor::Format format, float &min, float &max)
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
  switch (format)
  {
  case OpenVDS::VolumeDataChannelDescriptor::Format_U8: getRangeForFormat1<uint8_t>(min, max); break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_U16: getRangeForFormat1<uint16_t>(min, max); break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_R32: getRangeForFormat1<float>(min, max); break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_U32: getRangeForFormat1<uint32_t>(min, max); break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_R64: getRangeForFormat1<double>(min, max); break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_U64: getRangeForFormat1<uint64_t>(min, max); break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_1Bit:
  case OpenVDS::VolumeDataChannelDescriptor::Format_Any: break;
  }
}

static OpenVDS::VDS *generateSimpleInMemory3DVDS(int32_t samplesX = 100, int32_t samplesY = 100, int32_t samplesZ = 100, OpenVDS::VolumeDataChannelDescriptor::Format format = OpenVDS::VolumeDataChannelDescriptor::Format_R32)
{
  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.emplace_back(samplesX, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_SAMPLE, "ms", 0.0f, 4.f);
  axisDescriptors.emplace_back(samplesY, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_CROSSLINE, "", 1932.f, 2536.f);
  axisDescriptors.emplace_back(samplesZ, KNOWNMETADATA_SURVEYCOORDINATE_INLINECROSSLINE_AXISNAME_INLINE,    "", 9985.f, 10369.f);

  std::vector<OpenVDS::VolumeDataChannelDescriptor> channelDescriptors;
  float rangeMin, rangeMax;
  getRangeForFormat(format, rangeMin, rangeMax);
  channelDescriptors.emplace_back(format, OpenVDS::VolumeDataChannelDescriptor::Components_1, AMPLITUDE_ATTRIBUTE_NAME, "", rangeMin, rangeMax);

  OpenVDS::InMemoryOpenOptions options;
  OpenVDS::MetadataContainer metadataContainer;
  OpenVDS::Error error;
  return OpenVDS::Create(options, layoutDescriptor, axisDescriptors, channelDescriptors, metadataContainer, error);
}

static void fill3DVDSWithNoise(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::GetAccessManager(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());

  OpenVDS::VolumeDataChannelDescriptor::Format format = layout->GetChannelFormat(channel);

  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, format, &outputIndexer, frequency, 0.021f, 0.f, true, 345);
    page->Release();
  }
  pageAccessor->Commit();
  pageAccessor->SetMaxPages(0);
  accessManager->FlushUploadQueue();
  accessManager->DestroyVolumeDataPageAccessor(pageAccessor);

}

static void fill3DVDSWithBitNoise(OpenVDS::VDS *vds, int32_t channel = 0)
{
  OpenVDS::VolumeDataLayout *layout = OpenVDS::GetLayout(vds);
  //ASSERT_TRUE(layout);
  OpenVDS::VolumeDataAccessManager *accessManager = OpenVDS::GetAccessManager(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());

  std::mt19937 gen(123);
  std::bernoulli_distribution dist(0.8);

  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];
    uint8_t *buffer = static_cast<uint8_t *>(page->GetWritableBuffer(pitch));

    int32_t min[OpenVDS::Dimensionality_Max];
    int32_t max[OpenVDS::Dimensionality_Max];
    page->GetMinMax(min, max);
    int32_t size[OpenVDS::Dimensionality_Max];
    for (int i = 0; i < OpenVDS::Dimensionality_Max; i++)
      size[i] = max[i] - min[i];

    for (int z = 0; z < size[2]; z++)
    {
      for (int y = 0; y < size[1]; y++)
      {
        for(int x = 0; x < size[0]; x++)
        {
          int32_t byteIndex = z * pitch[2] + y * pitch[1] + (x / 8);
          int32_t bitIndex = z * size[2] * size[1] * size[0] + y * size[1] * size[0] + x;
          uint8_t byte = buffer[byteIndex];
          bool value = dist(gen);
          if (value)
            byte |= uint8_t(1) << (bitIndex % 8);
          buffer[byteIndex] = byte;
        }
      }
    }

    page->Release();
  }
  pageAccessor->Commit();
  pageAccessor->SetMaxPages(0);
  accessManager->FlushUploadQueue();
  accessManager->DestroyVolumeDataPageAccessor(pageAccessor);

}