]> SALOME platform Git repositories - modules/geom.git/blob - src/OCC2VTK/GEOM_EdgeSource.cxx
Salome HOME
Internal issue 0022865: Restructurization of Partition packages.
[modules/geom.git] / src / OCC2VTK / GEOM_EdgeSource.cxx
1 // Copyright (C) 2007-2011  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.
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 #include "GEOM_EdgeSource.h" 
20  
21 #include <vtkObjectFactory.h> 
22
23 #include <vtkPoints.h> 
24 #include <vtkCellArray.h> 
25
26 #include <BRep_Tool.hxx>
27 #include <Poly_Polygon3D.hxx>
28 #include <Poly_Triangulation.hxx>
29 #include <TColStd_Array1OfInteger.hxx>
30 #include <Poly_PolygonOnTriangulation.hxx>
31 #include <GeomAdaptor_Curve.hxx>
32 #include <GCPnts_AbscissaPoint.hxx>
33  
34 #include <vtkStripper.h>  
35 #include <vtkPolyData.h>  
36
37 vtkStandardNewMacro(GEOM_EdgeSource);
38  
39 GEOM_EdgeSource::GEOM_EdgeSource() :
40   myIsVector(false)
41
42
43  
44 GEOM_EdgeSource::~GEOM_EdgeSource() 
45
46
47
48 void GEOM_EdgeSource::AddEdge (const TopoDS_Edge& theEdge,
49                                bool theIsVector)
50 {
51   myEdgeSet.Add(theEdge);
52   myIsVector = theIsVector;
53 }
54
55 void
56 GEOM_EdgeSource:: 
57 Execute()
58 {
59   vtkPolyData* aPolyData = GetOutput();
60   aPolyData->Allocate();
61   vtkPoints* aPts = vtkPoints::New();
62   aPolyData->SetPoints(aPts);
63   aPts->Delete();
64
65   TEdgeSet::Iterator anIter (myEdgeSet);
66   for (; anIter.More(); anIter.Next()) {
67     TopoDS_Edge anEdge = anIter.Value();
68     if ( !myIsVector )
69       // draw curve direction (issue 0021087)
70       anEdge.Orientation( TopAbs_FORWARD );
71     OCC2VTK(anEdge,aPolyData,aPts,myIsVector||myIsVectorMode);
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 = M_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. * M_PI / NbPoints * (i-1));   
234       sinus   = sin(2. * M_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   myIsVectorMode = theMode;
269 }
270
271 bool GEOM_EdgeSource::GetVectorMode ()
272 {
273   return !myIsVector && myIsVectorMode;
274 }