Alvaro Cassinelli / Mbed 2 deprecated laserUI

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Scene.cpp Source File

Scene.cpp

00001 #include "Scene.h"
00002 //#include "hardwareIO.h" //(for tests using serial port only)
00003 
00004 Scene scene; // pre-instantiated GLOBAL (cross file object, declared extern in the Scene.h header)
00005 
00006 //extern LaserRenderer lsr; // Use of the global object lsr (LaserRenderer type). Object is pre-instantiated in LaserRenderer.cpp
00007  
00008  using namespace std;
00009 
00010 //======================================= BASE OBJECT CLASS =======================================
00011 //BaseObject::BaseObject() {}
00012 
00013 BaseObject::~BaseObject() {
00014  // in principle, we don't need to do anything: 
00015  // the vertexArray is a vector that does NOT contain pointers; also myDisplaySensingBuffer is destroyed by calling its destructor.
00016 }
00017 
00018 
00019 void BaseObject::addVertex(V3& _v3, Mat44& _RT) { // passing the current modelview (lsr.RT) as a parameter seems cleaner (more encapsulated class) than using the global lsr object...
00020     vertexArray.push_back(_RT*_v3); 
00021 }
00022 
00023 void BaseObject::transform(Mat44& _RT) { // this transform all the vertices of the object by _RT
00024     for (unsigned short i=0; i<vertexArray.size(); i++) vertexArray[i]=_RT*vertexArray[i];
00025 }
00026 
00027 void BaseObject::clear() { // this deletes all the vertices (and their projections) in the object:
00028     vertexArray.clear();
00029     displaySensingBuffer.lsdTrajectory.clear();
00030 }
00031 
00032 /*  THIS PRODUCE A CIRCULAR REFERENCE!
00033 void BaseObject::render(LaserRenderer* ptr_lsr) {
00034   // Use the lsr methods: again, this would be kind of convoluted if these objects belongs to a scene that belongs to lsr...
00035   ptr_lsr->renderObject(this);
00036 */
00037 
00038  //Or do it "directly" (but again, we need to use data from the lsr state machine): 
00039 /*
00040 // First, clear the current lsdTrajectory:
00041   Object.myDisplaySensingBuffer.lsdTrajectory.clear();
00042  if (lsr.renderingMode==PROJECTION)
00043     for (int i=0; i<vertexArray.size(); i++) {
00044     LaserPoint newLp=lsr.renderPointProj(Object.vertexArray[i]); // or use a render method for a LaserPoint or an extended V3 class (or directly)
00045     // Set per-vertex color too? No, for the time being one color per object... 
00046     // newLp.myColor=color;
00047     Object.myDisplaySensingBuffer.lsdTrajectory.push_back(newLp);
00048  }
00049  else
00050     for (int i=0; i<Object.vertexArray.size(); i++) {
00051     LaserPoint newLp=lsr.renderPointRaw(Object.vertexArray[i]);
00052     // Set per-vertex color too? No, for the time being one color per object... 
00053     // newLp.myColor=color;
00054     Object.myDisplaySensingBuffer.lsdTrajectory.push_back(newLp);
00055     }
00056 }
00057 }
00058 */
00059 
00060 Box3d BaseObject::getEnclosingBox() {
00061     // This will give the 3d enclosing box for the object, in LOCAL coordinates. 
00062     // (for the time being, let's compute every time we query about it. In the future we can optimize by computing only when the object changed)
00063     if (vertexArray.size()==0) {
00064     enclosingBox.minX=enclosingBox.maxX=enclosingBox.minY=enclosingBox.maxY=enclosingBox.minZ=enclosingBox.maxZ=0;
00065     } 
00066     else 
00067     { 
00068        enclosingBox.minX=enclosingBox.maxX=vertexArray[0].x;
00069        enclosingBox.minY=enclosingBox.maxY=vertexArray[0].y;
00070        enclosingBox.minZ=enclosingBox.maxZ=vertexArray[0].z;
00071         for (unsigned short i=1; i<vertexArray.size(); i++) {
00072             if (vertexArray[i].x>enclosingBox.maxX) enclosingBox.maxX=vertexArray[i].x;
00073             else if (vertexArray[i].x<enclosingBox.minX) enclosingBox.minX=vertexArray[i].x;
00074             if (vertexArray[i].y>enclosingBox.maxY) enclosingBox.maxY=vertexArray[i].y;
00075             else if (vertexArray[i].y<enclosingBox.minY) enclosingBox.minY=vertexArray[i].y;
00076         }
00077       }
00078     return (enclosingBox);
00079 }
00080 
00081  // Sensing methods (query and process sensed data): 
00082  // That this is separated from displaying routine make sense: we can query at ANY time for new data,
00083  // and this is uncorrelated with the display buffer continuous to work, displaying and sensing things.
00084  // ALSO, it is separated from the LaserRenderer: this routine does not uses any "state machine" data.  
00085  bool BaseObject::sense() {return(this->displaySensingBuffer.processSensedData());}
00086 
00087  // Max and Min intensity RATIOS (normalized between 0 and 255):
00088 unsigned char BaseObject::maxIntensity(void) {displaySensingBuffer.processSensedData(); return displaySensingBuffer.maxI;}
00089 unsigned char BaseObject::minIntensity(void) {displaySensingBuffer.processSensedData(); return displaySensingBuffer.minI;}
00090     
00091 
00092 //======================================= THE SCENE CLASS =======================================
00093 
00094 //Scene::Scene() : numTouchedObjects(0) {}
00095 
00096 Scene::~Scene() {
00097     this->clear(); // necessary because Scene object variable objectArray is an vector of POINTERS and we need to free memory for the pointed objects. 
00098 }
00099 
00100 void Scene::clear() {
00101  //NOTE: objectArray stores POINTERS to objects; we need therefore to delete first the object itself:
00102   for (int i=0; i<this->totalObjects(); i++) {
00103     // pc.printf("deleting object: %d\n", i);
00104      delete objectArray[i]; // this call the destructor for BaseObject
00105    }
00106     objectArray.clear(); // clear the vector of pointers
00107     ptr_currentObject=NULL;
00108  }
00109  
00110 void Scene::addObject(BaseObject* ptr_newObject) {
00111     objectArray.push_back(ptr_newObject); // note: the object pointed by ptr_newObject has been instantiated OUTSIDE this method. 
00112     //ptr_currentObject=ptr_newObject;
00113 }
00114 
00115 void Scene::addCurrentObject() { // this adds the "current" object (pointed by ptr_currentObject)
00116     objectArray.push_back(ptr_currentObject);
00117   // Note: the current object pointerd by ptr_currentObject can be BaseObject... or any child class. This is not a problem as long as the methods applied to the 
00118   // scene vector array don't use child methods (then we can do a dynamic cast before including in the array: pb = dynamic_cast<CBase*>(&d); ). IF we want
00119   // the scene class to be able to use child methods, then we need to make BaseObject polymorphic, by declaring all usable methods VIRTUAL.
00120  
00121 }
00122 
00123 void Scene::transform(Mat44& _RT) { // this transform all the objects of the scene by _RT
00124     for (int i=0; i<totalObjects(); i++) objectArray[i]->transform(_RT);
00125 }
00126 
00127 // ATTENTION: deleting objects imply stoping the displaying engine, and updating. This is mandatory, not in the case of ADDING objects...
00128 void Scene::deleteObject(int _id) {
00129     // We could use an STL map, but here I will do the matching manually:
00130     for (int i=0; i<totalObjects(); i++) 
00131         if ( (objectArray[i]->ID()) == _id) objectArray.erase(objectArray.begin()+i); // note: I don't stop the for-loop. I delete ALL objects with this ID 
00132     // Not sure I will use the "current pointer", but if I do, then we need to decide what it becomes here... let's point to the last element in the vector: 
00133     if (!objectArray.empty()) ptr_currentObject=objectArray.back(); else ptr_currentObject=NULL;
00134 }
00135 
00136 // number of objects in the scene:
00137 int Scene::totalObjects() {return(objectArray.size());}
00138 
00139 // total number of points in the scene:
00140 int Scene::totalPoints() {
00141 int ttlpoints=0;
00142 for (unsigned short i=0; i<objectArray.size(); i++) ttlpoints+=objectArray[i]->size();
00143 return(ttlpoints);
00144 }
00145     
00146 /*
00147 void Scene::render(LaserRenderer* ptr_lsr) {
00148     ptr_lsr->renderScene(this);
00149     // Or, if one want to use the object render method (that also calls lsr methods anyway, so it is heavier):
00150     //  for (int i=0; i<Scene.size(); i++) renderObject(ptr_scene->objectArray[i]);
00151 }
00152 */
00153 
00154  int Scene::sense() {
00155  numTouchedObjects=0;
00156  for (int i=0; i<totalObjects(); i++) 
00157     if (objectArray[i]->displaySensingBuffer.processSensedData()) numTouchedObjects++;
00158  //touchedScene=(numTouchedObjects>0);
00159  return(numTouchedObjects); 
00160 }
00161 
00162