1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif // HAVE_CONFIG_H
36 
37 #ifdef HAVE_VRML97
38 
39 /*!
40   \class SoVRMLSphereSensor SoVRMLSphereSensor.h Inventor/VRMLnodes/SoVRMLSphereSensor
41   \brief The SoVRMLSphereSensor class maps pointer motion into rotations on a sphere.
42 
43   \ingroup VRMLnodes
44 
45   \WEB3DCOPYRIGHT
46 
47   \verbatim
48   SphereSensor {
49     exposedField SFBool     autoOffset        TRUE
50     exposedField SFBool     enabled           TRUE
51     exposedField SFRotation offset            0 1 0 0  # [-1,1],(-,)
52     eventOut     SFBool     isActive
53     eventOut     SFRotation rotation_changed
54     eventOut     SFVec3f    trackPoint_changed
55   }
56   \endverbatim
57 
58   The SphereSensor node maps pointing device motion into spherical rotation
59   about the origin of the local coordinate system. The SphereSensor node uses
60   the descendent geometry of its parent node to determine whether it is liable
61   to generate events.
62 
63   The \e enabled exposed field enables and disables the SphereSensor
64   node. If \e enabled is TRUE, the sensor reacts appropriately to user
65   events. If \e enabled is FALSE, the sensor does not track user input
66   or send events. If \e enabled receives a FALSE event and \e isActive
67   is TRUE, the sensor becomes disabled and deactivated, and outputs an \e isActive
68   FALSE event. If \e enabled receives a TRUE event the sensor is enabled
69   and ready for user activation.
70 
71   The SphereSensor node generates events when the pointing device is activated
72   while the pointer is indicating any descendent geometry nodes of the sensor's
73   parent group. See 4.6.7.5, Activating and manipulating sensors
74   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.7.5>),
75   for details on using the pointing device to activate the SphereSensor.
76 
77   Upon activation of the pointing device (e.g., mouse button down)
78   over the sensor's geometry, an \e isActive TRUE event is sent. The
79   vector defined by the initial point of intersection on the
80   SphereSensor's geometry and the local origin determines the radius
81   of the sphere that is used to map subsequent pointing device motion
82   while dragging. The virtual sphere defined by this radius and the
83   local origin at the time of activation is used to interpret
84   subsequent pointing device motion and is not affected by any changes
85   to the sensor's coordinate system while the sensor is active.
86 
87   For each position of the bearing, a rotation_changed event is sent
88   which corresponds to the sum of the relative rotation from the
89   original intersection point plus the offset
90   value. trackPoint_changed events reflect the unclamped drag position
91   on the surface of this sphere.  When the pointing device is
92   deactivated and autoOffset is TRUE, offset is set to the last
93   rotation_changed value and an offset_changed event is generated. See
94   4.6.7.4, Drag sensors
95   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.7.4>),
96   for more details.
97 
98   When the sensor generates an \e isActive TRUE event, it grabs all
99   further motion events from the pointing device until it is released
100   and generates an isActive FALSE event (other pointing-device sensors
101   shall not generate events during this time). Motion of the pointing
102   device while isActive is TRUE is termed a "drag". If a 2D pointing
103   device is in use, isActive events will typically reflect the state
104   of the primary button associated with the device (i.e., isActive is
105   TRUE when the primary button is pressed and FALSE when it is
106   released).  If a 3D pointing device (e.g., wand) is in use, isActive
107   events will typically reflect whether the pointer is within (or in
108   contact with) the sensor's geometry.
109 
110   While the pointing device is activated, trackPoint_changed and
111   rotation_changed events are output. trackPoint_changed events
112   represent the unclamped intersection points on the surface of the
113   invisible sphere. If the pointing device is dragged off the sphere
114   while activated, browsers may interpret this in a variety of ways
115   (e.g., clamp all values to the sphere or continue to rotate as the
116   point is dragged away from the sphere).
117 
118   Each movement of the pointing device while \e isActive is TRUE
119   generates \e trackPoint_changed and \e rotation_changed events.  Further
120   information about this behaviour can be found in 4.6.7.3
121   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.7.3>),
122   Pointing-device sensors, 4.6.7.4, Drag sensors
123   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.7.4>),
124   and 4.6.7.5, Activating and manipulating sensors
125   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.7.5>).
126 
127 */
128 
129 /*!
130   \var SoSFRotation SoVRMLSphereSensor::offset
131   The sensor rotation value.
132 */
133 
134 /*!
135   \var SoSFRotation SoVRMLSphereSensor::rotation_changed
136 
137   An eventOut that is updated during interaction. Holds the differense
138   between the current rotation and the sensor rotation value.
139 
140 */
141 
142 #include <Inventor/VRMLnodes/SoVRMLSphereSensor.h>
143 
144 #include <Inventor/VRMLnodes/SoVRMLMacros.h>
145 #include <Inventor/projectors/SbSpherePlaneProjector.h>
146 
147 #include "nodes/SoSubNodeP.h"
148 
149 SO_NODE_SOURCE(SoVRMLSphereSensor);
150 
151 // Doc in parent
152 void
initClass(void)153 SoVRMLSphereSensor::initClass(void)
154 {
155   SO_NODE_INTERNAL_INIT_CLASS(SoVRMLSphereSensor, SO_VRML97_NODE_TYPE);
156 }
157 
158 /*!
159   Constructor.
160 */
SoVRMLSphereSensor(void)161 SoVRMLSphereSensor::SoVRMLSphereSensor(void)
162 {
163   SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLSphereSensor);
164 
165   SO_VRMLNODE_ADD_EXPOSED_FIELD(offset, (0.0f, 1.0f, 0.0f, 0.0f));
166   SO_VRMLNODE_ADD_EVENT_OUT(rotation_changed);
167 
168   this->sphereproj = new SbSpherePlaneProjector;
169 }
170 
171 /*!
172   Destructor.
173 */
~SoVRMLSphereSensor()174 SoVRMLSphereSensor::~SoVRMLSphereSensor()
175 {
176   delete this->sphereproj;
177 }
178 
179 // Doc in parent
180 SbBool
dragStart(void)181 SoVRMLSphereSensor::dragStart(void)
182 {
183   SbVec3f thehitpt = this->getLocalStartingPoint();
184   float radius = thehitpt.length();
185   if (radius == 0.0f) return FALSE;
186 
187   this->sphereproj->setSphere(SbSphere(SbVec3f(0.0f, 0.0f, 0.0f), radius));
188   this->sphereproj->setViewVolume(this->getViewVolume());
189   this->sphereproj->setWorkingSpace(this->getLocalToWorldMatrix());
190   this->getLocalToWorldMatrix().multVecMatrix(thehitpt, this->prevworldhitpt);
191   this->prevrotation = SbRotation::identity();
192   this->rotation_changed = this->offset.getValue();
193 
194   return TRUE;
195 }
196 
197 // Doc in parent
198 void
drag(void)199 SoVRMLSphereSensor::drag(void)
200 {
201   this->sphereproj->setViewVolume(this->getViewVolume());
202   this->sphereproj->setWorkingSpace(this->getLocalToWorldMatrix());
203 
204   SbVec3f startpt;
205   this->getWorldToLocalMatrix().multVecMatrix(this->prevworldhitpt, startpt);
206   SbVec3f projpt = this->sphereproj->project(this->getNormalizedLocaterPosition());
207   this->getLocalToWorldMatrix().multVecMatrix(projpt, this->prevworldhitpt);
208 
209   SbRotation rot = this->sphereproj->getRotation(startpt, projpt);
210   this->prevrotation *= rot;
211   this->rotation_changed = this->prevrotation * this->offset.getValue();
212 }
213 
214 // Doc in parent
215 void
dragFinish(void)216 SoVRMLSphereSensor::dragFinish(void)
217 {
218   if (this->autoOffset.getValue()) {
219     this->offset = this->rotation_changed.getValue();
220   }
221 }
222 
223 #endif // HAVE_VRML97
224