geopro/tests/render/test_voxel_build.cpp

57 lines
1.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include <cmath>
#include <limits>
#include <vtkImageData.h>
#include "actors/VoxelActor.hpp"
#include "model/ColorScale.hpp"
#include "model/Field.hpp"
using namespace geopro::core;
// buildVoxel 用小 ScalarVolume 构建:不崩、返回非空 volume、image dims/spacing/origin 正确。
TEST(VoxelBuild, BuildsVolumeFromSmallScalarVolume) {
// 2x3x4 小体,填一些值 + 一个 NaN验证哨兵透明分支
ScalarVolume vol(2, 3, 4);
double n = 0.0;
for (int k = 0; k < 4; ++k)
for (int j = 0; j < 3; ++j)
for (int i = 0; i < 2; ++i) vol.at(i, j, k) = n++;
vol.at(1, 1, 1) = std::numeric_limits<double>::quiet_NaN();
ColorScale cs;
cs.addStop(0.0, Rgba{0, 0, 255, 255});
cs.addStop(24.0, Rgba{255, 0, 0, 255});
vtkSmartPointer<vtkImageData> image;
auto volume = geopro::render::buildVoxel(
vol, cs, /*ox*/ 1.0, /*oy*/ 2.0, /*oz*/ 3.0,
/*dx*/ 1.0, /*dy*/ 1.0, /*dz*/ 0.5, /*vmin*/ 0.0, /*vmax*/ 24.0, image);
ASSERT_NE(volume.GetPointer(), nullptr);
ASSERT_NE(image.GetPointer(), nullptr);
int dims[3];
image->GetDimensions(dims);
EXPECT_EQ(dims[0], 2);
EXPECT_EQ(dims[1], 3);
EXPECT_EQ(dims[2], 4);
double sp[3];
image->GetSpacing(sp);
EXPECT_DOUBLE_EQ(sp[0], 1.0);
EXPECT_DOUBLE_EQ(sp[1], 1.0);
EXPECT_DOUBLE_EQ(sp[2], 0.5);
double org[3];
image->GetOrigin(org);
EXPECT_DOUBLE_EQ(org[0], 1.0);
EXPECT_DOUBLE_EQ(org[1], 2.0);
EXPECT_DOUBLE_EQ(org[2], 3.0);
// NaN 格应被替换为哨兵vmin-1 = -1不应残留 NaN。
EXPECT_FALSE(std::isnan(image->GetScalarComponentAsDouble(1, 1, 1, 0)));
}