NeuralEngine
A Game Engine with embeded Machine Learning algorithms based on Gaussian Processes.
FgCVPlotInternal.h
1
11#pragma once
12
13#include <MachineLearning/FgColor.h>
14#include <opencv2/core/core.hpp>
15
16#include <iomanip>
17#include <iostream>
18
19#define EXPECT_EQ(a__, b__) \
20 do { \
21 if ((a__) != (b__)) { \
22 std::cerr << "Incorrect " << #a__ << " (" << (a__) << "), should equal " \
23 << (b__) << std::endl; \
24 exit(-1); \
25 } \
26 } while (0)
27
28namespace NeuralEngine
29{
30 namespace MachineLearning
31 {
32 static const int paleness = 32;
33
34 static uint8_t channel2pale(uint8_t c) {
35 return c * (255 - 2 * paleness) / 255 + paleness;
36 }
37
38 static cv::Scalar color2scalar(const Color& color) {
39 return cv::Scalar(channel2pale(color.b), channel2pale(color.g),
40 channel2pale(color.r));
41 }
42
43 static float value2snap(float value) {
44 return std::max({ pow(10, floor(log10(value))),
45 pow(10, floor(log10(value / 2))) * 2,
46 pow(10, floor(log10(value / 5))) * 5 });
47 }
48
49 class NE_IMPEXP Trans {
50 public:
51 Trans(void* buffer) : Trans(*(cv::Mat*)buffer) {}
52
53 Trans(cv::Mat& buffer) : original_(buffer), alpha_(0), interim_(NULL) {}
54
55 Trans(cv::Mat& buffer, int alpha) : Trans(buffer) { setup(alpha); }
56
57 ~Trans() { flush(); }
58
59 cv::Mat& get() const { return (interim_ != NULL ? *interim_ : original_); }
60
61 void setup(int alpha) {
62 bool transparent = (alpha != 255);
63 if (transparent) {
64 interim_ = new cv::Mat();
65 original_.copyTo(*interim_);
66 }
67 alpha_ = alpha;
68 }
69
70 void flush() {
71 if (interim_) {
72 // std::cerr << "blending " << alpha_ << std::endl;
73 auto weight = alpha_ / 255.f;
74 cv::addWeighted(*interim_, weight, original_, 1 - weight, 0, original_);
75 delete interim_;
76 interim_ = NULL;
77 }
78 }
79
80 cv::Mat& with(int alpha) {
81 if (alpha != alpha_) {
82 flush();
83 setup(alpha);
84 }
85 return get();
86 }
87
88 cv::Mat& with(const Color& color) { return with(color.a); }
89
90 protected:
91 int alpha_;
92 cv::Mat& original_;
93 cv::Mat* interim_;
94 };
95 }
96}