Blue Brain BioExplorer
LoadModelFunctor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Daniel.Nachbaur@epfl.ch
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "LoadModelFunctor.h"
23 
24 #include "Errors.h"
25 
32 
33 namespace core
34 {
35 const float TOTAL_PROGRESS = 100.f;
36 
38  : _engine(engine)
39  , _params(params)
40 {
41 }
42 
44 {
45  return _performLoad([&] { return _loadData(std::move(blob), _params); });
46 }
47 
49 {
50  const auto& path = _params.getPath();
51  return _performLoad([&] { return _loadData(path, _params); });
52 }
53 
54 ModelDescriptorPtr LoadModelFunctor::_performLoad(const std::function<ModelDescriptorPtr()>& loadData)
55 {
56  try
57  {
58  return loadData();
59  }
60  catch (const std::exception& e)
61  {
62  progress("Loading failed", (TOTAL_PROGRESS - _currentProgress) / TOTAL_PROGRESS, 1.f);
63  throw LOADING_BINARY_FAILED(e.what());
64  }
65 }
66 
67 ModelDescriptorPtr LoadModelFunctor::_loadData(Blob&& blob, const ModelParams& params)
68 {
69  return _engine.getScene().loadModel(std::move(blob), params, {_getProgressFunc()});
70 }
71 
72 ModelDescriptorPtr LoadModelFunctor::_loadData(const std::string& path, const ModelParams& params)
73 {
74  return _engine.getScene().loadModel(path, params, {_getProgressFunc()});
75 }
76 
77 void LoadModelFunctor::_updateProgress(const std::string& message, const size_t increment)
78 {
79  _currentProgress += increment;
80  progress(message, increment / TOTAL_PROGRESS, _currentProgress / TOTAL_PROGRESS);
81 }
82 
83 std::function<void(std::string, float)> LoadModelFunctor::_getProgressFunc()
84 {
85  return [this](const std::string& msg, const float progress)
86  {
87  cancelCheck();
88  const size_t newProgress = progress * TOTAL_PROGRESS;
89  if (newProgress == 0 || newProgress % size_t(TOTAL_PROGRESS) > _nextTic)
90  {
91  _updateProgress(msg, newProgress - _nextTic);
92  _nextTic = newProgress;
93  }
94  };
95 }
96 } // namespace core
Provides an abstract implementation of a ray-tracing engine.
Definition: Engine.h:59
PLATFORM_API Scene & getScene()
Returns the scene.
Definition: Engine.h:147
ModelDescriptorPtr operator()()
LoadModelFunctor(Engine &engine, const ModelParams &params)
The ModelParams class represents the parameters needed for initializing a model instance.
Definition: Model.h:178
PLATFORM_API const std::string & getPath() const
getPath gets the path of the model
Definition: Model.h:238
PLATFORM_API ModelDescriptorPtr loadModel(Blob &&blob, const ModelParams &params, LoaderProgress cb)
Load a model from the given blob.
Definition: Scene.cpp:214
void progress(const std::string &message, const float increment, const float amount)
Definition: TaskFunctor.h:58
void cancelCheck() const
Definition: TaskFunctor.h:73
std::shared_ptr< ModelDescriptor > ModelDescriptorPtr
Definition: Types.h:112
const float TOTAL_PROGRESS
TaskRuntimeError LOADING_BINARY_FAILED(const std::string &error)
Definition: Errors.h:43