1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "GEOM_EdgeSource.h"
21 #include <vtkObjectFactory.h>
23 #include <vtkPoints.h>
24 #include <vtkCellArray.h>
26 #include <BRep_Tool.hxx>
27 #include <Poly_Polygon3D.hxx>
28 #include <Poly_Triangulation.hxx>
29 #include <TColStd_Array1OfInteger.hxx>
30 #include <Poly_PolygonOnTriangulation.hxx>
31 #include <GeomAdaptor_Curve.hxx>
32 #include <GCPnts_AbscissaPoint.hxx>
34 #include <vtkStripper.h>
35 #include <vtkPolyData.h>
37 vtkStandardNewMacro(GEOM_EdgeSource);
39 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 TopoDS_Edge anEdge = anIter.Value();
69 // draw curve direction (issue 0021087)
70 anEdge.Orientation( TopAbs_FORWARD );
71 OCC2VTK(anEdge,aPolyData,aPts,myIsVector||myIsVectorMode);
75 void GEOM_EdgeSource::OCC2VTK (const TopoDS_Edge& theEdge,
76 vtkPolyData* thePolyData,
80 Handle(Poly_PolygonOnTriangulation) aEdgePoly;
81 Standard_Integer i = 1;
82 Handle(Poly_Triangulation) T;
83 TopLoc_Location aEdgeLoc;
84 BRep_Tool::PolygonOnTriangulation(theEdge, aEdgePoly, T, aEdgeLoc, i);
86 Handle(Poly_Polygon3D) P;
87 if(aEdgePoly.IsNull())
88 P = BRep_Tool::Polygon3D(theEdge, aEdgeLoc);
90 if(P.IsNull() && aEdgePoly.IsNull())
96 Standard_Boolean isidtrsf = true;
97 if(!aEdgeLoc.IsIdentity()) {
99 edgeTransf = aEdgeLoc.Transformation();
104 if (aEdgePoly.IsNull()) {
105 Standard_Integer aNbNodes = P->NbNodes();
106 const TColgp_Array1OfPnt& aNodesP = P->Nodes();
109 aP2 = aNodesP(aNbNodes);
111 for (int j = 1; j < aNbNodes; j++) {
112 gp_Pnt pt1 = aNodesP(j);
113 gp_Pnt pt2 = aNodesP(j+1);
116 // apply edge transformation
117 pt1.Transform(edgeTransf);
118 pt2.Transform(edgeTransf);
121 float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
123 anIds[0] = thePts->InsertNextPoint(aCoord1);
125 float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
126 anIds[1] = thePts->InsertNextPoint(aCoord2);
128 thePolyData->InsertNextCell(VTK_LINE,2,anIds);
131 Standard_Integer aNbNodes = aEdgePoly->NbNodes();
132 const TColStd_Array1OfInteger& aNodeIds = aEdgePoly->Nodes();
133 const TColgp_Array1OfPnt& anId2Pnts = T->Nodes();
135 aP1 = anId2Pnts(aNodeIds(1));
136 aP2 = anId2Pnts(aNodeIds(aNbNodes));
138 for(int j = 1; j < aNbNodes; j++) {
139 Standard_Integer id1 = aNodeIds(j);
140 Standard_Integer id2 = aNodeIds(j+1);
142 gp_Pnt pt1 = anId2Pnts(id1);
143 gp_Pnt pt2 = anId2Pnts(id2);
146 // apply edge transformation
147 pt1.Transform(edgeTransf);
148 pt2.Transform(edgeTransf);
151 float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
153 anIds[0] = thePts->InsertNextPoint(aCoord1);
155 float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
156 anIds[1] = thePts->InsertNextPoint(aCoord2);
158 thePolyData->InsertNextCell(VTK_LINE,2,anIds);
163 // vector representation has an arrow on its end
167 // apply edge transformation
168 aP1.Transform(edgeTransf);
169 aP2.Transform(edgeTransf);
176 Handle(Geom_Curve) C = BRep_Tool::Curve(theEdge,fp,lp);
177 if ( theEdge.Orientation() == TopAbs_FORWARD ) {
178 C->D1(lp, aP2, aDirVec);
180 C->D1(fp, aP1, aDirVec);
184 GeomAdaptor_Curve aAdC;
185 aAdC.Load(C, fp, lp);
186 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
187 if (aDist < gp::Resolution()) return;
191 if ( theEdge.Orientation() == TopAbs_FORWARD )
192 aDirection = aDirVec;
194 aDirection = -aDirVec;
196 Standard_Real anAngle = M_PI/180.*5.;
197 Standard_Real aLength = aDist/10.;
199 Standard_Real dx,dy,dz;
200 aDirection.Coord(dx,dy,dz);
203 Standard_Real xo,yo,zo;
206 // Center of circle that arrow based
207 gp_XYZ aPc = aP2.XYZ() - aDirection.XYZ() * aLength;
209 // Construction of the base vectors for the arrow circle
211 if (Abs(dx) <= Abs(dy) && Abs(dx) <= Abs(dz)) aDirN = gp::DX();
212 else if (Abs(dy) <= Abs(dz) && Abs(dy) <= Abs(dx)) aDirN = gp::DY();
213 else aDirN = gp::DZ();
215 gp_Dir aDirI = aDirection ^ aDirN;
216 gp_Dir aDirJ = aDirection ^ aDirI;
218 // Add points and segments, composing the arrow
219 Standard_Real cosinus, sinus, Tg = tan(anAngle);
221 float coord[3] = {xo, yo, zo};
223 vtkIdType ptLoc = thePts->InsertNextPoint(coord);
224 vtkIdType ptFirst = 0;
225 vtkIdType ptPrev = 0;
231 for (int i = 1; i <= NbPoints; i++, ptPrev = ptCur)
233 cosinus = cos(2. * M_PI / NbPoints * (i-1));
234 sinus = sin(2. * M_PI / NbPoints * (i-1));
236 gp_XYZ aP = aPc + (aDirI.XYZ() * cosinus + aDirJ.XYZ() * sinus) * aLength * Tg;
242 ptCur = thePts->InsertNextPoint(coord);
249 // insert line (ptCur,ptPrev)
251 thePolyData->InsertNextCell(VTK_LINE,2,pts);
254 // insert line (ptCur,ptLoc)
256 thePolyData->InsertNextCell(VTK_LINE,2,pts);
259 // insert line (ptCur,ptFirst)
262 thePolyData->InsertNextCell(VTK_LINE,2,pts);
266 void GEOM_EdgeSource::SetVectorMode (bool theMode)
268 myIsVectorMode = theMode;
271 bool GEOM_EdgeSource::GetVectorMode ()
273 return !myIsVector && myIsVectorMode;