~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srCore/simulationUtils.h

  • Committer: Anne van Rossum
  • Date: 2010-08-10 15:58:55 UTC
  • Revision ID: anne@gamix-20100810155855-kve7x2vwouagdij9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * simulationUtils.h
 
3
 *
 
4
 *  Created on: Nov 21, 2008
 
5
 *      Author: winkler
 
6
 */
 
7
 
 
8
#ifndef SIMULATIONUTILS_H_
 
9
#define SIMULATIONUTILS_H_
 
10
 
 
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>
 
17
 
 
18
#include <dtCore/uniqueid.h>
 
19
#include <typeinfo>
 
20
 
 
21
#include <osgShadow/ShadowedScene>
 
22
#include <osgShadow/SoftShadowMap>
 
23
#include <osgShadow/ShadowMap>
 
24
#include <osgViewer/View>
 
25
 
 
26
#include <osgDB/WriteFile>
 
27
#include <osg/Camera>
 
28
 
 
29
#include <srCore/export.h>
 
30
 
 
31
#include <ode/ode.h>
 
32
 
 
33
// TODO: Everything I did not know where to put, ended up here. Restructuring here the code might be necessary.
 
34
 
 
35
namespace srCore {
 
36
 
 
37
/*
 
38
 * Enumeration: glType
 
39
 *
 
40
 * OpenGL type of camera (sensor) of basically two types: DEPTH_CAMERA and RGB_CAMERA.
 
41
 */
 
42
enum glType
 
43
{
 
44
        NO_CAMERA,
 
45
        DEPTH_CAMERA,
 
46
        RGB_CAMERA
 
47
 
 
48
};
 
49
 
 
50
//! screen shot call back class
 
51
class ScreenShotCallback : public osg::Camera::DrawCallback
 
52
{
 
53
public:
 
54
        ScreenShotCallback(glType type, osg::Viewport* viewport) :
 
55
                image (new osg::Image),
 
56
                viewport (viewport),
 
57
                mTakeScreenShotNextFrame(false),
 
58
                type (type)
 
59
 
 
60
        {}
 
61
 
 
62
        void setRequestDataToTrue()
 
63
        {
 
64
                mTakeScreenShotNextFrame  = true;
 
65
        }
 
66
 
 
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
 
72
        {
 
73
 
 
74
                if(mTakeScreenShotNextFrame)
 
75
                {
 
76
                        mTakeScreenShotNextFrame = false;
 
77
 
 
78
 
 
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());
 
83
 
 
84
                        //      std::cout << "cam viewport: " << x << ", " << y << ", " << width << ", " << height << std::endl;
 
85
 
 
86
                        switch (type) {
 
87
 
 
88
                        case DEPTH_CAMERA:
 
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
 
91
                                break;
 
92
 
 
93
                        case RGB_CAMERA:
 
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
 
96
 
 
97
                                if ((filename != "") && (image->getImageSizeInBytes() > 0)) {
 
98
                                        osgDB::writeImageFile(*image, filename);
 
99
                                }
 
100
 
 
101
                                break;
 
102
 
 
103
                        default:
 
104
                                break;
 
105
 
 
106
                        }
 
107
                        //                      unsigned char *dst;
 
108
                        //                      glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, dst);
 
109
 
 
110
                }
 
111
        }
 
112
 
 
113
        void takeScreenShot(const std::string filename)
 
114
        {
 
115
                this->filename = filename;
 
116
                setRequestDataToTrue();
 
117
        }
 
118
 
 
119
protected:
 
120
 
 
121
        osg::ref_ptr<osg::Image> image;
 
122
 
 
123
        std::string filename;
 
124
        osg::Viewport* viewport;
 
125
 
 
126
        mutable bool  mTakeScreenShotNextFrame;
 
127
        glType type;
 
128
 
 
129
 
 
130
};
 
131
 
 
132
/*
 
133
 * Class: ParameterReturnValue
 
134
 *
 
135
 * A small helper class for type-value pairs. In this case it is represented by a
 
136
 * "string"-"void pointer" pair.
 
137
 */
 
138
class ParameterReturnValue
 
139
{
 
140
public:
 
141
        ParameterReturnValue(const std::string typeName, void* returnValue):
 
142
                type (typeName),
 
143
                pValue (returnValue) {}
 
144
 
 
145
        ~ParameterReturnValue() {}
 
146
 
 
147
        const std::string type;
 
148
        void* pValue;
 
149
};
 
