Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

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

NEW tutorial to demonstrates how to call actions from another one

It implement some fixed hardcoded pipeline of actions. To demonstrates
this, an image resize action followed by an Otsu filter are applied
(while the intermediary result is deleted/never visible)
parent f694939d
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2018 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 "ActionsInActionAction.h"
// CamiTK
#include <Application.h>
#include <Property.h>
#include <Log.h>
// Qt
#include <QVector3D>
using namespace camitk;
// --------------- Constructor -------------------
ActionsInActionAction::ActionsInActionAction(ActionExtension* extension) : Action(extension) {
// Setting name, description and input component
setName(tr("Apply two actions from an action"));
setDescription("Demonstrate how to call one or more actions from another action.");
setComponent("ImageComponent");
// Setting classification family and tags
setFamily(tr("Tutorial"));
addTag(tr("Pipeline"));
// Setting the action's parameters
addParameter(new Property(tr("Resample Factor"), QVariant(0.5), tr("The resample factor to use when applying the ResampleAction"), tr("factor")));
}
// --------------- destructor -------------------
ActionsInActionAction::~ActionsInActionAction() {
// Do not do anything yet.
// Delete stuff if you create stuff
// (except if you use smart pointers of course !!)
}
// --------------- apply -------------------
Action::ApplyStatus ActionsInActionAction::apply() {
Action::ApplyStatus status;
PipelineActionStatus pipelineStatus = PipelineActionStatus::OK;
ImageComponent* input = dynamic_cast<ImageComponent*>(getTargets().last());
Action* resampleAction = Application::getAction("Resample");
if (resampleAction != nullptr) {
// set the input
resampleAction->setInputComponent(input);
// set the parameter
int* dims = input->getImageData()->GetDimensions();
double factor = property("Resample Factor").toDouble();
resampleAction->setProperty("New Image X Dimension", int(dims[0]*factor));
resampleAction->setProperty("New Image Y Dimension", int(dims[1]*factor));
resampleAction->setProperty("New Image Z Dimension", std::max(1,int(dims[2]*factor)));
// apply the action
status = resampleAction->applyInPipeline();
// If the image has been saved, then it is not modified any more...
if (status == Action::SUCCESS) {
input->setModified(false);
auto it = find_if(resampleAction->getOutputComponents().crbegin(), resampleAction->getOutputComponents().crend(), [] (const Component* c) { return c->isTopLevel(); } );
// Equivalent of
// ComponentList::reverse_iterator it = resampleAction->getOutputComponents().rbegin();
// while (it != resampleAction->getOutputComponents().rend() && !(*it)->isTopLevel()) {
// it++;
// }
Component* resampled = (*it);
Action* otsuThreshold = Application::getAction("Otsu Threshold Filter");
if (otsuThreshold != nullptr) {
otsuThreshold->setInputComponent(resampled);
status = otsuThreshold->applyInPipeline();
if (status == Action::SUCCESS) {
// if all goes well, don't show the intermediate result
delete resampled;
}
else {
pipelineStatus = PipelineActionStatus::OTSU_APPLY_ERROR;
}
}
else {
pipelineStatus = PipelineActionStatus::OTSU_NOT_FOUND;
}
}
else {
pipelineStatus = PipelineActionStatus::RESAMPLE_APPLY_ERROR;
}
}
else {
pipelineStatus = PipelineActionStatus::RESAMPLE_NOT_FOUND;
}
switch (pipelineStatus) {
case OTSU_APPLY_ERROR:
CAMITK_ERROR("\"Otsu Threshold Filter\" apply error:" + Action::getStatusAsString(status));
break;
case OTSU_NOT_FOUND:
CAMITK_ERROR("\"Otsu Threshold Filter\" action not found");
break;
case RESAMPLE_APPLY_ERROR:
CAMITK_ERROR("\"Resample Factor\" apply error:" + Action::getStatusAsString(status));
break;
case RESAMPLE_NOT_FOUND:
CAMITK_ERROR("\"Resample Factor\" action not found");
break;
}
Application::refresh();
return ((pipelineStatus == OK) ? SUCCESS : ERROR);
}
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2018 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 ACTIONSINACTIONACTION_H
#define ACTIONSINACTIONACTION_H
#include <Action.h>
#include <ImageComponent.h>
class ActionsInActionAction : public camitk::Action {
Q_OBJECT
public:
/// Default Constructor
ActionsInActionAction(camitk::ActionExtension*);
/// Default Destructor
virtual ~ActionsInActionAction();
public slots:
/** this method is automatically called when the action is triggered.
* Call getTargets() method to get the list of components to use.
* \note getTargets() is automatically filtered so that it only contains compatible components,
* i.e., instances of ComputeNeighborValueAction (or a subclass).
*/
virtual ApplyStatus apply();
private:
// many things can happen during the execution of this action
enum PipelineActionStatus{
RESAMPLE_NOT_FOUND,
OTSU_NOT_FOUND,
RESAMPLE_APPLY_ERROR,
OTSU_APPLY_ERROR,
OK,
};
};
#endif // ACTIONSINACTIONACTION_H
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2018 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 "ActionsInActionExtension.h"
// include generated actions headers
#include "ActionsInActionAction.h"
// --------------- getActions -------------------
void ActionsInActionExtension::init() {
// Creating and registering the instance of ActionsInActionAction
registerNewAction(ActionsInActionAction);
}
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2018 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 ACTIONS_IN_ACTION_EXTENSION_H
#define ACTIONS_IN_ACTION_EXTENSION_H
#include <ActionExtension.h>
class ActionsInActionExtension : public camitk::ActionExtension {
Q_OBJECT
Q_INTERFACES(camitk::ActionExtension);
Q_PLUGIN_METADATA(IID "fr.imag.camitk.tutorials.action.actionsinaction")
public:
/// Constructor
ActionsInActionExtension() : ActionExtension() {};
/// Destructor
virtual ~ActionsInActionExtension() = default;
/// Method returning the action extension name
virtual QString getName() {
return "ActionsInActionExtension";
};
/// Method returning the action extension descrption
virtual QString getDescription() {
return "Demo of applying actions from another action";
};
/// initialize all the actions
virtual void init();
};
#endif // ACTIONS_IN_ACTION_EXTENSION_H
# Call CamiTK CMake Macro to define the action
camitk_extension(ACTION_EXTENSION
ENABLE_AUTO_TEST
TEST_FILES brain.mha sinus.mhd
)
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