/* ---------------------------------------------------------------------------- "THE BEER-WARE LICENSE" (Revision 42): wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return. Daniel Kratzert ---------------------------------------------------------------------------- */ #include "dsrglwindow.h" #include "molecule.h" #include "dsrgui.h" #include "glureplace.h" DSRGlWindow::DSRGlWindow(QWidget *parent, Molecule *m, DSRMol header, QString fragment) { this->setParent(parent); m_molecule=m; showFit=NULL; showFitLabel=NULL; /* m_molecule->cell.symmops.clear(); m_molecule->cell.trans.clear(); m_molecule->asymm.clear(); m_molecule->sdm.clear(); m_molecule->showbonds.clear(); m_molecule->showatoms.clear(); m_molecule->selectedatoms.clear(); m_molecule->envi_sdm.clear(); m_molecule->contact.clear(); m_molecule->envi_sdm.clear(); m_molecule->contact.clear(); m_molecule->symmopsEQIV.clear(); m_molecule->labelEQIV.clear(); m_molecule->transEQIV.clear(); m_molecule->freeatoms.clear(); m_molecule->bindatoms.clear(); m_molecule->selectedatoms.clear(); m_molecule->showatoms.clear(); V3 nl(0,0,0); m_molecule->cell.trans.append(nl); m_molecule->cell.symmops.append(Matrix(1,0,0, 0,1,0, 0,0,1)); m_molecule->pmin=10000; m_molecule->pmax=-10000; // initial zoom level: // The Zom will later be adapted to fragment size. m_molecule->transEQIV.clear(); m_molecule->symmopsEQIV.clear(); m_molecule->labelEQIV.clear(); m_molecule->freeatoms.clear(); m_molecule->bindatoms.clear(); m_molecule->envi_sdm.clear(); */ L=50.0; m_molecule->LOD=3; labScal=0.4; dsrLabelColor = QColor("white"); if (fragment.size() > 0) { display_fragment(header); } } DSRGlWindow::~DSRGlWindow() { m_molecule->g_Program->release(); m_molecule->g_Program->deleteLater(); } static inline void transform_point(GLdouble out[4], const GLdouble m[16], const GLdouble in[4]) { #define M(row,col) m[col*4+row] out[0] = M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3]; out[1] = M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3]; out[2] = M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3]; out[3] = M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3]; #undef M } static inline bool posTo2D(V3 obj, const GLdouble model[16], const GLdouble proj[16], const GLint viewport[4], GLdouble * winx, GLdouble * winy,double retina = 1.0) { GLdouble in[4], out[4]; in[0] = obj.x; in[1] = obj.y; in[2] = obj.z; in[3] = 1.0; transform_point(out, model, in); transform_point(in, proj, out); if (in[3] == 0.0) return false; in[0] /= in[3]; in[1] /= in[3]; in[2] /= in[3]; *winx = viewport[0] + (1 + in[0]) * viewport[2] / 2; *winy = viewport[1] + (1 - in[1]) * viewport[3] / 2; *winx /= retina; *winy /= retina; return true; } void DSRGlWindow::project(double x, double y, double z, GLdouble &tx, GLdouble &ty){ makeCurrent(); GLdouble model[16]; GLdouble proj[16]; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glGetDoublev( GL_PROJECTION_MATRIX , (double*)proj ); glGetDoublev( GL_MODELVIEW_MATRIX, (double*)model ); V3 pos(x,y,z); posTo2D(pos, model, proj, viewport, &tx, &ty, retinafktr); } void DSRGlWindow::renderText(double x, double y, double z, const QString &text){ // Identify x and y locations to render text within widget GLdouble textPosX = 0.0, textPosY = 0.0; project(x, y, z, textPosX, textPosY); //printf("%f %f %f -> %f %f\n",x,y,z,textPosX,textPosY); // Render text QPainter painter(this); QFontMetrics mtr(myFont); QRect rc=mtr.boundingRect(text+"L"); QImage image(rc.width(),rc.height(), QImage::Format_ARGB32_Premultiplied); image.fill(0); QPainter p; p.begin(&image); p.setPen(dsrLabelColor); p.setBackground(Qt::NoBrush); p.setFont(myFont); p.drawText(image.rect(), Qt::AlignCenter, text); p.end(); painter.drawImage(textPosX, textPosY-rc.height(), image); painter.end(); } void DSRGlWindow::draw(){ m_molecule->dratom = 0; glPushMatrix(); glScaled( L, L, L ); GLdouble model[16]; GLdouble proj[16]; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); viewport[2]/=retinafktr; viewport[3]/=retinafktr; glGetDoublev( GL_PROJECTION_MATRIX, (double*)proj ); glGetDoublev( GL_MODELVIEW_MATRIX, (double*)model ); /*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", model[0],model[1],model[2],model[3], model[4],model[5],model[6],model[7], model[8],model[9],model[10],model[11], model[12],model[13],model[14],model[15]); GLdouble det= model[0]*model[5]*model[10] - model[8]*model[5]*model[2]+ model[1]*model[6]*model[8] - model[9]*model[6]*model[0]+ model[2]*model[4]*model[9] - model[10]*model[4]*model[1]; printf("die Determinante der Drehmatrix ist: %g\n",det); 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", proj[0],proj[1],proj[2],proj[3], proj[4],proj[5],proj[6],proj[7], proj[8],proj[9],proj[10],proj[11], proj[12],proj[13],proj[14],proj[15]); det= proj[0]*proj[5]*proj[10] - proj[8]*proj[5]*proj[2]+ proj[1]*proj[6]*proj[8] - proj[9]*proj[6]*proj[0]+ proj[2]*proj[4]*proj[9] - proj[10]*proj[4]*proj[1]; printf("die Determinante der Drehmatrix ist: %g\n",det);*/ m_molecule->adp = 0; m_molecule->intern = 0; m_molecule->tubes = 0; glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST ); glDepthFunc(GL_LEQUAL); m_molecule->atoms(xd, 50, 0.5);//smaller atoms m_molecule->bonds(bonds,0.5); glEnable(GL_COLOR_MATERIAL); qglColor(dsrLabelColor); // The fragment label color // glClear( GL_DEPTH_BUFFER_BIT); for (int i=0; iisChecked()) && (jselectedatoms.size()) && (xd.at(i).Label==selFragAt.at(j).Label)) { issel=true; renderText( xd.at(i).pos.x, xd.at(i).pos.y, xd.at(i).pos.z, xd.at(i).Label+"->"+m_molecule->selectedatoms.at(j).Label); } } if (!issel) { renderText( xd.at(i).pos.x, xd.at(i).pos.y, xd.at(i).pos.z, xd.at(i).Label); } } /* for (int i=0; itubes=0; m_molecule->intern=1; m_molecule->adp=0; m_molecule->dratom=1; m_molecule->atoms(selFragAt); m_molecule->dratom=0; glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); }glPopMatrix(); } if ((showFit!=nullptr)&&(!fita.isEmpty())&&(showFit->isChecked())){ glPushMatrix();{ glEnable(GL_BLEND); glScaled( L, L, L ); m_molecule->tubes=0; m_molecule->intern=0; m_molecule->adp=0; m_molecule->dratom=5; m_molecule->atoms(fita); m_molecule->dbond(fitabonds); m_molecule->lbond(); if (showFitLabel->isChecked()){ qglColor(QColor(0, 0, 175)); // The surrounding atoms label color for (int i=0; ig_Program=new QOpenGLShaderProgram(this); m_molecule->g_program=m_molecule->installShader(); GLenum err = GL_NO_ERROR; int ect=0; printf("DSR shaderprog %d context is valid = %d \n",m_molecule->g_program,context()->isValid()); if (!m_molecule->g_program) printf("%s\n",m_molecule->g_Program->log().toStdString().c_str()); while((err = glGetError()) != GL_NO_ERROR){ 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); } } void DSRGlWindow::resizeGL(int width, int height){ retinafktr=devicePixelRatio();// it qt5 this is in in qt6 its qreal!! printf("retinafktr=%f\n",retinafktr); wh=height*retinafktr; ww=width*retinafktr; glViewport(0, 0, ww, wh); glGetIntegerv(GL_VIEWPORT, vp); 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, this->width(),this->height(),retinafktr,devicePixelRatio()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective_( 29.0, (double)width/height, 5.0, 8000.0 ); } QSize DSRGlWindow::minimumSizeHint() const { return QSize(250, 250); } QSize DSRGlWindow::sizeHint() const { return QSize(400, 400); } void DSRGlWindow::paintGL(){ glClearColor(0.6f,0.6f,0.6f,1.0f); // The background color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, ww, wh); glGetIntegerv(GL_VIEWPORT, vp); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective_( 29.0, (double)ww/wh, 5.0, 8000.0 ); glMatrixMode(GL_MODELVIEW); glPushMatrix(); draw(); glPopMatrix(); } void DSRGlWindow::clear_molecule() { // clear the 3D view xd.clear(); bonds.clear(); fita.clear(); fitabonds.clear(); selFragAt.clear(); source_atoms.clear(); update(); } void DSRGlWindow::display_fragment(DSRMol frag, bool clear_sel) { //! uses a QStringlist of atoms from DSR and fills //! them into a MyAtom. MyAtom is then drawn in OpenGL //! @param The fragment from DSR //! @param clear_sel if enabled, the selected atoms are cleared //! before drawing the molecule V3 mid = V3(0,0,0); MyAtom newAtom; newAtom.part = 0; newAtom.resiNr = 0; newAtom.hidden = 0; newAtom.symmGroup = 0; newAtom.afix = 0; xd.clear(); bonds.clear(); fita.clear(); fitabonds.clear(); // Do not clear the selection during invert: if (clear_sel) { selFragAt.clear(); } if (frag.atoms.isEmpty()) { return; } foreach (QStringList atom, frag.atoms) { if (atom.length() < 4) continue; // Do not crash on empty string newAtom.Label = atom.at(0); newAtom.an = abs(atom.at(1).toInt())-1; newAtom.pos.x = atom.at(2).toDouble(); newAtom.pos.y = atom.at(3).toDouble(); newAtom.pos.z = atom.at(4).toDouble(); mid += newAtom.pos; //qDebug()<< newAtom.Label << newAtom.an << newAtom.pos.x << newAtom.pos.y << newAtom.pos.z; xd.append(newAtom); } mid *= -1.0/xd.size(); for (int i=0; idimension(xd); // Zoom level of the molecule inside the widget bonds=m_molecule->connecting(xd,true); update(); } void DSRGlWindow::set_label_color() { //! Sets the label color. The default color was not visible //! for white or yellow atoms. QColor lc= QColorDialog::getColor(QColor(6, 167, 222), this); if (lc.isValid()) { dsrLabelColor=lc; } else { dsrLabelColor = QColor("white"); } update(); } void DSRGlWindow::mousePressEvent(QMouseEvent *event){ //! Handles the mousePressEvents in the 3D view. //! Atoms are selectable if showFit is not NULL. //! New atoms will be selected if the mousePressEvent is inside the //! size of the respective atom and less than three atoms. //! With more than three atoms, the first selection will be removed. moux=event->pos().x(); mouy=event->pos().y(); if((event->buttons() == Qt::RightButton) && (event->modifiers() == Qt::ControlModifier)){ QMenu *menu = new QMenu(""); menu->addAction(QString("Set label color"), this, SLOT(set_label_color())); menu->exec(QCursor::pos()); } if (event->buttons() == Qt::LeftButton) { // Has to be after moux/mouy, otherwise zoom is strange: if (showFit == NULL) return; // <-prevents selection of atoms in edit window double nahda=200.0, da=0; int nahdai=-1; for (int j=0; jpos().x())*( xd.at(j).screenX-event->pos().x()))+ ((xd.at(j).screenY-event->pos().y())*( xd.at(j).screenY-event->pos().y()))); nahdai=(da- 1) && (nahdai < xd.size())){ // This might be a good idea in the first place, but results in wired behavior: //if (selFragAt.contains(xd[nahdai])) { // return; //} selFragAt.append(xd[nahdai]); if (selFragAt.size() > 3) selFragAt.removeFirst(); update(); if (selFragAt.size() == 3) { source_atoms = QString("%1 %2 %3") .arg(selFragAt.at(0).Label) .arg(selFragAt.at(1).Label) .arg(selFragAt.at(2).Label); emit sourceStringChanged(); } } makeInfo(); } } void DSRGlWindow::makeInfo(){ //! makes an info box that shows the fragment fit distances double f12dis=0, f13dis=0, t12dis=0, t13dis=0; if (selFragAt.size()>1) f12dis=sqrt(Distance(selFragAt.at(0).pos,selFragAt.at(1).pos)); if (selFragAt.size()>2) f13dis=sqrt(Distance(selFragAt.at(0).pos,selFragAt.at(2).pos)); if (m_molecule->selectedatoms.size()>1) t12dis=sqrt(Distance(m_molecule->selectedatoms.at(0).pos,m_molecule->selectedatoms.at(1).pos)); if (m_molecule->selectedatoms.size()>2) t13dis=sqrt(Distance(m_molecule->selectedatoms.at(0).pos,m_molecule->selectedatoms.at(2).pos)); emit updateInfo(QString( "" "" "" "" "" "" "" " " " " "" " " "" "" " " "" " " "" "
fragment: " " target:
%1" " %2
%31,2: %7 Å" " %4 1,2: %9 Å
%51,3: %8 Å" " %6 1,3: %10 Å
") .arg((selFragAt.size()>0)?selFragAt.at(0).Label:"") .arg((m_molecule->selectedatoms.size()>0)?m_molecule->selectedatoms.at(0).Label:"") .arg((selFragAt.size()>1)?selFragAt.at(1).Label:"") .arg((m_molecule->selectedatoms.size()>1)?m_molecule->selectedatoms.at(1).Label:"") .arg((selFragAt.size()>2)?selFragAt.at(2).Label:"") .arg((m_molecule->selectedatoms.size()>2)?m_molecule->selectedatoms.at(2).Label:"") .arg(f12dis, 4, 'f', 2) .arg(f13dis, 4, 'f', 2) .arg(t12dis, 4, 'f', 2) .arg(t13dis, 4, 'f', 2) ); tryfit(); } //void glRotateL(double ang, double x, double y, double z); inline void DSRGlWindow::__RotateCS( double c, double s, double& X, double& Y ) { double T = X; X = c*X - s*Y; Y = s*T + c*Y; } void DSRGlWindow::glTranslateL( const double dx, const double dy, const double dz ) { double mat[4][4]; makeCurrent(); glGetDoublev( GL_MODELVIEW_MATRIX, (double*)mat ); mat[3][0] += dx; mat[3][1] += dy; mat[3][2] += dz; glLoadMatrixd((double*)mat); } void DSRGlWindow::glRotateL( const double dang, const double x, const double y, const double z ) { double mat[4][4]; #ifndef M_PI #define M_PI 3.14159265358979323846 /* pi */ #endif double s = z; makeCurrent(); s = sin(dang*M_PI/180); const double c = cos(dang*M_PI/180); glGetDoublev( GL_MODELVIEW_MATRIX, (double*)mat ); // glGetDoublev( GL_PROJECTION_MATRIX, (double*)mat ); if( x!=0.0 ){ __RotateCS( c, s, mat[0][1], mat[0][2] ); __RotateCS( c, s, mat[1][1], mat[1][2] ); __RotateCS( c, s, mat[2][1], mat[2][2] ); // printf("x\n"); }else if( y!=0.0 ){ __RotateCS( c, s, mat[0][2], mat[0][0] ); __RotateCS( c, s, mat[1][2], mat[1][0] ); __RotateCS( c, s, mat[2][2], mat[2][0] ); // printf("y\n"); }else{ __RotateCS( c, s, mat[0][0], mat[0][1] ); __RotateCS( c, s, mat[1][0], mat[1][1] ); __RotateCS( c, s, mat[2][0], mat[2][1] ); // printf("z\n"); } glLoadMatrixd((double*)mat); } void DSRGlWindow::mouseMoveEvent(QMouseEvent *event){ //! Either rotates (left mouse button) the molecule //! or zooms the molecule (right mouse button). double x = event->pos().x(); double y = event->pos().y(); GLfloat dx = GLfloat( event->pos().x() - moux) / width(); GLfloat dy = GLfloat( event->pos().y() - mouy) / height(); if((event->buttons() & Qt::RightButton)){ //printf("mouseMoveEvent %f %f %f %f %x\n", x, y, moux, mouy, event->buttons()); makeCurrent(); glScaled(1.0-dy,1.0-dy,1.0-dy); }else if((event->buttons() & Qt::LeftButton)){ glRotateL(dy*360.0,1.0,0.0,0.0); glRotateL(dx*360.0,0.0,1.0,0.0); } moux=x; mouy=y; update(); } void DSRGlWindow::wheelEvent(QWheelEvent *event){ /* modifiers: Qt::NoModifier 0x00000000 No modifier key is pressed. Qt::ShiftModifier 0x02000000 A Shift key on the keyboard is pressed. Qt::ControlModifier 0x04000000 A Ctrl key on the keyboard is pressed. Qt::AltModifier 0x08000000 An Alt key on the keyboard is pressed. Qt::MetaModifier 0x10000000 A Meta key on the keyboard is pressed. Qt::KeypadModifier 0x20000000 A keypad button is pressed. Qt::GroupSwitchModifier 0x40000000 X11 only. A Mode_switch key on the keyboard is pressed. */ #if (QT_VERSION>=0x050000) int numDegrees = event->angleDelta().y() / 8; #else int numDegrees = event->delta() / 8; #endif int numSteps = numDegrees / 15; if (event->modifiers()==Qt::NoModifier){ int d = myFont.pointSize(); d = (d+numSteps>4)?d+numSteps:d; d = qMax(d,7); myFont.setPointSize(d); update(); } } void DSRGlWindow::tryfit(){ if (showFit==NULL) return; fita.clear(); fitabonds.clear(); if ((selFragAt.size()!=3)||(m_molecule->selectedatoms.size()!=3)) {update();return;} //tree orthogonal vectors of unit size form a rotation matrix //the transponse of the latter is its inverse V3 a1,a2,a3; a1=Normalize(m_molecule->selectedatoms.at(0).pos-m_molecule->selectedatoms.at(1).pos);//a1=(A-B)/|A-B| a2=Normalize(m_molecule->selectedatoms.at(0).pos-m_molecule->selectedatoms.at(2).pos);//a2=(A-C)/|A-B| a3=Normalize(a1%a2);//a3=(a1 x a2)/(a1 x a2) a2=a3%a1;//a2 = a3 x a1 Matrix amat=Matrix(a1,a2,a3); V3 b1,b2,b3; b1=Normalize(selFragAt.at(0).pos-selFragAt.at(1).pos); b2=Normalize(selFragAt.at(0).pos-selFragAt.at(2).pos); b3=Normalize(b1%b2); b2=b3%b1; Matrix bmat=transponse(Matrix(b1,b2,b3)); if ( (a1*(a2%a3)<0.9) || (b1*(b2%b3)<0.9) ) { //linear dependence id determinant is 0 update(); return; } // m_molecule->showatoms was m_molecule->asymm before, but showatoms is needed for disorder // around special positions: for (int i=0; ishowatoms.size(); i++) { fita.append(m_molecule->showatoms[i]); fita.last().pos+=-1.0*(m_molecule->selectedatoms.at(0).pos); fita.last().pos=fita.last().pos*amat; fita.last().pos=fita.last().pos*bmat; fita.last().pos+=selFragAt.at(0).pos; fita.last().part=1; if (Norm (fita.last().pos) > 12) { // 4 Angstroem cut-off (3*4=12) fita.removeLast(); } } fitabonds=m_molecule->connecting(fita,true); update(); // printf("FIT:\n================================================================================\n"); for (int i=0; ishowatoms.size(); j++){ V3 ort=m_molecule->showatoms.at(j).pos; ort+=-1.0*(m_molecule->selectedatoms.at(0).pos); ort=ort*amat; ort=ort*bmat; ort+=selFragAt.at(0).pos; double d=Distance(ort,xd.at(i).pos); if (d %-8s %f\n", xd.at(i).Label.toStdString().c_str(), // m_molecule->showatoms.at(imin).Label.toStdString().c_str(), sqrt(dmin)); } } void DSRGlWindow::qglColor(const QColor color) { glColor4d(color.redF(), color.greenF(), color.blueF(), color.alphaF()); }