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