]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_EdgeSource.cxx
Salome HOME
Bug IPAL21615 Problem in VTK viewer when number of isos = 0
[modules/geom.git] / src / OBJECT / GEOM_EdgeSource.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "GEOM_EdgeSource.h" 
23  
24 #include <vtkObjectFactory.h> 
25
26 #include <vtkPoints.h> 
27 #include <vtkCellArray.h> 
28
29 #include <BRep_Tool.hxx>
30 #include <Poly_Polygon3D.hxx>
31 #include <Poly_Triangulation.hxx>
32 #include <TColStd_Array1OfInteger.hxx>
33 #include <Poly_PolygonOnTriangulation.hxx>
34 #include <GeomAdaptor_Curve.hxx>
35 #include <GCPnts_AbscissaPoint.hxx>
36  
37 #include <vtkStripper.h>  
38 #include <vtkPolyData.h>  
39
40 vtkStandardNewMacro(GEOM_EdgeSource);
41  
42 GEOM_EdgeSource::GEOM_EdgeSource() :
43   myIsVector(false)
44
45
46  
47 GEOM_EdgeSource::~GEOM_EdgeSource() 
48
49
50
51 void GEOM_EdgeSource::AddEdge (const TopoDS_Edge& theEdge,
52                                bool theIsVector)
53 {
54   myEdgeSet.Add(theEdge);
55   myIsVector = theIsVector;
56 }
57
58 void
59 GEOM_EdgeSource:: 
60 Execute()
61 {
62   vtkPolyData* aPolyData = GetOutput();
63   aPolyData->Allocate();
64   vtkPoints* aPts = vtkPoints::New();
65   aPolyData->SetPoints(aPts);
66   aPts->Delete();
67
68   TEdgeSet::Iterator anIter (myEdgeSet);
69   for (; anIter.More(); anIter.Next()) {
70     const TopoDS_Edge& anEdge = anIter.Value();
71     OCC2VTK(anEdge,aPolyData,aPts,myIsVector);
72   }
73 }
74
75 void GEOM_EdgeSource::OCC2VTK (const TopoDS_Edge& theEdge,  
76                                vtkPolyData* thePolyData, 
77                                vtkPoints* thePts,
78                                bool theIsVector) 
79 {
80   Handle(Poly_PolygonOnTriangulation) aEdgePoly;
81   Standard_Integer i = 1;
82   Handle(Poly_Triangulation) T;
83   TopLoc_Location aEdgeLoc;
84   BRep_Tool::PolygonOnTriangulation(theEdge, aEdgePoly, T, aEdgeLoc, i);
85
86   Handle(Poly_Polygon3D) P;
87   if(aEdgePoly.IsNull())
88     P = BRep_Tool::Polygon3D(theEdge, aEdgeLoc);
89
90   if(P.IsNull() && aEdgePoly.IsNull())
91     return;
92   
93   // Location edges
94   //---------------
95   gp_Trsf edgeTransf;
96   Standard_Boolean isidtrsf = true;
97   if(!aEdgeLoc.IsIdentity())  {
98     isidtrsf = false;
99     edgeTransf = aEdgeLoc.Transformation();
100   }
101
102   gp_Pnt aP1, aP2;
103
104   if (aEdgePoly.IsNull()) {
105     Standard_Integer aNbNodes = P->NbNodes();
106     const TColgp_Array1OfPnt& aNodesP = P->Nodes();
107
108     aP1 = aNodesP(1);
109     aP2 = aNodesP(aNbNodes);
110
111     for (int j = 1; j < aNbNodes; j++) {
112       gp_Pnt pt1 = aNodesP(j);
113       gp_Pnt pt2 = aNodesP(j+1);
114
115       if (!isidtrsf) {
116         // apply edge transformation
117         pt1.Transform(edgeTransf);
118         pt2.Transform(edgeTransf);
119       }
120
121       float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
122       vtkIdType anIds[2];
123       anIds[0] = thePts->InsertNextPoint(aCoord1);
124
125       float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
126       anIds[1] = thePts->InsertNextPoint(aCoord2);
127
128       thePolyData->InsertNextCell(VTK_LINE,2,anIds);
129     }
130   } else {
131     Standard_Integer aNbNodes = aEdgePoly->NbNodes();
132     const TColStd_Array1OfInteger& aNodeIds = aEdgePoly->Nodes();
133     const TColgp_Array1OfPnt& anId2Pnts = T->Nodes();
134
135     aP1 = anId2Pnts(aNodeIds(1));
136     aP2 = anId2Pnts(aNodeIds(aNbNodes));
137
138     for(int j = 1; j < aNbNodes; j++) {
139       Standard_Integer id1 = aNodeIds(j);
140       Standard_Integer id2 = aNodeIds(j+1);
141       
142       gp_Pnt pt1 = anId2Pnts(id1);
143       gp_Pnt pt2 = anId2Pnts(id2);
144           
145       if(!isidtrsf) {
146         // apply edge transformation
147         pt1.Transform(edgeTransf);
148         pt2.Transform(edgeTransf);
149       }
150       
151       float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
152       vtkIdType anIds[2];
153       anIds[0] = thePts->InsertNextPoint(aCoord1);
154
155       float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
156       anIds[1] = thePts->InsertNextPoint(aCoord2);
157
158       thePolyData->InsertNextCell(VTK_LINE,2,anIds);
159     }
160   }
161
162   
163   // vector representation has an arrow on its end
164   if (theIsVector)
165   {
166     if (!isidtrsf) {
167       // apply edge transformation
168       aP1.Transform(edgeTransf);
169       aP2.Transform(edgeTransf);
170     }
171
172     // draw an arrow
173
174     double fp,lp;
175     gp_Vec aDirVec;
176     Handle(Geom_Curve) C = BRep_Tool::Curve(theEdge,fp,lp);
177     if ( theEdge.Orientation() == TopAbs_FORWARD ) {
178       C->D1(lp, aP2, aDirVec);
179     } else {
180       C->D1(fp, aP1, aDirVec);
181       aP2 = aP1;
182     }
183
184     GeomAdaptor_Curve aAdC;
185     aAdC.Load(C, fp, lp);
186     Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
187     if (aDist < gp::Resolution()) return;
188
189     gp_Dir aDirection;
190
191     if ( theEdge.Orientation() == TopAbs_FORWARD )
192       aDirection = aDirVec;
193     else
194       aDirection = -aDirVec;
195
196     Standard_Real anAngle = PI/180.*5.;
197     Standard_Real aLength = aDist/10.;
198
199     Standard_Real dx,dy,dz;
200     aDirection.Coord(dx,dy,dz);
201
202     // Arrow Point
203     Standard_Real xo,yo,zo;
204     aP2.Coord(xo,yo,zo);
205
206     // Center of circle that arrow based
207     gp_XYZ aPc = aP2.XYZ() - aDirection.XYZ() * aLength;
208
209     // Construction of the base vectors for the arrow circle
210     gp_Dir aDirN;
211     if      (Abs(dx) <= Abs(dy) && Abs(dx) <= Abs(dz)) aDirN = gp::DX();
212     else if (Abs(dy) <= Abs(dz) && Abs(dy) <= Abs(dx)) aDirN = gp::DY();
213     else aDirN = gp::DZ();
214
215     gp_Dir aDirI = aDirection ^ aDirN;
216     gp_Dir aDirJ = aDirection ^ aDirI;
217
218     // Add points and segments, composing the arrow
219     Standard_Real cosinus, sinus, Tg = tan(anAngle);
220
221     float coord[3] = {xo, yo, zo};
222
223     vtkIdType ptLoc = thePts->InsertNextPoint(coord);
224     vtkIdType ptFirst = 0;
225     vtkIdType ptPrev = 0;
226     vtkIdType ptCur = 0;
227
228     vtkIdType pts[2];
229
230     int NbPoints = 15;
231     for (int i = 1; i <= NbPoints; i++, ptPrev = ptCur)
232     {
233       cosinus = cos(2. * PI / NbPoints * (i-1));   
234       sinus   = sin(2. * PI / NbPoints * (i-1));
235
236       gp_XYZ aP = aPc + (aDirI.XYZ() * cosinus + aDirJ.XYZ() * sinus) * aLength * Tg;
237       coord[0] = aP.X();
238       coord[1] = aP.Y();
239       coord[2] = aP.Z();
240
241       // insert pts
242       ptCur = thePts->InsertNextPoint(coord);
243       pts[0] = ptCur;
244
245       if (i == 1) {
246         ptFirst = ptCur;
247       }
248       else {
249         // insert line (ptCur,ptPrev)
250         pts[1] = ptPrev;
251         thePolyData->InsertNextCell(VTK_LINE,2,pts);
252       }
253
254       // insert line (ptCur,ptLoc)
255       pts[1] = ptLoc;
256       thePolyData->InsertNextCell(VTK_LINE,2,pts);
257     }
258
259     // insert line (ptCur,ptFirst)
260     pts[0] = ptCur;
261     pts[1] = ptFirst;
262     thePolyData->InsertNextCell(VTK_LINE,2,pts);
263   }
264 }
265
266 void GEOM_EdgeSource::SetVectorMode (bool theMode)
267 {
268   myIsVector = theMode;
269 }
270
271 bool GEOM_EdgeSource::GetVectorMode ()
272 {
273   return myIsVector;
274 }