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