Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Numerous updates #8

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e5f0934
Update for PCL 1.9.1 API
lukehutch Aug 11, 2019
c56012f
`this.` should be `this->`
lukehutch Aug 21, 2019
2b3995b
Optimization: cache Java field id lookup
lukehutch Aug 21, 2019
5a76e79
throw on error getting handle field
lukehutch Aug 21, 2019
77faa03
Call `alloc()` from `NativeObject` constructors
lukehutch Aug 21, 2019
fc0b155
Build updates
lukehutch Aug 21, 2019
b544caf
Update javadoc
lukehutch Aug 21, 2019
1de2c02
Replace `dispose()` method with `AutoCloseable#close()`
lukehutch Aug 21, 2019
e84d483
Update Javadoc
lukehutch Aug 21, 2019
77e963a
Allow close() to be overridden
lukehutch Aug 21, 2019
c4af160
Consistency fix
lukehutch Aug 21, 2019
d03ef80
Use try-with-resources to close NativeObject instances
lukehutch Aug 21, 2019
3413040
Use try-with-resources to close NativeObjects
lukehutch Aug 21, 2019
e8d1e14
Remove unused #import
lukehutch Aug 21, 2019
fbcfad9
Fix exception messages
lukehutch Aug 21, 2019
304ff56
Add Point3di class for pcl::PointXYZI
lukehutch Aug 21, 2019
d4d9227
Change .equals() tolerance to 1.0e-6f
lukehutch Aug 21, 2019
1d2bd66
Add PointCloud3di
lukehutch Aug 21, 2019
ae84d29
Fix equals() methods
lukehutch Aug 21, 2019
7c640f2
Fix modifiers on native methods
lukehutch Aug 21, 2019
314afac
Fix previous commit
lukehutch Aug 21, 2019
524fca6
Add default floating point comparison precision
lukehutch Aug 21, 2019
cf19a38
Add sptr getter/setter
lukehutch Aug 21, 2019
b1a72f2
Create separate float-precision Matrix class
lukehutch Aug 21, 2019
e6c1245
Fix includes
lukehutch Aug 21, 2019
c73808e
ICP and GICP registration bindings (untested)
lukehutch Aug 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Eclipse
.classpath
.project
.cproject
.settings/

# Intellij
Expand All @@ -19,4 +20,5 @@ build/
CMakeFiles/
CMakeCache.txt
*.cmake
Makefile
Makefile
Debug/
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ else()
message(FATAL_ERROR "JNI not found.")
endif()

find_package(PCL 1.8.1 REQUIRED)
find_package(PCL 1.9.1 REQUIRED)

