24 #include "sensorcontrollers.h"
25 #include "mathutils.h"
26 #include "phyobject.h"
27 #include "graphicalwobject.h"
45 m_activations(numSensors),
46 m_activeSensor(numSensors, true)
60 namespace __SingleIR_internal {
62 #define GLMultMatrix glMultMatrixf
69 const float sensorCubeSide = 0.005f;
104 SingleIRGraphic(
WObject *
object,
const wMatrix& offset,
const QVector<wVector>& startingRayPoints,
const QVector<wVector>& endingRayPoints,
bool drawRay,
bool drawRealRay, QString
name =
"unamed") :
149 GLMultMatrix(&
tm[0][0]);
152 GLMultMatrix(&m_offset[0][0]);
161 const float hside = sensorCubeSide / 2.0;
164 glColor3f(0.0, 1.0, 0.0);
165 glNormal3f(0.0, 0.0, 1.0);
166 glVertex3f(-hside, 0.0, hside);
167 glVertex3f( hside, 0.0, hside);
168 glVertex3f( hside, hside, hside);
169 glVertex3f(-hside, hside, hside);
172 glColor3f(0.0, 0.0, 0.0);
173 glNormal3f(0.0, 0.0, 1.0);
174 glVertex3f(-hside, -hside, hside);
175 glVertex3f( hside, -hside, hside);
176 glVertex3f( hside, 0.0, hside);
177 glVertex3f(-hside, 0.0, hside);
180 glNormal3f(0.0, 0.0, -1.0);
181 glVertex3f( hside, -hside, -hside);
182 glVertex3f(-hside, -hside, -hside);
183 glVertex3f(-hside, hside, -hside);
184 glVertex3f( hside, hside, -hside);
187 glNormal3f(0.0, 1.0, 0.0);
188 glVertex3f(-hside, hside, hside);
189 glVertex3f( hside, hside, hside);
190 glVertex3f( hside, hside, -hside);
191 glVertex3f(-hside, hside, -hside);
194 glNormal3f(0.0, -1.0, 0.0);
195 glVertex3f(-hside, -hside, -hside);
196 glVertex3f( hside, -hside, -hside);
197 glVertex3f( hside, -hside, hside);
198 glVertex3f(-hside, -hside, hside);
201 glNormal3f(-1.0, 0.0, 0.0);
202 glVertex3f(-hside, -hside, -hside);
203 glVertex3f(-hside, -hside, hside);
204 glVertex3f(-hside, hside, hside);
205 glVertex3f(-hside, hside, -hside);
208 glNormal3f(1.0, 0.0, 0.0);
209 glVertex3f( hside, -hside, hside);
210 glVertex3f( hside, -hside, -hside);
211 glVertex3f( hside, hside, -hside);
212 glVertex3f( hside, hside, hside);
220 glPushAttrib(GL_LIGHTING_BIT);
221 glDisable(GL_LIGHTING);
226 glColor3f(1.0, 0.0, 0.0);
233 glVertex3f(s.x, s.y, s.z);
234 glVertex3f(e.x, e.y, e.z);
236 const wVector d = (e - s).normalize().
scale(sensorCubeSide * 5.0);
237 glVertex3f(m_offset.w_pos.x, m_offset.w_pos.y, m_offset.w_pos.z);
238 glVertex3f(m_offset.w_pos.x + d.x, m_offset.w_pos.y + d.y, m_offset.w_pos.z + d.z);
295 using namespace __SingleIR_internal;
299 m_transformation(
wMatrix::identity()),
306 m_drawRealRay(false),
307 m_startingRayPoints(),
310 m_sensorGraphics(NULL)
312 m_rayCastHit.
object = NULL;
318 m_transformation(mtr),
321 m_aperture(aperture),
325 m_drawRealRay(false),
326 m_startingRayPoints(),
329 m_sensorGraphics(NULL)
331 m_rayCastHit.
object = NULL;
334 if (m_numRays != 0) {
335 m_startingRayPoints.resize(m_numRays);
336 m_endingRayPoints.resize(m_numRays);
341 updateGraphicalRepresentation();
345 m_object(other.m_object),
346 m_transformation(other.m_transformation),
347 m_minDist(other.m_minDist),
348 m_maxDist(other.m_maxDist),
349 m_aperture(other.m_aperture),
350 m_numRays(other.m_numRays),
351 m_drawSensor(other.m_drawSensor),
352 m_drawRay(other.m_drawRay),
353 m_drawRealRay(other.m_drawRealRay),
354 m_startingRayPoints(other.m_startingRayPoints),
355 m_endingRayPoints(other.m_endingRayPoints),
356 m_rayCastHit(other.m_rayCastHit),
357 m_sensorGraphics(NULL)
359 updateGraphicalRepresentation();
364 if (&other ==
this) {
369 m_object = other.m_object;
370 m_transformation = other.m_transformation;
371 m_minDist = other.m_minDist;
372 m_maxDist = other.m_maxDist;
373 m_aperture = other.m_aperture;
374 m_numRays = other.m_numRays;
375 m_drawSensor = other.m_drawSensor;
376 m_drawRay = other.m_drawRay;
377 m_drawRealRay = other.m_drawRealRay;
378 m_startingRayPoints = other.m_startingRayPoints;
379 m_endingRayPoints = other.m_endingRayPoints;
380 m_rayCastHit = other.m_rayCastHit;
382 updateGraphicalRepresentation();
400 m_rayCastHit.
object = NULL;
404 QSet<PhyObject*> ignoredObjs;
406 if (phyObj != NULL) {
407 ignoredObjs.insert(phyObj);
411 double minDist = 2.0;
412 for (
unsigned int i = 0; i < m_numRays; i++) {
418 const rayCastHitVector v = m_object->
world()->
worldRayCast(start, end,
true, ignoredObjs);
422 if ((v.size() != 0) && (v[0].distance < minDist)) {
423 minDist = v[0].distance;
433 m_transformation = mtr;
436 m_aperture = aperture;
438 if (m_numRays != 0) {
439 m_startingRayPoints.resize(m_numRays);
440 m_endingRayPoints.resize(m_numRays);
442 m_startingRayPoints.clear();
443 m_endingRayPoints.clear();
445 m_rayCastHit.
object = NULL;
449 updateGraphicalRepresentation();
454 m_drawSensor = drawSensor;
456 m_drawRealRay = drawRealRay;
458 updateGraphicalRepresentation();
461 void SingleIR::computeRayPoints()
471 const double startAngle = (m_numRays == 1) ? 0.0 : -(deg2rad(m_aperture) / 2.0);
472 const double angularIncrement = (m_numRays == 1) ? 0.0 : (deg2rad(m_aperture) / double(m_numRays - 1));
475 for (
unsigned int i = 0; i < m_numRays; i++) {
476 const double curAngle = startAngle + i * angularIncrement;
481 const wVector localStart = localRay.
scale(m_minDist);
482 const wVector localEnd = localRay.
scale(m_maxDist);
490 void SingleIR::updateGraphicalRepresentation()
492 delete m_sensorGraphics;
493 m_sensorGraphics = NULL;
496 if (m_drawSensor &&
isValid()) {
498 m_sensorGraphics =
new SingleIRGraphic(m_object, m_transformation, m_startingRayPoints, m_endingRayPoints, m_drawRay, m_drawRealRay);
519 for (
int i = 0; i < m_sensors.size(); i++) {
521 m_sensors[i].update();
523 m_activations[i] = 1.0 - m_sensors[i].getRayCastHit().distance;
536 m_sensors[i].setGraphicalProperties(m_drawSensor, m_drawRay, m_drawRealRay);
538 m_sensors[i].setGraphicalProperties(
false, m_drawRay, m_drawRealRay);
544 m_drawSensor = drawSensor;
546 m_drawRealRay = drawRealRay;
548 for (
int i = 0; i < m_sensors.size(); i++) {
549 m_sensors[i].setGraphicalProperties(
m_activeSensor[i] && drawSensor, drawRay, drawRealRay);
570 for (
int i = 0; i < m_sensors.size(); i++) {
572 m_sensors[i].update();
576 if (m_sensors[i].getRayCastHit().
object != NULL) {
577 m_activations[i] = m_sensors[i].getRayCastHit().object->color().lightnessF();
593 m_sensors[i].setGraphicalProperties(m_drawSensor, m_drawRay, m_drawRealRay);
595 m_sensors[i].setGraphicalProperties(
false, m_drawRay, m_drawRealRay);
601 m_drawSensor = drawSensor;
603 m_drawRealRay = drawRealRay;
605 for (
int i = 0; i < m_sensors.size(); i++) {
606 m_sensors[i].setGraphicalProperties(
m_activeSensor[i] && drawSensor, drawRay, drawRealRay);