~ubuntu-branches/ubuntu/trusty/travis/trusty-proposed

« back to all changes in this revision

Viewing changes to src/xmatrix3.cpp

  • Committer: Package Import Robot
  • Author(s): Daniel Leidert
  • Date: 2014-01-18 20:07:16 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140118200716-whsmcg7fa1eyqecq
Tags: 140117-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
    TRAVIS - Trajectory Analyzer and Visualizer
3
 
    http://www.travis-analyzer.de/
4
 
 
5
 
    Copyright (c) 2009-2013 Martin Brehm
6
 
                  2012-2013 Martin Thomas
7
 
 
8
 
    This file written by Martin Brehm.
9
 
 
10
 
    This program is free software: you can redistribute it and/or modify
11
 
    it under the terms of the GNU General Public License as published by
12
 
    the Free Software Foundation, either version 3 of the License, or
13
 
    (at your option) any later version.
14
 
 
15
 
    This program is distributed in the hope that it will be useful,
16
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
    GNU General Public License for more details.
19
 
 
20
 
    You should have received a copy of the GNU General Public License
21
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
*****************************************************************************/
23
 
 
24
 
#include "xmatrix3.h"
25
 
#include "tools.h"
26
 
 
27
 
 
28
 
void CxMatrix3::Dump() const
29
 
{
30
 
        BXIN;
31
 
        int z/*, z2*/;
32
 
        for (z=0;z<3;z++)
33
 
        {
34
 
/*              for (z2=0;z2<3;z2++)
35
 
                        if ((GetAt(z2,z) < 0) && (GetAt(z2,z) > -0.000000001))
36
 
                                GetAt(z2,z) = 0;*/
37
 
                mprintf("( %6.3f %6.3f %6.3f )\n",GetAt(0,z),GetAt(1,z),GetAt(2,z));
38
 
        }
39
 
        BXOUT;
40
 
}
41
 
 
42
 
 
43
 
void CxMatrix3::MatUltra(const CxVector3 &vec1, const CxVector3 &vec2)
44
 
