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 "geometrytestutils.h" |
---|
6 | #include "../genotypeloader.h" |
---|
7 | #include "frams/genetics/preconfigured.h" |
---|
8 | #include <common/virtfile/stdiofile.h> |
---|
9 | #include <common/loggers/loggertostdout.h> |
---|
10 | #include <frams/model/geometry/geometryutils.h> |
---|
11 | #include <math.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <time.h> |
---|
15 | |
---|
16 | int printGenotypesList(const char *file) |
---|
17 | { |
---|
18 | long count = 0; |
---|
19 | long totalSize = 0; |
---|
20 | GenotypeMiniLoader loader(file); |
---|
21 | GenotypeMini *genotype; |
---|
22 | |
---|
23 | while (genotype = loader.loadNextGenotype()) |
---|
24 | { |
---|
25 | count++; |
---|
26 | totalSize += genotype->genotype.length(); |
---|
27 | |
---|
28 | fprintf(stderr, "%d. (%6d chars) %s\n", count, genotype->genotype.length(), |
---|
29 | genotype->name.c_str()); |
---|
30 | } |
---|
31 | |
---|
32 | if (loader.getStatus() == GenotypeMiniLoader::OnError) |
---|
33 | { |
---|
34 | fprintf(stderr, "Error: %s\n", loader.getError().c_str()); |
---|
35 | return 2; |
---|
36 | } |
---|
37 | else |
---|
38 | { |
---|
39 | fprintf(stderr, "\ntotal: %d items, %d chars\n", count, totalSize); |
---|
40 | return 0; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | class TestInvoker |
---|
45 | { |
---|
46 | public: |
---|
47 | virtual void operator()(Model &model) = 0; |
---|
48 | virtual ~TestInvoker() {} |
---|
49 | }; |
---|
50 | |
---|
51 | int executeTestUsingLoadedModel(const char *file, const char *genoId, TestInvoker &test) |
---|
52 | { |
---|
53 | const char* genoName = genoId; |
---|
54 | const int genoIndex = isdigit(genoId[0]) ? atol(genoId) : 0; |
---|
55 | long count = 0; |
---|
56 | GenotypeMiniLoader loader(file); |
---|
57 | GenotypeMini *genotype; |
---|
58 | |
---|
59 | while (genotype = loader.loadNextGenotype()) |
---|
60 | { |
---|
61 | count++; |
---|
62 | |
---|
63 | if ((genoIndex == count) || (strcmp(genotype->name.c_str(), genoName) == 0)) |
---|
64 | { |
---|
65 | Model model(genotype->genotype, Model::SHAPETYPE_UNKNOWN); |
---|
66 | |
---|
67 | if (!model.isValid()) |
---|
68 | { |
---|
69 | fprintf(stderr, "Cannot build a valid Model from this genotype!\n"); |
---|
70 | return 4; |
---|
71 | } |
---|
72 | SolidsShapeTypeModel sst_model(model); |
---|
73 | test(sst_model); |
---|
74 | return 0; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | if (loader.getStatus() == GenotypeMiniLoader::OnError) |
---|
79 | { |
---|
80 | fprintf(stderr, "Error: %s\n", loader.getError().c_str()); |
---|
81 | return 2; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | fprintf(stderr, "Genotype %s not found in %s\n", genoId, file); |
---|
86 | return 3; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | int executeTestUsingRandomModel(int shape, TestInvoker &test) |
---|
91 | { |
---|
92 | Model model; |
---|
93 | model.open(); |
---|
94 | |
---|
95 | if ((shape < 1) || (shape > 3)) |
---|
96 | { |
---|
97 | shape = (rand() % 3) + 1; |
---|
98 | } |
---|
99 | |
---|
100 | Part *part = model.addNewPart(Part::Shape(shape)); |
---|
101 | GeometryUtils::randomizePositionScaleAndOrient(part); |
---|
102 | |
---|
103 | model.close(); |
---|
104 | test(model); |
---|
105 | GeometryTestUtils::describePart(part, stdout); |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | class ModelBasedTestInvoker : public TestInvoker |
---|
110 | { |
---|
111 | private: |
---|
112 | void(*test)(Model &); |
---|
113 | public: |
---|
114 | ModelBasedTestInvoker(void(*_test)(Model &)) : |
---|
115 | test(_test) |
---|
116 | {} |
---|
117 | void operator()(Model &model) |
---|
118 | { |
---|
119 | test(model); |
---|
120 | } |
---|
121 | }; |
---|
122 | |
---|
123 | int GeometryTestUtils::execute(const SString header, int argc, char *argv[], void(*test)(Model &)) |
---|
124 | { |
---|
125 | LoggerToStdout messages_to_stdout(LoggerBase::Enable); //comment this object out to mute error/warning messages |
---|
126 | StdioFileSystem_autoselect stdiofilesys; |
---|
127 | PreconfiguredGenetics genetics; |
---|
128 | |
---|
129 | srand(time(NULL)); |
---|
130 | |
---|
131 | if ((argc == 3) && (strcmp("-l", argv[1]) == 0)) |
---|
132 | { |
---|
133 | return printGenotypesList(argv[2]); |
---|
134 | } |
---|
135 | |
---|
136 | if ((argc == 4) && (strcmp("-l", argv[1]) == 0)) |
---|
137 | { |
---|
138 | ModelBasedTestInvoker invoker(test); |
---|
139 | return executeTestUsingLoadedModel(argv[2], argv[3], invoker); |
---|
140 | } |
---|
141 | |
---|
142 | if ((argc == 2) && (strcmp("-c", argv[1]) == 0)) |
---|
143 | { |
---|
144 | ModelBasedTestInvoker invoker(test); |
---|
145 | return executeTestUsingRandomModel(-1, invoker); |
---|
146 | } |
---|
147 | |
---|
148 | if ((argc == 3) && (strcmp("-c", argv[1]) == 0) && isdigit(argv[2][0])) |
---|
149 | { |
---|
150 | int shape = atol(argv[2]); |
---|
151 | ModelBasedTestInvoker invoker(test); |
---|
152 | return executeTestUsingRandomModel(shape, invoker); |
---|
153 | } |
---|
154 | |
---|
155 | fprintf(stderr, |
---|
156 | "%s\n\n" |
---|
157 | "argument lists:\n" |
---|
158 | "-l FILENAME - to print list of models in file\n" |
---|
159 | "-l FILENAME GENO_ID - to load model from file and run test\n" |
---|
160 | "-c [SHAPE] - to create simple random model and run test\n\n" |
---|
161 | "FILENAME - name of file containing named f0 genotypes\n" |
---|
162 | "GENO_ID - either genotype name or index (1-based)\n" |
---|
163 | "SHAPE - 1=ellipsoid, 2=cuboid, 3=cylinder, others or none=random\n", |
---|
164 | header.c_str()); |
---|
165 | return 1; |
---|
166 | } |
---|
167 | |
---|
168 | class ModelAndDensityBasedTestInvoker : public TestInvoker |
---|
169 | { |
---|
170 | private: |
---|
171 | void(*test)(Model &, const double); |
---|
172 | double density; |
---|
173 | public: |
---|
174 | ModelAndDensityBasedTestInvoker(void(*_test)(Model &, const double), double _density) : |
---|
175 | test(_test), |
---|
176 | density(_density) |
---|
177 | {} |
---|
178 | |
---|
179 | void operator()(Model &model) |
---|
180 | { |
---|
181 | test(model, density); |
---|
182 | } |
---|
183 | }; |
---|
184 | |
---|
185 | int GeometryTestUtils::execute(const SString header, int argc, char *argv[], |
---|
186 | void(*test)(Model &, const double)) |
---|
187 | { |
---|
188 | LoggerToStdout messages_to_stdout(LoggerBase::Enable); //comment this object out to mute error/warning messages |
---|
189 | StdioFileSystem_autoselect stdiofilesys; |
---|
190 | PreconfiguredGenetics genetics; |
---|
191 | |
---|
192 | srand(time(NULL)); |
---|
193 | |
---|
194 | if ((argc == 3) && (strcmp("-l", argv[1]) == 0)) |
---|
195 | { |
---|
196 | return printGenotypesList(argv[2]); |
---|
197 | } |
---|
198 | |
---|
199 | if ((argc == 5) && (strcmp("-l", argv[1]) == 0) && isdigit(argv[4][0])) |
---|
200 | { |
---|
201 | double density = atol(argv[4]); |
---|
202 | ModelAndDensityBasedTestInvoker invoker(test, density); |
---|
203 | return executeTestUsingLoadedModel(argv[2], argv[3], invoker); |
---|
204 | } |
---|
205 | |
---|
206 | if ((argc == 3) && (strcmp("-c", argv[1]) == 0) && isdigit(argv[2][0])) |
---|
207 | { |
---|
208 | double density = atol(argv[2]); |
---|
209 | ModelAndDensityBasedTestInvoker invoker(test, density); |
---|
210 | return executeTestUsingRandomModel(-1, invoker); |
---|
211 | } |
---|
212 | |
---|
213 | if ((argc == 4) && (strcmp("-c", argv[1]) == 0) && isdigit(argv[2][0]) && isdigit(argv[3][0])) |
---|
214 | { |
---|
215 | double density = atol(argv[2]); |
---|
216 | int shape = atol(argv[3]); |
---|
217 | ModelAndDensityBasedTestInvoker invoker(test, density); |
---|
218 | return executeTestUsingRandomModel(shape, invoker); |
---|
219 | } |
---|
220 | |
---|
221 | fprintf(stderr, |
---|
222 | "%s\n\n" |
---|
223 | "argument list:\n" |
---|
224 | "-l FILENAME - to print the list of models in file\n" |
---|
225 | "-l FILENAME GENO_ID DENSITY - to load the model from the file and run test\n" |
---|
226 | "-c DENSITY [SHAPE] - to create a simple random model and run test\n\n" |
---|
227 | "FILENAME - name of the file containing named f0 genotypes\n" |
---|
228 | "GENO_ID - either genotype name or index (1-based)\n" |
---|
229 | "DENSITY - minimal number of samples per unit\n" |
---|
230 | "SHAPE - 1=ellipsoid, 2=cuboid, 3=cylinder, others or none=random\n", |
---|
231 | header.c_str()); |
---|
232 | return 1; |
---|
233 | } |
---|
234 | |
---|
235 | void GeometryTestUtils::describePart(const Part *part, FILE *output) |
---|
236 | { |
---|
237 | fprintf(output, "# shape=%d\n", part->shape); |
---|
238 | fprintf(output, "# x=%f\n", part->p.x); |
---|
239 | fprintf(output, "# y=%f\n", part->p.y); |
---|
240 | fprintf(output, "# z=%f\n", part->p.z); |
---|
241 | fprintf(output, "# sx=%f\n", part->scale.x); |
---|
242 | fprintf(output, "# sy=%f\n", part->scale.y); |
---|
243 | fprintf(output, "# sz=%f\n", part->scale.z); |
---|
244 | fprintf(output, "# rx=%f\n", part->rot.x); |
---|
245 | fprintf(output, "# ry=%f\n", part->rot.y); |
---|
246 | fprintf(output, "# rz=%f\n", part->rot.z); |
---|
247 | } |
---|