Normand Briere
2019-09-20 cbe4e90105d07d7d3fecabffaa01342403aa2ae3
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
 
import java.io.PrintStream;
 
class LA
{
 
    static cVector newVector(double x, double y, double z)
    {
        cVector temp = new cVector();
        temp.x = x;
        temp.y = y;
        temp.z = z;
        return temp;
    }
 
    static void setVector(cVector v, double x, double y, double z)
    {
        v.x = x;
        v.y = y;
        v.z = z;
    }
 
    static String toPOVRay(cVector v)
    {
        StringBuffer buf = new StringBuffer(50);
        toPOVRay(v, buf);
        return buf.toString();
    }
 
    static void toPOVRay(cVector v, StringBuffer buf)
    {
        buf.append('<');
        buf.append(v.x);
        buf.append(',');
        buf.append(v.y);
        buf.append(',');
        buf.append(v.z);
        buf.append('>');
    }
 
    static boolean vecEqual(cVector a, cVector b)
    {
        return a.x == b.x && a.y == b.y && a.z == b.z;
    }
 
    static double distance(cVector a, cVector b)
    {
        return Math.sqrt(distance2(a, b));
    }
 
    static double distance2(cVector a, cVector b)
    {
        double x = b.x - a.x;
        double y = b.y - a.y;
        double z = b.z - a.z;
 
        return x * x + y * y + z * z;
    }
 
    static void vecNegate(cVector v)
    {
        v.x *= -1;
        v.y *= -1;
        v.z *= -1;
    }
 
    static void vecScale(cVector v, double s)
    {
        v.x *= s;
        v.y *= s;
        v.z *= s;
    }
 
    static double vecDot(cVector a, cVector b)
    {
        return a.x * b.x + a.y * b.y + a.z * b.z;
    }
 
    static double vecLen(cVector v)
    {
        return (double) Math.sqrt(vecDot(v, v));
    }
 
    static double vecLen2(cVector v)
    {
        return vecDot(v, v);
    }
 
    static void vecCopy(cVector a, cVector b)
    {
        Grafreed.Assert (a != null);
        assert (b != null);
        
        b.x = a.x;
        b.y = a.y;
        b.z = a.z;
    }
 
    static void vecAdd(cVector a, cVector b, cVector c)
    {
        c.x = a.x + b.x;
        c.y = a.y + b.y;
        c.z = a.z + b.z;
    }
 
    static void vecSub(cVector a, cVector b, cVector c)
    {
        c.x = a.x - b.x;
        c.y = a.y - b.y;
        c.z = a.z - b.z;
    }
 
    static void vecCross(cVector a, cVector b, cVector c)
    {
        c.x = a.y * b.z - a.z * b.y;
        c.y = a.z * b.x - a.x * b.z;
        c.z = a.x * b.y - a.y * b.x;
    }
 
    static boolean vecNormalize(cVector v)
    {
        double len = v.x * v.x + v.y * v.y + v.z * v.z; // vecLen(v);
 
        if (len <= 0) // 0.0001)
        {
            return false;
        }
 
        len = Math.sqrt(len);
 
        //vecScale(v, 1 / Math.sqrt(len));
        v.x /= len;
        v.y /= len;
        v.z /= len;
        return true;
    }
 
    static cVector xformPos(cVector v, double mat[][])
    {
        new Exception().printStackTrace();
        cVector temp = new cVector();
        xformPos(v, mat, temp);
        return temp;
    }
 
    static void xformPos(cVector in, double mat[][], cVector out)
    {
        //assert (mat != null);
        if (mat == null)
        {
            out.set(in);
            return;
        }
        
        assert (in != null);
        for (int i = 0; i < 3; i++)
        {
            xfpTemp.set(i, in.x * mat[0][i] + in.y * mat[1][i] + in.z * mat[2][i] + mat[3][i]);
        }
 
        vecCopy(xfpTemp, out);
    }
 
    static cVector xformDir(cVector v, double mat[][])
    {
                    new Exception().printStackTrace();
        System.exit(0);
        cVector temp = new cVector();
        xformDir(v, mat, temp);
        return temp;
    }
 
    static void xformDir(cVector in, double mat[][], cVector out)
    {
        //xfdTemp.x = xfdTemp.y = xfdTemp.z = 0;
        //xformPos(xfdTemp, mat, xfdTemp);
        //xformPos(in, mat, out);
        //vecSub(out, xfdTemp, out);
        if (mat == null)
        {
            out.set(in);
            return;
        }
        
        for (int i = 0; i < 3; i++)
        {
            xfpTemp.set(i, in.x * mat[0][i] + in.y * mat[1][i] + in.z * mat[2][i]);
        }
 
        vecCopy(xfpTemp, out);
    }
 
