1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "GEOM_WireframeFace.h"
24 #include <vtkObjectFactory.h>
26 #include <vtkPoints.h>
27 #include <vtkCellArray.h>
29 #include <vtkPolyDataMapper.h>
30 #include <vtkPolyData.h>
32 #include <Precision.hxx>
33 #include <BRepTools.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <Geom2dHatch_Hatcher.hxx>
36 #include <Geom2dHatch_Intersector.hxx>
37 #include <TColStd_Array1OfReal.hxx>
38 #include <TColStd_Array1OfInteger.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <BRep_Tool.hxx>
43 #include <Geom2d_TrimmedCurve.hxx>
44 #include <Geom2d_Line.hxx>
45 #include <gp_Dir2d.hxx>
46 #include <gp_Pnt2d.hxx>
48 #include <Geom2dHatch_Hatcher.hxx>
49 #include <HatchGen_Domain.hxx>
51 #include <Adaptor3d_HCurve.hxx>
53 vtkStandardNewMacro(GEOM_WireframeFace);
55 GEOM_WireframeFace::GEOM_WireframeFace():
62 GEOM_WireframeFace::~GEOM_WireframeFace()
70 vtkPolyData* aPolyData = GetOutput();
71 aPolyData->Allocate();
72 vtkPoints* aPts = vtkPoints::New();
73 aPolyData->SetPoints(aPts);
76 TFaceSet::Iterator anIter(myFaceSet);
77 for(; anIter.More(); anIter.Next()){
78 const TopoDS_Face& aFace = anIter.Value();
79 OCC2VTK(aFace,aPolyData,aPts,NbIso,Discret);
83 void GEOM_WireframeFace::SetNbIso(const int theNb[2])
85 if ( theNb[0] == NbIso[0] && theNb[1] == NbIso[1])
94 void GEOM_WireframeFace::GetNbIso(int &theNbU,int &theNbV)
102 OCC2VTK(const TopoDS_Face& theFace,
103 vtkPolyData* thePolyData,
105 const int theNbIso[2],
106 const int theDiscret)
108 TopoDS_Face aFace = theFace;
109 aFace.Orientation(TopAbs_FORWARD);
110 CreateIso(aFace,theNbIso,theDiscret,thePolyData,thePts);
115 CreateIso(const TopoDS_Face& theFace,
116 const int theNbIso[2],
117 const int theDiscret,
118 vtkPolyData* thePolyData,
121 // Constants for iso building
122 static Standard_Real INTERSECTOR_CONFUSION = 1.e-10 ; // -8 ;
123 static Standard_Real INTERSECTOR_TANGENCY = 1.e-10 ; // -8 ;
125 static Standard_Real HATHCER_CONFUSION_2D = 1.e-8 ;
126 static Standard_Real HATHCER_CONFUSION_3D = 1.e-8 ;
129 aHatcher(Geom2dHatch_Intersector(INTERSECTOR_CONFUSION,
130 INTERSECTOR_TANGENCY),
131 HATHCER_CONFUSION_2D,
132 HATHCER_CONFUSION_3D,
136 Standard_Real anUMin, anUMax, aVMin, aVMax;
137 TColStd_Array1OfReal anUPrm(1, theNbIso[0]), aVPrm(1, theNbIso[1]);
138 TColStd_Array1OfInteger anUInd(1, theNbIso[0]), aVInd(1, theNbIso[1]);
143 //-----------------------------------------------------------------------
144 // If the Min Max bounds are infinite, there are bounded to Infinite
146 //-----------------------------------------------------------------------
147 BRepTools::UVBounds(theFace, anUMin, anUMax, aVMin, aVMax) ;
148 Standard_Boolean InfiniteUMin = Precision::IsNegativeInfinite (anUMin) ;
149 Standard_Boolean InfiniteUMax = Precision::IsPositiveInfinite (anUMax) ;
150 Standard_Boolean InfiniteVMin = Precision::IsNegativeInfinite (aVMin) ;
151 Standard_Boolean InfiniteVMax = Precision::IsPositiveInfinite (aVMax) ;
153 static float VTKINFINITE = 1.0E38;
154 if(InfiniteUMin && InfiniteUMax){
155 anUMin = - VTKINFINITE ;
156 anUMax = VTKINFINITE ;
157 }else if(InfiniteUMin){
158 anUMin = anUMax - VTKINFINITE ;
159 }else if(InfiniteUMax){
160 anUMax = anUMin + VTKINFINITE ;
163 if(InfiniteVMin && InfiniteVMax){
164 aVMin = - VTKINFINITE ;
165 aVMax = VTKINFINITE ;
166 }else if(InfiniteVMin){
167 aVMin = aVMax - VTKINFINITE ;
168 }else if(InfiniteVMax){
169 aVMax = aVMin + VTKINFINITE ;
172 //-----------------------------------------------------------------------
173 // Retreiving the edges and loading them into the hatcher.
174 //-----------------------------------------------------------------------
175 TopExp_Explorer ExpEdges(theFace, TopAbs_EDGE);
176 for(; ExpEdges.More(); ExpEdges.Next()){
177 const TopoDS_Edge& anEdge = TopoDS::Edge(ExpEdges.Current());
178 Standard_Real U1, U2 ;
179 const Handle(Geom2d_Curve) PCurve =
180 BRep_Tool::CurveOnSurface(anEdge, theFace, U1, U2) ;
182 if(PCurve.IsNull() || U1 == U2)
185 //-- Test if a TrimmedCurve is necessary
186 if(Abs(PCurve->FirstParameter()-U1) <= Precision::PConfusion() &&
187 Abs(PCurve->LastParameter()-U2) <= Precision::PConfusion())
189 aHatcher.AddElement(PCurve, anEdge.Orientation()) ;
191 if(!PCurve->IsPeriodic()){
192 Handle(Geom2d_TrimmedCurve) TrimPCurve =
193 Handle(Geom2d_TrimmedCurve)::DownCast(PCurve);
194 if(!TrimPCurve.IsNull()){
195 Handle_Geom2d_Curve aBasisCurve = TrimPCurve->BasisCurve();
196 if(aBasisCurve->FirstParameter()-U1 > Precision::PConfusion() ||
197 U2-aBasisCurve->LastParameter() > Precision::PConfusion())
199 aHatcher.AddElement(PCurve, anEdge.Orientation()) ;
203 if(PCurve->FirstParameter()-U1 > Precision::PConfusion()){
204 U1=PCurve->FirstParameter();
206 if(U2-PCurve->LastParameter() > Precision::PConfusion()){
207 U2=PCurve->LastParameter();
211 Handle(Geom2d_TrimmedCurve) TrimPCurve =
212 new Geom2d_TrimmedCurve(PCurve, U1, U2);
213 aHatcher.AddElement(TrimPCurve, anEdge.Orientation());
218 //-----------------------------------------------------------------------
219 // Loading and trimming the hatchings.
220 //-----------------------------------------------------------------------
221 Standard_Integer IIso;
222 Standard_Real DeltaU = Abs(anUMax - anUMin) ;
223 Standard_Real DeltaV = Abs(aVMax - aVMin) ;
224 Standard_Real confusion = Min(DeltaU, DeltaV) * HATHCER_CONFUSION_3D ;
225 aHatcher.Confusion3d (confusion) ;
228 Standard_Real StepU = DeltaU / (Standard_Real)theNbIso[0];
229 if(StepU > confusion){
230 Standard_Real UPrm = anUMin + StepU / 2.;
231 gp_Dir2d Dir(0., 1.) ;
232 for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++) {
233 anUPrm(IIso) = UPrm ;
234 gp_Pnt2d Ori (UPrm, 0.) ;
235 Geom2dAdaptor_Curve HCur (new Geom2d_Line (Ori, Dir)) ;
236 anUInd(IIso) = aHatcher.AddHatching (HCur) ;
243 Standard_Real StepV = DeltaV / (Standard_Real) theNbIso[1] ;
244 if(StepV > confusion){
245 Standard_Real VPrm = aVMin + StepV / 2.;
246 gp_Dir2d Dir(1., 0.);
247 for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
249 gp_Pnt2d Ori (0., VPrm);
250 Geom2dAdaptor_Curve HCur(new Geom2d_Line (Ori, Dir));
251 aVInd(IIso) = aHatcher.AddHatching (HCur) ;
257 //-----------------------------------------------------------------------
259 //-----------------------------------------------------------------------
262 Standard_Integer aNbDom = 0 ; // for debug purpose
263 Standard_Integer Index ;
265 for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++){
266 Index = anUInd(IIso) ;
268 if(aHatcher.TrimDone(Index) && !aHatcher.TrimFailed(Index)){
269 aHatcher.ComputeDomains(Index);
270 if(aHatcher.IsDone (Index))
271 aNbDom = aHatcher.NbDomains (Index);
276 for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
279 if(aHatcher.TrimDone (Index) && !aHatcher.TrimFailed(Index)){
280 aHatcher.ComputeDomains (Index);
281 if(aHatcher.IsDone (Index))
282 aNbDom = aHatcher.NbDomains (Index);
287 //-----------------------------------------------------------------------
288 // Push iso lines in vtk kernel
289 //-----------------------------------------------------------------------
290 for(Standard_Integer UIso = anUPrm.Lower() ; UIso <= anUPrm.Upper(); UIso++){
291 Standard_Integer UInd = anUInd.Value(UIso);
293 Standard_Real UPrm = anUPrm.Value(UIso);
294 if(aHatcher.IsDone(UInd)){
295 Standard_Integer NbDom = aHatcher.NbDomains(UInd);
296 for(Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
297 const HatchGen_Domain& Dom = aHatcher.Domain(UInd, IDom) ;
298 Standard_Real V1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
299 Standard_Real V2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
300 CreateIso_(theFace, GeomAbs_IsoU, UPrm, V1, V2, theDiscret, thePolyData, thePts);
306 for(Standard_Integer VIso = aVPrm.Lower() ; VIso <= aVPrm.Upper(); VIso++){
307 Standard_Integer VInd = aVInd.Value(VIso);
309 Standard_Real VPrm = aVPrm.Value(VIso);
310 if(aHatcher.IsDone (VInd)){
311 Standard_Integer NbDom = aHatcher.NbDomains(VInd);
312 for (Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
313 const HatchGen_Domain& Dom = aHatcher.Domain(VInd, IDom);
314 Standard_Real U1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
315 Standard_Real U2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
316 CreateIso_(theFace, GeomAbs_IsoV, VPrm, U1, U2, theDiscret, thePolyData, thePts);
327 CreateIso_(const TopoDS_Face& theFace,
328 GeomAbs_IsoType theIsoType,
332 const int theDiscret,
333 vtkPolyData* thePolyData,
336 Standard_Real U1, U2, V1, V2, stepU=0., stepV=0.;
340 TopLoc_Location aLoc;
341 const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,aLoc);
344 BRepAdaptor_Surface S(theFace,Standard_False);
346 GeomAbs_SurfaceType SurfType = S.GetType();
348 GeomAbs_CurveType CurvType = GeomAbs_OtherCurve;
350 Standard_Integer Intrv, nbIntv;
351 Standard_Integer nbUIntv = S.NbUIntervals(GeomAbs_CN);
352 Standard_Integer nbVIntv = S.NbVIntervals(GeomAbs_CN);
353 TColStd_Array1OfReal TI(1,Max(nbUIntv, nbVIntv)+1);
355 if(theIsoType == GeomAbs_IsoU){
356 S.VIntervals(TI, GeomAbs_CN);
364 S.UIntervals(TI, GeomAbs_CN);
376 for(Intrv = 1; Intrv <= nbIntv; Intrv++){
377 if(TI(Intrv) <= T1 && TI(Intrv + 1) <= T1)
379 if(TI(Intrv) >= T2 && TI(Intrv + 1) >= T2)
381 if(theIsoType == GeomAbs_IsoU){
382 V1 = Max(T1, TI(Intrv));
383 V2 = Min(T2, TI(Intrv + 1));
384 stepV = (V2 - V1) / theDiscret;
386 U1 = Max(T1, TI(Intrv));
387 U2 = Min(T2, TI(Intrv + 1));
388 stepU = (U2 - U1) / theDiscret;
394 case GeomAbs_Cylinder :
396 if(theIsoType == GeomAbs_IsoV){
397 for(j = 1; j < theDiscret; j++){
401 DrawTo(P,thePolyData,thePts);
405 case GeomAbs_Sphere :
407 case GeomAbs_OffsetSurface :
408 case GeomAbs_OtherSurface :
409 for(j = 1; j < theDiscret; j++){
413 DrawTo(P,thePolyData,thePts);
416 case GeomAbs_BezierSurface :
417 case GeomAbs_BSplineSurface :
418 for(j = 1; j <= theDiscret/2; j++){
419 Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
420 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
425 case GeomAbs_SurfaceOfExtrusion :
426 case GeomAbs_SurfaceOfRevolution :
427 if((theIsoType == GeomAbs_IsoV && SurfType == GeomAbs_SurfaceOfRevolution) ||
428 (theIsoType == GeomAbs_IsoU && SurfType == GeomAbs_SurfaceOfExtrusion))
430 if(SurfType == GeomAbs_SurfaceOfExtrusion)
432 for(j = 1; j < theDiscret; j++){
436 DrawTo(P,thePolyData,thePts);
439 CurvType = (S.BasisCurve())->GetType();
443 case GeomAbs_Circle :
444 case GeomAbs_Ellipse :
445 for (j = 1; j < theDiscret; j++) {
449 DrawTo(P,thePolyData,thePts);
452 case GeomAbs_Parabola :
453 case GeomAbs_Hyperbola :
454 case GeomAbs_BezierCurve :
455 case GeomAbs_BSplineCurve :
456 case GeomAbs_OtherCurve :
457 for(j = 1; j <= theDiscret/2; j++){
458 Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
459 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
469 DrawTo(P,thePolyData,thePts);
478 CreateIso__(const BRepAdaptor_Surface& theSurface,
479 GeomAbs_IsoType theIsoType,
482 Standard_Real theStep,
483 vtkPolyData* thePolyData,
487 if (theIsoType == GeomAbs_IsoU) {
488 theSurface.D0(theU, theV, Pl);
489 theSurface.D0(theU, theV + theStep/2., Pm);
490 theSurface.D0(theU, theV + theStep, Pr);
492 theSurface.D0(theU, theV, Pl);
493 theSurface.D0(theU + theStep/2., theV, Pm);
494 theSurface.D0(theU + theStep, theV, Pr);
497 static Standard_Real ISO_RATIO = 1.001;
498 if (Pm.Distance(Pl) + Pm.Distance(Pr) <= ISO_RATIO*Pl.Distance(Pr)) {
499 DrawTo(Pr,thePolyData,thePts);
501 if (theIsoType == GeomAbs_IsoU) {
502 CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
503 Standard_Real aLocalV = theV + theStep/2 ;
504 CreateIso__(theSurface, theIsoType, theU, aLocalV , theStep/2, thePolyData, thePts);
506 CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
507 Standard_Real aLocalU = theU + theStep/2 ;
508 CreateIso__(theSurface, theIsoType, aLocalU , theV, theStep/2, thePolyData, thePts);