Advertisement
Guest User

main.c

a guest
Jan 29th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     References:
  3.     1)http://www.gaalop.de/dhilden_data/CLUScripts/CandG_PrePrint.pdf
  4.     2)http://www2.montgomerycollege.edu/departments/planet/planet/Numerical_Relativity/GA-SIG/Conformal%20Geometry%20Papers/Cambridge/Covarient%20Approach%20to%20Geometry%20Using%20Geometric%20Algebra.pdf
  5.     3)http://peeterjoot.com/archives/geometric-algebra/bivector.pdf
  6.     4)Geometric Algebra for Computer Science: An Object-Oriented Approach to Geometry by Leo Dorst, Daniel Fontijne and Stephen Mann
  7.     5)Multivectors and Clifford Algebra in Electrodynamics By Bernard Jancewicz
  8.  
  9.     The following code has been written to the best of my understanding of the above concept.
  10.     However, I'm not sure of my math in the function where I compute the inner product of a trivector and a vector.
  11.     Formula from the 5th reference on page 24: (b ^ c ^ d) = b ^ c(d . a) + d ^ b(c . a) + c ^ d(b . a)
  12.     Doing the above for 10 components for 5 times each was painful and prone to errors.
  13.     Sorry for the ugly long names, tried my best to make the code self explanatory.
  14.     Will be more than happy to explain any part which is unclear in the code.
  15. */
  16.  
  17. #include <stdio.h>
  18.  
  19. // vec3. To be read as "x y z"
  20. typedef float vec3[3];
  21.  
  22. void vec3_extend_vec3(vec3 res, vec3 a, vec3 b);
  23. void vec3_add_vec3(vec3 res, vec3 a, vec3 b);
  24.  
  25. // Conformal geometric algebra 5d vector notation of 3d vector. To be read as "e1 e2 e3 e0 einf" where e0 is point at origin and einf is point at infinity.
  26. typedef float vec5[5];
  27.  
  28. // Conformal geometric algebra 10 bivector notation. To be read as "e1e2 e1e3 e1e0 e1einf e2e3 e2e0 e2einf e3e0 e3einf e0einf" where eij = ei ^ ej.
  29. // Conformal geometric algebra 10 trivector notation. To be read as "e1e2e3 e1e2e0 e1e2einf e1e3e0 e1e3einf e1e0einf e2e3e0 e2e3einf e2e0einf e3e0einf"  
  30. typedef float vec10[10];
  31.  
  32. void vec3_to_vec5(vec5 res, vec3 v);
  33. void vec5_outer_vec5(vec10 res, vec5 a, vec5 b);
  34.  
  35. // bivector ^ vector = trivector (res has 10 components)
  36. void vec10_outer_vec5(vec10 res, vec10 a, vec5 b);
  37.  
  38. // trivector . vector = bivector (res has 10 components)
  39. void vec10_inner_vec5(vec10 res, vec10 a, vec5 b);
  40.  
  41. // bivector . bivector = scalar
  42. float vec10_inner_vec10(vec10 a, vec10 b);
  43.  
  44. void create_sphere(vec5 sphere, vec3 sphere_pos_v3, float rad);
  45. void create_line(vec5 line, vec3 line_pos_v3, vec3 line_dir_v3);
  46.  
  47. /*
  48.     point_pair(bivector) = line(trivector) . sphere(vector)
  49.     sign(scalar) = point_pair . point_pair
  50. */
  51. void compute_intersection(vec10 line, vec5 sphere);
  52.    
  53. int main(void)
  54. {
  55.     vec3 sphere_pos_v3;
  56.     printf("Enter sphere pos in vec3: ");
  57.     scanf("%f %f %f", &sphere_pos_v3[0], &sphere_pos_v3[1], &sphere_pos_v3[2]);
  58.  
  59.     float rad;
  60.     printf("Enter sphere radius: ");
  61.     scanf("%f", &rad);
  62.  
  63.     vec5 sphere;
  64.     create_sphere(sphere, sphere_pos_v3, rad);
  65.  
  66.     vec3 line_pos_v3, line_dir_v3;
  67.     printf("Enter line pos in vec3: ");
  68.     scanf("%f %f %f", &line_pos_v3[0], &line_pos_v3[1], &line_pos_v3[2]);
  69.     printf("Enter line dir in vec3: ");
  70.     scanf("%f %f %f", &line_dir_v3[0], &line_dir_v3[1], &line_dir_v3[2]);
  71.    
  72.     vec3 line_dir_ext;
  73.     vec3_extend_vec3(line_dir_ext, line_dir_v3, sphere_pos_v3);
  74.     vec3 line_endpos_v3;
  75.     vec3_add_vec3(line_endpos_v3, line_pos_v3, line_dir_ext);
  76.  
  77.     vec10 line;
  78.     create_line(line, line_pos_v3, line_endpos_v3);
  79.  
  80.     compute_intersection(line, sphere);
  81.  
  82.     return 0;
  83. }
  84.  
  85. void vec3_extend_vec3(vec3 res, vec3 a, vec3 b)
  86. {
  87.     res[0] = a[0] * b[0];
  88.     res[1] = a[1] * b[1];
  89.     res[2] = a[2] * b[2];
  90. }
  91.  
  92. void vec3_add_vec3(vec3 res, vec3 a, vec3 b)
  93. {
  94.     res[0] = a[0] + b[0];
  95.     res[1] = a[1] + b[1];
  96.     res[2] = a[2] + b[2];
  97. }
  98.  
  99. void vec3_to_vec5(vec5 res, vec3 v)
  100. {
  101.     res[0] = v[0];
  102.     res[1] = v[1];
  103.     res[2] = v[2];
  104.     res[3] = 1.0f;
  105.     res[4] = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) / 2.0f;
  106. }
  107.  
  108. void vec5_outer_vec5(vec10 res, vec5 a, vec5 b)
  109. {
  110.     res[0] = (a[0] * b[1]) - (a[1] * b[0]);
  111.     res[1] = (a[0] * b[2]) - (a[2] * b[0]);
  112.     res[2] = (a[0] * b[3]) - (a[3] * b[0]);
  113.     res[3] = (a[0] * b[4]) - (a[4] * b[0]);
  114.  
  115.     res[4] = (a[1] * b[2]) - (a[2] * b[1]);
  116.     res[5] = (a[1] * b[3]) - (a[3] * b[1]);
  117.     res[6] = (a[1] * b[4]) - (a[4] * b[1]);
  118.  
  119.     res[7] = (a[2] * b[3]) - (a[3] * b[2]);
  120.     res[8] = (a[2] * b[4]) - (a[4] * b[2]);
  121.  
  122.     res[9] = (a[3] * b[4]) - (a[4] * b[3]);
  123. }
  124.  
  125. void vec10_outer_vec5(vec10 res, vec10 a, vec5 b)
  126. {
  127.     res[0] = (a[0] * b[2]) - (a[1] * b[1]) - (a[4] * b[0]);
  128.     res[1] = (a[0] * b[3]) - (a[2] * b[1]) - (a[5] * b[0]);
  129.     res[2] = (a[0] * b[4]) - (a[3] * b[1]) - (a[6] * b[0]);
  130.    
  131.     res[3] = (a[1] * b[3]) - (a[2] * b[2]) - (a[7] * b[0]);
  132.     res[4] = (a[1] * b[4]) - (a[3] * b[2]) - (a[8] * b[0]);
  133.    
  134.     res[5] = (a[2] * b[4]) - (a[3] * b[3]) - (a[9] * b[0]);
  135.    
  136.     res[6] = (a[4] * b[3]) - (a[5] * b[2]) - (a[7] * b[1]);
  137.     res[7] = (a[4] * b[4]) - (a[6] * b[2]) - (a[8] * b[1]);
  138.  
  139.     res[8] = (a[5] * b[4]) - (a[6] * b[3]) - (a[9] * b[1]);
  140.  
  141.     res[9] = (a[7] * b[4]) - (a[8] * b[3]) - (a[9] * b[2]);
  142. }
  143.  
  144. void vec10_inner_vec5(vec10 res, vec10 a, vec5 b)
  145. {
  146.     res[0] = (a[0] * b[2]) - (a[1] * b[3]) - (a[2] * b[4]);
  147.     res[1] = -(a[0] * b[1]) - (a[3] * b[3]) - (a[4] * b[4]);
  148.     res[2] = -(a[1] * b[1]) - (a[3] * b[2]) - (a[5] * b[4]);
  149.     res[3] = -(a[2] * b[1]) - (a[4] * b[2]) - (a[5] * b[3]);
  150.  
  151.     res[4] = (a[0] * b[0]) - (a[6] * b[3]) - (a[7] * b[4]);
  152.     res[5] = (a[1] * b[0]) - (a[6] * b[2]) - (a[8] * b[4]);
  153.     res[6] = (a[2] * b[0]) - (a[7] * b[2]) + (a[8] * b[3]);
  154.  
  155.     res[7] = (a[3] * b[0]) + (a[6] * b[1]) - (a[9] * b[4]);
  156.     res[8] = (a[4] * b[0]) + (a[7] * b[1]) + (a[9] * b[3]);
  157.  
  158.     res[9] = (a[5] * b[0]) + (a[8] * b[1]) + (a[9] * b[2]);
  159. }
  160.  
  161. float vec10_inner_vec10(vec10 a, vec10 b)
  162. {
  163.     return (-(a[0] * b[0]) - (a[1] * b[1]) + (a[2] * b[2]) + (a[3] * b[3]) - (a[4] * b[4]) + (a[5] * b[5]) + (a[6] * b[6]) + (a[7] * b[7]) + (a[8] * b[8]) - (a[9] * b[9]));
  164. }
  165.  
  166. void create_sphere(vec5 sphere, vec3 sphere_pos_v3, float rad)
  167. {
  168.     vec5 sphere_pos_v5;
  169.     vec3_to_vec5(sphere_pos_v5, sphere_pos_v3);
  170.  
  171.     sphere[0] = sphere_pos_v5[0];
  172.     sphere[1] = sphere_pos_v5[1];
  173.     sphere[2] = sphere_pos_v5[2];
  174.     sphere[3] = sphere_pos_v5[3];
  175.     sphere[4] = sphere_pos_v5[4] - (rad * rad / 2.0f);
  176. }
  177.  
  178. void create_line(vec10 line, vec3 line_pos_v3, vec3 line_endpos_v3)
  179. {
  180.     vec5 line_pos_v5, line_endpos_v5;
  181.     vec3_to_vec5(line_pos_v5, line_pos_v3);
  182.     vec3_to_vec5(line_endpos_v5, line_endpos_v3);
  183.  
  184.     vec10 temp;
  185.     vec5_outer_vec5(temp, line_pos_v5, line_endpos_v5);
  186.  
  187.     vec5 point_at_einf = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
  188.     vec10_outer_vec5(line, temp, point_at_einf);
  189. }
  190.  
  191. void compute_intersection(vec10 line, vec5 sphere)
  192. {
  193.     vec10 point_pair;
  194.     vec10_inner_vec5(point_pair, line, sphere);
  195.     float sign = vec10_inner_vec10(point_pair, point_pair);
  196.     if (sign > 0)
  197.         printf("True.\n");
  198.     else
  199.         printf("False.\n");
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement