Commit fc58eb68 authored by Emmanuel Promayon's avatar Emmanuel Promayon
Browse files

NEW add mesh picking management example

As the vtkmesh code was cleaned, this new tutorial was needed
to show how to override pointPicked(..) and cellPicked(..)
to manage mesh interaction.
parent 8a4f1b6a
# Call CamiTK CMake Macro to define the component
camitk_extension( COMPONENT_EXTENSION
DEFAULT
INSTALL_ALL_HEADERS
DESCRIPTION "This tutorial show how to overide the pointPicked and cellPicked method to interact directly with your component"
NEEDS_COMPONENT_EXTENSION vtkmesh
)
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2017 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
// CamiTK includes
#include "PickMe.h"
#include <vtkCell.h>
using namespace camitk;
// --------------- Constructor -------------------
PickMe::PickMe ( const QString& file ) throw ( AbortException ) : VtkMeshComponent ( file ) {
// initially all the point data have a value of 0.0 (everything is blue)
// when the user pick a cell or a point in 3D, the point data are modified
// using the point index (for point picking) or average of cell's point index
// (for cell picking)
// -> the mesh starts to show colors!
initPointData();
}
// --------------- destructor -------------------
PickMe::~PickMe() {
}
// --------------- initPointData -------------------
void PickMe::initPointData() {
// create a new vtkDataArray to store the demo value
demoPointData = vtkSmartPointer<vtkDoubleArray>::New();
int numberOfPoints = getPointSet()->GetNumberOfPoints();
demoPointData->SetNumberOfValues ( numberOfPoints );
demoPointData->SetName ( "Pick Me Data" );
demoPointData->FillComponent(0, 0.0);
// add the data to the mesh
addPointData(demoPointData->GetName(), demoPointData);
}
// -------------------- pointPicked --------------------
void PickMe::pointPicked ( vtkIdType pointId, bool sel/* picking does not change the selection state, do not bother with the 2nd parameter*/ ) {
MeshComponent::pointPicked ( pointId, sel );
// picking show demo point data
demoPointData->SetValue ( pointId, 1.0 - double ( pointId ) / double ( getPointSet()->GetNumberOfPoints() ) );
refreshPointData();
}
// -------------------- cellPicked --------------------
void PickMe::cellPicked ( vtkIdType cellId, bool sel /* picking does not change the selection state, do not bother with the 2nd parameter*/ ) {
MeshComponent::cellPicked ( cellId, sel );
// change data for all cell points
vtkSmartPointer<vtkCell> c = getPointSet()->GetCell ( cellId );
for ( vtkIdType i = 0; i < c->GetNumberOfPoints(); i++ ) {
vtkIdType pointId = c->GetPointId ( i );
demoPointData->SetValue ( pointId, double ( pointId ) / double ( getPointSet()->GetNumberOfPoints() ) );
}
refreshPointData();
}
// -------------------- refreshPointData --------------------
void PickMe::refreshPointData() {
// force the modification flag of the vtkDoubleArray so that the range is going to be recomputed
// This is needed so that VTK can "discover" that the data have changed (calls to SetValue do not modify the internal time stamp)
demoPointData->Modified();
// force the data visualization to be updated
setActiveData(MeshDataModel::POINTS, demoPointData->GetName());
}
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2017 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#ifndef PICKME_H
#define PICKME_H
#include <QObject>
#include <VtkMeshComponent.h>
#include <vtkDoubleArray.h>
#include <vtkSmartPointer.h>
class PickMe : public VtkMeshComponent {
Q_OBJECT
public:
/// Default Constructor
PickMe(const QString & file) throw(camitk::AbortException);
/// Default Destructor
virtual ~PickMe();
/// reimplemented to visualize the picked points (demo)
void pointPicked ( vtkIdType pointId, bool ) override;
/// reimplemented to visualize the picked cells (demo)
void cellPicked ( vtkIdType cellId, bool ) override;
private:
/// the demo point data array (i.e the data value displayed when picked is the id of the point, in point picking, or the average of the point id, in cell picking)
vtkSmartPointer<vtkDoubleArray> demoPointData;
/// initialize the point data
void initPointData();
/// update the visualization of point data
/// This is just needed for fluidify the visualization/interaction
/// (avoid the need to select/unselect data in the "Data" tab of the property editor)
void refreshPointData();
};
#endif // PICKME_H
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2017 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#include "PickMeComponent.h"
// include generated components headers
#include "PickMe.h"
using namespace camitk;
// --------------- GetFileExtensions -------------------
QStringList PickMeComponent::getFileExtensions() const {
QStringList ext;
ext << "pickme";
return ext;
}
// --------------- Open -------------------
Component * PickMeComponent::open(const QString & fileName) throw (AbortException) {
return new PickMe(fileName);
}
// --------------- Save --------------------
bool PickMeComponent::save(Component * component) const {
// depending on the components managed by PickMeComponent, use
// component->getPointSet() (for a MeshComponent derived class)
// or component->getImageData() (for a ImageComponent derived class)
// and save the data in the managed format in the file component->getFileName()
return false;
}
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2017 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#ifndef PICKMECOMPONENT_H
#define PICKMECOMPONENT_H
#include <QObject>
#include<ComponentExtension.h>
class PickMeComponent : public camitk::ComponentExtension {
Q_OBJECT
Q_INTERFACES(camitk::ComponentExtension);
Q_PLUGIN_METADATA(IID "fr.imag.camitk.wizardgenerated.component.PickMeComponent")
public:
/// Constructor
PickMeComponent() : ComponentExtension() {};
/// Method returning the component extension name
virtual QString getName() const {
return "Pick Me Component";
};
/// Method returning the component extension descrption
virtual QString getDescription() const {
return "This tutorial show how to override the pointPicked(..) and cellPicked(..) method to interact directly with your component";
};
/** Get the list of managed extensions
* (each file with an extension in the list can be loaded by this extension)
*/
virtual QStringList getFileExtensions() const;
/** Get a new instance from data stored in a file
* (this is the most important method to redefine in your subclass)
*/
virtual camitk::Component * open(const QString &) throw (camitk::AbortException);
/** Save a given Component (does not have to be top-level)
* into one of the currently managed format.
* @return false if the operation was not performed properly or not performed at all.
*/
virtual bool save(camitk::Component *) const;
protected:
/// Destructor
virtual ~PickMeComponent() {};
};
#endif // PICKMECOMPONENT
# vtk DataFile Version 3.0
vtk output
ASCII
DATASET POLYDATA
POINTS 25 float
-0.0981577 0.0639953 -0.152536 0.0662411 0.913408 0.214962 -0.0256973 1.90475 0.0318697
-0.0732923 2.97639 -0.14693 -0.0907575 4.03827 0.424344 0.991426 0.088565 -0.439235
0.956044 1.03466 -0.119008 0.966296 1.9451 -0.376246 1.08769 2.93903 -0.257324
1.03252 4.02625 -0.19867 1.98952 -0.0785815 0.407174 2.07315 0.973676 -0.168316
2.02312 1.95903 -0.0489972 1.90072 3.05295 -0.0383131 2.01426 4.03996 0.337362
3.01026 -0.0131874 -0.19971 3.09593 1.03124 -0.335646 2.95806 2.0721 0.065351
3.07098 3.09579 0.13096 3.00829 3.92759 -0.215786 4.05534 0.0646894 0.175641
4.00035 0.955553 -0.0894418 3.95042 2.05852 0.0185222 4.06053 2.96645 -0.364692
3.92506 4.01263 0.0126824
POLYGONS 32 128
3 11 6 5
3 12 8 7
3 5 1 0
3 7 3 2
3 15 11 10
3 20 16 15
3 6 2 1
3 9 4 3
3 6 1 5
3 7 2 6
3 8 3 7
3 9 3 8
3 12 7 6
3 19 14 13
3 11 5 10
3 13 9 8
3 12 6 11
3 13 8 12
3 14 9 13
3 17 12 11
3 17 13 12
3 16 11 15
3 17 11 16
3 18 13 17
3 19 13 18
3 22 17 16
3 22 18 17
3 21 16 20
3 24 19 18
3 22 16 21
3 23 18 22
3 24 18 23
Supports Markdown
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