{
45
 
        BXIN;
46
 
        CxMatrix3 mat, mat2;
47
 
        CxVector3 vectemp, vecx, vecy, vec2n, vectemp2;
48
 
/*      float mat[3][3], mat2[3][3];
49
 
        float vectemp[3], vecx[3], vecy[3], vec2n[3], vectemp2[3];*/
50
 
        float a;
51
 
 
52
 
#ifdef DEBUG_MATULTRA
53
 
        mprintf("\nLinAlg Checker\n\n");
54
 
        mprintf("Vektor 1: "); vec1.Dump();
55
 
        mprintf("\nVektor 2: "); vec2.Dump();
56
 
#endif
57
 
 
58
 
        vecx[0] = 1.0f;
59
 
        vecx[1] = 0.0f;
60
 
        vecx[2] = 0.0f;
61
 
        vecy[0] = 0.0f;
62
 
        vecy[1] = 1.0f;
63
 
        vecy[2] = 0.0f;
64
 
 
65
 
#ifdef DEBUG_MATULTRA
66
 
        mprintf("\nVektor X: "); vecx.Dump();
67
 
        mprintf("\nVektor Y: "); vecy.Dump();
68
 
#endif
69
 
 
70
 
        // Zu erst vec1 auf X drehen
71
 
        vectemp = CrossP(vec1,vecx);
72
 
        if (vectemp.GetLength() != 0)
73
 
        {
74
 
#ifdef DEBUG_MATULTRA
75
 
                mprintf("\nDrehachse fuer Vektor1->X: ");
76
 
                vectemp.Dump();
77
 
#endif
78
 
                vectemp.Normalize();
79
 
#ifdef DEBUG_MATULTRA
80
 
                mprintf("\nNormalisiert: ");
81
 
                vectemp.Dump();
82
 
#endif
83
 
                a = Angle(vec1,vecx);
84
 
#ifdef DEBUG_MATULTRA
85
 
                mprintf("\nDrehwinkel: %.1f Grad\n",a*180.0/Pi);
86
 
#endif
87
 
                mat.RotMat(vectemp,a);
88
 
        } else
89
 
        {
90
 
#ifdef DEBUG_MATULTRA
91
 
                mprintf("\nDer liegt schon auf der X-Achse!");
92
 
#endif
93
 
                mat.Unity();
94
 
        }
95
 
        vec2n = mat*vec2;
96
 
 
97
 
#ifdef DEBUG_MATULTRA
98
 
        mprintf("Drehmatrix dafuer:\n"); mat.Dump();
99
 
        vectemp = mat*vec1;
100
 
        mprintf("\nDamit wird Vektor1 zu "); vectemp.Dump();
101
 
        mprintf("\nUnd Vektor2 zu "); vec2n.Dump();
102
 
#endif
103
 
 
104
 
        if ((vec2-vec1).GetLength() < 0.0001) // vec1 == vec2, nichts in X-Y-Ebene drehen
105
 
        {
106
 
                *this = mat;
107
 
                BXOUT;
108
 
                return;
109
 
        }
110
 
 
111
 
        vectemp2[0] = 0;
112
 
        vectemp2[1] = vec2n[1];
113
 
        vectemp2[2] = vec2n[2];
114
 
 
115
 
#ifdef DEBUG_MATULTRA
116
 
        mprintf("\nProjektion von Vektor2n auf YZ-Ebene: ");
117
 
        vectemp2.Dump();
118
 
#endif
119
 
 
120
 
        a = Angle(vectemp2,vecy);
121
 
        
122
 
#ifdef DEBUG_MATULTRA
123
 
        mprintf("\n");
124
 
#endif
125
 
 
126
 
        if (vectemp2.GetLength()==0)
127
 
        {
128
 
                eprintf("\nMatUltra: Error caught.\n");
129
 
                a = (float)Pi/2.0f;
130
 
        }
131
 
 
132
 
        if (vec2n[2] > 0)
133
 
                a = -a; 
134
 
        
135
 
#ifdef DEBUG_MATULTRA
136
 
        mprintf("\nDrehachse fuer Vektor2->XY: "); 
137
 
        vecx.Dump();
138
 
        mprintf("\nDrehwinkel: %.1f Grad\n",a*180.0/Pi);
139
 
#endif
140
 
 
141
 
        mat2.RotMat(vecx,a);
142
 
 
143
 
#ifdef DEBUG_MATULTRA
144
 
        mprintf("Drehmatrix dafuer:\n"); mat2.Dump();
145
 
        vectemp = mat*vec1;
146
 
        vectemp2 = mat2*vectemp;
147
 
        mprintf("\nDamit wird Vektor1 zu "); vectemp2.Dump();
148
 
        vectemp = mat2*vec2n;
149
 
        mprintf("\nUnd Vektor2n zu "); vectemp.Dump();
150
 
#endif
151
 
 
152
 
        *this = mat2*mat;
153
 
 
154
 
#ifdef DEBUG_MATULTRA
155
 
        mprintf("\nMacht diese Gesamtmatrix:\n"); Dump();
156
 
        vectemp = *this*vec1;
157
 
        mprintf("\nDamit wird Vektor 1 von "); vec1.Dump(); mprintf(" zu "); vectemp.Dump();
158
 
        vectemp = *this*vec2;
159
 
        mprintf("\nUnd Vektor 2 wird von "); vec2.Dump(); mprintf(" zu "); vectemp.Dump();
160
 
        mprintf("\n");
161
 
#endif
162
 
 
163
 
        BXOUT;
164
 
}
165
 
 
166
 
 
167
 
void CxMatrix3::Invert()
168
 
{
169
 
        #define a11 GetAt(0,0)
170
 
        #define a12 GetAt(0,1)
171
 
        #define a13 GetAt(0,2)
172
 
        #define a21 GetAt(1,0)
173
 
        #define a22 GetAt(1,1)
174
 
        #define a23 GetAt(1,2)
175
 
        #define a31 GetAt(2,0)
176
 
        #define a32 GetAt(2,1)
177
 
        #define a33 GetAt(2,2)
178
 
 
179
 
        CxMatrix3 t;
180
 
        float det;
181
 
 
182
 
        det = a11 * (a33*a22 - a32*a23)
183
 
                 - a21 * (a33*a12 - a32*a13)
184
 
                 + a31 * (a23*a12 - a22*a13);
185
 
 
186
 
        if (det == 0)
187
 
        {
188
 
                eprintf("CxMatrix3::Invert(): Failed - matrix is singular.\n");
189
 
                return;
190
 
        }
191
 
 
192
 
        t.GetAt(0,0) =  (a33*a22 - a32*a23);
193
 
        t.GetAt(0,1) = -(a33*a12 - a32*a13);
194
 
        t.GetAt(0,2) =  (a23*a12 - a22*a13);
195
 
        t.GetAt(1,0) = -(a33*a21 - a31*a23);
196
 
        t.GetAt(1,1) =  (a33*a11 - a31*a13);
197
 
        t.GetAt(1,2) = -(a23*a11 - a21*a13);
198
 
        t.GetAt(2,0) =  (a32*a21 - a31*a22);
199
 
        t.GetAt(2,1) = -(a32*a11 - a31*a12);
200
 
        t.GetAt(2,2) =  (a22*a11 - a21*a12);
201
 
 
202
 
        t *= 1.0f/det;
203
 
 
204
 
        *this = t;
205
 
}
 
