fish/simplecoords/SimpleCoords.cpp

00001 #include <ocean/GLvish/VRenderObject.hpp>
00002 
00003 #include <fish/lakeview/bone/FishSlice.hpp>
00004 #include <fish/lakeview/bone/FishField.hpp>
00005 
00006 #include <field/DirectProductArray.hpp>
00007 #include <ocean/GLvish/BoundingBox.hpp>
00008 
00009 using namespace Wizt;
00010 using namespace Fiber;
00011 
00012 
00031 class SimpleCoords : public virtual VRenderObject,  virtual public Fish<Slice>, virtual public Fish<Grid>
00032 {
00033 public:
00034         // Shortcut some Datatypes used to acess coordinate and vector data.
00035         // Note that coordinates can come procedural or numerical and need different 
00036         // data types to be handled.
00037         typedef MemArray<1, Eagle::point3>             CoordsArray_t; 
00038 
00039         TypedSlot<double> PointSize;
00040 
00041         // Inner class to save local state Information. Besides the control parameters also 
00042         // pointers to the vector and coordinate data are saved here.
00043         struct  MyState : State
00044         {
00045                 bool recalc_dl;
00046                 RefPtr< CoordsArray_t > Crds; 
00047         };
00048 
00049         override RefPtr<State> newState() const
00050         {
00051                 return new MyState();
00052         }
00053 
00054         SimpleCoords(const string&name, int p, const RefPtr<VCreationPreferences>&VP)
00055         : VRenderObject(name, p, VP)
00056         , Fish<VObject>(this)
00057         , Fish<Grid>("coords")
00058         , PointSize(this, "pointsize", 2.0)
00059         {
00060                 PointSize.setProperty("max", 10.0); 
00061 
00062         }
00063 
00064         override bool update( VRequest&R, double precision );
00065         override void render( VRenderContext&R ) const;
00066 };
00067 
00068 
00069 
00074 bool SimpleCoords::update( VRequest&R, double precision )
00075 {
00076 //      printf("SimpleCoords::update(): updating\n"); 
00077 
00078 GridSelector GS; 
00079         MyGrid << R >> GS;
00080 
00081 // get the grid from the fiber bundle
00082 RefPtr<Grid> G = findMostRecentGrid( GS, R );
00083         if (!G) { puts("SimpleCoords::update(), ERROR: No Grid"); return true; } 
00084 
00085 // get the coordinates of the grid
00086 RefPtr<Field> Coords  = G->CartesianPositions(); 
00087         if (!Coords)  { puts("SimpleCoords::update(): ERROR: No coord field!"); return true; } 
00088 
00089 
00090 // get the actual state object
00091 RefPtr<MyState> S = myState(R); 
00092         assert(S); 
00093 
00094         S->Crds = Coords->getData(); 
00095         S->recalc_dl = true; // force recalcing the displaylinst on each update call
00096 
00097         return true;
00098 }
00099 
00100 
00101 void SimpleCoords::render( VRenderContext& Context ) const
00102 {
00103 //      printf("SimpleCoords::render(): starting to render\n");
00104 
00105 double point_size = 0.0; 
00106         PointSize  << Context >> point_size; 
00107 
00108         if(point_size <= 0.0) point_size = 1.0;
00109 
00110         glEnable(GL_DEPTH_TEST); 
00111         glDisable(GL_LIGHTING); 
00112 
00113         glPointSize(point_size);
00114 
00115         // get current state
00116         RefPtr<MyState>  state = myState(Context); 
00117 
00118         // return if no valid coord data is available 
00119         if(!state)
00120         {
00121                 printf("SimpleCoords::render(): Invalid State!\n");
00122                 return; 
00123         }
00124         if (!state->Crds)
00125         {
00126                 printf("SimpleCoords::render(): Invalid Coordinates!\n");
00127                 return; 
00128         }
00129         
00130         RefPtr<ValueSet> RenderParameterSpace = new ValueSet(); 
00131 
00132 
00133         RefPtr<DisplayList> DL; 
00134         try{
00135         DL = Context(*state)(this)( RenderParameterSpace ); 
00136         } 
00137         catch(...){}
00138         if ( DL && !state->recalc_dl )   //call display list if availlable
00139         {
00140                 if(DL->call())
00141                 {
00142                         //printf("SimpleCoords::render(): Called List\n");
00143                         return;
00144                 } 
00145         } 
00146         else                                                   //prepare display list
00147         {
00148                 DL = Context[*state][this][ RenderParameterSpace ]; 
00149                 if (!DL) throw "SimpleCoords::render() No Display List!?"; 
00150 
00151                 state->recalc_dl = false;
00152 
00153                 // Get the coordinates saved in actual state. 
00154                 // They can come as procedural or numerical coordinates. 
00155                 // Be prepared for both types, because they need different handling.
00156 
00157                 if (state->Crds)  // found numerical coordinates
00158                 {
00159                 point Start = state->Crds->first(); 
00160                 point End   = state->Crds->last(); 
00161                         setBoundingBall(Context, new BoundingBox(Start, End) ); 
00162                         printf("SimpleCoords::render(): got numerical coordinates\n" ); 
00163                 } 
00164 
00165         // create a Multiindex for traversing coordinate data. 
00166         MultiIndex<1> M; 
00167         MultiArray<1, point>&Cr = *state->Crds;
00168 
00169         glCompile( *DL ) 
00170                 {
00171                         printf("SimpleCoords::render(): compiling new list\n");
00172                         glBegin(GL_POINTS); 
00173                         glColor3f(1.0,1.0,1.0); 
00174 
00175                         do
00176                         {
00177                         point Vertex; 
00178 
00179                                 Vertex = Cr[M];
00180 
00181                                 glVertex(Vertex); 
00182                                 
00183                         }
00184                         while( M.inc( Cr.Size() ) ); // proceed to next Multiindex 
00185                         
00186                         glEnd();
00187  
00188                 } 
00189         }
00190 }
00191 
00192 
00193 static Ref<VCreator<SimpleCoords, AcceptList<Fiber::Grid> > > myCreator("Tutorial/SimpleCoords");
00194 
00195 VISH_DEFAULT_INIT

Generated on Thu Apr 2 18:58:50 2009 for VISHTutorial by  doxygen 1.4.7