1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "GEOM_EdgeSource.h"
24 #include <vtkObjectFactory.h>
26 #include <vtkPoints.h>
27 #include <vtkCellArray.h>
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>
35 #include <vtkStripper.h>
36 #include <vtkPolyData.h>
38 vtkStandardNewMacro(GEOM_EdgeSource);
40 GEOM_EdgeSource::GEOM_EdgeSource()
44 GEOM_EdgeSource::~GEOM_EdgeSource()
48 void GEOM_EdgeSource::AddEdge (const TopoDS_Edge& theEdge,
51 myEdgeSet.Add(theEdge);
52 myIsVector = theIsVector;
59 vtkPolyData* aPolyData = GetOutput();
60 aPolyData->Allocate();
61 vtkPoints* aPts = vtkPoints::New();
62 aPolyData->SetPoints(aPts);
65 TEdgeSet::Iterator anIter (myEdgeSet);
66 for (; anIter.More(); anIter.Next()) {
67 const TopoDS_Edge& anEdge = anIter.Value();
68 OCC2VTK(anEdge,aPolyData,aPts,myIsVector);
72 void GEOM_EdgeSource::OCC2VTK (const TopoDS_Edge& theEdge,
73 vtkPolyData* thePolyData,
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);
83 Handle(Poly_Polygon3D) P;
84 if(aEdgePoly.IsNull())
85 P = BRep_Tool::Polygon3D(theEdge, aEdgeLoc);
87 if(P.IsNull() && aEdgePoly.IsNull())
93 Standard_Boolean isidtrsf = true;
94 if(!aEdgeLoc.IsIdentity()) {
96 edgeTransf = aEdgeLoc.Transformation();
101 if (aEdgePoly.IsNull()) {
102 Standard_Integer aNbNodes = P->NbNodes();
103 const TColgp_Array1OfPnt& aNodesP = P->Nodes();
106 aP2 = aNodesP(aNbNodes);
108 for (int j = 1; j < aNbNodes; j++) {
109 gp_Pnt pt1 = aNodesP(j);
110 gp_Pnt pt2 = aNodesP(j+1);
113 // apply edge transformation
114 pt1.Transform(edgeTransf);
115 pt2.Transform(edgeTransf);
118 float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
120 anIds[0] = thePts->InsertNextPoint(aCoord1);
122 float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
123 anIds[1] = thePts->InsertNextPoint(aCoord2);
125 thePolyData->InsertNextCell(VTK_LINE,2,anIds);
128 Standard_Integer aNbNodes = aEdgePoly->NbNodes();
129 const TColStd_Array1OfInteger& aNodeIds = aEdgePoly->Nodes();
130 const TColgp_Array1OfPnt& anId2Pnts = T->Nodes();
133 aP2 = anId2Pnts(aNbNodes);
135 for(int j = 1; j < aNbNodes; j++) {
136 Standard_Integer id1 = aNodeIds(j);
137 Standard_Integer id2 = aNodeIds(j+1);
139 gp_Pnt pt1 = anId2Pnts(id1);
140 gp_Pnt pt2 = anId2Pnts(id2);
143 // apply edge transformation
144 pt1.Transform(edgeTransf);
145 pt2.Transform(edgeTransf);
148 float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
150 anIds[0] = thePts->InsertNextPoint(aCoord1);
152 float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
153 anIds[1] = thePts->InsertNextPoint(aCoord2);
155 thePolyData->InsertNextCell(VTK_LINE,2,anIds);
159 // vector representation has an arrow on its end
163 // apply edge transformation
164 aP1.Transform(edgeTransf);
165 aP2.Transform(edgeTransf);
169 gp_Vec aDirVec (aP1, aP2);
170 Standard_Real aDist = aDirVec.Magnitude();
171 if (aDist < gp::Resolution()) return;
172 gp_Dir aDirection (aDirVec);
174 Standard_Real anAngle = PI/180.*5.;
175 Standard_Real aLength = aDist/10.;
177 Standard_Real dx,dy,dz;
178 aDirection.Coord(dx,dy,dz);
180 // Pointe de la fleche
181 Standard_Real xo,yo,zo;
184 // Centre du cercle base de la fleche
185 gp_XYZ aPc = aP2.XYZ() - aDirection.XYZ() * aLength;
187 // Construction d'un repere i,j pour le cercle
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();
193 gp_Dir aDirI = aDirection ^ aDirN;
194 gp_Dir aDirJ = aDirection ^ aDirI;
196 // Add points and segments, composing the arrow
197 Standard_Real cosinus, sinus, Tg = tan(anAngle);
199 float coord[3] = {xo, yo, zo};
201 vtkIdType ptLoc = thePts->InsertNextPoint(coord);
202 vtkIdType ptFirst = 0;
203 vtkIdType ptPrev = 0;
209 for (int i = 1; i <= NbPoints; i++, ptPrev = ptCur)
211 cosinus = cos(2. * PI / NbPoints * (i-1));
212 sinus = sin(2. * PI / NbPoints * (i-1));
214 gp_XYZ aP = aPc + (aDirI.XYZ() * cosinus + aDirJ.XYZ() * sinus) * aLength * Tg;
220 ptCur = thePts->InsertNextPoint(coord);
227 // insert line (ptCur,ptPrev)
229 thePolyData->InsertNextCell(VTK_LINE,2,pts);
232 // insert line (ptCur,ptLoc)
234 thePolyData->InsertNextCell(VTK_LINE,2,pts);
237 // insert line (ptCur,ptFirst)
240 thePolyData->InsertNextCell(VTK_LINE,2,pts);