AnimatLab  2
Test
OsgMeshMgr.cpp
1 
7 #include "StdAfx.h"
8 #include "OsgMeshMgr.h"
9 
10 namespace OsgAnimatSim
11 {
12 
20 {
21 }
22 
30 {
31  try
32  {
33  m_aryMeshes.RemoveAll();
34  }
35  catch(...)
36  {Std_TraceMsg(0, "Caught Error in desctructor of OsgMeshMgr\r\n", "", -1, false, true);}
37 }
38 
39 std::string OsgMeshMgr::FileCreateTime(std::string strFilename)
40 {
41  std::stringstream ss;
42 
43  //NEED TO TEST
44 #ifdef WIN32
45  WIN32_FILE_ATTRIBUTE_DATA wfad;
46  SYSTEMTIME st;
47 
48  GetFileAttributesEx(strFilename.c_str(), GetFileExInfoStandard, &wfad);
49  FileTimeToSystemTime(&wfad.ftLastWriteTime, &st);
50 
51  ss << st.wMonth << '/' << st.wDay << '/' << st.wYear << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond;
52 #else
53  struct stat attrib;
54  stat(strFilename.c_str(), &attrib);
55 
56  struct tm *ts;
57  ts = localtime(&attrib.st_mtime);
58 
59  ss << ts->tm_mon << '/' << ts->tm_wday << '/' << ts->tm_year << " " << ts->tm_hour << ":" << ts->tm_min << ":" << ts->tm_sec;
60 #endif
61 
62  return ss.str();
63 }
64 
65 osg::Node *OsgMeshMgr::LoadMesh(std::string strFilename)
66 {
67  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair;
68 
69  //If not found then load it.
70  if(!FindMesh(strFilename, MeshPair, false))
71  return AddMesh(strFilename);
72  else
73  {
74  std::string strCreateDate = FileCreateTime(strFilename);
75 
76  //If the create dates match then use this one.
77  //If they do not then lets reload it.
78  if(strCreateDate == MeshPair.first)
79  return MeshPair.second.get();
80  else
81  {
82  ReleaseMesh(strFilename);
83  return AddMesh(strFilename);
84  }
85  }
86 }
87 
88 osg::Node *OsgMeshMgr::AddMesh(std::string strFilename)
89 {
90  osg::ref_ptr<osg::Node> lpTempMesh = osgDB::readNodeFile(strFilename.c_str());
91 
92  if(lpTempMesh)
93  {
94  osgUtil::SmoothingVisitor sv;
95  lpTempMesh->accept(sv);
96 
97  std::string strFileCreateTime = FileCreateTime(strFilename);
98  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair2(strFileCreateTime, lpTempMesh);
99 
100  m_aryMeshes.Add(Std_CheckString(strFilename), MeshPair2);
101  return lpTempMesh.get();
102  }
103  else
104  return NULL;
105 
106 }
107 
108 void OsgMeshMgr::ReleaseMesh(std::string strFilename)
109 {
110  int iIndex=0;
111  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair;
112  if(!FindMesh(strFilename, MeshPair, false)) return;
113 
114  MeshPair.second.release();
115 
116  m_aryMeshes.Remove(Std_CheckString(strFilename));
117 }
118 
119 bool OsgMeshMgr::ContainesMesh(std::string strFilename)
120 {
121  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair;
122 
123  if(FindMesh(strFilename, MeshPair, false))
124  return true;
125  else
126  return false;
127 }
128 
140 bool OsgMeshMgr::FindMesh(std::string strFilename, std::pair<std::string, osg::ref_ptr<osg::Node>> &MeshPair, bool bThrowError)
141 {
142  CStdMap<std::string, std::pair<std::string, osg::ref_ptr<osg::Node>> >::iterator oPos;
143  oPos = m_aryMeshes.find(Std_CheckString(strFilename));
144 
145  if(oPos != m_aryMeshes.end())
146  {
147  MeshPair = oPos->second;
148  return true;
149  }
150  else if(bThrowError)
151  THROW_TEXT_ERROR(Osg_Err_lMeshIDNotFound, Osg_Err_strMeshNotFound, " Mesh Filename: " + strFilename);
152 
153  return false;
154 }
155 
156 
157 } //OsgAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
bool FindMesh(std::string strFilename, std::pair< std::string, osg::ref_ptr< osg::Node >> &MeshPair, bool bThrowError=true)
Searches for a mesh that matches the specified filename.
Definition: OsgMeshMgr.cpp:140
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
Declares the vortex MeshMgr class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
OsgMeshMgr()
Default constructor.
Definition: OsgMeshMgr.cpp:19
virtual ~OsgMeshMgr()
Destructor.
Definition: OsgMeshMgr.cpp:29