00001 #ifndef _FV_MODEL_H_
00002 #define _FV_MODEL_H_
00003
00004 #include <set>
00005 #include <vector>
00006 #include <string>
00007 #include <cassert>
00008
00009 #include "Legend.h"
00010 #include "CutPlane.h"
00011 #include "Object.h"
00012 #include "ViewManager.h"
00013 #include "GrphElement.hpp"
00014 #include "Mesh.h"
00015
00016
00017 #include "../../utils/fv_dictstr.h"
00018 #include "../../include/fv_compiler.h"
00019
00020 namespace FemViewer {
00021
00022 class BBox3D;
00023 class BaseField;
00024 class Matrix;
00025 class ColorRGB;
00026 class Object;
00027 class GraphElement;
00028 class ViewManager;
00029
00030
00031 const int MeshKey = 0;
00032 const int FieldKey = 1;
00033
00034 class BaseInfo {
00035
00036 public:
00037
00038 BaseInfo(u_int status_=0)
00039 : _status(status_) {}
00040
00041 bool operator==(const BaseInfo& rhs_) {
00042 return _status == rhs_.status();
00043 }
00044
00045
00046 u_int status() const { return _status; }
00047
00048 void set_flags(u_int flags_) { _status = flags_; }
00049
00050
00051 bool is_flag_set(u_int flag_) const { return ( _status & flag_ ) > 0; }
00052
00053 void set_flag(u_int flag_) { _status |= flag_; }
00054
00055 void unset_flag(u_int flag_) { _status &= ~flag_; }
00056
00057 void change(u_int flag_, bool f_)
00058 { _status = (f_) ? _status | flag_ : _status & ~flag_; }
00059
00060 protected:
00061 u_int _status;
00062 };
00063
00064 class MeshInfo : public BaseInfo {
00065 public:
00066 enum { Wireframe = 1, };
00067
00068 public:
00069 MeshInfo(u_int status_=0) : BaseInfo(status_) {}
00070
00071 MeshInfo(const MeshInfo& rhs_) {
00072 _status = rhs_.status();
00073 }
00074
00075 MeshInfo& operator=(const MeshInfo& rhs_) {
00076 this->_status = rhs_.status();
00077 return *this;
00078 }
00079
00080 bool is_wireframe() const { return is_flag_set(Wireframe); }
00081 void set_wireframe(bool f_) { change(Wireframe,f_); }
00082 };
00083
00084 class FieldInfo : public BaseInfo {
00085 public:
00086 enum {
00087 Flooded = 1,
00088 Contoured = 2,
00089 ColoredContoured = 4,
00090 IsoSurfaced = 8,
00091 Vectored = 16,
00092 };
00093 public:
00094 FieldInfo(u_int status_=0) : BaseInfo(status_) {}
00095
00096 FieldInfo(const FieldInfo& rhs_) {
00097 _status = rhs_.status();
00098 }
00099
00100 FieldInfo& operator=(const FieldInfo& rhs_) {
00101 this->_status = rhs_.status();
00102 return *this;
00103 }
00104
00105 bool is_flooded() const { return is_flag_set(Flooded); }
00106 void set_flooded(bool f_) { change(Flooded,f_); }
00107
00108 bool is_contoured() const { return is_flag_set(Contoured); }
00109 void set_contoured(bool f_) { change(Contoured,f_); }
00110
00111 bool is_coloredcontoured() const { return is_flag_set(ColoredContoured); }
00112 void set_coloredcontoured(bool f_) { change(ColoredContoured,f_); }
00113
00114 bool is_isosurfaced() const { return is_flag_set(IsoSurfaced); }
00115 void set_isosurfaced(bool f_) { change(IsoSurfaced,f_); }
00116
00117 bool is_vectored() const { return is_flag_set(Vectored); }
00118 void set_vectored(bool f_) { change(Vectored,f_); }
00119 };
00120
00121 struct VisualInfo : public BaseInfo {
00122 public:
00123 enum {
00124 Hidden = 1,
00125 Cutted = 2,
00126 Changed = 4,
00127 Deleted = 8,
00128 };
00129 public:
00130 VisualInfo(u_int status_=0) : BaseInfo(status_) {}
00131 VisualInfo(const VisualInfo& rhs_) {
00132 _status = rhs_.status();
00133 }
00134
00135 VisualInfo& operator=(const VisualInfo& rhs_) {
00136 this->_status = rhs_.status();
00137 return *this;
00138 }
00139
00140 bool is_hidden() const { return is_flag_set(Hidden); }
00141 void set_hidden(bool f_) { change(Hidden,f_); }
00142
00143 bool is_cutted() const { return is_flag_set(Cutted); }
00144 void set_cutted(bool f_) { change(Cutted,f_); }
00145
00146 bool is_changed() const { return is_flag_set(Changed); }
00147 void set_changed(bool f_) { change(Changed,f_); }
00148
00149 bool is_deleted() const { return is_flag_set(Deleted); }
00150 void set_deleted(bool f_) { change(Deleted,f_); }
00151 };
00152
00153 class BaseHandle {
00154 public:
00155 explicit BaseHandle(int idx_=-1, bool act_=false)
00156 : _idx(idx_), _active(act_) {}
00157
00158 inline BaseHandle& operator=(const BaseHandle& rhs_) {
00159 _idx = rhs_.idx(); return *this;
00160 }
00161
00162 void activate(const bool flg_) { _active = flg_; }
00163
00164
00165 int idx() const { return _idx; }
00166
00167 bool active() const { return _active; }
00168
00169
00170 bool is_valid() const { return (_idx != -1); }
00171
00172
00173
00174
00175
00176 void reset() { _idx=-1; _active=false; }
00177
00178
00179
00180 bool operator==(const BaseHandle& rhs_) const
00181 { return _idx == rhs_.idx(); }
00182
00183 bool operator!=(const BaseHandle& rhs_) const
00184 { return _idx != rhs_._idx; }
00185
00186 bool operator <(const BaseHandle& rhs_) const
00187 { return _idx < rhs_._idx; }
00188
00189
00190 friend std::ostream& operator<<(std::ostream& os_,const BaseHandle& rhs_);
00191
00192 private:
00193 int _idx;
00194 bool _active;
00195 };
00196
00197 inline std::ostream& operator<<(std::ostream& os_,const BaseHandle& rhs_)
00198 {
00199 return os_ << rhs_.idx();
00200 }
00201
00202 struct MeshHandle : public BaseHandle {
00203 explicit MeshHandle(int idx_=-1) : BaseHandle(idx_) {}
00204 };
00205
00206 struct FieldHandle : public BaseHandle {
00207 explicit FieldHandle(int idx_=-1) : BaseHandle(idx_) {}
00208 };
00209
00210 struct grh_data
00211 {
00212 Object* objroot;
00213 const char *name;
00214 std::pair<int,Object*> object;
00215 bool change;
00216
00217 Mesh::arBndElems vecdt;
00218
00219 explicit grh_data(Object* obj_=NULL,const char* nm_="unknown",int id_=-1,Object *ob_=NULL,bool flg_=true)
00220 : objroot(obj_), name(nm_), object(id_,ob_), change(flg_), vecdt()
00221 {}
00222
00223 grh_data(const grh_data& rhs_) {
00224 objroot = rhs_.objroot;
00225 name = rhs_.name;
00226 object = rhs_.object;
00227 change = rhs_.change;
00228 vecdt = rhs_.vecdt;
00229 }
00230
00231 grh_data& operator=(const grh_data& rhs_) {
00232 objroot = rhs_.objroot;
00233 name = rhs_.name;
00234 object = rhs_.object;
00235 change = rhs_.change;
00236 vecdt = rhs_.vecdt;
00237 return *this;
00238 }
00239
00240 ~grh_data() { clear(); }
00241
00242 void clear()
00243 {
00244
00245
00246
00247 vecdt.clear();
00248 }
00249
00250 void reset()
00251 {
00252 object.second = objroot->ResetSubObject(object.first);
00253 change = true;
00254 vecdt.clear();
00255 }
00256 };
00257
00258 template<class TStatus, class TData, size_t nTSize>
00259 class ModelItem : public BaseHandle {
00260 private:
00261 Object* root;
00262 TData* elem;
00263 VisualInfo visInfo;
00264 TStatus attributes;
00265 std::vector<grh_data> vecData;
00266 std::vector<int> idxs;
00267
00268 public:
00269
00270 explicit ModelItem( Object* rt_=NULL,
00271 TData* elm_=NULL,
00272 size_t size=nTSize,
00273 u_int vis_=0,
00274 u_int attr_=0,
00275 int idx_=-1,
00276 bool act_=false)
00277 : BaseHandle(idx_,act_),
00278 root(rt_),
00279 elem(elm_),
00280 visInfo(vis_),
00281 attributes(attr_),
00282 vecData(nTSize),
00283 idxs()
00284 {}
00285
00286 ModelItem& operator=(const ModelItem& rhs_)
00287 {
00288 BaseHandle::operator=(rhs_);
00289 root = rhs_.root;
00290 elem = rhs_.elem;
00291 visInfo = rhs_.visInfo;
00292 attributes = rhs_.attributes;
00293 vecData = rhs_.vecData;
00294 idxs = rhs_.idxs;
00295 return *this;
00296 }
00297
00298 ~ModelItem()
00299 {
00300 vecData.clear();
00301 idxs.clear();
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 void RemoveItem(const int idx_)
00313 {
00314 assert( idx_ != -1);
00315
00316 if( idx_ >= vecData.size()) return;
00317 vecData.erase(vecData.begin()+idx_);
00318 }
00319
00320 void Reset()
00321 {
00322 this->reset();
00323
00324 std::vector<grh_data>::iterator itrb = vecData.begin();
00325 const std::vector<grh_data>::const_iterator itre = vecData.end();
00326
00327 for(;itrb != itre; ++itrb)
00328 {
00329 itrb->reset();
00330 }
00331 }
00332
00333 inline const TData* GetElem() const { return elem; }
00334 inline TData* GetElem() { return elem; }
00335
00336 inline const VisualInfo& GetVisualInfo() const { return visInfo; }
00337 inline VisualInfo& GetVisualInfo() { return visInfo; }
00338
00339 inline const TStatus& GetAttributes() const { return attributes; }
00340 inline TStatus& GetAttributes() { return attributes; }
00341
00342 inline const std::vector<grh_data>& GetGraphicData() const { return vecData; }
00343 inline std::vector<grh_data>& GetGraphicData() { return vecData; }
00344
00345 inline const std::vector<int>& GetIndexes() const { return idxs; }
00346 inline std::vector<int>& GetIndexes() { return idxs; }
00347
00348 };
00349
00350
00351
00352
00353
00354 class Model {
00355 public:
00356 typedef struct _struct_sol {
00357 static const int max_sols = 100;
00358 bool is_init;
00359 int nr_sols;
00360 int nr_equs;
00361 int nr_curr_sol;
00362 std::string formula;
00363 _struct_sol()
00364 : is_init(false),
00365 nr_sols(1),
00366 nr_equs(1),
00367 nr_curr_sol(0),
00368 formula("v0")
00369 {}
00370
00371 _struct_sol(const _struct_sol& rhs_)
00372 : is_init(rhs_.is_init),
00373 nr_sols(rhs_.nr_sols),
00374 nr_equs(rhs_.nr_equs),
00375 nr_curr_sol(rhs_.nr_curr_sol),
00376 formula(rhs_.formula)
00377 {
00378 assert(nr_sols >= 1 && nr_sols <= max_sols);
00379 assert(nr_equs >= 1 && nr_equs <= 10);
00380 assert(nr_curr_sol >= 0 && nr_curr_sol <= max_sols-1);
00381 }
00382
00383 } Solution, *SolutionPtr;
00384
00385 typedef struct _contener {
00386 Legend* legend_ptr;
00387 SolutionPtr sol_ptr;
00388 CutPlane* plane_ptr;
00389 Mesh::arBndElems* data_ptr;
00390 Object* object_ptr;
00391 } contener, *contener_ptr;
00392
00393 enum GeometryType {
00394 NoneType = 0,
00395 MeshTypes = 1,
00396 FieldTypes = 2,
00397 AllTypes = 3,
00398 };
00399
00400 private:
00401 static Model* _self;
00402 static ViewManager* _vmgr;
00403 static Object* _root;
00404 static Legend _legend;
00405 static CutPlane _cutplane;
00406 static int _mshlast;
00407 static int _fldlast;
00408 static contener _package;
00409 static Solution _solution;
00410
00411 Model();
00412 public:
00413 static void Init(ViewManager& vmgr_);
00414 static void Init() { if( ! _self) _self = new Model(); }
00415 static void Destroy();
00416 static void Reset() { Destroy(); Init(); }
00417 static void Refresh(GeometryType Type);
00418 static Legend& GetLegend() { return _legend; }
00419 static CutPlane& GetPalne() { return _cutplane; }
00420 static Solution& GetSolution() { return _solution; }
00421 static Model* GetInstance() { if(!_self) Init(); return _self; }
00422 static void SetViewManager(ViewManager* pVMgr) { _vmgr = pVMgr; Init(*_vmgr); }
00423 enum RenderAttributes {
00424 wireframe = 0x01,
00425 flooded = 0x02,
00426 contoured = 0x04,
00427 cutted = 0x08,
00428 };
00429
00430 u_int RenderOptions;
00431
00432 bool isMeshes;
00433 bool isFields;
00434
00435 bool isWireframe;
00436 bool isFlooded;
00437 bool isContoured;
00438 bool isColorContoured;
00439 bool isIsoSurfaced;
00440 bool isCutted;
00441
00442
00443
00444
00445
00446 bool isPlaneChange;
00447 bool isContourChange;
00448 bool isSolutionChange;
00449
00450
00451
00452 ~Model();
00453
00454 int AddMesh(Mesh* mesh_);
00455 int AddField(BaseField* pFiled);
00456
00457 void ChangeCuttingPlane();
00458 void ChangeSolution();
00459 void RemoveMesh(const u_int idx);
00460 void RemoveField(const u_int idx);
00461
00462 bool ActivateMeshObj(const u_int idx_, const bool flg_);
00463 bool ActivateFieldObj(const u_int idx_, const bool flg_);
00464
00465
00466
00467 void DeleteDisplayLists();
00468
00469 void DumpCharacteristics(std::ostream& os,
00470 const std::string& pIndentation, Matrix pTransformation);
00471
00472
00473 const BBox3D& GetModelBBox3D() const {
00474 FV_ASSERT(_vmgr != NULL);
00475 return _vmgr->GetGraphicData().GetGlobalBBox3D();
00476 }
00477
00478 void Update();
00479 void Redraw(bool redrawAll = true);
00480
00481 const BBox3D& GetGlobalBBox3D() { return _root->GetBBox3D(); }
00482
00483
00484 void reset();
00485 void clear();
00486
00487
00488 int GetMeshNumber() const { return static_cast<int>(vMeshes.size()); }
00489 int GetFieldNumber() const;
00490
00491 void SetGoemetry(Object* pRootObject);
00492
00493
00494 BaseField* GetCurrentField();
00495 private:
00496
00497
00498
00499 typedef ModelItem<MeshInfo,Mesh,0> MeshObj;
00500 typedef ModelItem<FieldInfo,BaseField,0> FieldObj;
00501 typedef std::vector<MeshObj> vecMeshes;
00502 typedef std::vector<FieldObj> vecFields;
00503 typedef vecMeshes::iterator vecMeshIter;
00504 typedef vecFields::iterator vecFieldIter;
00505 typedef vecMeshes::const_iterator vecMeshConstIter;
00506 typedef vecFields::const_iterator vecFieldConstIter;
00507 template< class TMObj >
00508 static inline bool InitModelObj(TMObj& ref_);
00509
00510 void RenderMeshes(const std::vector<MeshObj*>& vMeshes2render_);
00511 void RenderFields(const std::vector<FieldObj*>& vFields2render_);
00512
00513 bool CheckForChanges() const;
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 vecMeshes vMeshes;
00532 vecFields vFields;
00533
00534 };
00535
00536 template<class TMObj>
00537 inline bool Model::InitModelObj(TMObj& ref_)
00538 {
00539 return InitModelObj< typename TMObj::value_type >(ref_);
00540 }
00541
00542 template<>
00543 inline bool Model::InitModelObj<Model::MeshObj>(MeshObj& ref_)
00544 {
00545 int id;
00546 Object* obj = _root->AddNewObject("Mesh_Wireframe",&id);
00547 std::cout<<"first id= " << id << "obj= " << (void*)obj <<"\n";
00548 ref_.GetGraphicData().push_back( grh_data(_root,"Mesh_Wireframe",id,obj,true));
00549
00550 obj = _root->AddNewObject("Mesh_Cutted_Wireframe",&id);
00551 std::cout<<"second id= " << id << "obj= " << (void*)obj <<"\n";
00552 ref_.GetGraphicData().push_back( grh_data(_root,"Mesh_Cutted_Wireframe",id,obj,true));
00553 std::cout <<"After MeghObj Tempate\n";
00554 return true;
00555 }
00556
00557 template<>
00558 inline bool Model::InitModelObj<Model::FieldObj>(FieldObj& ref_)
00559 {
00560 int id;
00561 Object* obj = _root->AddNewObject("Filed_Flooded",&id);
00562 ref_.GetGraphicData().push_back( grh_data(_root,"Field_Flooded",id,obj,true));
00563
00564 obj = _root->AddNewObject("Filed_Contoured",&id);
00565 ref_.GetGraphicData().push_back( grh_data(_root,"Field_Contoured",id,obj,true));
00566
00567 obj = _root->AddNewObject("Filed_Cutted_Flooded",&id);
00568 ref_.GetGraphicData().push_back( grh_data(_root,"Field_Cutted_Flooded",id,obj,true));
00569
00570 obj = _root->AddNewObject("Filed_Cutted_Contoured",&id);
00571 ref_.GetGraphicData().push_back( grh_data(_root,"Field_Cutted_Contoure",id,obj,true));
00572
00573 return true;
00574
00575 }
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652 }
00653
00654 #endif
00655