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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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* -*- 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):  Zihan Chen, Anton Deguet
  Created on: 2013-03-20

  (C) Copyright 2013-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 <cisstCommon/cmnPortability.h>

#if (CISST_OS == CISST_WINDOWS)
#include <windows.h>
#endif

#if (CISST_OS == CISST_DARWIN)
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif

#include <QGridLayout>
#include <QLabel>
#include <QMenu>
#include <QAction>
#include <QMouseEvent>

#include <cisstVector/vctQtWidgetRotation.h>

// all these widgets should be replaced to use static vectors/matrices
#include <cisstVector/vctQtWidgetDynamicVector.h>
#include <cisstVector/vctQtWidgetDynamicMatrix.h>
#include <cisstVector/vctDynamicVectorTypes.h>
#include <cisstVector/vctDynamicMatrixTypes.h>


// =============================================
// vctQtWidgetRotationOpenGL
// =============================================

vctQtWidgetRotationOpenGL::vctQtWidgetRotationOpenGL(void)
{
    mRotation.SetAll(0.0);
    setFocusPolicy(Qt::StrongFocus);
    setToolTip(QString("'z' to reset orientation"));
    setMinimumHeight(100);
    setMinimumWidth(100);
    setContentsMargins(0, 0, 0, 0);
    ResetOrientation();
}

void vctQtWidgetRotationOpenGL::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 vctQtWidgetRotationOpenGL::SetValue(const vctMatRot3 & rot)
{
    vctEulerZYXRotation3 rotEuler;
    rotEuler.From(rot);
    mRotation.X() = rotEuler.gamma() * cmn180_PI;  // x
    mRotation.Y() = rotEuler.beta() * cmn180_PI;   // y
    mRotation.Z() = rotEuler.alpha() * cmn180_PI;  // z

    // update GL display
    update();
}

void vctQtWidgetRotationOpenGL::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 vctQtWidgetRotationOpenGL::mouseReleaseEvent(QMouseEvent *)
{
    mStartMousePosition = 0;
    // save current rotation
    mCurrentOrientation = mDeltaOrientation * mCurrentOrientation;
    mDeltaOrientation = vctQuatRot3::Identity();
}

void vctQtWidgetRotationOpenGL::keyPressEvent(QKeyEvent * event)
{
    switch (event->key()) {
    case Qt::Key_Z:
        ResetOrientation();
        break;
    default:
        break;
    }
}

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

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

    // disable lighting
    glDisable(GL_LIGHTING);

    // set line width
    glLineWidth(2.0);

    // user orientation
    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 reference coordinate frame here
    glEnable(GL_LINE_STIPPLE);
    glLineStipple(1, 0x00FF); // dashed

    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 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, 1.0f, 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, 1.0f);
    glEnd();

    glDisable(GL_LINE_STIPPLE);

    // orientation
    glRotatef(mRotation.Z(), 0.0, 0.0, 1.0);
    glRotatef(mRotation.Y(), 0.0, 1.0, 0.0);
    glRotatef(mRotation.X(), 1.0, 0.0, 0.0);

    // draw current frame
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 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, 1.0f, 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, 1.0f);
    glEnd();

    glFlush();
}

void vctQtWidgetRotationOpenGL::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);
}


// =============================================
// vctQtWidgetRotationDoubleRead
// =============================================
vctQtWidgetRotationDoubleRead::vctQtWidgetRotationDoubleRead(const DisplayModeType displayMode):
    DisplayMode(UNDEFINED_WIDGET),
    CurrentWidget(0),
    mRevoluteFactor(cmn180_PI)
{
    // Matrix Groupbox
    MatrixWidget = new vctQtWidgetDynamicMatrixDoubleRead();
    MatrixWidget->setMinimumSize(80, 80);
    MatrixWidget->resize(MatrixWidget->sizeHint());

    // Axis Angle Groupbox
    AxisWidget = new vctQtWidgetDynamicVectorDoubleRead();
    AngleWidget = new vctQtWidgetDynamicVectorDoubleRead();

    QGridLayout * axisAngleLayout = new QGridLayout;
    axisAngleLayout->setSpacing(1);
    axisAngleLayout->setContentsMargins(0, 0, 0, 0);
    axisAngleLayout->addWidget(new QLabel("Axis"), 0, 0, 1, 1);
    axisAngleLayout->addWidget(AxisWidget, 0, 1, 1, 1);
    axisAngleLayout->addWidget(new QLabel("Angle"), 1, 0, 1, 1);
    axisAngleLayout->addWidget(AngleWidget, 1, 1, 1, 1);

    AxisAngleWidget = new QWidget();
    AxisAngleWidget->setLayout(axisAngleLayout);

    // Quaternion Groupbox
    QuaternionWidget = new vctQtWidgetDynamicVectorDoubleRead();

    // Euler ZYZ Groupbox
    EulerZYZWidget = new vctQtWidgetDynamicVectorDoubleRead();

    // Euler ZYX Groupbox
    EulerZYXWidget = new vctQtWidgetDynamicVectorDoubleRead();

    // Visualization Groupbox
    OpenGLWidget = new vctQtWidgetRotationOpenGL();
    OpenGLWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));

    Layout = new QVBoxLayout;
    Layout->setSpacing(1);
    Layout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(Layout);
    this->setWindowTitle("vctQtWidgetRotationDoubleRead");

    // Set display mode
    SetDisplayMode(displayMode);

    // Contextual menu
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
            this, SLOT(ShowContextMenu(const QPoint&)));
    setToolTip(QString("right click for menu"));
}

