File size: 7,159 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2017-11-30

  (C) Copyright 2017-2020 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include <cisstConfig.h>

#include <cisstVector/vctVector3DQtWidget.h>
#include <QKeyEvent>
#include <QPainter>
#include <QtGlobal>

const double vctVector3DQtWidgetDefaultMax = 0.0001;

vctVector3DQtWidget::vctVector3DQtWidget(QWidget * parent):
    vctQtOpenGLBaseWidget(parent),
    mVector(0.0)
{
    this->setFocusPolicy(Qt::StrongFocus);
    this->setToolTip(QString("'a' to turn on/off autoresize\n'r' to reset autoresize if already in autoresize mode\n'z' to reset orientation"));

    setContentsMargins(0, 0, 0, 0);
    SetAutoResize(true);
    mAxisLength = 0.5;
    ResetOrientation();
}

void vctVector3DQtWidget::ResetOrientation(void)
{
    // start with default Z up, x toward left, y towards right
    mCurrentOrientation =
        vctQuatRot3(vctRodRot3(0.1 * cmnPI, 0.0, 0.0)) *
        vctQuatRot3(vctRodRot3(0.0, -0.75 * cmnPI, 0.0)) *
        vctQuatRot3(vctRodRot3(-0.5 * cmnPI, 0.0, 0.0));
    mStartMousePosition = 0;
    mDeltaOrientation = vctQuatRot3::Identity();
}

void vctVector3DQtWidget::SetAutoResize(const bool autoResize)
{
    if (autoResize) {
        // re-initialize to default, i.e. fairly small force
        mMaxNorm = vctVector3DQtWidgetDefaultMax;
        mScale = 1.0f / mMaxNorm;
    }
    mAutoResize = autoResize;
}

void vctVector3DQtWidget::SetValue(const vct3 & value)
{
    mVector = value;
    mVectorNorm = mVector.Norm();
    if (mAutoResize && (mVectorNorm > mMaxNorm)) {
        mMaxNorm = mVectorNorm;
        mScale = 1.0f / mMaxNorm;
    }
    // update GL display
    update();
}

void vctVector3DQtWidget::mouseMoveEvent(QMouseEvent * event)
{
    const double sensitivity = 0.01;
    if (event->buttons() & Qt::LeftButton) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
        const vctInt2 newMousePosition(event->position().x(), event->position().y());
#else
        const vctInt2 newMousePosition(event->x(), event->y());
#endif
        if (mStartMousePosition != 0) {
            const vct2 deltaMouse = sensitivity * vct2(newMousePosition - mStartMousePosition);
            vctRodRot3 deltaRot(deltaMouse.Y(), deltaMouse.X(), 0.0);
            mDeltaOrientation.From(deltaRot);
            paintGL();
        } else {
            mStartMousePosition = newMousePosition;
        }
    }
}

void vctVector3DQtWidget::mouseReleaseEvent(QMouseEvent *)
{
    mStartMousePosition = 0;
    // save current rotation
    mCurrentOrientation = mDeltaOrientation * mCurrentOrientation;
    mDeltaOrientation = vctQuatRot3::Identity();
}

void vctVector3DQtWidget::keyPressEvent(QKeyEvent * event)
{
    switch(event->key()) {
    case Qt::Key_A:
        // toggle autoresize
        SetAutoResize(!mAutoResize);
        break;
    case Qt::Key_R:
        // reset autoresize if already in autoresize mode
        if (mAutoResize) {
            SetAutoResize(true);
        }
        break;
    case Qt::Key_Z:
        ResetOrientation();
        break;
    default:
        break;
    }
}

void vctVector3DQtWidget::initializeGL(void)
{
    const QColor bgColor = palette().color(QPalette::Base);
    glClearColor(bgColor.redF(), bgColor.greenF(), bgColor.blueF(), 1.0);
    glShadeModel(GL_SMOOTH);
}

void vctVector3DQtWidget::paintGL(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    // disable lighting
    glDisable(GL_LIGHTING);

    // set line width
    glLineWidth(2.0);

    // gl transformation here
    // x+:left  y+:up   z+: point out screen
    glTranslatef(0.0f, 0.0f, -10.0f);
    vctAxAnRot3 rot(mDeltaOrientation * mCurrentOrientation);
    glRotated(rot.Angle() * cmn180_PI, rot.Axis().X(), rot.Axis().Y(), rot.Axis().Z());

    // draw X/Y/Z axis, using half
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(mAxisLength, 0.0f, 0.0f);
    glEnd();

    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, mAxisLength, 0.0f);
    glEnd();

    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, mAxisLength);
    glEnd();

    glPushMatrix();

    if (mAutoResize) {
        glScalef(mScale, mScale, mScale);
    }

    // draw line for force
    const QColor textColor = palette().color(QPalette::Text);
    glColor3f(textColor.redF(), textColor.greenF(), textColor.blueF());
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(mVector.X(), mVector.Y(), mVector.Z());
    glEnd();

    // draw X/Y/Z projections
    glEnable(GL_LINE_STIPPLE);

    glColor3f(1.0f, 0.0f, 0.0f);
    if (mVector.X() > 0.0) {
        glLineStipple(1, 0x00FF); // dashed
    } else {
        glLineStipple(1, 0x0101); // dotted
    }
    glBegin(GL_LINE_STRIP);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(mVector.X(), 0.0f, 0.0f);
    glVertex3f(mVector.X(), mVector.Y(), 0.0f);
    glEnd();

    glColor3f(0.0f, 1.0f, 0.0f);
    if (mVector.Y() > 0.0) {
        glLineStipple(1, 0x00FF); // dashed
    } else {
        glLineStipple(1, 0x0101); // dotted
    }

    glBegin(GL_LINE_STRIP);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, mVector.Y(), 0.0f);
    glVertex3f(mVector.X(), mVector.Y(), 0.0f);
    glEnd();


    glColor3f(0.0f, 0.0f, 1.0f);
    if (mVector.Z() > 0.0) {
        glLineStipple(1, 0x00FF); // dashed
    } else {
        glLineStipple(1, 0x0101); // dotted
    }
    glBegin(GL_LINE_LOOP);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, mVector.Z());
    glVertex3f(mVector.X(), mVector.Y(), mVector.Z());
    glVertex3f(mVector.X(), mVector.Y(), 0.0f);
    glEnd();

    glDisable(GL_LINE_STIPPLE);

    glPopMatrix();

    const int font_size = 8;
    QPainter painter(this);
    const QColor txtColor = palette().color(QPalette::Text);
    painter.setPen(txtColor);
    painter.setFont(QFont("Helvetica", font_size));
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.drawText(1, 1 + font_size,
                     QString::asprintf("%0.2f/%0.2f", mVectorNorm, mMaxNorm));
    painter.end();

    glFlush();
}


void vctVector3DQtWidget::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    GLdouble dWt;
    GLdouble dHt;

    if (width > height) {
        dHt = 1.0;
        dWt = static_cast<double>(width) / static_cast<double>(height);
    } else {
        dHt = static_cast<double>(height) / static_cast<double>(width);
        dWt = 1.0;
    }

    glOrtho(-dWt, dWt, -dHt, dHt, 8.0, 12.0);

    glMatrixMode(GL_MODELVIEW);
}