選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ZXPerspectiveTransform.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2012 ZXing authors
  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. #import "ZXPerspectiveTransform.h"
  17. @interface ZXPerspectiveTransform ()
  18. @property (nonatomic, assign, readonly) float a11;
  19. @property (nonatomic, assign, readonly) float a12;
  20. @property (nonatomic, assign, readonly) float a13;
  21. @property (nonatomic, assign, readonly) float a21;
  22. @property (nonatomic, assign, readonly) float a22;
  23. @property (nonatomic, assign, readonly) float a23;
  24. @property (nonatomic, assign, readonly) float a31;
  25. @property (nonatomic, assign, readonly) float a32;
  26. @property (nonatomic, assign, readonly) float a33;
  27. @end
  28. @implementation ZXPerspectiveTransform
  29. - (id)initWithA11:(float)a11 a21:(float)a21 a31:(float)a31 a12:(float)a12 a22:(float)a22 a32:(float)a32 a13:(float)a13 a23:(float)a23 a33:(float)a33 {
  30. if (self = [super init]) {
  31. _a11 = a11;
  32. _a12 = a12;
  33. _a13 = a13;
  34. _a21 = a21;
  35. _a22 = a22;
  36. _a23 = a23;
  37. _a31 = a31;
  38. _a32 = a32;
  39. _a33 = a33;
  40. }
  41. return self;
  42. }
  43. + (ZXPerspectiveTransform *)quadrilateralToQuadrilateral:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 x0p:(float)x0p y0p:(float)y0p x1p:(float)x1p y1p:(float)y1p x2p:(float)x2p y2p:(float)y2p x3p:(float)x3p y3p:(float)y3p {
  44. ZXPerspectiveTransform *qToS = [self quadrilateralToSquare:x0 y0:y0 x1:x1 y1:y1 x2:x2 y2:y2 x3:x3 y3:y3];
  45. ZXPerspectiveTransform *sToQ = [self squareToQuadrilateral:x0p y0:y0p x1:x1p y1:y1p x2:x2p y2:y2p x3:x3p y3:y3p];
  46. return [sToQ times:qToS];
  47. }
  48. - (void)transformPoints:(float *)points pointsLen:(int)pointsLen {
  49. int max = pointsLen;
  50. for (int i = 0; i < max; i += 2) {
  51. float x = points[i];
  52. float y = points[i + 1];
  53. float denominator = self.a13 * x + self.a23 * y + self.a33;
  54. points[i] = (self.a11 * x + self.a21 * y + self.a31) / denominator;
  55. points[i + 1] = (self.a12 * x + self.a22 * y + self.a32) / denominator;
  56. }
  57. }
  58. /**
  59. * Convenience method, not optimized for performance.
  60. */
  61. - (void)transformPoints:(float *)xValues yValues:(float *)yValues pointsLen:(int)pointsLen {
  62. int n = pointsLen;
  63. for (int i = 0; i < n; i ++) {
  64. float x = xValues[i];
  65. float y = yValues[i];
  66. float denominator = self.a13 * x + self.a23 * y + self.a33;
  67. xValues[i] = (self.a11 * x + self.a21 * y + self.a31) / denominator;
  68. yValues[i] = (self.a12 * x + self.a22 * y + self.a32) / denominator;
  69. }
  70. }
  71. + (ZXPerspectiveTransform *)squareToQuadrilateral:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {
  72. float dx3 = x0 - x1 + x2 - x3;
  73. float dy3 = y0 - y1 + y2 - y3;
  74. if (dx3 == 0.0f && dy3 == 0.0f) {
  75. // Affine
  76. return [[ZXPerspectiveTransform alloc] initWithA11:x1 - x0 a21:x2 - x1 a31:x0 a12:y1 - y0 a22:y2 - y1 a32:y0 a13:0.0f a23:0.0f a33:1.0f];
  77. } else {
  78. float dx1 = x1 - x2;
  79. float dx2 = x3 - x2;
  80. float dy1 = y1 - y2;
  81. float dy2 = y3 - y2;
  82. float denominator = dx1 * dy2 - dx2 * dy1;
  83. float a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
  84. float a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
  85. return [[ZXPerspectiveTransform alloc] initWithA11:x1 - x0 + a13 * x1 a21:x3 - x0 + a23 * x3 a31:x0 a12:y1 - y0 + a13 * y1 a22:y3 - y0 + a23 * y3 a32:y0 a13:a13 a23:a23 a33:1.0f];
  86. }
  87. }
  88. + (ZXPerspectiveTransform *)quadrilateralToSquare:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {
  89. return [[self squareToQuadrilateral:x0 y0:y0 x1:x1 y1:y1 x2:x2 y2:y2 x3:x3 y3:y3] buildAdjoint];
  90. }
  91. - (ZXPerspectiveTransform *)buildAdjoint {
  92. return [[ZXPerspectiveTransform alloc] initWithA11:self.a22 * self.a33 - self.a23 * self.a32
  93. a21:self.a23 * self.a31 - self.a21 * self.a33
  94. a31:self.a21 * self.a32 - self.a22 * self.a31
  95. a12:self.a13 * self.a32 - self.a12 * self.a33
  96. a22:self.a11 * self.a33 - self.a13 * self.a31
  97. a32:self.a12 * self.a31 - self.a11 * self.a32
  98. a13:self.a12 * self.a23 - self.a13 * self.a22
  99. a23:self.a13 * self.a21 - self.a11 * self.a23
  100. a33:self.a11 * self.a22 - self.a12 * self.a21];
  101. }
  102. - (ZXPerspectiveTransform *)times:(ZXPerspectiveTransform *)other {
  103. return [[ZXPerspectiveTransform alloc] initWithA11:self.a11 * other.a11 + self.a21 * other.a12 + self.a31 * other.a13
  104. a21:self.a11 * other.a21 + self.a21 * other.a22 + self.a31 * other.a23
  105. a31:self.a11 * other.a31 + self.a21 * other.a32 + self.a31 * other.a33
  106. a12:self.a12 * other.a11 + self.a22 * other.a12 + self.a32 * other.a13
  107. a22:self.a12 * other.a21 + self.a22 * other.a22 + self.a32 * other.a23
  108. a32:self.a12 * other.a31 + self.a22 * other.a32 + self.a32 * other.a33
  109. a13:self.a13 * other.a11 + self.a23 * other.a12 + self.a33 * other.a13
  110. a23:self.a13 * other.a21 + self.a23 * other.a22 + self.a33 * other.a23
  111. a33:self.a13 * other.a31 + self.a23 * other.a32 + self.a33 * other.a33];
  112. }
  113. @end