MatOfRect2d.mm 2.8 KB

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