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