CelestiaVector.mm
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:7k
源码类别:

OpenGL

开发平台:

Visual C++

  1. //
  2. //  CelestiaMath.mm
  3. //  celestia
  4. //
  5. //  Created by Bob Ippolito on Sat Jun 08 2002.
  6. //  Copyright (c) 2002 Chris Laurel. All rights reserved.
  7. //
  8. #import "CelestiaVector.h"
  9. #import "CelestiaVector_PrivateAPI.h"
  10. @implementation CelestiaVector(PrivateAPI)
  11. +(CelestiaVector*)vectorWithVec2f:(Vec2f)v
  12. {
  13.     return [CelestiaVector vectorWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y]];
  14. }
  15. +(CelestiaVector*)vectorWithPoint2f:(Point2f)v
  16. {
  17.     return [CelestiaVector vectorWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y]];
  18. }
  19. +(CelestiaVector*)vectorWithVec3f:(Vec3f)v
  20. {
  21.     return [CelestiaVector vectorWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z]];
  22. }
  23. +(CelestiaVector*)vectorWithPoint3f:(Point3f)v
  24. {
  25.     return [CelestiaVector vectorWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z]];
  26. }
  27. +(CelestiaVector*)vectorWithVec4f:(Vec4f)v
  28. {
  29.     return [CelestiaVector vectorWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z] w:[NSNumber numberWithFloat:v.w]];
  30. }
  31. +(CelestiaVector*)vectorWithVec3d:(Vec3d)v
  32. {
  33.     return [CelestiaVector vectorWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z]];
  34. }
  35. +(CelestiaVector*)vectorWithPoint3d:(Point3d)v
  36. {
  37.     return [CelestiaVector vectorWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z]];
  38. }
  39. +(CelestiaVector*)vectorWithVec4d:(Vec4d)v
  40. {
  41.     return [CelestiaVector vectorWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z] w:[NSNumber numberWithDouble:v.w]];
  42. }
  43. +(CelestiaVector*)vectorWithQuatd:(Quatd)v
  44. {
  45.     return [CelestiaVector vectorWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z] w:[NSNumber numberWithDouble:v.w]];
  46. }
  47. +(CelestiaVector*)vectorWithQuatf:(Quatf)v
  48. {
  49.     return [CelestiaVector vectorWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z] w:[NSNumber numberWithFloat:v.w]];
  50. }
  51. -(CelestiaVector*)initWithVec2f:(Vec2f)v
  52. {
  53.     return [self initWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y]];
  54. }
  55. -(CelestiaVector*)initWithVec3f:(Vec3f)v
  56. {
  57.     return [self initWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z]];
  58. }
  59. -(CelestiaVector*)initWithVec4f:(Vec4f)v
  60. {
  61.     return [self initWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z] w:[NSNumber numberWithFloat:v.w]];
  62. }
  63. -(CelestiaVector*)initWithVec3d:(Vec3d)v
  64. {
  65.     return [self initWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z]];
  66. }
  67. -(CelestiaVector*)initWithVec4d:(Vec4d)v
  68. {
  69.     return [self initWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z] w:[NSNumber numberWithDouble:v.w]];
  70. }
  71. -(CelestiaVector*)initWithQuatd:(Quatd)v
  72. {
  73.     return [self initWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z] w:[NSNumber numberWithDouble:v.w]];
  74. }
  75. -(CelestiaVector*)initWithQuatf:(Quatf)v
  76. {
  77.     return [self initWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z] w:[NSNumber numberWithFloat:v.w]];
  78. }
  79. -(Vec2f)vec2f
  80. {
  81.     return Vec2f([[self x] floatValue],[[self y] floatValue]);
  82. }
  83. -(Vec3f)vec3f
  84. {
  85.     return Vec3f([[self x] floatValue],[[self y] floatValue],[[self z] floatValue]);
  86. }
  87. -(Point2f)point2f
  88. {
  89.     return Point2f([[self x] floatValue],[[self y] floatValue]);
  90. }
  91. -(Point3f)point3f
  92. {
  93.     return Point3f([[self x] floatValue],[[self y] floatValue],[[self z] floatValue]);
  94. }
  95. -(Vec4f)vec4f
  96. {
  97.     return Vec4f([[self x] floatValue],[[self y] floatValue],[[self z] floatValue], [[self w] floatValue]);
  98. }
  99. -(Vec3d)vec3d
  100. {
  101.     return Vec3d([[self x] doubleValue],[[self y] doubleValue],[[self z] doubleValue]);
  102. }
  103. -(Point3d)point3d
  104. {
  105.     return Point3d([[self x] doubleValue],[[self y] doubleValue],[[self z] doubleValue]);
  106. }
  107. -(Vec4d)vec4d
  108. {
  109.     return Vec4d([[self x] doubleValue],[[self y] doubleValue],[[self z] doubleValue], [[self w] doubleValue]);
  110. }
  111. -(Quatf)quatf
  112. {
  113.     return Quatf([[self w] floatValue],[[self x] floatValue],[[self y] floatValue],[[self z] floatValue]);
  114. }
  115. -(Quatd)quatd
  116. {
  117.     return Quatd([[self w] doubleValue],[[self x] doubleValue],[[self y] doubleValue],[[self z] doubleValue]);
  118. }
  119. -(CelestiaVector*)initWithPoint2f:(Point2f)v
  120. {
  121.     return [self initWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y]];
  122. }
  123. -(CelestiaVector*)initWithPoint3d:(Point3d)v
  124. {
  125.     return [self initWithx:[NSNumber numberWithDouble:v.x] y:[NSNumber numberWithDouble:v.y] z:[NSNumber numberWithDouble:v.z]];
  126. }
  127. -(CelestiaVector*)initWithPoint3f:(Point3f)v
  128. {
  129.     return [self initWithx:[NSNumber numberWithFloat:v.x] y:[NSNumber numberWithFloat:v.y] z:[NSNumber numberWithFloat:v.z]];
  130. }
  131. @end
  132. @implementation CelestiaVector
  133. -(void)encodeWithCoder:(NSCoder*)coder
  134. {
  135.     //[super encodeWithCoder:coder];
  136.     [coder encodeObject:_array];
  137. }
  138. -(id)initWithCoder:(NSCoder*)coder
  139. {
  140.     //self = [super initWithCoder:coder];
  141.     self = [self init];
  142.     _array = [[coder decodeObject] retain];
  143.     return self;
  144. }
  145. -(void)dealloc
  146. {
  147.     if (_array != nil) {
  148.         [_array release];
  149.         _array = nil;
  150.     }
  151.     [super dealloc];
  152. }
  153. +(CelestiaVector*)vectorWithArray:(NSArray*)v
  154. {
  155.     return [[[CelestiaVector alloc] initWithArray:v] autorelease];
  156. }
  157. +(CelestiaVector*)vectorWithx:(NSNumber*)x y:(NSNumber*)y
  158. {
  159.     return [[[CelestiaVector alloc] initWithx:x y:y] autorelease];
  160. }
  161. +(CelestiaVector*)vectorWithx:(NSNumber*)x y:(NSNumber*)y z:(NSNumber*)z
  162. {
  163.     return [[[CelestiaVector alloc] initWithx:x y:y z:z] autorelease];
  164. }
  165. +(CelestiaVector*)vectorWithx:(NSNumber*)x y:(NSNumber*)y z:(NSNumber*)z w:(NSNumber*)w
  166. {
  167.     return [[[CelestiaVector alloc] initWithx:x y:y z:z w:w] autorelease];
  168. }
  169. -(CelestiaVector*)initWithArray:(NSArray*)v
  170. {
  171.     self = [super init];
  172.     _array = [v retain];
  173.     return self;
  174. }
  175. -(CelestiaVector*)initWithx:(NSNumber*)x y:(NSNumber*)y
  176. {
  177.     self = [super init];
  178.     _array = [[NSArray alloc] initWithObjects:x,y,nil];
  179.     return self;
  180. }
  181. -(CelestiaVector*)initWithx:(NSNumber*)x y:(NSNumber*)y z:(NSNumber*)z
  182. {
  183.     self = [super init];
  184.     _array = [[NSArray alloc] initWithObjects:x,y,z,nil];
  185.     return self;
  186. }
  187. -(CelestiaVector*)initWithx:(NSNumber*)x y:(NSNumber*)y z:(NSNumber*)z w:(NSNumber*)w
  188. {
  189.     self = [super init];
  190.     _array = [[NSArray alloc] initWithObjects:x,y,z,w,nil];
  191.     return self;
  192. }
  193. -(NSNumber*)x
  194. {
  195.     return [_array objectAtIndex:0];
  196. }
  197. -(NSNumber*)y
  198. {
  199.     return [_array objectAtIndex:1];
  200. }
  201. -(NSNumber*)z
  202. {
  203.     return [_array objectAtIndex:2];
  204. }
  205. -(NSNumber*)w
  206. {
  207.     return [_array objectAtIndex:3];
  208. }
  209. -objectAtIndex:(unsigned)index
  210. {
  211.     return [_array objectAtIndex:index];
  212. }
  213. -(unsigned)count
  214. {
  215.     return [_array count];
  216. }
  217. @end