GCC Code Coverage Report


Directory: ./
File: lib/geogram/lua/lua_vec_mat.h
Date: 2026-09-07 02:28:19
Exec Total Coverage
Lines: 0 34 0.0%
Functions: 0 4 0.0%
Branches: 0 26 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifndef GEOGRAM_LUA_LUA_VEC_MAT
41 #define GEOGRAM_LUA_LUA_VEC_MAT
42
43 #include <geogram/basic/common.h>
44 #include <geogram/lua/lua.h>
45 #include <geogram/basic/assert.h>
46 #include <geogram/basic/argused.h>
47 #include <geogram/basic/numeric.h>
48 #include <geogram/basic/vecg.h>
49 #include <geogram/basic/matrix.h>
50
51 /**
52 * \file geogram/lua/lua_vec_mat.h
53 * \brief Functions to exchange vec2,vec3,vec4 and mat4 objects
54 * between Lua and Geogram
55 */
56
57 namespace GEO {
58
59 /**
60 * \brief Converts a lua object into a value
61 * \tparam T type of the value
62 * \param[in] L a pointer to the Lua state
63 * \param[in] index the index of the object in thestack
64 * \param[out] result a reference to the read value
65 * \retval true if the conversion was successful
66 * \retval false otherwise (LUA type did not match)
67 */
68 template <class T> inline bool lua_toveccomp(
69 lua_State* L, int index, T& result
70 ) {
71 geo_argused(L);
72 geo_argused(index);
73 geo_argused(result);
74 geo_assert_not_reached;
75 }
76
77 template<> inline bool lua_toveccomp<double>(
78 lua_State* L, int index, double& result
79 ) {
80 if(lua_type(L,index) == LUA_TNUMBER) {
81 result = lua_tonumber(L,index);
82 return true;
83 }
84 return false;
85 }
86
87 template<> inline bool lua_toveccomp<float>(
88 lua_State* L, int index, float& result
89 ) {
90 if(lua_type(L,index) == LUA_TNUMBER) {
91 result = float(lua_tonumber(L,index));
92 return true;
93 }
94 return false;
95 }
96
97 template<> inline bool lua_toveccomp<Numeric::int32>(
98 lua_State* L, int index, Numeric::int32& result
99 ) {
100 if(lua_type(L,index) == LUA_TNUMBER && lua_isinteger(L,index)) {
101 result = GEO::Numeric::int32(lua_tointeger(L,index));
102 return true;
103 }
104 return false;
105 }
106
107
108 /**
109 * \brief Converts a lua object into a Geogram vec2,vec3 or vec4
110 * \tparam N dimension of the vector
111 * \tparam T type of the components
112 * \param[in] L a pointer to the Lua state
113 * \param[in] index the index of the object in thestack
114 * \param[out] result a reference to the converted vector
115 * \retval true if the conversion was successful
116 * \retval false otherwise (mtype does not match, or object on
117 * the stack is not an integer-indexed table of numbers of the
118 * correct size).
119 */
120 template<unsigned int N, class T> inline bool lua_tovec(
121 lua_State* L, int index, ::GEO::vecng<N,T>& result
122 ) {
123 if(!lua_istable(L,index)) {
124 return false;
125 }
126
127 index_t cur = 0;
128 bool ok = true;
129
130 for(lua_Integer i=1; lua_geti(L,index,i) != LUA_TNIL; ++i) {
131 if(cur < N) {
132 ok = ok && lua_toveccomp(L,-1,result[cur]);
133 }
134 ++cur;
135 lua_pop(L,1);
136 }
137 lua_pop(L,1); // lua_geti() pushes smthg on the stack
138 // even for the last round of the loop !
139
140 return(ok && cur == index_t(N));
141 }
142
143 /**
144 * \brief Converts a Lua object into a Geogram mat2, mat3 or mat4
145 * \tparam N dimension of the vector
146 * \tparam T type of the coefficients
147 * \param[in] L a pointer to the Lua state
148 * \param[in] index the index of the object in thestack
149 * \param[out] result a reference to the converted matrix
150 * \retval true if the conversion was successful
151 * \retval false otherwise (mtype does not match, or object
152 * is not a table of the correct size).
153 */
154 template<unsigned int N, class T> inline bool lua_tomat(
155 lua_State* L, int index, ::GEO::Matrix<N,T>& result
156 ) {
157
158 if(!lua_istable(L,index)) {
159 return false;
160 }
161
162 index_t cur = 0;
163 bool ok = true;
164
165 for(lua_Integer i=1; lua_geti(L,index,i) != LUA_TNIL; ++i) {
166 ::GEO::vecng<N,T> row;
167 if(cur < N) {
168 if(lua_tovec(L,-1,row)) {
169 for(index_t j=0; j<index_t(N); ++j) {
170 result(cur,j) = row[j];
171 }
172 } else {
173 ok = false;
174 }
175 }
176 ++cur;
177 lua_pop(L,1);
178 }
179 lua_pop(L,1); // lua_geti() pushes smthg on the stack
180 // even for the last round of the loop !
181
182 return(ok && cur == index_t(N));
183 }
184
185 /*******************************************************************/
186
187 /**
188 * \brief Pushes a vector component onto the Lua stack
189 * \details Specializations need to be declared, this generic version
190 * throws an assertion failure
191 * \param[in] L the Lua stack
192 * \param[in] val the value to be pushed
193 */
194 template <class T> inline void lua_pushveccomp(lua_State* L, T val) {
195 geo_argused(L);
196 geo_argused(val);
197 geo_assert_not_reached;
198 }
199
200 template<> inline void lua_pushveccomp(lua_State* L, double val) {
201 lua_pushnumber(L, val);
202 }
203
204 template<> inline void lua_pushveccomp(lua_State* L, float val) {
205 lua_pushnumber(L, double(val));
206 }
207
208 template<> inline void lua_pushveccomp(
209 lua_State* L, Numeric::int32 val
210 ) {
211 lua_pushinteger(L, lua_Integer(val));
212 }
213
214 /**
215 * \brief Pushes a vector onto the Lua stack
216 * \param[in] L the Lua stack
217 * \param[in] V a vec2,vec3,vec4,vec2i,vec3i or vec4i
218 */
219 template <unsigned int N, class T> inline void lua_push(
220 lua_State* L, const ::GEO::vecng<N,T>& V
221 ) {
222 lua_createtable(L, int(N), 0);
223 for(index_t i=0; i<index_t(N); ++i) {
224 lua_pushveccomp(L,V[i]);
225 lua_seti(L,-2,lua_Integer(i+1)); // indices start from 1 in Lua
226 }
227 }
228
229 /**
230 * \brief Pushes a matrix onto the Lua stack
231 * \param[in] L the Lua stack
232 * \param[in] M a mat2,mat3,mat4,mat2i,mat3i or mat4i
233 */
234 template <unsigned int N, class T> inline void lua_push(
235 lua_State* L, const ::GEO::Matrix<N,T>& M
236 ) {
237 lua_createtable(L, int(N), 0);
238 for(index_t i=0; i<index_t(N); ++i) {
239 lua_createtable(L, int(N), 0);
240 for(index_t j=0; j<index_t(N); ++j) {
241 lua_pushveccomp(L,M(i,j));
242 lua_seti(L,-2,lua_Integer(j+1)); // Idces start from 1 in Lua
243 }
244 lua_seti(L,-2,lua_Integer(i+1)); // Idem...
245 }
246 }
247
248 }
249
250
251 #endif
252