vtkBoarderWidget及图片坐标包含计算


开发环境:

  1. Windows 11 家庭中文版
  2. Microsoft Visual Studio Community 2019
  3. VTK-9.3.0.rc0
  4. vtk-example

demo解决问题:移动图片到坐标轴的中心,创建一个vtkBoarderWidget控件,移动控件,计算控件与图片的包含关系
在这里插入图片描述

关键点:

  1. 代码中使用 vtkImageChangeInformation 类来对图像进行中心偏移处理,将图像中心移动到坐标原点 (0, 0)。
  2. vtkBoarderWidget、vtkImageData坐标包含计算:
  vtkBorderWidget* borderWidget = reinterpret_cast<vtkBorderWidget*>(caller);

  // Get the world coordinates of the two corners of the box.
  vtkCoordinate* lowerLeftCoordinate =
      static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
          ->GetPositionCoordinate();
  double* lowerLeft =
      lowerLeftCoordinate->GetComputedWorldValue(this->Renderer);
  std::cout << "Lower left coordinate:  " << fmt(lowerLeft[0]) << ","
            << fmt(lowerLeft[1]) << "," << fmt(lowerLeft[2]) << std::endl;

  vtkCoordinate* upperRightCoordinate =
      static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
          ->GetPosition2Coordinate();
  double* upperRight =
      upperRightCoordinate->GetComputedWorldValue(this->Renderer);
  std::cout << "Upper right coordinate: " << fmt(upperRight[0]) << ","
            << fmt(upperRight[1]) << "," << fmt(upperRight[2]) << std::endl;

  double* bounds = this->ImageActor->GetBounds();
  double xmin = bounds[0];
  double xmax = bounds[1];
  double ymin = bounds[2];
  double ymax = bounds[3];

  if ((lowerLeft[0] > xmin) && (upperRight[0] < xmax) &&
      (lowerLeft[1] > ymin) && (upperRight[1] < ymax))
  {
    // std::cout << "box is inside image" << std::endl;
    // std::cout << "xmin: " << xmin << " xmax: " << xmax << " ymin: " << ymin
    // << " ymax: " << ymax << std::endl;
  }
  else
  {
    std::cout << "box is NOT inside image" << std::endl;
  }

prj name: CenterAnImage

#include <vtkBorderRepresentation.h>
#include <vtkBorderWidget.h>
#include <vtkCommand.h>
#include <vtkImageActor.h>
#include <vtkImageChangeInformation.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

#include <iomanip>
#include <iostream>
#include <sstream>

namespace {

class vtkBorderCallback : public vtkCommand
{
public:
  vtkBorderCallback()
  {
  }

  static vtkBorderCallback* New()
  {
    return new vtkBorderCallback;
  }

  virtual void Execute(vtkObject* caller, unsigned long, void*);

  void SetRenderer(vtkSmartPointer<vtkRenderer> renderer)
  {
    this->Renderer = renderer;
  }
  void SetImageActor(vtkSmartPointer<vtkImageActor> actor)
  {
    this->ImageActor = actor;
  }

private:
  vtkSmartPointer<vtkRenderer> Renderer;
  vtkSmartPointer<vtkImageActor> ImageActor;
};

} // namespace

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  if (argc != 2)
  {
    std::cerr << "Usage: " << argv[0]
              << " Required parameters: Filename e.g. Ox.jpg" << std::endl;
    return EXIT_FAILURE;
  }

  // Read the image.
  vtkNew<vtkImageReader2Factory> readerFactory;
  vtkSmartPointer<vtkImageReader2> imageReader;
  imageReader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
  imageReader->SetFileName(argv[1]);
  imageReader->Update();

  // Shift the image center to (0,0).
  int dims[3];
  imageReader->GetOutput()->GetDimensions(dims);

  vtkNew<vtkImageChangeInformation> changeInformation;
  changeInformation->SetInputConnection(imageReader->GetOutputPort());
  changeInformation->CenterImageOn();
  changeInformation->Update();

  vtkSmartPointer<vtkImageData> image = changeInformation->GetOutput();

  vtkNew<vtkImageActor> imageActor;
  imageActor->GetMapper()->SetInputData(image);

  vtkNew<vtkRenderWindow> renderWindow;

  vtkNew<vtkRenderWindowInteractor> interactor;

  vtkNew<vtkInteractorStyleImage> style;
  interactor->SetInteractorStyle(style);

  vtkNew<vtkBorderWidget> borderWidget;
  borderWidget->SetInteractor(interactor);
  static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
      ->GetBorderProperty()
      ->SetColor(colors->GetColor3d("Chartreuse").GetData());
  borderWidget->SelectableOff();

  interactor->SetRenderWindow(renderWindow);

  // Setup both renderers.
  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("Peru").GetData());
  renderWindow->AddRenderer(renderer);

  renderer->AddActor(imageActor);

  renderer->ResetCamera();

  vtkNew<vtkBorderCallback> borderCallback;
  borderCallback->SetRenderer(renderer);
  borderCallback->SetImageActor(imageActor);

  borderWidget->AddObserver(vtkCommand::InteractionEvent, borderCallback);
  borderWidget->On();
  renderWindow->SetWindowName("CenterAnImage");
  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

namespace {

void vtkBorderCallback::Execute(vtkObject* caller, unsigned long, void*)
{
  // Use this to format the output.
  auto fmt = [](const double& x) {
    std::ostringstream os;
    os << std::fixed << std::setprecision(2) << std::setw(8) << x;
    return os.str();
  };

  vtkBorderWidget* borderWidget = reinterpret_cast<vtkBorderWidget*>(caller);

  // Get the world coordinates of the two corners of the box.
  vtkCoordinate* lowerLeftCoordinate =
      static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
          ->GetPositionCoordinate();
  double* lowerLeft =
      lowerLeftCoordinate->GetComputedWorldValue(this->Renderer);
  std::cout << "Lower left coordinate:  " << fmt(lowerLeft[0]) << ","
            << fmt(lowerLeft[1]) << "," << fmt(lowerLeft[2]) << std::endl;

  vtkCoordinate* upperRightCoordinate =
      static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
          ->GetPosition2Coordinate();
  double* upperRight =
      upperRightCoordinate->GetComputedWorldValue(this->Renderer);
  std::cout << "Upper right coordinate: " << fmt(upperRight[0]) << ","
            << fmt(upperRight[1]) << "," << fmt(upperRight[2]) << std::endl;

  double* bounds = this->ImageActor->GetBounds();
  double xmin = bounds[0];
  double xmax = bounds[1];
  double ymin = bounds[2];
  double ymax = bounds[3];

  if ((lowerLeft[0] > xmin) && (upperRight[0] < xmax) &&
      (lowerLeft[1] > ymin) && (upperRight[1] < ymax))
  {
    // std::cout << "box is inside image" << std::endl;
    // std::cout << "xmin: " << xmin << " xmax: " << xmax << " ymin: " << ymin
    // << " ymax: " << ymax << std::endl;
  }
  else
  {
    std::cout << "box is NOT inside image" << std::endl;
  }
}

} // namespace

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值