Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: lhkbob (posted 2012-03-11 17:06:51, viewed 35868 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316  
public class StandaloneTest {
    
    public static void main(String[] args) {
        int numMatrices = 10000;
        int numTests = 10000;
        
        // data for Matrix4
        double[] dataDA4 = getData(numMatrices, 16);
        double[] dataDB4 = getData(numMatrices, 16);
        float[] dataFA4 = cast(dataDA4);
        float[] dataFB4 = cast(dataDB4);
        Matrix4[] a4 = getMatrix4(dataDA4);
        Matrix4[] b4 = getMatrix4(dataDB4);
        
        // data for Matrix3
        double[] dataDA3 = getData(numMatrices, 9);
        double[] dataDB3 = getData(numMatrices, 9);
        float[] dataFA3 = cast(dataDA3);
        float[] dataFB3 = cast(dataDB3);
        Matrix3[] a3 = getMatrix3(dataDA3);
        Matrix3[] b3 = getMatrix3(dataDB3);
        
        double ta4 = testA4(a4, b4);
        double tb4 = testB4(dataDA4, dataDB4);
        double tc4 = testC4(dataFA4, dataFA4);
        double ta3 = testA3(a3, b3);
        double tb3 = testB3(dataDA3, dataDB3);
        double tc3 = testC3(dataFA3, dataFB3);
        
        System.out.println("Test results:");
        System.out.println("\ttestA4 = " + ta4);
        System.out.println("\ttestB4 = " + tb4);
        System.out.println("\ttestC4 = " + tc4);
        System.out.println("\ttestA3 = " + ta3);
        System.out.println("\ttestB3 = " + tb3);
        System.out.println("\ttestC3 = " + tc3);
        System.out.println();
        
        for (int i = 0; i < 3; i++) {
            System.out.println("Test " + (i + 1));
            {
                long now = System.currentTimeMillis();
                double t = 0;
                for (int j = 0; j < numTests; j++) {
                    t += testA4(a4, b4);
                }
                System.out.println("testA4(Matrix4[]): " + (System.currentTimeMillis() - now) + " " + t);
            }
            {
                long now = System.currentTimeMillis();
                double t = 0;
                for (int j = 0; j < numTests; j++) {
                    t += testB4(dataDA4, dataDB4);
                }
                System.out.println("testB4(double[]): " + (System.currentTimeMillis() - now) + " " + t);
            }
            {
                long now = System.currentTimeMillis();
                double t = 0;
                for (int j = 0; j < numTests; j++) {
                    t += testC4(dataFA4, dataFB4);
                }
                System.out.println("testC4(float[]): " + (System.currentTimeMillis() - now) + " " + t);
            }
            {
                long now = System.currentTimeMillis();
                double t = 0;
                for (int j = 0; j < numTests; j++) {
                    t += testA3(a3, b3);
                }
                System.out.println("testA3(Matrix3[]): " + (System.currentTimeMillis() - now) + " " + t);
            }
            {
                long now = System.currentTimeMillis();
                double t = 0;
                for (int j = 0; j < numTests; j++) {
                    t += testB3(dataDA3, dataDB3);
                }
                System.out.println("testB3(double[]): " + (System.currentTimeMillis() - now) + " " + t);
            }
            {
                long now = System.currentTimeMillis();
                double t = 0;
                for (int j = 0; j < numTests; j++) {
                    t += testC3(dataFA3, dataFB3);
                }
                System.out.println("testC3(float[]): " + (System.currentTimeMillis() - now) + " " + t);
            }
            System.out.println();
        }
    }
    
    public static double testA4(Matrix4[] as, Matrix4[] bs) {
        Matrix4 m = new Matrix4();
        double d = 0;
        for (int i = 0; i < as.length; i++) {
            d += m.mul(as[i], bs[i]).trace();
        }
        return d;
    }
    
    public static double testB4(double[] dataA, double[] dataB) {
        Matrix4 m1 = new Matrix4();
        Matrix4 m2 = new Matrix4();
        
        double d = 0;
        for (int i = 0; i < dataA.length; i += 16) {
            m1.set(dataA, i, false);
            m2.set(dataB, i, false);
            d += m1.mul(m1, m2).trace();
        }
        return d;
    }
    
    public static double testC4(float[] dataA, float[] dataB) {
        Matrix4 m1 = new Matrix4();
        Matrix4 m2 = new Matrix4();
        double d = 0;
        for (int i = 0; i < dataA.length; i += 16) {
            m1.set(dataA, i, false);
            m2.set(dataB, i, false);
            d += m1.mul(m1, m2).trace();
        }
        return d;
    }
    
    public static double testA3(Matrix3[] as, Matrix3[] bs) {
        Matrix3 m = new Matrix3();
        double d = 0;
        for (int i = 0; i < as.length; i++) {
            d += m.mul(as[i], bs[i]).trace();
        }
        return d;
    }
    
    public static double testB3(double[] dataA, double[] dataB) {
        Matrix3 m1 = new Matrix3();
        Matrix3 m2 = new Matrix3();
        
        double d = 0;
        for (int i = 0; i < dataA.length; i += 9) {
            m1.set(dataA, i, false);
            m2.set(dataB, i, false);
            d += m1.mul(m1, m2).trace();
        }
        return d;
    }
    
    public static double testC3(float[] dataA, float[] dataB) {
        Matrix3 m1 = new Matrix3();
        Matrix3 m2 = new Matrix3();
        double d = 0;
        for (int i = 0; i < dataA.length; i += 9) {
            m1.set(dataA, i, false);
            m2.set(dataB, i, false);
            d += m1.mul(m1, m2).trace();
        }
        return d;
    }
    
    public static double r() {
        return Math.random() * 10;
    }
    
    public static double[] getData(int num, int size) {
        double[] m = new double[size * num];
        for (int i = 0; i < m.length; i++)
            m[i] = r();
        return m;
    }
    
    public static float[] cast(double[] d) {
        float[] m = new float[d.length];
        for (int i = 0; i < d.length; i++)
            m[i] = (float) d[i];
        return m;
    }
    
    public static Matrix4[] getMatrix4(double[] data) {
        Matrix4[] m = new Matrix4[data.length / 16];
        for (int i = 0; i < m.length; i ++) {
            m[i] = new Matrix4().set(data, i * 16, false);
        }
        
        return m;
    }
    
    public static Matrix3[] getMatrix3(double[] data) {
        Matrix3[] m = new Matrix3[data.length / 9];
        for (int i = 0; i < m.length; i ++) {
            m[i] = new Matrix3().set(data, i * 9, false);
        }
        
        return m;
    }

    public static final class Matrix3 {
        public double m00, m01, m02;
        public double m10, m11, m12;
        public double m20, m21, m22;
        
        public Matrix3 mul(Matrix3 a, Matrix3 b) {
            return set(a.m00 * b.m00 + a.m01 * b.m10 + a.m02 * b.m20, 
                       a.m00 * b.m01 + a.m01 * b.m11 + a.m02 * b.m21, 
                       a.m00 * b.m02 + a.m01 * b.m12 + a.m02 * b.m22, 
                       a.m10 * b.m00 + a.m11 * b.m10 + a.m12 * b.m20, 
                       a.m10 * b.m01 + a.m11 * b.m11 + a.m12 * b.m21, 
                       a.m10 * b.m02 + a.m11 * b.m12 + a.m12 * b.m22, 
                       a.m20 * b.m00 + a.m21 * b.m10 + a.m22 * b.m20, 
                       a.m20 * b.m01 + a.m21 * b.m11 + a.m22 * b.m21, 
                       a.m20 * b.m02 + a.m21 * b.m12 + a.m22 * b.m22);
        }
        
        public double trace() {
            return m00 + m11 + m22;
        }
        
        public Matrix3 set(double m00, double m01, double m02, 
                           double m10, double m11, double m12, 
                           double m20, double m21, double m22) {
            this.m00 = m00; this.m01 = m01; this.m02 = m02;
            this.m10 = m10; this.m11 = m11; this.m12 = m12;
            this.m20 = m20; this.m21 = m21; this.m22 = m22;
            return this;
        }
        
        public Matrix3 set(double[] values, int offset, boolean rowMajor) {
            if (rowMajor)
                return set(values[offset], values[offset + 1], values[offset + 2], 
                           values[offset + 3], values[offset + 4], values[offset + 5], 
                           values[offset + 6], values[offset + 7], values[offset + 8]);
            else
                return set(values[offset], values[offset + 3], values[offset + 6], 
                           values[offset + 1], values[offset + 4], values[offset + 7], 
                           values[offset + 2], values[offset + 5], values[offset + 8]);
        }
        
        public Matrix3 set(float[] values, int offset, boolean rowMajor) {
            if (rowMajor)
                return set(values[offset], values[offset + 1], values[offset + 2], 
                           values[offset + 3], values[offset + 4], values[offset + 5], 
                           values[offset + 6], values[offset + 7], values[offset + 8]);
            else
                return set(values[offset], values[offset + 3], values[offset + 6], 
                           values[offset + 1], values[offset + 4], values[offset + 7], 
                           values[offset + 2], values[offset + 5], values[offset + 8]);
        }
    }
    
    public static final class Matrix4 {
        public double m00, m01, m02, m03;
        public double m10, m11, m12, m13;
        public double m20, m21, m22, m23;
        public double m30, m31, m32, m33;
        
        public Matrix4 mul(Matrix4 a, Matrix4 b) {
            return set(a.m00 * b.m00 + a.m01 * b.m10 + a.m02 * b.m20 + a.m03 * b.m30,
                       a.m00 * b.m01 + a.m01 * b.m11 + a.m02 * b.m21 + a.m03 * b.m31,
                       a.m00 * b.m02 + a.m01 * b.m12 + a.m02 * b.m22 + a.m03 * b.m32, 
                       a.m00 * b.m03 + a.m01 * b.m13 + a.m02 * b.m23 + a.m03 * b.m33, 
                       a.m10 * b.m00 + a.m11 * b.m10 + a.m12 * b.m20 + a.m13 * b.m30, 
                       a.m10 * b.m01 + a.m11 * b.m11 + a.m12 * b.m21 + a.m13 * b.m31, 
                       a.m10 * b.m02 + a.m11 * b.m12 + a.m12 * b.m22 + a.m13 * b.m32, 
                       a.m10 * b.m03 + a.m11 * b.m13 + a.m12 * b.m23 + a.m13 * b.m33, 
                       a.m20 * b.m00 + a.m21 * b.m10 + a.m22 * b.m20 + a.m23 * b.m30, 
                       a.m20 * b.m01 + a.m21 * b.m11 + a.m22 * b.m21 + a.m23 * b.m31, 
                       a.m20 * b.m02 + a.m21 * b.m12 + a.m22 * b.m22 + a.m23 * b.m32, 
                       a.m20 * b.m03 + a.m21 * b.m13 + a.m22 * b.m23 + a.m23 * b.m33, 
                       a.m30 * b.m00 + a.m31 * b.m10 + a.m32 * b.m20 + a.m33 * b.m30, 
                       a.m30 * b.m01 + a.m31 * b.m11 + a.m32 * b.m21 + a.m33 * b.m31, 
                       a.m30 * b.m02 + a.m31 * b.m12 + a.m32 * b.m22 + a.m33 * b.m32, 
                       a.m30 * b.m03 + a.m31 * b.m13 + a.m32 * b.m23 + a.m33 * b.m33);
        }
        
        public Matrix4 set(double m00, double m01, double m02, double m03, 
                           double m10, double m11, double m12, double m13, 
                           double m20, double m21, double m22, double m23, 
                           double m30, double m31, double m32, double m33) {
           this.m00 = m00; this.m01 = m01; this.m02 = m02; this.m03 = m03;
           this.m10 = m10; this.m11 = m11; this.m12 = m12; this.m13 = m13;
           this.m20 = m20; this.m21 = m21; this.m22 = m22; this.m23 = m23;
           this.m30 = m30; this.m31 = m31; this.m32 = m32; this.m33 = m33;
           return this;
       }
        
        public Matrix4 set(double[] values, int offset, boolean rowMajor) {
            if (rowMajor)
                return set(values[offset], values[offset + 1], values[offset + 2], values[offset + 3], 
                           values[offset + 4], values[offset + 5], values[offset + 6], values[offset + 7], 
                           values[offset + 8], values[offset + 9], values[offset + 10], values[offset + 11], 
                           values[offset + 12], values[offset + 13], values[offset + 14], values[offset + 15]);
            else
                return set(values[offset], values[offset + 4], values[offset + 8], values[offset + 12], 
                           values[offset + 1], values[offset + 5], values[offset + 9], values[offset + 13], 
                           values[offset + 2], values[offset + 6], values[offset + 10], values[offset + 14], 
                           values[offset + 3], values[offset + 7], values[offset + 11], values[offset + 15]);
        }
        
        public Matrix4 set(float[] values, int offset, boolean rowMajor) {
            if (rowMajor)
                return set(values[offset], values[offset + 1], values[offset + 2], values[offset + 3], 
                           values[offset + 4], values[offset + 5], values[offset + 6], values[offset + 7], 
                           values[offset + 8], values[offset + 9], values[offset + 10], values[offset + 11], 
                           values[offset + 12], values[offset + 13], values[offset + 14], values[offset + 15]);
            else
                return set(values[offset], values[offset + 4], values[offset + 8], values[offset + 12], 
                           values[offset + 1], values[offset + 5], values[offset + 9], values[offset + 13], 
                           values[offset + 2], values[offset + 6], values[offset + 10], values[offset + 14], 
                           values[offset + 3], values[offset + 7], values[offset + 11], values[offset + 15]);
        }
        
        public double trace() {
            return m00 + m11 + m22 + m33;
        }
    }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!