Salome HOME
OCC functionality moving out from the widget
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_AISCurve.cxx
1 // Copyright (C) 2013  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 "CurveCreator_AISCurve.hxx"
21 #include "CurveCreator_Curve.hxx"
22
23 #include <Quantity_Color.hxx>
24 #include <Prs3d_Presentation.hxx>
25 #include <StdPrs_WFDeflectionShape.hxx>
26 #include <Geom_BSplineCurve.hxx>
27 #include <TColgp_HArray1OfPnt.hxx>
28 #include <GeomAPI_Interpolate.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Wire.hxx>
34
35 #include <Graphic3d_Array1OfVertex.hxx>
36 #include <Graphic3d_Group.hxx>
37 #include <Graphic3d_ArrayOfSegments.hxx>
38 #include <Prs3d_Root.hxx>
39
40 IMPLEMENT_STANDARD_HANDLE(CurveCreator_AISCurve, AIS_Shape)
41 IMPLEMENT_STANDARD_RTTIEXT(CurveCreator_AISCurve, AIS_Shape)
42
43 CurveCreator_AISCurve::CurveCreator_AISCurve( CurveCreator_Curve* theCurve )
44 : AIS_Shape( TopoDS_Shape() ), myCurve( theCurve )
45 {
46 }
47
48 void CurveCreator_AISCurve::getPoint( const int theISection, const int theIPoint,
49                                       gp_Pnt& thePoint )
50 {
51   double anX, anY, aZ;
52   myCurve->getCoordinates( theISection, theIPoint, anX, anY, aZ );
53   thePoint = gp_Pnt( anX, anY, aZ);
54   /*
55   double anX, anY, aZ;
56
57   std::deque<float> aPoint = myCurve->getPoint( theSectionId, thePointId );
58   anX = aPoint[0];
59   anY = aPoint[1];
60   aZ  = aPoint[2];
61   thePoint = gp_Pnt( anX, anY, aZ);*/
62 }
63
64 #define MAKE_BUILDER_USE
65 void CurveCreator_AISCurve::Compute( const Handle(PrsMgr_PresentationManager3d)& thePM,
66                                      const Handle(Prs3d_Presentation)& thePrs,
67                                      const Standard_Integer theMode )
68 {
69   thePrs->Clear();
70
71   for( int iSection = 0 ; iSection < myCurve->getNbSections() ; iSection++ ) {
72     CurveCreator::SectionType aSectType = myCurve->getSectionType( iSection );
73     int aPointSize = myCurve->getNbPoints( iSection );
74     bool aSectIsClosed = myCurve->isClosed( iSection );
75     if( aSectType == CurveCreator::Polyline )
76     {
77       int iPoint = 0;
78       gp_Pnt aPrevPoint, aPoint;
79       if ( aPointSize == 1 ) {
80         getPoint( iSection, iPoint, aPrevPoint );
81         TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
82         Set( aVertex );
83       }
84       else if ( aPointSize > 1 ) {
85 #ifdef MAKE_BUILDER_USE
86         TopoDS_Edge aPointEdge;
87         BRepBuilderAPI_MakeWire aMakeWire;
88         getPoint( iSection, iPoint, aPrevPoint );
89         iPoint++;
90         for( ; iPoint < aPointSize; iPoint++ ) {
91           getPoint( iSection, iPoint, aPoint );
92           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
93           aMakeWire.Add( BRepBuilderAPI_MakeWire( aPointEdge ) );
94           aPrevPoint = aPoint;
95         }
96         if( myCurve->isClosed( iSection ) && ( aPointSize > 2 ) ) {
97           getPoint( iSection, 0, aPoint );
98           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
99           aMakeWire.Add( BRepBuilderAPI_MakeWire( aPointEdge ) );
100         }
101
102         TopoDS_Wire aLineWire = aMakeWire.Wire();
103         Set( aLineWire );
104         AIS_Shape::Compute( thePM, thePrs, theMode );
105       }
106 #else
107       bool isClosed = myCurve->isClosed( iSection ) && ( aPointSize > 2 );
108       Handle(Graphic3d_ArrayOfPolylines) anArray = new Graphic3d_ArrayOfPolylines
109                                              ( !isClosed ? aPointSize : aPointSize+1 );
110       for( ; iPoint < aPointSize; iPoint++ ) {
111         getPoint( iSection, iPoint, aPoint );
112         anArray->AddVertex(aPoint.X(), aPoint.Y(), aPoint.Z());
113       }
114
115       if( myCurve->isClosed( iSection ) && ( aPointSize > 2 ) ) {
116         getPoint( iSection, 0, aPoint );
117         anArray->AddVertex(aPoint.X(), aPoint.Y(), aPoint.Z());
118       }
119       Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( thePrs );
120       aGroup->AddPrimitiveArray(anArray);
121 #endif
122     }
123     else if( aSectType == CurveCreator::Spline )
124     {
125       std::vector<double> aPoints;
126       for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
127       {
128         CurveCreator::Coordinates aCoords = myCurve->getPoint( iSection, iPoint );
129         double aX = aCoords[0];
130         double aY = aCoords[1];
131         double aZ = aCoords[2];
132         aPoints.push_back( aX );
133         aPoints.push_back( aY );
134       }
135       if( aPointSize > 1 )
136       {
137         Handle(Geom_BSplineCurve) aBSplineCurve;
138         Handle(Geom_Curve) aGeomCurve;
139         // fill array for algorithm by the received coordinates
140         int aLen = aPoints.size() / 2;
141         Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
142         std::vector<double>::const_iterator aListIter = aPoints.begin();
143         for (int ind = 1; ind <= aLen; ind++) {
144           gp_Pnt aPnt(gp::Origin());
145           aPnt.SetX(*aListIter);
146           aListIter++;
147           aPnt.SetY(*aListIter);
148           aListIter++;
149           aPnt.SetZ(0);
150           aHCurvePoints->SetValue(ind, aPnt);
151         }
152         // compute BSpline
153         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
154         aGBC.Perform();
155         if (aGBC.IsDone()) {
156           aBSplineCurve = aGBC.Curve();
157         }
158         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
159
160         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
161
162         Set( aWire );
163         AIS_Shape::Compute( thePM, thePrs, theMode );
164       }
165     }
166   }
167 }
168
169 void CurveCreator_AISCurve::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
170                                               const Standard_Integer theMode )
171 {
172   AIS_Shape::ComputeSelection( theSelection, theMode );
173 }