Normand Briere
2019-09-08 4a303a7b3635adfee8f46ac76af4e1b7b4a7029b
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author nbriere
 */
public class cMatrix3x3
{
    double[][] matrix = new double[3][3];
    
    cMatrix3x3()
    {
    }
 
    cMatrix3x3(cSpring.Point3D a, cSpring.Point3D b)
    {
        v1.x = a.x;
        v1.y = a.y;
        v1.z = a.z;
        v2.x = b.x;
        v2.y = b.y;
        v2.z = b.z;
        
        quat.setRotation(v1, v2);
        
        setRotation(quat);
    }
    
    cMatrix3x3(cVector a, cVector b)
    {
        quat.setRotation(a, b);
        
        setRotation(quat);
    }
    
    void setRotation(cQuat q)
    {
        matrix[0][0] = 1 - 2*(q.y*q.y + q.z*q.z);
        matrix[1][1] = 1 - 2*(q.x*q.x + q.z*q.z);
        matrix[2][2] = 1 - 2*(q.x*q.x + q.y*q.y);
        matrix[0][1] = 2*(q.x*q.y - q.z*q.w);
        matrix[0][2] = 2*(q.x*q.z + q.y*q.w);
        matrix[1][2] = 2*(q.y*q.z - q.x*q.w);
        matrix[1][0] = 2*(q.x*q.y + q.z*q.w);
        matrix[2][0] = 2*(q.x*q.z - q.y*q.w);
        matrix[2][1] = 2*(q.y*q.z + q.x*q.w);
    }
 
    void Transform(cSpring.Point3D p)
    {
        temp.set(p);
        
        p.x = (float) (matrix[0][0]*temp.x + matrix[0][1]*temp.y + matrix[0][2]*temp.z);
        p.y = (float) (matrix[1][0]*temp.x + matrix[1][1]*temp.y + matrix[1][2]*temp.z);
        p.z = (float) (matrix[2][0]*temp.x + matrix[2][1]*temp.y + matrix[2][2]*temp.z);
    }
    
    void Transform(cVector p)
    {
        temp.set(p);
        
        p.x = matrix[0][0]*temp.x + matrix[0][1]*temp.y + matrix[0][2]*temp.z;
        p.y = matrix[1][0]*temp.x + matrix[1][1]*temp.y + matrix[1][2]*temp.z;
        p.z = matrix[2][0]*temp.x + matrix[2][1]*temp.y + matrix[2][2]*temp.z;
    }
    
    static cSpring.Point3D temp = new cSpring.Point3D();
    static cQuat quat = new cQuat();
    static cVector v1 = new cVector();
    static cVector v2 = new cVector();
}