lTime.h 3.43 KB
Newer Older
wangdawei's avatar
wangdawei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

#ifndef L_TIME
#define L_TIME

//this file is an approximation of the ros time class, though with no templating, reduced error checking and no fancy dependencies.

#include <math.h>
#include <stdint.h>


class lTime
{
public:
    int error;
    void normalizeSecNSecSigned(int64_t& sec, int64_t& nsec)
      {
        int64_t nsec_part = nsec % 1000000000L;
        int64_t sec_part = sec + nsec / 1000000000L;
        if (nsec_part < 0)
          {
            nsec_part += 1000000000L;
            --sec_part;
          }

        sec = sec_part;
        nsec = nsec_part;
      }

      void normalizeSecNSecSigned(int32_t& sec, int32_t& nsec)
      {
        int64_t sec64 = sec;
        int64_t nsec64 = nsec;

        normalizeSecNSecSigned(sec64, nsec64);

        sec = (int32_t)sec64;
        nsec = (int32_t)nsec64;
      }


  int32_t sec, nsec;
  lTime() : sec(0), nsec(0) {error =0; }
  //LightDuration(int32_t _sec, int32_t _nsec);
  explicit lTime(double t){fromSec(t); error = 0;};
  ~lTime() {}

  double toSec() const { return (double)sec + 1e-9*(double)nsec; };
  int64_t toNSec() const {return (int64_t)sec*1000000000ll + (int64_t)nsec;  };

    lTime(int32_t _sec, int32_t _nsec)
    : sec(_sec), nsec(_nsec)
    {
      normalizeSecNSecSigned(sec, nsec);
    }

    lTime& fromSec(double d)
    {
      int64_t sec64 = (int64_t)floor(d);
      if (sec64 < INT32_MAX || sec64 > INT32_MAX)
        error++;
      sec = (int32_t)sec64;
      nsec = (int32_t)(nearbyint((d - (double)sec)*1000000000));
      return *(this);
    }

    lTime& fromNSec(int64_t t)
    {
      int64_t sec64 = t / 1000000000;
      if (sec64 < INT32_MIN || sec64 > INT32_MAX)
        error++;
      sec = (int32_t)sec64;
      nsec = (int32_t)(t % 1000000000);
      return *(this);
    }


    lTime operator+(const lTime &rhs) const
    {
      lTime t;
      return t.fromNSec(toNSec() + rhs.toNSec());
    }


    lTime operator*(double scale) const
    {
      return lTime(lTime() * scale);
    }

    lTime operator-(const lTime &rhs) const
    {
      lTime t;
      return t.fromNSec(toNSec() - rhs.toNSec());
    }


    lTime operator-() const
    {
      lTime t;
      return t.fromNSec(-toNSec());
    }

    lTime& operator+=(const lTime &rhs)
    {
      *this = *this + rhs;
      return *(this);
    }

    lTime& operator-=(const lTime &rhs)
    {
      *this += (-rhs);
      return *(this);
    }

    lTime& operator*=(double scale)
    {
      fromSec(toSec() * scale);
      return *(this);
    }

    bool operator<(const lTime &rhs) const
    {
      if (sec < rhs.sec)
        return true;
      else if (sec == rhs.sec && nsec < rhs.nsec)
        return true;
      return false;
    }

    bool operator>(const lTime &rhs) const
    {
      if (sec > rhs.sec)
        return true;
      else if (sec == rhs.sec && nsec > rhs.nsec)
        return true;
      return false;
    }


    bool operator<=(const lTime &rhs) const
    {
      if (sec < rhs.sec)
        return true;
      else if (sec == rhs.sec && nsec <= rhs.nsec)
        return true;
      return false;
    }

    bool operator>=(const lTime &rhs) const
    {
      if (sec > rhs.sec)
        return true;
      else if (sec == rhs.sec && nsec >= rhs.nsec)
        return true;
      return false;
    }

    bool operator==(const lTime &rhs) const
    {
      return sec == rhs.sec && nsec == rhs.nsec;
    }


    bool isZero() const
    {
      return sec == 0 && nsec == 0;
    }
};


#endif // LIGHT_DURATION