Mise à jour terminée. Pour connaître les apports de la version 13.8.4 par rapport à notre ancienne version vous pouvez lire les "Release Notes" suivantes :
https://about.gitlab.com/releases/2021/02/11/security-release-gitlab-13-8-4-released/
https://about.gitlab.com/releases/2021/02/05/gitlab-13-8-3-released/

common.h 7.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* ALTA --- Analysis of Bidirectional Reflectance Distribution Functions

   Copyright (C) 2014 CNRS
   Copyright (C) 2013, 2014 Inria

   This file is part of ALTA.

   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0.  If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/.  */

Laurent Belcour's avatar
Laurent Belcour committed
12 13 14 15
#pragma once

#include <vector>
#include <iostream>
16
#include <cassert>
17
#include <cmath>
18
#include <cstring>
19
#include <algorithm>
Laurent Belcour's avatar
Laurent Belcour committed
20

21
#ifdef OLD
22 23 24
/*! \brief A core implementation of a vector of double.
 *  \ingroup core
 *  \internal
25
 *
26 27 28 29
 *  \details
 *  This type is used for any transmission of vector data with unfixed
 *  dimension. It allows to have a generic fitter working for
 *  n-Dimensional data.
30
 */
Laurent Belcour's avatar
Laurent Belcour committed
31 32 33
class vec : public std::vector<double>
{
	public:
34
		// Constructor & Destructors
Laurent Belcour's avatar
Laurent Belcour committed
35
		//
36 37 38 39
		vec() : std::vector<double>()
		{
		}
		vec(int dim) : std::vector<double>(dim)
Laurent Belcour's avatar
Laurent Belcour committed
40
		{
41 42
			assign(dim, 0.0) ;
		}
43 44
        
		//! \brief get a subpart of the vector
45 46 47 48 49 50
        vec subvector(int start, int n) const
        {
            vec res(n);
            memcpy(&res[0], &this->at(start), n*sizeof(double));
            return res;
        }
51 52 53 54 55 56 57 58 59 60 61 62

		//! \brief copy operator. It resize the left operand to the size of the 
		//! right operand.
		vec operator=(const vec& a)
		{
			this->resize(a.size());
			for(unsigned int i=0; i<a.size(); ++i)
			{
				this->at(i) = a[i];
			}
			return *this ;
		}
63

Laurent Belcour's avatar
Laurent Belcour committed
64 65 66 67
		// Mathematical operators
		//
		friend vec operator-(const vec& a)
		{
68 69
			vec b(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
70 71 72 73 74 75 76 77 78 79
			{
				b[i] = -a[i] ;
			}
			return b ;
		}
		friend vec operator-(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
80 81
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
82 83 84 85 86 87 88 89 90 91
			{
				c[i] = a[i] - b[i];
			}
			return c ;
		}
		friend vec operator+(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
92 93
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
94 95 96 97 98 99 100 101 102 103
			{
				c[i] = a[i] + b[i];
			}
			return c ;
		}
		friend vec operator*(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
104 105
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
106 107 108 109 110
			{
				c[i] = a[i] * b[i];
			}
			return c ;
		}
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
		friend vec operator*(const vec& a, double b)
		{
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				c[i] = a[i] * b;
			}
			return c ;
		}
		friend vec operator*(double a, const vec& b)
		{
			vec c(b.size()) ;
			for(unsigned int i=0; i<b.size(); ++i)
			{
				c[i] = a * b[i];
			}
			return c ;
		}
		friend vec operator/(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				c[i] = a[i] / b[i];
			}
			return c ;
		}
		friend vec operator/(const vec& a, double b)
		{
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				c[i] = a[i] / b;
			}
			return c ;
		}
		friend vec operator/(double a, const vec& b)
		{
			vec c(b.size()) ;
			for(unsigned int i=0; i<b.size(); ++i)
			{
				c[i] = a / b[i];
			}
			return c ;
		}
159 160 161 162 163 164 165 166 167 168
        friend double norm(const vec& a)
        {
            double norm = 0.0 ;
            for(unsigned int i=0; i<a.size(); ++i)
            {
                norm += a[i]*a[i];
            }
            return sqrt(norm);
        }

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		friend vec normalize(const vec& a)
		{
			vec b(a.size());
			double norm = 0.0 ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				norm += a[i]*a[i];
			}
			norm = sqrt(norm);
			
			for(unsigned int i=0; i<a.size(); ++i)
			{
				b[i] = a[i]/norm;
			}
			return b;
		}
		friend double dot(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size());
#endif
			double res = 0.0;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				res += a[i]*b[i];
			}

			return res;
		}
198 199 200
        friend bool operator<(const vec& a, const vec& b)
        {
            bool lessthan = true ;
201
            for(unsigned int i=0; i<a.size(); ++i)
202 203 204 205 206 207
                lessthan &= a[i] < b[i];
            return lessthan;
        }
        friend bool operator>(const vec& a, const vec& b)
        {
            bool greatthan = true ;
208
            for(unsigned int i=0; i<a.size(); ++i)
209 210 211 212
                greatthan &= a[i] > b[i];
            return greatthan;
        }

