Salome HOME
46e7085f8bfbf0c4aa2fcca42137dc5623dc3422
[modules/geom.git] / src / OCC2VTK / GEOM_WireframeFace.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GEOM_WireframeFace.h"
21
22 #include <GEOMUtils_Hatcher.hxx>
23  
24 #include <vtkObjectFactory.h> 
25  
26 #include <vtkPoints.h> 
27 #include <vtkCellArray.h> 
28
29 #include <vtkPolyDataMapper.h>  
30 #include <vtkPolyData.h>  
31 #include <vtkInformation.h>
32 #include <vtkInformationVector.h>
33  
34 #include <Adaptor3d_HCurve.hxx>
35 #include <BRep_Tool.hxx>
36 #include <TColStd_Array1OfReal.hxx>
37
38 vtkStandardNewMacro(GEOM_WireframeFace);
39  
40 GEOM_WireframeFace::GEOM_WireframeFace(): 
41   Discret(15)
42
43   NbIso[0] = 1;
44   NbIso[1] = 1;
45
46   this->SetNumberOfInputPorts(0);
47
48  
49 GEOM_WireframeFace::~GEOM_WireframeFace() 
50
51
52  
53 int GEOM_WireframeFace::RequestData(vtkInformation *vtkNotUsed(request),
54                                     vtkInformationVector **vtkNotUsed(inputVector),
55                                     vtkInformationVector *outputVector)
56 {
57   vtkInformation *outInfo = outputVector->GetInformationObject(0);
58   vtkPolyData *aPolyData = vtkPolyData::SafeDownCast(
59     outInfo->Get(vtkDataObject::DATA_OBJECT()));
60
61   aPolyData->Allocate();
62   vtkPoints* aPts = vtkPoints::New();
63   aPolyData->SetPoints(aPts);
64   aPts->Delete();
65
66   TFaceSet::Iterator anIter(myFaceSet);
67   for(; anIter.More(); anIter.Next()){
68     const TopoDS_Face& aFace = anIter.Value();
69     OCC2VTK(aFace,aPolyData,aPts,NbIso,Discret);
70   }
71   return 1;
72 }
73
74 void GEOM_WireframeFace::SetNbIso(const int theNb[2])
75 {
76   if ( theNb[0] == NbIso[0] && theNb[1] == NbIso[1])
77     return;
78
79   NbIso[0] = theNb[0];
80   NbIso[1] = theNb[1];
81
82   Modified();
83 }
84
85 void GEOM_WireframeFace::GetNbIso(int &theNbU,int &theNbV)
86 {
87   theNbU = NbIso[0];
88   theNbV = NbIso[1];
89 }
90
91 void  
92 GEOM_WireframeFace:: 
93 OCC2VTK(const TopoDS_Face& theFace,
94         vtkPolyData* thePolyData,
95                     vtkPoints* thePts,  
96         const int theNbIso[2], 
97         const int theDiscret) 
98
99   TopoDS_Face aFace = theFace; 
100   aFace.Orientation(TopAbs_FORWARD);
101   CreateIso(aFace,theNbIso,theDiscret,thePolyData,thePts);
102 }
103
104 void
105 GEOM_WireframeFace::
106 CreateIso(const TopoDS_Face& theFace,
107           const int theNbIso[2],
108           const int theDiscret,
109           vtkPolyData* thePolyData,
110           vtkPoints* thePts)
111 {
112   GEOMUtils::Hatcher aHatcher(theFace);
113
114   aHatcher.Init(theNbIso[0], theNbIso[1]);
115   aHatcher.Perform();
116
117   if (aHatcher.IsDone()) {
118     // Push iso lines in vtk kernel
119     CreateIso(aHatcher, Standard_True, theDiscret, thePolyData, thePts);
120     CreateIso(aHatcher, Standard_False, theDiscret, thePolyData, thePts);
121   }
122 }
123
124
125
126 void
127 GEOM_WireframeFace::
128 CreateIso(const GEOMUtils::Hatcher &theHatcher,
129           const Standard_Boolean   IsUIso,
130           const int                theDiscret,
131           vtkPolyData              *thePolyData,
132           vtkPoints                *thePts)
133 {
134   Handle(TColStd_HArray1OfInteger) anIndices;
135   Handle(TColStd_HArray1OfReal)    aParams;
136
137   if (IsUIso) {
138     // U-isolines
139     anIndices = theHatcher.GetUIndices();
140     aParams   = theHatcher.GetUParams();
141   } else {
142     // V-isolines
143     anIndices = theHatcher.GetVIndices();
144     aParams   = theHatcher.GetVParams();
145   }
146
147   if (anIndices.IsNull() == Standard_False &&
148       aParams.IsNull()   == Standard_False) {
149     const GeomAbs_IsoType aType    = (IsUIso ? GeomAbs_IsoU : GeomAbs_IsoV);
150     Standard_Integer      anIsoInd = anIndices->Lower();
151
152     for (; anIsoInd <= anIndices->Upper(); anIsoInd++) {
153       const Standard_Integer aHatchingIndex = anIndices->Value(anIsoInd);
154
155       if (aHatchingIndex != 0) {
156         const Standard_Real    aParam     = aParams->Value(anIsoInd);
157         const Standard_Integer aNbDomains =
158           theHatcher.GetNbDomains(aHatchingIndex);
159
160         if (aNbDomains >= 0) {
161           Standard_Integer anIDom = 1;
162           Standard_Real    aV1;
163           Standard_Real    aV2;
164
165           for (; anIDom <= aNbDomains; anIDom++) {
166             if (theHatcher.GetDomain(aHatchingIndex, anIDom, aV1, aV2)) {
167               CreateIso_(theHatcher.GetFace(), aType, aParam, aV1, aV2,
168                          theDiscret, thePolyData, thePts);
169             }
170           }
171         }
172       }
173     }
174   }
175 }
176
177
178
179 void 
180 GEOM_WireframeFace:: 
181 CreateIso_(const TopoDS_Face& theFace,
182            GeomAbs_IsoType theIsoType, 
183            Standard_Real Par, 
184            Standard_Real T1,
185            Standard_Real T2,
186            const int theDiscret, 
187            vtkPolyData* thePolyData,
188            vtkPoints* thePts)
189 {
190   Standard_Real U1, U2, V1, V2, stepU=0., stepV=0.;
191   Standard_Integer j;
192   gp_Pnt P;
193
194   TopLoc_Location aLoc;
195   const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,aLoc);
196
197   if(!S.IsNull()){
198     BRepAdaptor_Surface S(theFace,Standard_False);
199       
200     GeomAbs_SurfaceType SurfType = S.GetType();
201
202     GeomAbs_CurveType CurvType = GeomAbs_OtherCurve;
203
204     Standard_Integer Intrv, nbIntv;
205     Standard_Integer nbUIntv = S.NbUIntervals(GeomAbs_CN);
206     Standard_Integer nbVIntv = S.NbVIntervals(GeomAbs_CN);
207     TColStd_Array1OfReal TI(1,Max(nbUIntv, nbVIntv)+1);
208
209     if(theIsoType == GeomAbs_IsoU){
210       S.VIntervals(TI, GeomAbs_CN);
211       V1 = Max(T1, TI(1));
212       V2 = Min(T2, TI(2));
213       U1 = Par;
214       U2 = Par;
215       stepU = 0;
216       nbIntv = nbVIntv;
217     }else{
218       S.UIntervals(TI, GeomAbs_CN);
219       U1 = Max(T1, TI(1));
220       U2 = Min(T2, TI(2));
221       V1 = Par;
222       V2 = Par;
223       stepV = 0;
224       nbIntv = nbUIntv;
225     }   
226         
227     S.D0(U1,V1,P);
228     MoveTo(P,thePts);
229
230     for(Intrv = 1; Intrv <= nbIntv; Intrv++){
231       if(TI(Intrv) <= T1 && TI(Intrv + 1) <= T1)
232         continue;
233       if(TI(Intrv) >= T2 && TI(Intrv + 1) >= T2)
234               continue;
235       if(theIsoType == GeomAbs_IsoU){
236               V1 = Max(T1, TI(Intrv));
237               V2 = Min(T2, TI(Intrv + 1));
238               stepV = (V2 - V1) / theDiscret;
239       }else{
240               U1 = Max(T1, TI(Intrv));
241               U2 = Min(T2, TI(Intrv + 1));
242               stepU = (U2 - U1) / theDiscret;
243       }
244
245       switch (SurfType) {
246       case GeomAbs_Plane :
247               break;
248       case GeomAbs_Cylinder :
249       case GeomAbs_Cone :
250         if(theIsoType == GeomAbs_IsoV){
251                 for(j = 1; j < theDiscret; j++){
252                   U1 += stepU;
253                   V1 += stepV;
254                   S.D0(U1,V1,P);
255                   DrawTo(P,thePolyData,thePts);
256                 }
257               }
258               break;
259       case GeomAbs_Sphere :
260       case GeomAbs_Torus :
261       case GeomAbs_OffsetSurface :
262       case GeomAbs_OtherSurface :
263         for(j = 1; j < theDiscret; j++){
264                 U1 += stepU;
265                 V1 += stepV;
266                 S.D0(U1,V1,P);
267                 DrawTo(P,thePolyData,thePts);
268               }
269               break;
270       case GeomAbs_BezierSurface :
271       case GeomAbs_BSplineSurface :
272         for(j = 1; j <= theDiscret/2; j++){
273           Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
274                 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
275                 U1 += stepU*2.;
276                 V1 += stepV*2.;
277               }
278               break;
279       case GeomAbs_SurfaceOfExtrusion :
280       case GeomAbs_SurfaceOfRevolution :
281         if((theIsoType == GeomAbs_IsoV && SurfType == GeomAbs_SurfaceOfRevolution) ||
282                  (theIsoType == GeomAbs_IsoU && SurfType == GeomAbs_SurfaceOfExtrusion)) 
283         {
284                 if(SurfType == GeomAbs_SurfaceOfExtrusion) 
285             break;
286                 for(j = 1; j < theDiscret; j++){
287                   U1 += stepU;
288                   V1 += stepV;
289                   S.D0(U1,V1,P);
290                   DrawTo(P,thePolyData,thePts);
291                 }
292               }else{
293                 CurvType = (S.BasisCurve())->GetType();
294                 switch(CurvType){
295                 case GeomAbs_Line :
296                   break;
297                 case GeomAbs_Circle :
298                 case GeomAbs_Ellipse :
299                   for (j = 1; j < theDiscret; j++) {
300                     U1 += stepU;
301                     V1 += stepV;
302                     S.D0(U1,V1,P);
303                     DrawTo(P,thePolyData,thePts);
304                   }
305                   break;
306                 case GeomAbs_Parabola :
307                 case GeomAbs_Hyperbola :
308                 case GeomAbs_BezierCurve :
309                 case GeomAbs_BSplineCurve :
310                 case GeomAbs_OtherCurve :
311                   for(j = 1; j <= theDiscret/2; j++){
312               Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
313                   CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
314                     U1 += stepU*2.;
315                     V1 += stepV*2.;
316                   }
317                   break;
318                 }
319               }
320       }
321     }
322     S.D0(U2,V2,P);
323     DrawTo(P,thePolyData,thePts);
324   }  
325 }
326  
327  
328  
329  
330 void 
331 GEOM_WireframeFace:: 
332 CreateIso__(const BRepAdaptor_Surface& theSurface, 
333             GeomAbs_IsoType theIsoType,
334                                     Standard_Real& theU, 
335                                     Standard_Real& theV, 
336                                     Standard_Real theStep, 
337             vtkPolyData* thePolyData,
338             vtkPoints* thePts)
339 {
340   gp_Pnt Pl, Pr, Pm;
341   if (theIsoType == GeomAbs_IsoU) {
342     theSurface.D0(theU, theV, Pl);
343     theSurface.D0(theU, theV + theStep/2., Pm);
344     theSurface.D0(theU, theV + theStep, Pr);
345   } else {
346     theSurface.D0(theU, theV, Pl);
347     theSurface.D0(theU + theStep/2., theV, Pm);
348     theSurface.D0(theU + theStep, theV, Pr);
349   }
350
351   static Standard_Real ISO_RATIO = 1.001;
352   if (Pm.Distance(Pl) + Pm.Distance(Pr) <= ISO_RATIO*Pl.Distance(Pr)) {
353     DrawTo(Pr,thePolyData,thePts);
354   } else {
355     if (theIsoType == GeomAbs_IsoU) {
356       CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
357       Standard_Real aLocalV = theV + theStep/2 ;
358       CreateIso__(theSurface, theIsoType, theU, aLocalV , theStep/2, thePolyData, thePts);
359     } else {
360       CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
361       Standard_Real aLocalU = theU + theStep/2 ;
362       CreateIso__(theSurface, theIsoType, aLocalU , theV, theStep/2, thePolyData, thePts);
363     }
364   }
365 }