24 #include "sensorcontrollers.h"
25 #include "mathutils.h"
26 #include "phyobject.h"
27 #include "graphicalwobject.h"
45 m_activations(numSensors)
54 namespace __SingleIR_internal {
56 #define GLMultMatrix glMultMatrixf
63 const float sensorCubeSide = 0.005f;
98 SingleIRGraphic(
WObject *
object,
const wMatrix& offset,
const QVector<wVector>& startingRayPoints,
const QVector<wVector>& endingRayPoints,
bool drawRay,
bool drawRealRay, QString
name =
"unamed") :
140 GLMultMatrix(&
tm[0][0]);
143 GLMultMatrix(&m_offset[0][0]);
152 const float hside = sensorCubeSide / 2.0;
155 glColor3f(0.0, 1.0, 0.0);
156 glNormal3f(0.0, 0.0, 1.0);
157 glVertex3f(-hside, 0.0, hside);
158 glVertex3f( hside, 0.0, hside);
159 glVertex3f( hside, hside, hside);
160 glVertex3f(-hside, hside, hside);
163 glColor3f(0.0, 0.0, 0.0);
164 glNormal3f(0.0, 0.0, 1.0);
165 glVertex3f(-hside, -hside, hside);
166 glVertex3f( hside, -hside, hside);
167 glVertex3f( hside, 0.0, hside);
168 glVertex3f(-hside, 0.0, hside);
171 glNormal3f(0.0, 0.0, -1.0);
172 glVertex3f( hside, -hside, -hside);
173 glVertex3f(-hside, -hside, -hside);
174 glVertex3f(-hside, hside, -hside);
175 glVertex3f( hside, hside, -hside);
178 glNormal3f(0.0, 1.0, 0.0);
179 glVertex3f(-hside, hside, hside);
180 glVertex3f( hside, hside, hside);
181 glVertex3f( hside, hside, -hside);
182 glVertex3f(-hside, hside, -hside);
185 glNormal3f(0.0, -1.0, 0.0);
186 glVertex3f(-hside, -hside, -hside);
187 glVertex3f( hside, -hside, -hside);
188 glVertex3f( hside, -hside, hside);
189 glVertex3f(-hside, -hside, hside);
192 glNormal3f(-1.0, 0.0, 0.0);
193 glVertex3f(-hside, -hside, -hside);
194 glVertex3f(-hside, -hside, hside);
195 glVertex3f(-hside, hside, hside);
196 glVertex3f(-hside, hside, -hside);
199 glNormal3f(1.0, 0.0, 0.0);
200 glVertex3f( hside, -hside, hside);
201 glVertex3f( hside, -hside, -hside);
202 glVertex3f( hside, hside, -hside);
203 glVertex3f( hside, hside, hside);
211 glPushAttrib(GL_LIGHTING_BIT);
212 glDisable(GL_LIGHTING);
217 glColor3f(1.0, 0.0, 0.0);
224 glVertex3f(s.x, s.y, s.z);
225 glVertex3f(e.x, e.y, e.z);
227 const wVector d = (e - s).normalize().
scale(sensorCubeSide * 5.0);
228 glVertex3f(m_offset.w_pos.x, m_offset.w_pos.y, m_offset.w_pos.z);
229 glVertex3f(m_offset.w_pos.x + d.x, m_offset.w_pos.y + d.y, m_offset.w_pos.z + d.z);
286 using namespace __SingleIR_internal;
290 m_transformation(
wMatrix::identity()),
297 m_drawRealRay(false),
298 m_startingRayPoints(),
301 m_sensorGraphics(NULL)
303 m_rayCastHit.
object = NULL;
309 m_transformation(mtr),
312 m_aperture(aperture),
316 m_drawRealRay(false),
317 m_startingRayPoints(),
320 m_sensorGraphics(NULL)
322 m_rayCastHit.
object = NULL;
325 if (m_numRays != 0) {
326 m_startingRayPoints.resize(m_numRays);
327 m_endingRayPoints.resize(m_numRays);
332 updateGraphicalRepresentation();
336 m_object(other.m_object),
337 m_transformation(other.m_transformation),
338 m_minDist(other.m_minDist),
339 m_maxDist(other.m_maxDist),
340 m_aperture(other.m_aperture),
341 m_numRays(other.m_numRays),
342 m_drawSensor(other.m_drawSensor),
343 m_drawRay(other.m_drawRay),
344 m_drawRealRay(other.m_drawRealRay),
345 m_startingRayPoints(other.m_startingRayPoints),
346 m_endingRayPoints(other.m_endingRayPoints),
347 m_rayCastHit(other.m_rayCastHit),
348 m_sensorGraphics(NULL)
350 updateGraphicalRepresentation();
355 if (&other ==
this) {
360 m_object = other.m_object;
361 m_transformation = other.m_transformation;
362 m_minDist = other.m_minDist;
363 m_maxDist = other.m_maxDist;
364 m_aperture = other.m_aperture;
365 m_numRays = other.m_numRays;
366 m_drawSensor = other.m_drawSensor;
367 m_drawRay = other.m_drawRay;
368 m_drawRealRay = other.m_drawRealRay;
369 m_startingRayPoints = other.m_startingRayPoints;
370 m_endingRayPoints = other.m_endingRayPoints;
371 m_rayCastHit = other.m_rayCastHit;
373 updateGraphicalRepresentation();
391 m_rayCastHit.
object = NULL;
395 QSet<PhyObject*> ignoredObjs;
397 if (phyObj != NULL) {
398 ignoredObjs.insert(phyObj);
402 double minDist = 2.0;
403 for (
unsigned int i = 0; i < m_numRays; i++) {
409 const rayCastHitVector v = m_object->
world()->
worldRayCast(start, end,
true, ignoredObjs);
413 if ((v.size() != 0) && (v[0].distance < minDist)) {
414 minDist = v[0].distance;
424 m_transformation = mtr;
427 m_aperture = aperture;
429 if (m_numRays != 0) {
430 m_startingRayPoints.resize(m_numRays);
431 m_endingRayPoints.resize(m_numRays);
433 m_startingRayPoints.clear();
434 m_endingRayPoints.clear();
436 m_rayCastHit.
object = NULL;
440 updateGraphicalRepresentation();
445 m_drawSensor = drawSensor;
447 m_drawRealRay = drawRealRay;
449 updateGraphicalRepresentation();
452 void SingleIR::computeRayPoints()
462 const double startAngle = (m_numRays == 1) ? 0.0 : -(deg2rad(m_aperture) / 2.0);
463 const double angularIncrement = (m_numRays == 1) ? 0.0 : (deg2rad(m_aperture) / double(m_numRays - 1));
466 for (
unsigned int i = 0; i < m_numRays; i++) {
467 const double curAngle = startAngle + i * angularIncrement;
472 const wVector localStart = localRay.
scale(m_minDist);
473 const wVector localEnd = localRay.
scale(m_maxDist);
481 void SingleIR::updateGraphicalRepresentation()
483 delete m_sensorGraphics;
484 m_sensorGraphics = NULL;
487 if (m_drawSensor &&
isValid()) {
489 m_sensorGraphics =
new SingleIRGraphic(m_object, m_transformation, m_startingRayPoints, m_endingRayPoints, m_drawRay, m_drawRealRay);
507 for (
int i = 0; i < m_sensors.size(); i++) {
508 m_sensors[i].update();
510 m_activations[i] = 1.0 - m_sensors[i].getRayCastHit().distance;
516 for (
int i = 0; i < m_sensors.size(); i++) {
517 m_sensors[i].setGraphicalProperties(drawSensor, drawRay, drawRealRay);
534 for (
int i = 0; i < m_sensors.size(); i++) {
535 m_sensors[i].update();
539 if (m_sensors[i].getRayCastHit().
object != NULL) {
540 m_activations[i] = m_sensors[i].getRayCastHit().object->color().lightnessF();
549 for (
int i = 0; i < m_sensors.size(); i++) {
550 m_sensors[i].setGraphicalProperties(drawSensor, drawRay, drawRealRay);