/
GraphicsSelection.cpp
299 lines (249 loc) · 8.9 KB
/
GraphicsSelection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "stdafx.h"
#pragma hdrstop
#include "GraphicView.h"
#include "structur.h"
#include "model.h"
#include "project.h"
#include "graphics/GridManager.h"
#include "User/message.h"
#include "main.h" // theApp
#include "MainFrm.h" // CMainFrame
#include "Units\Units.h"
#include "Dialog/DlgUtil.h" // IsContextHelpUp()
#include "Dialog/Dialogs.h"
#include "User/UserUtil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
bool CGraphicView::Get3DPointFromScreen( const CPoint& screenPt, CPoint3D& worldPt, float depth )
{
ASSERT_RETURN_ITEM( m_pDC != NULL, false );
// this function works for any screen point. If no depth value is specified then we return
// a world pt corresponding to the depth value of the pixel under the current mouse position
// otherwise a depth value between 0.0 and 1.0 can be specified where 0.0 corresponds to the near
// clip plane and 1.0 corresponds to the far clip plane.
//if( !wglGetCurrentContext() )
if( !wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC ) )
return false;
int viewport[4];
double modelview[16];
double projection[16];
float winX, winY, winZ;
double posX, posY, posZ;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)screenPt.x;
winY = (float)viewport[3] - (float)screenPt.y;
if( depth < 0. )
glReadPixels( screenPt.x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
else
{
ASSERT( depth >= 0. && depth <= 1.0 );
winZ = depth;
}
if( GL_FALSE == gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ) )
return false;
worldPt.x = posX;
worldPt.y = posY;
worldPt.z = posZ;
return true;
}
bool CGraphicView::GetXYPointForDXFTextExport( const CPoint& screenPt, CPoint3D& worldPt )
{
//get a ray that goes through screenPt from the front of the view frustum to the back of it
CPoint3D nearPt, farPt;
if( !Get3DPointFromScreen( screenPt, nearPt, 0.f ) )
return false;
if( !Get3DPointFromScreen( screenPt, farPt, 1.f ) )
return false;
CLine3D ray( nearPt, farPt );
//now find out where in 3D space the ray passes through the z=0 plane
CVector3D xyNormal( 0., 0., 1. );
CPoint3D ptOnXYPlane( 0., 0., 0. );
CPlane3D xyPlane( xyNormal, ptOnXYPlane );
double denom = xyNormal.dot( CVector3D( farPt, nearPt ) );
if( zero( denom ) ) return false;
double num = xyNormal.dot( CVector3D( ptOnXYPlane, nearPt ) );
double s = num/denom;
worldPt = ray.offset( s );
if( true/*is world point valid*/ )
return true;
else return false;
}
bool CGraphicView::GetSnapped3DPoint( const CPoint& screenPt, CPoint3D& worldPt )
{
// JL Note 2/15/2008 - not sure why we were return false when dragging a zoombox...
// zoombox will not draw if the code below is uncommented
if( /*Mouse( MOUSE_ZOOMBOX_DRAGGING ) ||*/ Mouse( MOUSE_MODEL_DRAGGING ) )
return FALSE;
//this function returns true and sets worldPt to the 3D coordinates of the closest grid point, node
//point or member point if a grid, node point or member is found in the selection area.
//If nothing is found then it does nothing to worldPt and returns false
// CTimeThisFunction timer( "GetSnapped3DPoint" );
CPoint point = screenPt;
bool bSuccess = false;
if( !m_hRC || !m_pDC )
{
TRACE( "Failed in CGraphicView::GetSnapped3DPoint\n" );
return bSuccess;
}
if( !wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC ) )
{
TRACE( "Failed in CGraphicView::GetSnapped3DPoint\n" );
return bSuccess;
}
// Create buffer to hold selection hits
//(must be done before entering selection mode)
// TeK Change 1/19/2009: We shouldn't be dynamically allocating memory on EVERY mouse-move!
// Changed to use a static buffer (twice as big!)
const int nBufSize = 4096*2;
static UINT selectBuf[nBufSize];
memset( selectBuf,0, sizeof(selectBuf));
double pickSize = double(ini.size().node*5);
m_Camera.StartMousePicking( point, CSize(pickSize, pickSize), nBufSize, selectBuf );
m_graphicsModel.SetWindowFilter( m_Filter );
m_graphicsModel.DrawMembersForSnapping( m_pDC );
CHitRecordCollection rRecCol;
m_Camera.EndMousePicking( rRecCol, selectBuf );
//first check for node and grid hits
if( m_graphicsModel.OverNodeOrGridPoint( screenPt, worldPt, m_Filter ) )
{
bSuccess = true;
}
CPoint screenSnapPt;
int screenDistanceToSnapPoint = 0;
if( GetScreenPtFrom3DPoint( worldPt, screenSnapPt ) )
{
screenDistanceToSnapPoint = max( abs( screenPt.x - screenSnapPt.x ), abs( screenPt.y - screenSnapPt.y ) );
}
//if we have snapped to a node or grid point and the distance between
//this point and the node or grid point is less than x pixels then we
//stay snapped to the node or grid point
//SHORT altState = GetAsyncKeyState( VK_MENU );
if( /*!(altState & 0x8000) && nHits > 0 &&*/ screenDistanceToSnapPoint > 8 )
{
if( const CMember* pM = m_graphicsModel.OverMember( rRecCol ) )
{
CPoint3D nearPt, farPt;
if( Get3DPointFromScreen( screenPt, nearPt, 0.f ) &&
Get3DPointFromScreen( screenPt, farPt, 1.f ) )
{
CLine3D ray( nearPt, farPt );
CPoint3D p1( pM->node(1)->x(), pM->node(1)->y(), pM->node(1)->z() );
CPoint3D p2( pM->node(2)->x(), pM->node(2)->y(), pM->node(2)->z() );
CLine3D memLine( p1, p2 );
CPoint3D pt3D = memLine.nearest_point( ray );
if( pt3D != CPoint3D::undefined_point && memLine.is_on_segment( pt3D ) )
{
worldPt = pt3D;
bSuccess = true;
}
else
bSuccess = false;
}
}
}
//delete[] selectBuf;
return bSuccess;
}
bool CGraphicView::GetScreenPtFrom3DPoint( const CPoint3D& modelSpacePt, CPoint& screenPt )
{
// this function works for any xyz point but usually we will just be returning the
// screen point for a grid point or node.
GLint viewport[4];
GLdouble mvMatrix[16], projMatrix[16];
double winX = 0.0;
double winY = 0.0;
double winZ = 0.0; //depth value between 0 and 1 - for depth sorting
bool bSuccess = false;
HGLRC currentRC = wglGetCurrentContext();
if( !currentRC )
{
TRACE( "CGraphicView::GetScreenPtFrom3DPoint Failed in GetCurrentContext\n" );
return bSuccess;
}
HDC hDC = wglGetCurrentDC( );
if( !hDC )
{
TRACE( "CGraphicView::GetScreenPtFrom3DPoint Failed in GetCurrentDC\n" );
return bSuccess;
}
//if( !wglMakeCurrent( m_pDC->GetSafeHdc(), m_hDC ) )
if( !wglMakeCurrent( hDC, currentRC ) )
{
TRACE( "CGraphicView::GetScreenPtFrom3DPoint Failed in MakeCurrent\n" );
return bSuccess;
}
glGetIntegerv( GL_VIEWPORT, viewport );
glGetDoublev( GL_MODELVIEW_MATRIX, mvMatrix );
glGetDoublev( GL_PROJECTION_MATRIX, projMatrix );
if( !gluProject( modelSpacePt.x, modelSpacePt.y, modelSpacePt.z,
mvMatrix, projMatrix, viewport,
&winX, &winY, &winZ ) )
{
//TRACE( "Failed in GetScreenPt #2\n" );
//wglMakeCurrent( NULL, NULL );
return bSuccess;
}
//openGL convention (origin at lower left)
screenPt.x = int(winX);
screenPt.y = int(winY);
//windows convention (origin at upper left)
screenPt.y = viewport[3] - viewport[1] - screenPt.y;
bSuccess = true;
return bSuccess;
}
bool CGraphicView::GetSnappedScreenPt( CPoint& point )
{
//this function gets the snapped screen point based on the current 3D point which
// will be snapped to the nearest grid point or node.
CPoint3D model3D;
if( !GetSnapped3DPoint( point, model3D ) )
return false;
GetScreenPtFrom3DPoint( model3D, point );
return true;
}
int CGraphicView::GetSelectionHitList( CPoint p, int selSizeX, int selSizeY, CHitRecordCollection& rRecordCollection )
{
if( !wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) )
return 0;
// Create buffer to hold selection hits
//(must be done before entering selection mode)
// TeK Change 1/19/2009: We shouldn't be dynamically allocating memory on EVERY mouse-move!
// Changed to use a static buffer (twice as big!)
const int nBufSize = 4096*2;
static UINT selectBuf[nBufSize];
memset( selectBuf,0, sizeof(selectBuf));
m_Camera.StartMousePicking( p, CSize(selSizeX, selSizeY), nBufSize, selectBuf );
m_graphicsModel.DrawModelForSelection( GetDC(), Mouse( MOUSE_SELECTIONBOX_DRAGGING ) );
/*glLoadName(1);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
glVertex3i(2, 0, 0);
glVertex3i(2, 6, 0);
glVertex3i(6, 6, 0);
glVertex3i(6, 0, 0);
glEnd();
glLoadName(2);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 1.0);
glVertex3i(3, 2, -1);
glVertex3i(3, 8, -1);
glVertex3i(8, 8, -1);
glVertex3i(8, 2, -1);
glEnd();
glLoadName(3);
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 1.0);
glVertex3i(0, 2, -2);
glVertex3i(0, 7, -2);
glVertex3i(5, 7, -2);
glVertex3i(5, 2, -2);
glEnd();*/
int nHits = m_Camera.EndMousePicking( rRecordCollection, selectBuf );
wglMakeCurrent( NULL, NULL );
return nHits;
}