MatOfDMatch.mm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // MatOfDMatch.m
  3. //
  4. // Created by Giles Payne on 2019/12/27.
  5. //
  6. #import "MatOfDMatch.h"
  7. #import "Range.h"
  8. #import "DMatch.h"
  9. #import "CvType.h"
  10. #import "ArrayUtil.h"
  11. @implementation MatOfDMatch
  12. static const int _depth = CV_32F;
  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<DMatch*>*)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<DMatch*>*)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].queryIdx];
  46. data[_channels * index + 1] = [NSNumber numberWithFloat:array[index].trainIdx];
  47. data[_channels * index + 2] = [NSNumber numberWithFloat:array[index].imgIdx];
  48. data[_channels * index + 3] = [NSNumber numberWithFloat:array[index].distance];
  49. }
  50. [self alloc:(int)array.count];
  51. [self put:0 col:0 data:data];
  52. }
  53. - (NSArray<DMatch*>*)toArray {
  54. int length = [self length] / _channels;
  55. NSMutableArray<DMatch*>* ret = createArrayWithSize(length, [DMatch 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] = [[DMatch alloc] initWithQueryIdx:data[index * _channels].intValue trainIdx:data[index * _channels + 1].intValue imgIdx:data[index * _channels + 2].intValue distance:data[index * _channels + 3].floatValue];
  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