RigsofRods
Soft-body Physics Simulation
Utils.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2016 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
28 #pragma once
29 
30 #include "Application.h"
31 
32 #include "utf8/checked.h"
33 #include "utf8/unchecked.h"
34 
35 #include <MyGUI.h>
36 #include <OgreUTFString.h>
37 
38 namespace RoR {
39 
40 Ogre::String sha1sum(const char *key, int len);
41 
42 Ogre::String HashData(const char *key, int len);
43 
44 Ogre::UTFString tryConvertUTF(const char* buffer);
45 
46 Ogre::UTFString formatBytes(double bytes);
47 
48 std::time_t getTimeStamp();
49 
50 Ogre::String getVersionString(bool multiline = true);
51 
52 inline void replaceString(std::string& str, std::string searchString, std::string replaceString)
53 {
54  std::string::size_type pos = 0;
55  while ((pos = str.find(searchString, pos)) != std::string::npos)
56  {
57  str.replace(pos, searchString.size(), replaceString);
58  pos++;
59  }
60 }
61 
62 Ogre::Real Round(Ogre::Real value, unsigned short ndigits = 0);
63 
64 std::string SanitizeUtf8String(std::string const& str_in);
65 std::string SanitizeUtf8CString(const char* start, const char* end = nullptr);
66 
67 inline std::string& TrimStr(std::string& s) { Ogre::StringUtil::trim(s); return s; }
68 std::string Sha1Hash(std::string const & data);
69 
70 std::string JoinStrVec(Ogre::StringVector tokens, const std::string& delim);
71 
72 // for std::vector
73 template <class T, class A, class Predicate>
74 inline void EraseIf(std::vector<T, A>& c, Predicate pred)
75 {
76  c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
77 }
78 
82 {
83 public:
84  World2ScreenConverter(Ogre::Matrix4 view_mtx, Ogre::Matrix4 proj_mtx, Ogre::Vector2 screen_size):
85  m_view_matrix(view_mtx), m_projection_matrix(proj_mtx), m_screen_size(screen_size)
86  {}
87 
89  Ogre::Vector3 Convert(Ogre::Vector3 world_pos)
90  {
91  Ogre::Vector3 view_space_pos = m_view_matrix * world_pos;
92  Ogre::Vector3 clip_space_pos = m_projection_matrix * view_space_pos; // Clip space pos is [-1.f, 1.f]
93  float screen_x = ((clip_space_pos.x / 2.f) + 0.5f) * m_screen_size.x;
94  float screen_y = (1.f - ((clip_space_pos.y / 2.f) + 0.5f)) * m_screen_size.y;
95  return Ogre::Vector3(screen_x, screen_y, view_space_pos.z);
96  }
97 
98 private:
99  Ogre::Matrix4 m_view_matrix;
100  Ogre::Matrix4 m_projection_matrix;
101  Ogre::Vector2 m_screen_size;
102 };
103 
104 bool IsDistanceWithin(Ogre::Vector3 const& a, Ogre::Vector3 const& b, float max);
105 
106 std::string PrintMeshInfo(std::string const& title, Ogre::MeshPtr mesh);
107 
108 void CvarAddFileToList(CVar* cvar, const std::string& filename);
109 void CvarRemoveFileFromList(CVar* cvar, const std::string& filename);
110 
111 } // namespace RoR
RoR::getVersionString
Ogre::String getVersionString(bool multiline=true)
Definition: Utils.cpp:79
RoR::World2ScreenConverter::m_projection_matrix
Ogre::Matrix4 m_projection_matrix
Definition: Utils.h:100
RoR::PrintMeshInfo
std::string PrintMeshInfo(std::string const &title, Ogre::MeshPtr mesh)
RoR::SanitizeUtf8String
std::string SanitizeUtf8String(std::string const &str_in)
Definition: Utils.cpp:117
RoR::Round
Ogre::Real Round(Ogre::Real value, unsigned short ndigits=0)
Definition: Utils.cpp:98
RoR::HashData
Ogre::String HashData(const char *key, int len)
Definition: Utils.cpp:51
RoR::TrimStr
std::string & TrimStr(std::string &s)
Definition: Utils.h:67
RoR::World2ScreenConverter::Convert
Ogre::Vector3 Convert(Ogre::Vector3 world_pos)
Definition: Utils.h:89
RoR::replaceString
void replaceString(std::string &str, std::string searchString, std::string replaceString)
Definition: Utils.h:52
RoR::getTimeStamp
std::time_t getTimeStamp()
Definition: Utils.cpp:74
RoR::formatBytes
Ogre::UTFString formatBytes(double bytes)
Definition: Utils.cpp:64
RoR::World2ScreenConverter
< Keeps data close for faster access.
Definition: Utils.h:81
RoR::World2ScreenConverter::m_screen_size
Ogre::Vector2 m_screen_size
Definition: Utils.h:101
Application.h
Central state/object manager and communications hub.
RoR::CvarAddFileToList
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:206
RoR::CVar
Quake-style console variable, defined in RoR.cfg or crated via Console UI and scripts.
Definition: CVar.h:52
RoR::SanitizeUtf8CString
std::string SanitizeUtf8CString(const char *start, const char *end=nullptr)
Definition: Utils.cpp:125
RoR::JoinStrVec
std::string JoinStrVec(Ogre::StringVector tokens, const std::string &delim)
Definition: Utils.cpp:146
RoR::World2ScreenConverter::m_view_matrix
Ogre::Matrix4 m_view_matrix
Definition: Utils.h:99
RoR::Sha1Hash
std::string Sha1Hash(std::string const &data)
Definition: Utils.cpp:138
RoR::CvarRemoveFileFromList
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:216
RoR::World2ScreenConverter::World2ScreenConverter
World2ScreenConverter(Ogre::Matrix4 view_mtx, Ogre::Matrix4 proj_mtx, Ogre::Vector2 screen_size)
Definition: Utils.h:84
RoR::EraseIf
void EraseIf(std::vector< T, A > &c, Predicate pred)
Definition: Utils.h:74
RoR::tryConvertUTF
Ogre::UTFString tryConvertUTF(const char *buffer)
Definition: Utils.cpp:58
RoR
Definition: AppContext.h:36
RoR::sha1sum
Ogre::String sha1sum(const char *key, int len)
Definition: Utils.cpp:43
RoR::IsDistanceWithin
bool IsDistanceWithin(Ogre::Vector3 const &a, Ogre::Vector3 const &b, float max)
Definition: Utils.cpp:158