/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2017 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #include #include #include #include #include namespace { glm::mat2x4 fromLuaConversion(lua_State* state, bool& success) { glm::mat2x4 result; int number = 1; for (glm::length_t i = 0; i < ghoul::glm_cols::value; ++i) { for (glm::length_t j = 0; j < ghoul::glm_rows::value; ++j) { lua_getfield(state, -1, std::to_string(number).c_str()); if (lua_isnumber(state, -1) != 1) { success = false; return glm::mat2x4(0); } else { result[i][j] = static_cast(lua_tonumber(state, -1)); lua_pop(state, 1); ++number; } } } success = true; return result; } bool toLuaConversion(lua_State* state, glm::mat2x4 value) { lua_newtable(state); int number = 1; for (glm::length_t i = 0; i < ghoul::glm_cols::value; ++i) { for (glm::length_t j = 0; j < ghoul::glm_rows::value; ++j) { lua_pushnumber(state, static_cast(value[i][j])); lua_setfield(state, -2, std::to_string(number).c_str()); ++number; } } return true; } glm::mat2x4 fromStringConversion(std::string val, bool& success) { glm::mat2x4 result; std::vector tokens = ghoul::tokenizeString(val, ','); if (tokens.size() != (ghoul::glm_rows::value * ghoul::glm_cols::value)) { success = false; return result; } int number = 0; for (glm::length_t i = 0; i < ghoul::glm_cols::value; ++i) { for (glm::length_t j = 0; j < ghoul::glm_rows::value; ++j) { std::stringstream s(tokens[number]); glm::mat2x4::value_type v; s >> v; if (s.fail()) { success = false; return result; } else { result[i][j] = v; ++number; } } } success = true; return result; } bool toStringConversion(std::string& outValue, glm::mat2x4 inValue) { outValue = ""; for (glm::length_t i = 0; i < ghoul::glm_cols::value; ++i) { for (glm::length_t j = 0; j < ghoul::glm_rows::value; ++j) { outValue += std::to_string(inValue[i][j]) + ","; } outValue.pop_back(); } return true; } } // namespace namespace openspace::properties { using nl = std::numeric_limits; REGISTER_NUMERICALPROPERTY_SOURCE( Mat2x4Property, glm::mat2x4, glm::mat2x4(0), glm::mat2x4( nl::lowest(), nl::lowest(), nl::lowest(), nl::lowest(), nl::lowest(), nl::lowest(), nl::lowest(), nl::lowest() ), glm::mat2x4( nl::max(), nl::max(), nl::max(), nl::max(), nl::max(), nl::max(), nl::max(), nl::max() ), glm::mat2x4( 0.01f, 0.01f, 0.01f, 0.01f, 0.01f, 0.01f, 0.01f, 0.01f ), fromLuaConversion, toLuaConversion, fromStringConversion, toStringConversion, LUA_TTABLE ) } // namespace openspace::properties