utils.h

Go to the documentation of this file.
00001 /*
00002  * utils.h
00003  *
00004  *  Created on: Nov 21, 2011
00005  *      Author: sushil
00006  */
00007 
00008 #ifndef FE_UTILS_H_
00009 #define FE_UTILS_H_
00010 
00011 #include <stdlib.h>
00012 #include <OgreMatrix3.h>
00013 #include <netinet/in.h>
00014 #include <arpa/inet.h>
00015 
00016 const float pi = 3.14159;
00017 const float twopi = 2.0 * pi;
00018 
00019 inline float clamp(float value, float lower, float upper){
00020         if (value > upper)
00021                 return upper;
00022         if(value < lower)
00023                 return lower;
00024         return value;
00025 }
00026 
00027 inline float makeAnglePosNeg(float angle)
00028 {
00029     //printf("makeAnglePosNeg:%f ", angle);
00030     while (angle > pi)
00031         angle -= twopi;
00032     while (angle < -pi)
00033         angle += twopi;
00034     //printf("->%f ", angle);
00035     return angle;
00036 }
00037 
00038 inline float differenceBetweenAngles(float angle1, float angle2){
00039     return makeAnglePosNeg(angle1 - angle2);
00040 }
00041 
00042 inline float feet(float x){
00043         return 0.3048 * x;
00044 }
00045 inline float meters(float x){
00046         return x;
00047 }
00048 
00049 inline float knots(float x) {
00050         return 0.514444444 * x;
00051 }
00052 inline float feetPerSecondSquared(float x) {
00053         return 0.3048 * x;
00054 }
00055 
00056 inline float degreesPerSecond(float x) {
00057         return 0.0174532925 * x;
00058 }
00059 
00060 inline float tons(float x){
00061         return x * 907.18474;
00062 }
00063 inline float pounds(float x){
00064         return x * 0.45359237;
00065 }
00066 
00067 inline int randInt(int low, int high) {
00068         return low + random() % (high - low);
00069 }
00070 
00071 #include <boost/date_time/posix_time/posix_time.hpp>
00072 using namespace boost::posix_time;
00073 
00074 inline ptime getCurrentTime(){
00075         return boost::posix_time::microsec_clock::local_time();
00076 }
00077 
00078 inline Ogre::Radian degrees(float d){
00079     return Ogre::Radian(d * pi / 180.0);
00080 }
00081 
00082 inline float toDegrees(float r){
00083     return r * 180/pi;
00084 }
00085 
00086 inline Ogre::Quaternion pitchYawRoll(float p, float y, float r){
00087     Ogre::Matrix3 m;
00088     m.FromEulerAnglesXYZ(degrees(p), degrees(y), degrees(r));
00089     Ogre::Quaternion q;
00090     q.FromRotationMatrix(m);
00091     return q;
00092 }
00093 
00094 inline std::string trim(std::string s){
00095         s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
00096         return s;
00097 }
00098 
00099 inline std::string int_to_string(const int& port) {
00100     std::stringstream ss;
00101     ss << port;
00102     return ss.str();
00103 }
00104 
00105 inline std::string ip_int_to_string(const long& ipi){
00106         struct in_addr addr;
00107         addr.s_addr = ipi;
00108         char *ips = inet_ntoa(addr);
00109         return std::string(ips);
00110 }
00111 
00112 inline unsigned int ip_string_to_int(const std::string& ips){
00113         unsigned int ipi = inet_addr(ips.c_str());
00114         return ipi;
00115 }
00116 #endif /* FE_UTILS_H_ */

Generated on Fri Dec 13 14:54:16 2013 for FastECSLent by  doxygen 1.5.4