MatOfPoint3.mm 2.5 KB

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