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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPickingManager.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.
=========================================================================*/
/*==============================================================================
Library: MSVTK
Copyright (c) Kitware Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// VTK includes
#include "vtkAbstractPicker.h"
#include "vtkAbstractPropPicker.h"
#include "vtkCamera.h"
#include "vtkCallbackCommand.h"
#include "vtkCommand.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPickingManager.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkTimeStamp.h"
// STL includes
#include <algorithm>
#include <limits>
#include <map>
#include <vector>
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkPickingManager);
//------------------------------------------------------------------------------
class vtkPickingManager::vtkInternal
{
public:
vtkInternal(vtkPickingManager* external);
~vtkInternal();
// Callback used to update the current time
// of the manager when an event occurs in the RenderWindowInteractor.
// Time is used to know if the cached information is still valid or obsolete.
static void UpdateTime(vtkObject *caller,
unsigned long event,
void *clientData,
void *callData);
// Select the best picker based on various criterias such as z-depth,
// 2D overlay and/or distance to picked point.
vtkAbstractPicker* SelectPicker();
// Compute the selection. The current implementation use the distance
// between the world coordinates of a pick to the camera's ones.
vtkAbstractPicker* ComputePickerSelection(double X, double Y, double Z,
vtkRenderer* renderer);
// Check if a given Observator is associated with a given Picker
bool IsObjectLinked(vtkAbstractPicker* picker, vtkObject* object);
// Create a new list of associated observers
void CreateDefaultCollection(vtkAbstractPicker* picker, vtkObject* object);
// Instead of a vtkCollection we are using a vector of a vtkSmartPointer
// containing vtkObject to allow using 0 as a valid value because it is
// allowed the return a picker event if he is not associated to a specific
// object.
// This is related with the capacity when a picker associated with a given
// object does not manage others object,
// it will automatically be removed from the list as well.
typedef std::vector<vtkSmartPointer<vtkObject> > CollectionType;
// For code clearance and performances during the computation std::map is
// used instead of a vector of pair. Nevertheless, it makes internally use of
// vtkSmartPointer and this class does not overload the order operators;
// therefore to following functor has to be implemented to keep the data
// structure consistent.
struct less_smartPtrPicker
{
bool operator () (const vtkSmartPointer<vtkAbstractPicker>& first,
const vtkSmartPointer<vtkAbstractPicker>& second) const
{
return first.GetPointer() < second.GetPointer();
}
};
typedef std::map<vtkSmartPointer<vtkAbstractPicker>,
CollectionType, less_smartPtrPicker > PickerObjectsType;
typedef std::pair<vtkSmartPointer<vtkAbstractPicker>,
CollectionType> PickerObjectsPairType;
// Associate a given vtkObject to a particular picker.
void LinkPickerObject(const PickerObjectsType::iterator& it,
vtkObject* object);
// Predicate comparing a vtkAbstractPicker*
// and a vtkSmartPointer<vtkAbstractPicker> using the PickerObjectsType.
// As we use a vtkSmartPointer, this predicate allows to compare the equality
// of a pointer on a vtkAbstractPicker with the adress contained in
// a corresponding vtkSmartPointer.
struct equal_smartPtrPicker
{
equal_smartPtrPicker(vtkAbstractPicker* picker) : Picker(picker) {}
bool operator () (const PickerObjectsPairType& pickerObjs) const
{
return this->Picker == pickerObjs.first.GetPointer();
}
vtkAbstractPicker* Picker;
};
// Predicate comparing a vtkObject*
// and a vtkSmartPointer<vtkObject> using the PickerObjectsType.
// As we use a vtkSmartPointer, this predicate allows to compare the equality
// of a pointer on a vtkObject with the adress contained in
// a corresponding vtkSmartPointer.
struct equal_smartPtrObject
{
equal_smartPtrObject(vtkObject* object) : Object(object) {}
bool operator () (const vtkSmartPointer<vtkObject>& smartObj) const
{
return this->Object == smartObj.GetPointer();
}
vtkObject* Object;
};
PickerObjectsType Pickers; // Map the picker with the objects
vtkTimeStamp CurrentInteractionTime; // Time of the last interaction event
vtkTimeStamp LastPickingTime; // Time of the last picking process
vtkSmartPointer<vtkAbstractPicker> LastSelectedPicker;
// Define callback to keep track of the CurrentTime and the LastPickingTime.
// The timeStamp is use to avoid repeating the picking process if the
// vtkWindowInteractor has not been modified, it is a huge optimization
// avoiding each picker to relaunch the whole mechanisme to determine which
// picker has been selected at a state of the rendering.
vtkSmartPointer<vtkCallbackCommand> TimerCallback;
vtkPickingManager* External;
};
//------------------------------------------------------------------------------
// vtkInternal methods
//------------------------------------------------------------------------------
vtkPickingManager::vtkInternal::vtkInternal(vtkPickingManager* external)
{
this->External = external;
this->TimerCallback = vtkSmartPointer<vtkCallbackCommand>::New();
this->TimerCallback->SetClientData(this);
this->TimerCallback->SetCallback(UpdateTime);
}
//------------------------------------------------------------------------------
vtkPickingManager::vtkInternal::~vtkInternal()
{}
//------------------------------------------------------------------------------
void vtkPickingManager::vtkInternal::
CreateDefaultCollection(vtkAbstractPicker* picker, vtkObject* object)
{
CollectionType objects;
objects.push_back(object);
this->Pickers.insert(PickerObjectsPairType(picker, objects));
}
//------------------------------------------------------------------------------
void vtkPickingManager::vtkInternal::
LinkPickerObject(const PickerObjectsType::iterator& it, vtkObject* object)
{
CollectionType::iterator itObj = std::find_if(it->second.begin(),
it->second.end(),
equal_smartPtrObject(object));
if (itObj != it->second.end() && object)
{
vtkDebugWithObjectMacro(
this->External, "vtkPickingtManager::Internal::LinkPickerObject: "
<< "Current object already linked with the given picker.");
return;
}
it->second.push_back(object);
}
//------------------------------------------------------------------------------
bool vtkPickingManager::vtkInternal::
IsObjectLinked(vtkAbstractPicker* picker, vtkObject* obj)
{
if(!picker || !obj)
{
return false;
}
PickerObjectsType::iterator itPick = std::find_if(
this->Pickers.begin(), this->Pickers.end(), equal_smartPtrPicker(picker));
if(itPick == this->Pickers.end())
{
return false;
}
CollectionType::iterator itObj = std::find_if(itPick->second.begin(),
itPick->second.end(),
equal_smartPtrObject(obj));
return (itObj != itPick->second.end());
}
//------------------------------------------------------------------------------
vtkAbstractPicker* vtkPickingManager::vtkInternal::SelectPicker()
{
if (!this->External->Interactor)
{
return 0;
}
else if (this->External->GetOptimizeOnInteractorEvents() &&
this->CurrentInteractionTime.GetMTime() == this->LastPickingTime)
{
return this->LastSelectedPicker;
}
// Get the event position
double X = this->External->Interactor->GetEventPosition()[0];
double Y = this->External->Interactor->GetEventPosition()[1];
// Get the poked renderer
vtkRenderer* renderer = this->External->Interactor->FindPokedRenderer(static_cast<int>(X), static_cast<int>(Y));
vtkAbstractPicker* selectedPicker =
this->ComputePickerSelection(X, Y, 0., renderer);
// Keep track of the lastet picker choosen & last picking time.
this->LastSelectedPicker = selectedPicker;
this->LastPickingTime = this->CurrentInteractionTime;
return selectedPicker;
}
//------------------------------------------------------------------------------
vtkAbstractPicker* vtkPickingManager::vtkInternal::
ComputePickerSelection(double X, double Y, double Z, vtkRenderer* renderer)
{
vtkAbstractPicker* closestPicker = 0;
if (!renderer)
{
return closestPicker;
}
double* camPos = renderer->GetActiveCamera()->GetPosition();
double smallestDistance2 = std::numeric_limits<double>::max();
for(PickerObjectsType::iterator it = this->Pickers.begin();
it != this->Pickers.end(); ++it)
{
int pickResult = it->first->Pick(X, Y, Z, renderer);
double* pPos = it->first->GetPickPosition();
if(pickResult > 0) // Keep closest object picked.
{
double distance2 = vtkMath::Distance2BetweenPoints(camPos, pPos);
if(smallestDistance2 > distance2)
{
smallestDistance2 = distance2;
closestPicker = it->first;
}
}
}
return closestPicker;
}
//------------------------------------------------------------------------------
void vtkPickingManager::vtkInternal::UpdateTime(vtkObject *vtkNotUsed(caller),
unsigned long vtkNotUsed(event),
void *clientData,
void *vtkNotUsed(calldata))
{
vtkPickingManager::vtkInternal* self =
reinterpret_cast<vtkPickingManager::vtkInternal*>(clientData);
if (!self)
{
return;
}
self->CurrentInteractionTime.Modified();
}
//------------------------------------------------------------------------------
// vtkPickingManager methods
//------------------------------------------------------------------------------
vtkPickingManager::vtkPickingManager()
: Interactor(0)
, Enabled(false)
, OptimizeOnInteractorEvents(true)
, Internal(0)
{
this->Internal = new vtkInternal(this);
}
//------------------------------------------------------------------------------
vtkPickingManager::~vtkPickingManager()
{
this->SetInteractor(0);
delete this->Internal;
}
//------------------------------------------------------------------------------
void vtkPickingManager::SetInteractor(vtkRenderWindowInteractor* rwi)
{
if (rwi == this->Interactor)
{
return;
}
if (this->Interactor)
{
this->Interactor->RemoveObserver(this->Internal->TimerCallback);
}
this->Interactor = rwi;
if (this->Interactor)
{
this->Interactor->AddObserver(
vtkCommand::ModifiedEvent, this->Internal->TimerCallback);
}
this->Modified();
}
//------------------------------------------------------------------------------
void vtkPickingManager::SetOptimizeOnInteractorEvents(bool optimize)
{
if (this->OptimizeOnInteractorEvents == optimize)
{
return;
}
this->OptimizeOnInteractorEvents = optimize;
this->Modified();
}
//------------------------------------------------------------------------------
void vtkPickingManager::AddPicker(vtkAbstractPicker* picker,
vtkObject* object)
{
if (!picker)
{
return;
}
// Linke the object if the picker is already registered
vtkPickingManager::vtkInternal::PickerObjectsType::iterator it =
std::find_if( this->Internal->Pickers.begin(),
this->Internal->Pickers.end(),
vtkPickingManager::vtkInternal::equal_smartPtrPicker(picker));
if (it != this->Internal->Pickers.end() )
{
vtkDebugMacro("vtkPickingtManager::AddPicker: "
<< "Picker already in the manager, the object will be linked");
this->Internal->LinkPickerObject(it, object);
return;
}
// The picker does not exists in the manager yet.
// Create the list of associated objects
this->Internal->CreateDefaultCollection(picker, object);
}
//------------------------------------------------------------------------------
void vtkPickingManager::RemovePicker(vtkAbstractPicker* picker,
vtkObject* object)
{
vtkPickingManager::vtkInternal::PickerObjectsType::iterator it =
std::find_if( this->Internal->Pickers.begin(),
this->Internal->Pickers.end(),
vtkPickingManager::vtkInternal::equal_smartPtrPicker(picker));
// The Picker does not exist
if (it == this->Internal->Pickers.end())
{
return;
}
vtkPickingManager::vtkInternal::CollectionType::iterator itObj =
std::find_if(it->second.begin(),
it->second.end(),
vtkPickingManager::vtkInternal::equal_smartPtrObject(object));
// The object is not associated with the given picker.
if (itObj == it->second.end())
{
return;
}
it->second.erase(itObj);
// Delete the picker when it is not associated with any object anymore.
if(it->second.size() == 0)
{
this->Internal->Pickers.erase(it);
}
}
//------------------------------------------------------------------------------
void vtkPickingManager::RemoveObject(vtkObject* object)
{
vtkPickingManager::vtkInternal::PickerObjectsType::iterator it =
this->Internal->Pickers.begin();
for(; it != this->Internal->Pickers.end();)
{
vtkPickingManager::vtkInternal::CollectionType::iterator itObj =
std::find_if(it->second.begin(),
it->second.end(),
vtkPickingManager::vtkInternal::equal_smartPtrObject(object));
if (itObj != it->second.end())
{
it->second.erase(itObj);
if (it->second.size() == 0)
{
vtkPickingManager::vtkInternal::PickerObjectsType::iterator
toRemove = it;
it++;
this->Internal->Pickers.erase(toRemove);
continue;
}
}
++it;
}
}
//------------------------------------------------------------------------------
bool vtkPickingManager::Pick(vtkAbstractPicker* picker, vtkObject* obj)
{
if (!this->Internal->IsObjectLinked(picker, obj))
{
return false;
}
return (this->Pick(picker));
}
//------------------------------------------------------------------------------
bool vtkPickingManager::Pick(vtkObject* obj)
{
vtkAbstractPicker* picker = this->Internal->SelectPicker();
if(!picker)
{
return false;
}
// If the object is not contained in the list of the associated active pickers
// return false
return (this->Internal->IsObjectLinked(picker, obj));
}
//------------------------------------------------------------------------------
bool vtkPickingManager::Pick(vtkAbstractPicker* picker)
{
return (picker == this->Internal->SelectPicker());
}
//------------------------------------------------------------------------------
vtkAssemblyPath* vtkPickingManager::
GetAssemblyPath(double X, double Y, double Z,
vtkAbstractPropPicker* picker,
vtkRenderer* renderer,
vtkObject* obj)
{
if (this->Enabled)
{
// Return 0 when the Picker is not selected
if (!this->Pick(picker, obj))
{
return 0;
}
}
else
{
picker->Pick(X, Y, Z, renderer);
}
return picker->GetPath();
}
//------------------------------------------------------------------------------
int vtkPickingManager::GetNumberOfPickers()
{
return static_cast<int>(this->Internal->Pickers.size());
}
//------------------------------------------------------------------------------
int vtkPickingManager::GetNumberOfObjectsLinked(vtkAbstractPicker* picker)
{
if (!picker)
{
return 0;
}
vtkPickingManager::vtkInternal::PickerObjectsType::iterator it =
std::find_if( this->Internal->Pickers.begin(),
this->Internal->Pickers.end(),
vtkPickingManager::vtkInternal::equal_smartPtrPicker(picker));
if (it == this->Internal->Pickers.end())
{
return 0;
}
return static_cast<int>(it->second.size());
}
//------------------------------------------------------------------------------
void vtkPickingManager::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "RenderWindowInteractor: " << this->Interactor << "\n";
os << indent << "NumberOfPickers: " << this->Internal->Pickers.size() << "\n";
vtkPickingManager::vtkInternal::PickerObjectsType::iterator it =
this->Internal->Pickers.begin();
for(; it != this->Internal->Pickers.end(); ++it)
{
os << indent << indent << "Picker: " << it->first.GetPointer() << "\n";
os << indent << indent << "NumberOfObjectsLinked: " << it->second.size()
<< "\n";
}
}
|