17 #include <dlib/matrix.h>
18 #define _USE_MATH_DEFINES
31 #define adoreMatrix dlib::matrix
39 void set(T* data,T value,
int size)
41 for(
int i=0;i<size;i++)
53 int n = ceil((xend -
x0) / dx) + 1;
54 adoreMatrix<T, 1, 0> result(1, n);
55 for (
int i = 0; i < n; i++)
57 result(0, i) =
x0 + dx*(T)i;
59 result(0, n - 1) = xend;
66 int sequence(T
x0, T dx, T xend, T* target,
int max_size)
68 int n = (
std::min)(ceil((xend -
x0) / dx) + 1, max_size);
69 for (
int i = 0; i < n; i++)
71 target[i] =
x0 + dx*(T)i;
82 for (
int i = 0; i < size; i++)
84 target[i] =
x0 + dx*(T)i;
90 template<
typename T,
int n>
94 adoreMatrix<T, 1, n> result;
95 for (
int i = 0; i < n; i++)
97 result(0, i) =
x0 + (
x1 -
x0) / (T)(n - 1)*(T)i;
109 adoreMatrix<T, 1, 0> result;
110 result = dlib::zeros_matrix<T>(1, n);
111 for (
int i = 0; i < n-1; i++)
113 result(0, i) =
x0 + (
x1 -
x0) / (T)(n - 1)*(T)i;
115 result(0, n - 1) =
x1;
123 template<
typename T,
typename Tarray>
127 for (
int i = 0; i < n; i++)
129 target[i] =
x0 + (
x1 -
x0) / (T)(n - 1)*(T)i;
131 T tmp = target[n - 1];
140 for (
int i = 0; i < m.nc()*m.nr(); i++)target[i] = m(i);
145 template<
typename T,
long nr,
long nc>
148 for (
int i = 0; i < m.nc()*m.nr(); i++)target[i] = m(i);
153 template<
typename T,
long nr,
long nc>
156 for (
int i = 0; i < m.nc(); i++)target[i] = m(col,i);
169 return (n*
binomial(n - 1, k - 1)) / k;
179 return x - ((
x >= 0) ? (T)1 : (T)-1) * (std::floor)((std::abs)(
x) / d) * d;
185 template<
typename T,
long N>
189 for (
int i = 0; i <
x.nr(); i++)qvalue +=
x(i)*
x(i);
190 return (std::sqrt)(qvalue);
199 template<
typename T,
long N>
204 for(
int j=1;j<data.nc();j++)
206 T di = data(row,j-1);
207 T dj = data(row,j) + offset;
208 T delta = (std::abs)(dj-di);
209 if((std::abs)(dj+twopi-di)<delta)
213 }
else if((std::abs)(dj-twopi-di)<delta)
239 d_tangential = (
x*p +
y*q) / dd;
240 d_normal = (-
y*p +
x*q) / sqrt(dd);
256 T l = sqrt(
x*
x +
y*
y);
257 d_tangential = (
x*p +
y*q) / l;
258 d_normal = (-
y*p +
x*q) / l;
274 T l = sqrt(
x*
x +
y*
y);
275 T t = (
x*p +
y*q) / l;
276 n = (-
y*p +
x*q) / l;
280 return sqrt(p*p+q*q);
289 return sqrt(p*p+q*q);
320 template<
typename T1,
typename T2>
323 const double d = std::sqrt( (pi(1)-pj(1))*(pi(1)-pj(1)) + (pi(2)-pj(2))*(pi(2)-pj(2)) );
324 if(d<1e-6)
return false;
326 const double qY_ni = -ni(1)*(qX-pi(1)) + ni(0)*(qY-pi(2));
332 const double qY_nj = -nj(1)*(qX-pj(1)) + nj(0)*(qY-pj(2));
338 const double cross_n = ni(0)*nj(1)-ni(1)*nj(0);
339 if(std::abs(cross_n)>1e-10)
341 const double s_ni = (pj(1)*nj(1)-pj(2)*nj(0)+pi(2)*nj(0)-pi(1)*nj(1)) / cross_n;
342 const double qX_ni = ni(0)*(qX-pi(1)) + ni(1)*(qY-pi(2));
360 const double L = pj(0)-pi(0);
361 const double cosv = (pj(1)-pi(1))/d;
362 const double sinv = (pj(2)-pi(2))/d;
363 const double qXr = cosv*(qX-pi(1)) + sinv*(qY-pi(2));
364 const double qYr =-sinv*(qX-pi(1)) + cosv*(qY-pi(2));
365 const double nixr = cosv*ni(0) + sinv*ni(1);
366 const double niyr =-sinv*ni(0) + cosv*ni(1);
367 const double njxr = cosv*nj(0) + sinv*nj(1);
368 const double njyr =-sinv*nj(0) + cosv*nj(1);
369 const double a = -d*niyr + d*njyr;
370 const double b = niyr*qXr - njyr*qXr + d*niyr - nixr*qYr + njxr*qYr;
371 const double c = -niyr*qXr + nixr*qYr;
374 if(std::abs(a)>1e-10)
376 const double s1 = (-b+std::sqrt(b*b-4.0*a*c))*0.5/a;
377 const double s2 = (-b-std::sqrt(b*b-4.0*a*c))*0.5/a;
378 sr = (0.0<=s1 && s1<=1.0)?s1:s2;
382 else if(std::abs(b)>1e-10)
390 if(!(0.0<=sr && sr<=1.0))
398 const double q0X = (1.0-sr) * pi(1) + sr * pj(1);
399 const double q0Y = (1.0-sr) * pi(2) + sr * pj(2);
400 const double dX = qX-q0X;
401 const double dY = qY-q0Y;
402 const double nx = (1.0-sr) * ni(0) + sr * nj(0);
403 const double ny = (1.0-sr) * ni(1) + sr * nj(1);
404 const double nL = std::sqrt(nx*nx+ny*ny);
405 t = (nx * dX + ny * dY) / nL;
406 const double e = (-ny * dX + nx * dY) / nL;
407 if(std::abs(e)>1.0e-10)
424 template<
typename T1,
typename T2>
427 adoreMatrix<double,3,1> pi,pj;
428 adoreMatrix<double,2,1> ni,nj;
429 for(
int i=0;i<centerline->getData().nc()-1;i++)
432 for(
int d=0;d<3;d++)pi(d)=centerline->getData()(d,i);
433 for(
int d=0;d<3;d++)pj(d)=centerline->getData()(d,j);
434 for(
int d=1;d<3;d++)ni(d-1)=normals->getData()(d,i);
435 for(
int d=1;d<3;d++)nj(d-1)=normals->getData()(d,j);
474 template<
typename T1,
typename T2>
475 bool fromRelative(
double s,
double t,
const T1 pi,
const T1 pj,
const T2 ni,
const T2 nj,
double& X,
double & Y,
double& Z)
478 const double L = pj(0)-pi(0);
479 const double sr = (s-pi(0))/L;
480 const double q0X = (1.0-sr) * pi(1) + sr * pj(1);
481 const double q0Y = (1.0-sr) * pi(2) + sr * pj(2);
482 const double q0Z = (1.0-sr) * pi(3) + sr * pj(3);
483 const double nx = (1.0-sr) * ni(0) + sr * nj(0);
484 const double ny = (1.0-sr) * ni(1) + sr * nj(1);
485 const double nL = std::sqrt(nx*nx+ny*ny);
486 X = q0X + nx * t / nL;
487 Y = q0Y + ny * t / nL;
499 template<
typename T1,
typename T2>
500 void fromRelative(
double s,
double t,T1 centerline,T2 normals,
double& X,
double& Y,
double& Z)
502 adoreMatrix<double,3,1> pi,pj;
503 adoreMatrix<double,2,1> ni,nj;
504 int i = centerline->findIndex(s);
506 for(
int d=0;d<3;d++)pi(d)=centerline->getData()(d,i);
507 for(
int d=0;d<3;d++)pj(d)=centerline->getData()(d,j);
508 for(
int d=1;d<3;d++)ni(d-1)=normals->getData()(d,i);
509 for(
int d=1;d<3;d++)nj(d-1)=normals->getData()(d,j);
522 inline bool intersectLines(T a, T b, T c, T d, T e, T f, T
g, T h, T&
x0, T&
x1,
bool& x0_inside,
bool& x1_inside)
531 if (
w == (T)0)
return false;
532 x1 = (a*q - b*p - e*q + f*p) /
w;
533 x0 = (e*s - f*
r - a*s + b*
r) / (-
w);
534 x0_inside = (T)0 <=
x0 &&
x0 <= (T)1;
535 x1_inside = (T)0 <=
x1 &&
x1 <= (T)1;
546 inline bool intersectLines2(T a, T b, T c, T d, T e, T f, T
g, T h, T&
x0, T&
x1, T
eps)
557 if (
w == (T)0)
return false;
558 x1 = (a*q - b*p - e*q + f*p) /
w;
559 x0 = (e*s - f*
r - a*s + b*
r) / (-
w);
562 return x0_inside && x1_inside;
578 inline void bound(T lb,T value[],
size_t count, T ub)
580 for (
size_t a=0; a< count;++a)
582 value[a] =
bound(lb, value[a], ub);
601 s[0]=u[1]*v[2]-u[2]*v[1];
602 s[1]=u[2]*v[0]-u[0]*v[2];
603 s[2]=u[0]*v[1]-u[1]*v[0];
610 template<
int d,
typename T>
626 template<
int k,
typename T>
634 l=((T)1)/std::sqrt(l);
655 bool overlaps(
const T& a0,
const T&a1,
const T& b0,
const T& b1)
657 return (b0<=a0 && a0<=b1) || (b0<=a1 && a1<=b1) || (a0<=b0 && b0<=a1) || (a0<=b1 && b1<=a1);
670 template<
typename T,
long N,
long M>
671 adoreMatrix<T, N, M>
min(adoreMatrix<T, N, M> a,
const adoreMatrix<T, N, M>& b)
673 for (
int i = 0; i < N; i++)
675 for (
int j = 0; j < M; j++)
677 a(i, j) = (
std::min)(a(i, j), b(i, j));
685 template<
typename T,
long N,
long M>
686 adoreMatrix<T, N, M>
max(adoreMatrix<T, N, M> a,
const adoreMatrix<T, N, M>& b)
688 for (
int i = 0; i < N; i++)
690 for (
int j = 0; j < M; j++)
692 a(i, j) = (
std::max)(a(i, j), b(i, j));
698 inline double min(
double a,
const double & b)
702 inline double max(
double a,
const double & b)
706 inline float min(
float a,
const float & b)
710 inline float max(
float a,
const float & b)
714 inline int min(
int a,
const int & b)
718 inline int max(
int a,
const int & b)
722 template <
typename T>
725 return (T(0) < val) - (val < T(0));
const double eps
Definition: cubic_piecewise_function.cpp:16
bool intersectLines(T a, T b, T c, T d, T e, T f, T g, T h, T &x0, T &x1, bool &x0_inside, bool &x1_inside)
Definition: adoremath.h:522
adoreMatrix< T, 1, 0 > sequence(T x0, T dx, T xend)
Definition: adoremath.h:51
void getRelativeCoordinatesPointVSLine(T a, T b, T c, T d, T e, T f, T &d_tangential, T &d_normal)
Definition: adoremath.h:247
T * normalize(T *v)
Definition: adoremath.h:627
bool annotatedDataOrdering_fct(std::pair< double, T > i, std::pair< double, T > j)
Definition: adoremath.h:591
T dot(T *u, T *v)
Definition: adoremath.h:611
T norm2(const adoreMatrix< T, N, 1 > &x)
Definition: adoremath.h:186
void createAngularContinuity(adoreMatrix< T, N, 0 > &data, int row)
Definition: adoremath.h:200
int max(int a, const int &b)
Definition: adoremath.h:718
void extendBounds(T &min, T value, T &max)
Definition: adoremath.h:646
adoreMatrix< T, 1, n > linspace(T x0, T x1)
Definition: adoremath.h:91
T remainder(T x, T d)
Definition: adoremath.h:177
int signum(T val)
Definition: adoremath.h:723
T bound(T lb, T value, T ub)
Definition: adoremath.h:569
bool toRelativeWithNormalExtrapolation(double qX, double qY, const T1 pi, const T1 pj, const T2 ni, const T2 nj, double &s, double &t)
Transformation from Euclidean coordinate system to a relative coordinate system represented by linear...
Definition: adoremath.h:321
void copyRowToArray(const adoreMatrix< T, nr, nc > &m, T *target, int col)
Definition: adoremath.h:154
int binomial(int n, int k)
Definition: adoremath.h:161
T min(T a, T b, T c, T d)
Definition: adoremath.h:663
void set(T *data, T value, int size)
Definition: adoremath.h:39
int min(int a, const int &b)
Definition: adoremath.h:714
T * cross(T *u, T *v, T *s)
Definition: adoremath.h:599
bool overlaps(const T &a0, const T &a1, const T &b0, const T &b1)
Definition: adoremath.h:655
double getDistancePointToLine(T a, T b, T c, T d, T e, T f, T &rel, T &n)
Definition: adoremath.h:265
adoreMatrix< T, N, M > max(adoreMatrix< T, N, M > a, const adoreMatrix< T, N, M > &b)
Definition: adoremath.h:686
bool intersectLines2(T a, T b, T c, T d, T e, T f, T g, T h, T &x0, T &x1, T eps)
Definition: adoremath.h:546
void comparePointWithLine(T a, T b, T c, T d, T e, T f, T &d_tangential, T &d_normal)
Definition: adoremath.h:229
bool fromRelative(double s, double t, const T1 pi, const T1 pj, const T2 ni, const T2 nj, double &X, double &Y, double &Z)
Transform from relative coordinates to Euclidean coordinates.
Definition: adoremath.h:475
void copyToArray(const adoreMatrix< T > &m, T *target)
Definition: adoremath.h:138
x0
Definition: adore_set_goal.py:25
x
Definition: adore_set_goal.py:30
g
Definition: adore_set_goal.py:29
y
Definition: adore_set_goal.py:31
x1
Definition: adore_set_pose.py:28
w
Definition: adore_set_pose.py:40
r
Definition: adore_suppress_lanechanges.py:209
Definition: areaofeffectconverter.h:20