0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / DsgPrs / DsgPrs_ShapeDirPresentation.cxx
CommitLineData
7fd59977 1
2// File: DsgPrs_ShapeDirPresentation.cxx
3// Created: Fri Oct 6 12:33:19 1995
4// Author: Jing Cheng MEI
5// <mei@junon>
6
7#include <DsgPrs_ShapeDirPresentation.ixx>
8
9#include <gp.hxx>
10#include <gp_Dir.hxx>
11#include <Bnd_Box.hxx>
12
13#include <Geom2d_Line.hxx>
14#include <Geom_Curve.hxx>
15#include <Geom_Surface.hxx>
16#include <GeomLProp_CLProps.hxx>
17#include <GeomLProp_SLProps.hxx>
18
19#include <TopoDS_Shape.hxx>
20#include <TopoDS.hxx>
21#include <TopoDS_Face.hxx>
22#include <TopLoc_Location.hxx>
23#include <TopAbs_ShapeEnum.hxx>
24#include <TopAbs_Orientation.hxx>
25#include <TopExp_Explorer.hxx>
26#include <TopTools_ListOfShape.hxx>
27#include <BRep_Tool.hxx>
28#include <BRepTools_WireExplorer.hxx>
29//#include <BRepAdaptor_Curve2d.hxx>
30#include <BRepClass_FaceClassifier.hxx>
31#include <BRepClass_Edge.hxx>
32#include <BRepBndLib.hxx>
33
34#include <Graphic3d_Group.hxx>
35#include <Graphic3d_Vertex.hxx>
36#include <Graphic3d_Array1OfVertex.hxx>
37#include <Prs3d_Arrow.hxx>
38#include <Prs3d_LineAspect.hxx>
39
40#include <Quantity_Length.hxx>
41#include <TColgp_Array1OfPnt2d.hxx>
42
43
44
45//=======================================================================
46//function : FindPointOnFace
47//purpose : internal use
48//=======================================================================
49
50static Standard_Boolean FindPointOnFace(const TopoDS_Face& face, gp_Pnt2d& pt2d)
51{
52 // discredisation of the external contour and computing the center of gravity
53
54 TopExp_Explorer wireExp;
55 wireExp.Init(face, TopAbs_WIRE);
56 if (!wireExp.More()) {
57 return Standard_False;
58 }
59
60 Standard_Integer npoints, nptt = 21;
61 TColgp_Array1OfPnt2d points(1, nptt);
62 Standard_Real area=0., xcent=0., ycent=0.;
63 TopExp_Explorer edgeExp;
64
65 for (edgeExp.Init(wireExp.Current(), TopAbs_EDGE); edgeExp.More(); edgeExp.Next()) {
66 // discretize the 2d curve
67 Standard_Real first, last;
68 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(edgeExp.Current()), face, first, last);
69 if (TopoDS::Edge(edgeExp.Current()).Orientation() == TopAbs_REVERSED) {
70 Standard_Real change = first;
71 first = last;
72 last = change;
73 }
74 if (c2d->DynamicType() == STANDARD_TYPE(Geom2d_Line)) {
75 npoints = 2;
76 c2d->D0(first, points(1));
77 c2d->D0(last, points(2));
78 }
79 else {
80 Standard_Real deltaT, t;
81 npoints = nptt;
82 deltaT = (last - first) / (nptt-1);
83 for (Standard_Integer i=1; i<=nptt; i++) {
84 if (i == 1) {
85 t = first;
86 }
87 else if (i == nptt) {
88 t = last;
89 }
90 else {
91 t = first + (i-1) * deltaT;
92 }
93 c2d->D0(t, points(i));
94 }
95 }
96
97 // compute the contribution to the center of gravity
98
99 Standard_Real h, c, d;
100 for (Standard_Integer i=1; i<=npoints-1; i++) {
101
102 h = 0.5*(points(i).Y() + points(i+1).Y());
103 c = points(i+1).X() - points(i).X();
104 d = points(i+1).X() + points(i).X();
105 area += h*c;
106 xcent += 0.5*h*c*d;
107 ycent += 0.5*h*h*c;
108 }
109 }
110
111 if (Abs(area) < gp::Resolution()) {
112 pt2d.SetCoord(points(1).X(), points(1).Y());
113 return Standard_False;
114 }
115
116 pt2d.SetCoord(xcent / area, ycent / area);
117
118 // verify that (upar vpar) is a point on the face
119
120 BRepClass_FaceClassifier fClass(face, pt2d, gp::Resolution());
6e6cd5d9 121
7fd59977 122 if ((fClass.State() == TopAbs_OUT) || (fClass.State() == TopAbs_UNKNOWN)) {
123 // try to find a point on face
124 pt2d=points(1);
125 }
126 return Standard_True;
127}
128
129
130//=======================================================================
131//function : ComputeDir
132//purpose : internal use
133//=======================================================================
134
135static Standard_Boolean ComputeDir(const TopoDS_Shape& shape, gp_Pnt& pt, gp_Dir& dir, const Standard_Integer mode)
136{
137 TopLoc_Location loc;
138 if (shape.ShapeType() == TopAbs_EDGE) {
139 Standard_Real first, last;
140 Handle(Geom_Curve) curv0 = BRep_Tool::Curve(TopoDS::Edge(shape), loc, first, last);
141 Handle(Geom_Curve) curve = Handle(Geom_Curve)::DownCast(curv0->Copy());
142 curve->Transform(loc.Transformation());
143 GeomLProp_CLProps lProps(curve, 1, gp::Resolution());
144 if (mode == 0) {
145 lProps.SetParameter(last);
146 }
147 else if (mode == 1) {
148 lProps.SetParameter(first);
149 }
150 if (!lProps.IsTangentDefined()) {
151 return Standard_False;
152 }
153 pt = lProps.Value();
154 lProps.Tangent(dir);
155 }
156 else if (shape.ShapeType() == TopAbs_FACE) {
157 gp_Pnt2d pt2d;
158 Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(shape));
159 if (BRep_Tool::NaturalRestriction(TopoDS::Face(shape))) {
160 Standard_Real u1, u2, v1, v2;
161 surface->Bounds(u1, u2, v1, v2);
162 pt2d.SetCoord((u1+u2)*0.5, (v1+v2)*0.5);
163 }
164 else {
165 Standard_Boolean found = FindPointOnFace(TopoDS::Face(shape), pt2d);
166 if (!found) {
167 return Standard_False;
168 }
169 }
170
171 GeomLProp_SLProps lProps(surface, pt2d.X(), pt2d.Y(), 1, gp::Resolution());
172 if (!lProps.IsNormalDefined()) {
173 return Standard_False;
174 }
175
176 pt = lProps.Value();
177 dir = lProps.Normal();
178 }
179 if ((shape.Orientation() == TopAbs_FORWARD) && (mode == 1) ||
180 (shape.Orientation() == TopAbs_REVERSED) && (mode == 0)) {
181 dir.Reverse();
182 }
183 return Standard_True;
184}
185
186
187//=======================================================================
188//function : Add
189//purpose :
190//=======================================================================
191
192void DsgPrs_ShapeDirPresentation::Add(const Handle(Prs3d_Presentation)& prs,
193 const Handle(Prs3d_Drawer)& drawer,
194 const TopoDS_Shape& shape,
195 const Standard_Integer mode)
196
197{
198 if ((mode != 0) && (mode != 1)) {
199 return;
200 }
201
202 gp_Dir dir;
203 gp_Pnt pt;
204 Bnd_Box box;
205
206 if (shape.ShapeType() == TopAbs_EDGE) {
207 ComputeDir(shape, pt, dir, mode);
208 BRepBndLib::Add(shape, box);
209 }
210 else if (shape.ShapeType() == TopAbs_FACE) {
211 ComputeDir(shape, pt, dir, mode);
212 BRepBndLib::Add(shape, box);
213 }
214 else if (shape.ShapeType() == TopAbs_WIRE) {
215 TopTools_ListOfShape aList;
216 Standard_Integer nb = 0;
217 BRepTools_WireExplorer anExp;
218 for (anExp.Init(TopoDS::Wire(shape)); anExp.More(); anExp.Next()) {
219 const TopoDS_Edge& edge = anExp.Current();
220 nb++;
221 if (nb <=3) {
222 BRepBndLib::Add(edge, box);
223 }
224 aList.Append(edge);
225 }
226
227 if (mode == 0) {
228 const TopoDS_Edge& edge = TopoDS::Edge(aList.Last());
229 ComputeDir(edge, pt, dir, mode);
230 }
231 else {
232 const TopoDS_Edge& edge = TopoDS::Edge(aList.First());
233 ComputeDir(edge, pt, dir, mode);
234 }
235 }
236 else {
237 TopExp_Explorer faceExp;
238
239 TopTools_ListOfShape aList;
240 Standard_Integer nb = 0;
241 for (faceExp.Init(shape, TopAbs_FACE); faceExp.More(); faceExp.Next()) {
242 nb++;
243 const TopoDS_Face& face = TopoDS::Face(faceExp.Current());
244 aList.Append(face);
245 BRepBndLib::Add(face, box);
246 if (nb > 3) break;
247 }
248 const TopoDS_Face& face = TopoDS::Face(aList.Last());
249 ComputeDir(face, pt, dir, mode);
250 }
251
252 Standard_Real c[6];
253 box.Get(c[0],c[1],c[2],c[3],c[4],c[5]);
254
255 gp_Pnt ptmin(c[0], c[1], c[2]), ptmax(c[3], c[4], c[5]);
256 Quantity_Length leng = ptmin.Distance(ptmax)/3.;
257 // mei 19/09/96 extrusion infinie -> taille fixe
258 if (leng >= 20000.) leng = 50;
259
260 gp_Pnt pt2(pt.X()+leng*dir.X(), pt.Y()+leng*dir.Y(), pt.Z()+leng*dir.Z());
261 Graphic3d_Array1OfVertex line(1,2);
262 line(1).SetCoord(pt.X(), pt.Y(), pt.Z());
263 line(2).SetCoord(pt2.X(), pt2.Y(), pt2.Z());
264
265 Prs3d_Root::CurrentGroup(prs)->SetPrimitivesAspect(drawer->LineAspect()->Aspect());
266 Prs3d_Root::CurrentGroup(prs)->Polyline(line);
267
c6541a0c 268 Prs3d_Arrow::Draw(prs, pt2, dir, M_PI/180.*10., leng*0.3);
7fd59977 269}
270
271