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