[vtkusers] Weird vtkClipPolyData error when using an *.obj

agerlach agerlach at gmail.com
Thu May 19 13:55:34 EDT 2011


OK. Here is an attempt at an example of my problem. Unfortunately for some
reason I cannot get an observer to work to identify the end of the
tracerWidet event. I'm sure it is something simple. So, may need some help
getting that to work before I will be able to demonstrate my problem.
(Please be patient, I'm a newbie) In my main application it uses a gui so I
use the gui button press to turn tracerWidet off and then compute the area.

To run place the following ply and obj model and texture in the same folder
as the exe. 
http://www.intellimedsystems.com/files/AreaTruth_T1.jpg Texture 
http://www.intellimedsystems.com/files/AreaTruth.ply ply model 
http://www.intellimedsystems.com/files/AreaTruth.obj obj model 

Run and type y to use the obj otherwise the ply will be loaded. 

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkImageTracerWidget.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkSelectPolyData.h>
#include <vtkClipPolyData.h>
#include <vtkMassProperties.h>
#include <vtkPLYReader.h>
#include <vtkOBJReader.h>
#include <vtkJPEGReader.h>
#include <vtkTexture.h>
#include <vtkCallbackCommand.h>


class vtkITWCallback : public vtkCommand

{

public:
    static vtkITWCallback *New()
    { return new vtkITWCallback; }

    virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
        vtkImageTracerWidget *tracer =
                reinterpret_cast&lt;vtkImageTracerWidget*&gt;(caller);

        if(tracer->IsClosed()) {
            vtkSmartPointer<vtkPolyData> tracePolyData =
                    vtkSmartPointer<vtkPolyData>::New();

            tracer->GetPath(tracePolyData);
            std::cout << "There are " << tracePolyData->GetNumberOfPoints()
<< " points in the path." << std::endl;

            vtkSmartPointer<vtkSelectPolyData> loop =
vtkSmartPointer<vtkSelectPolyData>::New();
            loop->SetInput(polyData);
            loop->SetLoop(tracePolyData->GetPoints());
            loop->SetSelectionModeToSmallestRegion();
            loop->Update();

            vtkSmartPointer<vtkClipPolyData> clip =
vtkSmartPointer<vtkClipPolyData>::New();
            clip->SetInput(loop->GetOutput());
            clip->Update();

            vtkSmartPointer<vtkMassProperties> mass =
vtkSmartPointer<vtkMassProperties>::New();
            mass->SetInput(clip->GetOutput());
            std::cout << "Area = " << mass->GetSurfaceArea() << "[mm]";
        }
        else
            std::cout << "Trace not closed";
    }

    vtkITWCallback():polyData(0){}

    vtkPolyData *polyData;


};

int main(int argc, char *argv[])
{
  bool usePLY = true;
  bool hasTexture = false;

  std::string input = " ";
  std::cout << "Use obj (y/n):\n>";
  std::getline(cin,input);

  if(input == "y" || input == "Y")
      usePLY = false;

  vtkSmartPointer<vtkPolyData> polyData;
  polyData = vtkSmartPointer<vtkPolyData>::New();

  vtkSmartPointer<vtkTexture> texture =
          vtkSmartPointer<vtkTexture>::New();

  if(usePLY)
  {
      std::cout << "Using *.ply";
      vtkSmartPointer<vtkPLYReader> plyReader =
              vtkSmartPointer<vtkPLYReader>::New();

      plyReader->SetFileName("AreaTruth.ply");
      plyReader->Update();
      polyData = plyReader->GetOutput();

  }
  else
  {
      std::cout << "Using *.obj";
      vtkSmartPointer<vtkOBJReader> objReader =
              vtkSmartPointer<vtkOBJReader>::New();

      objReader->SetFileName("AreaTruth.obj");
      objReader->Update();
      polyData = objReader->GetOutput();

      hasTexture = true;

      vtkSmartPointer<vtkJPEGReader> jpgReader =
              vtkSmartPointer<vtkJPEGReader>::New();
      jpgReader->SetFileName("AreaTruth_T1.jpg");
      jpgReader->Update();


      texture->SetInputConnection(jpgReader->GetOutputPort());
      texture->InterpolateOn();


  }

   // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInput(polyData);
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  if(hasTexture)
      actor->SetTexture(texture);

  // A renderer and render window
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  renderer->AddActor(actor);

  // An interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
    vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

  renderWindowInteractor->SetInteractorStyle(style);

  vtkSmartPointer<vtkCallbackCommand> callback =
    vtkSmartPointer<vtkCallbackCommand>::New();
  callback->SetCallback(CallbackFunction);

  vtkSmartPointer<vtkITWCallback> itwCallback =
          vtkSmartPointer<vtkITWCallback>::New();
  itwCallback->polyData = polyData;



  vtkSmartPointer<vtkImageTracerWidget> tracerWidget;
  tracerWidget = vtkSmartPointer<vtkImageTracerWidget>::New();
  tracerWidget->SetInteractor(renderWindowInteractor);
  tracerWidget->SetViewProp(actor);
  tracerWidget->AutoCloseOn();
  tracerWidget->AddObserver(vtkCommand::EndInteractionEvent, itwCallback);
  tracerWidget->On();
  renderWindow->Render();

  renderWindowInteractor->Initialize();
  renderWindow->Render();




  // Begin mouse interaction

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}


--
View this message in context: http://vtk.1045678.n5.nabble.com/Weird-vtkClipPolyData-error-when-using-an-obj-tp4408314p4410434.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list