~ubuntu-branches/ubuntu/trusty/libqglviewer/trusty

« back to all changes in this revision

Viewing changes to QGLViewer/constraint.h

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2013-12-28 14:57:47 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20131228145747-qy7hqddvex3oti0c
Tags: 2.5.0-1
* [f80a053] Imported Upstream version 2.5.0
* [c07abbe] Use wrap-and-sort.
* [89d8250] Remove google adsense-scripts.
* [4224be5] Set Standards-Versions: 3.9.5. No changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
 Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4
4
 
5
 
 This file is part of the QGLViewer library version 2.4.0.
 
5
 This file is part of the QGLViewer library version 2.5.0.
6
6
 
7
7
 http://www.libqglviewer.com - contact@libqglviewer.com
8
8
 
27
27
#include "quaternion.h"
28
28
 
29
29
namespace qglviewer {
30
 
  class Frame;
31
 
  class Camera;
 
30
class Frame;
 
31
class Camera;
32
32
 
33
 
  /*! \brief An interface class for Frame constraints.
 
33
/*! \brief An interface class for Frame constraints.
34
34
  \class Constraint constraint.h QGLViewer/constraint.h
35
35
 
36
36
  This class defines the interface for the Constraints that can be applied to a Frame to limit its
47
47
  \code
48
48
  Frame::translate(Vec& T)
49
49
  {
50
 
    if (constraint())
51
 
      constraint()->constrainTranslation(T, this);
52
 
    t += T;
 
50
        if (constraint())
 
51
          constraint()->constrainTranslation(T, this);
 
52
        t += T;
53
53
  }
54
54
 
55
55
  Frame::rotate(Quaternion& Q)
56
56
  {
57
 
    if (constraint())
58
 
      constraint()->constrainRotation(Q, this);
59
 
    q *= Q;
 
57
        if (constraint())
 
58
          constraint()->constrainRotation(Q, this);
 
59
        q *= Q;
60
60
  }
61
61
  \endcode
62
62
 
88
88
  class myConstraint : public Constraint
89
89
  {
90
90
  public:
91
 
    virtual void constrainTranslation(Vec& t, Frame * const fr)
92
 
      {
93
 
        // Express t in the world coordinate system.
94
 
        const Vec tWorld = fr->inverseTransformOf(t);
 
91
        virtual void constrainTranslation(Vec& t, Frame * const fr)
 
92
          {
 
93
                // Express t in the world coordinate system.
 
94
                const Vec tWorld = fr->inverseTransformOf(t);
95
95
        if (fr->position().z + tWorld.z < 0.0) // check the new fr z coordinate
96
96
          t.z = fr->transformOf(-fr->position().z); // t.z is clamped so that next z position is 0.0
97
 
      }
 
97
          }
98
98
  };
99
99
  \endcode
100
100
 
108
108
  \code
109
109
  myConstraint::constrainTranslation(Vec& v, Frame* const fr)
110
110
  {
111
 
    constraint1->constrainTranslation(v, fr);
112
 
    constraint2->constrainTranslation(v, fr);
113
 
    // and so on, with possible branches, tests, loops...
 
111
        constraint1->constrainTranslation(v, fr);
 
112
        constraint2->constrainTranslation(v, fr);
 
113
        // and so on, with possible branches, tests, loops...
114
114
  }
115
115
  \endcode
116
116
  */
117
 
  class QGLVIEWER_EXPORT Constraint
118
 
