/applications/utilities/surface/surfaceCoarsen/bunnylod/vector.C

https://github.com/xyuan/OpenFOAM-1.7.x · C · 108 lines · 93 code · 11 blank · 4 comment · 5 complexity · bce48ae8a68e90aeda0ed7486c02c40e MD5 · raw file

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <assert.h>
  4. #include "vector.h"
  5. float sqr(float a) {return a*a;}
  6. // vector (floating point) implementation
  7. float magnitude(Vector v) {
  8. return float(sqrt(sqr(v.x) + sqr( v.y)+ sqr(v.z)));
  9. }
  10. Vector normalize(Vector v) {
  11. float d=magnitude(v);
  12. if (d==0) {
  13. printf("Cant normalize ZERO vector\n");
  14. assert(0);
  15. d=0.1f;
  16. }
  17. v.x/=d;
  18. v.y/=d;
  19. v.z/=d;
  20. return v;
  21. }
  22. Vector operator+(Vector v1,Vector v2) {return Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);}
  23. Vector operator-(Vector v1,Vector v2) {return Vector(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z);}
  24. Vector operator-(Vector v) {return Vector(-v.x,-v.y,-v.z);}
  25. Vector operator*(Vector v1,float s) {return Vector(v1.x*s,v1.y*s,v1.z*s);}
  26. Vector operator*(float s, Vector v1) {return Vector(v1.x*s,v1.y*s,v1.z*s);}
  27. Vector operator/(Vector v1,float s) {return v1*(1.0f/s);}
  28. float operator^(Vector v1,Vector v2) {return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;}
  29. Vector operator*(Vector v1,Vector v2) {
  30. return Vector(
  31. v1.y * v2.z - v1.z*v2.y,
  32. v1.z * v2.x - v1.x*v2.z,
  33. v1.x * v2.y - v1.y*v2.x);
  34. }
  35. Vector planelineintersection(Vector n,float d,Vector p1,Vector p2){
  36. // returns the point where the line p1-p2 intersects the plane n&d
  37. Vector dif = p2-p1;
  38. float dn= n^dif;
  39. float t = -(d+(n^p1) )/dn;
  40. return p1 + (dif*t);
  41. }
  42. int concurrent(Vector a,Vector b) {
  43. return(a.x==b.x && a.y==b.y && a.z==b.z);
  44. }
  45. // Matrix Implementation
  46. matrix transpose(matrix m) {
  47. return matrix( Vector(m.x.x,m.y.x,m.z.x),
  48. Vector(m.x.y,m.y.y,m.z.y),
  49. Vector(m.x.z,m.y.z,m.z.z));
  50. }
  51. Vector operator*(matrix m,Vector v){
  52. m=transpose(m); // since column ordered
  53. return Vector(m.x^v,m.y^v,m.z^v);
  54. }
  55. matrix operator*(matrix m1,matrix m2){
  56. m1=transpose(m1);
  57. return matrix(m1*m2.x,m1*m2.y,m1*m2.z);
  58. }
  59. //Quaternion Implementation
  60. Quaternion operator*(Quaternion a,Quaternion b) {
  61. Quaternion c;
  62. c.r = a.r*b.r - a.x*b.x - a.y*b.y - a.z*b.z;
  63. c.x = a.r*b.x + a.x*b.r + a.y*b.z - a.z*b.y;
  64. c.y = a.r*b.y - a.x*b.z + a.y*b.r + a.z*b.x;
  65. c.z = a.r*b.z + a.x*b.y - a.y*b.x + a.z*b.r;
  66. return c;
  67. }
  68. Quaternion operator-(Quaternion q) {
  69. return Quaternion(q.r*-1,q.x,q.y,q.z);
  70. }
  71. Quaternion operator*(Quaternion a,float b) {
  72. return Quaternion(a.r*b, a.x*b, a.y*b, a.z*b);
  73. }
  74. Vector operator*(Quaternion q,Vector v) {
  75. return q.getmatrix() * v;
  76. }
  77. Vector operator*(Vector v,Quaternion q){
  78. assert(0); // must multiply with the quat on the left
  79. return Vector(0.0f,0.0f,0.0f);
  80. }
  81. Quaternion operator+(Quaternion a,Quaternion b) {
  82. return Quaternion(a.r+b.r, a.x+b.x, a.y+b.y, a.z+b.z);
  83. }
  84. float operator^(Quaternion a,Quaternion b) {
  85. return (a.r*b.r + a.x*b.x + a.y*b.y + a.z*b.z);
  86. }
  87. Quaternion slerp(Quaternion a,Quaternion b,float interp){
  88. if((a^b) <0.0) {
  89. a.r=-a.r;
  90. a.x=-a.x;
  91. a.y=-a.y;
  92. a.z=-a.z;
  93. }
  94. float theta = float(acos(a^b));
  95. if(theta==0.0f) { return(a);}
  96. return a*float(sin(theta-interp*theta)/sin(theta)) + b*float(sin(interp*theta)/sin(theta));
  97. }