需求:
两个vtkLineWidget2颜色不一样,需要将选中的vtkLineWidget2显示在上层,
默认按照vtkLineWidget2创建顺序显示,即后创建的覆盖在先创建的上面。
解决:
修改Point1WorldPosition 和Point2WorldPosition的z坐标值
double zIndexDown = 0.0;
double zIndexTop = 0.1;
代码:
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkLineRepresentation.h>
#include <vtkLineWidget2.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
namespace {
vtkNew<vtkLineWidget2> lineWidget;
vtkNew<vtkLineWidget2> lineWidget2;
double zIndex = 0.0;
double zIndexTop = 0.1;
// This does the actual work.
// Callback for the interaction
class vtkLineCallback : public vtkCommand {
public:
static vtkLineCallback* New() { return new vtkLineCallback; }
virtual void Execute(vtkObject* caller, unsigned long, void*) {
vtkLineWidget2* lineWidget = reinterpret_cast<vtkLineWidget2*>(caller);
bool isSecond = false;
if (lineWidget2 == lineWidget) {
isSecond = true;
}
// Get the actual box coordinates of the line
vtkNew<vtkPolyData> polydata;
static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
->GetPolyData(polydata);
// Display one of the points, just so we know it's working
double p[3];
polydata->GetPoint(0, p);
std::cout << "is second:" << isSecond << "\n";
std::cout << "P: " << p[0] << " " << p[1] << " " << p[2] << std::endl;
double Point1WorldPosition[3];
static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
->GetPoint1WorldPosition(Point1WorldPosition);
double Point2WorldPosition[3];
static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
->GetPoint2WorldPosition(Point2WorldPosition);
std::cout << "Point1WorldPosition: " << Point1WorldPosition[0] << " "
<< Point1WorldPosition[1] << " " << Point1WorldPosition[2]
<< std::endl;
if (isSecond) {
Point1WorldPosition[2] = zIndex;
Point2WorldPosition[2] = zIndex;
}
else {
Point1WorldPosition[2] = zIndexTop;
Point2WorldPosition[2] = zIndexTop;
}
static_cast<vtkLineRepresentation*>(
lineWidget->GetRepresentation())
->SetPoint1WorldPosition(Point1WorldPosition);
static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
->SetPoint2WorldPosition(Point2WorldPosition);
std::cout << "Point1WorldPosition set: " << Point1WorldPosition[0] << " "
<< Point1WorldPosition[1] << " "
<< Point1WorldPosition[2] << std::endl;
}
vtkLineCallback() {}
};
} // namespace
int main(int, char* []) {
vtkNew<vtkNamedColors> colors;
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetRadius(.4);
sphereSource->Update();
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
// A renderer and render window
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("LineWidget2");
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
// An interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// vtkNew<vtkLineWidget2> lineWidget;
vtkNew<vtkLineRepresentation> lineRep;
lineRep->SetLineColor(0, 1, 0);
lineWidget->SetRepresentation(lineRep);
lineWidget->SetInteractor(renderWindowInteractor);
// lineWidget->CreateDefaultRepresentation();
// vtkNew<vtkLineWidget2> lineWidget