0012121: Optimization of existing selection classes
[occt.git] / src / OpenGl / OpenGl_transform_persistence.cxx
CommitLineData
7fd59977 1
2#include <OpenGl_transform_persistence.hxx>
3
4#include <OpenGl_tgl_all.hxx>
5
6#include <stddef.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h> /* pour M_PI */
10
11#include <GL/gl.h>
12#include <GL/glu.h>
13
14/* ABD 29/10/04 Transform Persistence of Presentation( pan, zoom, rotate ) */
15
16#include <OpenGl_cmn_varargs.hxx>
17#include <OpenGl_telem_attri.hxx>
18#include <OpenGl_tsm.hxx>
19#include <OpenGl_telem.hxx>
20#include <OpenGl_telem_inquire.hxx>
21#include <OpenGl_tsm_ws.hxx> /* pour TsmGetWSAttri */
22#include <OpenGl_telem_view.hxx> /* pour tel_view_data */
23
24
25#include <OpenGl_tgl_funcs.hxx>
26
27#include <OpenGl_Memory.hxx>
28
29
30/*----------------------------------------------------------------------*/
31/*
32* Type definitions
33*/
34
35typedef struct
36{
37 TEL_VIEW_REP vrep;
38#ifdef CAL_100498
39 Tmatrix3 inverse_matrix;/* accelerates UVN2XYZ conversion */
40#endif
41} TEL_VIEW_DATA, *tel_view_data; /* Internal data stored for every view rep */
42
43
44
45static TStatus TransformPersistenceDisplay( TSM_ELEM_DATA, Tint, cmn_key* );
46static TStatus TransformPersistenceAdd( TSM_ELEM_DATA, Tint, cmn_key* );
47static TStatus TransformPersistenceDelete( TSM_ELEM_DATA, Tint, cmn_key* );
48static TStatus TransformPersistencePrint( TSM_ELEM_DATA, Tint, cmn_key* );
49
50static TStatus (*MtdTbl[])( TSM_ELEM_DATA, Tint, cmn_key* ) =
51{
52 TransformPersistenceDisplay, /* PickTraverse */
53 TransformPersistenceDisplay,
54 TransformPersistenceAdd,
55 0, /*TransformPersistenceDelete,*/
56 TransformPersistencePrint,
57 0 /* Inquire */
58};
59
60
61MtblPtr
62TelTransformPersistentInitClass( TelType *el )
63{
64 *el = TelTransformPersistence;
65 return MtdTbl;
66}
67
68static TStatus
69TransformPersistenceAdd( TSM_ELEM_DATA d, Tint n, cmn_key *k )
70{
71 tel_transform_persistence data = new TEL_TRANSFORM_PERSISTENCE();
72
73 if( !data )
74 return TFailure;
75
76 *data = *(tel_transform_persistence)(k[0]->data.pdata);
77
78 ((tsm_elem_data)(d.pdata))->pdata = data;
79
80 return TSuccess;
81}
82
83
84static TStatus
85TransformPersistenceDisplay( TSM_ELEM_DATA data, Tint n, cmn_key *k )
86{
87 CMN_KEY key;
88
89 key.id = TelTransformPersistence;
90 key.data.pdata = data.pdata;
91 TsmSetAttri( 1, &key );
92 return TSuccess;
93}
94
95static TStatus
96TransformPersistenceDelete( TSM_ELEM_DATA data, Tint n, cmn_key *k )
97{
98 /* transform_persistence_end();*/
99 return TSuccess;
100}
101
102
103static TStatus
104TransformPersistencePrint( TSM_ELEM_DATA data, Tint n, cmn_key *k )
105{
106
107 return TSuccess;
108}
109
110/***********************************/
111static int isGenerated = 0;
112
113void transform_persistence_end(void)
114{
115 if( isGenerated == 1 )
116 /* restore matrix */
117 {
118 glMatrixMode (GL_PROJECTION);
119 glPopMatrix ();
120 glMatrixMode (GL_MODELVIEW);
121 glPopMatrix ();
122 isGenerated = 0;
123 }
124
125 /*printf( "Transform Persistence End: %i \n", isGenerated );*/
126}
127
128
129void transform_persistence_begin( int theFlag,
130 float theX,
131 float theY,
132 float theZ )
133{
134 CMN_KEY_DATA key;
135 tel_view_data vptr;
136 int i, j;
137 GLdouble modelMatrix[4][4];
138 GLdouble projMatrix[4][4];
139 GLdouble idenMatrix[4][4] = { {1.,0.,0.,0.}, {0.,1.,0.,0.}, {0.,0.,1.,0.},{0.,0.,0.,1.} };
140 GLint viewport[4];
141 double det2;
142
143 double W, H, W1, W2, H1, H2;
144 double winx, winy, winz;
145 double scaleX, scaleY, scaleZ;
146
147#ifdef PRINT1
148 printf( "Transform Persistence Mode: %i\n", theFlag );
149 printf( "Transform Persistence PointX: %f\n", theX );
150 printf( "Transform Persistence PointY: %f\n", theY );
151 printf( "Transform Persistence PointZ: %f\n", theZ );
152#endif /*PRINT1*/
153
154 if( theFlag == 0 )
155 {
156 transform_persistence_end();
157 return;
158 }
159
160 glGetIntegerv (GL_VIEWPORT, viewport);
161 glGetDoublev( GL_MODELVIEW_MATRIX, (GLdouble *) modelMatrix );
162 glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble *) projMatrix );
163
164 W = viewport[2];
165 H = viewport[3];
166
167 if( isGenerated == 0 )
168 isGenerated = 1;
169 else
170 {
171 /* restore matrix */
172 glMatrixMode (GL_PROJECTION);
173 glPopMatrix ();
174 glMatrixMode (GL_MODELVIEW);
175 glPopMatrix ();
176 }
177
178 glMatrixMode( GL_MODELVIEW );
179 glPushMatrix();
180 glLoadIdentity();
181
182 glMatrixMode( GL_PROJECTION );
183 glPushMatrix();
184 glLoadIdentity();
185
186 /*pre loading matrix*/
187 if( theFlag & TPF_PAN )
188 /* Annulate translate matrix */
189 {
190 modelMatrix[3][0] = 0.;
191 modelMatrix[3][1] = 0.;
192 modelMatrix[3][2] = 0.;
193 projMatrix[3][0] = 0.;
194 projMatrix[3][1] = 0.;
195 projMatrix[3][2] = 0.;
196 }
197
198 if( theFlag & TPF_ZOOM )
199 /* Annulate zoom matrix */
200 {
201 if( W > H )
202 det2 = 0.002 / projMatrix[1][1];
203 else
204 det2 = 0.002 / projMatrix[0][0];
205
206 TsmGetWSAttri( TglActiveWs, WSViews, &key );
207 vptr = (tel_view_data)key.pdata ; /* Obtain defined view data*/
208 scaleX = vptr->vrep.extra.scaleFactors[0];
209 scaleY = vptr->vrep.extra.scaleFactors[1];
210 scaleZ = vptr->vrep.extra.scaleFactors[2];
211
212 for( i = 0; i < 3; i++ )
213 {
214 for( j = 0; j < 3; j++ )
215 {
216 if(i == 0)
217 modelMatrix[i][j] /= scaleX;
218 if(i == 1)
219 modelMatrix[i][j] /= scaleY;
220 if(i == 2)
221 modelMatrix[i][j] /= scaleZ;
222 }
223 }
224 for( i = 0 ; i < 2; i++ )
225 projMatrix[i][i] *= det2;
226 }
227
228 if( theFlag & TPF_ROTATE )
229 /* Annulate rotate matrix */
230 {
231 modelMatrix[0][0] = 1.;
232 modelMatrix[1][1] = 1.;
233 modelMatrix[2][2] = 1.;
234
235 modelMatrix[1][0] = 0.;
236 modelMatrix[2][0] = 0.;
237 modelMatrix[0][1] = 0.;
238 modelMatrix[2][1] = 0.;
239 modelMatrix[0][2] = 0.;
240 modelMatrix[1][2] = 0.;
241 }
242 else if( theFlag & TPF_RELATIVE_ROTATE )
243 /* Initialize relative rotate matrix*/
244 {
245 modelMatrix[3][0] = 0.;
246 modelMatrix[3][1] = 0.;
247 modelMatrix[3][2] = 0.;
248
249 glMatrixMode( GL_MODELVIEW );
250 glLoadIdentity();
251 glTranslated( theX, theY, theZ );
252 }
253
254 if( theFlag == TPF_TRIEDRON )
255 {
256 /* Annulate translation matrix */
257 modelMatrix[3][0] = 0.;
258 modelMatrix[3][1] = 0.;
259 modelMatrix[3][2] = 0.;
260
261 projMatrix[3][0] = 0.;
262 projMatrix[3][1] = 0.;
263 projMatrix[3][2] = 0.;
264
265 if( W > H )
266 det2 = 0.002 / projMatrix[1][1];
267 else
268 det2 = 0.002 / projMatrix[0][0];
269
270 for( i = 0 ; i < 2; i++ )
271 projMatrix[i][i] *= det2;
272 }
273
274 /* load matrix */
275 glMatrixMode (GL_MODELVIEW);
276 glMultMatrixd( (GLdouble *) modelMatrix);
277
278 glMatrixMode ( GL_PROJECTION );
279 glMultMatrixd( (GLdouble *) projMatrix);
280
281
282 /*post loading matrix*/
283 if( theFlag == TPF_TRIEDRON )
284 {
285 glMatrixMode( GL_PROJECTION );
286
287 gluUnProject( W/2, H/2., 0, (GLdouble*)idenMatrix, (GLdouble*)projMatrix, (GLint*)viewport, &winx, &winy, &winz);
288 W1 = winx;
289 H1 = winy;
290 gluUnProject( -W/2, -H/2., 0, (GLdouble*)idenMatrix, (GLdouble*)projMatrix, (GLint*)viewport, &winx, &winy, &winz);
291 W2 = winx;
292 H2 = winy;
293
294 if( theX == 0. && theY == 0. )
295 {
296 /*center*/
297 }
298 else if( theX > 0. && theY > 0. )
299 {
300 /*right upper*/
301 glTranslated( (W1 - W2)/2. - theZ/2., (H1-H2)/2. - theZ/2., 0. );
302 }
303 else if( theX > 0. && theY < 0. )
304 {
305 /*right lower*/
306 glTranslated( (W1 - W2)/2. - theZ/2., -(H1-H2)/2. + theZ/2., 0. );
307 }
308 else if( theX < 0. && theY > 0. )
309 {
310 /*left upper*/
311 glTranslated( -(W1 - W2)/2. + theZ/2., (H1-H2)/2. - theZ/2., 0. );
312 }
313 else if( theX < 0 && theY < 0 )
314 {
315 /*left lower*/
316 glTranslated( -(W1 - W2)/2. + theZ/2., -(H1-H2)/2. + theZ/2., 0. );
317 }
318 }
319}