1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
20 #include "GEOM_EdgeSource.h"
21 #include "OCC2VTK_internal.h"
23 #include <vtkObjectFactory.h>
25 #include <vtkPoints.h>
26 #include <vtkCellArray.h>
28 #include <BRep_Tool.hxx>
29 #include <Poly_Polygon3D.hxx>
30 #include <Poly_Triangulation.hxx>
31 #include <TColStd_Array1OfInteger.hxx>
32 #include <Poly_PolygonOnTriangulation.hxx>
33 #include <GeomAdaptor_Curve.hxx>
34 #include <GCPnts_AbscissaPoint.hxx>
36 #include <vtkStripper.h>
37 #include <vtkPolyData.h>
38 #include <vtkInformation.h>
39 #include <vtkInformationVector.h>
41 vtkStandardNewMacro(GEOM_EdgeSource);
43 GEOM_EdgeSource::GEOM_EdgeSource() :
46 myData = new EdgeSourceInternal;
47 this->SetNumberOfInputPorts(0);
50 GEOM_EdgeSource::~GEOM_EdgeSource()
55 void GEOM_EdgeSource::AddEdge (const TopoDS_Edge& theEdge,
58 myData->myEdgeSet.Add(theEdge);
59 myIsVector = theIsVector;
62 void GEOM_EdgeSource::Clear()
64 myData->myEdgeSet.Clear();
67 bool GEOM_EdgeSource::IsEmpty()
69 return myData->myEdgeSet.IsEmpty();
72 int GEOM_EdgeSource::RequestData(vtkInformation *vtkNotUsed(request),
73 vtkInformationVector **vtkNotUsed(inputVector),
74 vtkInformationVector *outputVector)
76 vtkInformation *outInfo = outputVector->GetInformationObject(0);
77 vtkPolyData *aPolyData = vtkPolyData::SafeDownCast(
78 outInfo->Get(vtkDataObject::DATA_OBJECT()));
80 aPolyData->Allocate();
81 vtkPoints* aPts = vtkPoints::New();
82 aPolyData->SetPoints(aPts);
85 TEdgeSet::Iterator anIter (myData->myEdgeSet);
86 for (; anIter.More(); anIter.Next()) {
87 TopoDS_Edge anEdge = anIter.Value();
89 // draw curve direction (issue 0021087)
90 anEdge.Orientation( TopAbs_FORWARD );
91 OCC2VTK(anEdge,aPolyData,aPts,myIsVector||myIsVectorMode);
96 void GEOM_EdgeSource::OCC2VTK (const TopoDS_Edge& theEdge,
97 vtkPolyData* thePolyData,
101 Handle(Poly_PolygonOnTriangulation) aEdgePoly;
102 Standard_Integer i = 1;
103 Handle(Poly_Triangulation) T;
104 TopLoc_Location aEdgeLoc;
105 BRep_Tool::PolygonOnTriangulation(theEdge, aEdgePoly, T, aEdgeLoc, i);
107 Handle(Poly_Polygon3D) P;
108 if(aEdgePoly.IsNull())
109 P = BRep_Tool::Polygon3D(theEdge, aEdgeLoc);
111 if(P.IsNull() && aEdgePoly.IsNull())
117 Standard_Boolean isidtrsf = true;
118 if(!aEdgeLoc.IsIdentity()) {
120 edgeTransf = aEdgeLoc.Transformation();
125 if (aEdgePoly.IsNull()) {
126 Standard_Integer aNbNodes = P->NbNodes();
127 const TColgp_Array1OfPnt& aNodesP = P->Nodes();
130 aP2 = aNodesP(aNbNodes);
132 for (int j = 1; j < aNbNodes; j++) {
133 gp_Pnt pt1 = aNodesP(j);
134 gp_Pnt pt2 = aNodesP(j+1);
137 // apply edge transformation
138 pt1.Transform(edgeTransf);
139 pt2.Transform(edgeTransf);
142 float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
144 anIds[0] = thePts->InsertNextPoint(aCoord1);
146 float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
147 anIds[1] = thePts->InsertNextPoint(aCoord2);
149 thePolyData->InsertNextCell(VTK_LINE,2,anIds);
152 Standard_Integer aNbNodes = aEdgePoly->NbNodes();
153 const TColStd_Array1OfInteger& aNodeIds = aEdgePoly->Nodes();
154 const TColgp_Array1OfPnt& anId2Pnts = T->Nodes();
156 aP1 = anId2Pnts(aNodeIds(1));
157 aP2 = anId2Pnts(aNodeIds(aNbNodes));
159 for(int j = 1; j < aNbNodes; j++) {
160 Standard_Integer id1 = aNodeIds(j);
161 Standard_Integer id2 = aNodeIds(j+1);
163 gp_Pnt pt1 = anId2Pnts(id1);
164 gp_Pnt pt2 = anId2Pnts(id2);
167 // apply edge transformation
168 pt1.Transform(edgeTransf);
169 pt2.Transform(edgeTransf);
172 float aCoord1[3] = {pt1.X(), pt1.Y(), pt1.Z()};
174 anIds[0] = thePts->InsertNextPoint(aCoord1);
176 float aCoord2[3] = {pt2.X(), pt2.Y(), pt2.Z()};
177 anIds[1] = thePts->InsertNextPoint(aCoord2);
179 thePolyData->InsertNextCell(VTK_LINE,2,anIds);
184 // vector representation has an arrow on its end
188 // apply edge transformation
189 aP1.Transform(edgeTransf);
190 aP2.Transform(edgeTransf);
197 Handle(Geom_Curve) C = BRep_Tool::Curve(theEdge,fp,lp);
198 if ( theEdge.Orientation() == TopAbs_FORWARD ) {
199 C->D1(lp, aP2, aDirVec);
201 C->D1(fp, aP1, aDirVec);
205 GeomAdaptor_Curve aAdC;
206 aAdC.Load(C, fp, lp);
207 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
208 if (aDist < gp::Resolution()) return;
212 if ( theEdge.Orientation() == TopAbs_FORWARD )
213 aDirection = aDirVec;
215 aDirection = -aDirVec;
217 Standard_Real anAngle = M_PI/180.*5.;
218 Standard_Real aLength = aDist/10.;
220 Standard_Real dx,dy,dz;
221 aDirection.Coord(dx,dy,dz);
224 Standard_Real xo,yo,zo;
227 // Center of circle that arrow based
228 gp_XYZ aPc = aP2.XYZ() - aDirection.XYZ() * aLength;
230 // Construction of the base vectors for the arrow circle
232 if (Abs(dx) <= Abs(dy) && Abs(dx) <= Abs(dz)) aDirN = gp::DX();
233 else if (Abs(dy) <= Abs(dz) && Abs(dy) <= Abs(dx)) aDirN = gp::DY();
234 else aDirN = gp::DZ();
236 gp_Dir aDirI = aDirection ^ aDirN;
237 gp_Dir aDirJ = aDirection ^ aDirI;
239 // Add points and segments, composing the arrow
240 Standard_Real cosinus, sinus, Tg = tan(anAngle);
242 float coord[3] = {xo, yo, zo};
244 vtkIdType ptLoc = thePts->InsertNextPoint(coord);
245 vtkIdType ptFirst = 0;
246 vtkIdType ptPrev = 0;
252 for (int i = 1; i <= NbPoints; i++, ptPrev = ptCur)
254 cosinus = cos(2. * M_PI / NbPoints * (i-1));
255 sinus = sin(2. * M_PI / NbPoints * (i-1));
257 gp_XYZ aP = aPc + (aDirI.XYZ() * cosinus + aDirJ.XYZ() * sinus) * aLength * Tg;
263 ptCur = thePts->InsertNextPoint(coord);
270 // insert line (ptCur,ptPrev)
272 thePolyData->InsertNextCell(VTK_LINE,2,pts);
275 // insert line (ptCur,ptLoc)
277 thePolyData->InsertNextCell(VTK_LINE,2,pts);
280 // insert line (ptCur,ptFirst)
283 thePolyData->InsertNextCell(VTK_LINE,2,pts);
287 void GEOM_EdgeSource::SetVectorMode (bool theMode)
289 myIsVectorMode = theMode;
292 bool GEOM_EdgeSource::GetVectorMode ()
294 return !myIsVector && myIsVectorMode;