Skip to content

Commit b8722c8

Browse files
author
mdcutone
committed
RF: Removed the inplace *= for poses, too confusing.
1 parent 651e2cd commit b8722c8

File tree

2 files changed

+104
-41
lines changed

2 files changed

+104
-41
lines changed

psychxr/libovr/_libovr.pyx

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -742,10 +742,20 @@ cdef class LibOVRPose(object):
742742
objects in a VR scene. Poses can be manipulated and interacted with using
743743
class methods and attributes.
744744
745+
Poses can be manipulated using operators such as ``*``, ``~``, and ``*=``.
746+
One pose can be transformed by another by multiplying them using the
747+
``*`` operator::
748+
749+
newPose = pose1 * pose2
750+
751+
The above code returns `pose2` transformed by `pose1`. On can get the
752+
inverse of a pose by using the ``~`` operator::
753+
754+
poseInverse = ~pose
755+
745756
Poses can be converted to 4x4 transformation matrices. One can use these
746757
matrices when rendering to transform the vertices of a model associated with
747-
the pose by passing them to OpenGL. Furthermore, poses can be used to
748-
transform other poses and vectors.
758+
the pose by passing them to OpenGL.
749759
750760
This class is a wrapper for the ``OVR::ovrPosef`` data structure. Fields
751761
``OVR::ovrPosef.Orientation`` and ``OVR::ovrPosef.Position`` are accessed
@@ -759,6 +769,13 @@ cdef class LibOVRPose(object):
759769
<https://developer.oculus.com/reference/libovr/1.38/o_v_r_math_8h/>`_, which
760770
is part of the Oculus PC SDK.
761771
772+
Parameters
773+
----------
774+
pos : tuple, list, or ndarray of float
775+
Position vector (x, y, z).
776+
ori : tuple, list, or ndarray of float
777+
Orientation quaternion vector (x, y, z, w).
778+
762779
"""
763780
cdef capi.ovrPosef* c_data
764781
cdef bint ptr_owner
@@ -767,15 +784,6 @@ cdef class LibOVRPose(object):
767784
cdef np.ndarray _ori
768785

769786
def __init__(self, pos=(0., 0., 0.), ori=(0., 0., 0., 1.)):
770-
"""
771-
Parameters
772-
----------
773-
pos : tuple, list, or ndarray of float
774-
Position vector (x, y, z).
775-
ori : tuple, list, or ndarray of float
776-
Orientation quaternion vector (x, y, z, w).
777-
778-
"""
779787
self._new_struct(pos, ori)
780788

781789
def __cinit__(self, *args, **kwargs):
@@ -838,14 +846,6 @@ cdef class LibOVRPose(object):
838846
ptr[0] = <capi.ovrPosef>pose_r
839847
return LibOVRPose.fromPtr(ptr, True)
840848

841-
def __imul__(self, LibOVRPose other):
842-
"""Inplace multiplication operator (*=) to combine poses.
843-
"""
844-
cdef libovr_math.Posef result = <libovr_math.Posef>self.c_data[0] * \
845-
<libovr_math.Posef>other.c_data[0]
846-
self.c_data[0] = <capi.ovrPosef>result
847-
return self
848-
849849
def __invert__(self):
850850
"""Invert operator (~) to invert a pose."""
851851
return self.inverted()
@@ -1828,6 +1828,24 @@ cdef class LibOVRPoseState(object):
18281828
predict the future positions of objects (see
18291829
:py:meth:`~psychxr.libovr.LibOVRPoseState.timeIntegrate`).
18301830
1831+
Parameters
1832+
----------
1833+
pose : LibOVRPose, list or tuple
1834+
Rigid body pose this state refers to. Can be a `LibOVRPose` pose
1835+
instance or a tuple/list of a position coordinate (x, y, z) and
1836+
orientation quaternion (x, y, z, w).
1837+
linearVelocity : tuple, list, or ndarray of float
1838+
Linear acceleration vector [vx, vy, vz] in meters/sec.
1839+
angularVelocity : tuple, list, or ndarray of float
1840+
Angular velocity vector [vx, vy, vz] in radians/sec.
1841+
linearAcceleration : tuple, list, or ndarray of float
1842+
Linear acceleration vector [ax, ay, az] in meters/sec^2.
1843+
angularAcceleration : tuple, list, or ndarray of float
1844+
Angular acceleration vector [ax, ay, az] in radians/sec^2.
1845+
timeInSeconds : float
1846+
Time in seconds this state refers to.
1847+
1848+
18311849
"""
18321850
cdef capi.ovrPoseStatef* c_data
18331851
cdef bint ptr_owner # owns the data
@@ -1839,7 +1857,14 @@ cdef class LibOVRPoseState(object):
18391857
cdef np.ndarray _linearAcceleration
18401858
cdef np.ndarray _angularAcceleration
18411859

