~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to src/srCore/sensor/distanceSensor.cpp

  • 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
 * distanceSensor.cpp
 
3
 *
 
4
 *  Created on: Dec 29, 2008
 
5
 *      Author: winkler
 
6
 */
 
7
 
 
8
#include "distanceSensor.h"
 
9
#include <math.h>
 
10
#include <osgDB/WriteFile>
 
11
 
 
12
namespace srCore {
 
13
 
 
14
 
 
15
DistanceSensor::DistanceSensor(dtCore::RefPtr<BodyBase> attachedTo, osg::Vec3 pos, osg::Vec3 orient, float flareAngle, const std::string &name):
 
16
        CameraSensorBase(attachedTo, pos, orient, name, "DistanceSensor", DEPTH_CAMERA, true),
 
17
        value (0)
 
18
{
 
19
        // setup light sensor
 
20
        // light sensor actually consists of a grid of 30x30 pixels. These values
 
21
        // will be added to get a more accurate sensor value.
 
22
        this->minDistance = 0.3;
 
23
        this->maxDistance = 100;
 
24
        this->resolution  = osg::Vec2(30,30);
 
25
        this->cameraType = DEPTH_CAMERA;
 
26
 
 
27
 
 
28
        if ((flareAngle>0) && (flareAngle != M_PI/4) ) {
 
29
 
 
30
                this->frustumDown = tan(flareAngle/2) * this->minDistance;
 
31
                this->frustumUp = tan(flareAngle/2) * this->minDistance;
 
32
                this->frustumRight = tan(flareAngle/2) * this->minDistance;
 
33
                this->frustumLeft = tan(flareAngle/2) * this->minDistance;
 
34
 
 
35
        }
 
36
 
 
37
        //this->init(attachedTo, pos, orient);
 
38
        this->setup();
 
39
        this->type = "DistanceSensor";
 
40
 
 
41
 
 
42
        this->sensorReturnValueType = typeid(unsigned long*).name();
 
43
 
 
44
}
 
45
 
 
46
void DistanceSensor::getSensorValue(std::ostream& theStream) const {
 
47
 
 
48
 
 
49
        theStream << value;
 
50
 
 
51
}
 
52
 
 
53
//std::ostream& operator<< (std::ostream& theStream, DistanceSensor& distSensor)
 
54
//{
 
55
//      distSensor.blub();
 
56
//      std::cout << " blub " << std::endl;
 
57
//      theStream << distSensor.value;//__getSensorValue__();
 
58
//      return theStream;
 
59
//
 
60
//}
 
61
 
 
62
 
 
63
 
 
64
 
 
65
void DistanceSensor::evaluate(ParameterReturnValue srv)
 
66
{
 
67
 
 
68
        /*
 
69
        osg::Image* image = new osg::Image;
 
70
 
 
71
        int x = static_cast<int>(this->camera->GetOSGCamera()->getViewport()->x());
 
72
        int y = static_cast<int>(this->camera->GetOSGCamera()->getViewport()->y());
 
73
        unsigned int width = static_cast<unsigned int>(this->camera->GetOSGCamera()->getViewport()->width());
 
74
        unsigned int height = static_cast<unsigned int>(this->camera->GetOSGCamera()->getViewport()->height());
 
75
 
 
76
        printf("%s: Viewport x: %d Viewport y: %d Höhe: %d, Breite: %d \n", this->camera->GetName().c_str(), x, y, height, width);
 
77
 
 
78
        image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
 
79
        image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE);
 
80
        */
 
81
 
 
82
        //this->value = __getSensorValue__();
 
83
 
 
84
        // sensor value type check
 
85
        if (srv.type != typeid(unsigned long*).name())
 
86
                printf("Parameter type needs to be unsigned long*. Casting could lead to errors.  \n");
 
87
 
 
88
        // cast void* to unsigned long int*
 
89
        unsigned long* data = reinterpret_cast<unsigned long*> (srv.pValue);
 
90
 
 
91
        // return sensor value
 
92
        *data = this->value;
 
93
        srv.pValue = data;
 
94
 
 
95
        //note: this is a raw sensor value. it might be necessary to normalize the sensor value to fit in a given range.
 
96
 
 
97
}
 
98
 
 
99
void DistanceSensor::getNewSensorValue()
 
100
{
 
101
                dynamic_cast<dtCore::Camera*>(sensor.get())->SetClearColor(osg::Vec4(255,255,255,255));
 
102
 
 
103
                unsigned long distValue = 0xFFFFFF;
 
104
 
 
105
                // get raw image data
 
106
                osg::ref_ptr<osg::Image> image = getRawData();
 
107
 
 
108
                // add red, green and blue values for all pixels
 
109
                //for (unsigned int i=0; i<image->getImageSizeInBytes(); i += 4) {
 
110
 
 
111
                if (image->valid()) {
 
112
 
 
113
                        unsigned int i = ((image->getImageSizeInBytes()) >> 1) & ~(0x3);
 
114
                        unsigned long depthValue = (image->data()[i+2] << 16) | (image->data()[i+1] << 8) | (image->data()[i]);
 
115
 
 
116
                        //unsigned int depthValue = 5;
 
117
 
 
118
                        if (distValue > depthValue)
 
119
                                distValue = depthValue;
 
120
 
 
121
                        //}
 
122
 
 
123
                        value = distValue;
 
124
 
 
125
                }
 
126
}
 
127
 
 
128
 
 
129
 
 
130
 
 
131
DistanceSensor::~DistanceSensor()
 
132
{
 
133
}
 
134
 
 
135
}