[vtkusers] collide-> GetNumberOfContacts produces 0 - with Code

Rene_Wohlgethan rwohlgethan at icqmail.com
Thu Feb 10 11:23:40 EST 2011


Hi, im trying the collision detection lib vtkBiong and i´ve got a little
problem.


I hope someone can help me. 


i try to move a cube in the direction of another cube until they collide.

But when i´m asking and printing collide->GetNumberOfContacts in the while
clause, it alway return the value zero.   I have seen that there are a lot
of post here already, but i didn´t found an answer for this. 

Has someone yet made the experience how to get this run correctly?


Thanks for your attention.

Rene


vtkTransform *trans0 = vtkTransform::New();
   vtkTransform *trans1 = vtkTransform::New();

   vtkCollisionDetectionFilter *collide =
vtkCollisionDetectionFilter::New();
   //collide->DebugOn();
   collide->SetInputConnection(0, cubeSource1->GetOutputPort());
   collide->SetTransform(0, trans0);
   collide->SetInputConnection(1, cubeSource2->GetOutputPort());
   collide->SetTransform(1, trans1);
   collide->SetBoxTolerance(0.0);
   collide->SetCellTolerance(0.0);
   collide->SetNumberOfCellsPerBucket(2);
   collide->SetCollisionModeToAllContacts();
   collide->GenerateScalarsOn();
   collide->Update();


  printf("Number Of Contacts: %d", collide->GetNumberOfContacts());
       while (collide->GetNumberOfContacts() == 0)
   {
	   collide->Update();
	   trans1->Translate(0.0, -0.1, 0.0);
	   trans1->Update();
	   printf("Number Of Contacts: %d \n", collide->GetNumberOfContacts());
	}

 


My Sourcecode looks like this:



#include "vtkProperty.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkCamera.h"
#include "vtkSphereSource.h"
#include "vtkCubeSource.h"
#include "vtkMatrix4x4.h"
#include "vtkCollisionDetectionFilter.h"
#include "vtkGlyph3D.h"
#include "vtkInteractorStyleJoystickActor.h"
#include "vtkInteractorStyleJoystickCamera.h"
#include "vtkTextActor.h"
#include "vtkCommand.h"
#include  <ostream>
#include  <vtkTransform.h>
//#include "vtkFileOutputWindow.h"

class vtkCollisionCallback : public vtkCommand
{
public:
  static vtkCollisionCallback *New() 
    { return new vtkCollisionCallback; }

  void SetTextActor(vtkTextActor *txt)
    {
    this->TextActor = txt;
    }
  void SetRenderWindow(vtkRenderWindow *renWin)
    {
    this->RenWin = renWin;
    }

  virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
      vtkCollisionDetectionFilter *collide =
reinterpret_cast<vtkCollisionDetectionFilter*>(caller);
      if (collide->GetNumberOfContacts() > 0)
        {
        sprintf(this->TextBuff, "Number Of Contacts: %d",
collide->GetNumberOfContacts());
		}
      else
        {
			sprintf(this->TextBuff, "No Contacts: %d",
collide->GetNumberOfContacts());
        }
      this->TextActor->SetInput(this->TextBuff);
      this->RenWin->Render();
    }
protected:
  vtkTextActor *TextActor;
  vtkRenderWindow *RenWin;
  char TextBuff[128];
};