  {
119
 
  public:
120
 
    /*! Virtual destructor. Empty. */
121
 
    virtual ~Constraint() {};
122
 
 
123
 
    /*! Filters the translation applied to the \p frame. This default implementation is empty (no
124
 
      filtering).
125
 
 
126
 
    Overload this method in your own Constraint class to define a new translation constraint. \p
127
 
    frame is the Frame to which is applied the translation. It is not defined \c const, but you
128
 
    should refrain from directly changing its value in the constraint. Use its Frame::position() and
129
 
    update the \p translation accordingly instead.
130
 
 
131
 
    \p translation is expressed in local frame coordinate system. Use Frame::inverseTransformOf() to
132
 
    express it in the world coordinate system if needed. */
133
 
    virtual void constrainTranslation(Vec& translation, Frame* const frame) { Q_UNUSED(translation); Q_UNUSED(frame); };
134
 
    /*! Filters the rotation applied to the \p frame. This default implementation is empty (no
135
 
      filtering).
136
 
 
137
 
    Overload this method in your own Constraint class to define a new rotation constraint. See
138
 
    constrainTranslation() for details.
139
 
 
140
 
    Use Frame::inverseTransformOf() on the \p rotation Quaternion::axis() to express \p rotation in
141
 
    the world coordinate system if needed. */
142
 
    virtual void constrainRotation(Quaternion& rotation, Frame* const frame) { Q_UNUSED(rotation); Q_UNUSED(frame); };
143
 
  };
144
 
 
145
 
  /*!
 
117
class QGLVIEWER_EXPORT Constraint
 
118
{
 
119
public:
 
120
        /*! Virtual destructor. Empty. */
 
121
        virtual ~Constraint() {}
 
122
 
 
123
        /*! Filters the translation applied to the \p frame. This default implementation is empty (no
 
124
          filtering).
 
125
 
 
126
        Overload this method in your own Constraint class to define a new translation constraint. \p
 
127
        frame is the Frame to which is applied the translation. It is not defined \c const, but you
 
128
        should refrain from directly changing its value in the constraint. Use its Frame::position() and
 
129
        update the \p translation accordingly instead.
 
130
 
 
131
        \p translation is expressed in local frame coordinate system. Use Frame::inverseTransformOf() to
 
132
        express it in the world coordinate system if needed. */
 
133
        virtual void constrainTranslation(Vec& translation, Frame* const frame) { Q_UNUSED(translation); Q_UNUSED(frame); }
 
134
        /*! Filters the rotation applied to the \p frame. This default implementation is empty (no
 
135
          filtering).
 
136
 
 
137
        Overload this method in your own Constraint class to define a new rotation constraint. See
 
138
        constrainTranslation() for details.
 
139
 
 
140
        Use Frame::inverseTransformOf() on the \p rotation Quaternion::axis() to express \p rotation in
 
141
        the world coordinate system if needed. */
 
142
        virtual void constrainRotation(Quaternion& rotation, Frame* const frame) { Q_UNUSED(rotation); Q_UNUSED(frame); }
 
143
};
 
144
 
 
145
/*!
146
146
   \brief An abstract class for Frame Constraints defined by an axis or a plane.
147
147
   \class AxisPlaneConstraint constraint.h QGLViewer/constraint.h
148
148
 
165
165
   However, adding an extra pointer to the QGLViewer::camera() in all the AxisPlaneConstraint
166
166
   derived classes (which the user would have to update in a multi-viewer application) was judged as
167
167
   an overkill. */
168
 
  class QGLVIEWER_EXPORT AxisPlaneConstraint : public Constraint
169
 
  {
170
 
  public:
171
 
    AxisPlaneConstraint();
172
 
    /*! Virtual destructor. Empty. */
173
 
    virtual ~AxisPlaneConstraint() {};
174
 
 
175
 
    /*! Type lists the different types of translation and rotation constraints that are available.
176
 
 
177
 
    It specifies the meaning of the constraint direction (see translationConstraintDirection() and
178
 
    rotationConstraintDirection()): as an axis direction (AxisPlaneConstraint::AXIS) or a plane
179
 
    normal (AxisPlaneConstraint::PLANE). AxisPlaneConstraint::FREE means no constraint while
180
 
    AxisPlaneConstraint::FORBIDDEN completely forbids the translation and/or the rotation.
181
 
 
182
 
    See translationConstraintType() and rotationConstraintType().
183
 
 
184
 
    \attention The AxisPlaneConstraint::PLANE Type is not valid for rotational constraint.
185
 
 
186
 
    New derived classes can use their own extended enum for specific constraints:
187
 
    \code
188
 
    class MyAxisPlaneConstraint : public AxisPlaneConstraint
189
 
    {
190
 
    public:
191
 
      enum MyType { FREE, AXIS, PLANE, FORBIDDEN, CUSTOM };
192
 
      virtual void constrainTranslation(Vec &translation, Frame *const frame)
193
 
      {
194
 
        // translationConstraintType() is simply an int. CUSTOM Type is handled seamlessly.
195
 
        switch (translationConstraintType())
 
168
class QGLVIEWER_EXPORT AxisPlaneConstraint : public Constraint
 
169
{
 
170
public:
 
171
        AxisPlaneConstraint();
 
172
        /*! Virtual destructor. Empty. */
 
173
        virtual ~AxisPlaneConstraint() {}
 
174
 
 
175
        /*! Type lists the different types of translation and rotation constraints that are available.
 
176
 
 
177
        It specifies the meaning of the constraint direction (see translationConstraintDirection() and
 
178
        rotationConstraintDirection()): as an axis direction (AxisPlaneConstraint::AXIS) or a plane
 
179
        normal (AxisPlaneConstraint::PLANE). AxisPlaneConstraint::FREE means no constraint while
 
180
        AxisPlaneConstraint::FORBIDDEN completely forbids the translation and/or the rotation.
 
181
 
 
182
        See translationConstraintType() and rotationConstraintType().
 
183
 
 
184
        \attention The AxisPlaneConstraint::PLANE Type is not valid for rotational constraint.
 
185
 
 
186
        New derived classes can use their own extended enum for specific constraints:
 
187
        \code
 
188
        class MyAxisPlaneConstraint : public AxisPlaneConstraint
196
189
        {
197
 
        case MyAxisPlaneConstraint::FREE: ... break;
198
 
        case MyAxisPlaneConstraint::CUSTOM: ... break;
199
 
        }
200
 
      };
201
 
 
202
 
      MyAxisPlaneConstraint* c = new MyAxisPlaneConstraint();
203
 
      // Note the Type conversion
204
 
      c->setTranslationConstraintType(AxisPlaneConstraint::Type(MyAxisPlaneConstraint::CUSTOM));
205
 
    };
206
 
    \endcode */
207
 
    enum Type { FREE, AXIS, PLANE, FORBIDDEN };
208
 
 
209
 
    /*! @name Translation constraint */
210
 
    //@{
211
 
    /*! Overloading of Constraint::constrainTranslation(). Empty */
212
 
    virtual void constrainTranslation(Vec& translation, Frame* const frame) { Q_UNUSED(translation); Q_UNUSED(frame); };
213
 
 
214
 
    void setTranslationConstraint(Type type, const Vec& direction);
215
 
    /*! Sets the Type() of the translationConstraintType(). Default is AxisPlaneConstraint::FREE. */
216
 
    void setTranslationConstraintType(Type type) { translationConstraintType_ = type; };
217
 
    void setTranslationConstraintDirection(const Vec& direction);
218
 
 
219
 
    /*! Returns the translation constraint Type().
220
 
 
221
 
    Depending on this value, the Frame will freely translate (AxisPlaneConstraint::FREE), will only
222
 
    be able to translate along an axis direction (AxisPlaneConstraint::AXIS), will be forced to stay
223
 
    into a plane (AxisPlaneConstraint::PLANE) or will not able to translate at all
224
 
    (AxisPlaneConstraint::FORBIDDEN).
225
 
 
226
 
    Use Frame::setPosition() to define the position of the constrained Frame before it gets
227
 
    constrained. */
228
 
    Type translationConstraintType() const { return translationConstraintType_; };
229
 
    /*! Returns the direction used by the translation constraint.
230
 
 
231
 
    It represents the axis direction (AxisPlaneConstraint::AXIS) or the plane normal
232
 
    (AxisPlaneConstraint::PLANE) depending on the translationConstraintType(). It is undefined for
233
 
    AxisPlaneConstraint::FREE or AxisPlaneConstraint::FORBIDDEN.
234
 
 
235
 
    The AxisPlaneConstraint derived classes express this direction in different coordinate system
236
 
    (camera for CameraConstraint, local for LocalConstraint, and world for WorldConstraint). This
237
 
    value can be modified with setTranslationConstraintDirection(). */
238
 
    Vec translationConstraintDirection() const { return translationConstraintDir_; };
239
 
    //@}
240
 
 
241
 
    /*! @name Rotation constraint */
242
 
    //@{
243
 
    /*! Overloading of Constraint::constrainRotation(). Empty. */
244
 
    virtual void constrainRotation(Quaternion& rotation, Frame* const frame) { Q_UNUSED(rotation); Q_UNUSED(frame); };
245
 
 
246
 
    void setRotationConstraint(Type type, const Vec& direction);
247
 
    void setRotationConstraintType(Type type);
248
 
    void setRotationConstraintDirection(const Vec& direction);
249
 
 
250
 
    /*! Returns the rotation constraint Type(). */
251
 
    Type rotationConstraintType() const { return rotationConstraintType_; };
252
 
    /*! Returns the axis direction used by the rotation constraint.
253
 
 
254
 
    This direction is defined only when rotationConstraintType() is AxisPlaneConstraint::AXIS.
255
 
 
256
 
    The AxisPlaneConstraint derived classes express this direction in different coordinate system
257
 
    (camera for CameraConstraint, local for LocalConstraint, and world for WorldConstraint). This
258
 
    value can be modified with setRotationConstraintDirection(). */
259
 
    Vec rotationConstraintDirection() const { return rotationConstraintDir_; };
260
 
    //@}
261
 
 
262
 
  private:
263
 
    // int and not Type to allow for overloading and new types definition.
264
 
    Type translationConstraintType_;
265
 
    Type rotationConstraintType_;
266
 
 
267
 
    Vec translationConstraintDir_;
268
 
    Vec rotationConstraintDir_;
269
 
  };