if (PCL_FOUND)
message(STATUS "PCL found.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,114 +8,87 @@
import com.movlad.pcl.visualization.Visualizer3d;

public class SphereDetection {

static {
// loading the native library in the main class

System.loadLibrary("pcl_java");
}

public static void main(String[] args) {
PointCloud3d noisySphere = createNoisySphere();
SampleConsensusModelSphere sphereModel = new SampleConsensusModelSphere(noisySphere);

/*
* In order to use a native object it needs to be created in the memory on the native side;
* the create() method does exactly that.
*/

sphereModel.create();

RandomSampleConsensus ransac = new RandomSampleConsensus(sphereModel);

ransac.create();
ransac.setDistanceThreshold(.01f);
ransac.computeModel(0);

PointCloud3d detectedSphere = ransac.getInliners(noisySphere);

for (Point3d point : detectedSphere) {
point.setRGB((short) 255, (short) 0, (short) 0);
}

Visualizer<Point3d> visualizer = new Visualizer3d();

visualizer.create();
visualizer.setWindowName("PCL Java Sphere Detection Example");
visualizer.setBackgroundColor(0.f, 0.f, 0.f);
visualizer.addCoordinateSystem(0.2, 0);
visualizer.addPointCloud(noisySphere, "noisy_sphere", 0);
visualizer.addPointCloud(detectedSphere, "detected_sphere", 0);
visualizer.setPointSize(2, "noisy_sphere");
visualizer.setPointSize(3, "detected_sphere");

while (!visualizer.wasStopped()) {
visualizer.spinOnce(100, false);

try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
try (PointCloud3d noisySphere = createNoisySphere();
SampleConsensusModelSphere sphereModel = new SampleConsensusModelSphere(noisySphere);
RandomSampleConsensus ransac = new RandomSampleConsensus(sphereModel)) {
ransac.setDistanceThreshold(.01f);
ransac.computeModel(0);

try (PointCloud3d detectedSphere = ransac.getInliners(noisySphere)) {
for (Point3d point : detectedSphere) {
point.setRGB((short) 255, (short) 0, (short) 0);
}

try (Visualizer<Point3d> visualizer = new Visualizer3d()) {
visualizer.setWindowName("PCL Java Sphere Detection Example");
visualizer.setBackgroundColor(0.f, 0.f, 0.f);
visualizer.addCoordinateSystem(0.2, 0);
visualizer.addPointCloud(noisySphere, "noisy_sphere", 0);
visualizer.addPointCloud(detectedSphere, "detected_sphere", 0);
visualizer.setPointSize(2, "noisy_sphere");
visualizer.setPointSize(3, "detected_sphere");

while (!visualizer.wasStopped()) {
visualizer.spinOnce(100, false);

try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

/*
* All natively allocated objects must be freed from the memory using the dispose() method.
*/

visualizer.dispose();
detectedSphere.dispose();
ransac.dispose();
sphereModel.dispose();
noisySphere.dispose();
}

/**
* @return a point cloud in the shape of a sphere with some noisy points
* here and there
* @return a point cloud in the shape of a sphere with some noisy points here
* and there
*/
private static PointCloud3d createNoisySphere() {
PointCloud3d noisySphere = new PointCloud3d();

noisySphere.create();


for (int i = 0; i < 500; i++) {
Point3d point = new Point3d();

point.create();
point.setX(randomFloat(-1f, 1f));
point.setY(randomFloat(-1f, 1f));

if (i % 5 == 0) {
point.setZ(randomFloat(-1f, 1f));
} else if (i % 2 == 0) {
point.setZ((float)Math.sqrt(1 - (point.getX() * point.getX()) - (point.getY() * point.getY())));
} else {
point.setZ((float) -Math.sqrt(1 - (point.getX() * point.getX()) - (point.getY() * point.getY())));

// Adding point to noisySphere will copy the reference, so point is closed
// in a try-with-resources block
try (Point3d point = new Point3d()) {

point.setX(randomFloat(-1f, 1f));
point.setY(randomFloat(-1f, 1f));

if (i % 5 == 0) {
point.setZ(randomFloat(-1f, 1f));
} else if (i % 2 == 0) {
point.setZ((float) Math.sqrt(1 - (point.getX() * point.getX()) - (point.getY() * point.getY())));
} else {
point.setZ((float) -Math.sqrt(1 - (point.getX() * point.getX()) - (point.getY() * point.getY())));
}

point.setRGB((short) 255, (short) 255, (short) 255);

noisySphere.add(point);
}

point.setRGB((short)255, (short)255, (short)255);

noisySphere.add(point);

/*
* The reference to point created within the loop is not the same as the reference
* to the point within the noisySphere point cloud; as such it needs to be released
* at the end of each loop iteration.
*/

point.dispose();
}

return noisySphere;
}

/**
* @param min is the minimum float that can be generated
* @param max is the maximum float that can be generated
* @return a float between min and max
*/
private static float randomFloat(float min, float max) {
private static float randomFloat(float min, float max) {
return (float) (min + Math.random() * (max - min));
}

Expand Down
35 changes: 35 additions & 0 deletions src/main/c/com_movlad_pcl_Matrix4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <jni.h>

#include <eigen3/Eigen/Dense>

#include "handle.h"

#include "com_movlad_pcl_Normal.h"


void Java_com_movlad_pcl_Matrix4_alloc(JNIEnv* env, jobject obj) {
Eigen::Matrix4d *matrix_ptr = new Eigen::Matrix4d();

set_handle(env, obj, matrix_ptr);
}

void Java_com_movlad_pcl_Matrix4_dispose(JNIEnv* env, jobject obj) {
Eigen::Matrix4d *matrix_ptr = get_handle<Eigen::Matrix4d>(env, obj);

delete matrix_ptr;

set_handle<Eigen::Matrix4d>(env, obj, nullptr);
}

jdouble Java_com_movlad_pcl_Matrix4_nGet(JNIEnv* env, jobject obj, jint r, jint c) {
Eigen::Matrix4d *matrix_ptr = get_handle<Eigen::Matrix4d>(env, obj);

return (jdouble) (*matrix_ptr)(r, c);
}

void Java_com_movlad_pcl_Matrix4_nSet(JNIEnv* env, jobject obj, jint r, jint c, jdouble value) {
Eigen::Matrix4d *matrix_ptr = get_handle<Eigen::Matrix4d>(env, obj);

(*matrix_ptr)(r, c) = (double) value;
}

45 changes: 45 additions & 0 deletions src/main/c/com_movlad_pcl_Matrix4.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/main/c/com_movlad_pcl_Matrix4f.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <jni.h>

#include <eigen3/Eigen/Dense>

#include "handle.h"

#include "com_movlad_pcl_Normal.h"


void Java_com_movlad_pcl_Matrix4f_alloc(JNIEnv* env, jobject obj) {
Eigen::Matrix4f *matrix_ptr = new Eigen::Matrix4f();

set_handle(env, obj, matrix_ptr);
}

void Java_com_movlad_pcl_Matrix4f_dispose(JNIEnv* env, jobject obj) {
Eigen::Matrix4f *matrix_ptr = get_handle<Eigen::Matrix4f>(env, obj);

delete matrix_ptr;

set_handle<Eigen::Matrix4f>(env, obj, nullptr);
}

jfloat Java_com_movlad_pcl_Matrix4f_nGet(JNIEnv* env, jobject obj, jint r, jint c) {
Eigen::Matrix4f *matrix_ptr = get_handle<Eigen::Matrix4f>(env, obj);

return (jfloat) (*matrix_ptr)(r, c);
}

void Java_com_movlad_pcl_Matrix4f_nSet(JNIEnv* env, jobject obj, jint r, jint c, jfloat value) {
Eigen::Matrix4f *matrix_ptr = get_handle<Eigen::Matrix4f>(env, obj);

(*matrix_ptr)(r, c) = (float) value;
}

45 changes: 45 additions & 0 deletions src/main/c/com_movlad_pcl_Matrix4f.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/main/c/com_movlad_pcl_NormalCloud.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdexcept>

#include "com_movlad_pcl_NormalCloud.h"
#include "sptr_wrapper.h"
#include "handle.h"
Expand Down Expand Up @@ -35,7 +37,7 @@ void Java_com_movlad_pcl_NormalCloud_add(JNIEnv *env, jobject obj, jobject point

void Java_com_movlad_pcl_NormalCloud_remove(JNIEnv *env, jobject obj, jobject point)
{
throw "Not yet implemented.";
throw std::runtime_error("Not yet implemented.");
}

void Java_com_movlad_pcl_NormalCloud_clear(JNIEnv *env, jobject obj)
Expand All @@ -58,4 +60,4 @@ jboolean Java_com_movlad_pcl_NormalCloud_isOrganized(JNIEnv *env, jobject obj)
bool isOrganized = cloud_ptr->isOrganized();

return isOrganized;
}
}
2 changes: 1 addition & 1 deletion src/main/c/com_movlad_pcl_Point3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ void Java_com_movlad_pcl_Point3d_setB(JNIEnv *env, jobject obj, jshort b)
jfloat Java_com_movlad_pcl_Point3d_getRGB(JNIEnv *env, jobject obj)
{
return get_handle<pcl::PointXYZRGB>(env, obj)->rgb;
}
}
Loading