Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <iostream>
00025 #include "SWI-cpp.h"
00026 #include <glm/glm.hpp>
00027 #include <glm/gtc/type_ptr.hpp>
00028 #include <glm/gtc/matrix_transform.hpp>
00029 #include <glm/gtc/quaternion.hpp>
00030 #include <glm/gtx/quaternion.hpp>
00031
00032
00033 using namespace std;
00034
00035 template<class T> T read_term(term_t term);
00036 template<> glm::mat4 read_term(term_t term);
00037
00038 template<class T> PlTerm construct_term(T& object);
00039 template<> PlTerm construct_term(glm::mat4& matrix);
00040
00041 void print(glm::mat4 mat);
00042 void print(glm::vec3 vec);
00043 void print(glm::quat quat);
00044
00045 PREDICATE(print_matrix, 1)
00046 {
00047 glm::mat4 load_matrix= read_term<glm::mat4>(A1);
00048 print(load_matrix);
00049 return TRUE;
00050 }
00051
00052 PREDICATE(mul_matrix, 3)
00053 {
00054 glm::mat4 left_matrix = read_term<glm::mat4>(A1);
00055 glm::mat4 right_matrix = read_term<glm::mat4>(A2);
00056
00057 glm::mat4 result = left_matrix * right_matrix;
00058
00059 #ifdef DEBUG
00060 cout << "Begin multiplication" << endl;
00061 cout << "--------------------------" << endl;
00062 print(left_matrix);
00063 cout << "--------------------------" << endl;
00064 print(right_matrix);
00065 cout << "--------------------------" << endl;
00066 print(result);
00067 cout << "--------------------------" << endl;
00068 cout << "End multiplication" << endl;
00069 #endif
00070
00071 return A3 = construct_term<glm::mat4>(result);
00072 }
00073
00074
00075 struct qqq{
00076 glm::vec3 a1;
00077 glm::quat a2;
00078 };
00079
00080 PREDICATE(mul_quaternion,5)
00081 {
00082 glm::vec3 translation1 = read_term<glm::vec3>(A1);
00083 glm::quat rotation1 = read_term<glm::quat>(A2);
00084 glm::vec3 translation2 = read_term<glm::vec3>(A3);
00085 glm::quat rotation2 = read_term<glm::quat>(A4);
00086
00087 glm::vec3 resTranslation = translation1 + translation2;
00088 glm::quat resRotation = rotation1 * rotation2;
00089
00090 PlTerm outerTerm;
00091 PlTail outerTail(outerTerm);
00092
00093 PlTerm innerTerm;
00094 PlTail innerTail(innerTerm);
00095
00096 innerTail.append(resTranslation.x);
00097 innerTail.append(resTranslation.y);
00098 innerTail.append(resTranslation.z);
00099 innerTail.close();
00100
00101 PlTerm innerTerm1;
00102 PlTail innerTail1(innerTerm1);
00103 innerTail1.append(resRotation.x);
00104 innerTail1.append(resRotation.y);
00105 innerTail1.append(resRotation.z);
00106 innerTail1.append(resRotation.w);
00107 innerTail1.close();
00108
00109
00110 outerTail.append(innerTerm);
00111 outerTail.append(innerTerm1);
00112 outerTail.close();
00113
00114 return A5 = outerTerm;
00115
00116
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 PREDICATE(interpolate_quaternion,6)
00131 {
00132 glm::vec3 translation1 = read_term<glm::vec3>(A1);
00133 glm::quat rotation1 = read_term<glm::quat>(A2);
00134 glm::vec3 translation2 = read_term<glm::vec3>(A3);
00135 glm::quat rotation2 = read_term<glm::quat>(A4);
00136 PlTerm element;
00137 PlTail tail(A5);
00138 float t;
00139 if(tail.next(element)) t = (double)(element);
00140 glm::vec3 resTranslation = translation1 * t + translation2 * (1.f-t);
00141 glm::quat resRotation = glm::fastMix(rotation1, rotation2,t);
00142
00143
00144 PlTerm outerTerm;
00145 PlTail outerTail(outerTerm);
00146
00147 PlTerm innerTerm;
00148 PlTail innerTail(innerTerm);
00149
00150 innerTail.append(resTranslation.x);
00151 innerTail.append(resTranslation.y);
00152 innerTail.append(resTranslation.z);
00153 innerTail.close();
00154
00155 PlTerm innerTerm1;
00156 PlTail innerTail1(innerTerm1);
00157 innerTail1.append(resRotation.x);
00158 innerTail1.append(resRotation.y);
00159 innerTail1.append(resRotation.z);
00160 innerTail1.append(resRotation.w);
00161 innerTail1.close();
00162
00163
00164 outerTail.append(innerTerm);
00165 outerTail.append(innerTerm1);
00166 outerTail.close();
00167
00168 return A6 = outerTerm;
00169
00170
00171 }
00172
00173 PREDICATE(quaternion_interpolate,6)
00174 {
00175 glm::vec3 translation1 = read_term<glm::vec3>(A1);
00176 glm::quat rotation1 = read_term<glm::quat>(A2);
00177 glm::vec3 translation2 = read_term<glm::vec3>(A3);
00178 glm::quat rotation2 = read_term<glm::quat>(A4);
00179 PlTerm element;
00180 PlTail tail(A5);
00181 float t;
00182 if(tail.next(element)) t = (double)(element);
00183 glm::vec3 resTranslation = translation1 * t + translation2 * (1.f-t);
00184 glm::quat resRotation = glm::mix(rotation1, rotation2,t);
00185
00186
00187 glm::mat4 rotation_matrix = glm::toMat4(resRotation);
00188 glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), resTranslation);
00189
00190 glm::mat4 converted = translation_matrix * rotation_matrix;
00191 return A6 = construct_term<glm::mat4>(converted);
00192
00193
00194 }
00195
00196
00197
00198
00199 PREDICATE(convert_to_mat, 3)
00200 {
00201 glm::vec3 translation = read_term<glm::vec3>(A1);
00202 glm::quat rotation = read_term<glm::quat>(A2);
00203
00204 glm::mat4 rotation_matrix = glm::toMat4(rotation);
00205 glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), translation);
00206
00207 glm::mat4 converted = translation_matrix * rotation_matrix;
00208
00209 #ifdef DEBUG
00210 cout << "Begin conversion to matrix" << endl;
00211 cout << "--------------------------" << endl;
00212 print(translation);
00213 cout << "--------------------------" << endl;
00214 print(rotation);
00215 cout << "--------------------------" << endl;
00216 print(converted);
00217 cout << "--------------------------" << endl;
00218 cout << "End conversion to matrix" << endl;
00219 #endif
00220
00221 return A3 = construct_term<glm::mat4>(converted);
00222 }
00223
00224 PREDICATE(transform_point, 3)
00225 {
00226 glm::vec3 point = read_term<glm::vec3>(A1);
00227 glm::mat4 matrix = read_term<glm::mat4>(A2);
00228
00229 glm::vec4 _translated = matrix * glm::vec4(point.x, point.y, point.z, 1);
00230
00231 glm::vec3 translated = glm::vec3(_translated.x,
00232 _translated.y,
00233 _translated.z);
00234
00235 #ifdef DEBUG
00236 cout << "Begin point tf" << endl;
00237 cout << "--------------------------" << endl;
00238 print(point);
00239 cout << "--------------------------" << endl;
00240 print(matrix);
00241 cout << "--------------------------" << endl;
00242 print(translated);
00243 cout << "--------------------------" << endl;
00244 cout << "End point tf" << endl;
00245 #endif
00246
00247 return A3 = construct_term<glm::vec3>(translated);
00248 }
00249
00250 template<class T>
00251 T read_term(term_t term)
00252 {
00253 T t;
00254 int index = 0;
00255 PlTerm element;
00256 PlTail tail(term);
00257 while(tail.next(element)) {
00258 t[index] = (double)(element);
00259 index++;
00260 }
00261 return t;
00262 }
00263
00264 template<>
00265 glm::mat4 read_term(term_t term)
00266 {
00267 glm::mat4 mat(1.0f);
00268
00269 PlTerm innerList;
00270 PlTail outerTail(term);
00271 int i = 0;
00272 while(outerTail.next(innerList)) {
00273 int j = 0;
00274 PlTerm element;
00275 PlTail innerTail(innerList);
00276 while(innerTail.next(element)) {
00277 mat[j][i] = (double)(element);
00278 j++;
00279 }
00280 i++;
00281 }
00282 return mat;
00283 }
00284
00285 template<class T>
00286 PlTerm construct_term(T& object)
00287 {
00288 PlTerm term;
00289 PlTail tail(term);
00290 for (int i = 0; i < object.length(); ++i)
00291 {
00292 tail.append(object[i]);
00293 }
00294 tail.close();
00295 return term;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 template<>
00330 PlTerm construct_term(glm::mat4& matrix)
00331 {
00332 PlTerm outerTerm;
00333 PlTail outerTail(outerTerm);
00334 for (int i = 0; i < 4; ++i)
00335 {
00336 PlTerm innerTerm;
00337 PlTail innerTail(innerTerm);
00338 for (int j = 0; j < 4; ++j)
00339 {
00340 innerTail.append(matrix[j][i]);
00341 }
00342 innerTail.close();
00343 outerTail.append(innerTerm);
00344 }
00345 outerTail.close();
00346 return outerTerm;
00347 }
00348
00349 void print(glm::mat4 mat)
00350 {
00351 for (int i = 0; i < 4; ++i)
00352 {
00353 for (int j = 0; j < 4; ++j)
00354 {
00355 cout << mat[j][i] << "\t";
00356 }
00357 cout << endl;
00358 }
00359 }
00360
00361 void print(glm::vec3 vec)
00362 {
00363 for (int i = 0; i < 3; ++i)
00364 {
00365 cout << vec[i];
00366 }
00367 cout << endl;
00368 }
00369
00370 void print(glm::quat quat)
00371 {
00372 for (int i = 0; i < 4; ++i)
00373 {
00374 cout << quat[i];
00375 }
00376 cout << endl;
00377 }