1
/*****************************************************************************
 
2
    TRAVIS - Trajectory Analyzer and Visualizer
 
3
    http://www.travis-analyzer.de/
 
4
 
 
5
    Copyright (c) 2009-2014 Martin Brehm
 
6
                  2012-2014 Martin Thomas
 
7
 
 
8
    This file written by Martin Brehm.
 
9
 
 
10
    This program is free software: you can redistribute it and/or modify
 
11
    it under the terms of the GNU General Public License as published by
 
12
    the Free Software Foundation, either version 3 of the License, or
 
13
    (at your option) any later version.
 
14
 
 
15
    This program is distributed in the hope that it will be useful,
 
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
    GNU General Public License for more details.
 
19
 
 
20
    You should have received a copy of the GNU General Public License
 
21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
*****************************************************************************/
 
23
 
 
24
#include "xmatrix3.h"
 
25
#include "tools.h"
 
26
 
 
27
 
 
28
void CxMatrix3::Dump() const
 
29
{
 
30
        BXIN;
 
31
        int z/*, z2*/;
 
32
        for (z=0;z<3;z++)
 
33
        {
 
34
/*              for (z2=0;z2<3;z2++)
 
35
                        if ((GetAt(z2,z) < 0) && (GetAt(z2,z) > -0.000000001))
 
36
                                GetAt(z2,z) = 0;*/
 
37
                mprintf("( %6.3f %6.3f %6.3f )\n",GetAt(0,z),GetAt(1,z),GetAt(2,z));
 
38
        }
 
39
        BXOUT;
 
40
}
 
41
 
 
42
 
 
43
void CxMatrix3::MatUltra(const CxVector3 &vec1, const CxVector3 &vec2)
 
44
{
 
45
        BXIN;
 
46
        CxMatrix3 mat, mat2;
 
47
        CxVector3 vectemp, vecx, vecy, vec2n, vectemp2;
 
48
/*      float mat[3][3], mat2[3][3];
 
49
        float vectemp[3], vecx[3], vecy[3], vec2n[3], vectemp2[3];*/
 
50
        float a;
 
51
 
 
52
#ifdef DEBUG_MATULTRA
 
53
        mprintf("\nLinAlg Checker\n\n");
 
54
        mprintf("Vektor 1: "); vec1.Dump();
 
55
        mprintf("\nVektor 2: "); vec2.Dump();
 
56
#endif
 
57
 
 
58
        vecx[0] = 1.0f;
 
59
        vecx[1] = 0.0f;
 
60
        vecx[2] = 0.0f;
 
61
        vecy[0] = 0.0f;
 
62
        vecy[1] = 1.0f;
 
63
        vecy[2] = 0.0f;
 
64
 
 
65
#ifdef DEBUG_MATULTRA
 
66
        mprintf("\nVektor X: "); vecx.Dump();
 
67
        mprintf("\nVektor Y: "); vecy.Dump();
 
68
#endif
 
69
 
 
70
        // Zu erst vec1 auf X drehen
 
71
        vectemp = CrossP(vec1,vecx);
 
72
        if (vectemp.GetLength() != 0)
 
73
        {
 
74
#ifdef DEBUG_MATULTRA
 
75
                mprintf("\nDrehachse fuer Vektor1->X: ");
 
76
                vectemp.Dump();
 
77
#endif
 
78
                vectemp.Normalize();
 
79
#ifdef DEBUG_MATULTRA
 
80
                mprintf("\nNormalisiert: ");
 
81
                vectemp.Dump();
 
82
#endif
 
83
                a = Angle(vec1,vecx);
 
84
#ifdef DEBUG_MATULTRA
 
85
                mprintf("\nDrehwinkel: %.1f Grad\n",a*180.0/Pi);
 
86
#endif
 
87
                mat.RotMat(vectemp,a);
 
88
        } else
 
89
        {
 
90
#ifdef DEBUG_MATULTRA
 
91
                mprintf("\nDer liegt schon auf der X-Achse!");
 
92
#endif
 
93
                mat.Unity();
 
94
        }
 
95
        vec2n = mat*vec2;
 
96
 
 
97
#ifdef DEBUG_MATULTRA
 
98
        mprintf("Drehmatrix dafuer:\n"); mat.Dump();
 
99
        vectemp = mat*vec1;
 
100
        mprintf("\nDamit wird Vektor1 zu "); vectemp.Dump();
 
101
        mprintf("\nUnd Vektor2 zu "); vec2n.Dump();
 
102
#endif
 
103
 
 
104
        if ((vec2-vec1).GetLength() < 0.0001) // vec1 == vec2, nichts in X-Y-Ebene drehen
 
105
        {
 
106
                *this = mat;
 
107
                BXOUT;
 
108
                return;
 
109
        }
 
110
 
 
111
        vectemp2[0] = 0;
 
112
        vectemp2[1] = vec2n[1];
 
113
        vectemp2[2] = vec2n[2];
 
114
 
 
115
#ifdef DEBUG_MATULTRA
 
116
        mprintf("\nProjektion von Vektor2n auf YZ-Ebene: ");
 
117
        vectemp2.Dump();
 
118
#endif
 
119
 
 
120
        a = Angle(vectemp2,vecy);
 
121
        
 
122
#ifdef DEBUG_MATULTRA
 
123
        mprintf("\n");
 
124
#endif
 
125
 
 
126
        if (vectemp2.GetLength()==0)
 
127
        {
 
128
                eprintf("\nMatUltra: Error caught.\n");
 
129
                a = (float)Pi/2.0f;
 
130
        }
 
131
 
 
132
        if (vec2n[2] > 0)
 
133
                a = -a; 
 
134
        
 
135
#ifdef DEBUG_MATULTRA
 
136
        mprintf("\nDrehachse fuer Vektor2->XY: "); 
 
137
        vecx.Dump();
 
138
        mprintf("\nDrehwinkel: %.1f Grad\n",a*180.0/Pi);
 
139
#endif
 
140
 
 
141
        mat2.RotMat(vecx,a);
 
142
 
 
143
#ifdef DEBUG_MATULTRA
 
144
        mprintf("Drehmatrix dafuer:\n"); mat2.Dump();
 
145
        vectemp = mat*vec1;
 
146
        vectemp2 = mat2*vectemp;
 
147
        mprintf("\nDamit wird Vektor1 zu "); vectemp2.Dump();
 
148
        vectemp = mat2*vec2n;
 
149
        mprintf("\nUnd Vektor2n zu "); vectemp.Dump();
 
150
#endif
 
151
 
 
152
        *this = mat2*mat;
 
153
 
 
154
#ifdef DEBUG_MATULTRA
 
155
        mprintf("\nMacht diese Gesamtmatrix:\n"); Dump();
 
156
        vectemp = *this*vec1;
 
157
        mprintf("\nDamit wird Vektor 1 von "); vec1.Dump(); mprintf(" zu "); vectemp.Dump();
 
158
        vectemp = *this*vec2;
 
159
        mprintf("\nUnd Vektor 2 wird von "); vec2.Dump(); mprintf(" zu "); vectemp.Dump();
 
160
        mprintf("\n");
 
161
#endif
 
162
 
 
163
        BXOUT;
 
164
}
 
