OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
FlushVolPredictor Class Reference
Collaboration diagram for FlushVolPredictor:

Public Member Functions

bool predict (const RGB &from, const RGB &to, float &flush)
 FlushVolPredictor (const std::string &data_file)
int get_min_flush_volume ()
 FlushVolPredictor ()=default

Private Types

using RGB = FlushPredict::RGBColor

Private Member Functions

uint64_t generate_hash_key (const RGB &from, const RGB &to)

Private Attributes

std::unordered_map< uint64_t, float > m_flush_map
std::vector< RGB > m_colors
bool m_valid { false }

Detailed Description

Member Typedef Documentation

◆ RGB

Constructor & Destructor Documentation

◆ FlushVolPredictor() [1/2]

FlushVolPredictor::FlushVolPredictor ( const std::string & data_file)
215{
216 auto rgb_hex_to_dec = [](const std::string& hexstr, FlushPredict::RGBColor& color)->bool
217 {
218 if (hexstr.empty() || hexstr.length() != 7 || hexstr[0] != '#')
219 {
220 assert(false);
221 color.r = 0, color.g = 0, color.b = 0;
222 return false;
223 }
224
225 auto hexToByte = [](const std::string& hex)->int
226 {
227 unsigned int byte;
228 std::istringstream(hex) >> std::hex >> byte;
229 return byte;
230 };
231 color.r = hexToByte(hexstr.substr(1, 2));
232 color.g = hexToByte(hexstr.substr(3, 2));
233 color.b = hexToByte(hexstr.substr(5, 2));
234 return true;
235 };
236
237 std::ifstream in(data_file);
238 if (!in.is_open()) {
239 m_valid = false;
240 return;
241 }
242 std::string line;
243 std::getline(in, line); //skip color description line
244 std::getline(in, line);
245 // read and save color lists
246 {
247 std::istringstream in(line);
248 std::string color;
249 while (in >> color) {
250 RGB c;
251 if (!rgb_hex_to_dec(color, c)) {
252 m_valid = false;
253 return;
254 }
255 m_colors.emplace_back(c);
256 }
257 }
258 std::getline(in, line); // skip colume name line
259 while (std::getline(in, line)) {
260 std::istringstream iss(line);
261 std::string rgb_from, rgb_to;
262 float value;
263 if (iss >> rgb_from >> rgb_to >> value) {
264 RGB from,to;
265 // transfer hex str to rgb format
266 if (!rgb_hex_to_dec(rgb_from, from)) {
267 m_valid = false;
268 return;
269 }
270 if (!rgb_hex_to_dec(rgb_to, to)) {
271 m_valid = false;
272 return;
273 }
274 // generate hash key for two rgb color
275 uint64_t key = generate_hash_key(from,to);
276 m_flush_map.emplace(key, value);
277 }
278 else {
279 m_valid = false;
280 return;
281 }
282 }
283 m_valid = true;
284}
uint64_t generate_hash_key(const RGB &from, const RGB &to)
Definition FlushVolPredictor.cpp:202
bool m_valid
Definition FlushVolPredictor.cpp:192
std::vector< RGB > m_colors
Definition FlushVolPredictor.cpp:191
std::unordered_map< uint64_t, float > m_flush_map
Definition FlushVolPredictor.cpp:190
FlushPredict::RGBColor RGB
Definition FlushVolPredictor.cpp:182

References generate_hash_key(), m_colors, m_flush_map, and m_valid.

Here is the call graph for this function:

◆ FlushVolPredictor() [2/2]

FlushVolPredictor::FlushVolPredictor ( )
default

Member Function Documentation

◆ generate_hash_key()

uint64_t FlushVolPredictor::generate_hash_key ( const RGB & from,
const RGB & to )
private
203{
204 uint64_t key = 0;
205 key |= (static_cast<uint64_t>(from.r) << 40);
206 key |= (static_cast<uint64_t>(from.g) << 32);
207 key |= (static_cast<uint64_t>(from.b) << 24);
208 key |= (static_cast<uint64_t>(to.r) << 16);
209 key |= (static_cast<uint64_t>(to.g) << 8);
210 key |= static_cast<uint64_t>(to.b);
211 return key;
212}

References FlushPredict::RGBColor::b, FlushPredict::RGBColor::g, and FlushPredict::RGBColor::r.

Referenced by FlushVolPredictor(), and predict().

Here is the caller graph for this function:

◆ get_min_flush_volume()

int FlushVolPredictor::get_min_flush_volume ( )
196{
197 if(!m_valid)
198 return std::numeric_limits<int>::max();
199 return static_cast<int>(std::min_element(m_flush_map.begin(), m_flush_map.end(), [](const auto& a, const auto& b) {return a.second < b.second; })->second);
200}

References m_flush_map, and m_valid.

◆ predict()

bool FlushVolPredictor::predict ( const RGB & from,
const RGB & to,
float & flush )
287{
288 if (!m_valid)
289 return false;
290
291 // find similar colors in color list
292 std::optional<RGB> similar_from, similar_to;
293 for (auto& color : m_colors) {
294 if (FlushPredict::is_similar_color(color, from)) {
295 similar_from = color;
296 break;
297 }
298 }
299 for (auto& color : m_colors) {
300 if (FlushPredict::is_similar_color(color, to)) {
301 similar_to = color;
302 break;
303 }
304 }
305
306 // `from` and `to` should have similar colors in list
307 if (!similar_from || !similar_to)
308 return false;
309
310 uint64_t key = generate_hash_key(*similar_from,*similar_to);
311 auto iter = m_flush_map.find(key);
312 if (iter == m_flush_map.end())
313 return false;
314
315 flush = iter->second;
316 return true;
317}
bool is_similar_color(const RGBColor &from, const RGBColor &to, float distance_threshold)
Definition FlushVolPredictor.cpp:169

References generate_hash_key(), FlushPredict::is_similar_color(), m_colors, m_flush_map, and m_valid.

Here is the call graph for this function:

Member Data Documentation

◆ m_colors

std::vector<RGB> FlushVolPredictor::m_colors
private

Referenced by FlushVolPredictor(), and predict().

◆ m_flush_map

std::unordered_map<uint64_t, float> FlushVolPredictor::m_flush_map
private

◆ m_valid

bool FlushVolPredictor::m_valid { false }
private
192{ false };

Referenced by FlushVolPredictor(), get_min_flush_volume(), and predict().


The documentation for this class was generated from the following file: