Apollo  6.0
Open source self driving car software
i_line.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
20 
21 namespace apollo {
22 namespace perception {
23 namespace common {
24 // Measure point to line distance in 2D space, point p is in inhomogeneous
25 // coordinates
26 template <typename T>
27 inline T ILineToPointDistance2d(const T *l, const T *p) {
28  return IDiv(IAbs(IDot2(l, p) + l[2]), IL2Norm2(l));
29 }
30 
31 // Fit a line l: ax+by+c = 0 in 2D space using total least square method.
32 // The input n 2D points in inhomogeneous coordinates. Array x has size
33 // 2*n and points are stored as [x0, y0, x1, y1, ...]. x will be destroyed
34 // after calling this routine.
35 template <typename T>
36 inline void ILineFit2dTotalLeastSquare(T *x, T *l, int n) {
37  IZero3(l);
38  if (n < 2) {
39  return;
40  }
41  T ma[4], eigv[2];
42  T mq[4] = {static_cast<T>(0.0), static_cast<T>(0.0), static_cast<T>(0.0),
43  static_cast<T>(0.0)};
44  // // compute the centroid of input data points
45  int i, length = 2 * n;
46  T xm = static_cast<T>(0.0);
47  T ym = static_cast<T>(0.0);
48  for (i = 0; i < length; i += 2) {
49  xm += x[i];
50  ym += x[i + 1];
51  }
52  xm = IDiv(xm, n);
53  ym = IDiv(ym, n);
54  for (i = 0; i < length; i += 2) {
55  x[i] -= xm;
56  x[i + 1] -= ym;
57  }
58  IMultAtAnx2(x, ma, n);
59  IEigSymmetric2x2Closed(ma, eigv, mq);
60  l[0] = mq[1];
61  l[1] = mq[3];
62  l[2] = -xm * l[0] - ym * l[1];
63 }
64 
65 // Fit a line l: ax+by+c = 0 in 2D space with two 2D points in inhomogeneous
66 // space.
67 template <typename T>
68 inline void ILineFit2d(const T *x, const T *xp, T *l) {
69  T ma[4] = {x[0], x[1], xp[0], xp[1]};
71 }
72 
73 } // namespace common
74 } // namespace perception
75 } // namespace apollo
void IZero3(T a[3])
Definition: i_blas.h:334
float IAbs(float a)
Definition: i_basic.h:29
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void IMultAtAnx2(const T *A, T *AtA, int n)
Definition: i_blas.h:4812
void ILineFit2dTotalLeastSquare(T *x, T *l, int n)
Definition: i_line.h:36
float IDiv(float a, float b)
Definition: i_basic.h:35
T IDot2(const T x[2], const T y[2])
Definition: i_blas.h:2256
void ILineFit2d(const T *x, const T *xp, T *l)
Definition: i_line.h:68
T ILineToPointDistance2d(const T *l, const T *p)
Definition: i_line.h:27
void IEigSymmetric2x2Closed(const T *A, T *EV, T *Q)
Definition: i_util.h:29
T IL2Norm2(const T x[2])
Definition: i_blas.h:2808