MatOfRect2i.mm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // MatOfRect2i.m
  3. //
  4. // Created by Giles Payne on 2019/12/27.
  5. //
  6. #import "MatOfRect2i.h"
  7. #import "Range.h"
  8. #import "Rect2i.h"
  9. #import "CvType.h"
  10. #import "ArrayUtil.h"
  11. @implementation MatOfRect2i
  12. static const int _depth = CV_32S;
  13. static const int _channels = 4;
  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<Rect2i*>*)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<Rect2i*>*)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 numberWithInt:array[index].x];
  46. data[_channels * index + 1] = [NSNumber numberWithInt:array[index].y];
  47. data[_channels * index + 2] = [NSNumber numberWithInt:array[index].width];
  48. data[_channels * index + 3] = [NSNumber numberWithInt:array[index].height];
  49. }
  50. [self alloc:(int)array.count];
  51. [self put:0 col:0 data:data];
  52. }
  53. - (NSArray<Rect2i*>*)toArray {
  54. int length = [self length] / _channels;
  55. NSMutableArray<Rect2i*>* ret = createArrayWithSize(length, [Rect2i new]);
  56. if (length > 0) {
  57. NSMutableArray<NSNumber*>* data = createArrayWithSize([self length], @0.0);
  58. [self get:0 col:0 data:data];
  59. for (int index = 0; index < length; index++) {
  60. ret[index] = [[Rect2i alloc] initWithX:data[index * _channels].intValue y:data[index * _channels + 1].intValue width:data[index * _channels + 2].intValue height:data[index * _channels + 3].intValue];
  61. }
  62. }
  63. return ret;
  64. }
  65. - (int)length {
  66. int num = [self checkVector:_channels depth:_depth];
  67. if (num < 0) {
  68. @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Incompatible Mat" userInfo:nil];
  69. }
  70. return num * _channels;
  71. }
  72. @end