[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[1130] | 2 | // Copyright (C) 1999-2021 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "printconvmap.h" |
---|
| 6 | #include <stdio.h> |
---|
| 7 | |
---|
[732] | 8 | void printN(const char* t, int maxlen) |
---|
[109] | 9 | { |
---|
[732] | 10 | while (maxlen-- > 0) |
---|
| 11 | if (*t) putchar(*(t++)); |
---|
| 12 | else putchar(' '); |
---|
[109] | 13 | } |
---|
| 14 | |
---|
[732] | 15 | void printN(char t, int maxlen) |
---|
[109] | 16 | { |
---|
[732] | 17 | while (maxlen-- > 0) putchar(t); |
---|
[109] | 18 | } |
---|
| 19 | |
---|
[739] | 20 | void printmapping(const char* gen1, int len1, const MultiRange &mr, const char* gen2, int len2, int left_column_padding) |
---|
[109] | 21 | { |
---|
[739] | 22 | printN(' ', left_column_padding - len1); |
---|
[732] | 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'); |
---|
[109] | 32 | } |
---|
| 33 | |
---|
| 34 | void stripstring(SString &str) |
---|
| 35 | { |
---|
[732] | 36 | char *t = str.directWrite(); |
---|
| 37 | for (; *t; t++) |
---|
| 38 | if (strchr("\n\r\t", *t)) *t = ' '; |
---|
[109] | 39 | } |
---|
| 40 | |
---|
[739] | 41 | void printConvMap(const SString& gen1, const SString& gen2, const MultiMap& map, int left_column_padding) |
---|
[109] | 42 | { |
---|
[732] | 43 | int y, y2, len1; |
---|
| 44 | int id = 0; |
---|
| 45 | if (map.isEmpty()) |
---|
[109] | 46 | { |
---|
[732] | 47 | printf("{ empty }\n"); |
---|
| 48 | return; |
---|
[109] | 49 | } |
---|
[973] | 50 | len1 = gen1.length(); |
---|
[732] | 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; |
---|
[739] | 59 | printN(' ', left_column_padding); |
---|
[732] | 60 | printf(" %s\n", g2.c_str()); |
---|
| 61 | int begin = map.getBegin(); |
---|
| 62 | int end = map.getEnd(); |
---|
| 63 | while (y < len1) |
---|
[109] | 64 | { |
---|
[732] | 65 | if (y < begin) |
---|
[109] | 66 | { |
---|
[732] | 67 | mr = &emptyrange; |
---|
| 68 | y2 = begin; |
---|
[109] | 69 | } |
---|
[732] | 70 | else if (y > end) |
---|
[109] | 71 | { |
---|
[732] | 72 | mr = &emptyrange; |
---|
| 73 | y2 = len1; |
---|
[109] | 74 | } |
---|
[973] | 75 | else { |
---|
[732] | 76 | id = map.findMappingId(y); |
---|
| 77 | mr = &map.getMapping(id)->to; |
---|
| 78 | y2 = map.getMapping(id + 1)->begin; |
---|
[109] | 79 | } |
---|
[739] | 80 | if ((y2 - y) > left_column_padding) y2 = y + left_column_padding; |
---|
[732] | 81 | if (y2 > (y + len1)) y2 = y + len1; |
---|
[973] | 82 | printmapping(g + y, y2 - y, *mr, g2.c_str(), g2.length(), left_column_padding); |
---|
[732] | 83 | y = y2; |
---|
[109] | 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[739] | 87 | ModelDisplayMap::ModelDisplayMap(Model &m) |
---|
| 88 | :model(m) |
---|
[109] | 89 | { |
---|
[1130] | 90 | joint_offset = std::max(10, (int(9 + model.getPartCount()) / 10) * 10); |
---|
| 91 | neuron_offset = joint_offset + std::max(10, (int(9 + model.getJointCount()) / 10) * 10); |
---|
| 92 | max_element = neuron_offset + std::max(10, (int(9 + model.getNeuroCount()) / 10) * 10); |
---|
[739] | 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); |
---|
[109] | 99 | } |
---|
[732] | 100 | |
---|
[739] | 101 | void ModelDisplayMap::print(int left_column_padding) |
---|
[732] | 102 | { |
---|
| 103 | MultiMap combined_map; |
---|
[739] | 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); |
---|
[732] | 116 | } |
---|