comparison Framework/SmartLoader.cpp @ 330:7a364e44fbb4 am-2

renamed SetDefaultView in FitContent
author am@osimis.io
date Wed, 17 Oct 2018 12:45:52 +0200
parents a902a07769d4
children c34784e5f299
comparison
equal deleted inserted replaced
329:b10dfdb96866 330:7a364e44fbb4
24 #include "Messages/MessageForwarder.h" 24 #include "Messages/MessageForwarder.h"
25 #include "Core/Images/Image.h" 25 #include "Core/Images/Image.h"
26 #include "Framework/Widgets/LayerWidget.h" 26 #include "Framework/Widgets/LayerWidget.h"
27 #include "Framework/StoneException.h" 27 #include "Framework/StoneException.h"
28 #include "Framework/Layers/FrameRenderer.h" 28 #include "Framework/Layers/FrameRenderer.h"
29 #include "Core/Logging.h"
29 30
30 namespace OrthancStone 31 namespace OrthancStone
31 { 32 {
32 enum CachedSliceStatus 33 enum CachedSliceStatus
33 { 34 {
48 public: 49 public:
49 50
50 CachedSlice(MessageBroker& broker) 51 CachedSlice(MessageBroker& broker)
51 : LayerSourceBase(broker) 52 : LayerSourceBase(broker)
52 {} 53 {}
54 virtual ~CachedSlice() {}
53 55
54 virtual bool GetExtent(std::vector<Vector>& points, 56 virtual bool GetExtent(std::vector<Vector>& points,
55 const CoordinateSystem3D& viewportSlice) 57 const CoordinateSystem3D& viewportSlice)
56 { 58 {
57 // TODO: viewportSlice is not used !!!! 59 // TODO: viewportSlice is not used !!!!
61 63
62 virtual void ScheduleLayerCreation(const CoordinateSystem3D& viewportSlice) 64 virtual void ScheduleLayerCreation(const CoordinateSystem3D& viewportSlice)
63 { 65 {
64 // TODO: viewportSlice is not used !!!! 66 // TODO: viewportSlice is not used !!!!
65 67
66 // it has already been loaded -> trigger the "layer ready" message immediately 68 // it has already been loaded -> trigger the "layer ready" message immediately otherwise, do nothing now. The LayerReady will be triggered
67 bool isFull = (effectiveQuality_ == SliceImageQuality_FullPng || effectiveQuality_ == SliceImageQuality_FullPam); 69 // once the LayerSource is ready
68 std::auto_ptr<Orthanc::ImageAccessor> accessor(new Orthanc::ImageAccessor()); 70 if (status_ == CachedSliceStatus_ImageLoaded)
69 image_->GetReadOnlyAccessor(*accessor); 71 {
70 LayerSourceBase::NotifyLayerReady(FrameRenderer::CreateRenderer(accessor.release(), *slice_, isFull), 72 LOG(WARNING) << "ScheduleLayerCreation for CachedSlice (image is loaded): " << slice_->GetOrthancInstanceId();
71 slice_->GetGeometry(), false); 73 bool isFull = (effectiveQuality_ == SliceImageQuality_FullPng || effectiveQuality_ == SliceImageQuality_FullPam);
74 std::auto_ptr<Orthanc::ImageAccessor> accessor(new Orthanc::ImageAccessor());
75 image_->GetReadOnlyAccessor(*accessor);
76 LayerSourceBase::NotifyLayerReady(FrameRenderer::CreateRenderer(accessor.release(), *slice_, isFull),
77 slice_->GetGeometry(), false);
78 }
79 else
80 {
81 LOG(WARNING) << "ScheduleLayerCreation for CachedSlice (image is not loaded yet): " << slice_->GetOrthancInstanceId();
82 }
72 } 83 }
73 84
74 CachedSlice* Clone() const 85 CachedSlice* Clone() const
75 { 86 {
76 CachedSlice* output = new CachedSlice(broker_); 87 CachedSlice* output = new CachedSlice(broker_);
103 // the messages to its observables 114 // the messages to its observables
104 // in both cases, we must be carefull about objects lifecycle !!! 115 // in both cases, we must be carefull about objects lifecycle !!!
105 116
106 std::auto_ptr<ILayerSource> layerSource; 117 std::auto_ptr<ILayerSource> layerSource;
107 std::string sliceKeyId = instanceId + ":" + std::to_string(frame); 118 std::string sliceKeyId = instanceId + ":" + std::to_string(frame);
119 SmartLoader::CachedSlice* cachedSlice = NULL;
108 120
109 if (cachedSlices_.find(sliceKeyId) != cachedSlices_.end()) // && cachedSlices_[sliceKeyId]->status_ == CachedSliceStatus_Loaded) 121 if (cachedSlices_.find(sliceKeyId) != cachedSlices_.end()) // && cachedSlices_[sliceKeyId]->status_ == CachedSliceStatus_Loaded)
110 { 122 {
111 layerSource.reset(cachedSlices_[sliceKeyId]->Clone()); 123 layerSource.reset(cachedSlices_[sliceKeyId]->Clone());
124 cachedSlice = dynamic_cast<SmartLoader::CachedSlice*>(layerSource.get());
112 } 125 }
113 else 126 else
114 { 127 {
115 layerSource.reset(new OrthancFrameLayerSource(IObserver::broker_, orthancApiClient_)); 128 layerSource.reset(new OrthancFrameLayerSource(IObserver::broker_, orthancApiClient_));
116 dynamic_cast<OrthancFrameLayerSource*>(layerSource.get())->SetImageQuality(imageQuality_); 129 dynamic_cast<OrthancFrameLayerSource*>(layerSource.get())->SetImageQuality(imageQuality_);
132 else 145 else
133 { 146 {
134 throw StoneException(ErrorCode_CanOnlyAddOneLayerAtATime); 147 throw StoneException(ErrorCode_CanOnlyAddOneLayerAtATime);
135 } 148 }
136 149
137 SmartLoader::CachedSlice* cachedSlice = dynamic_cast<SmartLoader::CachedSlice*>(layerSource.get());
138 if (cachedSlice != NULL) 150 if (cachedSlice != NULL)
139 { 151 {
140 cachedSlice->NotifyGeometryReady(); 152 cachedSlice->NotifyGeometryReady();
141 } 153 }
142 154
143 } 155 }
144 156
145 void SmartLoader::PreloadSlice(const std::string instanceId, unsigned int frame) 157 void SmartLoader::PreloadSlice(const std::string instanceId, unsigned int frame)
146 { 158 {
159 // TODO: reactivate -> need to be able to ScheduleLayerLoading in ILayerSource without calling ScheduleLayerCreation
160 return;
147 // TODO: check if it is already in the cache 161 // TODO: check if it is already in the cache
162
163
148 164
149 // create the slice in the cache with "empty" data 165 // create the slice in the cache with "empty" data
150 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_)); 166 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_));
151 cachedSlice->slice_.reset(new Slice(instanceId, frame)); 167 cachedSlice->slice_.reset(new Slice(instanceId, frame));
152 cachedSlice->status_ = CachedSliceStatus_ScheduledToLoad; 168 cachedSlice->status_ = CachedSliceStatus_ScheduledToLoad;
153 std::string sliceKeyId = instanceId + ":" + std::to_string(frame); 169 std::string sliceKeyId = instanceId + ":" + std::to_string(frame);
154 170
171 LOG(WARNING) << "Will preload: " << sliceKeyId;
172
155 cachedSlices_[sliceKeyId] = boost::shared_ptr<CachedSlice>(cachedSlice); 173 cachedSlices_[sliceKeyId] = boost::shared_ptr<CachedSlice>(cachedSlice);
156 174
157 std::auto_ptr<ILayerSource> layerSource(new OrthancFrameLayerSource(IObserver::broker_, orthancApiClient_)); 175 std::auto_ptr<ILayerSource> layerSource(new OrthancFrameLayerSource(IObserver::broker_, orthancApiClient_));
158 176
159 dynamic_cast<OrthancFrameLayerSource*>(layerSource.get())->SetImageQuality(imageQuality_); 177 dynamic_cast<OrthancFrameLayerSource*>(layerSource.get())->SetImageQuality(imageQuality_);
184 202
185 // save/replace the slice in cache 203 // save/replace the slice in cache
186 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() 204 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount()
187 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame()); 205 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame());
188 206
207 LOG(WARNING) << "Geometry ready: " << sliceKeyId;
208
189 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_)); 209 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_));
190 cachedSlice->slice_.reset(slice.Clone()); 210 cachedSlice->slice_.reset(slice.Clone());
191 cachedSlice->effectiveQuality_ = source.GetImageQuality(); 211 cachedSlice->effectiveQuality_ = source.GetImageQuality();
192 cachedSlice->status_ = CachedSliceStatus_GeometryLoaded; 212 cachedSlice->status_ = CachedSliceStatus_GeometryLoaded;
193 213
202 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_); 222 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_);
203 223
204 // save/replace the slice in cache 224 // save/replace the slice in cache
205 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ? 225 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ?
206 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame()); 226 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame());
227
228 LOG(WARNING) << "Image ready: " << sliceKeyId;
207 229
208 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_)); 230 boost::shared_ptr<CachedSlice> cachedSlice(new CachedSlice(IObserver::broker_));
209 cachedSlice->image_ = message.image_; 231 cachedSlice->image_ = message.image_;
210 cachedSlice->effectiveQuality_ = message.imageQuality_; 232 cachedSlice->effectiveQuality_ = message.imageQuality_;
211 cachedSlice->slice_.reset(message.slice_.Clone()); 233 cachedSlice->slice_.reset(message.slice_.Clone());
221 { 243 {
222 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_); 244 OrthancFrameLayerSource& source = dynamic_cast<OrthancFrameLayerSource&>(message.origin_);
223 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ? 245 const Slice& slice = source.GetSlice(0); // TODO handle GetSliceCount() ?
224 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame()); 246 std::string sliceKeyId = slice.GetOrthancInstanceId() + ":" + std::to_string(slice.GetFrame());
225 247
248 LOG(WARNING) << "Layer ready: " << sliceKeyId;
249
226 // remove the slice from the preloading slices now that it has been fully loaded and it is referenced in the cache 250 // remove the slice from the preloading slices now that it has been fully loaded and it is referenced in the cache
227 if (preloadingInstances_.find(sliceKeyId) != preloadingInstances_.end()) 251 if (preloadingInstances_.find(sliceKeyId) != preloadingInstances_.end())
228 { 252 {
229 preloadingInstances_.erase(sliceKeyId); 253 preloadingInstances_.erase(sliceKeyId);
230 } 254 }