void vctQtWidgetRotationDoubleRead::ShowContextMenu(const QPoint & pos)
{
    QPoint globalPos = this->mapToGlobal(pos);
    QMenu menu;
    QAction * matrix = new QAction("Matrix", this);
    QAction * axisAngle = new QAction("Axis Angle", this);
    QAction * quaternion = new QAction("Quaternion", this);
    QAction * eulerZYZ = new QAction("Euler ZYZ", this);
    QAction * eulerZYX = new QAction("Euler ZYX", this);
    QAction * openGL = new QAction("3D", this);
    if (DisplayMode != MATRIX_WIDGET) {
        menu.addAction(matrix);
    }
    if (DisplayMode != AXIS_ANGLE_WIDGET) {
        menu.addAction(axisAngle);
    }
    if (DisplayMode != QUATERNION_WIDGET) {
        menu.addAction(quaternion);
    }
    if (DisplayMode != EULERZYZ_WIDGET) {
        menu.addAction(eulerZYZ);
    }
    if (DisplayMode != EULERZYX_WIDGET) {
        menu.addAction(eulerZYX);
    }
    if (DisplayMode != OPENGL_WIDGET) {
        menu.addAction(openGL);
    }

    QAction * selectedItem = menu.exec(globalPos);
    if (selectedItem) {
        if (selectedItem == matrix) {
            SetDisplayMode(MATRIX_WIDGET);
        } else if (selectedItem == axisAngle) {
            SetDisplayMode(AXIS_ANGLE_WIDGET);
        } else if (selectedItem == quaternion) {
            SetDisplayMode(QUATERNION_WIDGET);
        } else if (selectedItem == eulerZYZ) {
            SetDisplayMode(EULERZYZ_WIDGET);
        } else if (selectedItem == eulerZYX) {
            SetDisplayMode(EULERZYX_WIDGET);
        } else if (selectedItem == openGL) {
            SetDisplayMode(OPENGL_WIDGET);
        }
    }
}

void vctQtWidgetRotationDoubleRead::UpdateCurrentWidget(void)
{
    vctAxAnRot3 rotAxAn;
    vctQuatRot3 rotQuat;
    vctEulerZYZRotation3 rotEulerZYZ;
    vctEulerZYXRotation3 rotEulerZYX;
    // compute the value based on the internal Rotation
    switch (DisplayMode) {
    case MATRIX_WIDGET:
        MatrixWidget->SetValue(vctDoubleMat(this->Rotation));
        break;
    case AXIS_ANGLE_WIDGET:
        rotAxAn.FromRaw(Rotation);
        AxisWidget->SetValue(vctDoubleVec(rotAxAn.Axis()));
        AngleWidget->SetValue(vctDoubleVec(1, rotAxAn.Angle() * mRevoluteFactor));
        break;
    case QUATERNION_WIDGET:
        rotQuat.FromRaw(Rotation);
        QuaternionWidget->SetValue(vctDoubleVec(rotQuat));
        break;
    case OPENGL_WIDGET:
        OpenGLWidget->SetValue(Rotation);
        break;
    case EULERZYZ_WIDGET:
        rotEulerZYZ.FromRaw(Rotation);
        EulerZYZWidget->SetValue(vctDoubleVec(rotEulerZYZ.GetAngles() * mRevoluteFactor));
        break;
    case EULERZYX_WIDGET:
        rotEulerZYX.FromRaw(Rotation);
        EulerZYXWidget->SetValue(vctDoubleVec(rotEulerZYX.GetAngles() * mRevoluteFactor));
        break;
    default:
        break;
    }
}

void vctQtWidgetRotationDoubleRead::SetDisplayMode(const DisplayModeType displayMode)
{
    // should never allow anyone to use undefined
    if (displayMode == UNDEFINED_WIDGET) {
        return;
    }

    // if the mode is unchanged, nothing to do
    if (displayMode == this->DisplayMode) {
        return;
    }

    // set the new display mode
    this->DisplayMode = displayMode;

    // mostly for initialization, there was no widget defined prior this call
    if (CurrentWidget) {
        Layout->removeWidget(CurrentWidget);
        CurrentWidget->hide();
    }

    // set the new current widget, these have been created in the ctor
    switch (displayMode) {
    case MATRIX_WIDGET:
        CurrentWidget = MatrixWidget;
        break;
    case AXIS_ANGLE_WIDGET:
        CurrentWidget = AxisAngleWidget;
        break;
    case QUATERNION_WIDGET:
        CurrentWidget = QuaternionWidget;
        break;
    case EULERZYZ_WIDGET:
        CurrentWidget = EulerZYZWidget;
        break;
    case EULERZYX_WIDGET:
        CurrentWidget = EulerZYXWidget;
        break;
    case OPENGL_WIDGET:
        CurrentWidget = OpenGLWidget;
        break;
    default:
        break;
    }
    Layout->addWidget(CurrentWidget);
    UpdateCurrentWidget();
    CurrentWidget->show();
    repaint();
}