MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / multiTable2DFromConfig

Method multiTable2DFromConfig

source/game/StarStoredFunctions.cpp:250–312  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

248}
249
250MultiTable2D FunctionDatabase::multiTable2DFromConfig(Json descriptor) {
251 try {
252 String interpolationModeString = descriptor.getString(0);
253 String boundModeString = descriptor.getString(1);
254
255 List<double> xaxis;
256 List<double> yaxis;
257 MultiArray2D points;
258
259 auto grid = descriptor.getArray(2);
260
261 for (size_t y = 0; y < grid.size(); ++y) {
262 auto row = grid[y].toArray();
263 if (y == 0) {
264 for (size_t x = 0; x < row.size(); ++x) {
265 if (x > 0)
266 xaxis.append(row[x].toFloat());
267 }
268 points.resize({row.size() - 1, grid.size() - 1});
269 } else {
270 yaxis.append(row[0].toFloat());
271 auto cells = row[1].toArray();
272 if (cells.size() != xaxis.size())
273 throw StarException("Number of sample points doesn't match axis size.");
274 for (size_t x = 0; x < cells.size(); x++)
275 points.set({x, y - 1}, cells[x].toFloat());
276 }
277 }
278
279 InterpolationMode interpolationMode;
280 if (interpolationModeString.equalsIgnoreCase("HalfStep")) {
281 interpolationMode = InterpolationMode::HalfStep;
282 } else if (interpolationModeString.equalsIgnoreCase("Linear")) {
283 interpolationMode = InterpolationMode::Linear;
284 } else if (interpolationModeString.equalsIgnoreCase("Cubic")) {
285 interpolationMode = InterpolationMode::Cubic;
286 } else {
287 throw StoredFunctionException(strf("Unrecognized InterpolationMode '{}'", interpolationModeString));
288 }
289
290 BoundMode boundMode;
291 if (boundModeString.equalsIgnoreCase("Clamp")) {
292 boundMode = BoundMode::Clamp;
293 } else if (boundModeString.equalsIgnoreCase("Extrapolate")) {
294 boundMode = BoundMode::Extrapolate;
295 } else if (boundModeString.equalsIgnoreCase("Wrap")) {
296 boundMode = BoundMode::Wrap;
297 } else {
298 throw StoredFunctionException(strf("Unrecognized BoundMode '{}'", boundModeString));
299 }
300
301 MultiTable2D table;
302 table.setRange(0, std::vector<double>(xaxis.begin(), xaxis.end()));
303 table.setRange(1, std::vector<double>(yaxis.begin(), yaxis.end()));
304 table.setInterpolationMode(interpolationMode);
305 table.setBoundMode(boundMode);
306 table.array() = points;
307

Callers

nothing calls this directly

Calls 15

StarExceptionClass · 0.85
strfFunction · 0.85
getStringMethod · 0.80
getArrayMethod · 0.80
toArrayMethod · 0.80
toFloatMethod · 0.80
sizeMethod · 0.45
appendMethod · 0.45
resizeMethod · 0.45
setMethod · 0.45
equalsIgnoreCaseMethod · 0.45
setRangeMethod · 0.45

Tested by

no test coverage detected