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