4
* Created on: Dec 29, 2008
8
#include "distanceSensor.h"
10
#include <osgDB/WriteFile>
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),
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;
28
if ((flareAngle>0) && (flareAngle != M_PI/4) ) {
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;
37
//this->init(attachedTo, pos, orient);
39
this->type = "DistanceSensor";
42
this->sensorReturnValueType = typeid(unsigned long*).name();
46
void DistanceSensor::getSensorValue(std::ostream& theStream) const {
53
//std::ostream& operator<< (std::ostream& theStream, DistanceSensor& distSensor)
56
// std::cout << " blub " << std::endl;
57
// theStream << distSensor.value;//__getSensorValue__();
65
void DistanceSensor::evaluate(ParameterReturnValue srv)
69
osg::Image* image = new osg::Image;
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());
76
printf("%s: Viewport x: %d Viewport y: %d Höhe: %d, Breite: %d \n", this->camera->GetName().c_str(), x, y, height, width);
78
image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
79
image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE);
82
//this->value = __getSensorValue__();
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");
88
// cast void* to unsigned long int*
89
unsigned long* data = reinterpret_cast<unsigned long*> (srv.pValue);
91
// return sensor value
95
//note: this is a raw sensor value. it might be necessary to normalize the sensor value to fit in a given range.
99
void DistanceSensor::getNewSensorValue()
101
dynamic_cast<dtCore::Camera*>(sensor.get())->SetClearColor(osg::Vec4(255,255,255,255));
103
unsigned long distValue = 0xFFFFFF;
105
// get raw image data
106
osg::ref_ptr<osg::Image> image = getRawData();
108
// add red, green and blue values for all pixels
109
//for (unsigned int i=0; i<image->getImageSizeInBytes(); i += 4) {
111
if (image->valid()) {
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]);
116
//unsigned int depthValue = 5;
118
if (distValue > depthValue)
119
distValue = depthValue;
131
DistanceSensor::~DistanceSensor()