1842-
def __init__(self):
1860+
def __init__(self,
1861+
object pose,
1862+
object linearVelocity=(0., 0., 0.),
1863+
object angularVelocity=(0., 0., 0.),
1864+
object linearAcceleration=(0., 0. ,0.),
1865+
object angularAcceleration=(0., 0., 0.),
1866+
double timeInSeconds=0.0
1867+
):
18431868
"""
18441869
Attributes
18451870
----------
@@ -1851,9 +1876,15 @@ cdef class LibOVRPoseState(object):
18511876
timeInSeconds : float
18521877
18531878
"""
1854-
self._new_struct()
1879+
self._new_struct(
1880+
pose,
1881+
linearVelocity,
1882+
angularVelocity,
1883+
linearAcceleration,
1884+
angularAcceleration,
1885+
timeInSeconds)
18551886

1856-
def __cinit__(self):
1887+
def __cinit__(self, *args, **kwargs):
18571888
self.ptr_owner = False
18581889

18591890
@staticmethod
@@ -1875,7 +1906,15 @@ cdef class LibOVRPoseState(object):
18751906

18761907
return wrapper
18771908

1878-
cdef void _new_struct(self):
1909+
cdef void _new_struct(
1910+
self,
1911+
object pose,
1912+
object linearVelocity,
1913+
object angularVelocity,
1914+
object linearAcceleration,
1915+
object angularAcceleration,
1916+
double timeInSeconds):
1917+
18791918
if self.c_data is not NULL: # already allocated, __init__ called twice?
18801919
return
18811920

@@ -1886,15 +1925,6 @@ cdef class LibOVRPoseState(object):
18861925
if _ptr is NULL:
18871926
raise MemoryError
18881927

1889-
# clear memory to defaults
1890-
_ptr.ThePose.Position = [0., 0., 0.]
1891-
_ptr.ThePose.Orientation = [0., 0., 0., 1.]
1892-
_ptr.AngularVelocity = [0., 0., 0.]
1893-
_ptr.LinearVelocity = [0., 0., 0.]
1894-
_ptr.AngularAcceleration = [0., 0., 0.]
1895-
_ptr.LinearAcceleration = [0., 0., 0.]
1896-
_ptr.TimeInSeconds = 0.0
1897-
18981928
self.c_data = _ptr
18991929
self.ptr_owner = True
19001930

@@ -1909,6 +1939,23 @@ cdef class LibOVRPoseState(object):
19091939
self._angularAcceleration = _wrap_ovrVector3f_as_ndarray(
19101940
&self.c_data.AngularAcceleration)
19111941

