1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GEOM_WireframeFace.h"
22 #include <vtkObjectFactory.h>
24 #include <vtkPoints.h>
25 #include <vtkCellArray.h>
27 #include <vtkPolyDataMapper.h>
28 #include <vtkPolyData.h>
30 #include <Precision.hxx>
31 #include <BRepTools.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <Geom2dHatch_Hatcher.hxx>
34 #include <Geom2dHatch_Intersector.hxx>
35 #include <TColStd_Array1OfReal.hxx>
36 #include <TColStd_Array1OfInteger.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <BRep_Tool.hxx>
41 #include <Geom2d_TrimmedCurve.hxx>
42 #include <Geom2d_Line.hxx>
43 #include <gp_Dir2d.hxx>
44 #include <gp_Pnt2d.hxx>
46 #include <Geom2dHatch_Hatcher.hxx>
47 #include <HatchGen_Domain.hxx>
49 #include <Adaptor3d_HCurve.hxx>
51 vtkStandardNewMacro(GEOM_WireframeFace);
53 GEOM_WireframeFace::GEOM_WireframeFace():
60 GEOM_WireframeFace::~GEOM_WireframeFace()
68 vtkPolyData* aPolyData = GetOutput();
69 aPolyData->Allocate();
70 vtkPoints* aPts = vtkPoints::New();
71 aPolyData->SetPoints(aPts);
74 TFaceSet::Iterator anIter(myFaceSet);
75 for(; anIter.More(); anIter.Next()){
76 const TopoDS_Face& aFace = anIter.Value();
77 OCC2VTK(aFace,aPolyData,aPts,NbIso,Discret);
81 void GEOM_WireframeFace::SetNbIso(const int theNb[2])
83 if ( theNb[0] == NbIso[0] && theNb[1] == NbIso[1])
92 void GEOM_WireframeFace::GetNbIso(int &theNbU,int &theNbV)
100 OCC2VTK(const TopoDS_Face& theFace,
101 vtkPolyData* thePolyData,
103 const int theNbIso[2],
104 const int theDiscret)
106 TopoDS_Face aFace = theFace;
107 aFace.Orientation(TopAbs_FORWARD);
108 CreateIso(aFace,theNbIso,theDiscret,thePolyData,thePts);
113 CreateIso(const TopoDS_Face& theFace,
114 const int theNbIso[2],
115 const int theDiscret,
116 vtkPolyData* thePolyData,
119 // Constants for iso building
120 static Standard_Real INTERSECTOR_CONFUSION = 1.e-10 ; // -8 ;
121 static Standard_Real INTERSECTOR_TANGENCY = 1.e-10 ; // -8 ;
123 static Standard_Real HATHCER_CONFUSION_2D = 1.e-8 ;
124 static Standard_Real HATHCER_CONFUSION_3D = 1.e-8 ;
127 aHatcher(Geom2dHatch_Intersector(INTERSECTOR_CONFUSION,
128 INTERSECTOR_TANGENCY),
129 HATHCER_CONFUSION_2D,
130 HATHCER_CONFUSION_3D,
134 Standard_Real anUMin, anUMax, aVMin, aVMax;
135 TColStd_Array1OfReal anUPrm(1, theNbIso[0]), aVPrm(1, theNbIso[1]);
136 TColStd_Array1OfInteger anUInd(1, theNbIso[0]), aVInd(1, theNbIso[1]);
141 //-----------------------------------------------------------------------
142 // If the Min Max bounds are infinite, there are bounded to Infinite
144 //-----------------------------------------------------------------------
145 BRepTools::UVBounds(theFace, anUMin, anUMax, aVMin, aVMax) ;
146 Standard_Boolean InfiniteUMin = Precision::IsNegativeInfinite (anUMin) ;
147 Standard_Boolean InfiniteUMax = Precision::IsPositiveInfinite (anUMax) ;
148 Standard_Boolean InfiniteVMin = Precision::IsNegativeInfinite (aVMin) ;
149 Standard_Boolean InfiniteVMax = Precision::IsPositiveInfinite (aVMax) ;
151 static float VTKINFINITE = 1.0E38;
152 if(InfiniteUMin && InfiniteUMax){
153 anUMin = - VTKINFINITE ;
154 anUMax = VTKINFINITE ;
155 }else if(InfiniteUMin){
156 anUMin = anUMax - VTKINFINITE ;
157 }else if(InfiniteUMax){
158 anUMax = anUMin + VTKINFINITE ;
161 if(InfiniteVMin && InfiniteVMax){
162 aVMin = - VTKINFINITE ;
163 aVMax = VTKINFINITE ;
164 }else if(InfiniteVMin){
165 aVMin = aVMax - VTKINFINITE ;
166 }else if(InfiniteVMax){
167 aVMax = aVMin + VTKINFINITE ;
170 //-----------------------------------------------------------------------
171 // Retreiving the edges and loading them into the hatcher.
172 //-----------------------------------------------------------------------
173 TopExp_Explorer ExpEdges(theFace, TopAbs_EDGE);
174 for(; ExpEdges.More(); ExpEdges.Next()){
175 const TopoDS_Edge& anEdge = TopoDS::Edge(ExpEdges.Current());
176 Standard_Real U1, U2 ;
177 const Handle(Geom2d_Curve) PCurve =
178 BRep_Tool::CurveOnSurface(anEdge, theFace, U1, U2) ;
180 if(PCurve.IsNull() || U1 == U2)
183 //-- Test if a TrimmedCurve is necessary
184 if(Abs(PCurve->FirstParameter()-U1) <= Precision::PConfusion() &&
185 Abs(PCurve->LastParameter()-U2) <= Precision::PConfusion())
187 aHatcher.AddElement(PCurve, anEdge.Orientation()) ;
189 if(!PCurve->IsPeriodic()){
190 Handle(Geom2d_TrimmedCurve) TrimPCurve =
191 Handle(Geom2d_TrimmedCurve)::DownCast(PCurve);
192 if(!TrimPCurve.IsNull()){
193 Handle_Geom2d_Curve aBasisCurve = TrimPCurve->BasisCurve();
194 if(aBasisCurve->FirstParameter()-U1 > Precision::PConfusion() ||
195 U2-aBasisCurve->LastParameter() > Precision::PConfusion())
197 aHatcher.AddElement(PCurve, anEdge.Orientation()) ;
201 if(PCurve->FirstParameter()-U1 > Precision::PConfusion()){
202 U1=PCurve->FirstParameter();
204 if(U2-PCurve->LastParameter() > Precision::PConfusion()){
205 U2=PCurve->LastParameter();
209 Handle(Geom2d_TrimmedCurve) TrimPCurve =
210 new Geom2d_TrimmedCurve(PCurve, U1, U2);
211 aHatcher.AddElement(TrimPCurve, anEdge.Orientation());
216 //-----------------------------------------------------------------------
217 // Loading and trimming the hatchings.
218 //-----------------------------------------------------------------------
219 Standard_Integer IIso;
220 Standard_Real DeltaU = Abs(anUMax - anUMin) ;
221 Standard_Real DeltaV = Abs(aVMax - aVMin) ;
222 Standard_Real confusion = Min(DeltaU, DeltaV) * HATHCER_CONFUSION_3D ;
223 aHatcher.Confusion3d (confusion) ;
226 Standard_Real StepU = DeltaU / (Standard_Real)theNbIso[0];
227 if(StepU > confusion){
228 Standard_Real UPrm = anUMin + StepU / 2.;
229 gp_Dir2d Dir(0., 1.) ;
230 for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++) {
231 anUPrm(IIso) = UPrm ;
232 gp_Pnt2d Ori (UPrm, 0.) ;
233 Geom2dAdaptor_Curve HCur (new Geom2d_Line (Ori, Dir)) ;
234 anUInd(IIso) = aHatcher.AddHatching (HCur) ;
241 Standard_Real StepV = DeltaV / (Standard_Real) theNbIso[1] ;
242 if(StepV > confusion){
243 Standard_Real VPrm = aVMin + StepV / 2.;
244 gp_Dir2d Dir(1., 0.);
245 for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
247 gp_Pnt2d Ori (0., VPrm);
248 Geom2dAdaptor_Curve HCur(new Geom2d_Line (Ori, Dir));
249 aVInd(IIso) = aHatcher.AddHatching (HCur) ;
255 //-----------------------------------------------------------------------
257 //-----------------------------------------------------------------------
260 Standard_Integer aNbDom = 0 ; // for debug purpose
261 Standard_Integer Index ;
263 for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++){
264 Index = anUInd(IIso) ;
266 if(aHatcher.TrimDone(Index) && !aHatcher.TrimFailed(Index)){
267 aHatcher.ComputeDomains(Index);
268 if(aHatcher.IsDone (Index))
269 aNbDom = aHatcher.NbDomains (Index);
274 for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
277 if(aHatcher.TrimDone (Index) && !aHatcher.TrimFailed(Index)){
278 aHatcher.ComputeDomains (Index);
279 if(aHatcher.IsDone (Index))
280 aNbDom = aHatcher.NbDomains (Index);
285 //-----------------------------------------------------------------------
286 // Push iso lines in vtk kernel
287 //-----------------------------------------------------------------------
288 for(Standard_Integer UIso = anUPrm.Lower() ; UIso <= anUPrm.Upper(); UIso++){
289 Standard_Integer UInd = anUInd.Value(UIso);
291 Standard_Real UPrm = anUPrm.Value(UIso);
292 if(aHatcher.IsDone(UInd)){
293 Standard_Integer NbDom = aHatcher.NbDomains(UInd);
294 for(Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
295 const HatchGen_Domain& Dom = aHatcher.Domain(UInd, IDom) ;
296 Standard_Real V1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
297 Standard_Real V2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
298 CreateIso_(theFace, GeomAbs_IsoU, UPrm, V1, V2, theDiscret, thePolyData, thePts);
304 for(Standard_Integer VIso = aVPrm.Lower() ; VIso <= aVPrm.Upper(); VIso++){
305 Standard_Integer VInd = aVInd.Value(VIso);
307 Standard_Real VPrm = aVPrm.Value(VIso);
308 if(aHatcher.IsDone (VInd)){
309 Standard_Integer NbDom = aHatcher.NbDomains(VInd);
310 for (Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
311 const HatchGen_Domain& Dom = aHatcher.Domain(VInd, IDom);
312 Standard_Real U1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
313 Standard_Real U2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
314 CreateIso_(theFace, GeomAbs_IsoV, VPrm, U1, U2, theDiscret, thePolyData, thePts);
325 CreateIso_(const TopoDS_Face& theFace,
326 GeomAbs_IsoType theIsoType,
330 const int theDiscret,
331 vtkPolyData* thePolyData,
334 Standard_Real U1, U2, V1, V2, stepU=0., stepV=0.;
338 TopLoc_Location aLoc;
339 const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,aLoc);
342 BRepAdaptor_Surface S(theFace,Standard_False);
344 GeomAbs_SurfaceType SurfType = S.GetType();
346 GeomAbs_CurveType CurvType = GeomAbs_OtherCurve;
348 Standard_Integer Intrv, nbIntv;
349 Standard_Integer nbUIntv = S.NbUIntervals(GeomAbs_CN);
350 Standard_Integer nbVIntv = S.NbVIntervals(GeomAbs_CN);
351 TColStd_Array1OfReal TI(1,Max(nbUIntv, nbVIntv)+1);
353 if(theIsoType == GeomAbs_IsoU){
354 S.VIntervals(TI, GeomAbs_CN);
362 S.UIntervals(TI, GeomAbs_CN);
374 for(Intrv = 1; Intrv <= nbIntv; Intrv++){
375 if(TI(Intrv) <= T1 && TI(Intrv + 1) <= T1)
377 if(TI(Intrv) >= T2 && TI(Intrv + 1) >= T2)
379 if(theIsoType == GeomAbs_IsoU){
380 V1 = Max(T1, TI(Intrv));
381 V2 = Min(T2, TI(Intrv + 1));
382 stepV = (V2 - V1) / theDiscret;
384 U1 = Max(T1, TI(Intrv));
385 U2 = Min(T2, TI(Intrv + 1));
386 stepU = (U2 - U1) / theDiscret;
392 case GeomAbs_Cylinder :
394 if(theIsoType == GeomAbs_IsoV){
395 for(j = 1; j < theDiscret; j++){
399 DrawTo(P,thePolyData,thePts);
403 case GeomAbs_Sphere :
405 case GeomAbs_OffsetSurface :
406 case GeomAbs_OtherSurface :
407 for(j = 1; j < theDiscret; j++){
411 DrawTo(P,thePolyData,thePts);
414 case GeomAbs_BezierSurface :
415 case GeomAbs_BSplineSurface :
416 for(j = 1; j <= theDiscret/2; j++){
417 Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
418 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
423 case GeomAbs_SurfaceOfExtrusion :
424 case GeomAbs_SurfaceOfRevolution :
425 if((theIsoType == GeomAbs_IsoV && SurfType == GeomAbs_SurfaceOfRevolution) ||
426 (theIsoType == GeomAbs_IsoU && SurfType == GeomAbs_SurfaceOfExtrusion))
428 if(SurfType == GeomAbs_SurfaceOfExtrusion)
430 for(j = 1; j < theDiscret; j++){
434 DrawTo(P,thePolyData,thePts);
437 CurvType = (S.BasisCurve())->GetType();
441 case GeomAbs_Circle :
442 case GeomAbs_Ellipse :
443 for (j = 1; j < theDiscret; j++) {
447 DrawTo(P,thePolyData,thePts);
450 case GeomAbs_Parabola :
451 case GeomAbs_Hyperbola :
452 case GeomAbs_BezierCurve :
453 case GeomAbs_BSplineCurve :
454 case GeomAbs_OtherCurve :
455 for(j = 1; j <= theDiscret/2; j++){
456 Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
457 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
467 DrawTo(P,thePolyData,thePts);
476 CreateIso__(const BRepAdaptor_Surface& theSurface,
477 GeomAbs_IsoType theIsoType,
480 Standard_Real theStep,
481 vtkPolyData* thePolyData,
485 if (theIsoType == GeomAbs_IsoU) {
486 theSurface.D0(theU, theV, Pl);
487 theSurface.D0(theU, theV + theStep/2., Pm);
488 theSurface.D0(theU, theV + theStep, Pr);
490 theSurface.D0(theU, theV, Pl);
491 theSurface.D0(theU + theStep/2., theV, Pm);
492 theSurface.D0(theU + theStep, theV, Pr);
495 static Standard_Real ISO_RATIO = 1.001;
496 if (Pm.Distance(Pl) + Pm.Distance(Pr) <= ISO_RATIO*Pl.Distance(Pr)) {
497 DrawTo(Pr,thePolyData,thePts);
499 if (theIsoType == GeomAbs_IsoU) {
500 CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
501 Standard_Real aLocalV = theV + theStep/2 ;
502 CreateIso__(theSurface, theIsoType, theU, aLocalV , theStep/2, thePolyData, thePts);
504 CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
505 Standard_Real aLocalU = theU + theStep/2 ;
506 CreateIso__(theSurface, theIsoType, aLocalU , theV, theStep/2, thePolyData, thePts);