270
 
 
271
 
 
272
 
  /*! \brief An AxisPlaneConstraint defined in the Frame local coordinate system.
 
190
        public:
 
191
          enum MyType { FREE, AXIS, PLANE, FORBIDDEN, CUSTOM };
 
192
          virtual void constrainTranslation(Vec &translation, Frame *const frame)
 
193
          {
 
194
                // translationConstraintType() is simply an int. CUSTOM Type is handled seamlessly.
 
195
                switch (translationConstraintType())
 
196
                {
 
197
                  case MyAxisPlaneConstraint::FREE: ... break;
 
198
                  case MyAxisPlaneConstraint::CUSTOM: ... break;
 
199
                }
 
200
          };
 
201
 
 
202
          MyAxisPlaneConstraint* c = new MyAxisPlaneConstraint();
 
203
          // Note the Type conversion
 
204
          c->setTranslationConstraintType(AxisPlaneConstraint::Type(MyAxisPlaneConstraint::CUSTOM));
 
205
        };
 
206
        \endcode */
 
207
        enum Type { FREE, AXIS, PLANE, FORBIDDEN };
 
208
 
 
209
        /*! @name Translation constraint */
 
210
        //@{
 
211
        /*! Overloading of Constraint::constrainTranslation(). Empty */
 
212
        virtual void constrainTranslation(Vec& translation, Frame* const frame) { Q_UNUSED(translation); Q_UNUSED(frame); };
 
213
 
 
214
        void setTranslationConstraint(Type type, const Vec& direction);
 
215
        /*! Sets the Type() of the translationConstraintType(). Default is AxisPlaneConstraint::FREE. */
 
216
        void setTranslationConstraintType(Type type) { translationConstraintType_ = type; };
 
217
        void setTranslationConstraintDirection(const Vec& direction);
 
