[vtkusers] vtkSelectVisiblePoints problem: random results...

Jonathan.Bailleul Jonathan.Bailleul at greyc.ismra.fr
Tue Jan 13 10:39:57 EST 2004


Dear all,

I expected using vtkSelectVisiblePoints in order to display continuously
the ids of polydata points of one shape (as a start).
When I use it on a program loading a single polydata, then it works
perfectly: the labels displayed only correspond to currently visible
points.

But when I use it with a second polydata (without anything), then even
the points on the non visible face - eg 46 - have their label displayed!
(http://www.greyc.ismra.fr/~bailleul/pub/visible.jpg). Note that if I
get close enough, these undesired labels disappear
(http://www.greyc.ismra.fr/~bailleul/pub/visible-closer.jpg). Please
also note that I used a cone glyph on every point.


Do you have any idea of the sourcee of the problem? I joined along the
faulty code...
In advance, many thanks for any help...



PS: If you wonder why I'm needing 2 files loaded concurrently, I try to
check the quality of landmarking of a training set.



-- 
-----------------------------------
Jonathan BAILLEUL, Doctorant
GREYC Image - Université de Caen 
http://www.greyc.ismra.fr/~bailleul
-------------- next part --------------
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkActor2D.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkPointPicker.h"
#include "vtkCellPicker.h"
#include "vtkPicker.h"
#include "vtkPolyDataReader.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkCallbackCommand.h"
#include "vtkIdList.h"
#include "vtkConeSource.h"
#include "vtkSphereSource.h"
#include "vtkGlyph3D.h"
#include "vtkCellArray.h"
#include "vtkTextSource.h"
#include "vtkTextProperty.h"
#include "vtkTextMapper.h"
#include "vtkVectorText.h"
#include "vtkCoordinate.h"
#include "vtkIdFilter.h"
#include "vtkLabeledDataMapper.h"
#include "vtkSelectVisiblePoints.h"


/* GLOS (Graphic Library in Open Source), an ANSI Common Lisp OpenGL subset.
   Copyright (C) 2000 the GLOS development team (http://glos.sourceforge.net) */
// using namespace std;


/* cf also ./labeledMesh.* for good stuff */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "Vector.h"


/* -------------------------------------------------------------------------- */
/* Global vars / functions  */
/* -------------------------------------------------------------------------- */





#define BG_R 0.2
#define BG_G 0.3
#define BG_B 0.4
#define DEBUG 0

// scale the actors (text & glyph) to match size of structure
float G_SIZESCALE = 0.1;



vtkRenderer* G_REN;
vtkRenderWindow* G_WREN;
vtkRenderWindowInteractor* G_IREN;
vtkPointPicker* G_PPICKER;
vtkCellPicker* G_CPICKER;
vtkPolyData* G_POLY; // last polydata source loaded
vtkGlyph3D* G_SELGLYPH; 
vtkActor* G_SELGLYPHACT; 
vtkActor *G_txtActor1; vtkActor *G_txtActor2; vtkActor *G_txtActor3;
vtkPolyDataMapper* G_SELGLYPHMAP;
// vtkPolyDataMapper* G_MAP;
char* G_FILENAME;
vtkPoints *G_POINTS; // points of polydata object
int G_NBPOINTS;





static void
usage(int argc, char **argv) 
{
  if ((((argc - 1) % 4) != 0) || (argc <= 1))
    {
      printf("Display .vtk object files for annotation purposes \n");
      printf("Usage: %s [ <vtk object filename> <object color components from 0-255: R G B> ] + \n", argv[0]);
      printf("Behaviour undefined if several objects loaded");
      exit(1);
    }  
}


/* -------------------------------------------------------------------------- */
/* actors creation for representing objects */
/* -------------------------------------------------------------------------- */

vtkActor* 
makeActor(char* filename, float r, float g, float b)
{
  // create object geometry
  vtkPolyDataReader *reader = vtkPolyDataReader::New();
  reader -> SetFileName(filename);
  reader -> Update();

  G_POLY = reader -> GetOutput();


  // map to graphics library
  vtkPolyDataMapper *map = vtkPolyDataMapper::New();
  map -> SetInput(reader -> GetOutput());


  // retrieve size of object diagonal to adjust (global) scale factor
  float ref_bounds[6]; map -> GetBounds(ref_bounds); 
  // retrieves norm of the diagonal of the bounding box of reference
  double diagsize = Vector<float>(Point<float>(ref_bounds[0], ref_bounds[2], ref_bounds[4]),
				  Point<float>(ref_bounds[1], ref_bounds[3], ref_bounds[5])).norm();  
  G_SIZESCALE = diagsize / 30;


  // actor coordinates geometry, properties, transformation
  vtkActor *actor = vtkActor::New();
  actor -> SetMapper(map);
  actor -> GetProperty() -> SetDiffuseColor(r, g, b);
  actor -> GetProperty() -> SetSpecularPower(50);
  actor -> GetProperty() -> SetSpecular(0.5);
  actor -> GetProperty() -> SetDiffuse(0.8);
  actor -> GetProperty() -> SetInterpolationToFlat(); //Gouraud, Phong

  vtkProperty *backprop = vtkProperty::New();
  backprop -> SetDiffuseColor(r, g, b);
  backprop -> SetDiffuse(0.2);
    
  actor -> SetBackfaceProperty(backprop);

  return actor;
}


vtkActor* 
makeActor_255(char* filename, int r, int g, int b)
{
  return makeActor(filename, (float) r / 255, (float) g / 255, (float) b / 255);
}



/* -------------------------------------------------------------------------- */
/* Events */
/* -------------------------------------------------------------------------- */


void 
PrintPoint(float* p, ostream &stream)
{
  stream << " [" << p[0] << "; " << p[1] << "; " << p[2] << "] "; 
}


/* -------------------------------------------------------------------------- */
/* Main program */
/* -------------------------------------------------------------------------- */


int 
main(int argc, char* argv[])
{
  usage(argc, argv);  


  int objects = (argc - 1) / 4;
  int r, g, b, index;
  

  // a renderer and render window
  vtkRenderer *ren1 = vtkRenderer::New(); G_REN = ren1;
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  G_WREN = renWin;
  renWin -> AddRenderer(ren1);
  renWin -> SetSize(450, 450);
  renWin -> SetWindowName(argv[1]); G_FILENAME = argv[1];


  // an interactor
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); G_IREN = iren;
  iren -> SetRenderWindow(renWin);


  /* Create and attach actors for every input vtk file */
  cout << endl << "Number of objects:" << objects;
  // add the actor to the scene
  for (int i = 0; i < objects; i++)
    {
      index = 1 + 4 * i;
      r = atoi(argv[index + 1]);
      g = atoi(argv[index + 2]);
      b = atoi(argv[index + 3]);
      ren1 -> AddActor(makeActor_255(argv[index], r, g, b));
      cout << endl << "Object " << i << ": "<< argv[index] << " " << r << " " << g << " " << b; 
    }
  cout << endl;
  
  
  /* Generate cone glyphs for vertices */

  vtkConeSource* cone = vtkConeSource::New();
  vtkSphereSource *sphere = vtkSphereSource::New();
  vtkGlyph3D* glyph = vtkGlyph3D::New();
  vtkGlyph3D* selglyph = vtkGlyph3D::New(); G_SELGLYPH = selglyph;
  vtkActor *spikeActor1 = vtkActor::New();

  vtkPolyDataMapper *spikeMapper1 = vtkPolyDataMapper::New(); 

  
  selglyph -> SetSource(sphere -> GetOutput());
  selglyph -> SetVectorModeToUseNormal(); 
  selglyph -> SetScaleModeToScaleByVector();
  selglyph -> SetScaleFactor(0.40 * G_SIZESCALE);

  // the main characteristic of a glyph is its ability to
  // automatically follow the attached points. all glyphs are copies
  // of the same object.
  glyph -> SetInput(G_POLY); // source points to glyph G_POLY: last vtkPolydata object read
  glyph -> SetSource(cone -> GetOutput()); // glyph object
  glyph -> ScalingOn();
  glyph -> SetVectorModeToUseNormal(); 
  glyph -> SetScaleModeToScaleByVector();
  glyph -> SetScaleFactor(0.40 * G_SIZESCALE);


  spikeMapper1 -> SetInput(glyph -> GetOutput());
  spikeActor1 -> SetMapper(spikeMapper1);

  


  /* 2d actors for landmark annotation creation and attachment */
  vtkActor2D* ptslabels_act = vtkActor2D::New();
  {
    G_POINTS = G_POLY -> GetPoints();
    G_NBPOINTS = G_POLY -> GetNumberOfPoints();
    vtkTextProperty *tp = vtkTextProperty::New();
    int i;
    char msg[50];



    /* numbering hypothesis 2 */

    // Generate data arrays containing point and cell ids
    vtkIdFilter* pts_ids = vtkIdFilter::New();
    pts_ids -> SetInput(G_POLY);
    pts_ids -> PointIdsOn();
    pts_ids -> CellIdsOn();
    pts_ids -> FieldDataOn();
    //pts_ids -> Execute();

    // Create labels for points
    vtkSelectVisiblePoints* vis_pts = vtkSelectVisiblePoints::New();
    vis_pts -> SetInput(pts_ids -> GetOutput());
    vis_pts -> SetRenderer(ren1);
    //vis_pts -> SelectionWindowOn(); 
    //visPts.SetSelection(xmin, xmin + xLength, ymin, ymin + yLength)


    //Create the mapper to display the point ids.  Specify the format
    //to use for the labels.  Also create the associated actor.
    vtkLabeledDataMapper* ldm = vtkLabeledDataMapper::New();
    ldm -> SetInput(vis_pts -> GetOutput());
    ldm -> SetLabelFormat("%g");
    ldm -> SetLabelModeToLabelFieldData();

    ptslabels_act -> SetMapper(ldm);
    
    

  }



  
  /* rendering setup and actors association */



  ren1 -> AddActor(spikeActor1);

  ren1 -> AddActor2D(ptslabels_act);

  ren1 -> SetBackground(BG_R, BG_G, BG_B); // fixed background color

  ren1 -> GetActiveCamera() -> SetViewUp(0, -1, 0);
  ren1 -> GetActiveCamera() -> Azimuth(180);

  // render an image (lights and cameras are created automatically)
  renWin -> Render();

  // begin mouse interaction
  iren -> Initialize();
  iren -> Start();

  return EXIT_SUCCESS;
}


More information about the vtkusers mailing list