    static double[][] newMatrix()
    {
        double temp[][] = new double[4][4];
        matIdentity(temp);
        return temp;
    }
 
    static void matPrint(double mat[][])
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                System.out.print(mat[i][j]);
                if (j < 3)
                {
                    System.out.print(", ");
                }
            }
            System.out.println();
        }
    }
 
    static void matIdentity(double mat[][])
    {
//        matIdentity(mat, false);
        int s = 4;
        
        for (int i = 0; i < s; i++)
        {
            double[] mati = mat[i];
            for (int j = 0; j < s; j++)
            {
                mati[j] = i != j ? 0 : 1;
            }
        }
    }
 
    static void matIdentity(double mat[][], boolean rotationonly)
    {
        int s = 4;
        
        if (rotationonly)
        {
            s = 3;
 
            for (int i = 0; i < s; i++)
            {
                double[] mati = mat[i];
                for (int j = 0; j < s; j++)
                {
                    mati[j] = i != j ? 0 : 1;
                }
            }
        }
        else
        {
            mat[3][0] = 0;
            mat[3][1] = 0;
            mat[3][2] = 0;
        }
    }
 
    static boolean isIdentity(double mat[][])
    {
        if (mat == null)
            return true;
        
        //new Exception().printStackTrace();
        //LA.matPrint(mat);
        for (int i = 0; i < 4; i++)
        {
            double[] mati = mat[i];
            for (int j = 0; j < 4; j++)
            {
                if (mati[j] != (i != j ? 0 : 1))
                {
                    return false;
                }
            }
        }
 
        return true;
    }
 
    static boolean isIdentityEps(double mat[][])
    {
        if (mat == null)
            return true;
        
        //new Exception().printStackTrace();
        //LA.matPrint(mat);
        for (int i = 0; i < 4; i++)
        {
            double[] mati = mat[i];
            for (int j = 0; j < 4; j++)
            {
                if ((mati[j] - ((i != j) ? 0 : 1)) > 0.00001)
                {
                    return false;
                }
            }
        }
 
        return true;
    }
 
    static void matCopy(double src[][], double dst[][])
    {
        for (int i = 0; i < 4; i++)
        {
            double[] dsti = dst[i];
            double[] srci = src[i];
            for (int j = 0; j < 4; j++)
            {
                dsti[j] = srci[j];
            }
 
        }
 
        // Last row should always be 0 0 0 1
        Grafreed.Assert(Math.abs(src[0][3]) <= 1E-15);
        Grafreed.Assert(Math.abs(src[1][3]) <= 1E-15);
        Grafreed.Assert(Math.abs(src[2][3]) <= 1E-15);
        Grafreed.Assert(Math.abs(src[3][3] - 1) <= 1E-15);
        Grafreed.Assert(Math.abs(dst[0][3]) <= 1E-15);
        Grafreed.Assert(Math.abs(dst[1][3]) <= 1E-15);
        Grafreed.Assert(Math.abs(dst[2][3]) <= 1E-15);
        Grafreed.Assert(Math.abs(dst[3][3] - 1) <= 1E-15);
    }
 
    static double toRadians(double degrees)
    {
        return (degrees * Math.PI) / 180;
    }
 
    static void matConcat(double left[][], double right[][], double product[][])
    {
        //double tmp[][] = left;
        //left = right;
        //right = tmp;
        
        for (int j = 0; j < 4; j++)
        {
            double[] rightj = right[j];
            double[] concat = concatTemp[j];
            for (int i = 0; i < 4; i++)
            {
                concat[i] = 0;
                for (int k = 0; k < 4; k++)
                {
                    concat[i] += left[k][i] * rightj[k];
                }
            }
        }
 
        matCopy(concatTemp, product);
    }
 
    static void matTranslate(double mat[][], double dx, double dy, double dz)
    {
        matIdentity(xlateTemp);
        xlateTemp[3][0] = dx;
        xlateTemp[3][1] = dy;
        xlateTemp[3][2] = dz;
        //xlateTemp[0][3] = dx;
        //xlateTemp[1][3] = dy;
        //xlateTemp[2][3] = dz;
        matConcat(xlateTemp, mat, mat);
    }
 
    static void matHomogene(double mat[][], double dx, double dy, double dz)
    {
        matIdentity(xlateTemp);
        xlateTemp[0][3] = dx;
        xlateTemp[1][3] = dy;
        xlateTemp[2][3] = dz;
        //xlateTemp[0][3] = dx;
        //xlateTemp[1][3] = dy;
        //xlateTemp[2][3] = dz;
        matConcat(xlateTemp, mat, mat);
    }
 
    static void matTranslateInv(double mat[][], double dx, double dy, double dz)
    {
        matIdentity(xlateTemp);
        xlateTemp[3][0] = dx;
        xlateTemp[3][1] = dy;
        xlateTemp[3][2] = dz;
        //xlateTemp[0][3] = dx;
        //xlateTemp[1][3] = dy;
        //xlateTemp[2][3] = dz;
        matConcat(mat, xlateTemp, mat);
    }
 
    static void matScale(double mat[][], double sx, double sy, double sz)
    {
        matIdentity(xlateTemp);
        xlateTemp[0][0] = sx;
        xlateTemp[1][1] = sy;
        xlateTemp[2][2] = sz;
        matConcat(xlateTemp, mat, mat);
        //matConcat(mat, xlateTemp, mat);
    }
 
    static void matXRotate(double mat[][], double radians)
    {
        matIdentity(rotTemp);
        rotTemp[1][1] = rotTemp[2][2] = (double) Math.cos(radians);
        rotTemp[2][1] = -(rotTemp[1][2] = (double) Math.sin(radians));
   //     if (CameraPane.LOCALTRANSFORM)
            matConcat(rotTemp, mat, mat);
   //     else
   //         matConcat(mat, rotTemp, mat);
    }
 
    static void matYRotate(double mat[][], double radians)
    {
        matIdentity(rotTemp);
        rotTemp[0][0] = rotTemp[2][2] = (double) Math.cos(-radians);
        rotTemp[2][0] = -(rotTemp[0][2] = (double) Math.sin(-radians));
   //     if (CameraPane.LOCALTRANSFORM)
            matConcat(rotTemp, mat, mat);
   //     else
   //         matConcat(mat, rotTemp, mat);
    }
 
    static void matZRotate(double mat[][], double radians)
    {
        matIdentity(rotTemp);
        rotTemp[0][0] = rotTemp[1][1] = (double) Math.cos(radians);
        rotTemp[1][0] = -(rotTemp[0][1] = (double) Math.sin(radians));
   //     if (CameraPane.LOCALTRANSFORM)
            matConcat(rotTemp, mat, mat);
   //     else
   //         matConcat(mat, rotTemp, mat);
    }
 
    // Project A onto B
    static void matRotate(double mat[][], cVector pA, cVector pB)
    {
        matIdentity(mat);
 
        double x, y, z, w;
 
        /*
        if (pA == null || pB == null)
        {
            x = y = z = 0.0D;
            w = 1.0D;
            return;
        }
         */
        cVector lA = cStatic.point1; // new cVector(pA);
        lA.set(pA);
        cVector lB = cStatic.point2; // new cVector(pB);
        lB.set(pB);
        if (lA.length2() > 0.0F)
        {
            lA.normalize();
        }
        if (lB.length2() > 0.0F)
        {
            lB.normalize();
        }
        double lDot = lA.dot(lB);
        cVector lCP = cStatic.point3; // new cVector();
        lCP.cross(lA, lB);
        if (lCP.length2() > 0.0F)
        {
            lCP.normalize();
        } else if (lDot < 0.0D)
        {
            lCP.cross(lA, cVector.Y);
            if (lCP.length2() > 0.0F)
            {
                lCP.normalize();
            } else
            {
                lCP.cross(lA, cVector.X);
                lCP.normalize();
            }
        }
        double lTmp = (lDot + 1.0D) / 2D;
        double lCos = Math.sqrt(lTmp);
        double lSin = Math.sqrt(1.0D - lTmp);
        x = lCP.x * lSin;
        y = lCP.y * lSin;
        z = lCP.z * lSin;
        w = lCos;
 
        /*
        |       2     2                                |
        | 1 - 2Y  - 2Z    2XY - 2ZW      2XZ + 2YW     |
        |                                              |
        |                       2     2                |
        M = | 2XY + 2ZW       1 - 2X  - 2Z   2YZ - 2XW     |
        |                                              |
        |                                      2     2 |
        | 2XZ - 2YW       2YZ + 2XW      1 - 2X  - 2Y  |
        |                                              |
         */
        mat[0][0] = 1 - 2 * (y*y + z*z);
        mat[1][1] = 1 - 2 * (x*x + z*z);
        mat[2][2] = 1 - 2 * (x*x + y*y);
        mat[0][1] = 2 * (x*y - z*w);
        mat[0][2] = 2 * (x*z + y*w);
        mat[1][0] = 2 * (x*y + z*w);
        mat[1][2] = 2 * (y*z - x*w);
        mat[2][0] = 2 * (x*z - y*w);
        mat[2][1] = 2 * (y*z + x*w);
    }
 
    static void matTranspose(double input[][], double output[][])
    {
        matCopy(input, output);
 
        for (int i = 1; i < 4; i++)
        {
            for (int j = 0; j < i; j++)
            {
                double t = output[i][j];
                output[i][j] = output[j][i];
                output[j][i] = t;
            }
        }
    }
 
    static void matInvert(double input[][], double output[][])
    {
        int irow = 0;
        int icol = 0;
        matCopy(input, output);
        for (int i = 0; i < 4; i++)
        {
            ipiv[i] = -1;
        }
 
        for (int i = 0; i < 4; i++)
        {
            double big = 0;
            for (int j = 0; j < 4; j++)
            {
                if (ipiv[j] != 0)
                {
                    double[] cj = output[j];
                    for (int k = 0; k < 4; k++)
                    {
                        if (ipiv[k] == -1)
                        {
                            double piv = Math.abs(cj[k]);
                            if (piv >= big)
                            {
                                big = piv;
                                irow = j;
                                icol = k;
                            }
                        } else if (ipiv[k] > 0)
                        {
                            System.out.println("singular matrix");
                            //new Exception().printStackTrace();
                            return;
                        }
                    }
 
                }
            }
 
            ipiv[icol]++;
            double[] col = output[icol];
            if (irow != icol)
            {
                double[] row = output[irow];
                for (int j = 0; j < 4; j++)
                {
                    double dum = row[j];
                    row[j] = col[j];
                    col[j] = dum;
                }
 
            }
            
            indxr[i] = irow;
            indxc[i] = icol;
            /*
            if (Math.abs(output[icol][icol]) < 1E-20)
            {
                System.out.println("singular matrix II");
                new Exception().printStackTrace();
                return;
            }
            */
            double pivinv = 1 / col[icol];
            col[icol] = 1;
            for (int j = 0; j < 4; j++)
            {
                col[j] *= pivinv;
            }
 
            for (int j = 0; j < 4; j++)
            {
                if (j != icol)
                {
                    double[] cj = output[j];
                    double dum = cj[icol];
                    cj[icol] = 0;
                    for (int k = 0; k < 4; k++)
                    {
                        cj[k] -= col[k] * dum;
                    }
                }
            }
        }
 
        for (int i = 3; i >= 0; i--)
        {
            if (indxr[i] != indxc[i])
            {
                for (int j = 0; j < 4; j++)
                {
                    double[] cj = output[j];
                    double dum = cj[indxr[i]];
                    cj[indxr[i]] = cj[indxc[i]];
                    cj[indxc[i]] = dum;
                }
            }
        }
    }
    
    private static cVector xfpTemp = new cVector();
    private static cVector xfdTemp = new cVector();
    private static double concatTemp[][] = new double[4][4];
    private static double xlateTemp[][] = new double[4][4];
    private static double rotTemp[][] = new double[4][4];
    private static int ipiv[] = new int[4];
    private static int indxr[] = new int[4];
    private static int indxc[] = new int[4];
    
    static double[][] Identity = new double[4][4];
    
    static int SIZE = 0; // 65536*64;
    
    static double[] costable = new double[SIZE];
    static double[] sintable = new double[SIZE];
    
    static double PI2 = 2 * Math.PI;
        
    static
    {
        for (int i=SIZE; --i>=0;)
        {
            costable[i] = Math.cos(PI2 * i/SIZE);
            sintable[i] = Math.sin(PI2 * i*i/SIZE/SIZE);
        }
        
        LA.matIdentity(Identity);
    }
    
    static double cos(double x0)
    {
        if (true)
            return Math.cos(x0);
        
        double x = x0;
        
        while (x < 0)
        {
            x += PI2;
        }
        
        while (x > PI2)
        {
            x -= PI2;
        }
        
        x /= PI2;
        
        int modi = (int)(x*(SIZE-1));
        
//        double real = Math.cos(x0);
//        double test = costable[modi];
//        
//        if (Math.abs(real - test) > 0.000001)
//            x = x;
 
        return costable[modi];
    }
    
    static double sin(double x0)
    {
        if (true)
            return Math.sin(x0);
        
        double x = x0;
                
        while (x < 0)
        {
            x += PI2;
        }
        
        while (x > PI2)
        {
            x -= PI2;
        }
        
        x /= PI2;
        
        x = Math.sqrt(x);
        
        int modi = (int)(x*(SIZE-1));
 
//        double real = Math.sin(x0);
//        double test = sintable[modi];
//        
//        if (Math.abs(real - test) > 0.000001)
//            x = x;
 
        return sintable[modi];
    }
}