0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / GProp / GProp_PEquation.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
16 #include <gp_Lin.hxx>
17 #include <gp_Pln.hxx>
18 #include <gp_Pnt.hxx>
19 #include <gp_Vec.hxx>
20 #include <GProp_PEquation.hxx>
21 #include <GProp_PGProps.hxx>
22 #include <GProp_PrincipalProps.hxx>
23 #include <Standard_NoSuchObject.hxx>
24
25 GProp_PEquation::GProp_PEquation(const TColgp_Array1OfPnt& Pnts, 
26                                        const Standard_Real Tol) 
27 {
28   GProp_PGProps Pmat(Pnts);
29   g = Pmat.CentreOfMass(); 
30   Standard_Real Xg,Yg,Zg;
31   g.Coord(Xg,Yg,Zg);
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   Standard_Real D,X,Y,Z;
43   Standard_Real Dmx1 = RealFirst();
44   Standard_Real Dmn1 = RealLast();
45   Standard_Real Dmx2 = RealFirst();
46   Standard_Real Dmn2 = RealLast();
47   Standard_Real Dmx3 = RealFirst();
48   Standard_Real Dmn3 = RealLast();
49
50   for (Standard_Integer i = Pnts.Lower(); i <= Pnts.Upper();i++){
51     Pnts(i).Coord(X,Y,Z);
52     D = (X-Xg)*Xv1 +(Y-Yg)*Yv1 + (Z-Zg)*Zv1;
53     if (D > Dmx1) Dmx1 = D;
54     if (D < Dmn1) Dmn1 = D;
55     D = (X-Xg)*Xv2 +(Y-Yg)*Yv2 + (Z-Zg)*Zv2;
56     if (D > Dmx2) Dmx2 = D;
57     if (D < Dmn2) Dmn2 = D;
58     D = (X-Xg)*Xv3 +(Y-Yg)*Yv3 + (Z-Zg)*Zv3;
59     if (D > Dmx3) Dmx3 = D;
60     if (D < Dmn3) Dmn3 = D;
61   }
62   Standard_Integer dimension= 3 ;
63   Standard_Integer It = 0;
64   if (Abs(Dmx1-Dmn1) <= Tol)  {
65     dimension =dimension-1;
66     It =1;
67   }
68   if (Abs(Dmx2-Dmn2) <= Tol)  {
69     dimension =dimension-1;
70     It =2*(It+1);
71   }
72   if (Abs(Dmx3-Dmn3) <= Tol)  {
73     dimension =dimension-1;
74     It = 3*(It+1);
75   }
76   switch (dimension)  {
77   case 0:
78     {
79       type = GProp_Point;
80       break;
81     }
82   case 1:
83     {
84       type = GProp_Line;
85       if (It == 4) v1 = V3;
86       else if (It == 6) v1 = V2;
87       else v1 = V1; 
88       break;
89     }
90   case 2:
91     {
92       type = GProp_Plane;
93       if (It == 1) v1 = V1;
94       else if (It == 2) v1 =V2;
95       else v1 = V3;
96       break;
97     }
98   case 3:
99     {
100       type = GProp_Space;
101       g.SetXYZ(g.XYZ() + Dmn1*V1.XYZ() + Dmn2*V2.XYZ() + Dmn3*V3.XYZ());
102       v1 = (Dmx1-Dmn1)*V1;      
103       v2 = (Dmx2-Dmn2)*V2;
104       v3 = (Dmx3-Dmn3)*V3;
105       break;
106     }
107   }
108 }
109 Standard_Boolean  GProp_PEquation::IsPlanar() const {
110
111   if (type == GProp_Plane) return Standard_True;
112   else  return Standard_False;
113 }
114
115 Standard_Boolean  GProp_PEquation::IsLinear() const {
116
117   if (type == GProp_Line) return Standard_True;
118   else  return Standard_False;
119 }
120
121 Standard_Boolean  GProp_PEquation::IsPoint() const {
122
123   if (type == GProp_Point) return Standard_True;
124   else  return Standard_False;
125 }
126
127 Standard_Boolean GProp_PEquation::IsSpace() const {
128   if (type == GProp_Space) return Standard_True;
129   else  return Standard_False;
130 }
131
132 gp_Pln  GProp_PEquation::Plane() const {
133   if (!IsPlanar()) throw Standard_NoSuchObject();
134   return gp_Pln(g,v1);
135 }
136 gp_Lin  GProp_PEquation::Line() const {
137   if (!IsLinear()) throw Standard_NoSuchObject();
138   return gp_Lin(g,gp_Dir(v1));  
139 }
140
141 gp_Pnt  GProp_PEquation::Point() const {
142   if (!IsPoint()) throw Standard_NoSuchObject();
143   return g;
144 }
145
146 void GProp_PEquation::Box(gp_Pnt& P  , gp_Vec& V1,
147                              gp_Vec& V2 , gp_Vec& V3) const {
148   if (!IsSpace()) throw Standard_NoSuchObject();
149   P = g;
150   V1 = v1;
151   V2 = v2;
152   V3 = v3;
153 }