1942+
# set values
1943+
if isinstance(pose, LibOVRPose):
1944+
_ptr.ThePose.Position = (<LibOVRPose>pose).c_data.Position
1945+
_ptr.ThePose.Orientation = (<LibOVRPose>pose).c_data.Orientation
1946+
elif isinstance(pose, (tuple, list,)):
1947+
self._pose.posOri = pose
1948+
else:
1949+
raise TypeError('Invalid value for `pose`, must be `LibOVRPose`'
1950+
', `list` or `tuple`.')
1951+
1952+
self._angularVelocity[:] = angularVelocity
1953+
self._linearVelocity[:] = linearVelocity
1954+
self._angularAcceleration[:] = angularAcceleration
1955+
self._linearAcceleration[:] = linearAcceleration
1956+
1957+
_ptr.TimeInSeconds = 0.0
1958+
19121959
def __dealloc__(self):
19131960
# don't do anything crazy like set c_data=NULL without deallocating!
19141961
if self.c_data is not NULL:
@@ -2032,17 +2079,18 @@ cdef class LibOVRPoseState(object):
20322079
Examples
20332080
--------
20342081
2035-
Time integrate a pose for a 1/4 second (note the returned object is a
2036-
:py:mod:`LibOVRPose`, not a :py:class:`LibOVRPoseState`)::
2082+
Time integrate a pose for 20 milliseconds (note the returned object is a
2083+
:py:mod:`LibOVRPose`, not another :py:class:`LibOVRPoseState`)::
20372084
2038-
newPose = oldPose.timeIntegrate(0.25)
2085+
newPose = oldPose.timeIntegrate(0.02)
20392086
pos, ori = newPose.posOri # extract components
20402087
20412088
Time integration can be used to predict the pose of an object at HMD
2042-
V-Sync if velocity and acceleration is known. Usually for predicting HMD
2043-
position, we would pass the predicted time to `getDevicePoses` or
2044-
`getTrackingState` directly and have `LibOVR` compute the pose offset.
2045-
For this example, we will use do so with the following::
2089+
V-Sync if velocity and acceleration are known. Usually we would pass the
2090+
predicted time to `getDevicePoses` or `getTrackingState` for a more
2091+
robust estimate of HMD pose at predicted display time. However, in most
2092+
cases the following will yield the same position and orientation as
2093+
`LibOVR` within a few decimal places::
20462094
20472095
tsec = timeInSeconds()
20482096
ptime = getPredictedDisplayTime(frame_index)
@@ -2079,6 +2127,21 @@ cdef class LibOVRPoseState(object):
20792127

20802128
return to_return
20812129

2130+
# def relativize(self, LibOVRPoseState poseState):
2131+
# """Get relative motion and acceleration derivatives.
2132+
#
2133+
# This function returns a new `LibOVRPoseState` object where velocity
2134+
# and acceleration derivatives of `poseState` are transformed into the
2135+
# reference frame of this pose state. If `timeInSeconds` differs between
2136+
# the poses, `poseState` is time integrated to the time this pose refers
2137+
# to.
2138+
#
2139+
# Parameters
2140+
# ----------
2141+
# poseState : LibOVRPoseState
2142+
#
2143+
# """
2144+
20822145

20832146
cdef class LibOVRTrackerInfo(object):
20842147
"""Class for storing tracker (sensor) information such as pose, status, and
@@ -5704,7 +5767,7 @@ def testBoundary(int deviceBitmask, int boundaryType):
57045767
Returns
57055768
-------
57065769
tuple (int, LibOVRBoundaryTestResult)
5707-
Result of the ``OVR::ovr_TestBoundary` LibOVR API call and
5770+
Result of the ``OVR::ovr_TestBoundary`` LibOVR API call and
57085771
collision test results.
57095772
57105773
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
'Intended Audience :: Science/Research'],
146146
"ext_modules": ext_modules,
147147
#"data_files": DATA_FILES,
148-
"install_requires" : ["Cython>=0.29.3"],
148+
"install_requires" : ["Cython>=0.29.3", "numpy>=1.13.3"],
149149
"requires" : [],
150150
"cmdclass" : {"build_ext": build_ext}}
151151

0 commit comments

Comments
 (0)