7fd59977 |
1 | //szv#4 S4163 |
2 | #include <ShapeAnalysis_Geom.ixx> |
3 | |
4 | #include <GProp_PGProps.hxx> |
5 | #include <GProp_PrincipalProps.hxx> |
6 | |
7 | #include <gp_Dir.hxx> |
8 | #include <gp_GTrsf.hxx> |
9 | #include <gp_Pln.hxx> |
10 | #include <gp_Pnt.hxx> |
11 | #include <gp_Trsf.hxx> |
12 | #include <gp_Vec.hxx> |
13 | #include <gp_XYZ.hxx> |
14 | |
15 | #include <Standard_ErrorHandler.hxx> |
16 | #include <Standard_Failure.hxx> |
17 | |
18 | //======================================================================= |
19 | //function : NearestPlane |
20 | //purpose : |
21 | //======================================================================= |
22 | |
23 | Standard_Boolean ShapeAnalysis_Geom::NearestPlane(const TColgp_Array1OfPnt& Pnts, |
24 | gp_Pln& aPln, Standard_Real& Dmax) |
25 | { |
26 | //szv#4:S4163:12Mar99 warning |
27 | GProp_PGProps Pmat(Pnts); |
28 | gp_Pnt g = Pmat.CentreOfMass(); |
29 | Standard_Real Xg,Yg,Zg; |
30 | g.Coord(Xg,Yg,Zg); |
31 | |
32 | GProp_PrincipalProps Pp = Pmat.PrincipalProperties(); |
33 | gp_Vec V1 = Pp.FirstAxisOfInertia(); |
34 | Standard_Real Xv1,Yv1,Zv1; |
35 | V1.Coord(Xv1,Yv1,Zv1); |
36 | gp_Vec V2 = Pp.SecondAxisOfInertia(); |
37 | Standard_Real Xv2,Yv2,Zv2; |
38 | V2.Coord(Xv2,Yv2,Zv2); |
39 | gp_Vec V3 = Pp.ThirdAxisOfInertia(); |
40 | Standard_Real Xv3,Yv3,Zv3; |
41 | V3.Coord(Xv3,Yv3,Zv3); |
42 | |
43 | Standard_Real D,X,Y,Z; |
44 | Standard_Real Dmx1 = RealFirst(); |
45 | Standard_Real Dmn1 = RealLast(); |
46 | Standard_Real Dmx2 = RealFirst(); |
47 | Standard_Real Dmn2 = RealLast(); |
48 | Standard_Real Dmx3 = RealFirst(); |
49 | Standard_Real Dmn3 = RealLast(); |
50 | |
51 | Standard_Integer ilow = Pnts.Lower(), iup = Pnts.Upper(); |
52 | Standard_Integer i; // svv Jan11 2000 : porting on DEC |
53 | for (i = ilow; i <= iup; i ++) { |
54 | Pnts(i).Coord(X,Y,Z); |
55 | D = (X-Xg)*Xv1 +(Y-Yg)*Yv1 + (Z-Zg)*Zv1; |
56 | if (D > Dmx1) Dmx1 = D; |
57 | if (D < Dmn1) Dmn1 = D; |
58 | D = (X-Xg)*Xv2 +(Y-Yg)*Yv2 + (Z-Zg)*Zv2; |
59 | if (D > Dmx2) Dmx2 = D; |
60 | if (D < Dmn2) Dmn2 = D; |
61 | D = (X-Xg)*Xv3 +(Y-Yg)*Yv3 + (Z-Zg)*Zv3; |
62 | if (D > Dmx3) Dmx3 = D; |
63 | if (D < Dmn3) Dmn3 = D; |
64 | } |
65 | |
66 | //szv#4:S4163:12Mar99 optimized |
67 | Standard_Real Dev1 = Dmx1-Dmn1, Dev2 = Dmx2-Dmn2, Dev3 = Dmx3-Dmn3; |
68 | Standard_Integer It = (Dev1 < Dev2)? ((Dev1 < Dev3)? 1 : 3) : ((Dev2 < Dev3)? 2 : 3); |
69 | |
70 | switch (It) { |
71 | case 1: |
72 | { |
73 | //szv#4:S4163:12Mar99 optimized |
74 | if ((2.*Dev1 > Dev2) || (2.*Dev1 > Dev3)) It = 0; |
75 | else aPln = gp_Pln(g,V1); |
76 | break; |
77 | } |
78 | case 2: |
79 | { |
80 | //szv#4:S4163:12Mar99 optimized |
81 | if ((2.*Dev2 > Dev1) || (2.*Dev2 > Dev3)) It = 0; |
82 | else aPln = gp_Pln(g,V2); |
83 | break; |
84 | } |
85 | case 3: |
86 | { |
87 | //szv#4:S4163:12Mar99 optimized |
88 | if ((2.*Dev3 > Dev2) || (2.*Dev3 > Dev1)) It = 0; |
89 | else aPln = gp_Pln(g,V3); |
90 | break; |
91 | } |
92 | } |
93 | |
94 | Dmax = RealFirst(); |
95 | if ( It != 0 ) //szv#4:S4163:12Mar99 anti-exception |
96 | for (i = ilow; i <= iup; i ++) { |
97 | D = aPln.Distance (Pnts(i)); |
98 | if (Dmax < D) Dmax = D; |
99 | } |
100 | |
101 | return (It != 0); |
102 | } |
103 | |
104 | //======================================================================= |
105 | //function : PositionTrsf |
106 | //purpose : |
107 | //======================================================================= |
108 | |
109 | Standard_Boolean ShapeAnalysis_Geom::PositionTrsf(const Handle(TColStd_HArray2OfReal)& coefs,gp_Trsf& trsf, |
110 | const Standard_Real unit,const Standard_Real prec) |
111 | { |
112 | Standard_Boolean result = Standard_True; |
113 | |
114 | trsf = gp_Trsf(); //szv#4:S4163:12Mar99 moved |
115 | |
116 | if (coefs.IsNull()) return Standard_True; //szv#4:S4163:12Mar99 moved |
117 | |
118 | gp_GTrsf gtrsf; |
119 | for (Standard_Integer i = 1; i <= 3; i ++) |
120 | for (Standard_Integer j = 1; j <= 4; j ++) |
121 | gtrsf.SetValue (i,j, coefs->Value(i,j)); |
122 | |
123 | //try { //szv#4:S4163:12Mar99 waste try |
124 | //// trsf = gtrsf.Trsf(); |
125 | // --- Prec et Unit ont ete lues suite aux StepFile_Read |
126 | // Valables pour tous les composants d un assemblage transmis |
127 | //trsf = gp_Trsf(); // Identite forcee au depart //szv#4:S4163:12Mar99 not needed |
128 | // On prend le contenu de <gtrsf>. Attention a l adressage |
129 | gp_XYZ v1 ( gtrsf.Value(1,1), gtrsf.Value(2,1), gtrsf.Value(3,1) ); |
130 | gp_XYZ v2 ( gtrsf.Value(1,2), gtrsf.Value(2,2), gtrsf.Value(3,2) ); |
131 | gp_XYZ v3 ( gtrsf.Value(1,3), gtrsf.Value(2,3), gtrsf.Value(3,3) ); |
132 | // A-t-on affaire a une similitude ? |
133 | Standard_Real m1 = v1.Modulus(); |
134 | Standard_Real m2 = v2.Modulus(); |
135 | Standard_Real m3 = v3.Modulus(); |
136 | |
137 | // D abord est-elle singuliere cette matrice ? |
138 | if (m1 < prec || m2 < prec || m3 < prec) return Standard_False; |
139 | Standard_Real mm = (m1+m2+m3)/3.; // voici la Norme moyenne, cf Scale |
140 | //szv#4:S4163:12Mar99 optimized |
141 | Standard_Real pmm = prec*mm; |
142 | if ( Abs(m1 - mm) > pmm || Abs(m2 - mm) > pmm || Abs(m3 - mm) > pmm ) |
143 | return Standard_False; |
144 | //szv#4:S4163:12Mar99 warning |
145 | v1.Divide(m1); |
146 | v2.Divide(m2); |
147 | v3.Divide(m3); |
148 | //szv#4:S4163:12Mar99 optimized |
149 | if ( Abs(v1.Dot(v2)) > prec || Abs(v2.Dot(v3)) > prec || Abs(v3.Dot(v1)) > prec ) |
150 | return Standard_False; |
151 | |
152 | // Ici, Orthogonale et memes normes. En plus on l a Normee |
153 | // On isole le cas de l Identite (tellement facile et avantageux) |
154 | if (v1.X() != 1 || v1.Y() != 0 || v1.Z() != 0 || |
155 | v2.X() != 0 || v2.Y() != 1 || v2.Z() != 0 || |
156 | v3.X() != 0 || v3.Y() != 0 || v3.Z() != 1 ) { |
157 | // Pas Identite : vraie construction depuis un Ax3 |
158 | gp_Dir d1(v1); |
159 | gp_Dir d2(v2); |
160 | gp_Dir d3(v3); |
161 | gp_Ax3 axes (gp_Pnt(0,0,0),d3,d1); |
162 | d3.Cross(d1); |
163 | if (d3.Dot(d2) < 0) axes.YReverse(); |
164 | trsf.SetTransformation(axes); |
165 | } |
166 | |
167 | // Restent les autres caracteristiques : |
168 | if ( Abs(mm - 1.) > prec ) trsf.SetScale(gp_Pnt(0,0,0), mm); //szv#4:S4163:12Mar99 optimized |
169 | gp_Vec tp (gtrsf.TranslationPart()); |
170 | if (unit != 1.) tp.Multiply(unit); |
171 | if (tp.X() != 0 || tp.Y() != 0 || tp.Z() != 0) trsf.SetTranslationPart(tp); |
172 | /* } |
173 | catch(Standard_Failure) { |
174 | trsf = gp_Trsf(); |
175 | result = Standard_False; |
176 | } */ |
177 | |
178 | return result; |
179 | } |