0031035: Coding - uninitialized class fields reported by Visual Studio Code Analysis
[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 : type(GProp_None)
28 {
29   GProp_PGProps Pmat(Pnts);
30   g = Pmat.CentreOfMass(); 
31   Standard_Real Xg,Yg,Zg;
32   g.Coord(Xg,Yg,Zg);
33   GProp_PrincipalProps Pp = Pmat.PrincipalProperties();
34   gp_Vec V1 = Pp.FirstAxisOfInertia();
35   Standard_Real Xv1,Yv1,Zv1;
36   V1.Coord(Xv1,Yv1,Zv1); 
37   gp_Vec V2 = Pp.SecondAxisOfInertia(); 
38   Standard_Real Xv2,Yv2,Zv2;
39   V2.Coord(Xv2,Yv2,Zv2);
40   gp_Vec V3 = Pp.ThirdAxisOfInertia(); 
41   Standard_Real Xv3,Yv3,Zv3;
42   V3.Coord(Xv3,Yv3,Zv3);
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   for (Standard_Integer i = Pnts.Lower(); i <= Pnts.Upper();i++){
52     Pnts(i).Coord(X,Y,Z);
53     D = (X-Xg)*Xv1 +(Y-Yg)*Yv1 + (Z-Zg)*Zv1;
54     if (D > Dmx1) Dmx1 = D;
55     if (D < Dmn1) Dmn1 = D;
56     D = (X-Xg)*Xv2 +(Y-Yg)*Yv2 + (Z-Zg)*Zv2;
57     if (D > Dmx2) Dmx2 = D;
58     if (D < Dmn2) Dmn2 = D;
59     D = (X-Xg)*Xv3 +(Y-Yg)*Yv3 + (Z-Zg)*Zv3;
60     if (D > Dmx3) Dmx3 = D;
61     if (D < Dmn3) Dmn3 = D;
62   }
63   Standard_Integer dimension= 3 ;
64   Standard_Integer It = 0;
65   if (Abs(Dmx1-Dmn1) <= Tol)  {
66     dimension =dimension-1;
67     It =1;
68   }
69   if (Abs(Dmx2-Dmn2) <= Tol)  {
70     dimension =dimension-1;
71     It =2*(It+1);
72   }
73   if (Abs(Dmx3-Dmn3) <= Tol)  {
74     dimension =dimension-1;
75     It = 3*(It+1);
76   }
77   switch (dimension)  {
78   case 0:
79     {
80       type = GProp_Point;
81       break;
82     }
83   case 1:
84     {
85       type = GProp_Line;
86       if (It == 4) v1 = V3;
87       else if (It == 6) v1 = V2;
88       else v1 = V1; 
89       break;
90     }
91   case 2:
92     {
93       type = GProp_Plane;
94       if (It == 1) v1 = V1;
95       else if (It == 2) v1 =V2;
96       else v1 = V3;
97       break;
98     }
99   case 3:
100     {
101       type = GProp_Space;
102       g.SetXYZ(g.XYZ() + Dmn1*V1.XYZ() + Dmn2*V2.XYZ() + Dmn3*V3.XYZ());
103       v1 = (Dmx1-Dmn1)*V1;      
104       v2 = (Dmx2-Dmn2)*V2;
105       v3 = (Dmx3-Dmn3)*V3;
106       break;
107     }
108   }
109 }
110 Standard_Boolean  GProp_PEquation::IsPlanar() const {
111
112   if (type == GProp_Plane) return Standard_True;
113   else  return Standard_False;
114 }
115
116 Standard_Boolean  GProp_PEquation::IsLinear() const {
117
118   if (type == GProp_Line) return Standard_True;
119   else  return Standard_False;
120 }
121
122 Standard_Boolean  GProp_PEquation::IsPoint() const {
123
124   if (type == GProp_Point) return Standard_True;
125   else  return Standard_False;
126 }
127
128 Standard_Boolean GProp_PEquation::IsSpace() const {
129   if (type == GProp_Space) return Standard_True;
130   else  return Standard_False;
131 }
132
133 gp_Pln  GProp_PEquation::Plane() const {
134   if (!IsPlanar()) throw Standard_NoSuchObject();
135   return gp_Pln(g,v1);
136 }
137 gp_Lin  GProp_PEquation::Line() const {
138   if (!IsLinear()) throw Standard_NoSuchObject();
139   return gp_Lin(g,gp_Dir(v1));  
140 }
141
142 gp_Pnt  GProp_PEquation::Point() const {
143   if (!IsPoint()) throw Standard_NoSuchObject();
144   return g;
145 }
146
147 void GProp_PEquation::Box(gp_Pnt& P  , gp_Vec& V1,
148                              gp_Vec& V2 , gp_Vec& V3) const {
149   if (!IsSpace()) throw Standard_NoSuchObject();
150   P = g;
151   V1 = v1;
152   V2 = v2;
153   V3 = v3;
154 }