4
* Created on: Nov 21, 2008
8
#ifndef SIMULATIONUTILS_H_
9
#define SIMULATIONUTILS_H_
11
#include <dtGame/messagetype.h>
12
#include <dtGame/messagefactory.h>
13
#include <dtGame/gameapplication.h>
14
#include <dtCore/scene.h>
15
#include <dtCore/view.h>
16
#include <dtCore/light.h>
18
#include <dtCore/uniqueid.h>
21
#include <osgShadow/ShadowedScene>
22
#include <osgShadow/SoftShadowMap>
23
#include <osgShadow/ShadowMap>
24
#include <osgViewer/View>
26
#include <osgDB/WriteFile>
29
#include <srCore/export.h>
33
// TODO: Everything I did not know where to put, ended up here. Restructuring here the code might be necessary.
40
* OpenGL type of camera (sensor) of basically two types: DEPTH_CAMERA and RGB_CAMERA.
50
//! screen shot call back class
51
class ScreenShotCallback : public osg::Camera::DrawCallback
54
ScreenShotCallback(glType type, osg::Viewport* viewport) :
55
image (new osg::Image),
57
mTakeScreenShotNextFrame(false),
62
void setRequestDataToTrue()
64
mTakeScreenShotNextFrame = true;
67
// overwrite operator() of DrawCallback to retrieve the right image
68
// image will be retrieved after the simulation step, right after it is written
69
// to the viewport. Only then, it is possible to get the corresponding image, as
70
// many cameras use the same viewport.
71
virtual void operator()(const osg::Camera &camera) const
74
if(mTakeScreenShotNextFrame)
76
mTakeScreenShotNextFrame = false;
79
int x = static_cast<int>(camera.getViewport()->x());
80
int y = static_cast<int>(camera.getViewport()->y());
81
unsigned int width = static_cast<unsigned int>(camera.getViewport()->width());
82
unsigned int height = static_cast<unsigned int>(camera.getViewport()->height());
84
// std::cout << "cam viewport: " << x << ", " << y << ", " << width << ", " << height << std::endl;
89
image->allocateImage(width, height, 1, GL_DEPTH_COMPONENT, GL_FLOAT); //GL_RGB, GL_UNSIGNED_BYTE
90
image->readPixels(x, y, width, height, GL_DEPTH_COMPONENT, GL_FLOAT); //GL_DEPTH_COMPONENT
94
image->allocateImage(x + width, y + height, 1, GL_RGB, GL_UNSIGNED_BYTE); //GL_RGB, GL_UNSIGNED_BYTE
95
image->readPixels(0, 0, x + width, y + height, GL_RGB, GL_UNSIGNED_BYTE); //GL_DEPTH_COMPONENT
97
if ((filename != "") && (image->getImageSizeInBytes() > 0)) {
98
osgDB::writeImageFile(*image, filename);
107
// unsigned char *dst;
108
// glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, dst);
113
void takeScreenShot(const std::string filename)
115
this->filename = filename;
116
setRequestDataToTrue();
121
osg::ref_ptr<osg::Image> image;
123
std::string filename;
124
osg::Viewport* viewport;
126
mutable bool mTakeScreenShotNextFrame;
133
* Class: ParameterReturnValue
135
* A small helper class for type-value pairs. In this case it is represented by a
136
* "string"-"void pointer" pair.
138
class ParameterReturnValue
141
ParameterReturnValue(const std::string typeName, void* returnValue):
143
pValue (returnValue) {}
145
~ParameterReturnValue() {}
147
const std::string type;
152
* Function: MAKE_PARAMETER_VALUE
154
* Returns a default ParameterReturnValue class, in which the string gets the C++ name
155
* of the given typename.
158
* ParameterReturnValue
161
inline ParameterReturnValue MAKE_PARAMETER_VALUE(T *t) {
162
return ParameterReturnValue(typeid(t).name(), t);
165
class ROBOT_EXPORT SimulationMessageType : public dtGame::MessageType
167
DECLARE_ENUM(SimulationMessageType);
179
static const SimulationMessageType COMM_BUS;
181
static void RegisterMessageTypes(dtGame::MessageFactory& factory);
185
SimulationMessageType( const std::string &name,
186
const std::string &category,
187
const std::string &description,
188
const unsigned short messageId) :
189
dtGame::MessageType(name, category, description, messageId)
194
virtual ~SimulationMessageType() { }
197
class CommMessage : public dtGame::Message
201
CommMessage(SimulationMessageType::commType type, dtCore::UniqueId* id,
202
const std::string& message);
204
virtual ~CommMessage();
206
void setBusID(dtCore::UniqueId* uniqueId );
207
dtCore::UniqueId* getBusID() const;
209
void setMessage(const std::string &message );
210
const std::string& getMessage() const;
212
void setCommType(SimulationMessageType::commType type);
213
const SimulationMessageType::commType& getCommType() const;
216
dtCore::UniqueId* busID;
218
SimulationMessageType::commType type;
225
* Calculates min(abs(t1), abs(t2))
228
* minimum of two values, both taken absolute
231
inline T minAbs(T t1, T t2){
232
if (fabs(t1)<fabs(t2)) return fabs(t1);
233
else return fabs(t2);
236
//! Returns max(abs(t1), abs(t2)) -- used for robotUniKarl movement principle
238
inline T maxAbs(T t1, T t2){
239
if (fabs(t1)>fabs(t2)) return fabs(t1);
240
else return fabs(t2);
243
class SimulationUtils {
247
virtual ~SimulationUtils();
249
static void NearCallback(void* data, dGeomID o1, dGeomID o2);
251
static void setSceneFile(std::string file) {sceneFile = file;};
252
static std::string getSceneFile() {return sceneFile;};
254
static inline bool getDetailedGraphics() {return detailedGraphics;};
255
static inline void setDetailedGraphics(bool set) {detailedGraphics = set;};
256
static inline bool getWithShadows() {return withShadows;};
257
static inline void setWithShadows(bool set) {withShadows = set;};
258
static inline std::string getStageProjectPath() {return stageProjectPath;};
259
static inline void setStageProjectPath(std::string path) {stageProjectPath = path;};
260
static inline bool getConnectorsAsJoints() {return connectorsAsJoints;};
261
static inline void setConnectorsAsJoints(bool asJoints) { connectorsAsJoints = asJoints;};
262
static inline void setWithWheels(bool robotsWithWheels) { withWheels = robotsWithWheels;};
263
static inline bool getWithWheels() {return withWheels;};
264
static inline int getCollisionGeometryDetails() {return collisionGeometryDetails;};
265
static inline void setCollisionGeometryDetails(int details) {collisionGeometryDetails = details;};
267
static osg::ref_ptr<osgShadow::ShadowedScene> getShadowScene();
269
static bool fileExists(std::string fileName);
272
static std::string sceneFile;
273
static bool detailedGraphics;
274
static bool withShadows;
275
static bool withWheels;
277
static std::string stageProjectPath;
279
static int geometryDetailsNodeMask;
280
static int collisionNodeMask;
281
static int laserRayNodeMask;
283
static int collisionGeometryDetails;
284
static bool connectorsAsJoints;
285
static dJointGroupID mContactJointGroupID;
290
static osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene;
296
#endif /* SIMULATIONUTILS_H_ */