~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to src/srCore/sensor/lightSensor.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
#include "lightSensor.h"
 
2
#include <math.h>
 
3
#include <osgDB/WriteFile>
 
4
 
 
5
namespace srCore {
 
6
 
 
7
LightSensor::LightSensor(dtCore::RefPtr<BodyBase> attachedTo, osg::Vec3 pos, osg::Vec3 orient, float flareAngle, const std::string &name):
 
8
        CameraSensorBase(attachedTo, pos, orient, name, "LightSensor", RGB_CAMERA, true)
 
9
{
 
10
        // setup light sensor
 
11
        // light sensor actually consists of a grid of 30x30 pixels. These values
 
12
        // will be added to get a more accurate sensor value.
 
13
        this->minDistance = 1;
 
14
        this->maxDistance = 100;
 
15
        this->resolution  = osg::Vec2(30,30);
 
16
        this->cameraType = RGB_CAMERA;
 
17
 
 
18
 
 
19
        if ((flareAngle>0) && (flareAngle != M_PI/4) ) {
 
20
 
 
21
                this->frustumDown = tan(flareAngle/2) * this->minDistance;
 
22
                this->frustumUp = tan(flareAngle/2) * this->minDistance;
 
23
                this->frustumRight = tan(flareAngle/2) * this->minDistance;
 
24
                this->frustumLeft = tan(flareAngle/2) * this->minDistance;
 
25
 
 
26
        }
 
27
 
 
28
        //this->init(attachedTo, pos, orient);
 
29
        this->setup();
 
30
        this->type = "LightSensor";
 
31
 
 
32
 
 
33
        this->sensorReturnValueType = typeid(unsigned long*).name();
 
34
 
 
35
}
 
36
 
 
37
void LightSensor::getSensorValue(std::ostream& theStream) const {
 
38
 
 
39
        theStream << value;
 
40
}
 
41
 
 
42
 
 
43
// DO NOT USE THIS FUNCTION DIRECTLY, USE THE STREAM OPERATOR INSTEAD !!!
 
44
void LightSensor::getNewSensorValue() {
 
45
 
 
46
    dynamic_cast<dtCore::Camera*>(sensor.get())->SetClearColor(osg::Vec4(255,255,255,255));
 
47
 
 
48
        unsigned long lightValue = 0;
 
49
 
 
50
        // get raw image data
 
51
        osg::ref_ptr<osg::Image> image = getRawData();
 
52
 
 
53
        // add red, green and blue values for all pixels
 
54
        for (unsigned int i=0; i<image->getImageSizeInBytes(); i++)
 
55
                lightValue += image->data()[i];
 
56
 
 
57
 
 
58
        this->value = (int) (lightValue / 2700);
 
59
}
 
60
 
 
61
 
 
62
void LightSensor::evaluate(ParameterReturnValue srv)
 
63
{
 
64
 
 
65
        /*
 
66
        osg::Image* image = new osg::Image;
 
67
 
 
68
        int x = static_cast<int>(this->camera->GetOSGCamera()->getViewport()->x());
 
69
        int y = static_cast<int>(this->camera->GetOSGCamera()->getViewport()->y());
 
70
        unsigned int width = static_cast<unsigned int>(this->camera->GetOSGCamera()->getViewport()->width());
 
71
        unsigned int height = static_cast<unsigned int>(this->camera->GetOSGCamera()->getViewport()->height());
 
72
 
 
73
        printf("%s: Viewport x: %d Viewport y: %d Höhe: %d, Breite: %d \n", this->camera->GetName().c_str(), x, y, height, width);
 
74
 
 
75
        image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
 
76
        image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE);
 
77
        */
 
78
 
 
79
 
 
80
        // sensor value type check
 
81
        if (srv.type != typeid(unsigned long*).name())
 
82
                printf("Parameter type needs to be unsigned long*. Casting could lead to errors.  \n");
 
83
 
 
84
        // cast void* to unsigned long int*
 
85
        unsigned long* data = reinterpret_cast<unsigned long*> (srv.pValue);
 
86
 
 
87
        *data = value;
 
88
        srv.pValue = data;
 
89
        // return sensor value
 
90
 
 
91
        //srv.pValue = &(this->value);
 
92
 
 
93
        //note: this is a raw sensor value. it might be necessary to normalize the sensor value to fit in a given range.
 
94
 
 
95
}
 
96
 
 
97
 
 
98
LightSensor::~LightSensor()
 
99
{
 
100
}
 
101
 
 
102
}