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