[vtkusers] Deleting VTK boxWidget

Tamim tamim.boubou at gmail.com
Thu Mar 1 05:57:37 EST 2018


Hi all,

I'm trying to move a boxWidget with the keyboard and when I press 'delete' I
want it to be deleted(obviously)

I'm having troubles about the reference count and the stuff related to it.

at first when the box is created I'm printing the reference count and it's 3
(I don't know why)

inside the 'execute' function I'm checking again and it's 2 ? (I don't know
why either)

I know that in order to properly delete an object all other objects related
to it has to be deleted right?

but I'm unable to know what is the other object that is related to the box
widget

I'm using vtktransform,vtkmatrix4x4 vtkactor, and I've tried them all but
non of them seems 

Here is the main parts of the code :

 //setters for the boxcallback class
    boxCallback->setWidget(m_boxWidget);
    boxCallback->SetActor(cube);
    boxCallback->setViewer(m_pclViewer);
    boxCallback->setID(m_referenceID);

// observers.
    m_boxWidget->AddObserver(vtkCallbackCommand::StartInteractionEvent,
boxCallback);
    m_boxWidget->AddObserver(vtkCallbackCommand::InteractionEvent,
boxCallback);

   
m_pclViewer->getRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActiveCamera()->AddObserver(vtkCallbackCommand::ModifiedEvent,
boxCallback);
   
m_pclViewer->getRenderWindow()->GetInteractor()->AddObserver(vtkCallbackCommand::KeyPressEvent,
boxCallback);


// THE EXECUTE METHOD.

 virtual void Execute(vtkObject* caller, unsigned long event, void* data)
    {
      m_orientation = new double[3];
      m_boxWidget->Print(std::cout);
      vtkSmartPointer<vtkBoxRepresentation> boxRep =
       
reinterpret_cast<vtkBoxRepresentation*>(m_boxWidget->GetRepresentation());

      vtkSmartPointer<vtkMatrix4x4> boxMatrix = m_boxTransform->GetMatrix();
      vtkSmartPointer<vtkMatrix4x4> tempMatrix =
vtkSmartPointer<vtkMatrix4x4>::New();

      switch (event) {

        case vtkCallbackCommand::KeyPressEvent: { // Keyboard event.

          m_keyPressed =
m_pclViewer->getRenderWindow()->GetInteractor()->GetKeySym(); //get the
pressed key from the user

          // Get the transform of the box and store it in a 4x4
transformation matrix
          boxRep->GetTransform(m_boxTransform);
          boxMatrix = m_boxTransform->GetMatrix();
          m_cam->GetPosition(m_camPos_a);

          // Store the latest position of the box in a new matrix with
default orientation :
          tempMatrix->SetElement(0, 3, boxMatrix->GetElement(0, 3));
          tempMatrix->SetElement(1, 3, boxMatrix->GetElement(1, 3));
          tempMatrix->SetElement(2, 3, boxMatrix->GetElement(2, 3));
          m_tempTransform->SetMatrix(tempMatrix);

          //Get the orientation of the camera ,do some adjustments and
calculate the distance for the box to be moved.
          m_orientation = m_cam->GetOrientation();
          double movingValue = getvalueByPressedKey(MOVEMENT_FACTOR);

          // initial camera orientation at Z is 90 ....made it zero so we
don't have to rotate the box
          m_orientation[2] = 90 - m_orientation[2];
          m_xDistance = movingValue * cos(m_orientation[2] * PI / 180);
          m_yDistance = movingValue * sin(m_orientation[2] * PI / 180);

          // According to current key pressed do.
          KeyboardCharacter keyPressed = getKeyboardCharacter();

          switch (keyPressed) {
            case KeyboardCharacter::UnknownKey: {
              break;
            }

            case KeyboardCharacter::TopView: {
              m_cam->SetFocalPoint(boxMatrix->GetElement(0, 3),
boxMatrix->GetElement(1, 3), boxMatrix->GetElement(2, 3));
              m_pclViewer->setCameraPosition(boxMatrix->GetElement(0, 3),
boxMatrix->GetElement(1, 3), upValue * 2 + boxMatrix->GetElement(2, 3), 1,
0, 0);
              break;
            }
            case KeyboardCharacter::FrontView: {
              m_cam->SetFocalPoint(boxMatrix->GetElement(0, 3),
boxMatrix->GetElement(1, 3), boxMatrix->GetElement(2, 3));
              m_pclViewer->setCameraPosition(-upValue +
boxMatrix->GetElement(0, 3), boxMatrix->GetElement(1, 3),
boxMatrix->GetElement(2, 3), 0, 0, 1);
              break;
            }

            case KeyboardCharacter::LeftView: {
              m_cam->SetFocalPoint(boxMatrix->GetElement(0, 3),
boxMatrix->GetElement(1, 3), boxMatrix->GetElement(2, 3));
              m_pclViewer->setCameraPosition(boxMatrix->GetElement(0, 3),
leftRightVAlue + boxMatrix->GetElement(1, 3), boxMatrix->GetElement(2, 3),
0, 0, 1);
              break;
            }
            case KeyboardCharacter::RightView: {
              m_cam->SetFocalPoint(boxMatrix->GetElement(0, 3),
boxMatrix->GetElement(1, 3), boxMatrix->GetElement(2, 3));
              m_pclViewer->setCameraPosition(boxMatrix->GetElement(0, 3),
-leftRightVAlue + boxMatrix->GetElement(1, 3), boxMatrix->GetElement(2, 3),
0, 0, 1);
              break;
            }

            case KeyboardCharacter::MoveUp: {
              m_tempTransform->Translate(m_xDistance, m_yDistance, 0);
              break;
            }
            case KeyboardCharacter::MoveDown: {
              m_tempTransform->Translate(-m_xDistance, -m_yDistance, 0);
              break;
            }
            case KeyboardCharacter::MoveLeft: {
              m_tempTransform->Translate(-m_yDistance, m_xDistance, 0);
              break;
            }
            case KeyboardCharacter::MoveRight: {
              m_tempTransform->Translate(m_yDistance, -m_xDistance, 0);
              break;
            }
            case KeyboardCharacter::MoveTop: {
              m_tempTransform->Translate(0, 0, MOVEMENT_FACTOR);
              break;
            }
            case KeyboardCharacter::MoveBottom: {
              m_tempTransform->Translate(0, 0, -MOVEMENT_FACTOR);
              break;
            }
/*// HERE IS THE IMPORTANT PART */

            case KeyboardCharacter::DeleteLabel: {
              std::cout << "widget refrence count at first:" << std::endl;
              std::cout << m_boxWidget->GetReferenceCount() << std::endl;
              // actor
              std::cout << "initial for the actor" << std::endl;
              std::cout << m_actor->GetReferenceCount() << std::endl;
             
m_pclViewer->getRenderWindow()->GetRenderers()->GetFirstRenderer()->RemoveActor(m_actor);
              std::cout << "remove actor (renderer)" << std::endl;
              std::cout << m_actor->GetReferenceCount() << std::endl;

              m_pclViewer->removeShape(m_boxID);
              std::cout << "pcl remove shape " << std::endl;
              std::cout << m_actor->GetReferenceCount() << std::endl;//
correct...the reference count is 1
             // m_actor->Delete(); 
              // end actor 
              std::cout << "widget refrence count " << std::endl; //2
              std::cout << m_boxWidget->GetReferenceCount() << std::endl;

              std::cout << "represntation refrence count " << std::endl; //3
              std::cout << boxRep->GetReferenceCount() << std::endl;

              std::cout << "boxtransform refrence count before delete: " <<
std::endl; //1
              std::cout << m_boxTransform->GetReferenceCount() << std::endl;
              
              std::cout << "temptransform refrence count before delete " <<
std::endl; //1
              std::cout << m_tempTransform->GetReferenceCount() <<
std::endl;
             
              std::cout << "boxmatrix refrence count before delete " <<
std::endl; //2
              std::cout << boxMatrix->GetReferenceCount() << std::endl;

              std::cout << "tempmatrix refrence count before delete " <<
std::endl; //1 ?
              std::cout << tempMatrix->GetReferenceCount() << std::endl;

              std::cout << "now tmp matrix, box transform, tmptransform are
deleted" << std::endl;
              tempMatrix->Delete();
              m_boxTransform->Delete();
              m_tempTransform->Delete();

              std::cout << "boxmatrix refrence count after delete " <<
std::endl; //1
              std::cout << boxMatrix->GetReferenceCount() << std::endl;
              boxMatrix->Delete();
              m_boxWidget->Delete();
              m_boxWidget->Print(std::cout);
              std::cout << "represntation refrence count after delete: " <<
std::endl;
              std::cout << boxRep->GetReferenceCount() << std::endl;
              
              std::cout << "OUTSIDE DELETE." << std::endl;
              break;
            }
            default:
              break;
          }

// REST OF THE CODE (I don't think it's relevant)

          //Store the new position after translation and restore the current
orientation of the box.
          m_tempTransform->GetPosition(m_boxPos_a);

          for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
              tempMatrix->SetElement(i, j, boxMatrix->GetElement(i, j));
            }
          }
          tempMatrix->SetElement(0, 3, m_boxPos_a[0]);
          tempMatrix->SetElement(1, 3, m_boxPos_a[1]);
          tempMatrix->SetElement(2, 3, m_boxPos_a[2]);
          m_tempTransform->SetMatrix(tempMatrix);
          // applying the transform and updating the screen
          boxRep->SetTransform(m_tempTransform);
          this->m_actor->SetUserTransform(m_tempTransform);
          m_pclViewer->getRenderWindow()->Render();
          break;
        }
      }
    }

any hint or idea would be great .




--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list