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