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/

HardCodedPipelineAction.cpp 7.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*****************************************************************************
 * $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$
 ****************************************************************************/
Emmanuel Promayon's avatar
Emmanuel Promayon committed
25
#include "HardCodedPipelineAction.h"
26
27
28
29
30
31
32
33
34
35
36
37
38

// CamiTK
#include <Application.h>
#include <Property.h>
#include <Log.h>

// Qt
#include <QVector3D>

using namespace camitk;


// --------------- Constructor -------------------
Emmanuel Promayon's avatar
Emmanuel Promayon committed
39
HardCodedPipelineAction::HardCodedPipelineAction(ActionExtension* extension) : Action(extension) {
40
    // Setting name, description and input component
Emmanuel Promayon's avatar
Emmanuel Promayon committed
41
    setName(tr("Hard-Coded Action Pipeline"));
42
    setDescription("Demonstrate how to call one or more actions from another action (that is, implement a hard-coded action pipeline without modifying anything of the called actions).");
43
44
45
46
47
48
49
    setComponent("ImageComponent");

    // Setting classification family and tags
    setFamily(tr("Tutorial"));
    addTag(tr("Pipeline"));

    // Setting the action's parameters
50
    addParameter(new Property(tr("Resample Factor"), QVariant(0.5), tr("The resample factor to use when applying the Resample action"), tr("factor")));
51
52
53
54

}

// --------------- destructor -------------------
Emmanuel Promayon's avatar
Emmanuel Promayon committed
55
HardCodedPipelineAction::~HardCodedPipelineAction() {
56
57
58
59
60
61
    // Do not do anything yet.
    // Delete stuff if you create stuff
    // (except if you use smart pointers of course !!)
}

// --------------- apply -------------------
Emmanuel Promayon's avatar
Emmanuel Promayon committed
62
Action::ApplyStatus HardCodedPipelineAction::apply() {
63
64
65

    PipelineActionStatus pipelineStatus = PipelineActionStatus::OK;

66
67
68
69
70
71
72
    //
    // Hard-coded Pipeline starts here
    // 1. apply Resample action, and check that everything is ok
    // 2. apply Otsu threshold
    // if everything is alright, remove the intermediate results and force the application refresh

    // 1. apply the resample action (first action in the pipeline) on the last selected component
73
    pipelineStatus = applyResample(getTargets().last());
74

75
76
    // If the image has been saved, then it is not modified any more...
    if (pipelineStatus == OK) {
77

78
79
80
81
82
        // You can most of the time safely ignore the safeguard bits below and simply use:
        // Component* resampled = resampleAction->getOutputComponent()
        // But in some rare case, the second action of this pipeline is not going to use the correct
        // input. For these cases, you need to use:
        Component* resampled = getLastTopLevelOutputComponents("Resample");
83

84
        // 2. apply the Otsu action (second action in the pipeline)
85
        pipelineStatus = applyOtsu(resampled);
86

87
88
89
        if (pipelineStatus == OK) {
            // if all goes well, don't show the intermediate result
            delete resampled;
90
91
92
93
94
        }
    }

    switch (pipelineStatus) {
        case OTSU_APPLY_ERROR:
95
            CAMITK_ERROR("\"Otsu Threshold Filter\" apply error");
96
97
98
99
100
            break;
        case OTSU_NOT_FOUND:
            CAMITK_ERROR("\"Otsu Threshold Filter\" action not found");
            break;
        case RESAMPLE_APPLY_ERROR:
101
            CAMITK_ERROR("\"Resample Factor\" apply error.");
102
103
104
105
            break;
        case RESAMPLE_NOT_FOUND:
            CAMITK_ERROR("\"Resample Factor\" action not found");
            break;
106
107
108
        case OK:
            CAMITK_TRACE("Pipeline successful");
            break;
109
110
111
112
113
114
115
    }

    Application::refresh();

    return ((pipelineStatus == OK) ? SUCCESS : ERROR);
}

116
// --------------- getLastTopLevelOutputComponents -------------------
Emmanuel Promayon's avatar
Emmanuel Promayon committed
117
Component* HardCodedPipelineAction::getLastTopLevelOutputComponents(const QString& actionName) {
118
119
120
121
122
123
124
125
126
127
128
129
130
131

    Component* lastOutputComponent = nullptr;

    Action* currentAction = Application::getAction(actionName);

    if (currentAction != nullptr) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
        // it Qt >= 5.6 crbegin() exist and we can use a lambda expression to find the first
        // top level component, looking from the end to the beginning of the component list.
        auto it = find_if(currentAction->getOutputComponents().crbegin(),
                          currentAction->getOutputComponents().crend(),
        [](const Component * c) {
            return c->isTopLevel();
        });
132
        // This is the equivalent of:
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        // ComponentList::reverse_iterator it = currentAction->getOutputComponents().rbegin();
        // while (it != currentAction->getOutputComponents().rend() && !(*it)->isTopLevel()) {
        //    it++;
        // }
        lastOutputComponent = (*it);
#else
        // In Qt < 5.6, too bad: crbegin(), crend(), rbegin() and rend() do not exist
        unsigned int i = currentAction->getOutputComponents().size() - 1;
        while (i > 0 && !currentAction->getOutputComponents().at(i)->isTopLevel()) {
            i--;
        }
        if (i >= 0) {
            lastOutputComponent = currentAction->getOutputComponents().at(i);
        }
#endif
    }

    return lastOutputComponent;
}

// --------------- applyResample -------------------
Emmanuel Promayon's avatar
Emmanuel Promayon committed
154
HardCodedPipelineAction::PipelineActionStatus HardCodedPipelineAction::applyResample(Component* input) {
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
    Action* resampleAction = Application::getAction("Resample");

    if (resampleAction == nullptr) {
        return RESAMPLE_NOT_FOUND;
    }
    else {
        // set the input
        resampleAction->setInputComponent(input);

        // set the parameter
        ImageComponent* inputImage = dynamic_cast<ImageComponent*>(input);
        // Note: as ActionsInActionAction takes ImageComponent as inputs, the dynamic_cast is 
        // guaranty to work perfectly well.
        int* dims = inputImage->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
        Action::ApplyStatus status = resampleAction->applyInPipeline();

        // reset the flag (just in case)
        input->setModified(false);

        return (status == Action::SUCCESS) ? OK : RESAMPLE_APPLY_ERROR;
    }


}


// --------------- applyOtsu -------------------
Emmanuel Promayon's avatar
Emmanuel Promayon committed
188
HardCodedPipelineAction::PipelineActionStatus HardCodedPipelineAction::applyOtsu(Component* input) {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    Action* otsuThreshold = Application::getAction("Otsu Threshold Filter");
    if (otsuThreshold == nullptr) {
        return OTSU_NOT_FOUND;
    }
    else {
        // set the input
        otsuThreshold->setInputComponent(input);

        // nothing special to do on the action parameters

        // apply the action
        Action::ApplyStatus status = otsuThreshold->applyInPipeline();

        return (status == Action::SUCCESS) ? OK : OTSU_APPLY_ERROR;
    }
}
205