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