Commit 1547de52 authored by David Gobbi's avatar David Gobbi

ENH: Picking texture pixels with vtkSurfacePicker and some code cleanup.

parent 6a1dd6d6
......@@ -19,6 +19,7 @@ IF (VTK_USE_RENDERING AND VTK_USE_DISPLAY)
rendererSource
rotations
ScalarBar
SurfacePickerWithTexture
TestFlyTo
TestLeaderActor2D
TestOnePoint
......
package require vtk
package require vtkinteraction
#
# Do picking with an actor with a texture.
# This example draws a cone at the pick point, with the color
# of the cone set from the color of the texture at the pick position.
#
# renderer and interactor
vtkRenderer ren
vtkRenderWindow renWin
renWin AddRenderer ren
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
# read the volume
vtkJPEGReader reader
reader SetFileName "$VTK_DATA_ROOT/Data/beach.jpg"
#---------------------------------------------------------
# Do the surface rendering
vtkSphereSource sphereSource
sphereSource SetRadius 100
vtkTextureMapToSphere textureSphere
textureSphere SetInputConnection [sphereSource GetOutputPort]
vtkStripper sphereStripper
sphereStripper SetInputConnection [textureSphere GetOutputPort]
sphereStripper SetMaximumLength 5
vtkPolyDataMapper sphereMapper
sphereMapper SetInputConnection [sphereStripper GetOutputPort]
sphereMapper ScalarVisibilityOff
vtkTexture sphereTexture
sphereTexture SetInputConnection [reader GetOutputPort]
vtkActor sphere
sphere SetMapper sphereMapper
sphere SetTexture sphereTexture
#---------------------------------------------------------
ren AddViewProp sphere
set camera [ren GetActiveCamera]
$camera SetFocalPoint 0 0 0
$camera SetPosition 100 400 -100
$camera SetViewUp 0 0 -1
renWin Render
#---------------------------------------------------------
# the cone should point along the Z axis
vtkConeSource coneSource
coneSource CappingOn
coneSource SetHeight 12
coneSource SetRadius 5
coneSource SetResolution 31
coneSource SetCenter 6 0 0
coneSource SetDirection -1 0 0
#---------------------------------------------------------
vtkSurfacePicker picker
picker SetTolerance 1e-6
picker PickTextureDataOn
# A function to point an actor along a vector
proc PointCone {actor nx ny nz} {
if [expr $nx < 0.0] {
$actor RotateWXYZ 180 0 1 0
$actor RotateWXYZ 180 [expr ($nx - 1.0)*0.5] [expr $ny*0.5] [expr $nz*0.5]
} else {
$actor RotateWXYZ 180 [expr ($nx + 1.0)*0.5] [expr $ny*0.5] [expr $nz*0.5]
}
}
# Pick the actor
picker Pick 104 154 0 ren
#puts [picker Print]
set p [picker GetPickPosition]
set n [picker GetPickNormal]
set ijk [picker GetPointIJK]
set data [picker GetDataSet]
set i [lindex $ijk 0]
set j [lindex $ijk 1]
set k [lindex $ijk 2]
if { [$data IsA "vtkImageData"] } {
set r [$data GetScalarComponentAsDouble $i $j $k 0]
set g [$data GetScalarComponentAsDouble $i $j $k 1]
set b [$data GetScalarComponentAsDouble $i $j $k 2]
} else {
set r 255.0
set g 0.0
set b 0.0
}
set r [expr $r / 255.0]
set g [expr $g / 255.0]
set b [expr $b / 255.0]
vtkActor coneActor1
coneActor1 PickableOff
vtkDataSetMapper coneMapper1
coneMapper1 SetInputConnection [coneSource GetOutputPort]
coneActor1 SetMapper coneMapper1
[coneActor1 GetProperty] SetColor $r $g $b
coneActor1 SetPosition [lindex $p 0] [lindex $p 1] [lindex $p 2]
PointCone coneActor1 [lindex $n 0] [lindex $n 1] [lindex $n 2]
ren AddViewProp coneActor1
renWin Render
#---------------------------------------------------------
# test-related code
proc TkCheckAbort {} {
set foo [renWin GetEventPending]
if {$foo != 0} {renWin SetAbortRender 1}
}
renWin AddObserver AbortCheckEvent {TkCheckAbort}
iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize
wm withdraw .
This diff is collapsed.
......@@ -41,6 +41,7 @@
#include "vtkPicker.h"
class vtkMapper;
class vtkTexture;
class vtkAbstractVolumeMapper;
class vtkImageActor;
class vtkPlaneCollection;
......@@ -160,12 +161,32 @@ public:
// values within the cell.
vtkGetVector3Macro(PCoords, double);
// Description:
// Get the texture that was picked. This will be null unless the picked
// prop has a texture.
vtkTexture *GetTexture() { return this->Texture; };
// Description:
// If this is "On" and if the picked prop has a texture, then the data
// returned by GetDataSet() will be the texture's data instead of the
// mapper's data. The GetPointId(), GetCellId(), GetPCoords() etc. will
// all return information for use with the texture's data. If the picked
// prop does not have any texture, then GetDataSet() will return the
// mapper's data instead and GetPointId() etc. will return information
// related to the mapper's data. The default value of PickTextureData
// is "Off".
vtkSetMacro(PickTextureData, int);
vtkBooleanMacro(PickTextureData, int);
vtkGetMacro(PickTextureData, int);
protected:
vtkSurfacePicker();
~vtkSurfacePicker();
void Initialize();
virtual void ResetPickInfo();
virtual double IntersectWithLine(double p1[3], double p2[3], double tol,
vtkAssemblyPath *path, vtkProp3D *p,
vtkAbstractMapper3D *m);
......@@ -196,8 +217,13 @@ protected:
static int ComputeSurfaceNormal(vtkDataSet *data, vtkCell *cell,
const double *weights, double normal[3]);
static int ComputeSurfaceTCoord(vtkDataSet *data, vtkCell *cell,
const double *weights, double tcoord[3]);
static void TriangleFromStrip(vtkGenericCell *cell, int subId);
void SetImageDataPickInfo(const double x[3], const int extent[6]);
double ComputeVolumeOpacity(const int xi[3], const double pcoords[3],
vtkImageData *data, vtkDataArray *scalars,
vtkPiecewiseFunction *scalarOpacity,
......@@ -221,7 +247,12 @@ protected:
double PickNormal[3];
double MapperNormal[3];
vtkTexture *Texture;
int PickTextureData;
private:
void ResetSurfacePickerInfo();
vtkGenericCell *Cell; //used to accelerate picking
vtkDoubleArray *Gradients; //used in volume picking
......
......@@ -20,7 +20,7 @@
#include "vtkVolume.h"
#include "vtkVolumeMapper.h"
vtkCxxRevisionMacro(vtkVolumePicker, "1.6");
vtkCxxRevisionMacro(vtkVolumePicker, "1.7");
vtkStandardNewMacro(vtkVolumePicker);
//----------------------------------------------------------------------------
......@@ -47,11 +47,11 @@ void vtkVolumePicker::PrintSelf(ostream& os, vtkIndent indent)
}
//----------------------------------------------------------------------------
void vtkVolumePicker::Initialize()
void vtkVolumePicker::ResetPickInfo()
{
this->Superclass::ResetPickInfo();
this->CroppingPlaneId = -1;
this->Superclass::Initialize();
}
//----------------------------------------------------------------------------
......@@ -146,35 +146,26 @@ double vtkVolumePicker::IntersectVolumeWithLine(const double p1[3],
// Set data values at the intersected cropping or clipping plane
if ((tMin = t1) < this->GlobalTMin)
{
this->ResetPickInfo();
this->DataSet = data;
this->Mapper = vmapper;
double x[3];
for (int j = 0; j < 3; j++)
{
double x = x1[j]*(1.0 - tMin) + x2[j]*tMin;
x[j] = x1[j]*(1.0 - tMin) + x2[j]*tMin;
if (planeId >= 0 && j == planeId/2)
{
x = bounds[planeId];
x[j] = bounds[planeId];
}
else if (planeId < 0 && extentPlaneId >= 0 && j == extentPlaneId/2)
{
x = extent[extentPlaneId];
}
this->MapperPosition[j] = x*spacing[j] + origin[j];
int xi = int(floor(x));
this->CellIJK[j] = xi;
this->PCoords[j] = x - xi;
if (xi == extent[2*j+1])
{ // Avoid out-of-bounds cell
this->CellIJK[j]--;
this->PCoords[j] = 1.0;
}
this->PointIJK[j] = this->CellIJK[j];
if (this->PCoords[j] >= 0.5)
{
this->PointIJK[j]++;
x[j] = extent[extentPlaneId];
}
this->MapperPosition[j] = x[j]*spacing[j] + origin[j];
}
this->PointId = data->ComputePointId(this->PointIJK);
this->CellId = data->ComputeCellId(this->CellIJK);
this->SubId = 0;
this->SetImageDataPickInfo(x, extent);
}
}
else
......
......@@ -61,7 +61,7 @@ protected:
vtkVolumePicker();
~vtkVolumePicker();
void Initialize();
virtual void ResetPickInfo();
virtual double IntersectVolumeWithLine(const double p1[3],
const double p2[3],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment