Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SceneBase.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __SCENEBASE_HH__
26 #define __SCENEBASE_HH__
27 
28 #include <bias_config.h>
29 
30 #ifndef BIAS_HAVE_OPENGL
31 # error You need BIAS with USE_OpenGL Please recompile BIAS.
32 #endif // BUILD_GLviewer
33 
34 #include <list>
35 #include <Base/Geometry/HomgPoint3D.hh>
36 #include <Base/Geometry/HomgPlane3D.hh>
37 
38 #include <Gui/biasgl.h>
39 #include <Base/Math/Vector3.hh>
40 #include <Base/Debug/Debug.hh>
41 
42 
43 namespace BIAS {
44 
53  };
54 
58  };
59 
60  //forward declarations
61  class BIASGLviewer_EXPORT GLProjectionParametersInterface;
62 
63  /** \class SceneBase
64  \brief Base class for all scenes.
65  \ingroup g_glviewer
66  \author bartczak
67  */
68  class BIASGLviewer_EXPORT SceneBase : public BIAS::Debug
69  {
70  public:
71  enum EChannel { C_BothMono, C_LeftOnly, C_RightOnly, C_BothStereo };
72 
73  /**\brief constructor*/
74  SceneBase();
75  /**\brief destructor*/
76  virtual ~SceneBase(){};
77 
78  /**\brief Set the camera as projectionparametersinterface, can be of type
79  * GLProjection of of any from GLProjectionParametersBase derived class*/
81  { camera_ = nc; IsActive_=true; };
82 
83  /**\brief Get the camera as projectionparametersinterface, can be of type
84  * GLProjection of of any from GLProjectionParametersBase derived class*/
86  { return camera_; };
87 
88  /**\brief Select a scene element/primitive at position p.
89  * Overwrite in derived class to react to selections
90  * \param p [in]: position to select object at.
91  * \return false if nothing could be done, true on success */
92  virtual bool SelectObjectAt(BIAS::Vector3<double>& p){return false;};
93  /** \brief Reset the selection, overwrite to react on calls to
94  * this function in derived classes.*/
95  virtual void ResetSelection(){};
96 
97  /**\brief Set the drawing style for points*/
98  void SetPointDrawStyle(POINT_DRAW_STYLE style){dPointDrawStyle_ = style;};
99  /**\brief Get the drawing style for points*/
100  POINT_DRAW_STYLE GetPointDrawStyle(){return dPointDrawStyle_;};
101 
102  /**\brief Set the drawing style for triangles*/
104  { dTriangleMeshDrawStyle_ = style;};
105  /**\brief Get the drawing style for triangles*/
106  TRIANGLEMESH_DRAW_STYLE GetTriangleMeshDrawStyle(){return dTriangleMeshDrawStyle_;};
107 
108  /**\brief Set whether to render the boundign box or not*/
109  virtual void SetRenderBoundingBox(bool render)
110  { renderBoundingBox_ = render; }
111 
112  /**\brief Method is called by GLProjection if its params have changed.
113  The method should then take necassary steps for adapting
114  the rendering state. Needed information should then be fetched
115  through the camera_ pointer.
116  This method is only called if the
117  abstract set of parameters has changed, say the parameters
118  that are also contained in BIAS::Projection */
119  virtual void UpdateCameraRelatedState() {}
120 
121  /** \brief this is the entry point seen from GLProjectionParametersBase.
122  It is called whenever redrawing is required. It checks the
123  IsActive flag and eventually call Draw(). */
124  virtual void Render() ;
125 
126  /**\brief Returns whether the scene is active or not*/
127  bool IsActive() const {return IsActive_;};
128  /**\brief Set whether the scene is active or not*/
129  void SetActive(bool b) {IsActive_ = b;};
130 
131  /**\brief Returns the active color channel*/
132  enum EChannel GetActiveChannel() const
133  { return ActiveChannel_; }
134  /**\brief Set a color channel active*/
135  void SetActiveChannel(const enum EChannel channel)
136  { ActiveChannel_ = channel; }
137 
138  /** \brief sets a color mask for the scene rendering */
139  void SetColorMask(bool red=true, bool green=true, bool blue=true,bool alpha=true)
140  {
141  colorMask_[0] = (red) ? GL_TRUE : GL_FALSE;
142  colorMask_[1] = (green) ? GL_TRUE : GL_FALSE;
143  colorMask_[2] = (blue) ? GL_TRUE : GL_FALSE;
144  colorMask_[3] = (alpha) ? GL_TRUE : GL_FALSE;
145  }
146 
147  /** \brief Determine and return the bouning box of all elements of the scene*/
149  {
150  themin = BIAS::Vector3<double>(0.0, 0.0, 0.0);
151  themax = BIAS::Vector3<double>(0.0, 0.0, 0.0);
152  }
153 
154  /** @brief check if bounding boxes of two scenes overlap */
155  virtual bool HasBoundingBoxCollision(SceneBase* otherScene);
156 
157  /** @brief compute time to contact of bounding boxes */
158  virtual double TimeToContact(SceneBase* otherScene,
159  BIAS::Vector3<double>& collisionpoint);
160 
161  /** @brief get mass and velocity in m/s */
162  void GetMomentum(double& mass, BIAS::Vector3<double>& velocity) const {
163  mass = mass_;
164  velocity = velocity_;
165  }
166 
167  /** @brief set mass and velocity in m/s */
168  void SetMomentum(const double& mass,
169  const BIAS::Vector3<double>& velocity) {
170  mass_ = mass;
171  velocity_ = velocity;
172  }
173 
174 
175  /** @brief set collision behaviour:
176  * 1.0=elastic, no energy is lost,
177  * >1 superelastic (requires energy from outside)
178  * <1 plastic, converts kinetic into other energy */
179  void SetElasticity(const double& elas) {
180  elasticity_ = elas;
181  }
182 
183  /**\brief returns the elasticity value*/
184  double GetElasticity(){
185  return elasticity_;
186  }
187 
188  /** @brief set behaviour with respect to gravity */
189  void SetGravityImmunity(bool i) {
190  gravityImmunity_ = i;
191  }
192 
193  /** @brief is this scene accelerated by gravity ? */
194  bool GetGravityImmunity() const {
195  return gravityImmunity_;
196  }
197 
198  /** @brief compute momentum of each scene after collision
199  *
200  * see http://de.wikipedia.org/wiki/Sto%C3%9F_(Physik)
201  * @param contactnormal the normal of the plane between the two objects
202  * at the moment they collide (imagine a piece of paper in between)
203  */
204  virtual void PerformCollision(SceneBase* otherscene,
205  const BIAS::Vector3<double>& contactnormal=
206  BIAS::Vector3<double>(-1e10,-1e10,-1e10));
207 
208  /** @brief change position using a small time step of delta seconds
209  * and change velocity using gravity vector in m/s^2
210  *
211  * No collision detection and handling here, since other scenes are
212  * unknown at this level.
213  */
214  void ApplyTimeStep(const double& delta = 1.0,
215  const BIAS::Vector3<double>& gravity =
216  BIAS::Vector3<double>(0,-9.81,0),
217  const double& frictioncoefficient = 0.999);
218 
219  /** @brief call this method for a vector of scenes to solve the collision
220  problem. STATIC and THUS CONST !!! */
221  static void AnimationAndCollision(std::vector<SceneBase*> scenes,
222  const BIAS::Vector3<double>& gravity =
223  BIAS::Vector3<double>(0,10,0),
224  const double& friction = 0.999);
225 
226  /**\brief dump info about the scene */
227  void DumpInfo() const;
228 
229  /** @brief this moves the scene (no rotation yet) */
230  virtual void MoveScene(const double& delta = 1.0);
231 
232 
233  std::list<BIAS::HomgPoint3D > CornersFromBoundingBox(const BIAS::Vector3<double>& b1,
234  const BIAS::Vector3<double>& b2);
235  std::list<BIAS::HomgPlane3D > FacesFromBoundingBox(const BIAS::Vector3<double>& b1,
236  const BIAS::Vector3<double>& b2);
237 
238  /** @brief render the bounding box of the scene using gl-lines */
239  virtual void RenderBoundingBox();
240 
241  /** @brief set time quantization for animations */
242  static void SetAnimationSpeed(double s) {
243  animationSpeed_ = s;
244  }
245 
246  /** @brief get time quantization for animations */
247  static double GetAnimationSpeed() {
248  return animationSpeed_;
249  }
250 
251  protected:
252 
253  /// how long does a single step in time take for animations ?
254  static double animationSpeed_;
255  /** \brief To do anything usefull, overload this method, assume context is
256  * ready and draw. Draw() is called from Render() only if the
257  * scene object is active (IsActive == true). */
258  virtual void Draw() = 0;
259 
262 
263  bool IsActive_;
264 
265  enum EChannel ActiveChannel_;
267  GLboolean colorMask_[4];
268 
269  /// for collision:
270  double mass_;
271  /// scene velocity in m/s
273  /// if true, scene is not accelerated by gravity
275  /// the more elastic the less enegry is lost in collisions
276  double elasticity_;
277  /// show the bounding box ?
279  };
280 }//end namespace
281 
282 #endif // __SCENEBASE_HH__
bool renderBoundingBox_
show the bounding box ?
Definition: SceneBase.hh:278
virtual void UpdateCameraRelatedState()
Method is called by GLProjection if its params have changed.
Definition: SceneBase.hh:119
static double animationSpeed_
how long does a single step in time take for animations ?
Definition: SceneBase.hh:254
bool gravityImmunity_
if true, scene is not accelerated by gravity
Definition: SceneBase.hh:274
void SetGravityImmunity(bool i)
set behaviour with respect to gravity
Definition: SceneBase.hh:189
void SetTriangleMeshDrawStyle(TRIANGLEMESH_DRAW_STYLE style)
Set the drawing style for triangles.
Definition: SceneBase.hh:103
TRIANGLEMESH_DRAW_STYLE dTriangleMeshDrawStyle_
Definition: SceneBase.hh:261
virtual bool SelectObjectAt(BIAS::Vector3< double > &p)
Select a scene element/primitive at position p.
Definition: SceneBase.hh:92
void SetPointDrawStyle(POINT_DRAW_STYLE style)
Set the drawing style for points.
Definition: SceneBase.hh:98
double GetElasticity()
returns the elasticity value
Definition: SceneBase.hh:184
virtual ~SceneBase()
destructor
Definition: SceneBase.hh:76
bool GetGravityImmunity() const
is this scene accelerated by gravity ?
Definition: SceneBase.hh:194
void SetActive(bool b)
Set whether the scene is active or not.
Definition: SceneBase.hh:129
Abstract interface class to handle changes in rendering parameters by controllers and in rendering co...
void GetMomentum(double &mass, BIAS::Vector3< double > &velocity) const
get mass and velocity in m/s
Definition: SceneBase.hh:162
virtual void SetRenderBoundingBox(bool render)
Set whether to render the boundign box or not.
Definition: SceneBase.hh:109
static void SetAnimationSpeed(double s)
set time quantization for animations
Definition: SceneBase.hh:242
bool IsActive() const
Returns whether the scene is active or not.
Definition: SceneBase.hh:127
BIAS::Vector3< double > velocity_
scene velocity in m/s
Definition: SceneBase.hh:272
POINT_DRAW_STYLE GetPointDrawStyle()
Get the drawing style for points.
Definition: SceneBase.hh:100
double mass_
for collision:
Definition: SceneBase.hh:270
void SetElasticity(const double &elas)
set collision behaviour: 1.0=elastic, no energy is lost, &gt;1 superelastic (requires energy from outsid...
Definition: SceneBase.hh:179
POINT_DRAW_STYLE dPointDrawStyle_
Definition: SceneBase.hh:260
virtual GLProjectionParametersInterface * GetGLProjectionParametersInterface()
Get the camera as projectionparametersinterface, can be of type GLProjection of of any from GLProject...
Definition: SceneBase.hh:85
virtual void ResetSelection()
Reset the selection, overwrite to react on calls to this function in derived classes.
Definition: SceneBase.hh:95
TRIANGLEMESH_DRAW_STYLE
Definition: SceneBase.hh:55
TRIANGLEMESH_DRAW_STYLE GetTriangleMeshDrawStyle()
Get the drawing style for triangles.
Definition: SceneBase.hh:106
class BIASGLviewer_EXPORT GLProjectionParametersInterface
Base class for all scenes.
Definition: SceneBase.hh:68
POINT_DRAW_STYLE
Definition: SceneBase.hh:45
void SetActiveChannel(const enum EChannel channel)
Set a color channel active.
Definition: SceneBase.hh:135
static double GetAnimationSpeed()
get time quantization for animations
Definition: SceneBase.hh:247
double elasticity_
the more elastic the less enegry is lost in collisions
Definition: SceneBase.hh:276
virtual void GetBoundingBox(BIAS::Vector3< double > &themin, BIAS::Vector3< double > &themax)
Determine and return the bouning box of all elements of the scene.
Definition: SceneBase.hh:148
void SetMomentum(const double &mass, const BIAS::Vector3< double > &velocity)
set mass and velocity in m/s
Definition: SceneBase.hh:168
void SetColorMask(bool red=true, bool green=true, bool blue=true, bool alpha=true)
sets a color mask for the scene rendering
Definition: SceneBase.hh:139
virtual void SetGLProjectionParametersInterface(GLProjectionParametersInterface *nc)
Set the camera as projectionparametersinterface, can be of type GLProjection of of any from GLProject...
Definition: SceneBase.hh:80
BIAS::GLProjectionParametersInterface * camera_
Definition: SceneBase.hh:266