Skip to content

Commit

Permalink
Abstract method of reading vertexes and point clouds can now handle m…
Browse files Browse the repository at this point in the history
…ultiple features assigned to a single point better
  • Loading branch information
lessthanoptimal committed Jun 23, 2024
1 parent 0e1d6a4 commit 2ca471c
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 89 deletions.
8 changes: 3 additions & 5 deletions applications/src/main/java/boofcv/app/MeshViewerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import boofcv.struct.image.InterleavedU8;
import boofcv.struct.mesh.VertexMesh;
import org.apache.commons.io.FilenameUtils;
import org.ddogleg.struct.DogArray_I32;

import javax.swing.*;
import java.awt.*;
Expand All @@ -49,7 +48,6 @@ public MeshViewerApp() {
private static void loadFile( File file ) {
// Load the mesh
var mesh = new VertexMesh();
var colors = new DogArray_I32();
String extension = FilenameUtils.getExtension(file.getName()).toLowerCase(Locale.ENGLISH);
var type = switch (extension) {
case "ply" -> PointCloudIO.Format.PLY;
Expand All @@ -61,7 +59,7 @@ private static void loadFile( File file ) {
};

try (var input = new FileInputStream(file)) {
PointCloudIO.load(type, input, mesh, colors);
PointCloudIO.load(type, input, mesh);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
Expand All @@ -86,8 +84,8 @@ private static void loadFile( File file ) {
SwingUtilities.invokeLater(() -> {
var panel = new MeshViewerPanel();
panel.setMesh(mesh, false);
if (colors.size > 0)
panel.setVertexColors("RGB", colors.data);
if (mesh.rgb.size > 0)
panel.setVertexColors("RGB", mesh.rgb.data);
if (_image != null)
panel.setTextureImage(_image);
panel.setPreferredSize(new Dimension(500, 500));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Peter Abeles. All Rights Reserved.
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -175,7 +175,10 @@ private void process( GrayU8 disparity, ColorImage color, PointCloudWriter outpu
// Bring it back into left camera frame
GeometryMath_F32.multTran(rectifiedR, p, p);

output.add(p.x, p.y, p.z, getColor(color, pixelX, pixelY));
output.startPoint();
output.location(p.x, p.y, p.z);
output.color(getColor(color, pixelX, pixelY));
output.stopPoint();
}
}
}
Expand Down Expand Up @@ -217,7 +220,10 @@ private void process( GrayF32 disparity, ColorImage color, PointCloudWriter outp
// Bring it back into left camera frame
GeometryMath_F32.multTran(rectifiedR, p, p);

output.add(p.x, p.y, p.z, getColor(color, pixelX, pixelY));
output.startPoint();
output.location(p.x, p.y, p.z);
output.color(getColor(color, pixelX, pixelY));
output.stopPoint();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Peter Abeles. All Rights Reserved.
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -40,10 +40,21 @@ public interface PointCloudWriter {
*/
void initialize( int size, boolean hasColor );

/** A new point is being added with new attributes to follow */
void startPoint();

/** It's done specifying attributes for the point */
void stopPoint();

/**
* Adds a 3D point with color information
* Set the 3D location of the point
*/
void add( double x, double y, double z, int rgb );
void location( double x, double y, double z );

/**
* Sets the points color
*/
void color( int rgb );

class CloudArraysF32 implements PointCloudWriter {
// Storage for point cloud
Expand All @@ -57,10 +68,17 @@ class CloudArraysF32 implements PointCloudWriter {
cloudXyz.reserve(size*3);
}

@Override public void add( double x, double y, double z, int rgb ) {
@Override public void startPoint() {}

@Override public void stopPoint() {}

@Override public void location( double x, double y, double z ) {
cloudXyz.add((float)x);
cloudXyz.add((float)y);
cloudXyz.add((float)z);
}

@Override public void color( int rgb ) {
cloudRgb.add(rgb);
}

Expand All @@ -79,10 +97,15 @@ static PointCloudWriter wrapF32( DogArray<Point3D_F32> cloud ) {
cloud.reset();
}

@Override
public void add( double x, double y, double z, int rgb ) {
@Override public void startPoint() {}

@Override public void stopPoint() {}

@Override public void location( double x, double y, double z ) {
cloud.grow().setTo((float)x, (float)y, (float)z);
}

@Override public void color( int rgb ) {}
};
}

Expand All @@ -93,8 +116,13 @@ static PointCloudWriter wrapF64( DogArray<Point3D_F64> cloud ) {
cloud.reset();
}

@Override
public void add( double x, double y, double z, int rgb ) {
@Override public void startPoint() {}

@Override public void stopPoint() {}

@Override public void color( int rgb ) {}

@Override public void location( double x, double y, double z ) {
cloud.grow().setTo(x, y, z);
}
};
Expand All @@ -107,9 +135,18 @@ static PointCloudWriter wrapF32RGB( DogArray<Point3dRgbI_F32> cloud ) {
cloud.reset();
}

@Override
public void add( double x, double y, double z, int rgb ) {
cloud.grow().setTo((float)x, (float)y, (float)z, rgb);
@Override public void startPoint() {
cloud.grow();
}

@Override public void stopPoint() {}

@Override public void location( double x, double y, double z ) {
cloud.getTail().setTo((float)x, (float)y, (float)z);
}

@Override public void color( int rgb ) {
cloud.getTail().rgb = rgb;
}
};
}
Expand All @@ -120,9 +157,18 @@ static PointCloudWriter wrapF64RGB( DogArray<Point3dRgbI_F64> cloud ) {
cloud.reset();
}

@Override
public void add( double x, double y, double z, int rgb ) {
cloud.grow().setTo(x, y, z, rgb);
@Override public void startPoint() {
cloud.grow();
}

@Override public void stopPoint() {}

@Override public void location( double x, double y, double z ) {
cloud.getTail().setTo(x, y, z);
}

@Override public void color( int rgb ) {
cloud.getTail().rgb = rgb;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Peter Abeles. All Rights Reserved.
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -35,32 +35,41 @@ class TestPointCloudWriter extends BoofStandardJUnit {

static abstract class PcwTests {
boolean supportsColor = true;

public abstract PointCloudWriter create();

public abstract int size( PointCloudWriter data );
public abstract Point3D_F64 getPoint(PointCloudWriter data , int i );
public abstract int getColor(PointCloudWriter data , int i);

public abstract Point3D_F64 getPoint( PointCloudWriter data, int i );

public abstract int getColor( PointCloudWriter data, int i );

@Test
void simpleXyzColor() {
PointCloudWriter alg = create();

assertEquals(0,size(alg));
alg.add(0,1,2, 345);
assertEquals(1,size(alg));
alg.add(2,1,3,3434);
assertEquals(2,size(alg));

if( supportsColor ) {
assertEquals(0, size(alg));
alg.startPoint();
alg.location(0, 1, 2);
alg.color(345);
alg.stopPoint();
assertEquals(1, size(alg));
alg.startPoint();
alg.location(2, 1, 3);
alg.color(3434);
alg.stopPoint();
assertEquals(2, size(alg));

if (supportsColor) {
assertEquals(345, getColor(alg, 0));
assertEquals(3434, getColor(alg, 1));
}

Point3D_F64 found = getPoint(alg,1);
assertEquals(2,found.x, UtilEjml.TEST_F64);
assertEquals(1,found.y, UtilEjml.TEST_F64);
assertEquals(3,found.z, UtilEjml.TEST_F64);
Point3D_F64 found = getPoint(alg, 1);
assertEquals(2, found.x, UtilEjml.TEST_F64);
assertEquals(1, found.y, UtilEjml.TEST_F64);
assertEquals(3, found.z, UtilEjml.TEST_F64);
}

}

@Nested
Expand All @@ -72,18 +81,18 @@ public PointCloudWriter create() {
}

@Override
public int size(PointCloudWriter data) {
public int size( PointCloudWriter data ) {
return ((CloudArraysF32)data).cloudXyz.size/3;
}

@Override
public Point3D_F64 getPoint(PointCloudWriter data, int i) {
public Point3D_F64 getPoint( PointCloudWriter data, int i ) {
var list = ((CloudArraysF32)data).cloudXyz;
return new Point3D_F64( list.get(i*3), list.get(i*3+1), list.get(i*3+2));
return new Point3D_F64(list.get(i*3), list.get(i*3 + 1), list.get(i*3 + 2));
}

@Override
public int getColor(PointCloudWriter data, int i) {
public int getColor( PointCloudWriter data, int i ) {
var list = ((CloudArraysF32)data).cloudRgb;
return list.get(i);
}
Expand All @@ -102,18 +111,18 @@ public PointCloudWriter create() {
}

@Override
public int size(PointCloudWriter data) {
public int size( PointCloudWriter data ) {
return cloud.size;
}

@Override
public Point3D_F64 getPoint(PointCloudWriter data, int i) {
public Point3D_F64 getPoint( PointCloudWriter data, int i ) {
Point3D_F32 c = cloud.get(i);
return new Point3D_F64( c.x, c.y, c.z);
return new Point3D_F64(c.x, c.y, c.z);
}

@Override
public int getColor(PointCloudWriter data, int i) {
public int getColor( PointCloudWriter data, int i ) {
throw new RuntimeException("Not supported");
}
}
Expand All @@ -131,17 +140,17 @@ public PointCloudWriter create() {
}

@Override
public int size(PointCloudWriter data) {
public int size( PointCloudWriter data ) {
return cloud.size;
}

@Override
public Point3D_F64 getPoint(PointCloudWriter data, int i) {
public Point3D_F64 getPoint( PointCloudWriter data, int i ) {
return cloud.get(i);
}

@Override
public int getColor(PointCloudWriter data, int i) {
public int getColor( PointCloudWriter data, int i ) {
throw new RuntimeException("Not supported");
}
}
Expand All @@ -158,18 +167,18 @@ public PointCloudWriter create() {
}

@Override
public int size(PointCloudWriter data) {
public int size( PointCloudWriter data ) {
return cloud.size;
}

@Override
public Point3D_F64 getPoint(PointCloudWriter data, int i) {
public Point3D_F64 getPoint( PointCloudWriter data, int i ) {
Point3D_F32 c = cloud.get(i);
return new Point3D_F64( c.x, c.y, c.z);
return new Point3D_F64(c.x, c.y, c.z);
}

@Override
public int getColor(PointCloudWriter data, int i) {
public int getColor( PointCloudWriter data, int i ) {
return cloud.get(i).rgb;
}
}
Expand All @@ -186,19 +195,18 @@ public PointCloudWriter create() {
}

@Override
public int size(PointCloudWriter data) {
public int size( PointCloudWriter data ) {
return cloud.size;
}

@Override
public Point3D_F64 getPoint(PointCloudWriter data, int i) {
public Point3D_F64 getPoint( PointCloudWriter data, int i ) {
return cloud.get(i);
}

@Override
public int getColor(PointCloudWriter data, int i) {
public int getColor( PointCloudWriter data, int i ) {
return cloud.get(i).rgb;
}
}

}
Loading

0 comments on commit 2ca471c

Please sign in to comment.