// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen // SPDX-License-Identifier: BSD-3-Clause #include "vtkChartXYZ.h" #include "vtkContextMouseEvent.h" #include "vtkContextScene.h" #include "vtkContextView.h" #include "vtkFloatArray.h" #include "vtkNew.h" #include "vtkPlotLine3D.h" #include "vtkPlotPoints3D.h" #include "vtkRegressionTestImage.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkRenderer.h" #include "vtkTable.h" #include "vtkUnsignedCharArray.h" #include "vtkVector.h" #include int TestChartXYZUnconstrained(int, char*[]) { // Implicitly tested here: // - clipping planes can be disabled in all dimensions. (Clipping in Z is a pain.) // - user can zoom while the box scales the same amount. (It can be confusing otherwise.) // - The plot is allowed to escape the specified geometry/margins when SetFitToScene == false vtkNew chart; vtkNew view; view->GetRenderWindow()->SetSize(600, 500); view->GetScene()->AddItem(chart); chart->SetMargins({ 40, 40, 40, 40 }); chart->SetFitToScene(false); chart->SetClippingPlanesEnabled(false); chart->SetScaleBoxWithPlot(true); vtkNew table; for (auto& name : std::array{ "X", "Y", "Z" }) { vtkNew arr; arr->SetName(name.c_str()); table->AddColumn(arr); } table->SetNumberOfRows(8); vtkIdType pointindex = 0; for (int x = 0; x < 2; ++x) { for (int y = 0; y < 2; ++y) { for (int z = 0; z < 2; ++z) { table->SetValue(pointindex, 0, x + 100); table->SetValue(pointindex, 1, y - 75); table->SetValue(pointindex, 2, z + 50); pointindex++; } } } vtkNew plot; plot->SetInputData(table); chart->AddPlot(plot); vtkNew table2; for (auto& name : std::array{ "X", "Y", "Z" }) { vtkNew arr; arr->SetName(name.c_str()); table2->AddColumn(arr); } pointindex = 0; auto p = [&table2, &pointindex](float x, float y, float z) { table2->SetValue(pointindex, 0, x * 0.8 + 100.1); table2->SetValue(pointindex, 1, y * 0.8 - 74.9); table2->SetValue(pointindex, 2, z * 0.8 + 50.1); pointindex++; }; table2->SetNumberOfRows(16); p(0, 0, 0); p(1, 0, 0); p(1, 1, 0); p(0, 1, 0); p(0, 0, 0); p(0, 0, 1); p(1, 0, 1); p(1, 0, 0); p(1, 0, 1); p(1, 1, 1); p(1, 1, 0); p(1, 1, 1); p(0, 1, 1); p(0, 1, 0); p(0, 1, 1); p(0, 0, 1); vtkNew plot2; plot2->SetInputData(table2); chart->AddPlot(plot2); view->GetRenderWindow()->SetMultiSamples(0); view->GetInteractor()->Initialize(); view->GetRenderWindow()->Render(); vtkContextMouseEvent mouseEvent; mouseEvent.SetInteractor(view->GetInteractor()); vtkVector2f pos; vtkVector2f lastPos; // rotate mouseEvent.SetButton(vtkContextMouseEvent::LEFT_BUTTON); lastPos.Set(114, 55); mouseEvent.SetLastScenePos(lastPos); pos.Set(174, 121); mouseEvent.SetScenePos(pos); vtkVector2d sP(pos.Cast().GetData()); vtkVector2d lSP(lastPos.Cast().GetData()); vtkVector2d scenePos(mouseEvent.GetScenePos().Cast().GetData()); vtkVector2d lastScenePos(mouseEvent.GetLastScenePos().Cast().GetData()); chart->MouseMoveEvent(mouseEvent); // spin mouseEvent.SetButton(vtkContextMouseEvent::LEFT_BUTTON); mouseEvent.GetInteractor()->SetShiftKey(1); lastPos.Set(0, 0); mouseEvent.SetLastScenePos(lastPos); pos.Set(20, 10); mouseEvent.SetScenePos(pos); chart->MouseMoveEvent(mouseEvent); // zoom mouseEvent.SetButton(vtkContextMouseEvent::RIGHT_BUTTON); mouseEvent.GetInteractor()->SetShiftKey(0); lastPos.Set(0, 0); mouseEvent.SetLastScenePos(lastPos); pos.Set(0, 10); mouseEvent.SetScenePos(pos); chart->MouseMoveEvent(mouseEvent); // mouse wheel zoom chart->MouseWheelEvent(mouseEvent, -1); // pan mouseEvent.SetButton(vtkContextMouseEvent::RIGHT_BUTTON); mouseEvent.GetInteractor()->SetShiftKey(1); lastPos.Set(0, 0); mouseEvent.SetLastScenePos(lastPos); pos.Set(100, 100); mouseEvent.SetScenePos(pos); chart->MouseMoveEvent(mouseEvent); view->GetRenderWindow()->Render(); view->GetInteractor()->Start(); return EXIT_SUCCESS; }