150
 
 
151
/*
 
152
 * Function: MAKE_PARAMETER_VALUE
 
153
 *
 
154
 * Returns a default ParameterReturnValue class, in which the string gets the C++ name
 
155
 * of the given typename.
 
156
 *
 
157
 * Returns:
 
158
 *   ParameterReturnValue
 
159
 */
 
160
template<class T>
 
161
inline ParameterReturnValue MAKE_PARAMETER_VALUE(T *t) {
 
162
        return ParameterReturnValue(typeid(t).name(), t);
 
163
};
 
164
 
 
165
class ROBOT_EXPORT SimulationMessageType : public dtGame::MessageType
 
166
{
 
167
        DECLARE_ENUM(SimulationMessageType);
 
168
 
 
169
        enum commType
 
170
        {
 
171
                NONE,
 
172
                GLOBAL,
 
173
                ORGANISM,
 
174
                LOCAL
 
175
        };
 
176
 
 
177
public:
 
178
 
 
179
        static const SimulationMessageType COMM_BUS;
 
180
 
 
181
        static void RegisterMessageTypes(dtGame::MessageFactory& factory);
 
182
 
 
183
protected:
 
184
 
 
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)
 
190
        {
 
191
                AddInstance(this);
 
192
        }
 
193
 
 
194
        virtual ~SimulationMessageType() { }
 
195
};
 
196
 
 
197
class CommMessage : public dtGame::Message
 
198
{
 
199
public:
 
200
        CommMessage();
 
201
        CommMessage(SimulationMessageType::commType type, dtCore::UniqueId* id,
 
202
                        const std::string& message);
 
203
protected:
 
204
        virtual ~CommMessage();
 
205
public:
 
206
        void setBusID(dtCore::UniqueId* uniqueId );
 
207
        dtCore::UniqueId* getBusID() const;
 
208
 
 
209
        void setMessage(const std::string &message );
 
210
        const std::string& getMessage() const;
 
211
 
 
212
        void setCommType(SimulationMessageType::commType type);
 
213
        const SimulationMessageType::commType& getCommType() const;
 
214
 
 
215
private:
 
216
        dtCore::UniqueId* busID;
 
217
        std::string message;
 
218
        SimulationMessageType::commType type;
 
219
};
 
220
 
 
221
 
 
222
/*
 
223
 * Function: minAbs
 
224
 *
 
225
 * Calculates min(abs(t1), abs(t2))
 
226
 *
 
227
 * Returns:
 
228
 *   minimum of two values, both taken absolute
 
229
 */
 
230
template<class T>
 
231
inline T minAbs(T t1, T t2){
 
232
        if (fabs(t1)<fabs(t2)) return fabs(t1);
 
233
        else return fabs(t2);
 
234
}
 
235
 
 
236
//! Returns max(abs(t1), abs(t2))       --      used for robotUniKarl movement principle
 
237
template<class T>
 
238
inline T maxAbs(T t1, T t2){
 
239
        if (fabs(t1)>fabs(t2)) return fabs(t1);
 
240
        else return fabs(t2);
 
241
}
 
242
 
 
243
class SimulationUtils {
 
244
public:
 
245
 
 
246
        SimulationUtils();
 
247
        virtual ~SimulationUtils();
 
248
 
 
249
        static void NearCallback(void* data, dGeomID o1, dGeomID o2);
 
250
 
 
251
        static void setSceneFile(std::string file) {sceneFile = file;};
 
252
        static std::string getSceneFile() {return sceneFile;};
 
253
 
 
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;};
 
266
 
 
267
        static osg::ref_ptr<osgShadow::ShadowedScene> getShadowScene();
 
268
 
 
269
        static bool fileExists(std::string fileName);
 
270
 
 
271
        //private:
 
272
        static std::string sceneFile;
 
273
        static bool detailedGraphics;
 
274
        static bool withShadows;
 
275
        static bool withWheels;
 
276
 
 
277
        static std::string stageProjectPath;
 
278
 
 
279
        static int geometryDetailsNodeMask;
 
280
        static int collisionNodeMask;
 
281
        static int laserRayNodeMask;
 
282
 
 
283
        static int collisionGeometryDetails;
 
284
        static bool connectorsAsJoints;
 
285
        static dJointGroupID mContactJointGroupID;
 
286
 
 
287
private:
 
288
 
 
289
        
 
290
        static osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene;
 
291
        static bool startup;
 
292
 
 
293
};
 
294
 
 
295
}
 
296
#endif /* SIMULATIONUTILS_H_ */