Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Thu Apr 17 08:04:14 2014 +0000
Revision:
47:199042980678
Parent:
45:5ef809480c12
publishing for sharing with Ken Iwasaki

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 40:3ba2b0ea9f33 1 #ifndef SCENE_H
mbedalvaro 40:3ba2b0ea9f33 2 #define SCENE_H
mbedalvaro 40:3ba2b0ea9f33 3
mbedalvaro 40:3ba2b0ea9f33 4 #include <vector>
mbedalvaro 40:3ba2b0ea9f33 5 #include "matrixClass.h"
mbedalvaro 40:3ba2b0ea9f33 6 #include "classLaserSensingTrajectory.h" // BaseObjet use instances of the classes LaserPoint and LaserSensingTrajectory (cannot forward declare only)
mbedalvaro 40:3ba2b0ea9f33 7 //#include "LaserRenderer.h"
mbedalvaro 40:3ba2b0ea9f33 8
mbedalvaro 40:3ba2b0ea9f33 9 // Forward declared classes (attention: a pointer to an "incomplete class" - eg, only forward declared - is not allowed. It is just like unsing an actual object...)
mbedalvaro 40:3ba2b0ea9f33 10 class LaserRenderer;
mbedalvaro 40:3ba2b0ea9f33 11 class laserSensingDisplay;
mbedalvaro 40:3ba2b0ea9f33 12
mbedalvaro 40:3ba2b0ea9f33 13 // A base class for an "object":
mbedalvaro 40:3ba2b0ea9f33 14 // Note: all the 3d coordinates of the object are in PROJECTOR coordinates in fact... we don't store a POSE for each object, but instead the
mbedalvaro 44:2432c218f191 15 // final RT*vi where vi are the points of the objects in local coordinates.
mbedalvaro 40:3ba2b0ea9f33 16 // Also, we store IN THE OBJECT the list of projected points (that is, the 3d points projected by using K). The object has methods to study this
mbedalvaro 40:3ba2b0ea9f33 17 // list (because projected points are also "sensed") and get information about light or dark zones.
mbedalvaro 40:3ba2b0ea9f33 18 // Now, to be able to test things (where the object was "touched", etc), we may need to mantain a correspondence between the projected points
mbedalvaro 40:3ba2b0ea9f33 19 // and the original 3d point. There are two options: put them into a single structure "laserpoint", or use two separate arrays.
mbedalvaro 40:3ba2b0ea9f33 20 class BaseObject {
mbedalvaro 40:3ba2b0ea9f33 21 public:
mbedalvaro 40:3ba2b0ea9f33 22
mbedalvaro 40:3ba2b0ea9f33 23 friend class Scene;
mbedalvaro 40:3ba2b0ea9f33 24 friend class LaserRenderer;
mbedalvaro 40:3ba2b0ea9f33 25 friend class laserSensingDisplay;
mbedalvaro 40:3ba2b0ea9f33 26
mbedalvaro 40:3ba2b0ea9f33 27 BaseObject(unsigned int _id=0):myID(_id) {}
mbedalvaro 40:3ba2b0ea9f33 28 ~BaseObject();
mbedalvaro 40:3ba2b0ea9f33 29
mbedalvaro 40:3ba2b0ea9f33 30 // Setters:
mbedalvaro 40:3ba2b0ea9f33 31 void setID(int _id) {myID=_id;}
mbedalvaro 40:3ba2b0ea9f33 32 void setColor( unsigned char _color) {myColor=_color;} // color is coded in three bits in fact (for RGB laser), or more or less...
mbedalvaro 40:3ba2b0ea9f33 33 // Per-object adjustement of mirror parameters (could be automated if we know the number of points, etc. For the time being, this is manual):
mbedalvaro 40:3ba2b0ea9f33 34
mbedalvaro 40:3ba2b0ea9f33 35 // Getters:
mbedalvaro 40:3ba2b0ea9f33 36 int size() {return(vertexArray.size());} // ATTN: in principle, the vertexArray size and the myDisplaySensingBuffer number of points should be equal!! we could check this here?
mbedalvaro 40:3ba2b0ea9f33 37 int color() {return(myColor);}
mbedalvaro 40:3ba2b0ea9f33 38 int ID() {return (myID);}
mbedalvaro 40:3ba2b0ea9f33 39 Box3d getEnclosingBox();
mbedalvaro 40:3ba2b0ea9f33 40
mbedalvaro 40:3ba2b0ea9f33 41 // Building methods:
mbedalvaro 40:3ba2b0ea9f33 42 // NOTE: I will try to have an encapsulated class, so I prefer not to refer to global variables (like the renderer lsr)
mbedalvaro 45:5ef809480c12 43 void addVertex(V3& _v3);// add a vertex in LOCAL COORDINATES
mbedalvaro 45:5ef809480c12 44 void addVertex(V3& _v3, Mat44& _RT); // this will be usefull to add a vertex USING THE CURRENT MODELVIEW (in global lsr object) when
mbedalvaro 45:5ef809480c12 45 // creating objects using the "open-gl" like "begin/end" wrappers. NOTE: it is NOT projected and added to the myDisplaySensingBuffer yet.
mbedalvaro 40:3ba2b0ea9f33 46 void clear(); // this deletes all the vertices and their projections from *this BaseObject instance
mbedalvaro 40:3ba2b0ea9f33 47
mbedalvaro 45:5ef809480c12 48 // Transformation of vertices:
mbedalvaro 40:3ba2b0ea9f33 49 void transform(Mat44& _RT); // this transform all the vertices of the object by _RT
mbedalvaro 40:3ba2b0ea9f33 50
mbedalvaro 40:3ba2b0ea9f33 51 // void render(LaserRenderer* ptr_lsr); // this will project ALL the 3d points in the object vertexArray and store into display sensing buffer (myDisplaySensingBuffer), using
mbedalvaro 40:3ba2b0ea9f33 52 // the current projection matrix and the current projecting mode in the global lsr object. NOTE: THE PREVIOUS DISPLAY BUFFER for this object is CLEARED. This ensures that when
mbedalvaro 40:3ba2b0ea9f33 53 // "rendered", we have the same number of projected and 3d points
mbedalvaro 40:3ba2b0ea9f33 54
mbedalvaro 40:3ba2b0ea9f33 55 // Sensing methods (note: only way to get data - touched, not touched, etc - through getters that will re-execute the processing on new data...
mbedalvaro 40:3ba2b0ea9f33 56 // (we could have a boolean to check if new data is available, but since the displaying is done very fast, it is likely that we may need to re-compute
mbedalvaro 40:3ba2b0ea9f33 57 // the processing anyway...)
mbedalvaro 40:3ba2b0ea9f33 58 bool sense(void);
mbedalvaro 40:3ba2b0ea9f33 59 unsigned char maxIntensity(void);
mbedalvaro 40:3ba2b0ea9f33 60 unsigned char minIntensity(void);
mbedalvaro 40:3ba2b0ea9f33 61
mbedalvaro 40:3ba2b0ea9f33 62 // Virtual methods? for more complex testing - like overall direction, or even motion in case of "sprites"
mbedalvaro 40:3ba2b0ea9f33 63 // ...
mbedalvaro 40:3ba2b0ea9f33 64 // NOTE: as always, I run into this annoying problem: if I have a vector of pointers to the base class, AND virtual methods, then I can store all the
mbedalvaro 40:3ba2b0ea9f33 65 // childrens pointers in the vector (using downcast "dynamic type casting" which won't slice the object). But then, it means the base class declaration
mbedalvaro 40:3ba2b0ea9f33 66 // needs to know somehting about the children methods! (at least their types and names). THIS IS REALLY ANNOYING, because I would like to add new kind
mbedalvaro 40:3ba2b0ea9f33 67 // of children objects without needing to modify the base class declaration! How to do that? The thing is, sometimes I may need to call to a child method;
mbedalvaro 40:3ba2b0ea9f33 68 // if the pointer points to an object that does not have this method implemented, NOTHING should happen (pure virtual function). Otherwise, it should work
mbedalvaro 40:3ba2b0ea9f33 69 // as specified by this child. This very much reminds me to weakly typed Objective-C polymorphic arrays... (NSArrays). How to do this in C++?
mbedalvaro 40:3ba2b0ea9f33 70
mbedalvaro 40:3ba2b0ea9f33 71 unsigned char myColor; // per object color (public because it will be used intensively by the displaying engine)
mbedalvaro 40:3ba2b0ea9f33 72
mbedalvaro 40:3ba2b0ea9f33 73 LaserSensingTrajectory displaySensingBuffer;
mbedalvaro 40:3ba2b0ea9f33 74
mbedalvaro 40:3ba2b0ea9f33 75 private:
mbedalvaro 40:3ba2b0ea9f33 76
mbedalvaro 40:3ba2b0ea9f33 77 // PRIVATE INSTANCE VARIABLES:
mbedalvaro 40:3ba2b0ea9f33 78 int myID;
mbedalvaro 40:3ba2b0ea9f33 79
mbedalvaro 40:3ba2b0ea9f33 80 // Array of 3d coordinates and corresponding 2D projections with sense data:
mbedalvaro 40:3ba2b0ea9f33 81 // These are private, so we can ensure that their size is the same...
mbedalvaro 40:3ba2b0ea9f33 82 vector<V3> vertexArray;
mbedalvaro 40:3ba2b0ea9f33 83 //LaserSensingTrajectory displaySensingBuffer; // the object displaySensingBuffer contains the trajectory of v2 projected points, as well
mbedalvaro 40:3ba2b0ea9f33 84 // as some methods to perform the analysis of the saccade. NOTE: I could have made BaseObject a CHILD of the clas LaserSensingTrajectory. But I prefer to
mbedalvaro 40:3ba2b0ea9f33 85 // have a clear separation between sensing parameters "tweaking" (like mirror delays, setting methods and variables) and the more pure graphical BaseObject
mbedalvaro 40:3ba2b0ea9f33 86
mbedalvaro 40:3ba2b0ea9f33 87 Box3d enclosingBox; // this will be filled when rendering the object or when explicitly computing the enclosing box
mbedalvaro 47:199042980678 88
mbedalvaro 47:199042980678 89 // FUTURE WORK:
mbedalvaro 47:199042980678 90 // - OBJECTS should have their own transformation matrix, and methods (inherited from a base class?) to modify it or recreate it. This way we will
mbedalvaro 47:199042980678 91 // avoid transforming the object 3d points independently, which will create problems because approximations. Eventually, these objects will have
mbedalvaro 47:199042980678 92 // behaviours that will affect this transformation matrix (as a function of time). When rendering an object, the object original 3d points will be first modified by
mbedalvaro 47:199042980678 93 // the object's own RT, THEN by the global modelview. This is exactly like in an openGL program...
mbedalvaro 40:3ba2b0ea9f33 94 };
mbedalvaro 40:3ba2b0ea9f33 95
mbedalvaro 40:3ba2b0ea9f33 96 // ==============================================================================================================================================
mbedalvaro 40:3ba2b0ea9f33 97
mbedalvaro 40:3ba2b0ea9f33 98
mbedalvaro 40:3ba2b0ea9f33 99 // The scene, which is just a collection of objets, plus some methods to add objects, etc:
mbedalvaro 40:3ba2b0ea9f33 100 class Scene {
mbedalvaro 40:3ba2b0ea9f33 101 public:
mbedalvaro 40:3ba2b0ea9f33 102
mbedalvaro 40:3ba2b0ea9f33 103 friend class LaserRenderer;
mbedalvaro 40:3ba2b0ea9f33 104 friend class laserSensingDisplay;
mbedalvaro 40:3ba2b0ea9f33 105
mbedalvaro 40:3ba2b0ea9f33 106 Scene(): numTouchedObjects(0) {clear();}
mbedalvaro 40:3ba2b0ea9f33 107 ~Scene();
mbedalvaro 40:3ba2b0ea9f33 108
mbedalvaro 40:3ba2b0ea9f33 109 // TO THINK ABOUT: METHODS TO TRANSFORM THE COORDINATES OF THE POINTS IN THE SCENE (affecting 3d coordinates) and to RERENDER,
mbedalvaro 40:3ba2b0ea9f33 110 // or even TO TRANSFORM AND RENDER (but without modifying the original 3d points).
mbedalvaro 40:3ba2b0ea9f33 111
mbedalvaro 40:3ba2b0ea9f33 112 // Create/destroy new objects:
mbedalvaro 40:3ba2b0ea9f33 113 // TWO WAYS to create objects:
mbedalvaro 40:3ba2b0ea9f33 114 // (1) Use a strategy somehow similar to OpenGL, by using a "begin/end" structure
mbedalvaro 40:3ba2b0ea9f33 115 // (2) Making a specific object class inheriting from Object struct (or class), and then ADD to the Scene:
mbedalvaro 40:3ba2b0ea9f33 116
mbedalvaro 40:3ba2b0ea9f33 117 void addObject(BaseObject* ptr_newObject);
mbedalvaro 40:3ba2b0ea9f33 118 void addCurrentObject(); // this adds the "current" object (pointed by ptr_currentObject)
mbedalvaro 40:3ba2b0ea9f33 119
mbedalvaro 40:3ba2b0ea9f33 120 void transform(Mat44& _RT); // this transform all the objects of the scene by _RT
mbedalvaro 40:3ba2b0ea9f33 121
mbedalvaro 40:3ba2b0ea9f33 122 // Rendering whole scene:
mbedalvaro 40:3ba2b0ea9f33 123 // void render(LaserRenderer* ptr_lsr);
mbedalvaro 40:3ba2b0ea9f33 124
mbedalvaro 40:3ba2b0ea9f33 125 void clear(); // "delete" all the objects in the scene (attn! if we store the objects in the scene as POINTERS, we must 'delete' the pointer created with 'new',
mbedalvaro 40:3ba2b0ea9f33 126 // so the memory for the object is liberated by calling its destructor, and THEN clear the vector of pointers)
mbedalvaro 40:3ba2b0ea9f33 127 void deleteObject(int _id); // selective deletion (if we know the object index ; perhaps using a STL map with names?)
mbedalvaro 40:3ba2b0ea9f33 128
mbedalvaro 40:3ba2b0ea9f33 129 int totalObjects(); // number of objects in the scene
mbedalvaro 40:3ba2b0ea9f33 130 int totalPoints(); // total number of points in the scene
mbedalvaro 40:3ba2b0ea9f33 131
mbedalvaro 40:3ba2b0ea9f33 132 int sense();
mbedalvaro 40:3ba2b0ea9f33 133
mbedalvaro 40:3ba2b0ea9f33 134 // Methods to "rearrange" the objects in the scene for rendering:
mbedalvaro 40:3ba2b0ea9f33 135 // TO DO ........................
mbedalvaro 40:3ba2b0ea9f33 136
mbedalvaro 40:3ba2b0ea9f33 137 // Pointer to the current object being built (in case we use begin/end methods for creating and adding new objects to the scene).
mbedalvaro 40:3ba2b0ea9f33 138 // In the future, we may have methods to set this pointer to some other object for adding vertices, changing color, etc. Perhaps not necessary...
mbedalvaro 40:3ba2b0ea9f33 139 BaseObject* ptr_currentObject; // made it public because accessed by the WRAPPER FUNCTION "vertex" (can be made a friend...)
mbedalvaro 40:3ba2b0ea9f33 140
mbedalvaro 40:3ba2b0ea9f33 141 //private:
mbedalvaro 40:3ba2b0ea9f33 142
mbedalvaro 40:3ba2b0ea9f33 143 // The array of objects in this scene:
mbedalvaro 40:3ba2b0ea9f33 144 vector <BaseObject*> objectArray; // better to use a vector of pointer: the advantage would be, we could have a polymorphic superclass "Objects" with virtual methods...
mbedalvaro 40:3ba2b0ea9f33 145
mbedalvaro 40:3ba2b0ea9f33 146 //bool touchedScene;
mbedalvaro 40:3ba2b0ea9f33 147 int numTouchedObjects; // only modified and accessed when calling sense() method
mbedalvaro 40:3ba2b0ea9f33 148 };
mbedalvaro 40:3ba2b0ea9f33 149
mbedalvaro 40:3ba2b0ea9f33 150 #endif
mbedalvaro 40:3ba2b0ea9f33 151
mbedalvaro 40:3ba2b0ea9f33 152
mbedalvaro 40:3ba2b0ea9f33 153