29#import <GNUstepBase/GSObjCRuntime.h>
31#import <objc/objc-runtime.h>
45static JSBool
VectorGetProperty(JSContext *context, JSObject *
this, jsid propID, jsval *value);
46static JSBool
VectorSetProperty(JSContext *context, JSObject *
this, jsid propID, JSBool strict, jsval *value);
48static JSBool
VectorConstruct(JSContext *context, uintN argc, jsval *vp);
51static JSBool
VectorToString(JSContext *context, uintN argc, jsval *vp);
52static JSBool
VectorToSource(JSContext *context, uintN argc, jsval *vp);
53static JSBool
VectorAdd(JSContext *context, uintN argc, jsval *vp);
54static JSBool
VectorSubtract(JSContext *context, uintN argc, jsval *vp);
57static JSBool
VectorMultiply(JSContext *context, uintN argc, jsval *vp);
58static JSBool
VectorDot(JSContext *context, uintN argc, jsval *vp);
59static JSBool
VectorAngleTo(JSContext *context, uintN argc, jsval *vp);
62static JSBool
VectorCross(JSContext *context, uintN argc, jsval *vp);
64static JSBool
VectorDirection(JSContext *context, uintN argc, jsval *vp);
65static JSBool
VectorMagnitude(JSContext *context, uintN argc, jsval *vp);
68static JSBool
VectorRotateBy(JSContext *context, uintN argc, jsval *vp);
69static JSBool
VectorToArray(JSContext *context, uintN argc, jsval *vp);
91 JSCLASS_NO_OPTIONAL_MEMBERS
163 JSObject *result = NULL;
164 HPVector *
private = NULL;
166 private = malloc(
sizeof *
private);
169 *
private = vectorToHPVector(vector);
174 if (
EXPECT_NOT(!JS_SetPrivate(context, result,
private))) result = NULL;
177 if (
EXPECT_NOT(result == NULL)) free(
private);
189 JSObject *
object = NULL;
191 assert(outValue != NULL);
196 *outValue = OBJECT_TO_JSVAL(
object);
206 JSObject *result = NULL;
207 HPVector *
private = NULL;
209 private = malloc(
sizeof *
private);
217 if (
EXPECT_NOT(!JS_SetPrivate(context, result,
private))) result = NULL;
220 if (
EXPECT_NOT(result == NULL)) free(
private);
232 JSObject *
object = NULL;
234 assert(outValue != NULL);
239 *outValue = OBJECT_TO_JSVAL(
object);
248 return VectorToJSValue(context, make_vector(point.x, point.y, 0), outValue);
254 if (
EXPECT_NOT(!JSVAL_IS_OBJECT(value)))
return NO;
261 if (
EXPECT_NOT(!JSVAL_IS_OBJECT(value)))
return NO;
264 *outVector = HPVectorToVector(tmp);
273 NSUInteger vectorCount;
274 NSUInteger entityCount;
275 NSUInteger arrayCount;
276 NSUInteger protoCount;
277 NSUInteger nullCount;
278 NSUInteger failCount;
280static VectorStatistics sVectorConversionStats;
283@implementation PlayerEntity (JSVectorStatistics)
288- (NSString *) reportJSVectorStatistics
290 VectorStatistics *stats = &sVectorConversionStats;
292 NSUInteger sum = stats->vectorCount + stats->entityCount + stats->arrayCount + stats->protoCount;
293 double convFac = 100.0 / sum;
294 if (sum == 0) convFac = 0;
296 return [NSString stringWithFormat:
297 @" vector-to-vector conversions: %lu (%g %%)\n"
298 " entity-to-vector conversions: %lu (%g %%)\n"
299 " array-to-vector conversions: %lu (%g %%)\n"
300 "prototype-to-zero conversions: %lu (%g %%)\n"
301 " null conversions: %lu (%g %%)\n"
302 " failed conversions: %lu (%g %%)\n"
304 (long)stats->vectorCount, stats->vectorCount * convFac,
305 (long)stats->entityCount, stats->entityCount * convFac,
306 (long)stats->arrayCount, stats->arrayCount * convFac,
307 (long)stats->protoCount, stats->protoCount * convFac,
308 (long)stats->nullCount, stats->nullCount * convFac,
309 (long)stats->failCount, stats->failCount * convFac,
314- (void) clearJSVectorStatistics
316 memset(&sVectorConversionStats, 0,
sizeof sVectorConversionStats);
321#define COUNT(FIELD) do { sVectorConversionStats.FIELD++; } while (0)
325#define COUNT(FIELD) do {} while (0)
334 assert(outVector != NULL);
336 HPVector *
private = NULL;
338 jsval arrayX, arrayY, arrayZ;
349 private = JS_GetInstancePrivate(context, vectorObj, &
sVectorClass, NULL);
350 if (
EXPECT(
private != NULL))
353 *outVector = *
private;
358 if (
EXPECT(JS_IsArrayObject(context, vectorObj)))
361 if (JS_GetArrayLength(context, vectorObj, &arrayLength) && arrayLength == 3)
363 if (JS_LookupElement(context, vectorObj, 0, &arrayX) &&
364 JS_LookupElement(context, vectorObj, 1, &arrayY) &&
365 JS_LookupElement(context, vectorObj, 2, &arrayZ))
368 if (JS_ValueToNumber(context, arrayX, &
x) &&
369 JS_ValueToNumber(context, arrayY, &
y) &&
370 JS_ValueToNumber(context, arrayZ, &z))
373 *outVector = make_HPvector(
x,
y, z);
384 Entity *entity = [(id)JS_GetPrivate(context, vectorObj) weakRefUnderlyingObject];
396 if (JS_InstanceOf(context, vectorObj, &
sVectorClass, NULL))
410static BOOL
GetThisVector(JSContext *context, JSObject *vectorObj, HPVector *outVector, NSString *method)
414 jsval arg = OBJECT_TO_JSVAL(vectorObj);
430 HPVector *
private = NULL;
434 private = JS_GetInstancePrivate(context, vectorObj, &
sVectorClass, NULL);
441 if (JS_InstanceOf(context, vectorObj, &
sVectorClass, NULL))
460 assert(argv != NULL && outVector != NULL);
462 if (outConsumed != NULL) *outConsumed = 0;
465 if (JSVAL_IS_OBJECT(argv[0]))
469 if (outConsumed != NULL) *outConsumed = 1;
474 if (!permitNumberList)
return NO;
477 if (argc < 3)
return NO;
480 if (
EXPECT_NOT(!JS_ValueToNumber(context, argv[0], &
x) || isnan(
x)))
return NO;
481 if (
EXPECT_NOT(!JS_ValueToNumber(context, argv[1], &
y) || isnan(
y)))
return NO;
482 if (
EXPECT_NOT(!JS_ValueToNumber(context, argv[2], &z) || isnan(z)))
return NO;
485 *outVector = make_HPvector(
x,
y, z);
486 if (outConsumed != NULL) *outConsumed = 3;
495BOOL
VectorFromArgumentList(JSContext *context, NSString *scriptClass, NSString *
function, uintN argc, jsval *argv, HPVector *outVector, uintN *outConsumed)
501 @"Could not construct vector from parameters",
502 @"Vector, Entity or array of three numbers");
518 if (!JSID_IS_INT(propID))
return YES;
527 switch (JSID_TO_INT(propID))
546 return JS_NewNumberValue(context, fValue, value);
552static JSBool
VectorSetProperty(JSContext *context, JSObject *
this, jsid propID, JSBool strict, jsval *value)
554 if (!JSID_IS_INT(propID))
return YES;
562 if (
EXPECT_NOT(!JS_ValueToNumber(context, *value, &dval)))
568 switch (JSID_TO_INT(propID))
597 Vector *
private = NULL;
599 private = JS_GetInstancePrivate(context,
this, &
sVectorClass, NULL);
614 HPVector *
private = NULL;
615 JSObject *
this = NULL;
617 private = malloc(
sizeof *
private);
620 this = JS_NewObject(context, &
sVectorClass, NULL, NULL);
629 @"Could not construct vector from parameters",
630 @"Vector, Entity or array of three numbers");
637 if (
EXPECT_NOT(!JS_SetPrivate(context,
this,
private)))
675 NSString *str = [NSString stringWithFormat:@"Vector3D(%g, %g, %g)", thisv.x, thisv.y, thisv.z];
683static JSBool
VectorAdd(JSContext *context, uintN argc, jsval *vp)
687 HPVector thisv, thatv, result;
692 result = HPvector_add(thisv, thatv);
705 HPVector thisv, thatv, result;
710 result = HPvector_subtract(thisv, thatv);
723 HPVector thisv, thatv;
729 result = HPdistance(thisv, thatv);
742 HPVector thisv, thatv;
748 result = HPdistance2(thisv, thatv);
761 HPVector thisv, result;
767 result = HPvector_multiply_scalar(thisv, scalar);
776static JSBool
VectorDot(JSContext *context, uintN argc, jsval *vp)
780 HPVector thisv, thatv;
786 result = HPdot_product(thisv, thatv);
799 HPVector thisv, thatv;
805 result = HPdot_product(HPvector_normal(thisv), HPvector_normal(thatv));
806 if (result > 1.0) result = 1.0;
807 if (result < -1.0) result = -1.0;
810 result = acos(result);
819static JSBool
VectorCross(JSContext *context, uintN argc, jsval *vp)
823 HPVector thisv, thatv, result;
828 result = HPtrue_cross_product(thisv, thatv);
841 HPVector thisv, thatv, theotherv;
852 result = HPtriple_product(thisv, thatv, theotherv);
865 HPVector thisv, result;
869 result = HPvector_normal(thisv);
887 result = HPmagnitude(thisv);
905 result = HPmagnitude2(thisv);
918 HPVector thisv, thatv;
951 HPVector thisv, result;
971 JSObject *result = NULL;
976 result = JS_NewArrayObject(context, 0, NULL);
982 if (JS_NewNumberValue(context, thisv.x, &nVal) && JS_SetElement(context, result, 0, &nVal) &&
983 JS_NewNumberValue(context, thisv.y, &nVal) && JS_SetElement(context, result, 1, &nVal) &&
984 JS_NewNumberValue(context, thisv.z, &nVal) && JS_SetElement(context, result, 2, &nVal))
1004 NSString *coordScheme =
nil;
1017 result = [UNIVERSE legacyPositionFrom:thisv asCoordinateSystem:coordScheme];
1032 NSString *coordScheme =
nil;
1045 NSString *arg = [NSString stringWithFormat:@"%@ %f %f %f", coordScheme, thisv.x, thisv.y, thisv.z];
1046 result = [UNIVERSE coordinatesFromCoordinateSystemString:arg];
1067 uintN inArgc = argc;
1069 jsval *inArgv = argv;
1071 if (
EXPECT_NOT(argc < 3))
goto INSUFFICIENT_ARGUMENTS;
1075 if (
EXPECT_NOT(argc < 2))
goto INSUFFICIENT_ARGUMENTS;
1079 if (
EXPECT_NOT(argc < 1))
goto INSUFFICIENT_ARGUMENTS;
1082 result = OOHPVectorInterpolate(av, bv, interp);
1086INSUFFICIENT_ARGUMENTS:
1088 @"Insufficient parameters",
1089 @"vector expression, vector expression and number");
HPVector OOHPVectorRandomRadial(OOHPScalar maxLength)
const HPVector kZeroHPVector
HPVector OORandomUnitHPVector(void)
HPVector OOHPVectorRandomSpatial(OOHPScalar maxLength)
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_EXIT_VOID
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_PROFILE_ENTER
OOINLINE JSClass * JSEntityClass(void)
BOOL QuaternionFromArgumentList(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, Quaternion *outQuaternion, uintN *outConsumed) GCC_ATTR((nonnull(1
BOOL BOOL JSVectorSetVector(JSContext *context, JSObject *vectorObj, Vector vector) GCC_ATTR((nonnull(1)))
BOOL BOOL VectorFromArgumentListNoError(JSContext *context, uintN argc, jsval *argv, HPVector *outVector, uintN *outConsumed) GCC_ATTR((nonnull(1
BOOL JSVectorSetHPVector(JSContext *context, JSObject *vectorObj, HPVector vector) GCC_ATTR((nonnull(1)))
BOOL JSObjectGetVector(JSContext *context, JSObject *vectorObj, HPVector *outVector) GCC_ATTR((nonnull(1
BOOL VectorFromArgumentList(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, HPVector *outVector, uintN *outConsumed) GCC_ATTR((nonnull(1
static JSBool VectorStaticRandomDirectionAndLength(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorFromCoordinateSystem(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorToArray(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorAdd(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorToString(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorCross(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorSubtract(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorStaticInterpolate(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorRotateBy(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorToCoordinateSystem(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorDistanceTo(JSContext *context, uintN argc, jsval *vp)
void InitOOJSVector(JSContext *context, JSObject *global)
static JSBool VectorMagnitude(JSContext *context, uintN argc, jsval *vp)
static JSPropertySpec sVectorProperties[]
static JSBool VectorDot(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorRotationTo(JSContext *context, uintN argc, jsval *vp)
static JSObject * sVectorPrototype
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector)
static JSFunctionSpec sVectorMethods[]
static JSBool VectorTripleProduct(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorDirection(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorAngleTo(JSContext *context, uintN argc, jsval *vp)
static JSFunctionSpec sVectorStaticMethods[]
BOOL JSValueToHPVector(JSContext *context, jsval value, HPVector *outVector)
BOOL JSObjectGetVector(JSContext *context, JSObject *vectorObj, HPVector *outVector)
static void VectorFinalize(JSContext *context, JSObject *this)
static JSBool VectorStaticRandom(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorToSource(JSContext *context, uintN argc, jsval *vp)
JSObject * JSVectorWithHPVector(JSContext *context, HPVector vector)
BOOL NSPointToVectorJSValue(JSContext *context, NSPoint point, jsval *outValue)
static BOOL VectorFromArgumentListNoErrorInternal(JSContext *context, uintN argc, jsval *argv, HPVector *outVector, uintN *outConsumed, BOOL permitNumberList)
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue)
JSObject * JSVectorWithVector(JSContext *context, Vector vector)
static JSBool VectorConstruct(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static BOOL GetThisVector(JSContext *context, JSObject *vectorObj, HPVector *outVector, NSString *method) NONNULL_FUNC
static JSBool VectorSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSBool VectorSquaredMagnitude(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorSquaredDistanceTo(JSContext *context, uintN argc, jsval *vp)
BOOL HPVectorToJSValue(JSContext *context, HPVector vector, jsval *outValue)
static JSClass sVectorClass
static JSBool VectorMultiply(JSContext *context, uintN argc, jsval *vp)
static JSBool VectorStaticRandomDirection(JSContext *context, uintN argc, jsval *vp)
BOOL OOJSArgumentListGetNumberNoError(JSContext *context, uintN argc, jsval *argv, double *outNumber, uintN *outConsumed)
BOOL OOJSArgumentListGetNumber(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, double *outNumber, uintN *outConsumed)
#define OOJS_PROP_READWRITE_CB
#define OOJS_RETURN_DOUBLE(value)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
OOINLINE BOOL OOJSIsMemberOfSubclass(JSContext *context, JSObject *object, JSClass *superclass)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
#define OOJS_RETURN_QUATERNION(value)
#define OOJS_RETURN_JSOBJECT(o)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_RETURN_HPVECTOR(value)
HPVector quaternion_rotate_HPvector(Quaternion q, HPVector v)
Quaternion quaternion_rotation_between(Vector v0, Vector v1)
Quaternion quaternion_limited_rotation_between(Vector v0, Vector v1, float maxArc)