int main(int, char *[])
{
   //vtkFileOutputWindow *fout =  vtkFileOutputWindow::New();
   //  fout->SetFileName("squawks.txt");
   //  fout->SetInstance(fout);
  vtkCubeSource *cubeSource1 =     vtkCubeSource::New();
  cubeSource1->SetCenter(0.0, 5.0, 0.0);
  cubeSource1->SetXLength(1.0);
  cubeSource1->SetYLength(1.0);
  cubeSource1->SetZLength(1.0);
  cubeSource1->Update();

  vtkCubeSource *cubeSource2 =     vtkCubeSource::New();
  cubeSource2->SetCenter( 0, 0, 0);
  cubeSource2->SetXLength(2.5);
  cubeSource2->SetYLength(2.5);
  cubeSource2->SetZLength(2.5);
  cubeSource2->Update();


   vtkTransform *trans0 = vtkTransform::New();
   vtkTransform *trans1 = vtkTransform::New();

   vtkCollisionDetectionFilter *collide =
vtkCollisionDetectionFilter::New();
   //collide->DebugOn();
   collide->SetInputConnection(0, cubeSource1->GetOutputPort());
   collide->SetTransform(0, trans0);
   collide->SetInputConnection(1, cubeSource2->GetOutputPort());
   collide->SetTransform(1, trans1);
   collide->SetBoxTolerance(0.0);
   collide->SetCellTolerance(0.0);
   collide->SetNumberOfCellsPerBucket(2);
   collide->SetCollisionModeToAllContacts();
   collide->GenerateScalarsOn();
   collide->Update();


  printf("Number Of Contacts: %d", collide->GetNumberOfContacts());
       while (collide->GetNumberOfContacts() == 0)
   {
	   collide->Update();
	   trans1->Translate(0.0, -0.1, 0.0);
	   trans1->Update();
	   printf("Number Of Contacts: %d \n", collide->GetNumberOfContacts());
	}

   vtkPolyDataMapper *mapper1 = vtkPolyDataMapper::New();
   mapper1->SetInputConnection(collide->GetOutputPort(0));
   vtkActor *actor1 = vtkActor::New();
   actor1->SetMapper(mapper1);
   (actor1->GetProperty())->BackfaceCullingOn();
   actor1->SetUserTransform(trans0);

   vtkPolyDataMapper *mapper2 = vtkPolyDataMapper::New();
   mapper2->SetInputConnection(collide->GetOutputPort(1));
   vtkActor *actor2 = vtkActor::New();
   actor2->SetMapper(mapper2);
   (actor2->GetProperty())->BackfaceCullingOn();
   actor2->SetUserTransform(trans1);
   
   vtkPolyDataMapper *mapper3 = vtkPolyDataMapper::New();
   mapper3->SetInputConnection(collide->GetContactsOutputPort());
   mapper3->SetResolveCoincidentTopologyToPolygonOffset();
   vtkActor *actor3 = vtkActor::New();
   actor3->SetMapper(mapper3);
   (actor3->GetProperty())->SetColor(0,0,0);
   (actor3->GetProperty())->SetLineWidth(3.0);

   vtkTextActor *txt = vtkTextActor::New();


  

   vtkRenderer *ren = vtkRenderer::New();
   ren->AddActor(actor1);
   ren->AddActor(actor2);
   ren->AddActor(actor3);
   ren->AddActor(txt);
   ren->SetBackground(0.5,0.5,0.5);

   vtkRenderWindow *renWin = vtkRenderWindow::New();
   renWin->AddRenderer(ren);

   vtkInteractorStyleJoystickCamera *istyle =
vtkInteractorStyleJoystickCamera::New();
   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
   iren->SetRenderWindow(renWin);
   iren->SetInteractorStyle(istyle);

   vtkCollisionCallback *cbCollide = vtkCollisionCallback::New();
   cbCollide->SetTextActor(txt);
   cbCollide->SetRenderWindow(renWin);
   collide->AddObserver(vtkCommand::EndEvent, cbCollide);

   renWin->Render();
   iren->Start(); 
 
   cubeSource1->Delete();
   cubeSource2->Delete();
   trans0->Delete();
   trans1->Delete();
   collide->Delete();
   //point->Delete();
   //points->Delete();
   mapper1->Delete();
   mapper2->Delete();
   mapper3->Delete();
   actor1->Delete();
   actor2->Delete();
  actor3->Delete();
   txt->Delete();
   ren->Delete();
   cbCollide->Delete();
   renWin->Delete();
   istyle->Delete();
   iren->Delete();




}
-- 
View this message in context: http://vtk.1045678.n5.nabble.com/collide-GetNumberOfContacts-produces-0-with-Code-tp3379613p3379613.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list