Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

HMDMgr.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2019 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010 * RCS INFORMATION:
00011 *
00012 *      $RCSfile: OptiXDisplayDevice.h
00013 *      $Author: johns $      $Locker:  $               $State: Exp $
00014 *      $Revision: 1.8 $         $Date: 2019/01/17 21:20:59 $
00015 *
00016 ***************************************************************************
00017 * DESCRIPTION:
00018 *   VMD head mounted display (HMD) interface class
00019 *
00020 ***************************************************************************/
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include "HMDMgr.h"
00026 #include "Inform.h"
00027 #include "Matrix4.h"
00028 #include "VMDQuat.h"
00029 
00030 #if defined(VMDUSEOPENHMD)
00031 #include <openhmd.h>
00032 #endif
00033 
00034 #if 0
00035 // helper routine
00036 static void quat_rot_matrix(float *m, const float *q) {
00037   m[ 0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
00038   m[ 1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
00039   m[ 2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
00040   m[ 3] = 0.0;
00041 
00042   m[ 4] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
00043   m[ 5] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
00044   m[ 6] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
00045   m[ 7] = 0.0;
00046 
00047   m[ 8] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
00048   m[ 9] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
00049   m[10] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
00050   m[11] = 0.0;
00051 
00052   m[12] = 0.0;
00053   m[13] = 0.0;
00054   m[14] = 0.0;
00055   m[15] = 1.0;
00056 }
00057 #endif
00058 
00059 HMDMgr::HMDMgr(void) {
00060   hmdcount = 0;
00061 
00062 #if defined(VMDUSEOPENHMD)
00063   int i;
00064   ctx = ohmd_ctx_create();
00065 
00066   hmdcount = ohmd_ctx_probe(ctx);
00067 
00068   if (hmdcount < 1) {
00069     msgInfo << "No HMD device detected." << sendmsg;
00070   } else {
00071     msgInfo << "Found " << hmdcount << " HMD device" 
00072             << ((hmdcount > 1) ? "s" : "") << sendmsg;
00073   }
00074 
00075   for (i=0; i < hmdcount; i++) {
00076     printf("HMD[%d] ", i);
00077     printf("%s", ohmd_list_gets(ctx, i, OHMD_PRODUCT));
00078     printf(", %s", ohmd_list_gets(ctx, i, OHMD_VENDOR));
00079     printf(", USB dev: %s\n", ohmd_list_gets(ctx, i, OHMD_PATH));
00080   }
00081 
00082   // open first HMD we find...
00083   hmd = ohmd_list_open_device(ctx, 0);
00084 
00085   if (!hmd) {
00086     printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
00087   }
00088 #endif
00089 }
00090 
00091 HMDMgr::~HMDMgr(void) {
00092 #if defined(VMDUSEOPENHMD)
00093   if (ctx)
00094     ohmd_ctx_destroy(ctx);
00095 #endif
00096 }
00097 
00098 
00099 int HMDMgr::device_count(void) {
00100   return hmdcount;
00101 }
00102 
00103 
00104 void HMDMgr::update(void) {
00105 #if defined(VMDUSEOPENHMD)
00106   ohmd_ctx_update(ctx);
00107 #endif
00108 }
00109 
00110 
00111 void HMDMgr::get_rot_quat(float *q, int doupdate) {
00112 #if defined(VMDUSEOPENHMD)
00113   if (doupdate)
00114     update();
00115   ohmd_device_getf(hmd, OHMD_ROTATION_QUAT, q);
00116 #else
00117   memset(q, 0, 4 * sizeof(float));
00118 #endif
00119 }
00120 
00121 
00122 void HMDMgr::reset_orientation(void) {
00123 #if defined(VMDUSEOPENHMD)
00124   // reset rotation and position
00125   float zero[] = {0, 0, 0, 1};
00126   ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
00127   ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);
00128 #endif
00129 }
00130 
00131 
00132 void HMDMgr::rot_point_quat(float *p, const float *op) {
00133   float q[4];
00134   get_rot_quat(q, 0);
00135 
00136   // manipulate the HMD pose quaternion to account
00137   // for reversed coordinate system handedness and 
00138   // reversing the direction of rotation we apply to
00139   // the camera basis vectors vs. what the HMD reports
00140   q[0] = -q[0];
00141   q[1] = -q[1];
00142 
00143 #if 0
00144   Matrix4 m;
00145   q[0] = -q[0];
00146   q[1] = -q[1];
00147   q[2] = -q[2];
00148   q[3] =  q[3];
00149   quat_rot_matrix(&m.mat[0], q);
00150   m.multnorm3d(op, p);
00151 #else
00152   Quat Q(q[0], q[1], q[2], q[3]);
00153   Q.multpoint3(op, p);
00154 #endif
00155 }
00156 
00157 
00158 void HMDMgr::rot_basis_quat(float *u, float *v, float *w,                    
00159                             const float *ou, const float *ov, const float *ow) {
00160   float q[4];
00161   get_rot_quat(q, 0);
00162 
00163   // manipulate the HMD pose quaternion to account
00164   // for reversed coordinate system handedness and 
00165   // reversing the direction of rotation we apply to
00166   // the camera basis vectors vs. what the HMD reports
00167   q[0] = -q[0];
00168   q[1] = -q[1];
00169 
00170 #if 0
00171   Matrix4 m;
00172   q[0] = -q[0];
00173   q[1] = -q[1];
00174   q[2] = -q[2];
00175   q[3] =  q[3];
00176   quat_rot_matrix(&m.mat[0], q);
00177   m.multnorm3d(ou, u);
00178   m.multnorm3d(ov, v);
00179   m.multnorm3d(ow, w);
00180 #else
00181   Quat Q(q[0], q[1], q[2], q[3]);
00182   Q.multpoint3(ou, u);
00183   Q.multpoint3(ov, v);
00184   Q.multpoint3(ow, w);
00185 #endif
00186 }
00187 
00188 
00189 #if 0
00190 int HMDMgr::device_list(int **, char ***) {
00191   return 0;
00192 }
00193 #endif
00194 

Generated on Tue Oct 15 02:44:19 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002