218
 
 
219
        /*! Returns the translation constraint Type().
 
220
 
 
221
        Depending on this value, the Frame will freely translate (AxisPlaneConstraint::FREE), will only
 
222
        be able to translate along an axis direction (AxisPlaneConstraint::AXIS), will be forced to stay
 
223
        into a plane (AxisPlaneConstraint::PLANE) or will not able to translate at all
 
224
        (AxisPlaneConstraint::FORBIDDEN).
 
225
 
 
226
        Use Frame::setPosition() to define the position of the constrained Frame before it gets
 
227
        constrained. */
 
228
        Type translationConstraintType() const { return translationConstraintType_; };
 
229
        /*! Returns the direction used by the translation constraint.
 
230
 
 
231
        It represents the axis direction (AxisPlaneConstraint::AXIS) or the plane normal
 
232
        (AxisPlaneConstraint::PLANE) depending on the translationConstraintType(). It is undefined for
 
233
        AxisPlaneConstraint::FREE or AxisPlaneConstraint::FORBIDDEN.
 
234
 
 
235
        The AxisPlaneConstraint derived classes express this direction in different coordinate system
 
236
        (camera for CameraConstraint, local for LocalConstraint, and world for WorldConstraint). This
 
237
        value can be modified with setTranslationConstraintDirection(). */
 
238
        Vec translationConstraintDirection() const { return translationConstraintDir_; };
 
239
        //@}
 
240
 
 
241
        /*! @name Rotation constraint */
 
242
        //@{
 
243
        /*! Overloading of Constraint::constrainRotation(). Empty. */
 
244
        virtual void constrainRotation(Quaternion& rotation, Frame* const frame) { Q_UNUSED(rotation); Q_UNUSED(frame); };
 
245
 
 
246
        void setRotationConstraint(Type type, const Vec& direction);
 
247
        void setRotationConstraintType(Type type);
 
248
        void setRotationConstraintDirection(const Vec& direction);
 
249
 
 
250
        /*! Returns the rotation constraint Type(). */
 
251
        Type rotationConstraintType() const { return rotationConstraintType_; };
 
252
        /*! Returns the axis direction used by the rotation constraint.
 
253
 
 
254
        This direction is defined only when rotationConstraintType() is AxisPlaneConstraint::AXIS.
 
255
 
 
256
        The AxisPlaneConstraint derived classes express this direction in different coordinate system
 
257
        (camera for CameraConstraint, local for LocalConstraint, and world for WorldConstraint). This
 
258
        value can be modified with setRotationConstraintDirection(). */
 
259
        Vec rotationConstraintDirection() const { return rotationConstraintDir_; };
 
260
        //@}
 
261
 
 
262
private:
 
263
        // int and not Type to allow for overloading and new types definition.
 
264
        Type translationConstraintType_;
 
265
        Type rotationConstraintType_;
 
266
 
 
267
        Vec translationConstraintDir_;
 
268
        Vec rotationConstraintDir_;
 
269
};
 
270
 
 
271
 
 
272
/*! \brief An AxisPlaneConstraint defined in the Frame local coordinate system.
273
273
  \class LocalConstraint constraint.h QGLViewer/constraint.h
274
274
 
275
275
  The translationConstraintDirection() and rotationConstraintDirection() are expressed in the Frame
276
276
  local coordinate system (see Frame::referenceFrame()).
277
277
 
278
278
  See the <a href="../examples/constrainedFrame.html">constrainedFrame</a> example for an illustration. */
279
 
  class QGLVIEWER_EXPORT LocalConstraint : public AxisPlaneConstraint
280
 
  {
281
 
  public:
282
 
    /*! Virtual destructor. Empty. */
283
 
    virtual ~LocalConstraint() {};
284
 
 
285
 
    virtual void constrainTranslation(Vec&     translation, Frame* const frame);
286
 
    virtual void constrainRotation   (Quaternion& rotation, Frame* const frame);
287
 
  };
288
 
 
289
 
 
290
 
 
291
 
  /*! \brief An AxisPlaneConstraint defined in the world coordinate system.
292
 
    \class WorldConstraint constraint.h QGLViewer/constraint.h
 
279
class QGLVIEWER_EXPORT LocalConstraint : public AxisPlaneConstraint
 
280
{
 
281
public:
 
282
        /*! Virtual destructor. Empty. */
 