213
/*
Laurent Belcour's avatar
Laurent Belcour committed
214 215
		friend double norm(const vec& a)
		{
Laurent Belcour's avatar
Laurent Belcour committed
216

Laurent Belcour's avatar
Laurent Belcour committed
217 218
		}
*/
219
		//! \brief IO Functions
Laurent Belcour's avatar
Laurent Belcour committed
220
		//
Laurent Belcour's avatar
Laurent Belcour committed
221 222
		friend std::ostream& operator<< (std::ostream& out, const vec& v)
		{
Laurent Belcour's avatar
Laurent Belcour committed
223
			out << "[" ;
Laurent Belcour's avatar
Laurent Belcour committed
224
			for(unsigned int i=0; i<v.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
225
			{
Laurent Belcour's avatar
Laurent Belcour committed
226
				if(i > 0) out << ", " ;
Laurent Belcour's avatar
Laurent Belcour committed
227 228 229 230 231
				out << v[i] ;
			}
			out << "]" ;

			return out ;
232
        }
Laurent Belcour's avatar
Laurent Belcour committed
233 234 235


} ;
236 237 238 239 240 241 242 243 244 245
#endif 

#include <Eigen/Core>
typedef Eigen::VectorXd vec;

double norm(const vec& a);

vec normalize(const vec& a);

double dot(const vec& a, const vec& b);
246

247 248
vec product(const vec& a, const vec& b);

249 250
std::ostream& operator<<(std::ostream& out, const vec& v);

251 252 253 254 255 256 257 258 259 260 261 262 263 264
//! \brief locate the first index of value v in vector vec. Complexity in
//! O(n) is the worst case.
template<typename T> int is_in(std::vector<T> ve, T v)
{
    int res = -1;
    for(unsigned int i=0; i<ve.size(); ++i)
    {
        if(ve[i] == v)
            return i;
    }

    return res;
}

265 266 267 268 269
template<typename T> T clamp(T x, T a, T b)
{
	return std::max<T>(std::min<T>(x, b), a);
}

pacanows's avatar
pacanows committed
270
#ifdef _WIN32
Laurent Belcour's avatar
Laurent Belcour committed
271 272 273 274 275
#define NOT_IMPLEMENTED() \
std::cerr << "<<ERROR>> not implemented " << __FUNCDNAME__ << " in file " << __FILE__ \
          << ":" << __LINE__ << std::endl; \
throw
#else
276
#define NOT_IMPLEMENTED() \
Laurent Belcour's avatar
Laurent Belcour committed
277
std::cerr << "<<ERROR>> not implemented " << __PRETTY_FUNCTION__ << " in file " << __FILE__ \
278 279
          << ":" << __LINE__ << std::endl; \
throw
Laurent Belcour's avatar
Laurent Belcour committed
280
#endif
281

Laurent Belcour's avatar
Laurent Belcour committed
282
// Mathematical definition not provided on the Window plateform
pacanows's avatar
pacanows committed
283
#ifdef _WIN32
284
#define M_PI 3.1415926535897932384626433832795
Laurent Belcour's avatar
Laurent Belcour committed
285 286 287 288 289

template<typename T> bool isnan(T x)
{
	return x==std::numeric_limits<T>::signaling_NaN();
}
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

template<typename T> T erf(T x)
{
    // constants
    const double a1 =  0.254829592;
    const double a2 = -0.284496736;
    const double a3 =  1.421413741;
    const double a4 = -1.453152027;
    const double a5 =  1.061405429;
    const double p  =  0.3275911;

    // Save the sign of x
    int sign = 1;
    if (x < 0) {
        sign = -1;
	 }
    x = fabs(x);

    // A&S formula 7.1.26
    const double t = 1.0/(1.0 + p*x);
    const double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);

    return sign*y;
}
314
#endif
315

pacanows's avatar
pacanows committed
316
#ifdef _WIN32
317 318 319
#define ALTA_DLL_EXPORT extern "C" __declspec(dllexport)
#else
#define ALTA_DLL_EXPORT extern "C"
320
#endif
Laurent Belcour's avatar
Laurent Belcour committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

/*! \brief The timer class: a cross plateform timing solution
 */
class timer
{
    public:

        //! \brief Constructor
        timer();

        //! \brief Start the timer
        void start();

        //! \brief Stop the timer
        void stop();

        //! \brief Reset the timer
        void reset();

        //! \brief Compute the time taken since the start, accounting for
        //! all pauses.
        int elapsed() const;

        //! \brief Print the time to the standard output
        void print(std::ostream& out) const;

        //! \brief ostream compliant operator
348
        friend std::ostream& operator<<(std::ostream& out, const timer& t);
Laurent Belcour's avatar
Laurent Belcour committed
349 350 351 352 353 354 355 356 357 358 359

    private:

        //! \brief get the current time in seconds. This is a system dependant
        //! function.
        //! \internal
        unsigned int current_time() const;

        unsigned int _start, _stop;
        unsigned int _elapsed;
};