[VTK] Bounding box and minimum bounding box

I’m very happy to meet you in Xueyi’s CSDN, and give you some sweets

Welcome to joinXueyi Community-CSDN Community Cloud

Foreword

This article shares the technology of creating bounding boxes and minimum bounding boxes in VTK, as well as extended applications. I hope it will be helpful to all of you!

Thank you all for your likes and follows. Xiaoyi will continue to work hard to share and make progress together!

Your likes are my motivation(^U^)ノ~YO

Table of Contents

Preface

1. Use vtkOutlineFilter to calculate the bounding box of the object

2. Use vtkOBBTree to calculate the minimum bounding box of the object

3. Calculate the coordinate system of the object based on the minimum bounding box, and then rotate and move based on the object coordinate system

in conclusion:


1. Use vtkOutlineFilter to calculate the bounding box of the object

Use OutLine to calculate the bounding box, as shown in the red box in the picture above

 vtkSmartPointer<vtkOutlineFilter> outline =
            vtkSmartPointer<vtkOutlineFilter>::New();
        outline->SetInputData(dynamic_cast<vtkPolyData*>(picker->GetActor()->GetMapper()->GetInput()));

        vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
        outlineMapper->SetInputConnection(outline->GetOutputPort());
        vtkSmartPointer<vtkActor> outlineActor =
            vtkSmartPointer<vtkActor>::New();
        outlineActor->SetMapper(outlineMapper);
        outlineActor->GetProperty()->SetColor(1, 0, 0);
        m_viewer->renderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(outlineActor);

2. Use vtkOBBTree to calculate the minimum bounding box of the object

Use OBBTree to calculate the minimum bounding box, as shown in the green bounding box in the above figure

 int maxLevel = 5;
        //Create the tree
        auto obbTree = vtkSmartPointer<vtkOBBTree>::New();
        obbTree->SetDataSet(dynamic_cast<vtkPolyData*>(picker->GetActor()->GetMapper()->GetInput()));
        obbTree->SetMaxLevel(maxLevel);
        obbTree->BuildLocator();

        double corner[3] = { 0.0, 0.0 ,0.0 };
        double max[3] = { 0.0, 0.0 ,0.0 };
        double mid[3] = { 0.0, 0.0, 0.0 };
        double min[3] = { 0.0, 0.0, 0.0 };
        double size[3] = { 0.0, 0.0, 0.0 };

        obbTree->ComputeOBB(dynamic_cast<vtkPolyData*>(picker->GetActor()->GetMapper()->GetInput()), corner, max, mid, min, size);
        //Initialize the representation
        auto polydata = vtkSmartPointer<vtkPolyData>::New();
        obbTree->GenerateRepresentation(0, polydata);

        auto obbtreeMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
        obbtreeMapper->SetInputData(polydata);

        auto obbtreeActor = vtkSmartPointer<vtkActor>::New();
        obbtreeActor->SetMapper(obbtreeMapper);
        obbtreeActor->GetProperty()->SetInterpolationToFlat();
        obbtreeActor->GetProperty()->SetOpacity(.5);
        obbtreeActor->GetProperty()->EdgeVisibilityOn();
        obbtreeActor->GetProperty()->SetColor(0.,1.,0.);
        m_viewer->renderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(obbtreeActor);

The geometric data of the green bounding box is

 x[0] = OBBptr->Corner[0];
    x[1] = OBBptr->Corner[1];
    x[2] = OBBptr->Corner[2];
    cubeIds[0] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[0][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[0][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[0][2];
    cubeIds[1] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[1][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[1][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[1][2];
    cubeIds[2] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[0][0] + OBBptr->Axes[1][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[0][1] + OBBptr->Axes[1][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[0][2] + OBBptr->Axes[1][2];
    cubeIds[3] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[2][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[2][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[2][2];
    cubeIds[4] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[0][0] + OBBptr->Axes[2][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[0][1] + OBBptr->Axes[2][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[0][2] + OBBptr->Axes[2][2];
    cubeIds[5] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[1][0] + OBBptr->Axes[2][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[1][1] + OBBptr->Axes[2][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[1][2] + OBBptr->Axes[2][2];
    cubeIds[6] = pts->InsertNextPoint(x);

    x[0] = OBBptr->Corner[0] + OBBptr->Axes[0][0] + OBBptr->Axes[1][0] + OBBptr->Axes[2][0];
    x[1] = OBBptr->Corner[1] + OBBptr->Axes[0][1] + OBBptr->Axes[1][1] + OBBptr->Axes[2][1];
    x[2] = OBBptr->Corner[2] + OBBptr->Axes[0][2] + OBBptr->Axes[1][2] + OBBptr->Axes[2][2];
    cubeIds[7] = pts->InsertNextPoint(x);

The topological data is

 ptIds[0] = cubeIds[0];
    ptIds[1] = cubeIds[2];
    ptIds[2] = cubeIds[3];
    ptIds[3] = cubeIds[1];
    polys->InsertNextCell(4, ptIds);

    ptIds[0] = cubeIds[0];
    ptIds[1] = cubeIds[1];
    ptIds[2] = cubeIds[5];
    ptIds[3] = cubeIds[4];
    polys->InsertNextCell(4, ptIds);

    ptIds[0] = cubeIds[0];
    ptIds[1] = cubeIds[4];
    ptIds[2] = cubeIds[6];
    ptIds[3] = cubeIds[2];
    polys->InsertNextCell(4, ptIds);

    ptIds[0] = cubeIds[1];
    ptIds[1] = cubeIds[3];
    ptIds[2] = cubeIds[7];
    ptIds[3] = cubeIds[5];
    polys->InsertNextCell(4, ptIds);

    ptIds[0] = cubeIds[4];
    ptIds[1] = cubeIds[5];
    ptIds[2] = cubeIds[7];
    ptIds[3] = cubeIds[6];
    polys->InsertNextCell(4, ptIds);

    ptIds[0] = cubeIds[2];
    ptIds[1] = cubeIds[6];
    ptIds[2] = cubeIds[7];
    ptIds[3] = cubeIds[3];
    polys->InsertNextCell(4, ptIds);

3. Calculate the coordinate system of the object based on the minimum bounding box, and then rotate and move based on the object coordinate system

For a detailed introduction, see VTK-Rotation Gadget_Xue Yi’s Blog-CSDN Blog

Conclusion:

Summarize the method of calculating object bounding box in VTK and the extended usage of minimum bounding box.

Thank you all for your likes and follows. Xiaoyi will continue to work hard to share and make progress together!

Your appreciation is my greatest motivation(^U^)ノ~YO

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 54,249 people are learning the system