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) ;
227 Standard_Real StepU = DeltaU / (Standard_Real)theNbIso[0];
228 if(StepU > confusion){
229 Standard_Real UPrm = anUMin + StepU / 2.;
230 gp_Dir2d Dir(0., 1.) ;
231 for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++) {
232 anUPrm(IIso) = UPrm ;
233 gp_Pnt2d Ori (UPrm, 0.) ;
234 Geom2dAdaptor_Curve HCur (new Geom2d_Line (Ori, Dir)) ;
235 anUInd(IIso) = aHatcher.AddHatching (HCur) ;
240 Standard_Real StepV = DeltaV / (Standard_Real) theNbIso[1] ;
241 if(StepV > confusion){
242 Standard_Real VPrm = aVMin + StepV / 2.;
243 gp_Dir2d Dir(1., 0.);
244 for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
246 gp_Pnt2d Ori (0., VPrm);
247 Geom2dAdaptor_Curve HCur(new Geom2d_Line (Ori, Dir));
248 aVInd(IIso) = aHatcher.AddHatching (HCur) ;
253 //-----------------------------------------------------------------------
255 //-----------------------------------------------------------------------
258 Standard_Integer aNbDom = 0 ; // for debug purpose
259 Standard_Integer Index ;
261 for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++){
262 Index = anUInd(IIso) ;
264 if(aHatcher.TrimDone(Index) && !aHatcher.TrimFailed(Index)){
265 aHatcher.ComputeDomains(Index);
266 if(aHatcher.IsDone (Index))
267 aNbDom = aHatcher.NbDomains (Index);
272 for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
275 if(aHatcher.TrimDone (Index) && !aHatcher.TrimFailed(Index)){
276 aHatcher.ComputeDomains (Index);
277 if(aHatcher.IsDone (Index))
278 aNbDom = aHatcher.NbDomains (Index);
283 //-----------------------------------------------------------------------
284 // Push iso lines in vtk kernel
285 //-----------------------------------------------------------------------
286 for(Standard_Integer UIso = anUPrm.Lower() ; UIso <= anUPrm.Upper(); UIso++){
287 Standard_Integer UInd = anUInd.Value(UIso);
289 Standard_Real UPrm = anUPrm.Value(UIso);
290 if(aHatcher.IsDone(UInd)){
291 Standard_Integer NbDom = aHatcher.NbDomains(UInd);
292 for(Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
293 const HatchGen_Domain& Dom = aHatcher.Domain(UInd, IDom) ;
294 Standard_Real V1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
295 Standard_Real V2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
296 CreateIso_(theFace, GeomAbs_IsoU, UPrm, V1, V2, theDiscret, thePolyData, thePts);
302 for(Standard_Integer VIso = aVPrm.Lower() ; VIso <= aVPrm.Upper(); VIso++){
303 Standard_Integer VInd = aVInd.Value(VIso);
305 Standard_Real VPrm = aVPrm.Value(VIso);
306 if(aHatcher.IsDone (VInd)){
307 Standard_Integer NbDom = aHatcher.NbDomains(VInd);
308 for (Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
309 const HatchGen_Domain& Dom = aHatcher.Domain(VInd, IDom);
310 Standard_Real U1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
311 Standard_Real U2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
312 CreateIso_(theFace, GeomAbs_IsoV, VPrm, U1, U2, theDiscret, thePolyData, thePts);
323 CreateIso_(const TopoDS_Face& theFace,
324 GeomAbs_IsoType theIsoType,
328 const int theDiscret,
329 vtkPolyData* thePolyData,
332 Standard_Real U1, U2, V1, V2, stepU=0., stepV=0.;
336 TopLoc_Location aLoc;
337 const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,aLoc);
340 BRepAdaptor_Surface S(theFace,Standard_False);
342 GeomAbs_SurfaceType SurfType = S.GetType();
344 GeomAbs_CurveType CurvType = GeomAbs_OtherCurve;
346 Standard_Integer Intrv, nbIntv;
347 Standard_Integer nbUIntv = S.NbUIntervals(GeomAbs_CN);
348 Standard_Integer nbVIntv = S.NbVIntervals(GeomAbs_CN);
349 TColStd_Array1OfReal TI(1,Max(nbUIntv, nbVIntv)+1);
351 if(theIsoType == GeomAbs_IsoU){
352 S.VIntervals(TI, GeomAbs_CN);
360 S.UIntervals(TI, GeomAbs_CN);
372 for(Intrv = 1; Intrv <= nbIntv; Intrv++){
373 if(TI(Intrv) <= T1 && TI(Intrv + 1) <= T1)
375 if(TI(Intrv) >= T2 && TI(Intrv + 1) >= T2)
377 if(theIsoType == GeomAbs_IsoU){
378 V1 = Max(T1, TI(Intrv));
379 V2 = Min(T2, TI(Intrv + 1));
380 stepV = (V2 - V1) / theDiscret;
382 U1 = Max(T1, TI(Intrv));
383 U2 = Min(T2, TI(Intrv + 1));
384 stepU = (U2 - U1) / theDiscret;
390 case GeomAbs_Cylinder :
392 if(theIsoType == GeomAbs_IsoV){
393 for(j = 1; j < theDiscret; j++){
397 DrawTo(P,thePolyData,thePts);
401 case GeomAbs_Sphere :
403 case GeomAbs_OffsetSurface :
404 case GeomAbs_OtherSurface :
405 for(j = 1; j < theDiscret; j++){
409 DrawTo(P,thePolyData,thePts);
412 case GeomAbs_BezierSurface :
413 case GeomAbs_BSplineSurface :
414 for(j = 1; j <= theDiscret/2; j++){
415 Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
416 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
421 case GeomAbs_SurfaceOfExtrusion :
422 case GeomAbs_SurfaceOfRevolution :
423 if((theIsoType == GeomAbs_IsoV && SurfType == GeomAbs_SurfaceOfRevolution) ||
424 (theIsoType == GeomAbs_IsoU && SurfType == GeomAbs_SurfaceOfExtrusion))
426 if(SurfType == GeomAbs_SurfaceOfExtrusion)
428 for(j = 1; j < theDiscret; j++){
432 DrawTo(P,thePolyData,thePts);
435 CurvType = (S.BasisCurve())->GetType();
439 case GeomAbs_Circle :
440 case GeomAbs_Ellipse :
441 for (j = 1; j < theDiscret; j++) {
445 DrawTo(P,thePolyData,thePts);
448 case GeomAbs_Parabola :
449 case GeomAbs_Hyperbola :
450 case GeomAbs_BezierCurve :
451 case GeomAbs_BSplineCurve :
452 case GeomAbs_OtherCurve :
453 for(j = 1; j <= theDiscret/2; j++){
454 Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
455 CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
465 DrawTo(P,thePolyData,thePts);
474 CreateIso__(const BRepAdaptor_Surface& theSurface,
475 GeomAbs_IsoType theIsoType,
478 Standard_Real theStep,
479 vtkPolyData* thePolyData,
483 if (theIsoType == GeomAbs_IsoU) {
484 theSurface.D0(theU, theV, Pl);
485 theSurface.D0(theU, theV + theStep/2., Pm);
486 theSurface.D0(theU, theV + theStep, Pr);
488 theSurface.D0(theU, theV, Pl);
489 theSurface.D0(theU + theStep/2., theV, Pm);
490 theSurface.D0(theU + theStep, theV, Pr);
493 static Standard_Real ISO_RATIO = 1.001;
494 if (Pm.Distance(Pl) + Pm.Distance(Pr) <= ISO_RATIO*Pl.Distance(Pr)) {
495 DrawTo(Pr,thePolyData,thePts);
497 if (theIsoType == GeomAbs_IsoU) {
498 CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
499 Standard_Real aLocalV = theV + theStep/2 ;
500 CreateIso__(theSurface, theIsoType, theU, aLocalV , theStep/2, thePolyData, thePts);
502 CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
503 Standard_Real aLocalU = theU + theStep/2 ;
504 CreateIso__(theSurface, theIsoType, aLocalU , theV, theStep/2, thePolyData, thePts);