0024750: Replace instantiations of TCollection generic classes by NCollection templat...
[occt.git] / src / GProp / GProp_PGProps.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 #include <GProp_PGProps.ixx>
16 #include <Standard_DomainError.hxx>
17 #include <Standard_DimensionError.hxx>
18
19 #include <gp.hxx>
20 #include <gp_XYZ.hxx>
21 //#include <gp.hxx>
22
23 typedef gp_Pnt Pnt;
24 typedef gp_Mat Mat;
25 typedef gp_XYZ XYZ;
26 typedef TColgp_Array1OfPnt          Array1OfPnt;
27 typedef TColgp_Array2OfPnt          Array2OfPnt;
28 typedef TColStd_Array1OfReal Array1OfReal;
29 typedef TColStd_Array2OfReal Array2OfReal;
30
31
32
33 GProp_PGProps::GProp_PGProps ()
34 {
35   g = gp::Origin();
36   loc = gp::Origin();
37   dim = 0.0;
38 }
39
40 void GProp_PGProps::AddPoint (const Pnt& P)
41 {
42   Standard_Real Xp, Yp, Zp;
43   P.Coord (Xp, Yp, Zp);
44   Standard_Real Ixy = - Xp * Yp;
45   Standard_Real Ixz = - Xp * Zp;
46   Standard_Real Iyz = - Yp * Zp;
47
48   Standard_Real Ixx = Yp * Yp + Zp * Zp;
49   Standard_Real Iyy = Xp * Xp + Zp * Zp;
50   Standard_Real Izz = Xp * Xp + Yp * Yp;
51   Mat Mp (XYZ (Ixx, Ixy, Ixz), XYZ (Ixy, Iyy, Iyz), XYZ (Ixz, Iyz, Izz));
52   if (dim == 0) {
53     dim = 1;
54     g = P;
55     inertia = Mp;
56   }
57   else {
58     Standard_Real X, Y, Z;
59     g.Coord (X, Y, Z);
60     X = X * dim + Xp;
61     Y = Y * dim + Yp;
62     Z = Z * dim + Zp;
63     dim = dim + 1;
64     X /= dim;
65     Y /= dim;
66     Z /= dim;
67     g.SetCoord (X, Y, Z);
68     inertia = inertia + Mp;
69   }      
70 }
71
72 void GProp_PGProps::AddPoint (const gp_Pnt& P, const Standard_Real Density)
73 {
74   if (Density <= gp::Resolution())  Standard_DomainError::Raise();
75   Standard_Real Xp, Yp, Zp;
76   P.Coord (Xp, Yp, Zp);
77   Standard_Real Ixy = - Xp * Yp;
78   Standard_Real Ixz = - Xp * Zp;
79   Standard_Real Iyz = - Yp * Zp;
80   Standard_Real Ixx = Yp * Yp + Zp * Zp;
81   Standard_Real Iyy = Xp * Xp + Zp * Zp;
82   Standard_Real Izz = Xp * Xp + Yp * Yp;
83   Mat Mp (XYZ (Ixx, Ixy, Ixz), XYZ (Ixy, Iyy, Iyz), XYZ (Ixz, Iyz, Izz));
84   if (dim == 0) {
85     dim = Density;
86     g.SetXYZ (P.XYZ().Multiplied (Density));
87     inertia = Mp * Density;
88   }
89   else {
90     Standard_Real X, Y, Z;
91     g.Coord (X, Y, Z);
92     X = X * dim + Xp * Density;
93     Y = Y * dim + Yp * Density;
94     Z = Z * dim + Zp * Density;
95     dim = dim + Density;
96     X /= dim;
97     Y /= dim;
98     Z /= dim;
99     g.SetCoord (X, Y, Z);
100     inertia = inertia + Mp * Density;
101   }      
102 }
103
104 GProp_PGProps::GProp_PGProps (const Array1OfPnt& Pnts)
105 {
106   for (Standard_Integer i = Pnts.Lower(); i <= Pnts.Upper(); i++) AddPoint(Pnts(i));
107 }
108
109 GProp_PGProps::GProp_PGProps (const Array2OfPnt& Pnts)
110 {
111   for (Standard_Integer j = Pnts.LowerCol(); j <= Pnts.UpperCol(); j++)
112     {
113       for (Standard_Integer i = Pnts.LowerRow(); i <= Pnts.UpperRow(); i++) AddPoint(Pnts (i, j));
114     }      
115 }
116
117 GProp_PGProps::GProp_PGProps (const Array1OfPnt& Pnts,const Array1OfReal& Density)
118 {
119   if (Pnts.Length() != Density.Length())  Standard_DomainError::Raise();
120   Standard_Integer ip = Pnts.Lower();
121   Standard_Integer id = Density.Lower();
122   while (id <= Pnts.Upper()) {
123     Standard_Real D = Density (id);
124     if (D <= gp::Resolution()) Standard_DomainError::Raise();
125     AddPoint(Pnts (ip),D); 
126     ip++;  id++;
127   }      
128 }
129
130 GProp_PGProps::GProp_PGProps (const Array2OfPnt& Pnts,const Array2OfReal& Density)
131 {
132   if (Pnts.ColLength() != Density.ColLength() || Pnts.RowLength() != Density.RowLength()) Standard_DomainError::Raise();
133   Standard_Integer ip = Pnts.LowerRow();
134   Standard_Integer id = Density.LowerRow();
135   Standard_Integer jp = Pnts.LowerCol();
136   Standard_Integer jd = Density.LowerCol();
137   while (jp <= Pnts.UpperCol()) {
138     while (ip <= Pnts.UpperRow()) {
139       Standard_Real D = Density (id, jd);
140       if (D <= gp::Resolution())  Standard_DomainError::Raise();
141       AddPoint(Pnts (ip, jp),D); 
142       ip++; id++;
143     }      
144     jp++; jd++;
145   }
146 }
147
148
149
150 void GProp_PGProps::Barycentre(const Array1OfPnt&  Pnts,
151                                const Array1OfReal& Density,
152                                Standard_Real&      Mass, 
153                                Pnt&                G)
154 {
155   if (Pnts.Length() != Density.Length())  Standard_DimensionError::Raise();
156   Standard_Integer ip = Pnts.Lower();
157   Standard_Integer id = Density.Lower();
158   Mass = Density (id);
159   XYZ Gxyz = Pnts (ip).XYZ();
160   Gxyz.Multiply (Mass);
161   while (ip <= Pnts.Upper()) {
162     Mass = Mass + Density (id);
163     Gxyz.Add ( (Pnts(ip).XYZ()).Multiplied (Density (id)) );
164     ip++;
165     id++;
166   }
167   Gxyz.Divide (Mass);
168   G.SetXYZ (Gxyz);
169 }
170
171
172
173
174 void GProp_PGProps::Barycentre(const Array2OfPnt&  Pnts, 
175                                const Array2OfReal& Density,
176                                Standard_Real&      Mass,
177                                Pnt&                G)
178 {
179   if (Pnts.RowLength() != Density.RowLength() || Pnts.ColLength() != Density.ColLength()) Standard_DimensionError::Raise();
180   Standard_Integer ip = Pnts.LowerRow();
181   Standard_Integer id = Density.LowerRow();
182   Standard_Integer jp = Pnts.LowerCol();
183   Standard_Integer jd = Density.LowerCol();
184   Mass = 0.0;
185   XYZ Gxyz (0.0, 0.0, 0.0);
186   while (jp <= Pnts.UpperCol()) {
187     while (ip <= Pnts.UpperRow()) {
188       Mass = Mass + Density (id, jd);
189       Gxyz.Add ((Pnts(ip, jp).XYZ()).Multiplied (Density (id, jd)));
190       ip++;   id++;
191     }
192     jp++;   jd++;
193   }
194   Gxyz.Divide (Mass);
195   G.SetXYZ (Gxyz);
196 }
197
198
199
200
201 Pnt GProp_PGProps::Barycentre (const Array1OfPnt& Pnts) {
202
203   XYZ Gxyz = Pnts (Pnts.Lower()).XYZ();
204   for (Standard_Integer i = Pnts.Lower() + 1; i <= Pnts.Upper(); i++) {
205     Gxyz.Add (Pnts(i).XYZ());
206   }
207   Gxyz.Divide (Pnts.Length());
208   return Pnt (Gxyz);
209 }
210
211
212
213
214 Pnt GProp_PGProps::Barycentre (const Array2OfPnt& Pnts) {
215
216   XYZ Gxyz (0.0, 0.0, 0.0);
217   for (Standard_Integer j = Pnts.LowerCol(); j <= Pnts.UpperCol(); j++) {
218     for (Standard_Integer i = Pnts.LowerRow(); i <= Pnts.UpperRow(); i++) {
219       Gxyz.Add (Pnts(i, j).XYZ());
220     }
221   }
222   Gxyz.Divide (Pnts.RowLength() * Pnts.ColLength());
223   return Pnt (Gxyz);
224 }
225
226
227