ceb418e1 |
1 | // Created on: 2014-08-13 |
2 | // Created by: Oleg AGASHIN |
3 | // Copyright (c) 2011-2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #ifndef _BRepMesh_EdgeParameterProvider_HeaderFile |
17 | #define _BRepMesh_EdgeParameterProvider_HeaderFile |
18 | |
7bd071ed |
19 | #include <IMeshData_Types.hxx> |
20 | #include <IMeshData_Edge.hxx> |
21 | #include <IMeshData_Face.hxx> |
22 | #include <TopoDS.hxx> |
ceb418e1 |
23 | #include <Standard.hxx> |
24 | #include <Standard_DefineAlloc.hxx> |
25 | #include <Extrema_LocateExtPC.hxx> |
51aed730 |
26 | #include <BRepAdaptor_Curve.hxx> |
7bd071ed |
27 | #include <Adaptor3d_CurveOnSurface.hxx> |
c22b52d6 |
28 | #include <Geom2dAdaptor_Curve.hxx> |
ceb418e1 |
29 | |
30 | class gp_Pnt; |
31 | class TopoDS_Edge; |
32 | class TopoDS_Face; |
ceb418e1 |
33 | |
34 | //! Auxiliary class provides correct parameters |
35 | //! on curve regarding SameParameter flag. |
7bd071ed |
36 | template<class ParametersCollection> |
37 | class BRepMesh_EdgeParameterProvider : public Standard_Transient |
ceb418e1 |
38 | { |
39 | public: |
40 | |
41 | DEFINE_STANDARD_ALLOC |
42 | |
7bd071ed |
43 | //! Constructor. Initializes empty provider. |
44 | BRepMesh_EdgeParameterProvider() |
d533dafb |
45 | : myIsSameParam(Standard_False), |
46 | myFirstParam(0.0), |
47 | myOldFirstParam(0.0), |
48 | myScale(0.0), |
49 | myCurParam(0.0), |
50 | myFoundParam(0.0) |
7bd071ed |
51 | { |
52 | } |
53 | |
ceb418e1 |
54 | //! Constructor. |
55 | //! @param theEdge edge which parameters should be processed. |
56 | //! @param theFace face the parametric values are defined for. |
57 | //! @param theParameters parameters corresponded to discretization points. |
58 | BRepMesh_EdgeParameterProvider( |
7bd071ed |
59 | const IMeshData::IEdgeHandle& theEdge, |
60 | const TopAbs_Orientation theOrientation, |
61 | const IMeshData::IFaceHandle& theFace, |
62 | const ParametersCollection& theParameters) |
63 | { |
64 | Init(theEdge, theOrientation, theFace, theParameters); |
65 | } |
66 | |
67 | //! Initialized provider by the given data. |
68 | void Init ( |
69 | const IMeshData::IEdgeHandle& theEdge, |
70 | const TopAbs_Orientation theOrientation, |
71 | const IMeshData::IFaceHandle& theFace, |
72 | const ParametersCollection& theParameters) |
73 | { |
74 | myParameters = theParameters; |
75 | myIsSameParam = theEdge->GetSameParam(); |
76 | myScale = 1.; |
77 | |
78 | // Extract actual parametric values |
79 | const TopoDS_Edge aEdge = TopoDS::Edge(theEdge->GetEdge().Oriented(theOrientation)); |
80 | |
81 | myCurveAdaptor.Initialize(aEdge, theFace->GetFace()); |
82 | if (myIsSameParam) |
83 | { |
84 | return; |
85 | } |
86 | |
87 | myFirstParam = myCurveAdaptor.FirstParameter(); |
88 | const Standard_Real aLastParam = myCurveAdaptor.LastParameter(); |
89 | |
90 | myFoundParam = myCurParam = myFirstParam; |
91 | |
92 | // Extract parameters stored in polygon |
93 | myOldFirstParam = myParameters->Value(myParameters->Lower()); |
94 | const Standard_Real aOldLastParam = myParameters->Value(myParameters->Upper()); |
95 | |
96 | // Calculate scale factor between actual and stored parameters |
97 | if ((myOldFirstParam != myFirstParam || aOldLastParam != aLastParam) && |
98 | myOldFirstParam != aOldLastParam) |
99 | { |
100 | myScale = (aLastParam - myFirstParam) / (aOldLastParam - myOldFirstParam); |
101 | } |
102 | |
103 | myProjector.Initialize(myCurveAdaptor, myCurveAdaptor.FirstParameter(), |
104 | myCurveAdaptor.LastParameter(),Precision::PConfusion()); |
105 | } |
ceb418e1 |
106 | |
107 | //! Returns parameter according to SameParameter flag of the edge. |
108 | //! If SameParameter is TRUE returns value from parameters w/o changes, |
109 | //! elsewhere scales initial parameter and tries to determine resulting |
110 | //! value using projection of the corresponded 3D point on PCurve. |
111 | Standard_Real Parameter(const Standard_Integer theIndex, |
7bd071ed |
112 | const gp_Pnt& thePoint3d) const |
113 | { |
114 | if (myIsSameParam) |
115 | { |
116 | return myParameters->Value(theIndex); |
117 | } |
118 | |
119 | // Use scaled |
120 | const Standard_Real aParam = myParameters->Value(theIndex); |
121 | |
122 | const Standard_Real aPrevParam = myCurParam; |
123 | myCurParam = myFirstParam + myScale * (aParam - myOldFirstParam); |
124 | |
125 | const Standard_Real aPrevFoundParam = myFoundParam; |
126 | myFoundParam += (myCurParam - aPrevParam); |
127 | |
128 | myProjector.Perform(thePoint3d, myFoundParam); |
129 | if (myProjector.IsDone()) |
130 | { |
131 | const Standard_Real aFoundParam = myProjector.Point().Parameter(); |
132 | if ((aPrevFoundParam < myFoundParam && aPrevFoundParam < aFoundParam) || |
133 | (aPrevFoundParam > myFoundParam && aPrevFoundParam > aFoundParam)) |
134 | { |
135 | // Rude protection against case when amplified parameter goes before |
136 | // previous one due to period or other reason occurred in projector. |
137 | // Using parameter returned by projector as is can produce self-intersections. |
138 | myFoundParam = aFoundParam; |
139 | } |
140 | } |
141 | |
142 | return myFoundParam; |
143 | } |
144 | |
145 | //! Returns pcurve used to compute parameters. |
c22b52d6 |
146 | const Handle(Adaptor2d_Curve2d)& GetPCurve() const |
7bd071ed |
147 | { |
148 | return myCurveAdaptor.CurveOnSurface().GetCurve(); |
149 | } |
ceb418e1 |
150 | |
151 | private: |
152 | |
7bd071ed |
153 | ParametersCollection myParameters; |
ceb418e1 |
154 | |
155 | Standard_Boolean myIsSameParam; |
156 | Standard_Real myFirstParam; |
157 | |
158 | Standard_Real myOldFirstParam; |
159 | Standard_Real myScale; |
160 | |
7bd071ed |
161 | mutable Standard_Real myCurParam; |
162 | mutable Standard_Real myFoundParam; |
ceb418e1 |
163 | |
51aed730 |
164 | BRepAdaptor_Curve myCurveAdaptor; |
7bd071ed |
165 | |
166 | mutable Extrema_LocateExtPC myProjector; |
ceb418e1 |
167 | }; |
168 | |
169 | #endif |