JNIEXPORT jobject JNICALL Java_ch_heigvd_jpal_JPal_labeller (JNIEnv *env, jobject obj, jobjectArray layers, jdoubleArray layersFactor, jdouble scale, jdoubleArray bbox, jobjectArray jstats, jboolean displayAll) { std::cout << "Env (labeller): " << env << std::endl; std::cout << "obj (labeller): " << obj << std::endl; int i; const char *str; char **layersN; double *layersF; int nbLayer = env->GetArrayLength (layers); double bounding_box[4]; jclass cls = env->GetObjectClass (obj); jfieldID fid = env->GetFieldID (cls, "pal_adr", "J"); Pal *pal = (Pal*) env->GetLongField (obj, fid); layersN = new char*[nbLayer]; layersF = new double[nbLayer]; //str = env->GetStringUTFChars(name, NULL); //env->ReleaseStringUTFChars(name, str); env->GetDoubleArrayRegion (bbox, 0, 4, bounding_box); env->GetDoubleArrayRegion (layersFactor, 0, nbLayer, layersF); for (i = 0;i < nbLayer;i++) { jstring jstr = (jstring) env->GetObjectArrayElement (layers, i); str = env->GetStringUTFChars (jstr, NULL); layersN[i] = new char[strlen (str) +1]; strcpy (layersN[i], str); std::cout << "JNI_WRAP: Layers " << i << " : " << layersN[i] << " (" << layersF[i] << ")" << std::endl; env->ReleaseStringUTFChars (jstr, str); env->DeleteLocalRef (jstr); } std::cout << "JNI_WRAP: Labeller is going to run:" << std::endl; PalStat * stats; std::list<Label*> *solList = pal->labeller (nbLayer, layersN, layersF, scale, bounding_box, &stats, displayAll); std::cout << "JNI_WRAP: Labelling done... translate stats..." << std::endl; int nbLayers = stats->getNbLayers(); jclass stringClass = env->FindClass ("java/lang/String"); jobjectArray lyrsNames = env->NewObjectArray (nbLayers, stringClass, NULL); jintArray nbLyrObjs = env->NewIntArray (nbLayers); jintArray nbLyrLObjs = env->NewIntArray (nbLayers); jint *lnbo = new jint[nbLayers]; jint *lnblo = new jint[nbLayers]; int k; for (k = 0;k < nbLayers;k++) { env->SetObjectArrayElement (lyrsNames, k, env->NewStringUTF (stats->getLayerName (k))); lnbo[k] = stats->getLayerNbObjects (k); lnblo[k] = stats->getLayerNbLabelledObjects (k); } env->SetIntArrayRegion (nbLyrObjs, 0, nbLayers, lnbo); env->SetIntArrayRegion (nbLyrLObjs, 0, nbLayers, lnblo); delete[] lnbo; delete[] lnblo; jclass jpalStatClass = env->FindClass ("ch/heigvd/jpal/JPalStat"); jmethodID jpalStatSet = env->GetMethodID (jpalStatClass, "set", "(II[Ljava/lang/String;[I[I)V"); env->CallVoidMethod (jstats, jpalStatSet, stats->getNbObjects(), stats->getNbLabelledObjects(), lyrsNames, nbLyrObjs, nbLyrLObjs); //env->NewObject(jpalStatClass, jpalStatInit, env->DeleteLocalRef (lyrsNames); env->DeleteLocalRef (nbLyrObjs); env->DeleteLocalRef (nbLyrLObjs); delete stats; std::cout << "JNI_WRAP: Labelling is finished" << std::endl; jclass listClass; jclass labelClass; jmethodID listInit; jmethodID listAdd; jmethodID labelInit; listClass = env->FindClass ("java/util/LinkedList"); labelClass = env->FindClass ("ch/heigvd/jpal/JPalLabel"); listInit = env->GetMethodID (listClass, "<init>", "()V"); listAdd = env->GetMethodID (listClass, "add", "(Ljava/lang/Object;)Z"); labelInit = env->GetMethodID (labelClass, "<init>", "(DDDLjava/lang/String;Ljava/lang/String;)V"); jstring layerName; jstring featID; jobject jlist = env->NewObject (listClass, listInit); //jobject jlabel; Label *label; for (std::list<Label*>::iterator it = solList->begin(); it != solList->end();it++) { label = *it; layerName = env->NewStringUTF (label->getLayerName()); featID = env->NewStringUTF (label->getFeatureId()); env->CallBooleanMethod (jlist, listAdd, env->NewObject (labelClass, labelInit, label->getOrigX(), label->getOrigY(), label->getRotation(), layerName, featID)); //std::cout << "Identifier: " << label->getLayerName() << "/" << label->getFeatureId() << std::endl; // std::cout << "coord:" << label->getOrigX() << " " << label->getOrigY() << std::endl; // std::cout << "alpha: " << label->getRotation() << std::endl << std::endl; delete label; } env->DeleteLocalRef (listClass); env->DeleteLocalRef (labelClass); return jlist; }
JNIEXPORT jobject JNICALL Java_ch_heigvd_jpal_JPal_labeller_1all (JNIEnv *env, jobject obj, jdouble scale, jdoubleArray bbox, jobjectArray jstats, jboolean displayAll) { std::cout << "JNI_WRAP: Labeller_all" << std::endl; double bounding_box[4]; jclass cls = env->GetObjectClass (obj); jfieldID fid = env->GetFieldID (cls, "pal_adr", "J"); Pal *pal = (Pal*) env->GetLongField (obj, fid); env->GetDoubleArrayRegion (bbox, 0, 4, bounding_box); std::cout << "JNI_WRAP: Labeller is going to run:" << std::endl; PalStat *stats; std::cout << "JNI_WRAP displayAll:" << displayAll << std::endl; std::list<Label*> *solList = pal->labeller (scale, bounding_box, &stats, displayAll); std::cout << "JNI_WRAP: Labelling done... translate stats..." << std::endl; int nbLayers; nbLayers = stats->getNbLayers(); jclass stringClass = env->FindClass ("java/lang/String"); jobjectArray lyrsNames = env->NewObjectArray (nbLayers, stringClass, NULL); jintArray nbLyrObjs = env->NewIntArray (nbLayers); jintArray nbLyrLObjs = env->NewIntArray (nbLayers); jint *lnbo = new jint[nbLayers]; jint *lnblo = new jint[nbLayers]; int k; for (k = 0;k < nbLayers;k++) { env->SetObjectArrayElement (lyrsNames, k, env->NewStringUTF (stats->getLayerName (k))); lnbo[k] = stats->getLayerNbObjects (k); lnblo[k] = stats->getLayerNbLabelledObjects (k); } env->SetIntArrayRegion (nbLyrObjs, 0, nbLayers, lnbo); env->SetIntArrayRegion (nbLyrLObjs, 0, nbLayers, lnblo); delete[] lnbo; delete[] lnblo; jclass jpalStatClass = env->FindClass ("ch/heigvd/jpal/JPalStat"); jmethodID jpalStatSet = env->GetMethodID (jpalStatClass, "set", "(II[Ljava/lang/String;[I[I)V"); env->CallVoidMethod (jstats, jpalStatSet, stats->getNbObjects(), stats->getNbLabelledObjects(), lyrsNames, nbLyrObjs, nbLyrLObjs); //env->NewObject(jpalStatClass, jpalStatInit, env->DeleteLocalRef (lyrsNames); env->DeleteLocalRef (nbLyrObjs); env->DeleteLocalRef (nbLyrLObjs); delete stats; std::cout << "JNI_WRAP: Labelling is finished" << std::endl; jclass listClass; jclass labelClass; jmethodID listInit; jmethodID listAdd; jmethodID labelInit; listClass = env->FindClass ("java/util/LinkedList"); labelClass = env->FindClass ("ch/heigvd/jpal/JPalLabel"); listInit = env->GetMethodID (listClass, "<init>", "()V"); listAdd = env->GetMethodID (listClass, "add", "(Ljava/lang/Object;)Z"); labelInit = env->GetMethodID (labelClass, "<init>", "(DDDLjava/lang/String;Ljava/lang/String;)V"); jstring layerName; jstring featID; jobject jlist = env->NewObject (listClass, listInit); //jobject jlabel; Label *label; for (std::list<Label*>::iterator it = solList->begin(); it != solList->end();it++) { label = *it; layerName = env->NewStringUTF (label->getLayerName()); featID = env->NewStringUTF (label->getFeatureId()); env->CallBooleanMethod (jlist, listAdd, env->NewObject (labelClass, labelInit, label->getOrigX(), label->getOrigY(), label->getRotation(), layerName, featID)); delete label; } env->DeleteLocalRef (listClass); env->DeleteLocalRef (labelClass); return jlist; }