summaryrefslogtreecommitdiff
path: root/src/gp/gp_Mat.cxx
blob: 430cf8aabe8b21559b1a82c1efb31176220f2095 (plain)
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
// File gp_Mat.cxx, JCV 05/12/90
//  10/09/97 : PMN : Correction BUC40192 (pb avec les matrices negatives)

#ifndef DEB
#define No_Standard_OutOfRange
#define No_Standard_ConstructionError
#endif

#include <gp_Mat.ixx>

gp_Mat::gp_Mat (const gp_XYZ& Col1,
		const gp_XYZ& Col2,
		const gp_XYZ& Col3)
{
  Mat00 = Col1.X(); Mat10 = Col1.Y(); Mat20 = Col1.Z();
  Mat01 = Col2.X(); Mat11 = Col2.Y(); Mat21 = Col2.Z();
  Mat02 = Col3.X(); Mat12 = Col3.Y(); Mat22 = Col3.Z();
}

void gp_Mat::SetCol (const Standard_Integer Col,
		     const gp_XYZ& Value) {

  Standard_OutOfRange_Raise_if (Col < 1 || Col > 3, " ");
  if      (Col == 1) {
    Mat00 = Value.X(); Mat10 = Value.Y(); Mat20 = Value.Z();
  }
  else if (Col == 2) {
    Mat01 = Value.X(); Mat11 = Value.Y(); Mat21 = Value.Z();
  }
  else {
    Mat02 = Value.X(); Mat12 = Value.Y(); Mat22 = Value.Z();
  }
}

void gp_Mat::SetCols (const gp_XYZ& Col1,
		      const gp_XYZ& Col2,
		      const gp_XYZ& Col3)
{
  Mat00 = Col1.X(); Mat10 = Col1.Y(); Mat20 = Col1.Z();
  Mat01 = Col2.X(); Mat11 = Col2.Y(); Mat21 = Col2.Z();
  Mat02 = Col3.X(); Mat12 = Col3.Y(); Mat22 = Col3.Z();
}

void gp_Mat::SetCross (const gp_XYZ& Ref)
{
  Standard_Real X = Ref.X();
  Standard_Real Y = Ref.Y();
  Standard_Real Z = Ref.Z();
  Mat00 = Mat11 = Mat22 = 0.0;
  Mat01 = - Z;  
  Mat02 = Y;
  Mat12 = - X;
  Mat10 = Z;
  Mat20 = - Y;
  Mat21 = X;
}

void gp_Mat::SetDot (const gp_XYZ& Ref)
{
  Standard_Real X = Ref.X();
  Standard_Real Y = Ref.Y();
  Standard_Real Z = Ref.Z();
  Mat00 = X * X;
  Mat11 = Y * Y;
  Mat22 = Z * Z;
  Mat01 = X * Y;
  Mat02 = X * Z;
  Mat12 = Y * Z;
  Mat10 = Mat01;
  Mat20 = Mat02;
  Mat21 = Mat12;
}

void gp_Mat::SetRotation (const gp_XYZ& Axis,
			  const Standard_Real Ang)
{
  //    Rot = I + sin(Ang) * M + (1. - cos(Ang)) * M*M
  //    avec  M . XYZ = Axis ^ XYZ

//  const Standard_Address M = (Standard_Address)&(matrix[0][0]);
  gp_XYZ V = Axis.Normalized();
  SetCross (V);
  Multiply (sin(Ang));
  gp_Mat Temp;
  Temp.SetScale (1.0);
  Add (Temp);
  Standard_Real A = V.X();
  Standard_Real B = V.Y();
  Standard_Real C = V.Z();
  Temp.SetRow (1, gp_XYZ(- C*C - B*B,      A*B,           A*C     ));
  Temp.SetRow (2, gp_XYZ(     A*B,      -A*A - C*C,        B*C    ));
  Temp.SetRow (3, gp_XYZ(     A*C,          B*C,       - A*A - B*B));
  Temp.Multiply (1.0 - cos(Ang));
  Add (Temp);
}

void gp_Mat::SetRow (const Standard_Integer Row,
		     const gp_XYZ& Value)
{
  Standard_OutOfRange_Raise_if (Row < 1 || Row > 3, " ");
  if      (Row == 1) {
    Mat00 = Value.X(); Mat01 = Value.Y(); Mat02 = Value.Z();
  }
  else if (Row == 2) {
    Mat10 = Value.X(); Mat11 = Value.Y(); Mat12 = Value.Z();
  }
  else {
    Mat20 = Value.X(); Mat21 = Value.Y(); Mat22 = Value.Z();
  }
}

