comparison Framework/SmartLoader.cpp @ 334:c34784e5f299 am-2

compatibility fixes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Oct 2018 09:02:24 +0200
parents 7a364e44fbb4
children 8eb4fe74000f
comparison
equal deleted inserted replaced
333:08683537a227 334:c34784e5f299
28 #include "Framework/Layers/FrameRenderer.h" 28 #include "Framework/Layers/FrameRenderer.h"
29 #include "Core/Logging.h" 29 #include "Core/Logging.h"
30 30
31 namespace OrthancStone 31 namespace OrthancStone
32 { 32 {
33 enum CachedSliceStatus 33 enum CachedSliceStatus
34 { 34 {
35 CachedSliceStatus_ScheduledToLoad, 35 CachedSliceStatus_ScheduledToLoad,
36 CachedSliceStatus_GeometryLoaded, 36 CachedSliceStatus_GeometryLoaded,
37 CachedSliceStatus_ImageLoaded 37 CachedSliceStatus_ImageLoaded
38 }; 38 };
39 39
40 class SmartLoader::CachedSlice : public LayerSourceBase 40 class SmartLoader::CachedSlice : public LayerSourceBase
41 { 41 {
42 public: 42 public:
43 unsigned int sliceIndex_; 43 unsigned int sliceIndex_;
45 boost::shared_ptr<Orthanc::ImageAccessor> image_; 45 boost::shared_ptr<Orthanc::ImageAccessor> image_;
46 SliceImageQuality effectiveQuality_; 46 SliceImageQuality effectiveQuality_;
47 CachedSliceStatus status_; 47 CachedSliceStatus status_;
48 48
49 public: 49 public:
50 50 CachedSlice(MessageBroker& broker) :
51 CachedSlice(MessageBroker& broker) 51 LayerSourceBase(broker)
52 : LayerSourceBase(broker) 52 {
53 {} 53 }
54 virtual ~CachedSlice() {} 54
55 virtual ~CachedSlice()
56 {
57 }
55 58
56 virtual bool GetExtent(std::vector<Vector>& points, 59 virtual bool GetExtent(std::vector<Vector>& points,
57 const CoordinateSystem3D& viewportSlice) 60 const CoordinateSystem3D& viewportSlice)
58 { 61 {
59 // TODO: viewportSlice is not used !!!! 62 // TODO: viewportSlice is not used !!!!
95 } 98 }
96 99
97 }; 100 };
98 101
99 102
100 SmartLoader::SmartLoader(MessageBroker& broker, OrthancApiClient& orthancApiClient) : 103 SmartLoader::SmartLoader(MessageBroker& broker,
104 OrthancApiClient& orthancApiClient) :
101 IObservable(broker), 105 IObservable(broker),
102 IObserver(broker), 106 IObserver(broker),
103 imageQuality_(SliceImageQuality_FullPam), 107 imageQuality_(SliceImageQuality_FullPam),
104 orthancApiClient_(orthancApiClient) 108 orthancApiClient_(orthancApiClient)
105 { 109 {
106 } 110 }
107 111
108 void SmartLoader::SetFrameInWidget(LayerWidget& layerWidget, size_t layerIndex, const std::string& instanceId, unsigned int frame) 112 void SmartLoader::SetFrameInWidget(LayerWidget& layerWidget,
113 size_t layerIndex,
114 const std::string& instanceId,
115 unsigned int frame)
109 { 116 {
110 // TODO: check if this frame has already been loaded or is already being loaded. 117 // TODO: check if this frame has already been loaded or is already being loaded.
111 // - if already loaded: create a "clone" that will emit the GeometryReady/ImageReady messages "immediately" 118 // - if already loaded: create a "clone" that will emit the GeometryReady/ImageReady messages "immediately"
112 // (it can not be immediate because Observers needs to register first and this is done after this method returns) 119 // (it can not be immediate because Observers needs to register first and this is done after this method returns)
113 // - if currently loading, we need to return an object that will observe the existing LayerSource and forward 120 // - if currently loading, we need to return an object that will observe the existing LayerSource and forward
114 // the messages to its observables 121 // the messages to its observables
115 // in both cases, we must be carefull about objects lifecycle !!! 122 // in both cases, we must be carefull about objects lifecycle !!!
116 123
117 std::auto_ptr<ILayerSource> layerSource; 124 std::auto_ptr<ILayerSource> layerSource;
118 std::string sliceKeyId = instanceId + ":" + std::to_string(frame); 125 std::string sliceKeyId = instanceId + ":" + boost::lexical_cast<std::string>(frame);
119 SmartLoader::CachedSlice* cachedSlice = NULL; 126 SmartLoader::CachedSlice* cachedSlice = NULL;
120 127
121 if (cachedSlices_.find(sliceKeyId) != cachedSlices_.end()) // && cachedSlices_[sliceKeyId]->status_ == CachedSliceStatus_Loaded) 128 if (cachedSlices_.find(sliceKeyId) != cachedSlices_.end()) // && cachedSlices_[sliceKeyId]->status_ == CachedSliceStatus_Loaded)
122 { 129 {
123 layerSource.reset(cachedSlices_[sliceKeyId]->Clone()); 130 layerSource.reset(cachedSlices_[sliceKeyId]->Clone());
152 cachedSlice->NotifyGeometryReady(); 159 cachedSlice->NotifyGeometryReady();
153 } 160 }
154 161
155 } 162 }
156 163
157 void SmartLoader::PreloadSlice(const std::string instanceId, unsigned int frame) 164 void SmartLoader::PreloadSlice(const std::string instanceId,
165 unsigned int frame)
158 { 166 {
159 // TODO: reactivate -> need to be able to ScheduleLayerLoading in ILayerSource without calling ScheduleLayerCreation 167 // TODO: reactivate -> need to be able to ScheduleLayerLoading in ILayerSource without calling ScheduleLayerCreation
160 return; 168 return;
161 // TODO: check if it is already in the cache 169 // TODO: check if it is already in the cache
162 170
164 172
165 // create the slice in the cache with "empty" data 173 // create the slice in the cache with "empty" data
166 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_)); 174 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_));
167 cachedSlice->slice_.reset(new Slice(instanceId, frame)); 175 cachedSlice->slice_.reset(new Slice(instanceId, frame));
168 cachedSlice->status_ = CachedSliceStatus_ScheduledToLoad; 176 cachedSlice->status_ = CachedSliceStatus_ScheduledToLoad;
169 std::string sliceKeyId = instanceId + ":" + std::to_string(frame); 177 std::string sliceKeyId = instanceId + ":" + boost::lexical_cast<std::string>(frame);
170 178
171 LOG(WARNING) << "Will preload: " << sliceKeyId; 179 LOG(WARNING) << "Will preload: " << sliceKeyId;
172 180
173 cachedSlices_[sliceKeyId] = boost::shared_ptr<CachedSlice>(cachedSlice); 181 cachedSlices_[sliceKeyId] = boost::shared_ptr<CachedSlice>(cachedSlice);
174 182
200 { 208 {
201 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_); 209 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_);
202 210
203 // save/replace the slice in cache 211 // save/replace the slice in cache
204 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() 212 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount()
205 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame()); 213 std::string sliceKeyId = (slice.GetOrthancInstanceId() + ":" +
214 boost::lexical_cast<std::string>(slice.GetFrame()));
206 215
207 LOG(WARNING) << "Geometry ready: " << sliceKeyId; 216 LOG(WARNING) << "Geometry ready: " << sliceKeyId;
208 217
209 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_)); 218 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_));
210 cachedSlice->slice_.reset(slice.Clone()); 219 cachedSlice->slice_.reset(slice.Clone());
215 224
216 // re-emit original Layer message to observers 225 // re-emit original Layer message to observers
217 EmitMessage(message); 226 EmitMessage(message);
218 } 227 }
219 228
229
220 void SmartLoader::OnImageReady(const ILayerSource::ImageReadyMessage& message) 230 void SmartLoader::OnImageReady(const ILayerSource::ImageReadyMessage& message)
221 { 231 {
222 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_); 232 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_);
223 233
224 // save/replace the slice in cache 234 // save/replace the slice in cache
225 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ? 235 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ?
226 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame()); 236 std::string sliceKeyId = (slice.GetOrthancInstanceId() + ":" +
237 boost::lexical_cast<std::string>(slice.GetFrame()));
227 238
228 LOG(WARNING) << "Image ready: " << sliceKeyId; 239 LOG(WARNING) << "Image ready: " << sliceKeyId;
229 240
230 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_)); 241 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_));
231 cachedSlice->image_ = message.image_; 242 cachedSlice->image_ = message.image_;
237 248
238 // re-emit original Layer message to observers 249 // re-emit original Layer message to observers
239 EmitMessage(message); 250 EmitMessage(message);
240 } 251 }
241 252
253
242 void SmartLoader::OnLayerReady(const ILayerSource::LayerReadyMessage& message) 254 void SmartLoader::OnLayerReady(const ILayerSource::LayerReadyMessage& message)
243 { 255 {
244 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_); 256 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_);
245 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ? 257 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ?
246 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame()); 258 std::string sliceKeyId = (slice.GetOrthancInstanceId() + ":" +
259 boost::lexical_cast<std::string>(slice.GetFrame()));
247 260
248 LOG(WARNING) << "Layer ready: " << sliceKeyId; 261 LOG(WARNING) << "Layer ready: " << sliceKeyId;
249 262
250 // remove the slice from the preloading slices now that it has been fully loaded and it is referenced in the cache 263 // remove the slice from the preloading slices now that it has been fully loaded and it is referenced in the cache
251 if (preloadingInstances_.find(sliceKeyId) != preloadingInstances_.end()) 264 if (preloadingInstances_.find(sliceKeyId) != preloadingInstances_.end())
254 } 267 }
255 268
256 // re-emit original Layer message to observers 269 // re-emit original Layer message to observers
257 EmitMessage(message); 270 EmitMessage(message);
258 } 271 }
259
260 } 272 }