Salome HOME
3acdd879b365036efe66dd0220b036427411dce3
[modules/gui.git] / src / GLViewer / GLViewer_Tools.h
1 //  Copyright (C) 2005 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 // File:      GLViewer_Tools.h
23 // Created:   April, 2005
24
25 #ifndef GLVIEWER_TOOLS_H
26 #define GLVIEWER_TOOLS_H
27
28 #ifdef WNT
29 #include "windows.h"
30 #endif
31
32 #include "GLViewer.h"
33 #include "GLViewer_Defs.h"
34
35 class GLViewer_LineField;
36
37 /****************************************************************************
38 **  Class:   GLViewer_Tools 
39 **  Descr:   Tools for Viewer
40 **  Module:  GLViewer
41 **  Created: UI team, 27.10.05
42 *****************************************************************************/
43 class GLVIEWER_API GLViewer_Tools
44 {
45 public:
46         //GLViewer_Tools();
47         //virtual ~GLViewer_Tools();
48   //static 
49
50 };
51
52 //! Dimension of line
53 enum FieldDim
54 {
55   FD_X = 0, /*along x axis*/
56   FD_Y      /*along y axis*/
57 };
58
59 /*!
60   Class GLViewer_LineList 
61   Tools for distinct line
62   This class implmented interface for segment operations:
63   add, cut, remove and etc.
64   Memory does not changed and allocated only one time
65 */
66 class GLViewer_LineList  
67 {
68 public:
69   GLViewer_LineList( int  );
70   virtual ~GLViewer_LineList();
71
72   //! Returns number of segments
73   int         count() const { return mySegmentNumber; }
74   //! Returns real size
75   int         size() const { return myRealSize; }
76   
77   bool        addSegment( double coord1, double coord2 );
78   bool        removeSegment( int index );
79   bool        removeSegment( double coord1, double coord2 );
80
81   bool        readSegment( int index, double& coord1, double& coord2 );
82
83   //! Returns index of segment, else -1
84   int         contains( double thePoint ) const;
85
86   //! Sets level of segments
87   void        setMainCoord( double theVal ) { myMainCoord = theVal; }
88   double      mainCoord() const { return myMainCoord; }
89
90   void        clear();
91   void        print();
92
93   void        show( FieldDim );
94   
95   GLViewer_LineList& operator = ( GLViewer_LineList );
96
97 private:
98   double*     myArray;
99   int         myRealSize;
100   int         mySegmentNumber;
101
102   double      myMainCoord;
103 };
104
105 /*! struct GraphNode describe node in algorithm on rare grid*/
106 struct GraphNode
107 {
108   int       myCount;
109   FieldDim  myDim;
110   int       myLineIndex;
111   int       mySegmentindex;
112   int       prevNodeIndex; //feedback for searching for solution
113 };
114
115 /*! struct SearchPoint describe node for solving algorithm*/
116 struct SearchPoint
117 {
118   int       myXLineIndex;
119   int       myXSegmentIndex;
120   int       myYLineIndex;
121   int       myYSegmentIndex;
122   int       mySolveIndex;
123 };
124
125 /*! Class  GLViewer_LineField 
126 * Tools for solving algorithm of finding shortest path on rare grid with minimum of 
127 * line turns number
128 */
129 class GLViewer_LineField
130 {
131 public:
132   //!Searched point
133   enum  FieldPoint
134   {
135     FP_Start = 0,
136     FP_End = 1
137   };
138
139   //! Status of interation
140   enum IterationStatus
141   {
142     IS_ERROR = 0,
143     IS_LOOP,
144     IS_NOT_SOLVED,
145     IS_SOLVED
146   };
147
148   //! Final status of solving
149   enum EndStatus
150   {
151     ES_ERROR = 0,
152     ES_LOOP,
153     ES_SOLVED
154   };
155
156   GLViewer_LineField();
157   GLViewer_LineField( const int theMAXSize, const int xn, const int yn );
158   virtual ~GLViewer_LineField();
159
160   //! Adds new line
161   /*!best way, if line is already sorted*/
162   void                addLine( FieldDim, GLViewer_LineList* );
163   //! Calls previous
164   void                addLine( FieldDim theDim, double theMC, double theBegin, double theEnd );
165   
166   //! Adds new line and sorted field
167   /*! Returns position*/
168   int                 insertLine( FieldDim theDim, GLViewer_LineList*, int thePosition );
169   //! Calls previous
170   int                 insertLine( FieldDim theDim, double theMC, double theBegin, double theEnd, int thePosition );
171
172   //! Returns other dimension
173   static FieldDim     invertDim( FieldDim );
174
175   //! Returns line by index and dimension
176   GLViewer_LineList*  getLine( int index, FieldDim );
177
178   //! Nullifys field and sets same continued segments
179   void                setBorders( double X1, double X2, double Y1, double Y2 );
180   //! Cut rectangle in grid
181   void                addRectangle( double top, double right, double bottom, double left );
182
183   //! returns arrey of intersects indexes with \param theLL
184   int*                intersectIndexes( FieldDim theDim, int theIndex, const GLViewer_LineList* theLL , int& theSize );
185
186   void                print();
187
188   void                show();  
189
190   int                 getDimSize( FieldDim );
191   //! Returns number of segment
192   int                 segmentNumber();
193
194   //! Sets start/end search point
195   bool                setPoint( FieldPoint, double x, double y );
196
197   //! Optimize field
198   /*! Removes all multiple segments*/
199   void                optimize();
200   //! Some prepare actions
201   /*! Needs call setPoint before*/
202   void                initialize();
203   //! Main method
204   EndStatus           startAlgorithm();
205
206   //! Returns solution and size of solution
207   double*             solution( int& size );
208
209 protected:
210   //! One iteration of algorithm
211   void                iteration();
212   //! Checks for complete status
213   IterationStatus     checkComplete();  
214
215   //! Finds LileList by counts and returns indexes
216   int*                findByCount( int& theParam );
217   //! Finds LileList by segment and dimension
218   int                 findBySegment( FieldDim, int coord1, int coord2, bool inCurArray = true );
219
220   //! Returns current solution array
221   GraphNode*          getCurArray();
222   //! Returns 
223   GraphNode*          getSecArray();
224
225   //! Returns maximum segment number
226   int                 maxSegmentNum();
227
228   //! Returns list of LileList by dimension
229   GLViewer_LineList** getLLArray( FieldDim );
230
231 private:
232   GLViewer_LineList**    myXLineArray,
233                    **    myYLineArray;
234
235   int           myXSize,
236                 myYSize;
237
238   GraphNode*    myGraphArray1,
239            *    myGraphArray2;
240   int           myCurArrayIndex;
241
242   SearchPoint   myStartPoint,
243                 myEndPoint;
244   int           myCurCount;
245 };
246
247 #endif //GLVIEWER_TOOLS_H