Advertisement
prupel

Quaternions.java (rough)

Jul 4th, 2012
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public final class Quaternions
  4. {
  5.     public static float[] add (float[] q1, float[] q2, float[] dst)
  6.     {
  7.         if (dst == null) dst = new float[4];
  8.        
  9.         for (int i = 0; i < 4; i++) dst[i] = q1[i] + q2[i];
  10.         return dst;
  11.     }
  12.     public static float[] add (float[] q, float s, float[] dst)
  13.     {
  14.         if (dst == null) dst = new float[4];
  15.        
  16.         for (int i = 0; i < 4; i++) dst[i] = q[i] + s;
  17.         return dst;
  18.     }
  19.    
  20.     public static float[] add_r (float[] q1, float[] q2)
  21.     {
  22.         return add(q1, q2, q1);
  23.     }
  24.    
  25.     public static float[] add_r (float[] q, float s)
  26.     {
  27.         return add(q, s, q);
  28.     }
  29.    
  30.     public static float[] sub (float[] q1, float[] q2, float[] dst)
  31.     {
  32.         if (dst == null) dst = new float[4];
  33.        
  34.         for (int i = 0; i < 4; i++) dst[i] = q1[i] - q2[i];
  35.         return dst;
  36.     }
  37.    
  38.     public static float[] sub (float[] q, float s, float[] dst)
  39.     {
  40.         if (dst == null) dst = new float[4];
  41.        
  42.         for (int i = 0; i < 4; i++) dst[i] = q[i] - s;
  43.         return dst;
  44.     }
  45.    
  46.     public static float[] sub_r (float[] q1, float[] q2)
  47.     {
  48.         return sub(q1, q2, q1);
  49.     }
  50.    
  51.     public static float[] sub_r (float[] q, float s)
  52.     {
  53.         return sub(q, s, q);
  54.     }
  55.    
  56.     public static float[] mul (float[] q1, float[] q2, float[] dst)
  57.     {
  58.         float[] tq1 = Arrays.copyOf(q1, q1.length);
  59.         float[] tq2 = Arrays.copyOf(q2, q2.length);
  60.         float[] tf = new float[4];
  61.        
  62.         Vectors.mul_r(tq1, q2[3]); 
  63.         Vectors.mul_r(tq2, q1[3]);
  64.        
  65.         float[] cross = Vectors.cross(q1, q2, null);
  66.        
  67.         Vectors.add(tq1, tq2, tf);
  68.         Vectors.add_r (tf, cross);
  69.        
  70.         tf[3] = q1[3] * q2[3] - Vectors.dot(q1, q2);
  71.  
  72.         dst[0] = tf[0];
  73.         dst[1] = tf[1];
  74.         dst[2] = tf[2];
  75.         dst[3] = tf[3];
  76.        
  77.         return dst;
  78.     }
  79.    
  80.     public static float[] mul_r (float[] q1, float[] q2)
  81.     {
  82.         return mul(q1, q2, q1);
  83.     }
  84.    
  85.     public static float[] conjugate (float[] q, float[] dst)
  86.     {
  87.         if (dst == null) dst = new float[4];
  88.        
  89.         for (int i = 0; i < 3; i++) dst[i] = -q[i];
  90.         dst[3] = q[3];
  91.        
  92.         return dst;
  93.     }
  94.    
  95.     public static float[] conjugate_r (float[] q)
  96.     {
  97.         return conjugate(q, q);
  98.     }
  99.    
  100.     public static float[] divide (float[] q, float s, float[] dst)
  101.     {
  102.         if (dst == null) dst = new float[4];
  103.        
  104.         for (int i = 0; i < i; i++) dst[i] = q[i] / s;
  105.         return dst;
  106.     }
  107.    
  108.     public static float[] divide (float[] q1, float[] q2, float[] dst)
  109.     {
  110.         float[] tmp = inverse(q2, null);
  111.         return mul(q1, tmp, dst);
  112.     }
  113.    
  114.     public static float[] divide_r (float[] q, float s)
  115.     {
  116.         return divide(q, s, q);
  117.     }
  118.    
  119.     public static float[] divide_r (float[] q1, float[] q2)
  120.     {
  121.         return divide(q1, q2, q1);
  122.     }
  123.    
  124.     public static float[] inverse (float[] q, float[] dst)
  125.     {
  126.         if (dst == null) dst = new float[4];
  127.        
  128.         int t = 0;
  129.         for (int i = 0; i < 4; i++) t += q[i] * q[i];
  130.        
  131.         divide(conjugate(q, null), t, dst);
  132.         return dst;
  133.     }
  134.    
  135.     public static float[] inverse_r (float[] q)
  136.     {
  137.         return inverse(q, q);
  138.     }
  139.    
  140.     public static float[] norm (float[] q, float[] dst)
  141.     {
  142.         if (dst == null) dst = new float[4];
  143.        
  144.         float l = abs(q);
  145.         for (int i = 0; i < 4; i ++) dst[i] = q[i] / l;
  146.        
  147.         return dst;
  148.     }
  149.    
  150.     public static float[] norm_r (float[] q)
  151.     {
  152.         return norm(q, q);
  153.     }
  154.    
  155.     public static float abs (float[] q)
  156.     {
  157.         float a = 0;
  158.         for (int i = 0; i < 4; i++) a += q[i] * q[i];
  159.        
  160.         return a;
  161.     }
  162.    
  163.     public static float[] axis (float[] q, float[] dst)
  164.     {
  165.         if (dst == null) dst = new float[4];
  166.        
  167.         dst[0] = 2.0F * (float) Math.acos(q[3]);
  168.        
  169.         float m = Vectors.len(q);
  170.         for (int i = 1; i < 4; i++) dst[i] = q[i - 1] / m;
  171.        
  172.         return dst;
  173.     }
  174.    
  175.     public static String toString (float[] q)
  176.     {
  177.         StringBuilder s = new StringBuilder();
  178.         s.append(q[3]);
  179.         s.append(q[0] >= 0 ? " + " : " - ");
  180.         s.append(Math.abs(q[0]));
  181.         s.append("i");
  182.         s.append(q[1] >= 0 ? " + " : " - ");
  183.         s.append(Math.abs(q[1]));
  184.         s.append("j");
  185.         s.append(q[2] >= 0 ? " + " : " - ");
  186.         s.append(Math.abs(q[2]));
  187.         s.append("k");
  188.        
  189.         return s.toString();
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement