1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPointPicker.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPointPicker.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkProp3D.h"
#include "vtkMapper.h"
#include "vtkAbstractVolumeMapper.h"
#include "vtkImageMapper3D.h"
#include "vtkObjectFactory.h"
#include "vtkIdList.h"
#include "vtkCellArray.h"
#include "vtkPolyData.h"
inline vtkCellArray* GET_CELLS( int cell_type, vtkPolyData* poly_input )
{
switch( cell_type )
{
case 0:
return poly_input->GetVerts();
case 1:
return poly_input->GetLines();
case 2:
return poly_input->GetPolys();
case 3:
return poly_input->GetStrips();
}
return NULL;
}
vtkStandardNewMacro(vtkPointPicker);
vtkPointPicker::vtkPointPicker()
{
this->PointId = -1;
this->UseCells = 0;
}
double vtkPointPicker::IntersectWithLine(double p1[3], double p2[3], double tol,
vtkAssemblyPath *path, vtkProp3D *p,
vtkAbstractMapper3D *m)
{
vtkIdType numPts;
vtkIdType ptId, ptIndex, minPtId;
int i;
double ray[3], rayFactor, tMin, x[3], t, projXYZ[3], minXYZ[3];
vtkDataSet *input;
vtkMapper *mapper;
vtkAbstractVolumeMapper *volumeMapper = 0;
vtkImageMapper3D *imageMapper = 0;
// Get the underlying dataset
//
if ( (mapper=vtkMapper::SafeDownCast(m)) != NULL )
{
input = mapper->GetInput();
}
else if ( (volumeMapper=vtkAbstractVolumeMapper::SafeDownCast(m)) != NULL )
{
input = volumeMapper->GetDataSetInput();
}
else if ( (imageMapper=vtkImageMapper3D::SafeDownCast(m)) != NULL )
{
input = imageMapper->GetInput();
}
else
{
return 2.0;
}
ptId = 0;
numPts = input->GetNumberOfPoints();
if ( numPts <= ptId )
{
return 2.0;
}
// Determine appropriate info
//
for (i=0; i<3; i++)
{
ray[i] = p2[i] - p1[i];
}
if (( rayFactor = vtkMath::Dot(ray,ray)) == 0.0 )
{
vtkErrorMacro("Cannot process points");
return 2.0;
}
// For image, find the single intersection point
//
if ( imageMapper != NULL )
{
// Get the slice plane for the image and intersect with ray
double normal[4];
imageMapper->GetSlicePlaneInDataCoords(p->GetMatrix(), normal);
double w1 = vtkMath::Dot(p1, normal) + normal[3];
double w2 = vtkMath::Dot(p2, normal) + normal[3];
if (w1*w2 >= 0)
{
w1 = 0.0;
w2 = 1.0;
}
double w = (w2 - w1);
x[0] = (p1[0]*w2 - p2[0]*w1)/w;
x[1] = (p1[1]*w2 - p2[1]*w1)/w;
x[2] = (p1[2]*w2 - p2[2]*w1)/w;
// Get the one point that will be checked
ptId = input->FindPoint(x);
numPts = ptId + 1;
if (ptId < 0)
{
return VTK_DOUBLE_MAX;
}
}
// Project each point onto ray. Keep track of the one within the
// tolerance and closest to the eye (and within the clipping range).
//
double dist, maxDist, minPtDist=VTK_DOUBLE_MAX;
vtkPolyData* poly_input = vtkPolyData::SafeDownCast( input );
if ( this->UseCells && ( imageMapper == NULL ) && ( poly_input != NULL ) )
{
minPtId = -1;
tMin=VTK_DOUBLE_MAX;
for ( int iCellType = 0; iCellType<4; iCellType++ )
{
vtkCellArray* cells = GET_CELLS( iCellType, poly_input );
if (cells != NULL)
{
cells->InitTraversal();
vtkIdType n_cell_pts = 0;
vtkIdType *pt_ids = NULL;
while( cells->GetNextCell( n_cell_pts, pt_ids ) )
{
for (ptIndex=0; ptIndex<n_cell_pts; ptIndex++)
{
ptId = pt_ids[ptIndex];
input->GetPoint(ptId,x);
t = (ray[0]*(x[0]-p1[0]) + ray[1]*(x[1]-p1[1]) + ray[2]*(x[2]-p1[2])) / rayFactor;
// If we find a point closer than we currently have, see whether it
// lies within the pick tolerance and clipping planes. We keep track
// of the point closest to the line (use a fudge factor for points
// nearly the same distance away.)
//
if ( t >= 0.0 && t <= 1.0 && t <= (tMin+this->Tolerance) )
{
for(maxDist=0.0, i=0; i<3; i++)
{
projXYZ[i] = p1[i] + t*ray[i];
dist = fabs(x[i]-projXYZ[i]);
if ( dist > maxDist )
{
maxDist = dist;
}
}
if ( maxDist <= tol && maxDist < minPtDist ) // within tolerance
{
minPtId = ptId;
minXYZ[0]=x[0]; minXYZ[1]=x[1]; minXYZ[2]=x[2];
minPtDist = maxDist;
tMin = t;
}
}
}
}
}
}
}
else
{
for (minPtId=(-1),tMin=VTK_DOUBLE_MAX; ptId<numPts; ptId++)
{
input->GetPoint(ptId,x);
t = (ray[0]*(x[0]-p1[0]) + ray[1]*(x[1]-p1[1]) + ray[2]*(x[2]-p1[2])) / rayFactor;
// If we find a point closer than we currently have, see whether it
// lies within the pick tolerance and clipping planes. We keep track
// of the point closest to the line (use a fudge factor for points
// nearly the same distance away.)
//
if ( t >= 0.0 && t <= 1.0 && t <= (tMin+this->Tolerance) )
{
for(maxDist=0.0, i=0; i<3; i++)
{
projXYZ[i] = p1[i] + t*ray[i];
dist = fabs(x[i]-projXYZ[i]);
if ( dist > maxDist )
{
maxDist = dist;
}
}
if ( maxDist <= tol && maxDist < minPtDist ) // within tolerance
{
minPtId = ptId;
minXYZ[0]=x[0]; minXYZ[1]=x[1]; minXYZ[2]=x[2];
minPtDist = maxDist;
tMin = t;
}
}
}
}
// Now compare this against other actors.
//
if ( minPtId>(-1) && tMin < this->GlobalTMin )
{
this->MarkPicked(path, p, m, tMin, minXYZ);
this->PointId = minPtId;
vtkDebugMacro("Picked point id= " << minPtId);
}
return tMin;
}
void vtkPointPicker::Initialize()
{
this->PointId = (-1);
this->vtkPicker::Initialize();
}
void vtkPointPicker::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Point Id: " << this->PointId << "\n";
}
|