283
        virtual ~LocalConstraint() {};
 
284
 
 
285
        virtual void constrainTranslation(Vec&     translation, Frame* const frame);
 
286
        virtual void constrainRotation   (Quaternion& rotation, Frame* const frame);
 
287
};
 
288
 
 
289
 
 
290
 
 
291
/*! \brief An AxisPlaneConstraint defined in the world coordinate system.
 
292
        \class WorldConstraint constraint.h QGLViewer/constraint.h
293
293
 
294
294
  The translationConstraintDirection() and rotationConstraintDirection() are expressed in world
295
295
  coordinate system.
296
296
 
297
297
  See the <a href="../examples/constrainedFrame.html">constrainedFrame</a> and <a
298
298
  href="../examples/multiView.html">multiView</a> examples for an illustration. */
299
 
  class QGLVIEWER_EXPORT WorldConstraint : public AxisPlaneConstraint
300
 
  {
301
 
  public:
302
 
    /*! Virtual destructor. Empty. */
303
 
    virtual ~WorldConstraint() {};
304
 
 
305
 
    virtual void constrainTranslation(Vec&     translation, Frame* const frame);
306
 
    virtual void constrainRotation   (Quaternion& rotation, Frame* const frame);
307
 
  };
308
 
 
309
 
 
310
 
 
311
 
  /*! \brief An AxisPlaneConstraint defined in the camera coordinate system.
 
299
class QGLVIEWER_EXPORT WorldConstraint : public AxisPlaneConstraint
 
300
{
 
301
public:
 
302
        /*! Virtual destructor. Empty. */
 
303
        virtual ~WorldConstraint() {};
 
304
 
 
305
        virtual void constrainTranslation(Vec&     translation, Frame* const frame);
 
306
        virtual void constrainRotation   (Quaternion& rotation, Frame* const frame);
 
307
};
 
308
 
 
309
 
 
310
 
 
311
/*! \brief An AxisPlaneConstraint defined in the camera coordinate system.
312
312
  \class CameraConstraint constraint.h QGLViewer/constraint.h
313
313
 
314
314
  The translationConstraintDirection() and rotationConstraintDirection() are expressed in the
316
316
 
317
317
  See the <a href="../examples/constrainedFrame.html">constrainedFrame</a> and <a
318
318
  href="../examples/constrainedCamera.html">constrainedCamera</a> examples for an illustration. */
319
 
  class QGLVIEWER_EXPORT CameraConstraint : public AxisPlaneConstraint
320
 
  {
321
 
  public:
322
 
    explicit CameraConstraint(const Camera* const camera);
323
 
    /*! Virtual destructor. Empty. */
324
 
    virtual ~CameraConstraint() {};
325
 
 
326
 
    virtual void constrainTranslation(Vec&     translation, Frame* const frame);
327
 
    virtual void constrainRotation   (Quaternion& rotation, Frame* const frame);
328
 
 
329
 
    /*! Returns the associated Camera. Set using the CameraConstraint constructor. */
330
 
    const Camera* camera() const { return camera_; };
331
 
 
332
 
  private:
333
 
    const Camera* const camera_;
334
 
  };
 
319
class QGLVIEWER_EXPORT CameraConstraint : public AxisPlaneConstraint
 
320
{
 
321
public:
 
322
        explicit CameraConstraint(const Camera* const camera);
 
323
        /*! Virtual destructor. Empty. */
 
324
        virtual ~CameraConstraint() {};
 
325
 
 
326
        virtual void constrainTranslation(Vec&     translation, Frame* const frame);
 
327
        virtual void constrainRotation   (Quaternion& rotation, Frame* const frame);
 
328
 
 
329
        /*! Returns the associated Camera. Set using the CameraConstraint constructor. */
 
330
        const Camera* camera() const { return camera_; };
 
331
 
 
332
private:
 
333
        const Camera* const camera_;
 
334
};
335
335
 
336
336
} // namespace qglviewer
337
337