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