1 // Copyright (c) 2018 OPEN CASCADE SAS
2 // This file is part of Open CASCADE Technology software library.
4 // This library is free software; you can redistribute it and/or modify it under
5 // the terms of the GNU Lesser General Public License version 2.1 as published
6 // by the Free Software Foundation, with special exception defined in the file
7 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
8 // distribution for complete text of the license and disclaimer of any warranty.
10 // Alternatively, this file may be used under the terms of Open CASCADE
11 // commercial license or contractual agreement.
14 #include <BRepGProp_MeshCinert.hxx>
17 #include <TopoDS_Edge.hxx>
18 #include <Poly_Polygon3D.hxx>
19 #include <BRep_Tool.hxx>
21 //=======================================================================
22 //function : BRepGProp_MeshCinert
24 //=======================================================================
26 BRepGProp_MeshCinert::BRepGProp_MeshCinert()
30 //=======================================================================
31 //function : SetLocation
33 //=======================================================================
34 void BRepGProp_MeshCinert::SetLocation(const gp_Pnt& CLocation)
39 //=======================================================================
42 //=======================================================================
43 void BRepGProp_MeshCinert::Perform(const TColgp_Array1OfPnt& theNodes)
46 Standard_Real Ix, Iy, Iz, Ixx, Iyy, Izz, Ixy, Ixz, Iyz;
47 dim = Ix = Iy = Iz = Ixx = Iyy = Izz = Ixy = Ixz = Iyz = 0.0;
49 Standard_Integer Order = 2;
52 Standard_Real ur, um, u;
53 Standard_Real x, y, z;
54 Standard_Real xloc, yloc, zloc;
58 math_Vector GaussP (1, Order);
59 math_Vector GaussW (1, Order);
61 math::GaussPoints (Order,GaussP);
62 math::GaussWeights (Order,GaussW);
64 Standard_Integer nIndex = 0;
66 for(nIndex = 1; nIndex < theNodes.Length(); nIndex++)
68 const gp_XYZ& aP1 = theNodes(nIndex).XYZ();
69 const gp_XYZ& aP2 = theNodes(nIndex + 1).XYZ();
70 Standard_Real dimLocal, IxLocal, IyLocal, IzLocal, IxxLocal, IyyLocal, IzzLocal, IxyLocal, IxzLocal, IyzLocal;
71 dimLocal = IxLocal = IyLocal = IzLocal = IxxLocal = IyyLocal = IzzLocal = IxyLocal = IxzLocal = IyzLocal = 0.0;
73 loc.Coord (xloc, yloc, zloc);
77 Upper = (aP2 - aP1).Modulus();
78 if (Upper < gp::Resolution())
84 D = (aP2 - aP1) / Upper;
86 for (i = 1; i <= Order; i++) {
87 u = um + ur * GaussP (i);
98 IxyLocal += x * y * ds;
99 IyzLocal += y * z * ds;
100 IxzLocal += x * z * ds;
104 IxxLocal += (y + z) * ds;
105 IyyLocal += (x + z) * ds;
106 IzzLocal += (x + y) * ds;
132 inertia = gp_Mat (gp_XYZ (Ixx, -Ixy, -Ixz),
133 gp_XYZ (-Ixy, Iyy, -Iyz),
134 gp_XYZ (-Ixz, -Iyz, Izz));
136 if (Abs(dim) < gp::Resolution())
139 g.SetCoord (Ix/dim, Iy/dim, Iz/dim);
142 //=======================================================================
143 //function : PreparePolygon
145 //=======================================================================
146 void BRepGProp_MeshCinert::PreparePolygon(const TopoDS_Edge& theE,
147 Handle(TColgp_HArray1OfPnt)& thePolyg)
149 TopLoc_Location aLoc;
150 const Handle(Poly_Polygon3D)& aPolyg = BRep_Tool::Polygon3D(theE, aLoc);
151 if (!aPolyg.IsNull())
153 const TColgp_Array1OfPnt& aNodes = aPolyg->Nodes();
154 thePolyg = new TColgp_HArray1OfPnt(1, aNodes.Length());
156 if (aLoc.IsIdentity())
158 for (i = 1; i <= aNodes.Length(); ++i)
160 thePolyg->SetValue(i, aNodes(i));
165 const gp_Trsf& aTr = aLoc.Transformation();
166 for (i = 1; i <= aNodes.Length(); ++i)
168 thePolyg->SetValue(i, aNodes.Value(i).Transformed(aTr));
174 //Try to get PolygonOnTriangulation
175 Handle(Poly_Triangulation) aTri;
176 Handle(Poly_PolygonOnTriangulation) aPOnTri;
177 BRep_Tool::PolygonOnTriangulation(theE, aPOnTri, aTri, aLoc);
178 if (!aPOnTri.IsNull())
180 Standard_Integer aNbNodes = aPOnTri->NbNodes();
181 thePolyg = new TColgp_HArray1OfPnt(1, aNbNodes);
182 const TColStd_Array1OfInteger& aNodeInds = aPOnTri->Nodes();
183 const TColgp_Array1OfPnt& aNodes = aTri->Nodes();
185 if (aLoc.IsIdentity())
187 for (i = 1; i <= aNbNodes; ++i)
189 thePolyg->SetValue(i, aNodes(aNodeInds(i)));
194 const gp_Trsf& aTr = aLoc.Transformation();
195 for (i = 1; i <= aNbNodes; ++i)
197 thePolyg->SetValue(i, aNodes.Value(aNodeInds(i)).Transformed(aTr));
203 //Try to get Polygon2D on Surface
204 Handle(Poly_Polygon2D) aPolyg2D;
205 Handle(Geom_Surface) aS;
206 BRep_Tool::PolygonOnSurface(theE, aPolyg2D, aS, aLoc);
207 if (!aPolyg2D.IsNull())
209 Standard_Integer aNbNodes = aPolyg2D->NbNodes();
210 thePolyg = new TColgp_HArray1OfPnt(1, aNbNodes);
211 const TColgp_Array1OfPnt2d& aNodes2D = aPolyg2D->Nodes();
213 if (aLoc.IsIdentity())
215 for (i = 1; i <= aNbNodes; ++i)
217 const gp_Pnt2d& aP2d = aNodes2D(i);
218 gp_Pnt aP = aS->Value(aP2d.X(), aP2d.Y());
219 thePolyg->SetValue(i, aP);
224 const gp_Trsf& aTr = aLoc.Transformation();
225 for (i = 1; i <= aNbNodes; ++i)
227 const gp_Pnt2d& aP2d = aNodes2D(i);
228 gp_Pnt aP = aS->Value(aP2d.X(), aP2d.Y());
230 thePolyg->SetValue(i, aP);