geopro/tests/render/test_anomaly.cpp

88 lines
2.6 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 <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include "actors/AnomalyActor.hpp"
#include "model/Anomaly.hpp"
using namespace geopro::core;
namespace {
vtkPolyData* polyOf(const vtkSmartPointer<vtkActor>& a) {
auto* m = vtkPolyDataMapper::SafeDownCast(a->GetMapper());
return m ? vtkPolyData::SafeDownCast(m->GetInput()) : nullptr;
}
} // namespace
// Polyline 异常开放折线n 点 → n 个点y 取负;颜色取 lineColor。
TEST(Anomaly, PolylineBuildsOpenLine) {
Anomaly a;
a.markType = AnomalyMarkType::Polyline;
a.localPts = {{0.0, 1.0}, {10.0, 2.0}, {20.0, 3.0}};
a.lineColor = "#FF0000";
a.lineWidth = 5.0;
a.dashed = true;
auto actors = geopro::render::buildAnomalies({a});
ASSERT_EQ(actors.size(), 1u);
auto* poly = polyOf(actors[0]);
ASSERT_NE(poly, nullptr);
EXPECT_EQ(poly->GetNumberOfPoints(), 3);
EXPECT_EQ(poly->GetNumberOfLines(), 1);
double p1[3];
poly->GetPoint(1, p1);
EXPECT_DOUBLE_EQ(p1[0], 10.0);
EXPECT_DOUBLE_EQ(p1[1], -2.0); // y 取负
double rgb[3];
actors[0]->GetProperty()->GetColor(rgb);
EXPECT_NEAR(rgb[0], 1.0, 1e-6);
EXPECT_NEAR(rgb[1], 0.0, 1e-6);
EXPECT_NEAR(rgb[2], 0.0, 1e-6);
}
// Polygon 异常:闭合轮廓 → 折线点数 = n+1首尾相连
TEST(Anomaly, PolygonClosesLoop) {
Anomaly a;
a.markType = AnomalyMarkType::Polygon;
a.localPts = {{0.0, 0.0}, {10.0, 0.0}, {10.0, 5.0}, {0.0, 5.0}};
auto actors = geopro::render::buildAnomalies({a});
ASSERT_EQ(actors.size(), 1u);
auto* poly = polyOf(actors[0]);
ASSERT_NE(poly, nullptr);
EXPECT_EQ(poly->GetNumberOfPoints(), 4);
EXPECT_EQ(poly->GetNumberOfLines(), 1);
// 闭合折线单元含 n+1 个 id。
vtkIdType npts = 0;
const vtkIdType* pts = nullptr;
poly->GetLines()->InitTraversal();
poly->GetLines()->GetNextCell(npts, pts);
EXPECT_EQ(npts, 5); // 4 + 回起点
}
// Point 异常:每点一个 vtkVertex。
TEST(Anomaly, PointBuildsVerts) {
Anomaly a;
a.markType = AnomalyMarkType::Point;
a.localPts = {{1.0, 1.0}, {2.0, 2.0}};
auto actors = geopro::render::buildAnomalies({a});
ASSERT_EQ(actors.size(), 1u);
auto* poly = polyOf(actors[0]);
ASSERT_NE(poly, nullptr);
EXPECT_EQ(poly->GetNumberOfVerts(), 2);
EXPECT_EQ(poly->GetNumberOfLines(), 0);
}
// 空几何异常被跳过。
TEST(Anomaly, EmptyPointsSkipped) {
Anomaly a;
a.markType = AnomalyMarkType::Polyline; // 无 localPts
auto actors = geopro::render::buildAnomalies({a});
EXPECT_TRUE(actors.empty());
}