void gp_Mat::SetRows (const gp_XYZ& Row1,
		      const gp_XYZ& Row2,
		      const gp_XYZ& Row3)
{
  Mat00 = Row1.X(); Mat01 = Row1.Y(); Mat02 = Row1.Z();
  Mat10 = Row2.X(); Mat11 = Row2.Y(); Mat12 = Row2.Z();
  Mat20 = Row3.X(); Mat21 = Row3.Y(); Mat22 = Row3.Z();
}

gp_XYZ gp_Mat::Column (const Standard_Integer Col) const
{
  Standard_OutOfRange_Raise_if (Col < 1 || Col > 3, "");
  if (Col == 1) return gp_XYZ (Mat00,Mat10,Mat20);
  if (Col == 2) return gp_XYZ (Mat01,Mat11,Mat21);
  return gp_XYZ (Mat02,Mat12,Mat22);
}

gp_XYZ gp_Mat::Diagonal () const
{
  return gp_XYZ (Mat00, Mat11, Mat22);
}

gp_XYZ gp_Mat::Row (const Standard_Integer Row) const
{
  Standard_OutOfRange_Raise_if (Row < 1 || Row > 3, "");
  if (Row == 1) return gp_XYZ (Mat00,Mat01,Mat02);
  if (Row == 2) return gp_XYZ (Mat10,Mat11,Mat12);
  return gp_XYZ (Mat20,Mat21,Mat22);
}

void gp_Mat::Invert ()
{ 
  gp_Mat NewMat;    
  //
  // calcul de  la transposee de la commatrice
  //
  Nat00 = Mat11 * Mat22 - Mat12 * Mat21 ;
  Nat10 = -(Mat10 * Mat22 - Mat20 * Mat12) ;
  Nat20 = Mat10 * Mat21 - Mat20 * Mat11 ;
  Nat01 = - (Mat01 * Mat22 - Mat21 * Mat02) ;
  Nat11 = Mat00 * Mat22 - Mat20 * Mat02 ;
  Nat21 = -(Mat00 * Mat21 - Mat20 * Mat01) ;
  Nat02 = Mat01 * Mat12 - Mat11 * Mat02 ;
  Nat12 = -(Mat00 * Mat12 - Mat10 * Mat02) ;
  Nat22 = Mat00 * Mat11 - Mat01 * Mat10 ;
  Standard_Real det =  Mat00 * Nat00 + Mat01* Nat10 + Mat02 * Nat20 ;
  Standard_Real val = det;
  if (val < 0) val = - val;
  Standard_ConstructionError_Raise_if
    (val <= gp::Resolution(),"");
  det = 1.0e0 / det ;
  Mat00 = Nat00;
  Mat10 = Nat10;
  Mat20 = Nat20;
  Mat01 = Nat01;
  Mat11 = Nat11;
  Mat21 = Nat21;
  Mat02 = Nat02;
  Mat12 = Nat12;
  Mat22 = Nat22;
  Multiply(det) ;
}

gp_Mat gp_Mat::Inverted () const
{ 
  gp_Mat NewMat;
  //
  // calcul de  la transposee de la commatrice
  //
  Nat00 = Mat11 * Mat22 - Mat12 * Mat21 ;
  Nat10 = -(Mat10 * Mat22 - Mat20 * Mat12) ;
  Nat20 = Mat10 * Mat21 - Mat20 * Mat11 ;
  Nat01 = - (Mat01 * Mat22 - Mat21 * Mat02) ;
  Nat11 = Mat00 * Mat22 - Mat20 * Mat02 ;
  Nat21 = -(Mat00 * Mat21 - Mat20 * Mat01) ;
  Nat02 = Mat01 * Mat12 - Mat11 * Mat02 ;
  Nat12 = -(Mat00 * Mat12 - Mat10 * Mat02) ;
  Nat22 = Mat00 * Mat11 - Mat01 * Mat10 ;
  Standard_Real det =  Mat00 * Nat00 + Mat01* Nat10 + Mat02 * Nat20 ;
  Standard_Real val = det;
  if (val < 0) val = - val;
  Standard_ConstructionError_Raise_if
    (val <= gp::Resolution(),"");
  det = 1.0e0 / det ;
  NewMat.Multiply(det) ;
  return NewMat;
}

void gp_Mat::Power (const Standard_Integer N)
{
  if (N == 1) { }
  else if (N == 0)  { SetIdentity() ; }
  else if (N == -1) { Invert(); }
  else {
    if (N < 0) { Invert(); }
    Standard_Integer Npower = N;
    if (Npower < 0) Npower = - Npower;
    Npower--;
    gp_Mat Temp = *this;
    while (1) {
      if (IsOdd(Npower)) Multiply (Temp);
      if (Npower == 1)   break; 
      Temp.Multiply (Temp);
      Npower>>=1;
    }
  }
}