Home | History | Annotate | Download | only in linearmath
      1 #include "mathtypes.h"
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 ////////////////////////////////
      6 //////// btVector3      ////////
      7 ////////////////////////////////
      8 static jfieldID vector3_x = NULL, vector3_y = NULL, vector3_z = NULL;
      9 
     10 void vector3_getFields(JNIEnv * const &jenv, jobject &v3) {
     11 	jclass cls = jenv->GetObjectClass(v3);
     12 	vector3_x = jenv->GetFieldID(cls, "x", "F");
     13 	vector3_y = jenv->GetFieldID(cls, "y", "F");
     14 	vector3_z = jenv->GetFieldID(cls, "z", "F");
     15 	jenv->DeleteLocalRef(cls);
     16 }
     17 
     18 inline void vector3_ensurefields(JNIEnv * const &jenv, jobject &v3) {
     19 	if (!vector3_x) vector3_getFields(jenv, v3);
     20 }
     21 
     22 void Vector3_to_btVector3(JNIEnv * const &jenv, btVector3 &target, jobject &source)
     23 {
     24 	vector3_ensurefields(jenv, source);
     25 	target.setValue(jenv->GetFloatField(source, vector3_x), jenv->GetFloatField(source, vector3_y), jenv->GetFloatField(source, vector3_z));
     26 }
     27 
     28 void btVector3_to_Vector3(JNIEnv * const &jenv, jobject &target, const btVector3 &source)
     29 {
     30 	vector3_ensurefields(jenv, target);
     31 	jenv->SetFloatField(target, vector3_x, source.getX());
     32 	jenv->SetFloatField(target, vector3_y, source.getY());
     33 	jenv->SetFloatField(target, vector3_z, source.getZ());
     34 }
     35 
     36 
     37 
     38 ////////////////////////////////
     39 //////// btQuaternion   ////////
     40 ////////////////////////////////
     41 static jfieldID quaternion_x = NULL, quaternion_y = NULL, quaternion_z = NULL, quaternion_w = NULL;
     42 
     43 void quaternion_getFields(JNIEnv * const &jenv, jobject &q) {
     44 	jclass cls = jenv->GetObjectClass(q);
     45 	quaternion_x = jenv->GetFieldID(cls, "x", "F");
     46 	quaternion_y = jenv->GetFieldID(cls, "y", "F");
     47 	quaternion_z = jenv->GetFieldID(cls, "z", "F");
     48 	quaternion_w = jenv->GetFieldID(cls, "w", "F");
     49 	jenv->DeleteLocalRef(cls);
     50 }
     51 
     52 inline void quaternion_ensurefields(JNIEnv * const &jenv, jobject &q) {
     53 	if (!quaternion_x) quaternion_getFields(jenv, q);
     54 }
     55 
     56 void Quaternion_to_btQuaternion(JNIEnv * const &jenv, btQuaternion &target, jobject &source)
     57 {
     58 	quaternion_ensurefields(jenv, source);
     59 	target.setValue(
     60 			jenv->GetFloatField(source, quaternion_x),
     61 			jenv->GetFloatField(source, quaternion_y),
     62 			jenv->GetFloatField(source, quaternion_z),
     63 			jenv->GetFloatField(source, quaternion_w));
     64 }
     65 
     66 void btQuaternion_to_Quaternion(JNIEnv * const &jenv, jobject &target, const btQuaternion & source)
     67 {
     68 	quaternion_ensurefields(jenv, target);
     69 	jenv->SetFloatField(target, quaternion_x, source.getX());
     70 	jenv->SetFloatField(target, quaternion_y, source.getY());
     71 	jenv->SetFloatField(target, quaternion_z, source.getZ());
     72 	jenv->SetFloatField(target, quaternion_w, source.getW());
     73 }
     74 
     75 ////////////////////////////////
     76 //////// btMatrix3x3    ////////
     77 ////////////////////////////////
     78 static jfieldID matrix3_val = NULL;
     79 
     80 void matrix3_getFields(JNIEnv * const &jenv, jobject &m3) {
     81 	jclass cls = jenv->GetObjectClass(m3);
     82 	matrix3_val = jenv->GetFieldID(cls, "val", "[F");
     83 	jenv->DeleteLocalRef(cls);
     84 }
     85 
     86 inline void matrix3_ensurefields(JNIEnv * const &jenv, jobject &m3) {
     87 	if (!matrix3_val) matrix3_getFields(jenv, m3);
     88 }
     89 
     90 void Matrix3_to_btMatrix3(JNIEnv * const &jenv, btMatrix3x3 &target, jobject &source)
     91 {
     92 	matrix3_ensurefields(jenv, source);
     93 
     94 	jfloatArray valArray = (jfloatArray) jenv->GetObjectField(source, matrix3_val);
     95 	jfloat * elements = jenv->GetFloatArrayElements(valArray, NULL);
     96 
     97 	// Convert to column-major
     98 	target.setValue(
     99 	elements[0], elements[3], elements[6],
    100 	elements[1], elements[4], elements[7],
    101 	elements[2], elements[5], elements[8]);
    102 
    103 	jenv->ReleaseFloatArrayElements(valArray, elements, JNI_ABORT);
    104 	jenv->DeleteLocalRef(valArray);
    105 }
    106 
    107 void btMatrix3_to_Matrix3(JNIEnv * const &jenv, jobject &target, const btMatrix3x3 &source)
    108 {
    109 	matrix3_ensurefields(jenv, target);
    110 
    111 	jfloatArray valArray = (jfloatArray) jenv->GetObjectField(target, matrix3_val);
    112 	jfloat * elements = jenv->GetFloatArrayElements(valArray, NULL);
    113 
    114 	// Convert to column-major
    115 	elements[0] = (jfloat) source.getColumn(0).getX();
    116 	elements[1] = (jfloat) source.getColumn(0).getY();
    117 	elements[2] = (jfloat) source.getColumn(0).getZ();
    118 	elements[3] = (jfloat) source.getColumn(1).getX();
    119 	elements[4] = (jfloat) source.getColumn(1).getY();
    120 	elements[5] = (jfloat) source.getColumn(1).getZ();
    121 	elements[6] = (jfloat) source.getColumn(2).getX();
    122 	elements[7] = (jfloat) source.getColumn(2).getY();
    123 	elements[8] = (jfloat) source.getColumn(2).getZ();
    124 
    125 	jenv->ReleaseFloatArrayElements(valArray, elements, 0);
    126 	jenv->DeleteLocalRef(valArray);
    127 }
    128 
    129 ////////////////////////////////
    130 //////// btTransform    ////////
    131 ////////////////////////////////
    132 static jfieldID matrix4_val = NULL;
    133 
    134 void matrix4_getFields(JNIEnv * const &jenv, jobject &m4) {
    135 	jclass cls = jenv->GetObjectClass(m4);
    136 	matrix4_val = jenv->GetFieldID(cls, "val", "[F");
    137 	jenv->DeleteLocalRef(cls);
    138 }
    139 
    140 inline void matrix4_ensurefields(JNIEnv * const &jenv, jobject &m4) {
    141 	if (!matrix4_val) matrix4_getFields(jenv, m4);
    142 }
    143 
    144 void Matrix4_to_btTransform(JNIEnv * const &jenv, btTransform &target, jobject &source)
    145 {
    146 	matrix4_ensurefields(jenv, source);
    147 
    148 	jfloatArray valArray = (jfloatArray) jenv->GetObjectField(source, matrix4_val);
    149 	jfloat * elements = jenv->GetFloatArrayElements(valArray, NULL);
    150 
    151 	target.setFromOpenGLMatrix(elements);
    152 
    153 	jenv->ReleaseFloatArrayElements(valArray, elements, JNI_ABORT);
    154 	jenv->DeleteLocalRef(valArray);
    155 }
    156 
    157 void btTransform_to_Matrix4(JNIEnv * const &jenv, jobject &target, const btTransform &source)
    158 {
    159 	matrix4_ensurefields(jenv, target);
    160 
    161 	jfloatArray valArray = (jfloatArray) jenv->GetObjectField(target, matrix4_val);
    162 	jfloat * elements = jenv->GetFloatArrayElements(valArray, NULL);
    163 
    164     ATTRIBUTE_ALIGNED16(btScalar dst[16]);
    165 	source.getOpenGLMatrix(dst);
    166 
    167     memcpy(elements, dst, sizeof(btScalar)*16);
    168 
    169 	jenv->ReleaseFloatArrayElements(valArray, elements, 0);
    170 	jenv->DeleteLocalRef(valArray);
    171 }