MatOfPoint2f.mm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // MatOfPoint2f.mm
  3. //
  4. // Created by Giles Payne on 2019/12/27.
  5. //
  6. #import "MatOfPoint2f.h"
  7. #import "Range.h"
  8. #import "Point2f.h"
  9. #import "CvType.h"
  10. #import "ArrayUtil.h"
  11. @implementation MatOfPoint2f
  12. static const int _depth = CV_32F;
  13. static const int _channels = 2;
  14. #ifdef __cplusplus
  15. - (instancetype)initWithNativeMat:(cv::Mat*)nativeMat {
  16. self = [super initWithNativeMat:nativeMat];
  17. if (self && ![self empty] && [self checkVector:_channels depth:_depth] < 0) {
  18. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Incompatible Mat" userInfo:nil];
  19. }
  20. return self;
  21. }
  22. #endif
  23. - (instancetype)initWithMat:(Mat*)mat {
  24. self = [super initWithMat:mat rowRange:[Range all]];
  25. if (self && ![self empty] && [self checkVector:_channels depth:_depth] < 0) {
  26. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Incompatible Mat" userInfo:nil];
  27. }
  28. return self;
  29. }
  30. - (instancetype)initWithArray:(NSArray<Point2f*>*)array {
  31. self = [super init];
  32. if (self) {
  33. [self fromArray:array];
  34. }
  35. return self;
  36. }
  37. - (void)alloc:(int)elemNumber {
  38. if (elemNumber>0) {
  39. [super create:elemNumber cols:1 type:[CvType makeType:_depth channels:_channels]];
  40. }
  41. }
  42. - (void)fromArray:(NSArray<Point2f*>*)array {
  43. NSMutableArray<NSNumber*>* data = [[NSMutableArray alloc] initWithCapacity:array.count * _channels];
  44. for (int index = 0; index < (int)array.count; index++) {
  45. data[_channels * index] = [NSNumber numberWithFloat:array[index].x];
  46. data[_channels * index + 1] = [NSNumber numberWithFloat:array[index].y];
  47. }
  48. [self alloc:(int)array.count];
  49. [self put:0 col:0 data:data];
  50. }
  51. - (NSArray<Point2f*>*)toArray {
  52. int length = [self length] / _channels;
  53. NSMutableArray<Point2f*>* ret = createArrayWithSize(length, [Point2f new]);
  54. if (length > 0) {
  55. NSMutableArray<NSNumber*>* data = createArrayWithSize([self length], @0.0);
  56. [self get:0 col:0 data:data];
  57. for (int index = 0; index < length; index++) {
  58. ret[index] = [[Point2f alloc] initWithX:data[index * _channels].floatValue y:data[index * _channels + 1].floatValue];
  59. }
  60. }
  61. return ret;
  62. }
  63. - (int)length {
  64. int num = [self checkVector:_channels depth:_depth];
  65. if (num < 0) {
  66. @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Incompatible Mat" userInfo:nil];
  67. }
  68. return num * _channels;
  69. }
  70. @end