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
56 #define MAKE_BUILDER_USE
57 void CurveCreator_AISCurve::Compute( const Handle(PrsMgr_PresentationManager3d)& thePM,
58                                      const Handle(Prs3d_Presentation)& thePrs,
59                                      const Standard_Integer theMode )
60 {
61   thePrs->Clear();
62
63   for( int iSection = 0 ; iSection < myCurve->getNbSections() ; iSection++ ) {
64     CurveCreator::SectionType aSectType = myCurve->getSectionType( iSection );
65     int aPointSize = myCurve->getNbPoints( iSection );
66     bool aSectIsClosed = myCurve->isClosed( iSection );
67     if( aSectType == CurveCreator::Polyline )
68     {
69       int iPoint = 0;
70       gp_Pnt aPrevPoint, aPoint;
71       if ( aPointSize == 1 ) {
72         getPoint( iSection, iPoint, aPrevPoint );
73         TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
74         Set( aVertex );
75       }
76       else if ( aPointSize > 1 ) {
77 #ifdef MAKE_BUILDER_USE
78         TopoDS_Edge aPointEdge;
79         BRepBuilderAPI_MakeWire aMakeWire;
80         getPoint( iSection, iPoint, aPrevPoint );
81         iPoint++;
82         for( ; iPoint < aPointSize; iPoint++ ) {
83           getPoint( iSection, iPoint, aPoint );
84           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
85           aMakeWire.Add( BRepBuilderAPI_MakeWire( aPointEdge ) );
86           aPrevPoint = aPoint;
87         }
88         if( myCurve->isClosed( iSection ) && ( aPointSize > 2 ) ) {
89           getPoint( iSection, 0, aPoint );
90           aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
91           aMakeWire.Add( BRepBuilderAPI_MakeWire( aPointEdge ) );
92         }
93
94         TopoDS_Wire aLineWire = aMakeWire.Wire();
95         Set( aLineWire );
96         AIS_Shape::Compute( thePM, thePrs, theMode );
97       }
98 #else
99       bool isClosed = myCurve->isClosed( iSection ) && ( aPointSize > 2 );
100       Handle(Graphic3d_ArrayOfPolylines) anArray = new Graphic3d_ArrayOfPolylines
101                                              ( !isClosed ? aPointSize : aPointSize+1 );
102       for( ; iPoint < aPointSize; iPoint++ ) {
103         getPoint( iSection, iPoint, aPoint );
104         anArray->AddVertex(aPoint.X(), aPoint.Y(), aPoint.Z());
105       }
106
107       if( myCurve->isClosed( iSection ) && ( aPointSize > 2 ) ) {
108         getPoint( iSection, 0, aPoint );
109         anArray->AddVertex(aPoint.X(), aPoint.Y(), aPoint.Z());
110       }
111       Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( thePrs );
112       aGroup->AddPrimitiveArray(anArray);
113 #endif
114     }
115     else if( aSectType == CurveCreator::Spline )
116     {
117       std::vector<double> aPoints;
118       for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
119       {
120         CurveCreator::Coordinates aCoords = myCurve->getPoint( iSection, iPoint );
121         double aX = aCoords[0];
122         double aY = aCoords[1];
123         double aZ = aCoords[2];
124         aPoints.push_back( aX );
125         aPoints.push_back( aY );
126       }
127       if( aPointSize > 1 )
128       {
129         Handle(Geom_BSplineCurve) aBSplineCurve;
130         Handle(Geom_Curve) aGeomCurve;
131         // fill array for algorithm by the received coordinates
132         int aLen = aPoints.size() / 2;
133         Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
134         std::vector<double>::const_iterator aListIter = aPoints.begin();
135         for (int ind = 1; ind <= aLen; ind++) {
136           gp_Pnt aPnt(gp::Origin());
137           aPnt.SetX(*aListIter);
138           aListIter++;
139           aPnt.SetY(*aListIter);
140           aListIter++;
141           aPnt.SetZ(0);
142           aHCurvePoints->SetValue(ind, aPnt);
143         }
144         // compute BSpline
145         GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
146         aGBC.Perform();
147         if (aGBC.IsDone()) {
148           aBSplineCurve = aGBC.Curve();
149         }
150         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
151
152         TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
153
154         Set( aWire );
155         AIS_Shape::Compute( thePM, thePrs, theMode );
156       }
157     }
158   }
159 }
160
161 void CurveCreator_AISCurve::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
162                                               const Standard_Integer theMode )
163 {
164   AIS_Shape::ComputeSelection( theSelection, theMode );
165 }