1 /*
2 OpenVR for PyMOL Copyright Notice
3 =====================================
4 
5 The OpenVR for PyMOL source code is copyrighted, but you can freely use and
6 copy it as long as you don't change or remove any of the Copyright notices.
7 OpenVR for PyMOL is made available under the following open-source license
8 terms:
9 
10 ------------------------------------------------------------------------------
11 Copyright (c) 2018 EPAM Systems, Inc.
12 
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19 
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30 ------------------------------------------------------------------------------
31 
32 */
33 
34 // this headers
35 #include "OpenVRScenePicker.h"
36 
37 // system headers
38 #include "os_gl.h"
39 #include <math.h>
40 #include <string.h>
41 
42 // local headers
43 #include "OpenVRUtils.h"
44 
OpenVRScenePicker()45 OpenVRScenePicker::OpenVRScenePicker()
46 : m_inputHandlers(0)
47 , m_ownerID(~0u)
48 , m_clickX(0)
49 , m_clickY(0)
50 , m_active(false)
51 {
52   memset(m_matrix, 0, sizeof(m_matrix));
53   m_matrix[0] = m_matrix[5] = m_matrix[10] = m_matrix[15] = 1.0f;
54 }
55 
Init(OpenVRInputHandlers * inputHandlers)56 void OpenVRScenePicker::Init(OpenVRInputHandlers* inputHandlers)
57 {
58   m_inputHandlers = inputHandlers;
59 }
60 
Free()61 void OpenVRScenePicker::Free()
62 {
63 }
64 
Activate(unsigned ownerID,int x,int y)65 void OpenVRScenePicker::Activate(unsigned ownerID, int x, int y)
66 {
67   m_ownerID = ownerID;
68   m_clickX = x;
69   m_clickY = y;
70   m_active = true;
71 }
72 
Deactivate()73 void OpenVRScenePicker::Deactivate()
74 {
75   m_ownerID = ~0u;
76   m_active = false;
77 }
78 
IsActive() const79 bool OpenVRScenePicker::IsActive() const
80 {
81   return m_active;
82 }
83 
LaserShoot(float const * origin,float const * dir,float const * color,float * distance)84 bool OpenVRScenePicker::LaserShoot(float const* origin, float const* dir, float const* color, float* distance /* = 0 */)
85 {
86   float matrix[16];
87   float* right = &matrix[0];  // head coordinates X-axis
88   float* up = &matrix[4];     // head coordinates Y-axis
89   float* back = &matrix[8];   // head coordinates Z-axis
90   float* pivot = &matrix[12]; // head coordinates origin
91 
92   pivot[0] = origin[0];
93   pivot[1] = origin[1];
94   pivot[2] = origin[2];
95   pivot[3] = 1.0f;
96 
97   back[0] = -dir[0];
98   back[1] = -dir[1];
99   back[2] = -dir[2];
100   back[3] = 0.0f;
101 
102   if (fabsf(back[1]) < 0.95f) {
103     up[0] = 0.0f;
104     up[1] = 1.0f;
105     up[2] = 0.0f;
106     up[3] = 0.0f;
107     OpenVRUtils::VectorCrossProduct(up, back, right);
108     OpenVRUtils::VectorNormalize(right);
109     OpenVRUtils::VectorCrossProduct(back, right, up);
110     OpenVRUtils::VectorNormalize(up);
111   } else {
112     right[0] = 1.0f;
113     right[1] = 0.0f;
114     right[2] = 0.0f;
115     right[3] = 0.0f;
116     OpenVRUtils::VectorCrossProduct(back, right, up);
117     OpenVRUtils::VectorNormalize(up);
118     OpenVRUtils::VectorCrossProduct(up, back, right);
119     OpenVRUtils::VectorNormalize(right);
120   }
121 
122   OpenVRUtils::MatrixFastInverseGLGL(matrix, m_matrix);
123 
124   return false;
125 }
126 
LaserClick(int glutButton,int glutState)127 void OpenVRScenePicker::LaserClick(int glutButton, int glutState)
128 {
129   m_inputHandlers->MouseFunc(glutButton, glutState, m_clickX, m_clickY, 0);
130 }
131 
IsLaserAllowed(unsigned deviceIndex) const132 bool OpenVRScenePicker::IsLaserAllowed(unsigned deviceIndex) const
133 {
134   return m_active && (m_ownerID == deviceIndex || m_ownerID == ~0u);
135 }
136 
GetMatrix() const137 float const* OpenVRScenePicker::GetMatrix() const
138 {
139   return m_matrix;
140 }
141 
GetLaserColor() const142 float const* OpenVRScenePicker::GetLaserColor() const
143 {
144   static const float color[4] = {1.0f, 1.0f, 0.0f, 0.5f};
145   return color;
146 }
147