-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNNRenderer.cpp
More file actions
86 lines (84 loc) · 2.87 KB
/
NNRenderer.cpp
File metadata and controls
86 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "NNRenderer.hpp"
namespace EvoAI{
NNRenderer::NNRenderer(NeuralNetwork* NN)
: neurons()
, connections()
, nn(NN){
font = sf::Font();
if(!font.loadFromFile("EvoAI/data/fonts/MonaspaceRadon-Regular.otf")){
throw std::runtime_error("Cannot Load Font -> EvoAI/data/fonts/MonaspaceRadon-Regular.otf");
}
setUp();
}
void NNRenderer::Render(sf::RenderWindow& win, bool renderTexts) noexcept{
for(auto& ci:connections){
if(renderTexts){
win.draw(ci.info);
}
win.draw(ci.connectionLine, 2, sf::Lines);
}
for(auto& ni:neurons){
if(renderTexts){
win.draw(ni.info);
}
win.draw(ni.neuronShape);
}
}
void NNRenderer::setNeuralNetwork(NeuralNetwork* NN) noexcept{
neurons.clear();
connections.clear();
this->nn = NN;
setUp();
}
void NNRenderer::setFont(sf::Font f) noexcept{
font = f;
}
/// private
void NNRenderer::setUp() noexcept{
sf::Vector2f position(5.0,5.0);
sf::Vector2f space(0.0,150.0);
for(auto& l: nn->getLayers()){
for(auto& neuron:l.getNeurons()){
setUpNeuronInfo(neuron,position);
position += space;
}
position = sf::Vector2f(position.x,5.0);
position += sf::Vector2f(270.0,0.0);
}
}
void NNRenderer::setUpNeuronInfo(Neuron& n, sf::Vector2f& p) noexcept{
NeuronInfo ni;
const int textSize = 15;
ni.info = sf::Text(n.toString("\n"), font, textSize);
ni.info.setPosition(sf::Vector2f(p.x+5.0,p.y+10));
ni.neuronShape = sf::CircleShape(6);
ni.neuronShape.setPosition(p);
ni.neuronShape.setFillColor(sf::Color::Red);
neurons.emplace_back(ni);
int space = 10;
bool change = false;
for(auto& c:n.getConnections()){
if(change){
setUpConnectionInfo(c,p, space);
}else{
setUpConnectionInfo(c,p, -space);
}
change = !change;
}
}
void NNRenderer::setUpConnectionInfo(Connection& c, sf::Vector2f& p, int space) noexcept{
const int textSize = 15;
ConnectionInfo ci;
sf::Color color = sf::Color::White;
if(c.isRecurrent()){
color = sf::Color::Red;
}
ci.connectionLine[0] = sf::Vertex(sf::Vector2f(p.x+5,p.y+5),color);
auto dlyr = c.getDest().layer * 270.0 + 10;
auto dnrn = c.getDest().neuron * 150.0 + 10;
ci.connectionLine[1] = sf::Vertex(sf::Vector2f(dlyr,dnrn),color);
ci.info = sf::Text("W: " + std::to_string(c.getWeight()),font, textSize);
ci.info.setPosition(sf::Vector2f(((p.x + dlyr) / 2),(p.y + dnrn) / 2 - space));
connections.emplace_back(ci);
}
}