Skip to content

Commit c868b7d

Browse files
committed
Point: use free function dot() instead of operator*()
Also rename crossProduct() to cross() which is consistent with dot() and the name used in e.g. Python numpy and the eigen c++ library.
1 parent 149d101 commit c868b7d

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

core/src/PerspectiveTransform.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ PerspectiveTransform PerspectiveTransform::UnitSquareTo(const QuadrilateralF& q)
7171
} else {
7272
auto d1 = q[1] - q[2];
7373
auto d2 = q[3] - q[2];
74-
auto denominator = crossProduct(d1, d2);
75-
auto a13 = crossProduct(d3, d2) / denominator;
76-
auto a23 = crossProduct(d1, d3) / denominator;
74+
auto denominator = cross(d1, d2);
75+
auto a13 = cross(d3, d2) / denominator;
76+
auto a23 = cross(d1, d3) / denominator;
7777
return {x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0,
7878
y1 - y0 + a13 * y1, y3 - y0 + a23 * y3, y0,
7979
a13, a23, 1.0f};

core/src/Point.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,26 @@ PointT<T> operator/(const PointT<T>& a, U d)
7070
return {a.x / d, a.y / d};
7171
}
7272

73-
template <typename T, typename U>
74-
double operator*(const PointT<T>& a, const PointT<U>& b)
73+
template <typename T>
74+
double length(PointT<T> d)
7575
{
76-
return double(a.x) * b.x + a.y * b.y;
76+
return std::sqrt(dot(d, d));
7777
}
7878

7979
template <typename T>
8080
double distance(PointT<T> a, PointT<T> b)
8181
{
82-
auto d = a - b;
83-
return std::sqrt(d * d);
82+
return length(a - b);
8483
}
8584

86-
template <typename T>
87-
double length(PointT<T> d)
85+
template <typename T, typename U>
86+
double dot(const PointT<T>& a, const PointT<U>& b)
8887
{
89-
return std::sqrt(d * d);
88+
return double(a.x) * b.x + a.y * b.y;
9089
}
9190

9291
template <typename T>
93-
double crossProduct(PointT<T> a, PointT<T> b)
92+
double cross(PointT<T> a, PointT<T> b)
9493
{
9594
return a.x * b.y - b.x * a.y;
9695
}

core/src/Quadrilateral.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool IsConvex(const Quadrilateral<PointT>& poly)
7878
{
7979
auto d1 = poly[(i + 2) % N] - poly[(i + 1) % N];
8080
auto d2 = poly[i] - poly[(i + 1) % N];
81-
auto cp = crossProduct(d1, d2);
81+
auto cp = cross(d1, d2);
8282

8383
if (i == 0)
8484
sign = cp > 0;

core/src/datamatrix/DMDetector.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,12 @@ class RegressionLine
448448
a = +sumXY / std::sqrt(sumXX * sumXX + sumXY * sumXY);
449449
b = -sumXX / std::sqrt(sumXX * sumXX + sumXY * sumXY);
450450
}
451-
if (_directionInward * normal() < 0) {
451+
if (dot(_directionInward, normal()) < 0) {
452452
a = -a;
453453
b = -b;
454454
}
455-
c = normal() * mean; // (a*mean.x + b*mean.y);
456-
return _directionInward * normal() > 0.5; // angle between original and new direction is at most 60 degree
455+
c = dot(normal(), mean); // (a*mean.x + b*mean.y);
456+
return dot(_directionInward, normal()) > 0.5; // angle between original and new direction is at most 60 degree
457457
}
458458

459459
template <typename Container, typename Filter>
@@ -474,7 +474,7 @@ class RegressionLine
474474
int length() const { return _points.size() >= 2 ? int(distance(_points.front(), _points.back())) : 0; }
475475
bool isValid() const { return !std::isnan(a); }
476476
PointF normal() const { return isValid() ? PointF(a, b) : _directionInward; }
477-
double signedDistance(PointI p) const { return normal() * p - c; }
477+
double signedDistance(PointI p) const { return dot(normal(), p) - c; }
478478
PointF project(PointI p) const { return p - signedDistance(p) * normal(); }
479479

480480
void reverse() { std::reverse(_points.begin(), _points.end()); }
@@ -483,7 +483,7 @@ class RegressionLine
483483
assert(_directionInward != PointF());
484484
_points.push_back(p);
485485
if (_points.size() == 1)
486-
c = normal() * p;
486+
c = dot(normal(), p);
487487
}
488488

489489
void pop_back() { _points.pop_back(); }
@@ -605,7 +605,7 @@ class EdgeTracer : public BitMatrixCursor<PointI>
605605
auto old_d = d;
606606
setDirection(p - origin);
607607
// if the new direction is pointing "backward", i.e. angle(new, old) > 90 deg -> break
608-
if (d * old_d < 0)
608+
if (dot(d, old_d) < 0)
609609
return false;
610610
// make sure d stays in the same quadrant to prevent an infinite loop
611611
if (std::abs(d.x) == std::abs(d.y))
@@ -651,7 +651,7 @@ class EdgeTracer : public BitMatrixCursor<PointI>
651651
// In case the 'go outward' step in traceStep lead us astray, we might end up with a line
652652
// that is almost perpendicular to d. Then the back-projection below can result in an
653653
// endless loop. Break if the angle between d and line is greater than 45 deg.
654-
if (std::abs(normalized(d) * line.normal()) > 0.7) // thresh is approx. sin(45 deg)
654+
if (std::abs(dot(normalized(d), line.normal())) > 0.7) // thresh is approx. sin(45 deg)
655655
return false;
656656

657657
auto np = line.project(p);
@@ -663,7 +663,7 @@ class EdgeTracer : public BitMatrixCursor<PointI>
663663
p = round(np);
664664
}
665665
else {
666-
auto stepLengthInMainDir = line.points().empty() ? 0.0 : mainDirection(d) * (p - line.points().back());
666+
auto stepLengthInMainDir = line.points().empty() ? 0.0 : dot(mainDirection(d), (p - line.points().back()));
667667
line.add(p);
668668

669669
if (stepLengthInMainDir > 1) {

core/src/qrcode/QRFinderPatternFinder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ static FinderPatternInfo SelectBestPatterns(std::vector<FinderPattern> possibleC
422422
std::array<FinderPattern, 3> bestPatterns;
423423
std::array<double, 3> squares;
424424

425-
auto squaredDistance = [](PointF a, PointF b) { return (a - b) * (a - b); };
425+
auto squaredDistance = [](PointF a, PointF b) { return dot((a - b), (a - b)); };
426426

427427
for (int i = 0; i < nbPossibleCenters - 2; i++) {
428428
auto& fpi = possibleCenters[i];
@@ -484,7 +484,7 @@ static FinderPatternInfo SelectBestPatterns(std::vector<FinderPattern> possibleC
484484
// This asks whether BC x BA has a positive z component, which is the arrangement
485485
// we want for A, B, C. If it's negative, then we've got it flipped around and
486486
// should swap A and C.
487-
if (crossProduct(c - b, a - b) < 0) {
487+
if (cross(c - b, a - b) < 0) {
488488
std::swap(a, c);
489489
}
490490

0 commit comments

Comments
 (0)