common.cpp 2.59 KB
Newer Older
1 2
#include "common.h"

3 4 5 6 7 8 9 10 11 12
#ifdef _WIN32
#include <windows.h>
#include <winbase.h>
#else
#include <sys/times.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
#endif

13 14 15
double norm(const vec& a)
{
	double norm = 0.0 ;
Laurent Belcour's avatar
Laurent Belcour committed
16
	for(int i=0; i<a.size(); ++i)
17 18 19 20 21 22 23 24 25 26
	{
		norm += a[i]*a[i];
	}
	return sqrt(norm);
}

vec normalize(const vec& a)
{
	vec b(a.size());
	double norm = 0.0 ;
Laurent Belcour's avatar
Laurent Belcour committed
27
	for(int i=0; i<a.size(); ++i)
28 29 30 31 32
	{
		norm += a[i]*a[i];
	}
	norm = sqrt(norm);

Laurent Belcour's avatar
Laurent Belcour committed
33
	for(int i=0; i<a.size(); ++i)
34 35 36 37 38 39 40 41 42 43 44 45
	{
		b[i] = a[i]/norm;
	}
	return b;
}

double dot(const vec& a, const vec& b)
{
#ifdef DEBUG
	assert(a.size() == b.size());
#endif
	double res = 0.0;
Laurent Belcour's avatar
Laurent Belcour committed
46
	for(int i=0; i<a.size(); ++i)
47 48 49 50 51 52 53 54 55
	{
		res += a[i]*b[i];
	}

	return res;
}

vec product(const vec& a, const vec& b)
{
56
    if(a.size() == 1 && b.size() >= 1)
57 58 59 60 61 62 63 64
    {
        vec res(b.size());
        for(int i=0; i<b.size(); ++i)
        {
            res[i] = a[0]*b[i];
        }
        return res;
    }
65
    else if(b.size() == 1 && a.size() >= 1)
66 67 68 69 70 71 72 73 74 75
    {
        vec res(a.size());
        for(int i=0; i<a.size(); ++i)
        {
            res[i] = a[i]*b[0];
        }
        return res;
    }
    else
    {
76
#ifndef DEBUG
77
        assert(a.size() == b.size());
78
#endif
79 80 81
        vec res(b.size());
        for(int i=0; i<b.size(); ++i)
        {
82
            res[i] = a[i]*b[i];
83 84 85
        }
        return res;
    }
86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

std::ostream& operator<<(std::ostream& out, const vec& v)
{
    out << "[";
    for(int i=0; i<v.size(); ++i)
    {
        out << v(i);
        if(i != v.size()-1) { out << ", "; }
    }
    out << "]";
    return out;
}

Laurent Belcour's avatar
Laurent Belcour committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122







/* ---- Timer implementation ---- */

timer::timer()
{
    reset();
}

void timer::start()
{
    _elapsed += _stop-_start;
    _start = current_time();
}

void timer::stop()
{
    _stop = current_time();
Laurent Belcour's avatar
Merging  
Laurent Belcour committed
123 124
    _elapsed += _stop - _start;
    _start = _stop;
Laurent Belcour's avatar
Laurent Belcour committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}

void timer::reset()
{
    _elapsed = 0;
    _start   = 0;
    _stop    = 0;
}

int timer::elapsed() const
{
    return _elapsed;
}

void timer::print(std::ostream& out) const
{
    unsigned int tsec = elapsed() ;
    unsigned int sec  = tsec % 60 ;
    unsigned int min  = (tsec / 60) % 60 ;
    unsigned int hour = (tsec / 360) ;
145
    out << hour << "h " << min << "m " << sec << "s" ;
Laurent Belcour's avatar
Laurent Belcour committed
146 147
}

148
std::ostream& operator<<(std::ostream& out, const timer& t)
Laurent Belcour's avatar
Laurent Belcour committed
149 150 151 152 153 154 155 156
{
    t.print(out);
    return out;
}

unsigned int timer::current_time() const
{
#ifdef WIN32
157 158 159
	SYSTEMTIME res;
	GetSystemTime(&res);
	return (unsigned int)(res.wSecond + res.wMinute*60 + res.wHour*360);
160 161
#elif __APPLE__
	return 0;
Laurent Belcour's avatar
Laurent Belcour committed
162 163 164 165 166 167
#else
    struct timespec res;
    clock_gettime(CLOCK_MONOTONIC, &res);
    return static_cast<unsigned int>(res.tv_sec);
#endif
}