H3D API  2.4.1
CylinderSensor.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __CYLINDERSENSOR_H__
30 #define __CYLINDERSENSOR_H__
31 
32 #include <H3D/X3DDragSensorNode.h>
33 #include <H3D/SFRotation.h>
34 #include <H3D/SFFloat.h>
35 
36 namespace H3D {
37 
139  class H3DAPI_API CylinderSensor :
140  public X3DDragSensorNode {
141  public:
142 
146  class H3DAPI_API CheckAngleLimits:
147  public SFFloat {
148  public:
149  CheckAngleLimits() { limit = 2 * (H3DFloat)Constants::pi; }
150 
151  virtual void setValue( const H3DFloat &b, int _id = 0 ) {
152  if( b < -limit ) {
153  SFFloat::setValue( -limit, _id );
154  Console(LogLevel::Warning) << "Warning: The value of field " << getName()
155  << " in CylinderSensor node( "
156  << owner->getName()
157  << " ) is outside valid range [-2pi, 2pi]."
158  << " Value set to -2pi" << endl;
159  }
160  else if( b > limit ) {
161  SFFloat::setValue( limit, _id );
162  Console(LogLevel::Warning) << "Warning: The value of field " << getName()
163  << " in CylinderSensor node( "
164  << owner->getName()
165  << " ) is outside valid range [-2pi, 2pi]."
166  << " Value set to 2pi" << endl;
167  }
168  else
169  SFFloat::setValue( b, _id );
170  }
171  protected:
172  virtual void update() {
173  SFFloat::update();
174  if( value < -limit ) {
175  value = -limit;
176  Console(LogLevel::Warning) << "Warning: The value of field " << getName()
177  << " in CylinderSensor node( "
178  << owner->getName()
179  << " ) is outside valid range [-2pi, 2pi]."
180  << " Value set to -2pi" << endl;
181  }
182  else if( value > limit ) {
183  value = limit;
184  Console(LogLevel::Warning) << "Warning: The value of field " << getName()
185  << " in CylinderSensor node( "
186  << owner->getName()
187  << " ) is outside valid range [-2pi, 2pi]."
188  << " Value set to 2pi" << endl;
189  }
190  }
191 
192  H3DFloat limit;
193  };
194 #ifdef __BORLANDC__
195  friend class CheckAngleLimits;
196 #endif
197 
199  CylinderSensor( Inst< SFBool > _autoOffset = 0,
200  Inst< SFString > _description = 0,
201  Inst< SFFloat > _diskAngle = 0,
202  Inst< SFBool > _enabled = 0,
203  Inst< CheckAngleLimits > _maxAngle = 0,
204  Inst< SFNode > _metadata = 0,
205  Inst< CheckAngleLimits > _minAngle = 0,
206  Inst< SFFloat > _offset = 0,
207  Inst< SFBool > _isActive = 0,
208  Inst< SFBool > _isOver = 0,
209  Inst< SFRotation > _rotation_changed = 0,
210  Inst< SFVec3f > _trackPoint_changed = 0,
211  Inst< SFRotation > _axisRotation = 0 );
212 
213  // fields
223  H3DUniquePtr< SFFloat > diskAngle;
224 
234  H3DUniquePtr< CheckAngleLimits > maxAngle;
235 
245  H3DUniquePtr< CheckAngleLimits > minAngle;
246 
255  H3DUniquePtr< SFFloat > offset;
256 
265  H3DUniquePtr< SFRotation > rotation_changed;
266 
274  H3DUniquePtr< SFRotation > axisRotation;
275 
278 
279  protected:
280  // Virtual function that should check the isActive field and if it
281  // is true then update specific fields. If isActive field is false then
282  // reset state of internal variables. Do not call enabled->getValue() in
283  // this function.
284  // _enabled - Should be treated as enabled->getValue().
285  // from - The start of the line segment for which to intersect with.
286  // to - The end of the line segment for which to intersect with.
287  virtual void setDragOutputEvents( bool _enabled,
288  const Vec3f &from,
289  const Vec3f &to );
290 
291  // Called to generate isOver events and other events which dependens on
292  // isOver.
293  virtual void onIsOver( NodeIntersectResult *result = 0, unsigned int closest_index = 0,
294  Matrix4f *global_to_local = 0 );
295 
296  // Stores points of intersection with any of the geometry which causes this
297  // sensor to generate isOver events.
298  Vec3f geometry_intersection;
299  // Stores global_to_local matrix of the geometry which causes this
300  // sensor to generate isOver events.
301  Matrix4f geometry_global_to_local;
302 
303  // Intersect segment S(t)=sa+t(sb-sa), 0<=t<=1 against cylinder specified
304  // by ca, cb and r (see below).
305  int intersectSegmentCylinder( Vec3f sa, Vec3f sb, float &t );
306 
307  // intersection between the segment from a to b and the plane defined
308  // by planeD and planeNormal (see below). Results are stored in
309  // t and q.
310  int intersectLinePlane( Vec3f a, Vec3f b, float &t, Vec3f &q );
311 
312  // Last intersection point with the cylinder. Only used when sensor is
313  // active.
314  Vec3f last_intersection;
315 
316  // Cylinder segment points. These are modified to make sure that
317  // intersection with cylinder fails only when outside the radius, not the
318  // caps.
319  Vec3f ca, cb;
320  // The y_axis, that is, the direction of the cylinder. Used to simplify
321  // some calculations since it is always the same value.
322  Vec3f y_axis;
323 
324  // The radius of the cylinder.
325  H3DFloat radius;
326  // A number used for plane equation and cylinder cap plane intersections.
327  H3DFloat plane_d;
328 
329  // The transformation from global to local of the geometry for which this
330  // sensor is active. It will not change while sensor is active.
331  Matrix4f geometry_global_to_local_original;
332 
333  // If true a new Cylinder will be defined.
334  bool new_cylinder, use_caps, prev_new_cylinder;
335  };
336 }
337 
338 #endif
Contains the SFFloat field class.
Contains the SFRotation field class.
Header file for X3DDragSensorNode, X3D scene-graph node.
The CheckAngleLimits field checks that the float is in the correct range.
Definition: CylinderSensor.h:147
virtual void update()
Make the field up to date given that an event has occured.
Definition: CylinderSensor.h:172
The CylinderSensor node maps pointer motion (e.g., a mouse or wand) into a rotation on an invisible c...
Definition: CylinderSensor.h:140
H3DUniquePtr< SFRotation > axisRotation
The local sensor coordinate system is created by applying the axisRotation field value to the local c...
Definition: CylinderSensor.h:274
H3DUniquePtr< SFFloat > offset
When the pointing device is deactivated and autoOffset is TRUE, offset is set to the last rotation an...
Definition: CylinderSensor.h:255
H3DUniquePtr< CheckAngleLimits > maxAngle
Used to clamp rotation values.
Definition: CylinderSensor.h:234
H3DUniquePtr< CheckAngleLimits > minAngle
Used to clamp rotation values.
Definition: CylinderSensor.h:245
H3DUniquePtr< SFRotation > rotation_changed
For each position of the bearing when isActive is true, a rotation_changed event is sent which corres...
Definition: CylinderSensor.h:265
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: CylinderSensor.h:277
H3DUniquePtr< SFFloat > diskAngle
Used when determining whether the sides of the invisible cylinder or the caps (disks) are used for ma...
Definition: CylinderSensor.h:223
The SFFloat field contains one single-precision floating point number.
Definition: SFFloat.h:41
virtual void setValue(const H3DFloat &v, int id=0)
Set the value of the field.
Definition: SField.h:215
virtual void update()
Make the field up to date given that an event has occured.
Definition: SField.h:181
This abstract node type is the base type for all drag-style pointing device sensors.
Definition: X3DDragSensorNode.h:42
float H3DFloat
H3D API namespace.
Definition: Anchor.h:38
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
Used as input to intersection functions.
Definition: Node.h:114