| 1 | /* |
| 2 | ---------------------------------------------------------------------------- |
| 3 | "THE BEER-WARE LICENSE" (Revision 42): |
| 4 | <[email protected]> wrote this file. As long as you retain |
| 5 | this notice you can do whatever you want with this stuff. If we meet some day, |
| 6 | and you think this stuff is worth it, you can buy me a beer in return. |
| 7 | Daniel Kratzert |
| 8 | ---------------------------------------------------------------------------- |
| 9 | */ |
| 10 | |
| 11 | #include "dsrglwindow.h" |
| 12 | #include "molecule.h" |
| 13 | #include "dsrgui.h" |
| 14 | #include "glureplace.h" |
| 15 | |
| 16 | |
| 17 | DSRGlWindow::DSRGlWindow(QWidget *parent, Molecule *m, |
| 18 | DSRMol header, QString fragment) { |
| 19 | this->setParent(parent); |
| 20 | m_molecule=m; |
| 21 | showFit=NULL; |
| 22 | showFitLabel=NULL; |
| 23 | /* |
| 24 | m_molecule->cell.symmops.clear(); |
| 25 | m_molecule->cell.trans.clear(); |
| 26 | m_molecule->asymm.clear(); |
| 27 | m_molecule->sdm.clear(); |
| 28 | m_molecule->showbonds.clear(); |
| 29 | m_molecule->showatoms.clear(); |
| 30 | m_molecule->selectedatoms.clear(); |
| 31 | m_molecule->envi_sdm.clear(); |
| 32 | m_molecule->contact.clear(); |
| 33 | m_molecule->envi_sdm.clear(); |
| 34 | m_molecule->contact.clear(); |
| 35 | m_molecule->symmopsEQIV.clear(); |
| 36 | m_molecule->labelEQIV.clear(); |
| 37 | m_molecule->transEQIV.clear(); |
| 38 | m_molecule->freeatoms.clear(); |
| 39 | m_molecule->bindatoms.clear(); |
| 40 | m_molecule->selectedatoms.clear(); |
| 41 | m_molecule->showatoms.clear(); |
| 42 | V3 nl(0,0,0); |
| 43 | m_molecule->cell.trans.append(nl); |
| 44 | m_molecule->cell.symmops.append(Matrix(1,0,0, 0,1,0, 0,0,1)); |
| 45 | m_molecule->pmin=10000; |
| 46 | m_molecule->pmax=-10000; |
| 47 | // initial zoom level: |
| 48 | // The Zom will later be adapted to fragment size. |
| 49 | m_molecule->transEQIV.clear(); |
| 50 | m_molecule->symmopsEQIV.clear(); |
| 51 | m_molecule->labelEQIV.clear(); |
| 52 | m_molecule->freeatoms.clear(); |
| 53 | m_molecule->bindatoms.clear(); |
| 54 | m_molecule->envi_sdm.clear(); |
| 55 | */ |
| 56 | L=50.0; |
| 57 | m_molecule->LOD=3; |
| 58 | labScal=0.4; |
| 59 | dsrLabelColor = QColor("white"); |
| 60 | if (fragment.size() > 0) { |
| 61 | display_fragment(header); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | DSRGlWindow::~DSRGlWindow() { |
| 66 | m_molecule->g_Program->release(); |
| 67 | m_molecule->g_Program->deleteLater(); |
| 68 | } |
| 69 | |
| 70 | static inline void transform_point(GLdouble out[4], const GLdouble m[16], const GLdouble in[4]) { |
| 71 | #define M(row,col) m[col*4+row] |
| 72 | out[0] = |
| 73 | M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3]; |
| 74 | out[1] = |
| 75 | M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3]; |
| 76 | out[2] = |
| 77 | M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3]; |
| 78 | out[3] = |
| 79 | M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3]; |
| 80 | #undef M |
| 81 | } |
| 82 | |
| 83 | static inline bool posTo2D(V3 obj, |
| 84 | const GLdouble model[16], const GLdouble proj[16], |
| 85 | const GLint viewport[4], |
| 86 | GLdouble * winx, GLdouble * winy,double retina = 1.0) { |
| 87 | GLdouble in[4], out[4]; |
| 88 | |
| 89 | in[0] = obj.x; |
| 90 | in[1] = obj.y; |
| 91 | in[2] = obj.z; |
| 92 | in[3] = 1.0; |
| 93 | transform_point(out, model, in); |
| 94 | transform_point(in, proj, out); |
| 95 | |
| 96 | if (in[3] == 0.0) return false; |
| 97 | |
| 98 | in[0] /= in[3]; |
| 99 | in[1] /= in[3]; |
| 100 | in[2] /= in[3]; |
| 101 | |
| 102 | *winx = viewport[0] + (1 + in[0]) * viewport[2] / 2; |
| 103 | *winy = viewport[1] + (1 - in[1]) * viewport[3] / 2; |
| 104 | *winx /= retina; |
| 105 | *winy /= retina; |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void DSRGlWindow::project(double x, double y, double z, GLdouble &tx, GLdouble &ty){ |
| 110 | makeCurrent(); |
| 111 | GLdouble model[16]; |
| 112 | GLdouble proj[16]; |
| 113 | GLint viewport[4]; |
| 114 | glGetIntegerv(GL_VIEWPORT, viewport); |
| 115 | glGetDoublev( GL_PROJECTION_MATRIX , (double*)proj ); |
| 116 | glGetDoublev( GL_MODELVIEW_MATRIX, (double*)model ); |
| 117 | V3 pos(x,y,z); |
| 118 | posTo2D(pos, model, proj, viewport, &tx, &ty, retinafktr); |
| 119 | } |
| 120 | |
| 121 | void DSRGlWindow::renderText(double x, double y, double z, const QString &text){ |
| 122 | // Identify x and y locations to render text within widget |
| 123 | GLdouble textPosX = 0.0, textPosY = 0.0; |
| 124 | project(x, y, z, textPosX, textPosY); |
| 125 | //printf("%f %f %f -> %f %f\n",x,y,z,textPosX,textPosY); |
| 126 | // Render text |
| 127 | QPainter painter(this); |
| 128 | QFontMetrics mtr(myFont); |
| 129 | QRect rc=mtr.boundingRect(text+"L"); |
| 130 | QImage image(rc.width(),rc.height(), QImage::Format_ARGB32_Premultiplied); |
| 131 | image.fill(0); |
| 132 | QPainter p; |
| 133 | p.begin(&image); |
| 134 | p.setPen(dsrLabelColor); |
| 135 | p.setBackground(Qt::NoBrush); |
| 136 | p.setFont(myFont); |
| 137 | p.drawText(image.rect(), Qt::AlignCenter, text); |
| 138 | p.end(); |
| 139 | painter.drawImage(textPosX, textPosY-rc.height(), image); |
| 140 | painter.end(); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void DSRGlWindow::draw(){ |
| 145 | m_molecule->dratom = 0; |
| 146 | glPushMatrix(); |
| 147 | glScaled( L, L, L ); |
| 148 | GLdouble model[16]; |
| 149 | GLdouble proj[16]; |
| 150 | GLint viewport[4]; |
| 151 | glGetIntegerv(GL_VIEWPORT, viewport); |
| 152 | viewport[2]/=retinafktr; |
| 153 | viewport[3]/=retinafktr; |
| 154 | glGetDoublev( GL_PROJECTION_MATRIX, (double*)proj ); |
| 155 | glGetDoublev( GL_MODELVIEW_MATRIX, (double*)model ); |
| 156 | /*printf("The Mmatrix is:\n%9.6f %9.6f %9.6f %9.6f\n%9.6f %9.6f %9.6f %9.6f\n%9.6f %9.6f %9.6f %9.6f\n%9.6f %9.6f %9.6f %9.6f\n", |
| 157 | model[0],model[1],model[2],model[3], |
| 158 | model[4],model[5],model[6],model[7], |
| 159 | model[8],model[9],model[10],model[11], |
| 160 | model[12],model[13],model[14],model[15]); |
| 161 | GLdouble det= |
| 162 | model[0]*model[5]*model[10] - model[8]*model[5]*model[2]+ |
| 163 | model[1]*model[6]*model[8] - model[9]*model[6]*model[0]+ |
| 164 | model[2]*model[4]*model[9] - model[10]*model[4]*model[1]; |
| 165 | printf("die Determinante der Drehmatrix ist: %g\n",det); |
| 166 | printf("Die Pmatrix ist:\n%9.6f %9.6f %9.6f %9.6f\n%9.6f %9.6f %9.6f %9.6f\n%9.6f %9.6f %9.6f %9.6f\n%9.6f %9.6f %9.6f %9.6f\n", |
| 167 | proj[0],proj[1],proj[2],proj[3], |
| 168 | proj[4],proj[5],proj[6],proj[7], |
| 169 | proj[8],proj[9],proj[10],proj[11], |
| 170 | proj[12],proj[13],proj[14],proj[15]); |
| 171 | det= |
| 172 | proj[0]*proj[5]*proj[10] - proj[8]*proj[5]*proj[2]+ |
| 173 | proj[1]*proj[6]*proj[8] - proj[9]*proj[6]*proj[0]+ |
| 174 | proj[2]*proj[4]*proj[9] - proj[10]*proj[4]*proj[1]; |
| 175 | printf("die Determinante der Drehmatrix ist: %g\n",det);*/ |
| 176 | m_molecule->adp = 0; |
| 177 | m_molecule->intern = 0; |
| 178 | m_molecule->tubes = 0; |
| 179 | glDisable(GL_BLEND); |
| 180 | glEnable(GL_DEPTH_TEST ); |
| 181 | glDepthFunc(GL_LEQUAL); |
| 182 | m_molecule->atoms(xd, 50, 0.5);//smaller atoms |
| 183 | m_molecule->bonds(bonds,0.5); |
| 184 | glEnable(GL_COLOR_MATERIAL); |
| 185 | qglColor(dsrLabelColor); // The fragment label color |
| 186 | // glClear( GL_DEPTH_BUFFER_BIT); |
| 187 | for (int i=0; i<xd.size(); i++){ |
| 188 | GLdouble in[4], out[4]; |
| 189 | in[0] = xd.at(i).pos.x; |
| 190 | in[1] = xd.at(i).pos.y; |
| 191 | in[2] = xd.at(i).pos.z; |
| 192 | in[3] = 1.0; |
| 193 | transform_point(out, model, in); |
| 194 | posTo2D(xd.at(i).pos,model,proj,viewport, &xd[i].screenX, &xd[i].screenY,1.0); |
| 195 | bool issel=false; |
| 196 | for (int j=0; j<selFragAt.size(); j++) { |
| 197 | if ((showFitLabel!=nullptr) |
| 198 | && (!showFitLabel->isChecked()) |
| 199 | && (j<m_molecule->selectedatoms.size()) |
| 200 | && (xd.at(i).Label==selFragAt.at(j).Label)) { |
| 201 | issel=true; |
| 202 | renderText( xd.at(i).pos.x, xd.at(i).pos.y, xd.at(i).pos.z, |
| 203 | xd.at(i).Label+"->"+m_molecule->selectedatoms.at(j).Label); |
| 204 | } |
| 205 | } |
| 206 | if (!issel) { |
| 207 | renderText( xd.at(i).pos.x, xd.at(i).pos.y, xd.at(i).pos.z, |
| 208 | xd.at(i).Label); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | /* for (int i=0; i<xd.size(); i++){ |
| 215 | GLdouble in[4], out[4]; |
| 216 | in[0] = xd.at(i).pos.x; |
| 217 | in[1] = xd.at(i).pos.y; |
| 218 | in[2] = xd.at(i).pos.z; |
| 219 | in[3] = 1.0; |
| 220 | transform_point(out, model, in); |
| 221 | posTo2D(xd.at(i).pos,model,proj,viewport, &xd[i].screenX, &xd[i].screenY, retinafktr); |
| 222 | }*/ |
| 223 | glPopMatrix(); |
| 224 | if (!selFragAt.isEmpty()){ |
| 225 | glPushMatrix();{ |
| 226 | glScaled( L, L, L ); |
| 227 | m_molecule->tubes=0; |
| 228 | m_molecule->intern=1; |
| 229 | m_molecule->adp=0; |
| 230 | m_molecule->dratom=1; |
| 231 | m_molecule->atoms(selFragAt); |
| 232 | m_molecule->dratom=0; |
| 233 | glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
| 234 | }glPopMatrix(); |
| 235 | } |
| 236 | if ((showFit!=nullptr)&&(!fita.isEmpty())&&(showFit->isChecked())){ |
| 237 | glPushMatrix();{ |
| 238 | glEnable(GL_BLEND); |
| 239 | glScaled( L, L, L ); |
| 240 | m_molecule->tubes=0; |
| 241 | m_molecule->intern=0; |
| 242 | m_molecule->adp=0; |
| 243 | m_molecule->dratom=5; |
| 244 | m_molecule->atoms(fita); |
| 245 | m_molecule->dbond(fitabonds); |
| 246 | m_molecule->lbond(); |
| 247 | if (showFitLabel->isChecked()){ |
| 248 | qglColor(QColor(0, 0, 175)); // The surrounding atoms label color |
| 249 | for (int i=0; i<fita.size(); i++){ |
| 250 | renderText( fita.at(i).pos.x, fita.at(i).pos.y, fita.at(i).pos.z, fita.at(i).Label); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | }glPopMatrix(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void DSRGlWindow::initializeGL(){ |
| 259 | glEnable(GL_LINE_SMOOTH); |
| 260 | glHint(GL_LINE_SMOOTH_HINT,GL_NICEST); |
| 261 | //glEnable(GL_POLYGON_SMOOTH); |
| 262 | myFont = QFont("Arial", 12, -1, false); // the initial label font size |
| 263 | const GLfloat position[] = {100.0f, 100.0f,100.0f,0.0f}; |
| 264 | const GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 265 | const GLfloat specular[] = { 1.0f, 0.9f, 0.9f, 1.0f }; |
| 266 | const GLfloat ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f }; |
| 267 | glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 ); |
| 268 | glLightfv( GL_LIGHT0, GL_POSITION, position ); |
| 269 | glLightfv( GL_LIGHT0, GL_AMBIENT, ambient ); |
| 270 | glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse ); |
| 271 | glLightfv( GL_LIGHT0, GL_SPECULAR, specular ); |
| 272 | glLightfv( GL_LIGHT1, GL_POSITION, position ); |
| 273 | glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse ); |
| 274 | glLightfv( GL_LIGHT1, GL_AMBIENT, ambient ); |
| 275 | glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse ); |
| 276 | glEnable( GL_LIGHTING ); |
| 277 | glEnable( GL_LIGHT0 ); |
| 278 | // glEnable( GL_BLEND); |
| 279 | glDisable(GL_BLEND); |
| 280 | glAlphaFunc ( GL_GREATER, 0.01f ) ; |
| 281 | //glEnable(GL_ALPHA_TEST); |
| 282 | glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ; |
| 283 | const GLfloat OBJ_SPE[] = { 1.0, 1.0, 1.0, 1.0 }; |
| 284 | const GLfloat OBJ_SHIN = 127.0; |
| 285 | glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, OBJ_SPE ); |
| 286 | glEnable ( GL_COLOR_MATERIAL ) ; |
| 287 | glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ; |
| 288 | glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, OBJ_SHIN ); |
| 289 | glShadeModel( GL_SMOOTH ); |
| 290 | glEnable(GL_NORMALIZE); |
| 291 | glClearColor(0.4f,0.4f,0.4f,1.0f); |
| 292 | glEnable(GL_DEPTH_TEST ); |
| 293 | glDepthFunc(GL_LEQUAL); |
| 294 | gluLookAt_(0.0, 200, 50 , 0.0, 0.0, 0.0, 0.0, -100, 400 ); |
| 295 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
| 296 | |
| 297 | makeCurrent(); |
| 298 | m_molecule->g_Program=new QOpenGLShaderProgram(this); |
| 299 | m_molecule->g_program=m_molecule->installShader(); |
| 300 | GLenum err = GL_NO_ERROR; |
| 301 | int ect=0; |
| 302 | printf("DSR shaderprog %d context is valid = %d \n",m_molecule->g_program,context()->isValid()); |
| 303 | if (!m_molecule->g_program) printf("%s\n",m_molecule->g_Program->log().toStdString().c_str()); |
| 304 | while((err = glGetError()) != GL_NO_ERROR){ |
| 305 | printf("in DSR: GL_INVALID_VALUE%d GL_INVALID_OPERATION%d [%d] err=%x\n",err==GL_INVALID_VALUE ,err == GL_INVALID_OPERATION,++ect,err); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void DSRGlWindow::resizeGL(int width, int height){ |
| 310 | retinafktr=devicePixelRatio();// it qt5 this is in in qt6 its qreal!! |
| 311 | printf("retinafktr=%f\n",retinafktr); |
| 312 | wh=height*retinafktr; |
| 313 | ww=width*retinafktr; |
| 314 | glViewport(0, 0, ww, wh); |
| 315 | glGetIntegerv(GL_VIEWPORT, vp); |
| 316 | printf("VP %d %d %d %d w = %d h = %d width = %d height = %d retina factor = %f devicePixelRatio() = %f\n",vp[0],vp[1],vp[2],vp[3],ww,wh, |
| 317 | this->width(),this->height(),retinafktr,devicePixelRatio()); |
| 318 | glMatrixMode(GL_PROJECTION); |
| 319 | glLoadIdentity(); |
| 320 | gluPerspective_( 29.0, (double)width/height, 5.0, 8000.0 ); |
| 321 | } |
| 322 | |
| 323 | QSize DSRGlWindow::minimumSizeHint() const |
| 324 | { |
| 325 | return QSize(250, 250); |
| 326 | } |
| 327 | |
| 328 | QSize DSRGlWindow::sizeHint() const |
| 329 | { |
| 330 | return QSize(400, 400); |
| 331 | } |
| 332 | |
| 333 | void DSRGlWindow::paintGL(){ |
| 334 | glClearColor(0.6f,0.6f,0.6f,1.0f); // The background color |
| 335 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 336 | glViewport(0, 0, ww, wh); |
| 337 | glGetIntegerv(GL_VIEWPORT, vp); |
| 338 | glMatrixMode(GL_PROJECTION); |
| 339 | glLoadIdentity(); |
| 340 | gluPerspective_( 29.0, (double)ww/wh, 5.0, 8000.0 ); |
| 341 | glMatrixMode(GL_MODELVIEW); |
| 342 | glPushMatrix(); |
| 343 | draw(); |
| 344 | glPopMatrix(); |
| 345 | } |
| 346 | |
| 347 | void DSRGlWindow::clear_molecule() |
| 348 | { // clear the 3D view |
| 349 | xd.clear(); |
| 350 | bonds.clear(); |
| 351 | fita.clear(); |
| 352 | fitabonds.clear(); |
| 353 | selFragAt.clear(); |
| 354 | source_atoms.clear(); |
| 355 | update(); |
| 356 | } |
| 357 | |
| 358 | void DSRGlWindow::display_fragment(DSRMol frag, bool clear_sel) |
| 359 | { //! uses a QStringlist of atoms from DSR and fills |
| 360 | //! them into a MyAtom. MyAtom is then drawn in OpenGL |
| 361 | //! @param The fragment from DSR |
| 362 | //! @param clear_sel if enabled, the selected atoms are cleared |
| 363 | //! before drawing the molecule |
| 364 | V3 mid = V3(0,0,0); |
| 365 | MyAtom newAtom; |
| 366 | newAtom.part = 0; |
| 367 | newAtom.resiNr = 0; |
| 368 | newAtom.hidden = 0; |
| 369 | newAtom.symmGroup = 0; |
| 370 | newAtom.afix = 0; |
| 371 | xd.clear(); |
| 372 | bonds.clear(); |
| 373 | fita.clear(); |
| 374 | fitabonds.clear(); |
| 375 | // Do not clear the selection during invert: |
| 376 | if (clear_sel) { |
| 377 | selFragAt.clear(); |
| 378 | } |
| 379 | if (frag.atoms.isEmpty()) { |
| 380 | return; |
| 381 | } |
| 382 | foreach (QStringList atom, frag.atoms) { |
| 383 | if (atom.length() < 4) continue; // Do not crash on empty string |
| 384 | newAtom.Label = atom.at(0); |
| 385 | newAtom.an = abs(atom.at(1).toInt())-1; |
| 386 | newAtom.pos.x = atom.at(2).toDouble(); |
| 387 | newAtom.pos.y = atom.at(3).toDouble(); |
| 388 | newAtom.pos.z = atom.at(4).toDouble(); |
| 389 | mid += newAtom.pos; |
| 390 | //qDebug()<< newAtom.Label << newAtom.an << newAtom.pos.x << newAtom.pos.y << newAtom.pos.z; |
| 391 | xd.append(newAtom); |
| 392 | } |
| 393 | mid *= -1.0/xd.size(); |
| 394 | for (int i=0; i<xd.size(); i++){ |
| 395 | xd[i].pos += mid; |
| 396 | } |
| 397 | L = 90.0/m_molecule->dimension(xd); // Zoom level of the molecule inside the widget |
| 398 | bonds=m_molecule->connecting(xd,true); |
| 399 | update(); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void DSRGlWindow::set_label_color() { |
| 404 | //! Sets the label color. The default color was not visible |
| 405 | //! for white or yellow atoms. |
| 406 | QColor lc= QColorDialog::getColor(QColor(6, 167, 222), this); |
| 407 | if (lc.isValid()) { |
| 408 | dsrLabelColor=lc; |
| 409 | } else { |
| 410 | dsrLabelColor = QColor("white"); |
| 411 | } |
| 412 | update(); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void DSRGlWindow::mousePressEvent(QMouseEvent *event){ |
| 417 | //! Handles the mousePressEvents in the 3D view. |
| 418 | //! Atoms are selectable if showFit is not NULL. |
| 419 | //! New atoms will be selected if the mousePressEvent is inside the |
| 420 | //! size of the respective atom and less than three atoms. |
| 421 | //! With more than three atoms, the first selection will be removed. |
| 422 | moux=event->pos().x(); |
| 423 | mouy=event->pos().y(); |
| 424 | if((event->buttons() == Qt::RightButton) && (event->modifiers() == Qt::ControlModifier)){ |
| 425 | QMenu *menu = new QMenu(""); |
| 426 | menu->addAction(QString("Set label color"), this, SLOT(set_label_color())); |
| 427 | menu->exec(QCursor::pos()); |
| 428 | } |
| 429 | if (event->buttons() == Qt::LeftButton) { |
| 430 | // Has to be after moux/mouy, otherwise zoom is strange: |
| 431 | if (showFit == NULL) return; // <-prevents selection of atoms in edit window |
| 432 | double nahda=200.0, da=0; |
| 433 | int nahdai=-1; |
| 434 | for (int j=0; j<xd.size();j++){ |
| 435 | if (xd.at(j).hidden) continue; |
| 436 | da=(((xd.at(j).screenX-event->pos().x())*( xd.at(j).screenX-event->pos().x()))+ |
| 437 | ((xd.at(j).screenY-event->pos().y())*( xd.at(j).screenY-event->pos().y()))); |
| 438 | nahdai=(da<nahda)?j:nahdai; |
| 439 | nahda=qMin(nahda,da); |
| 440 | } |
| 441 | if ((nahdai >- 1) && (nahdai < xd.size())){ |
| 442 | // This might be a good idea in the first place, but results in wired behavior: |
| 443 | //if (selFragAt.contains(xd[nahdai])) { |
| 444 | // return; |
| 445 | //} |
| 446 | selFragAt.append(xd[nahdai]); |
| 447 | if (selFragAt.size() > 3) selFragAt.removeFirst(); |
| 448 | update(); |
| 449 | if (selFragAt.size() == 3) { |
| 450 | source_atoms = QString("%1 %2 %3") |
| 451 | .arg(selFragAt.at(0).Label) |
| 452 | .arg(selFragAt.at(1).Label) |
| 453 | .arg(selFragAt.at(2).Label); |
| 454 | emit sourceStringChanged(); |
| 455 | } |
| 456 | } |
| 457 | makeInfo(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void DSRGlWindow::makeInfo(){ |
| 462 | //! makes an info box that shows the fragment fit distances |
| 463 | double f12dis=0, f13dis=0, t12dis=0, t13dis=0; |
| 464 | if (selFragAt.size()>1) f12dis=sqrt(Distance(selFragAt.at(0).pos,selFragAt.at(1).pos)); |
| 465 | if (selFragAt.size()>2) f13dis=sqrt(Distance(selFragAt.at(0).pos,selFragAt.at(2).pos)); |
| 466 | if (m_molecule->selectedatoms.size()>1) t12dis=sqrt(Distance(m_molecule->selectedatoms.at(0).pos,m_molecule->selectedatoms.at(1).pos)); |
| 467 | if (m_molecule->selectedatoms.size()>2) t13dis=sqrt(Distance(m_molecule->selectedatoms.at(0).pos,m_molecule->selectedatoms.at(2).pos)); |
| 468 | emit updateInfo(QString( |
| 469 | "<table>" |
| 470 | "<tr><th colspan=\"2\" align=\"left\">fragment: </th>" |
| 471 | "<th><th>" |
| 472 | " <th colspan=\"2\" align=\"left\">target:</th>" |
| 473 | "</tr>" |
| 474 | "<tr><td colspan=\"2\"><font color=blue>%1</font></td>" |
| 475 | "<td><td>" |
| 476 | " <td colspan=\"2\"><font color=blue>%2</font></td>" |
| 477 | "</tr>" |
| 478 | "<tr><td align=\"left\"><font color=blue>%3</font></td> " |
| 479 | " <td align=\"left\">1,2: <b>%7 Å</b></td>" |
| 480 | "<td><td>" |
| 481 | " <td align=\"left\"><font color=blue>%4 </font></td>" |
| 482 | " <td align=\"right\">1,2: <b>%9 Å</b></td>" |
| 483 | "</tr>" |
| 484 | "<tr><td align=\"left\"><font color=blue>%5</font></td>" |
| 485 | " <td align=\"left\">1,3: <b>%8 Å</b></td>" |
| 486 | "<td><td>" |
| 487 | " <td align=\"right\"><font color=blue>%6 </font></td>" |
| 488 | " <td align=\"right\">1,3: <b>%10 Å<b></td>" |
| 489 | "</tr>" |
| 490 | "</table>") |
| 491 | .arg((selFragAt.size()>0)?selFragAt.at(0).Label:"") |
| 492 | .arg((m_molecule->selectedatoms.size()>0)?m_molecule->selectedatoms.at(0).Label:"") |
| 493 | .arg((selFragAt.size()>1)?selFragAt.at(1).Label:"") |
| 494 | .arg((m_molecule->selectedatoms.size()>1)?m_molecule->selectedatoms.at(1).Label:"") |
| 495 | .arg((selFragAt.size()>2)?selFragAt.at(2).Label:"") |
| 496 | .arg((m_molecule->selectedatoms.size()>2)?m_molecule->selectedatoms.at(2).Label:"") |
| 497 | .arg(f12dis, 4, 'f', 2) |
| 498 | .arg(f13dis, 4, 'f', 2) |
| 499 | .arg(t12dis, 4, 'f', 2) |
| 500 | .arg(t13dis, 4, 'f', 2) |
| 501 | ); |
| 502 | tryfit(); |
| 503 | } |
| 504 | |
| 505 | //void glRotateL(double ang, double x, double y, double z); |
| 506 | inline void DSRGlWindow::__RotateCS( double c, double s, double& X, double& Y ) { |
| 507 | double T = X; |
| 508 | X = c*X - s*Y; |
| 509 | Y = s*T + c*Y; |
| 510 | } |
| 511 | |
| 512 | void DSRGlWindow::glTranslateL( const double dx, const double dy, const double dz ) { |
| 513 | double mat[4][4]; |
| 514 | makeCurrent(); |
| 515 | |
| 516 | glGetDoublev( GL_MODELVIEW_MATRIX, (double*)mat ); |
| 517 | mat[3][0] += dx; mat[3][1] += dy; mat[3][2] += dz; |
| 518 | glLoadMatrixd((double*)mat); |
| 519 | } |
| 520 | void DSRGlWindow::glRotateL( const double dang, const double x, const double y, const double z ) { |
| 521 | double mat[4][4]; |
| 522 | #ifndef M_PI |
| 523 | #define M_PI 3.14159265358979323846 /* pi */ |
| 524 | #endif |
| 525 | |
| 526 | double s = z; |
| 527 | makeCurrent(); |
| 528 | s = sin(dang*M_PI/180); |
| 529 | const double c = cos(dang*M_PI/180); |
| 530 | glGetDoublev( GL_MODELVIEW_MATRIX, (double*)mat ); |
| 531 | // glGetDoublev( GL_PROJECTION_MATRIX, (double*)mat ); |
| 532 | if( x!=0.0 ){ |
| 533 | __RotateCS( c, s, mat[0][1], mat[0][2] ); |
| 534 | __RotateCS( c, s, mat[1][1], mat[1][2] ); |
| 535 | __RotateCS( c, s, mat[2][1], mat[2][2] ); |
| 536 | // printf("x\n"); |
| 537 | }else if( y!=0.0 ){ |
| 538 | __RotateCS( c, s, mat[0][2], mat[0][0] ); |
| 539 | __RotateCS( c, s, mat[1][2], mat[1][0] ); |
| 540 | __RotateCS( c, s, mat[2][2], mat[2][0] ); |
| 541 | // printf("y\n"); |
| 542 | }else{ |
| 543 | __RotateCS( c, s, mat[0][0], mat[0][1] ); |
| 544 | __RotateCS( c, s, mat[1][0], mat[1][1] ); |
| 545 | __RotateCS( c, s, mat[2][0], mat[2][1] ); |
| 546 | // printf("z\n"); |
| 547 | } |
| 548 | glLoadMatrixd((double*)mat); |
| 549 | } |
| 550 | |
| 551 | |
| 552 | |
| 553 | void DSRGlWindow::mouseMoveEvent(QMouseEvent *event){ |
| 554 | //! Either rotates (left mouse button) the molecule |
| 555 | //! or zooms the molecule (right mouse button). |
| 556 | double x = event->pos().x(); |
| 557 | double y = event->pos().y(); |
| 558 | |
| 559 | GLfloat dx = GLfloat( event->pos().x() - moux) / width(); |
| 560 | GLfloat dy = GLfloat( event->pos().y() - mouy) / height(); |
| 561 | if((event->buttons() & Qt::RightButton)){ |
| 562 | //printf("mouseMoveEvent %f %f %f %f %x\n", x, y, moux, mouy, event->buttons()); |
| 563 | makeCurrent(); |
| 564 | glScaled(1.0-dy,1.0-dy,1.0-dy); |
| 565 | }else if((event->buttons() & Qt::LeftButton)){ |
| 566 | glRotateL(dy*360.0,1.0,0.0,0.0); |
| 567 | glRotateL(dx*360.0,0.0,1.0,0.0); |
| 568 | } |
| 569 | moux=x; |
| 570 | mouy=y; |
| 571 | update(); |
| 572 | } |
| 573 | |
| 574 | void DSRGlWindow::wheelEvent(QWheelEvent *event){ |
| 575 | /* |
| 576 | modifiers: |
| 577 | Qt::NoModifier 0x00000000 No modifier key is pressed. |
| 578 | Qt::ShiftModifier 0x02000000 A Shift key on the keyboard is pressed. |
| 579 | Qt::ControlModifier 0x04000000 A Ctrl key on the keyboard is pressed. |
| 580 | Qt::AltModifier 0x08000000 An Alt key on the keyboard is pressed. |
| 581 | Qt::MetaModifier 0x10000000 A Meta key on the keyboard is pressed. |
| 582 | Qt::KeypadModifier 0x20000000 A keypad button is pressed. |
| 583 | Qt::GroupSwitchModifier 0x40000000 X11 only. A Mode_switch key on the keyboard is pressed. |
| 584 | */ |
| 585 | #if (QT_VERSION>=0x050000) |
| 586 | int numDegrees = event->angleDelta().y() / 8; |
| 587 | #else |
| 588 | int numDegrees = event->delta() / 8; |
| 589 | #endif |
| 590 | int numSteps = numDegrees / 15; |
| 591 | if (event->modifiers()==Qt::NoModifier){ |
| 592 | int d = myFont.pointSize(); |
| 593 | d = (d+numSteps>4)?d+numSteps:d; |
| 594 | d = qMax(d,7); |
| 595 | myFont.setPointSize(d); |
| 596 | update(); |
| 597 | } |
| 598 | |
| 599 | } |
| 600 | |
| 601 | void DSRGlWindow::tryfit(){ |
| 602 | if (showFit==NULL) return; |
| 603 | fita.clear(); |
| 604 | fitabonds.clear(); |
| 605 | if ((selFragAt.size()!=3)||(m_molecule->selectedatoms.size()!=3)) {update();return;} |
| 606 | |
| 607 | //tree orthogonal vectors of unit size form a rotation matrix |
| 608 | //the transponse of the latter is its inverse |
| 609 | V3 a1,a2,a3; |
| 610 | a1=Normalize(m_molecule->selectedatoms.at(0).pos-m_molecule->selectedatoms.at(1).pos);//a1=(A-B)/|A-B| |
| 611 | a2=Normalize(m_molecule->selectedatoms.at(0).pos-m_molecule->selectedatoms.at(2).pos);//a2=(A-C)/|A-B| |
| 612 | a3=Normalize(a1%a2);//a3=(a1 x a2)/(a1 x a2) |
| 613 | a2=a3%a1;//a2 = a3 x a1 |
| 614 | Matrix amat=Matrix(a1,a2,a3); |
| 615 | |
| 616 | V3 b1,b2,b3; |
| 617 | b1=Normalize(selFragAt.at(0).pos-selFragAt.at(1).pos); |
| 618 | b2=Normalize(selFragAt.at(0).pos-selFragAt.at(2).pos); |
| 619 | b3=Normalize(b1%b2); |
| 620 | b2=b3%b1; |
| 621 | Matrix bmat=transponse(Matrix(b1,b2,b3)); |
| 622 | if ( (a1*(a2%a3)<0.9) || (b1*(b2%b3)<0.9) ) { //linear dependence id determinant is 0 |
| 623 | update(); |
| 624 | return; |
| 625 | } |
| 626 | // m_molecule->showatoms was m_molecule->asymm before, but showatoms is needed for disorder |
| 627 | // around special positions: |
| 628 | for (int i=0; i<m_molecule->showatoms.size(); i++) { |
| 629 | fita.append(m_molecule->showatoms[i]); |
| 630 | fita.last().pos+=-1.0*(m_molecule->selectedatoms.at(0).pos); |
| 631 | fita.last().pos=fita.last().pos*amat; |
| 632 | fita.last().pos=fita.last().pos*bmat; |
| 633 | fita.last().pos+=selFragAt.at(0).pos; |
| 634 | fita.last().part=1; |
| 635 | if (Norm (fita.last().pos) > 12) { // 4 Angstroem cut-off (3*4=12) |
| 636 | fita.removeLast(); |
| 637 | } |
| 638 | } |
| 639 | fitabonds=m_molecule->connecting(fita,true); |
| 640 | update(); |
| 641 | // printf("FIT:\n================================================================================\n"); |
| 642 | for (int i=0; i<xd.size(); i++){ |
| 643 | int imin=0; |
| 644 | double dmin=10.0; |
| 645 | for (int j=0; j<m_molecule->showatoms.size(); j++){ |
| 646 | V3 ort=m_molecule->showatoms.at(j).pos; |
| 647 | ort+=-1.0*(m_molecule->selectedatoms.at(0).pos); |
| 648 | ort=ort*amat; |
| 649 | ort=ort*bmat; |
| 650 | ort+=selFragAt.at(0).pos; |
| 651 | |
| 652 | double d=Distance(ort,xd.at(i).pos); |
| 653 | if (d<dmin) imin=j; |
| 654 | dmin=(d<dmin)?d:dmin; |
| 655 | } |
| 656 | // printf("%-8s <==> %-8s %f\n", xd.at(i).Label.toStdString().c_str(), |
| 657 | // m_molecule->showatoms.at(imin).Label.toStdString().c_str(), sqrt(dmin)); |
| 658 | } |
| 659 | } |
| 660 | void DSRGlWindow::qglColor(const QColor color) { |
| 661 | glColor4d(color.redF(), color.greenF(), color.blueF(), color.alphaF()); |
| 662 | } |