Float4.mm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Float4.mm
  3. //
  4. // Created by Giles Payne on 2020/02/05.
  5. //
  6. #import "Float4.h"
  7. #import "Mat.h"
  8. @implementation Float4 {
  9. cv::Vec4f native;
  10. }
  11. -(float)v0 {
  12. return native[0];
  13. }
  14. -(void)setV0:(float)v {
  15. native[0] = v;
  16. }
  17. -(float)v1 {
  18. return native[1];
  19. }
  20. -(void)setV1:(float)v {
  21. native[1] = v;
  22. }
  23. -(float)v2 {
  24. return native[2];
  25. }
  26. -(void)setV2:(float)v {
  27. native[2] = v;
  28. }
  29. -(float)v3 {
  30. return native[3];
  31. }
  32. -(void)setV3:(float)v {
  33. native[3] = v;
  34. }
  35. -(instancetype)init {
  36. return [self initWithV0:0.0 v1:0.0 v2:0.0 v3:0.0];
  37. }
  38. -(instancetype)initWithV0:(float)v0 v1:(float)v1 v2:(float)v2 v3:(float)v3 {
  39. self = [super init];
  40. if (self) {
  41. self.v0 = v0;
  42. self.v1 = v1;
  43. self.v2 = v2;
  44. self.v3 = v3;
  45. }
  46. return self;
  47. }
  48. -(instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  49. self = [super init];
  50. if (self) {
  51. [self set:vals];
  52. }
  53. return self;
  54. }
  55. +(instancetype)fromNative:(cv::Vec4f&)vec4f {
  56. return [[Float4 alloc] initWithV0:vec4f[0] v1:vec4f[1] v2:vec4f[2] v3:vec4f[3]];
  57. }
  58. -(void)set:(NSArray<NSNumber*>*)vals {
  59. self.v0 = (vals != nil && vals.count > 0) ? vals[0].floatValue : 0;
  60. self.v1 = (vals != nil && vals.count > 1) ? vals[1].floatValue : 0;
  61. self.v2 = (vals != nil && vals.count > 2) ? vals[2].floatValue : 0;
  62. self.v3 = (vals != nil && vals.count > 3) ? vals[3].floatValue : 0;
  63. }
  64. -(NSArray<NSNumber*>*)get {
  65. return @[[NSNumber numberWithFloat:native[0]], [NSNumber numberWithFloat:native[1]], [NSNumber numberWithFloat:native[2]], [NSNumber numberWithFloat:native[3]]];
  66. }
  67. - (BOOL)isEqual:(id)other {
  68. if (other == self) {
  69. return YES;
  70. } else if (![other isKindOfClass:[Float4 class]]) {
  71. return NO;
  72. } else {
  73. Float4* point = (Float4*)other;
  74. return self.v0 == point.v0 && self.v1 == point.v1 && self.v2 == point.v2 && self.v3 == point.v3;
  75. }
  76. }
  77. @end