165
 
 
166
 
 
167
void CxMatrix3::Invert()
 
168
{
 
169
        #define a11 GetAt(0,0)
 
170
        #define a12 GetAt(0,1)
 
171
        #define a13 GetAt(0,2)
 
172
        #define a21 GetAt(1,0)
 
173
        #define a22 GetAt(1,1)
 
174
        #define a23 GetAt(1,2)
 
175
        #define a31 GetAt(2,0)
 
176
        #define a32 GetAt(2,1)
 
177
        #define a33 GetAt(2,2)
 
178
 
 
179
        CxMatrix3 t;
 
180
        float det;
 
181
 
 
182
        det = a11 * (a33*a22 - a32*a23)
 
183
                 - a21 * (a33*a12 - a32*a13)
 
184
                 + a31 * (a23*a12 - a22*a13);
 
185
 
 
186
        if (det == 0)
 
187
        {
 
188
                eprintf("CxMatrix3::Invert(): Failed - matrix is singular.\n");
 
189
                return;
 
190
        }
 
191
 
 
192
        t.GetAt(0,0) =  (a33*a22 - a32*a23);
 
193
        t.GetAt(0,1) = -(a33*a12 - a32*a13);
 
194
        t.GetAt(0,2) =  (a23*a12 - a22*a13);
 
195
        t.GetAt(1,0) = -(a33*a21 - a31*a23);
 
196
        t.GetAt(1,1) =  (a33*a11 - a31*a13);
 
197
        t.GetAt(1,2) = -(a23*a11 - a21*a13);
 
198
        t.GetAt(2,0) =  (a32*a21 - a31*a22);
 
199
        t.GetAt(2,1) = -(a32*a11 - a31*a12);
 
200
        t.GetAt(2,2) =  (a22*a11 - a21*a12);
 
201
 
 
202
        t *= 1.0f/det;
 
203
 
 
204
        *this = t;
 
205
}