0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / gp / gp_Torus.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <gp_Torus.hxx>
16
17 #include <gp_Ax1.hxx>
18 #include <gp_Ax2.hxx>
19 #include <gp_Ax3.hxx>
20 #include <gp_Pnt.hxx>
21 #include <gp_Trsf.hxx>
22 #include <Standard_DimensionError.hxx>
23
24 void gp_Torus::Coefficients (TColStd_Array1OfReal& theCoef) const
25 {
26   //  R = majorRadius;
27   //  r = minorRadius.
28
29   //  X = (R + r*cos(V))*cos(U)
30   //  Y = (R + r*cos(V))*sin(U)
31   //  Z = r*sin(V)
32
33   //Therefore,
34   //  4*R*R*(r*r - Z*Z) = (X*X + Y*Y + Z*Z - R*R - r*r)^2
35   //Or
36   //  X^4+Y^4+Z^4+
37   //  2*((X*Y)^2+(X*Z)^2+(Y*Z)^2)-
38   //  2*(R^2+r^2)*(X^2+Y^2)+
39   //  2*(R^2-r^2)*Z^2+(R^2-r^2)^2 = 0.0
40
41   const Standard_Integer aLowIndex = theCoef.Lower();
42   Standard_DimensionError_Raise_if (theCoef.Length() < 35,
43               "gp_Torus::theCoefficients(): Dimension mismatch");
44   
45   gp_Trsf aTr;
46   aTr.SetTransformation (pos);
47   const Standard_Real aT11 = aTr.Value (1, 1);
48   const Standard_Real aT12 = aTr.Value (1, 2);
49   const Standard_Real aT13 = aTr.Value (1, 3);
50   const Standard_Real aT14 = aTr.Value (1, 4);
51   const Standard_Real aT21 = aTr.Value (2, 1);
52   const Standard_Real aT22 = aTr.Value (2, 2);
53   const Standard_Real aT23 = aTr.Value (2, 3);
54   const Standard_Real aT24 = aTr.Value (2, 4);
55   const Standard_Real aT31 = aTr.Value (3, 1);
56   const Standard_Real aT32 = aTr.Value (3, 2);
57   const Standard_Real aT33 = aTr.Value (3, 3);
58   const Standard_Real aT34 = aTr.Value (3, 4);
59
60   const Standard_Real aTcol1sq = aT11*aT11 + aT21*aT21 + aT31*aT31;
61   const Standard_Real aTcol2sq = aT12*aT12 + aT22*aT22 + aT32*aT32;
62   const Standard_Real aTcol3sq = aT13*aT13 + aT23*aT23 + aT33*aT33;
63   const Standard_Real aTcol4sq = aT14*aT14 + aT24*aT24 + aT34*aT34;
64   const Standard_Real aTcol1Tcol2 = aT11*aT12 + aT21*aT22 + aT31*aT32;
65   const Standard_Real aTcol1Tcol3 = aT11*aT13 + aT21*aT23 + aT31*aT33;
66   const Standard_Real aTcol2Tcol3 = aT12*aT13 + aT22*aT23 + aT32*aT33;
67   const Standard_Real aTcol1Tcol4 = aT11*aT14 + aT21*aT24 + aT31*aT34;
68   const Standard_Real aTcol2Tcol4 = aT12*aT14 + aT22*aT24 + aT32*aT34;
69   const Standard_Real aTcol3Tcol4 = aT13*aT14 + aT23*aT24 + aT33*aT34;
70   
71   const Standard_Real aSumRadius = (majorRadius*majorRadius +
72                                     minorRadius*minorRadius);
73   const Standard_Real aSubRadius = (majorRadius*majorRadius -
74                                     minorRadius*minorRadius);
75
76   /*
77   After substitution
78   Transpose([X Y Z 1]) = aTr*Transpose([X Y Z 1])
79   we will obtain:
80   */
81
82   theCoef(aLowIndex)     = aTcol1sq*aTcol1sq;  //X^4
83   theCoef(aLowIndex+1)   = aTcol2sq*aTcol2sq;  //Y^4
84   theCoef(aLowIndex+2)   = aTcol3sq*aTcol3sq;  //Z^4
85   theCoef(aLowIndex+3)   = 4.0*aTcol1sq*aTcol1Tcol2; //X^3*Y
86   theCoef(aLowIndex+4)   = 4.0*aTcol1sq*aTcol1Tcol3; //X^3*Z
87   theCoef(aLowIndex+5)   = 4.0*aTcol2sq*aTcol1Tcol2; //X*Y^3
88   theCoef(aLowIndex+6)   = 4.0*aTcol2sq*aTcol2Tcol3; //Y^3*Z
89   theCoef(aLowIndex+7)   = 4.0*aTcol3sq*aTcol1Tcol3; //X*Z^3
90   theCoef(aLowIndex+8)   = 4.0*aTcol3sq*aTcol2Tcol3; //Y*Z^3
91   theCoef(aLowIndex+9)   = 2.0*(aTcol1sq*aTcol2sq + 
92                                 2.0*aTcol1Tcol2*aTcol1Tcol2);  //X^2*Y^2
93   theCoef(aLowIndex+10)  = 2.0*(aTcol1sq*aTcol3sq +
94                                 2.0*aTcol1Tcol3*aTcol1Tcol3);  //X^2*Z^2
95   theCoef(aLowIndex+11)  = 2.0*(aTcol2sq*aTcol3sq +
96                                 2.0*aTcol2Tcol3*aTcol2Tcol3);  //Y^2*Z^2
97   theCoef(aLowIndex+12)  = 4.0*(aTcol1sq*aTcol2Tcol3 +
98                                 2.0*aTcol1Tcol2*aTcol1Tcol3); //X^2*Y*Z
99   theCoef(aLowIndex+13)  = 4.0*(aTcol2sq*aTcol1Tcol3 +
100                                 2.0*aTcol1Tcol2*aTcol2Tcol3); //X*Y^2*Z
101   theCoef(aLowIndex+14)  = 4.0*(aTcol3sq*aTcol1Tcol2 +
102                                 2.0*aTcol1Tcol3*aTcol2Tcol3); //X*Y*Z^2
103
104   theCoef(aLowIndex+15)  = 4.0*aTcol1sq*aTcol1Tcol4; //X^3
105   theCoef(aLowIndex+16)  = 4.0*aTcol2sq*aTcol2Tcol4; //Y^3
106   theCoef(aLowIndex+17)  = 4.0*aTcol3sq*aTcol3Tcol4; //Z^3
107   theCoef(aLowIndex+18)  = 4.0*(aTcol1sq*aTcol2Tcol4 +
108                                 2.0*aTcol1Tcol4*aTcol1Tcol2); //X^2*Y
109   theCoef(aLowIndex+19)  = 4.0*(aTcol1sq*aTcol3Tcol4 +
110                                 2.0*aTcol1Tcol4*aTcol1Tcol3); //X^2*Z
111   theCoef(aLowIndex+20)  = 4.0*(aTcol2sq*aTcol1Tcol4 +
112                                 2.0*aTcol2Tcol4*aTcol1Tcol2); //X*Y^2
113   theCoef(aLowIndex+21)  = 4.0*(aTcol2sq*aTcol3Tcol4 +
114                                 2.0*aTcol2Tcol4*aTcol2Tcol3); //Y^2*Z
115   theCoef(aLowIndex+22)  = 4.0*(aTcol3sq*aTcol1Tcol4 +
116                                 2.0*aTcol3Tcol4*aTcol1Tcol3); //X*Z^2
117   theCoef(aLowIndex+23)  = 4.0*(aTcol3sq*aTcol2Tcol4 +
118                                 2.0*aTcol3Tcol4*aTcol2Tcol3); //Y*Z^2
119   theCoef(aLowIndex+24)  = 8.0*(aTcol1Tcol2*aTcol3Tcol4 +
120                                 aTcol2Tcol3*aTcol1Tcol4 +
121                                 aTcol2Tcol4*aTcol1Tcol3); //X*Y*Z
122
123   theCoef(aLowIndex+25)  = 2.0*(aSubRadius*aT31*aT31 -
124                                 aSumRadius*(aT11*aT11 + aT21*aT21) +
125                                 aTcol4sq*aTcol1sq +
126                                 2.0*aTcol1Tcol4*aTcol1Tcol4); //X^2
127   theCoef(aLowIndex+26)  = 2.0*(aSubRadius*aT32*aT32 -
128                                 aSumRadius*(aT12*aT12 + aT22*aT22) +
129                                 aTcol4sq*aTcol2sq +
130                                 2.0*aTcol2Tcol4*aTcol2Tcol4); //Y^2
131   theCoef(aLowIndex+27)  = 2.0*(aSubRadius*aT33*aT33 -
132                                 aSumRadius*(aT13*aT13 + aT23*aT23) +
133                                 aTcol4sq*aTcol3sq +
134                                 2.0*aTcol3Tcol4*aTcol3Tcol4); //Z^2
135   theCoef(aLowIndex+28)  = 4.0*(aSubRadius*aT31*aT32 -
136                                 aSumRadius*(aT11*aT12 + aT21*aT22) +
137                                 aTcol4sq*aTcol1Tcol2 +
138                                 2.0*aTcol1Tcol4*aTcol2Tcol4); //X*Y
139   theCoef(aLowIndex+29)  = 4.0*(aSubRadius*aT31*aT33 - 
140                                 aSumRadius*(aT11*aT13 + aT21*aT23) +
141                                 aTcol4sq*aTcol1Tcol3 +
142                                 2.0*aTcol1Tcol4*aTcol3Tcol4); //X*Z
143   theCoef(aLowIndex+30)  = 4.0*(aSubRadius*aT32*aT33 -
144                                 aSumRadius*(aT12*aT13 + aT22*aT23) +
145                                 aTcol4sq*aTcol2Tcol3 +
146                                 2.0*aTcol2Tcol4*aTcol3Tcol4); //Y*Z
147
148   theCoef(aLowIndex+31)  = 4.0*(aTcol4sq*aTcol1Tcol4 +
149                                 aSubRadius*aT31*aT34 -
150                                 aSumRadius*(aT11*aT14 + aT21*aT24)); //X
151   theCoef(aLowIndex+32)  = 4.0*(aTcol4sq*aTcol2Tcol4 +
152                                 aSubRadius*aT32*aT34 -
153                                 aSumRadius*(aT12*aT14 + aT22*aT24)); //Y
154   theCoef(aLowIndex+33)  = 4.0*(aTcol4sq*aTcol3Tcol4 +
155                                 aSubRadius*aT33*aT34 -
156                                 aSumRadius*(aT13*aT14 + aT23*aT24)); //Z;
157
158   theCoef(aLowIndex+34)  = 2.0*aSubRadius*aT34*aT34 - 
159                            2.0*aSumRadius*(aT14*aT14 + aT24*aT24) +
160                            aTcol4sq*aTcol4sq + aSubRadius*aSubRadius;
161 }
162
163 void gp_Torus::Mirror (const gp_Pnt& P)
164 { pos.Mirror (P); }
165
166 gp_Torus gp_Torus::Mirrored (const gp_Pnt& P) const
167 {
168   gp_Torus C = *this;
169   C.pos.Mirror (P);
170   return C;
171 }
172
173 void gp_Torus::Mirror (const gp_Ax1& A1)
174 { pos.Mirror (A1); }
175
176 gp_Torus gp_Torus::Mirrored (const gp_Ax1& A1) const
177 {
178   gp_Torus C = *this;
179   C.pos.Mirror (A1);
180   return C;
181 }
182
183 void gp_Torus::Mirror (const gp_Ax2& A2)
184 { pos.Mirror (A2); }
185
186 gp_Torus gp_Torus::Mirrored (const gp_Ax2& A2) const
187 {
188   gp_Torus C = *this;
189   C.pos.Mirror (A2);
190   return C;
191 }
192