Salome HOME
Formatting.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_AISCurve.cxx
1 #include "HYDROGUI_AISCurve.h"
2
3 #include "CurveCreator_Curve.hxx"
4
5 #include <AIS_Point.hxx>
6 #include <AIS_Line.hxx>
7
8 #include <gp_Pnt.hxx>
9 #include <gp_Lin.hxx>
10
11 #include <Geom_CartesianPoint.hxx>
12
13 HYDROGUI_AISCurveSection::HYDROGUI_AISCurveSection( Handle_AIS_InteractiveContext theContext, 
14                                                    CurveCreator_Curve* theCurve, int theSection) :
15   myCurve(theCurve), mySection(theSection), myContext(theContext), myIsHL(false)
16 {
17   buildSection();
18 }
19
20 HYDROGUI_AISCurveSection::~HYDROGUI_AISCurveSection()
21 {
22   Erase();
23   for( int i = 0 ; i < myObjects.size() ; i++ ){
24     myObjects[i].Nullify();
25   }
26   myObjects.clear();
27 }
28
29 Quantity_Color HYDROGUI_AISCurveSection::getActiveColor()
30 {
31   if( myIsHL ){
32     return Quantity_Color( 1., 0., 0., Quantity_TOC_RGB );
33   }
34   return Quantity_Color( 0., 1., 0., Quantity_TOC_RGB );
35 }
36
37 void  HYDROGUI_AISCurveSection::highlight( bool isHL )
38 {
39   myIsHL = isHL;
40   Quantity_Color aColor = getActiveColor();
41   for( int i = 0 ; i < myObjects.size() ; i++ ){
42     myObjects[i]->SetColor(aColor);
43     myContext->Display(myObjects[i]);
44   }
45 }
46
47 void HYDROGUI_AISCurveSection::Display()
48 {
49   for( int i = 0 ; i < myObjects.size() ; i++ ){
50     myContext->Display(myObjects[i]);
51   }
52 }
53
54 void HYDROGUI_AISCurveSection::Erase()
55 {
56   for( int i = 0 ; i < myObjects.size() ; i++ ){
57     myContext->Erase(myObjects[i]);
58   }
59 }
60
61 void HYDROGUI_AISCurveSection::buildSection()
62 {
63   int aSectSize = myCurve->getNbPoints( mySection );
64   double anX;
65   double anY;
66   double aZ;
67   int i = 0; 
68   for( ; i < ( aSectSize - 1 ) ; i++ ){
69     Handle_AIS_Point anAISPnt = getAISPoint(i);
70     myObjects.push_back( anAISPnt );
71     Handle_AIS_Line aLine = getAISLine( i, i+1 );
72     myObjects.push_back( aLine );
73   }
74   if( aSectSize != 0 ){
75     Handle_AIS_Point anAISPnt = getAISPoint(i); 
76     myObjects.push_back( anAISPnt );
77     if( myCurve->isClosed(mySection) && ( aSectSize > 1 ) ){
78       Handle_AIS_Line aLine = getAISLine( i, 0 );
79       myObjects.push_back( aLine );
80     }
81   }
82 }
83
84 Handle_AIS_Point HYDROGUI_AISCurveSection::getAISPoint( int theIndx )
85 {
86   double anX;
87   double anY;
88   double aZ;
89   getPoint( theIndx, anX, anY, aZ );
90   gp_Pnt aPoint( anX, anY, aZ);
91
92   AIS_Point* aPnt = new AIS_Point( new Geom_CartesianPoint(aPoint));
93   return aPnt;
94 }
95
96 Handle_AIS_Line HYDROGUI_AISCurveSection::getAISLine( int theIndx1, int theIndx2 )
97 {
98   double anX;
99   double anY;
100   double aZ;
101   getPoint( theIndx1, anX, anY, aZ );
102   gp_Pnt aPoint1( anX, anY, aZ);
103
104   double anX2;
105   double anY2;
106   double aZ2;
107   getPoint( theIndx2, anX2, anY2, aZ2 );
108 //MTN to avoid crash during line construction
109   if( ( anX == anX2 ) && ( anY == anY2 ) && (aZ == aZ2 ) ){
110     aZ2 += 1e-7;
111   }
112
113   gp_Pnt aPoint2( anX2, anY2, aZ2 );
114
115   AIS_Line* aLine = new AIS_Line( new Geom_CartesianPoint(aPoint1), new Geom_CartesianPoint(aPoint2) );
116   return aLine;
117 }
118
119 void HYDROGUI_AISCurveSection::getPoint( int theIndx, double& theX, double& theY, double& theZ )
120 {
121   CurveCreator::Dimension aDim = myCurve->getDimension();
122   CurveCreator::Coordinates aCoords = myCurve->getCoordinates( mySection, theIndx );
123   theX = aCoords[0];
124   theY = aCoords[1];
125   theZ = 0.;
126   if( aDim == CurveCreator::Dim3d ){
127     theZ = aCoords[2];
128   }
129 }
130
131 /******************************* HYDROGUI_AISCurve ********************************************/
132 HYDROGUI_AISCurve::HYDROGUI_AISCurve( CurveCreator_Curve* theCurve, Handle_AIS_InteractiveContext theContext ) :
133   CurveCreator_Listener(), myCurve(theCurve), myContext( theContext )
134 {
135   myCurve->setListener(this);
136   buildCurve();
137 }
138
139 HYDROGUI_AISCurve::~HYDROGUI_AISCurve(void)
140 {
141 }
142
143 void HYDROGUI_AISCurve::setCurve( CurveCreator_Curve* theCurve )
144 {
145   myCurve = theCurve;
146   buildCurve();
147 }
148
149 void HYDROGUI_AISCurve::Display()
150 {
151   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){
152     myCurveRepresentation[i]->Display();
153   }
154 }
155
156 void HYDROGUI_AISCurve::buildCurve()
157 {
158   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){
159     myCurveRepresentation[i]->Erase();
160     delete myCurveRepresentation[i];
161   }
162   myCurveRepresentation.clear();
163
164   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
165     HYDROGUI_AISCurveSection* aSection = new HYDROGUI_AISCurveSection( myContext, myCurve, i);
166     myCurveRepresentation.push_back( aSection );
167     myCurveRepresentation[i]->Display();
168   }
169 }
170
171 void HYDROGUI_AISCurve::pointInserted( int theSection, int theIndx )
172 {
173   buildCurve();
174 }
175
176 void HYDROGUI_AISCurve::highlightSection( int theSection, bool isHL )
177 {
178   if( theSection >= myCurveRepresentation.size() )
179     return;
180   myCurveRepresentation[theSection]->highlight(isHL);
181 }