|
| 1 | +/* |
| 2 | + For more information, please see: http://software.sci.utah.edu |
| 3 | +
|
| 4 | + The MIT License |
| 5 | +
|
| 6 | + Copyright (c) 2009 Scientific Computing and Imaging Institute, |
| 7 | + University of Utah. |
| 8 | +
|
| 9 | +
|
| 10 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | + copy of this software and associated documentation files (the "Software"), |
| 12 | + to deal in the Software without restriction, including without limitation |
| 13 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 | + and/or sell copies of the Software, and to permit persons to whom the |
| 15 | + Software is furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | + The above copyright notice and this permission notice shall be included |
| 18 | + in all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 21 | + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 26 | + DEALINGS IN THE SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +// Core includes |
| 30 | +#include <Core/Math/MathFunctions.h> |
| 31 | +#include <Core/Utils/TimeSince.h> |
| 32 | + |
| 33 | +// Application includes |
| 34 | +#include <Application/Layer/LayerManager.h> |
| 35 | +#include <Application/Layer/LayerGroup.h> |
| 36 | +#include <Application/StatusBar/StatusBar.h> |
| 37 | +#include <Application/Filters/ITKFilter.h> |
| 38 | +#include <Application/Layer/Actions/ActionComputeIsosurface.h> |
| 39 | + |
| 40 | +#include <Application/Tools/Actions/ActionGrowCut.h> |
| 41 | +#include <Application/Tools/Actions/ActionGrowCutPostProcess.h> |
| 42 | + |
| 43 | +// REGISTER ACTION: |
| 44 | +// Define a function that registers the action. The action also needs to be |
| 45 | +// registered in the CMake file. |
| 46 | +// NOTE: Registration needs to be done outside of any namespace |
| 47 | +CORE_REGISTER_ACTION( Seg3D, GrowCut ) |
| 48 | + |
| 49 | +namespace Seg3D |
| 50 | +{ |
| 51 | + |
| 52 | +bool ActionGrowCut::validate( Core::ActionContextHandle& context ) |
| 53 | +{ |
| 54 | + // Check for layer existence and type information |
| 55 | + if ( !LayerManager::CheckLayerExistenceAndType( this->data_layer_, |
| 56 | + Core::VolumeType::DATA_E, context ) ) { return false; } |
| 57 | + |
| 58 | + // Check for layer availability |
| 59 | + if ( !LayerManager::CheckLayerAvailability( this->data_layer_, |
| 60 | + false, context ) ) { return false; } |
| 61 | + |
| 62 | + // Check for layer existence and type information foreground layer |
| 63 | + if ( !LayerManager::CheckLayerExistenceAndType( this->foreground_layer_, |
| 64 | + Core::VolumeType::MASK_E, context ) ) { return false; } |
| 65 | + |
| 66 | + // Check for layer existence and type information background layer |
| 67 | + if ( !LayerManager::CheckLayerExistenceAndType( this->background_layer_, |
| 68 | + Core::VolumeType::MASK_E, context ) ) { return false; } |
| 69 | + |
| 70 | + // Check for layer existence and type information output layer |
| 71 | + if ( !LayerManager::CheckLayerExistenceAndType( this->output_layer_, |
| 72 | + Core::VolumeType::MASK_E, context ) ) { return false; } |
| 73 | + |
| 74 | + // Check whether foreground and data have the same size |
| 75 | + if ( !LayerManager::CheckLayerSize( this->foreground_layer_, this->data_layer_, |
| 76 | + context ) ) { return false; } |
| 77 | + |
| 78 | + // Check whether background and data have the same size |
| 79 | + if ( !LayerManager::CheckLayerSize( this->background_layer_, this->data_layer_, |
| 80 | + context ) ) { return false; } |
| 81 | + |
| 82 | + // Check whether output and data have the same size |
| 83 | + if ( !LayerManager::CheckLayerSize( this->output_layer_, this->data_layer_, |
| 84 | + context ) ) { return false; } |
| 85 | + |
| 86 | + // Check for layer availability foreground layer |
| 87 | + if ( !LayerManager::CheckLayerAvailability( this->foreground_layer_, |
| 88 | + false, context ) ) { return false; } |
| 89 | + |
| 90 | + // Check for layer availability background layer |
| 91 | + if ( !LayerManager::CheckLayerAvailability( this->background_layer_, |
| 92 | + false, context ) ) { return false; } |
| 93 | + |
| 94 | + // Check for layer availability output layer |
| 95 | + if ( !LayerManager::CheckLayerAvailability( this->output_layer_, |
| 96 | + false, context ) ) { return false; } |
| 97 | + |
| 98 | + // Validation successful |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +// ALGORITHM CLASS |
| 103 | +// This class does the actual work and is run on a separate thread. |
| 104 | +// NOTE: The separation of the algorithm into a private class is for the purpose of running the |
| 105 | +// filter on a separate thread. |
| 106 | + |
| 107 | +class GrowCutAlgo : public ITKFilter |
| 108 | +{ |
| 109 | + |
| 110 | +public: |
| 111 | + LayerHandle data_layer_; |
| 112 | + LayerHandle foreground_layer_; |
| 113 | + LayerHandle background_layer_; |
| 114 | + LayerHandle output_layer_; |
| 115 | + |
| 116 | + GrowCutterHandle grow_cutter_; |
| 117 | + |
| 118 | + bool run_smoothing_; |
| 119 | + |
| 120 | + Core::ActionContextHandle context_; |
| 121 | + |
| 122 | +public: |
| 123 | + // RUN: |
| 124 | + // Implementation of run of the Runnable base class, this function is called when the thread |
| 125 | + // is launched. |
| 126 | + |
| 127 | + virtual void run_filter() |
| 128 | + { |
| 129 | + Core::TimeSince::start_timer( "growcut" ); |
| 130 | + // Retrieve the image as an itk image from the underlying data structure |
| 131 | + // NOTE: This only does wrapping and does not regenerate the data. |
| 132 | + |
| 133 | + Core::ITKImageDataT<short>::Handle data_image; |
| 134 | + this->get_itk_image_from_layer<short>( this->data_layer_, data_image ); |
| 135 | + |
| 136 | + Core::ITKImageDataT<unsigned char>::Handle foreground_image; |
| 137 | + this->get_itk_image_from_layer<unsigned char>( this->foreground_layer_, foreground_image ); |
| 138 | + |
| 139 | + Core::ITKImageDataT<unsigned char>::Handle background_image; |
| 140 | + this->get_itk_image_from_layer<unsigned char>( this->background_layer_, background_image ); |
| 141 | + |
| 142 | + if ( !this->grow_cutter_ ) |
| 143 | + { |
| 144 | + this->grow_cutter_ = GrowCutterHandle( new GrowCutter() ); |
| 145 | + std::cerr << " New grow cutter handle" << std::endl; |
| 146 | + } |
| 147 | + |
| 148 | + this->output_layer_->update_progress_signal_( 0.1 ); |
| 149 | + |
| 150 | + this->grow_cutter_->set_foreground_image( foreground_image->get_image() ); |
| 151 | + this->grow_cutter_->set_background_image( background_image->get_image() ); |
| 152 | + this->grow_cutter_->set_data_image( data_image->get_image() ); |
| 153 | + |
| 154 | + this->output_layer_->update_progress_signal_( 0.2 ); |
| 155 | + |
| 156 | + this->grow_cutter_->execute(); |
| 157 | + |
| 158 | + this->output_layer_->update_progress_signal_( 1.0 ); |
| 159 | + |
| 160 | + this->insert_itk_image_into_layer( this->output_layer_, this->grow_cutter_->get_output().GetPointer() ); |
| 161 | + |
| 162 | + CORE_LOG_SUCCESS( "GrowCut duration: " + |
| 163 | + Core::TimeSince::format_double( Core::TimeSince::get_time_since( "growcut" ) / 1000, 2 ) + "s" ); |
| 164 | + |
| 165 | + this->dispatch_unlock_layer( this->output_layer_ ); |
| 166 | + |
| 167 | + if ( this->run_smoothing_ ) |
| 168 | + { |
| 169 | + ActionGrowCutPostProcess::Dispatch( this->context_, this->output_layer_->get_layer_id(), true ); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + // re-create the isosurface for the result |
| 174 | + LayerGroupHandle group = this->output_layer_->get_layer_group(); |
| 175 | + double quality = 1.0; |
| 176 | + Core::ImportFromString( group->isosurface_quality_state_->get(), quality ); |
| 177 | + bool capping_enabled = group->isosurface_capping_enabled_state_->get(); |
| 178 | + |
| 179 | + MaskLayerHandle mask_layer = boost::dynamic_pointer_cast<MaskLayer>( this->output_layer_ ); |
| 180 | + ActionComputeIsosurface::Dispatch( this->context_, mask_layer, quality, capping_enabled, true ); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // GET_FILTER_NAME: |
| 185 | + // The name of the filter, this information is used for generating new layer labels. |
| 186 | + virtual std::string get_filter_name() const |
| 187 | + { |
| 188 | + return "GrowCut"; |
| 189 | + } |
| 190 | + |
| 191 | + // GET_LAYER_PREFIX: |
| 192 | + // This function returns the name of the filter. The latter is prepended to the new layer name, |
| 193 | + // when a new layer is generated. |
| 194 | + virtual std::string get_layer_prefix() const |
| 195 | + { |
| 196 | + return "GrowCut"; |
| 197 | + } |
| 198 | +}; |
| 199 | + |
| 200 | +bool ActionGrowCut::run( Core::ActionContextHandle& context, |
| 201 | + Core::ActionResultHandle& result ) |
| 202 | +{ |
| 203 | + |
| 204 | + // Create algorithm |
| 205 | + boost::shared_ptr<GrowCutAlgo> algo( new GrowCutAlgo ); |
| 206 | + |
| 207 | + // Find the handle to the layers |
| 208 | + if ( !( algo->find_layer( this->data_layer_, algo->data_layer_ ) ) ) |
| 209 | + { |
| 210 | + return false; |
| 211 | + } |
| 212 | + if ( !( algo->find_layer( this->foreground_layer_, algo->foreground_layer_ ) ) ) |
| 213 | + { |
| 214 | + return false; |
| 215 | + } |
| 216 | + if ( !( algo->find_layer( this->background_layer_, algo->background_layer_ ) ) ) |
| 217 | + { |
| 218 | + return false; |
| 219 | + } |
| 220 | + |
| 221 | + if ( !( algo->find_layer( this->output_layer_, algo->output_layer_ ) ) ) |
| 222 | + { |
| 223 | + return false; |
| 224 | + } |
| 225 | + |
| 226 | + // Lock the layers, so they cannot be used else where |
| 227 | + algo->lock_for_use( algo->data_layer_ ); |
| 228 | + algo->lock_for_use( algo->foreground_layer_ ); |
| 229 | + algo->lock_for_use( algo->background_layer_ ); |
| 230 | + algo->lock_for_processing( algo->output_layer_ ); |
| 231 | + |
| 232 | + algo->grow_cutter_ = this->grow_cutter_; |
| 233 | + algo->run_smoothing_ = this->run_smoothing_; |
| 234 | + algo->context_ = context; |
| 235 | + |
| 236 | + result = Core::ActionResultHandle( new Core::ActionResult( algo->output_layer_->get_layer_id() ) ); |
| 237 | + |
| 238 | + // If the action is run from a script (provenance is a special case of script), |
| 239 | + // return a notifier that the script engine can wait on. |
| 240 | + if ( context->source() == Core::ActionSource::SCRIPT_E || |
| 241 | + context->source() == Core::ActionSource::PROVENANCE_E ) |
| 242 | + { |
| 243 | + context->report_need_resource( algo->get_notifier() ); |
| 244 | + } |
| 245 | + |
| 246 | + // Build the undo-redo record |
| 247 | + algo->create_undo_redo_and_provenance_record( context, this->shared_from_this() ); |
| 248 | + |
| 249 | + // Start the filter. |
| 250 | + Core::Runnable::Start( algo ); |
| 251 | + |
| 252 | + return true; |
| 253 | +} |
| 254 | + |
| 255 | +void ActionGrowCut::Dispatch( Core::ActionContextHandle context, std::string data_layer, |
| 256 | + std::string foreground_layer, std::string background_layer, |
| 257 | + std::string output_layer, bool run_smoothing, GrowCutterHandle grow_cutter ) |
| 258 | +{ |
| 259 | + // Create a new action |
| 260 | + ActionGrowCut* action = new ActionGrowCut; |
| 261 | + |
| 262 | + // Setup the parameters |
| 263 | + action->data_layer_ = data_layer; |
| 264 | + action->foreground_layer_ = foreground_layer; |
| 265 | + action->background_layer_ = background_layer; |
| 266 | + action->output_layer_ = output_layer; |
| 267 | + action->grow_cutter_ = grow_cutter; |
| 268 | + action->run_smoothing_ = run_smoothing; |
| 269 | + |
| 270 | + // Dispatch action to underlying engine |
| 271 | + Core::ActionDispatcher::PostAction( Core::ActionHandle( action ), context ); |
| 272 | +} |
| 273 | + |
| 274 | +void ActionGrowCut::DispatchAndWait( Core::ActionContextHandle context, std::string data_layer, |
| 275 | + std::string foreground_layer, std::string background_layer, |
| 276 | + std::string output_layer, bool run_smoothing, |
| 277 | + GrowCutterHandle grow_cutter ) |
| 278 | +{ |
| 279 | + // Create a new action |
| 280 | + ActionGrowCut* action = new ActionGrowCut; |
| 281 | + |
| 282 | + // Setup the parameters |
| 283 | + action->data_layer_ = data_layer; |
| 284 | + action->foreground_layer_ = foreground_layer; |
| 285 | + action->background_layer_ = background_layer; |
| 286 | + action->output_layer_ = output_layer; |
| 287 | + action->grow_cutter_ = grow_cutter; |
| 288 | + action->run_smoothing_ = run_smoothing; |
| 289 | + |
| 290 | + // Dispatch action to underlying engine |
| 291 | + Core::ActionDispatcher::PostAndWaitAction( Core::ActionHandle( action ), context ); |
| 292 | +} |
| 293 | +} // end namespace Seg3D |
0 commit comments