grid axis

This commit is contained in:
AndreaRigoni
2026-03-18 07:23:58 +00:00
parent 92a06f6274
commit 0e8ac47fcf
3 changed files with 52 additions and 25 deletions

View File

@@ -29,6 +29,7 @@ Viewport::Viewport()
, m_Marker(vtkSmartPointer<vtkOrientationMarkerWidget>::New())
, m_CameraWidget(nullptr)
, m_Colors(vtkSmartPointer<vtkNamedColors>::New())
, m_GridAxis(Y)
{
}
@@ -267,6 +268,13 @@ bool Viewport::GetGridVisible() const
return false;
}
void Viewport::SetGridAxis(Axis axis)
{
m_GridAxis = axis;
UpdateGrid();
Render();
}
void Viewport::addProp(vtkProp* prop)
{
if (m_Renderer) {
@@ -313,24 +321,35 @@ void Viewport::UpdateGrid()
double focalPoint[3];
camera->GetFocalPoint(focalPoint);
// Align center to spacing
double centerX = std::round(focalPoint[0] / spacing) * spacing;
double centerY = std::round(focalPoint[1] / spacing) * spacing;
// Indices for the two dimensions of the grid plane
int idxH, idxV, idxN;
if (m_GridAxis == X) { idxH = 1; idxV = 2; idxN = 0; }
else if (m_GridAxis == Y) { idxH = 0; idxV = 2; idxN = 1; }
else { idxH = 0; idxV = 1; idxN = 2; }
// Number of lines: enough to cover the screen even if aspect ratio is wide
// or if we rotate. 20x20 is usually plenty.
// Align center to spacing
double centerH = std::round(focalPoint[idxH] / spacing) * spacing;
double centerV = std::round(focalPoint[idxV] / spacing) * spacing;
double centerN = 0.0; // Grid plane typically passes through the origin
// Number of lines
int numLines = 20;
double halfSize = (numLines / 2.0) * spacing;
double xmin = centerX - halfSize;
double xmax = centerX + halfSize;
double ymin = centerY - halfSize;
double ymax = centerY + halfSize;
double minH = centerH - halfSize;
double maxH = centerH + halfSize;
double minV = centerV - halfSize;
double maxV = centerV + halfSize;
// Update Plane Source
m_GridSource->SetOrigin(xmin, ymin, 0.0);
m_GridSource->SetPoint1(xmax, ymin, 0.0);
m_GridSource->SetPoint2(xmin, ymax, 0.0);
// Update Plane Source mapping axes to origin/point1/point2
double origin[3] = {0,0,0}, p1[3] = {0,0,0}, p2[3] = {0,0,0};
origin[idxH] = minH; origin[idxV] = minV; origin[idxN] = centerN;
p1[idxH] = maxH; p1[idxV] = minV; p1[idxN] = centerN;
p2[idxH] = minH; p2[idxV] = maxV; p2[idxN] = centerN;
m_GridSource->SetOrigin(origin);
m_GridSource->SetPoint1(p1);
m_GridSource->SetPoint2(p2);
m_GridSource->SetXResolution(numLines);
m_GridSource->SetYResolution(numLines);
m_GridSource->Update();