Geogram Version 1.9.9
A programming library of geometric algorithms
Loading...
Searching...
No Matches
lua_vec_mat.h
Go to the documentation of this file.
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
44#include <geogram/lua/lua.h>
48#include <geogram/basic/vecg.h>
50
57namespace GEO {
58
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);
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
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
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) {
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
194 template <class T> inline void lua_pushveccomp(lua_State* L, T val) {
195 geo_argused(L);
196 geo_argused(val);
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
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
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
A function to suppress unused parameters compilation warnings.
Assertion checking mechanism.
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition assert.h:177
A matrix type.
Definition matrix.h:66
Generic maths vector.
Definition vecg.h:74
Common include file, providing basic definitions. Should be included before anything else by all head...
Generic matrix type.
int32_t int32
Definition numeric.h:130
Global Vorpaline namespace.
Definition basic.h:55
bool lua_tomat(lua_State *L, int index, ::GEO::Matrix< N, T > &result)
Converts a Lua object into a Geogram mat2, mat3 or mat4.
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
void lua_push(lua_State *L, const ::GEO::vecng< N, T > &V)
Pushes a vector onto the Lua stack.
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:330
bool lua_toveccomp(lua_State *L, int index, T &result)
Converts a lua object into a value.
Definition lua_vec_mat.h:68
bool lua_tovec(lua_State *L, int index, ::GEO::vecng< N, T > &result)
Converts a lua object into a Geogram vec2,vec3 or vec4.
void lua_pushveccomp(lua_State *L, T val)
Pushes a vector component onto the Lua stack.
Types and functions for numbers manipulation.
Generic implementation of geometric vectors.