ResultPoint.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
  2. /*
  3. * ResultPoint.cpp
  4. * zxing
  5. *
  6. * Created by Christian Brunschen on 13/05/2008.
  7. * Copyright 2008 ZXing authors All rights reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #include <zxing/ResultPoint.h>
  22. #include <zxing/common/detector/MathUtils.h>
  23. using zxing::common::detector::MathUtils;
  24. namespace zxing {
  25. ResultPoint::ResultPoint() : posX_(0), posY_(0) {}
  26. ResultPoint::ResultPoint(float x, float y) : posX_(x), posY_(y) {}
  27. ResultPoint::ResultPoint(int x, int y) : posX_(float(x)), posY_(float(y)) {}
  28. ResultPoint::~ResultPoint() {}
  29. float ResultPoint::getX() const {
  30. return posX_;
  31. }
  32. float ResultPoint::getY() const {
  33. return posY_;
  34. }
  35. bool ResultPoint::equals(Ref<ResultPoint> other) {
  36. return posX_ == other->getX() && posY_ == other->getY();
  37. }
  38. /**
  39. * <p>Orders an array of three ResultPoints in an order [A,B,C] such that AB < AC and
  40. * BC < AC and the angle between BC and BA is less than 180 degrees.
  41. */
  42. void ResultPoint::orderBestPatterns(std::vector<Ref<ResultPoint> > &patterns) {
  43. // Find distances between pattern centers
  44. float zeroOneDistance = distance(patterns[0]->getX(), patterns[1]->getX(),patterns[0]->getY(), patterns[1]->getY());
  45. float oneTwoDistance = distance(patterns[1]->getX(), patterns[2]->getX(),patterns[1]->getY(), patterns[2]->getY());
  46. float zeroTwoDistance = distance(patterns[0]->getX(), patterns[2]->getX(),patterns[0]->getY(), patterns[2]->getY());
  47. Ref<ResultPoint> pointA, pointB, pointC;
  48. // Assume one closest to other two is B; A and C will just be guesses at first
  49. if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) {
  50. pointB = patterns[0];
  51. pointA = patterns[1];
  52. pointC = patterns[2];
  53. } else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) {
  54. pointB = patterns[1];
  55. pointA = patterns[0];
  56. pointC = patterns[2];
  57. } else {
  58. pointB = patterns[2];
  59. pointA = patterns[0];
  60. pointC = patterns[1];
  61. }
  62. // Use cross product to figure out whether A and C are correct or flipped.
  63. // This asks whether BC x BA has a positive z component, which is the arrangement
  64. // we want for A, B, C. If it's negative, then we've got it flipped around and
  65. // should swap A and C.
  66. if (crossProductZ(pointA, pointB, pointC) < 0.0f) {
  67. Ref<ResultPoint> temp = pointA;
  68. pointA = pointC;
  69. pointC = temp;
  70. }
  71. patterns[0] = pointA;
  72. patterns[1] = pointB;
  73. patterns[2] = pointC;
  74. }
  75. float ResultPoint::distance(Ref<ResultPoint> pattern1, Ref<ResultPoint> pattern2) {
  76. return MathUtils::distance(pattern1->posX_,
  77. pattern1->posY_,
  78. pattern2->posX_,
  79. pattern2->posY_);
  80. }
  81. float ResultPoint::distance(float x1, float x2, float y1, float y2) {
  82. float xDiff = x1 - x2;
  83. float yDiff = y1 - y2;
  84. return (float) sqrt((double) (xDiff * xDiff + yDiff * yDiff));
  85. }
  86. float ResultPoint::crossProductZ(Ref<ResultPoint> pointA, Ref<ResultPoint> pointB, Ref<ResultPoint> pointC) {
  87. float bX = pointB->getX();
  88. float bY = pointB->getY();
  89. return ((pointC->getX() - bX) * (pointA->getY() - bY)) - ((pointC->getY() - bY) * (pointA->getX() - bX));
  90. }
  91. }