1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "printconvmap.h" |
---|
6 | #include <stdio.h> |
---|
7 | |
---|
8 | void printN(const char* t, int maxlen) |
---|
9 | { |
---|
10 | while (maxlen-- > 0) |
---|
11 | if (*t) putchar(*(t++)); |
---|
12 | else putchar(' '); |
---|
13 | } |
---|
14 | |
---|
15 | void printN(char t, int maxlen) |
---|
16 | { |
---|
17 | while (maxlen-- > 0) putchar(t); |
---|
18 | } |
---|
19 | |
---|
20 | void printmapping(const char* gen1, int len1, const MultiRange &mr, const char* gen2, int len2, int left_column_padding) |
---|
21 | { |
---|
22 | printN(' ', left_column_padding - len1); |
---|
23 | printN(gen1, len1); |
---|
24 | printf(" : "); |
---|
25 | int i; |
---|
26 | for (i = 0; i < len2; i++) |
---|
27 | if (mr.contains(i)) |
---|
28 | putchar(gen2[i]); |
---|
29 | else |
---|
30 | putchar('.'); |
---|
31 | putchar('\n'); |
---|
32 | } |
---|
33 | |
---|
34 | void stripstring(SString &str) |
---|
35 | { |
---|
36 | char *t = str.directWrite(); |
---|
37 | for (; *t; t++) |
---|
38 | if (strchr("\n\r\t", *t)) *t = ' '; |
---|
39 | } |
---|
40 | |
---|
41 | void printConvMap(const SString& gen1, const SString& gen2, const MultiMap& map, int left_column_padding) |
---|
42 | { |
---|
43 | int y, y2, len1; |
---|
44 | int id = 0; |
---|
45 | if (map.isEmpty()) |
---|
46 | { |
---|
47 | printf("{ empty }\n"); |
---|
48 | return; |
---|
49 | } |
---|
50 | len1 = gen1.length(); |
---|
51 | SString g1 = gen1; |
---|
52 | stripstring(g1); |
---|
53 | SString g2 = gen2; |
---|
54 | stripstring(g2); |
---|
55 | const char* g = g1.c_str(); |
---|
56 | y = 0; |
---|
57 | MultiRange *mr; |
---|
58 | MultiRange emptyrange; |
---|
59 | printN(' ', left_column_padding); |
---|
60 | printf(" %s\n", g2.c_str()); |
---|
61 | int begin = map.getBegin(); |
---|
62 | int end = map.getEnd(); |
---|
63 | while (y < len1) |
---|
64 | { |
---|
65 | if (y < begin) |
---|
66 | { |
---|
67 | mr = &emptyrange; |
---|
68 | y2 = begin; |
---|
69 | } |
---|
70 | else if (y > end) |
---|
71 | { |
---|
72 | mr = &emptyrange; |
---|
73 | y2 = len1; |
---|
74 | } |
---|
75 | else { |
---|
76 | id = map.findMappingId(y); |
---|
77 | mr = &map.getMapping(id)->to; |
---|
78 | y2 = map.getMapping(id + 1)->begin; |
---|
79 | } |
---|
80 | if ((y2 - y) > left_column_padding) y2 = y + left_column_padding; |
---|
81 | if (y2 > (y + len1)) y2 = y + len1; |
---|
82 | printmapping(g + y, y2 - y, *mr, g2.c_str(), g2.length(), left_column_padding); |
---|
83 | y = y2; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | ModelDisplayMap::ModelDisplayMap(Model &m) |
---|
88 | :model(m) |
---|
89 | { |
---|
90 | joint_offset = max(10, (int(9 + model.getPartCount()) / 10) * 10); |
---|
91 | neuron_offset = joint_offset + max(10, (int(9 + model.getJointCount()) / 10) * 10); |
---|
92 | max_element = neuron_offset + max(10, (int(9 + model.getNeuroCount()) / 10) * 10); |
---|
93 | for (int i = 0; i < model.getPartCount(); i++) |
---|
94 | map.add(Model::partToMap(i), Model::partToMap(i), i, i); |
---|
95 | for (int i = 0; i < model.getJointCount(); i++) |
---|
96 | map.add(Model::jointToMap(i), Model::jointToMap(i), joint_offset + i, joint_offset + i); |
---|
97 | for (int i = 0; i < model.getNeuroCount(); i++) |
---|
98 | map.add(Model::neuroToMap(i), Model::neuroToMap(i), neuron_offset + i, neuron_offset + i); |
---|
99 | } |
---|
100 | |
---|
101 | void ModelDisplayMap::print(int left_column_padding) |
---|
102 | { |
---|
103 | MultiMap combined_map; |
---|
104 | combined_map.addCombined(model.getMap(), getMap()); |
---|
105 | static const SString t09("0123456789"); |
---|
106 | SString g; |
---|
107 | for (int i = 0; i < max_element; i += 10) |
---|
108 | g += t09; |
---|
109 | printN(' ', left_column_padding); |
---|
110 | printf(" Parts "); |
---|
111 | printN(' ', joint_offset - 10); |
---|
112 | printf("Joints "); |
---|
113 | printN(' ', (neuron_offset - joint_offset) - 10); |
---|
114 | printf("Neurons\n"); |
---|
115 | printConvMap(model.getGeno().getGenes(), g, combined_map, left_column_padding); |
---|
116 | } |
---|