0028316: Coding Rules - Elimilate confusing aliases of Standard_Real type in V3d_View
[occt.git] / src / DsgPrs / DsgPrs_ShapeDirPresentation.cxx
CommitLineData
b311480e 1// Created on: 1995-10-06
2// Created by: Jing Cheng MEI
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
7fd59977 17
7fd59977 18#include <Bnd_Box.hxx>
42cf5bc1 19#include <BRep_Tool.hxx>
20#include <BRepBndLib.hxx>
21#include <BRepClass_Edge.hxx>
22#include <BRepClass_FaceClassifier.hxx>
23#include <BRepTools_WireExplorer.hxx>
24#include <DsgPrs_ShapeDirPresentation.hxx>
7fd59977 25#include <Geom2d_Line.hxx>
26#include <Geom_Curve.hxx>
27#include <Geom_Surface.hxx>
28#include <GeomLProp_CLProps.hxx>
29#include <GeomLProp_SLProps.hxx>
42cf5bc1 30#include <gp.hxx>
31#include <gp_Dir.hxx>
b8ddfc2f 32#include <Graphic3d_ArrayOfSegments.hxx>
42cf5bc1 33#include <Graphic3d_Group.hxx>
7fd59977 34#include <Prs3d_Arrow.hxx>
35#include <Prs3d_LineAspect.hxx>
42cf5bc1 36#include <Prs3d_Presentation.hxx>
7fd59977 37#include <TColgp_Array1OfPnt2d.hxx>
42cf5bc1 38#include <TopAbs_Orientation.hxx>
39#include <TopAbs_ShapeEnum.hxx>
40#include <TopExp_Explorer.hxx>
41#include <TopLoc_Location.hxx>
42#include <TopoDS.hxx>
43#include <TopoDS_Face.hxx>
44#include <TopoDS_Shape.hxx>
45#include <TopTools_ListOfShape.hxx>
7fd59977 46
7fd59977 47//=======================================================================
48//function : FindPointOnFace
49//purpose : internal use
50//=======================================================================
7fd59977 51static Standard_Boolean FindPointOnFace(const TopoDS_Face& face, gp_Pnt2d& pt2d)
52{
53 // discredisation of the external contour and computing the center of gravity
54
55 TopExp_Explorer wireExp;
56 wireExp.Init(face, TopAbs_WIRE);
57 if (!wireExp.More()) {
58 return Standard_False;
59 }
60
61 Standard_Integer npoints, nptt = 21;
62 TColgp_Array1OfPnt2d points(1, nptt);
63 Standard_Real area=0., xcent=0., ycent=0.;
64 TopExp_Explorer edgeExp;
65
66 for (edgeExp.Init(wireExp.Current(), TopAbs_EDGE); edgeExp.More(); edgeExp.Next()) {
67 // discretize the 2d curve
68 Standard_Real first, last;
69 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(edgeExp.Current()), face, first, last);
70 if (TopoDS::Edge(edgeExp.Current()).Orientation() == TopAbs_REVERSED) {
71 Standard_Real change = first;
72 first = last;
73 last = change;
74 }
75 if (c2d->DynamicType() == STANDARD_TYPE(Geom2d_Line)) {
76 npoints = 2;
77 c2d->D0(first, points(1));
78 c2d->D0(last, points(2));
79 }
80 else {
81 Standard_Real deltaT, t;
82 npoints = nptt;
83 deltaT = (last - first) / (nptt-1);
84 for (Standard_Integer i=1; i<=nptt; i++) {
85 if (i == 1) {
86 t = first;
87 }
88 else if (i == nptt) {
89 t = last;
90 }
91 else {
92 t = first + (i-1) * deltaT;
93 }
94 c2d->D0(t, points(i));
95 }
96 }
97
98 // compute the contribution to the center of gravity
99
100 Standard_Real h, c, d;
101 for (Standard_Integer i=1; i<=npoints-1; i++) {
102
103 h = 0.5*(points(i).Y() + points(i+1).Y());
104 c = points(i+1).X() - points(i).X();
105 d = points(i+1).X() + points(i).X();
106 area += h*c;
107 xcent += 0.5*h*c*d;
108 ycent += 0.5*h*h*c;
109 }
110 }
111
112 if (Abs(area) < gp::Resolution()) {
113 pt2d.SetCoord(points(1).X(), points(1).Y());
114 return Standard_False;
115 }
116
117 pt2d.SetCoord(xcent / area, ycent / area);
118
119 // verify that (upar vpar) is a point on the face
120
121 BRepClass_FaceClassifier fClass(face, pt2d, gp::Resolution());
6e6cd5d9 122
7fd59977 123 if ((fClass.State() == TopAbs_OUT) || (fClass.State() == TopAbs_UNKNOWN)) {
124 // try to find a point on face
125 pt2d=points(1);
126 }
127 return Standard_True;
128}
129
130
131//=======================================================================
132//function : ComputeDir
133//purpose : internal use
134//=======================================================================
135
136static Standard_Boolean ComputeDir(const TopoDS_Shape& shape, gp_Pnt& pt, gp_Dir& dir, const Standard_Integer mode)
137{
138 TopLoc_Location loc;
139 if (shape.ShapeType() == TopAbs_EDGE) {
140 Standard_Real first, last;
141 Handle(Geom_Curve) curv0 = BRep_Tool::Curve(TopoDS::Edge(shape), loc, first, last);
142 Handle(Geom_Curve) curve = Handle(Geom_Curve)::DownCast(curv0->Copy());
143 curve->Transform(loc.Transformation());
144 GeomLProp_CLProps lProps(curve, 1, gp::Resolution());
b8ddfc2f 145 lProps.SetParameter((mode == 0)? last : first);
146 if (!lProps.IsTangentDefined())
7fd59977 147 return Standard_False;
7fd59977 148 pt = lProps.Value();
149 lProps.Tangent(dir);
150 }
151 else if (shape.ShapeType() == TopAbs_FACE) {
152 gp_Pnt2d pt2d;
153 Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(shape));
154 if (BRep_Tool::NaturalRestriction(TopoDS::Face(shape))) {
155 Standard_Real u1, u2, v1, v2;
156 surface->Bounds(u1, u2, v1, v2);
157 pt2d.SetCoord((u1+u2)*0.5, (v1+v2)*0.5);
158 }
159 else {
b8ddfc2f 160 if (!FindPointOnFace(TopoDS::Face(shape), pt2d))
161 return Standard_False;
7fd59977 162 }
163
164 GeomLProp_SLProps lProps(surface, pt2d.X(), pt2d.Y(), 1, gp::Resolution());
b8ddfc2f 165 if (!lProps.IsNormalDefined())
7fd59977 166 return Standard_False;
7fd59977 167
168 pt = lProps.Value();
169 dir = lProps.Normal();
170 }
0ebaa4db 171 if (((shape.Orientation() == TopAbs_FORWARD) && (mode == 1)) ||
172 ((shape.Orientation() == TopAbs_REVERSED) && (mode == 0))) {
7fd59977 173 dir.Reverse();
174 }
175 return Standard_True;
176}
177
178
179//=======================================================================
180//function : Add
181//purpose :
182//=======================================================================
183
184void DsgPrs_ShapeDirPresentation::Add(const Handle(Prs3d_Presentation)& prs,
185 const Handle(Prs3d_Drawer)& drawer,
186 const TopoDS_Shape& shape,
187 const Standard_Integer mode)
188
189{
b8ddfc2f 190 if ((mode != 0) && (mode != 1))
7fd59977 191 return;
7fd59977 192
193 gp_Dir dir;
194 gp_Pnt pt;
195 Bnd_Box box;
196
197 if (shape.ShapeType() == TopAbs_EDGE) {
198 ComputeDir(shape, pt, dir, mode);
199 BRepBndLib::Add(shape, box);
200 }
201 else if (shape.ShapeType() == TopAbs_FACE) {
202 ComputeDir(shape, pt, dir, mode);
203 BRepBndLib::Add(shape, box);
204 }
205 else if (shape.ShapeType() == TopAbs_WIRE) {
206 TopTools_ListOfShape aList;
207 Standard_Integer nb = 0;
208 BRepTools_WireExplorer anExp;
209 for (anExp.Init(TopoDS::Wire(shape)); anExp.More(); anExp.Next()) {
210 const TopoDS_Edge& edge = anExp.Current();
211 nb++;
b8ddfc2f 212 if (nb <=3)
213 BRepBndLib::Add(edge, box);
7fd59977 214 aList.Append(edge);
215 }
216
217 if (mode == 0) {
218 const TopoDS_Edge& edge = TopoDS::Edge(aList.Last());
219 ComputeDir(edge, pt, dir, mode);
220 }
221 else {
222 const TopoDS_Edge& edge = TopoDS::Edge(aList.First());
223 ComputeDir(edge, pt, dir, mode);
224 }
225 }
226 else {
227 TopExp_Explorer faceExp;
228
229 TopTools_ListOfShape aList;
230 Standard_Integer nb = 0;
231 for (faceExp.Init(shape, TopAbs_FACE); faceExp.More(); faceExp.Next()) {
232 nb++;
233 const TopoDS_Face& face = TopoDS::Face(faceExp.Current());
234 aList.Append(face);
235 BRepBndLib::Add(face, box);
236 if (nb > 3) break;
237 }
238 const TopoDS_Face& face = TopoDS::Face(aList.Last());
239 ComputeDir(face, pt, dir, mode);
240 }
241
242 Standard_Real c[6];
243 box.Get(c[0],c[1],c[2],c[3],c[4],c[5]);
244
245 gp_Pnt ptmin(c[0], c[1], c[2]), ptmax(c[3], c[4], c[5]);
ee2be2a8 246 Standard_Real leng = ptmin.Distance(ptmax)/3.;
7fd59977 247 // mei 19/09/96 extrusion infinie -> taille fixe
248 if (leng >= 20000.) leng = 50;
249
b8ddfc2f 250 gp_Pnt pt2(pt.XYZ()+leng*dir.XYZ());
251
7fd59977 252 Prs3d_Root::CurrentGroup(prs)->SetPrimitivesAspect(drawer->LineAspect()->Aspect());
b8ddfc2f 253
254 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
255 aPrims->AddVertex(pt);
256 aPrims->AddVertex(pt2);
257 Prs3d_Root::CurrentGroup(prs)->AddPrimitiveArray(aPrims);
7fd59977 258
4ad142d9 259 Prs3d_Arrow::Draw (Prs3d_Root::CurrentGroup (prs), pt2, dir, M_PI/180.*10., leng*0.3);
7fd59977 260}