]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_EdgeSource.cxx
Salome HOME
IMP 0016175: EDF455: Save GUIState don't redisplay the objects.
[modules/geom.git] / src / OBJECT / GEOM_EdgeSource.cxx
1 #include "GEOM_EdgeSource.h" 
2  
3 #include <vtkObjectFactory.h> 
4
5 #include <vtkPoints.h> 
6 #include <vtkCellArray.h> 
7
8 #include <BRep_Tool.hxx>
9 #include <Poly_Polygon3D.hxx>
10 #include <Poly_Triangulation.hxx>
11 #include <TColStd_Array1OfInteger.hxx>
12 #include <Poly_PolygonOnTriangulation.hxx>
13  
14 #include <vtkStripper.h>  
15 #include <vtkPolyData.h>  
16  
17 vtkStandardNewMacro(GEOM_EdgeSource);
18  
19 GEOM_EdgeSource::GEOM_EdgeSource() 
20
21
22  
23 GEOM_EdgeSource::~GEOM_EdgeSource() 
24
25
26
27 void GEOM_EdgeSource::AddEdge (const TopoDS_Edge& theEdge,
28                                bool theIsVector)
29 {
30   myEdgeSet.Add(theEdge);
31   myIsVector = theIsVector;
32 }
33
34 void
35 GEOM_EdgeSource:: 
36 Execute()
37 {
38   vtkPolyData* aPolyData = GetOutput();
39   aPolyData->Allocate();
40   vtkPoints* aPts = vtkPoints::New();
41   aPolyData->SetPoints(aPts);
42   aPts->Delete();
43
44   TEdgeSet::Iterator anIter (myEdgeSet);
45   for (; anIter.More(); anIter.Next()) {
46     const TopoDS_Edge& anEdge = anIter.Value();
47     OCC2VTK(anEdge,aPolyData,aPts,myIsVector);
48   }
49 }
50
51 void GEOM_EdgeSource::OCC2VTK (const TopoDS_Edge& theEdge,  
52                                vtkPolyData* thePolyData, 
53                                vtkPoints* thePts,
54                                bool theIsVector) 
55 {
56   Handle(Poly_PolygonOnTriangulation) aEdgePoly;
57   Standard_Integer i = 1;
58   Handle(Poly_Triangulation) T;
59   TopLoc_Location aEdgeLoc;
60   BRep_Tool::PolygonOnTriangulation(theEdge, aEdgePoly, T, aEdgeLoc, i);
61
62   Handle(Poly_Polygon3D) P;
63   if(aEdgePoly.IsNull())
64     P = BRep_Tool::Polygon3D(theEdge, aEdgeLoc);
65
66   if(P.IsNull() && aEdgePoly.IsNull())
67     return;
68   
69   // Location edges
70   //---------------
71   gp_Trsf edgeTransf;
72   Standard_Boolean isidtrsf = true;
73   if(!aEdgeLoc.IsIdentity())  {
74     isidtrsf = false;
75     edgeTransf = aEdgeLoc.Transformation();
76   }
77
78   gp_Pnt aP1, aP2;
79
80   if (aEdgePoly.IsNull()) {
81     Standard_Integer aNbNodes = P->NbNodes();
82     const TColgp_Array1OfPnt& aNodesP = P->Nodes();
83
84     aP1 = aNodesP(1);
85     aP2 = aNodesP(aNbNodes);
86
87     for (int j = 1; j < aNbNodes; j++) {
88       gp_Pnt pt1 = aNodesP(j);
89       gp_Pnt pt2 = aNodesP(j+1);
90
91       if (!isidtrsf) {
92         // apply edge transformation
93         pt1.Transform(edgeTransf);
94         pt2.Transform(edgeTransf);
95       }
96
97       float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
98       vtkIdType anIds[2];
99       anIds[0] = thePts->InsertNextPoint(aCoord1);
100
101       float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
102       anIds[1] = thePts->InsertNextPoint(aCoord2);
103
104       thePolyData->InsertNextCell(VTK_LINE,2,anIds);
105     }
106   } else {
107     Standard_Integer aNbNodes = aEdgePoly->NbNodes();
108     const TColStd_Array1OfInteger& aNodeIds = aEdgePoly->Nodes();
109     const TColgp_Array1OfPnt& anId2Pnts = T->Nodes();
110
111     aP1 = anId2Pnts(1);
112     aP2 = anId2Pnts(aNbNodes);
113
114     for(int j = 1; j < aNbNodes; j++) {
115       Standard_Integer id1 = aNodeIds(j);
116       Standard_Integer id2 = aNodeIds(j+1);
117       
118       gp_Pnt pt1 = anId2Pnts(id1);
119       gp_Pnt pt2 = anId2Pnts(id2);
120           
121       if(!isidtrsf) {
122         // apply edge transformation
123         pt1.Transform(edgeTransf);
124         pt2.Transform(edgeTransf);
125       }
126       
127       float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
128       vtkIdType anIds[2];
129       anIds[0] = thePts->InsertNextPoint(aCoord1);
130
131       float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
132       anIds[1] = thePts->InsertNextPoint(aCoord2);
133
134       thePolyData->InsertNextCell(VTK_LINE,2,anIds);
135     }
136   }
137
138   // vector representation has an arrow on its end
139   if (theIsVector)
140   {
141     if (!isidtrsf) {
142       // apply edge transformation
143       aP1.Transform(edgeTransf);
144       aP2.Transform(edgeTransf);
145     }
146
147     // draw an arrow
148     gp_Vec aDirVec (aP1, aP2);
149     Standard_Real aDist = aDirVec.Magnitude();
150     if (aDist < gp::Resolution()) return;
151     gp_Dir aDirection (aDirVec);
152
153     Standard_Real anAngle = PI/180.*5.;
154     Standard_Real aLength = aDist/10.;
155
156     Standard_Real dx,dy,dz;
157     aDirection.Coord(dx,dy,dz);
158
159     // Pointe de la fleche
160     Standard_Real xo,yo,zo;
161     aP2.Coord(xo,yo,zo);
162
163     // Centre du cercle base de la fleche
164     gp_XYZ aPc = aP2.XYZ() - aDirection.XYZ() * aLength;
165
166     // Construction d'un repere i,j pour le cercle
167     gp_Dir aDirN;
168     if      (Abs(dx) <= Abs(dy) && Abs(dx) <= Abs(dz)) aDirN = gp::DX();
169     else if (Abs(dy) <= Abs(dz) && Abs(dy) <= Abs(dx)) aDirN = gp::DY();
170     else aDirN = gp::DZ();
171
172     gp_Dir aDirI = aDirection ^ aDirN;
173     gp_Dir aDirJ = aDirection ^ aDirI;
174
175     // Add points and segments, composing the arrow
176     Standard_Real cosinus, sinus, Tg = tan(anAngle);
177
178     float coord[3] = {xo, yo, zo};
179
180     vtkIdType ptLoc = thePts->InsertNextPoint(coord);
181     vtkIdType ptFirst = 0;
182     vtkIdType ptPrev = 0;
183     vtkIdType ptCur = 0;
184
185     vtkIdType pts[2];
186
187     int NbPoints = 15;
188     for (int i = 1; i <= NbPoints; i++, ptPrev = ptCur)
189     {
190       cosinus = cos(2. * PI / NbPoints * (i-1));   
191       sinus   = sin(2. * PI / NbPoints * (i-1));
192
193       gp_XYZ aP = aPc + (aDirI.XYZ() * cosinus + aDirJ.XYZ() * sinus) * aLength * Tg;
194       coord[0] = aP.X();
195       coord[1] = aP.Y();
196       coord[2] = aP.Z();
197
198       // insert pts
199       ptCur = thePts->InsertNextPoint(coord);
200       pts[0] = ptCur;
201
202       if (i == 1) {
203         ptFirst = ptCur;
204       }
205       else {
206         // insert line (ptCur,ptPrev)
207         pts[1] = ptPrev;
208         thePolyData->InsertNextCell(VTK_LINE,2,pts);
209       }
210
211       // insert line (ptCur,ptLoc)
212       pts[1] = ptLoc;
213       thePolyData->InsertNextCell(VTK_LINE,2,pts);
214     }
215
216     // insert line (ptCur,ptFirst)
217     pts[0] = ptCur;
218     pts[1] = ptFirst;
219     thePolyData->InsertNextCell(VTK_LINE,2,pts);
220   }
221 }