Salome HOME
Fix documentation problems (reported as doxygen warnings)
[modules/geom.git] / src / OBJECT / GEOM_EdgeSource.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "GEOM_EdgeSource.h" 
23  
24 #include <vtkObjectFactory.h> 
25
26 #include <vtkPoints.h> 
27 #include <vtkCellArray.h> 
28
29 #include <BRep_Tool.hxx>
30 #include <Poly_Polygon3D.hxx>
31 #include <Poly_Triangulation.hxx>
32 #include <TColStd_Array1OfInteger.hxx>
33 #include <Poly_PolygonOnTriangulation.hxx>
34  
35 #include <vtkStripper.h>  
36 #include <vtkPolyData.h>  
37  
38 vtkStandardNewMacro(GEOM_EdgeSource);
39  
40 GEOM_EdgeSource::GEOM_EdgeSource() 
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     const TopoDS_Edge& anEdge = anIter.Value();
68     OCC2VTK(anEdge,aPolyData,aPts,myIsVector);
69   }
70 }
71
72 void GEOM_EdgeSource::OCC2VTK (const TopoDS_Edge& theEdge,  
73                                vtkPolyData* thePolyData, 
74                                vtkPoints* thePts,
75                                bool theIsVector) 
76 {
77   Handle(Poly_PolygonOnTriangulation) aEdgePoly;
78   Standard_Integer i = 1;
79   Handle(Poly_Triangulation) T;
80   TopLoc_Location aEdgeLoc;
81   BRep_Tool::PolygonOnTriangulation(theEdge, aEdgePoly, T, aEdgeLoc, i);
82
83   Handle(Poly_Polygon3D) P;
84   if(aEdgePoly.IsNull())
85     P = BRep_Tool::Polygon3D(theEdge, aEdgeLoc);
86
87   if(P.IsNull() && aEdgePoly.IsNull())
88     return;
89   
90   // Location edges
91   //---------------
92   gp_Trsf edgeTransf;
93   Standard_Boolean isidtrsf = true;
94   if(!aEdgeLoc.IsIdentity())  {
95     isidtrsf = false;
96     edgeTransf = aEdgeLoc.Transformation();
97   }
98
99   gp_Pnt aP1, aP2;
100
101   if (aEdgePoly.IsNull()) {
102     Standard_Integer aNbNodes = P->NbNodes();
103     const TColgp_Array1OfPnt& aNodesP = P->Nodes();
104
105     aP1 = aNodesP(1);
106     aP2 = aNodesP(aNbNodes);
107
108     for (int j = 1; j < aNbNodes; j++) {
109       gp_Pnt pt1 = aNodesP(j);
110       gp_Pnt pt2 = aNodesP(j+1);
111
112       if (!isidtrsf) {
113         // apply edge transformation
114         pt1.Transform(edgeTransf);
115         pt2.Transform(edgeTransf);
116       }
117
118       float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
119       vtkIdType anIds[2];
120       anIds[0] = thePts->InsertNextPoint(aCoord1);
121
122       float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
123       anIds[1] = thePts->InsertNextPoint(aCoord2);
124
125       thePolyData->InsertNextCell(VTK_LINE,2,anIds);
126     }
127   } else {
128     Standard_Integer aNbNodes = aEdgePoly->NbNodes();
129     const TColStd_Array1OfInteger& aNodeIds = aEdgePoly->Nodes();
130     const TColgp_Array1OfPnt& anId2Pnts = T->Nodes();
131
132     aP1 = anId2Pnts(1);
133     aP2 = anId2Pnts(aNbNodes);
134
135     for(int j = 1; j < aNbNodes; j++) {
136       Standard_Integer id1 = aNodeIds(j);
137       Standard_Integer id2 = aNodeIds(j+1);
138       
139       gp_Pnt pt1 = anId2Pnts(id1);
140       gp_Pnt pt2 = anId2Pnts(id2);
141           
142       if(!isidtrsf) {
143         // apply edge transformation
144         pt1.Transform(edgeTransf);
145         pt2.Transform(edgeTransf);
146       }
147       
148       float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
149       vtkIdType anIds[2];
150       anIds[0] = thePts->InsertNextPoint(aCoord1);
151
152       float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
153       anIds[1] = thePts->InsertNextPoint(aCoord2);
154
155       thePolyData->InsertNextCell(VTK_LINE,2,anIds);
156     }
157   }
158
159   // vector representation has an arrow on its end
160   if (theIsVector)
161   {
162     if (!isidtrsf) {
163       // apply edge transformation
164       aP1.Transform(edgeTransf);
165       aP2.Transform(edgeTransf);
166     }
167
168     // draw an arrow
169     gp_Vec aDirVec (aP1, aP2);
170     Standard_Real aDist = aDirVec.Magnitude();
171     if (aDist < gp::Resolution()) return;
172     gp_Dir aDirection (aDirVec);
173
174     Standard_Real anAngle = PI/180.*5.;
175     Standard_Real aLength = aDist/10.;
176
177     Standard_Real dx,dy,dz;
178     aDirection.Coord(dx,dy,dz);
179
180     // Pointe de la fleche
181     Standard_Real xo,yo,zo;
182     aP2.Coord(xo,yo,zo);
183
184     // Centre du cercle base de la fleche
185     gp_XYZ aPc = aP2.XYZ() - aDirection.XYZ() * aLength;
186
187     // Construction d'un repere i,j pour le cercle
188     gp_Dir aDirN;
189     if      (Abs(dx) <= Abs(dy) && Abs(dx) <= Abs(dz)) aDirN = gp::DX();
190     else if (Abs(dy) <= Abs(dz) && Abs(dy) <= Abs(dx)) aDirN = gp::DY();
191     else aDirN = gp::DZ();
192
193     gp_Dir aDirI = aDirection ^ aDirN;
194     gp_Dir aDirJ = aDirection ^ aDirI;
195
196     // Add points and segments, composing the arrow
197     Standard_Real cosinus, sinus, Tg = tan(anAngle);
198
199     float coord[3] = {xo, yo, zo};
200
201     vtkIdType ptLoc = thePts->InsertNextPoint(coord);
202     vtkIdType ptFirst = 0;
203     vtkIdType ptPrev = 0;
204     vtkIdType ptCur = 0;
205
206     vtkIdType pts[2];
207
208     int NbPoints = 15;
209     for (int i = 1; i <= NbPoints; i++, ptPrev = ptCur)
210     {
211       cosinus = cos(2. * PI / NbPoints * (i-1));   
212       sinus   = sin(2. * PI / NbPoints * (i-1));
213
214       gp_XYZ aP = aPc + (aDirI.XYZ() * cosinus + aDirJ.XYZ() * sinus) * aLength * Tg;
215       coord[0] = aP.X();
216       coord[1] = aP.Y();
217       coord[2] = aP.Z();
218
219       // insert pts
220       ptCur = thePts->InsertNextPoint(coord);
221       pts[0] = ptCur;
222
223       if (i == 1) {
224         ptFirst = ptCur;
225       }
226       else {
227         // insert line (ptCur,ptPrev)
228         pts[1] = ptPrev;
229         thePolyData->InsertNextCell(VTK_LINE,2,pts);
230       }
231
232       // insert line (ptCur,ptLoc)
233       pts[1] = ptLoc;
234       thePolyData->InsertNextCell(VTK_LINE,2,pts);
235     }
236
237     // insert line (ptCur,ptFirst)
238     pts[0] = ptCur;
239     pts[1] = ptFirst;
240     thePolyData->InsertNextCell(VTK_LINE,2,pts);
241   }
242 }