Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/obj texture #430

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 4 additions & 19 deletions applications/src/main/java/boofcv/app/MeshViewerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@
import boofcv.struct.image.ImageType;
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.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Locale;

/**
* Very simple app for opening and viewing a 3D mesh
Expand All @@ -49,19 +45,8 @@ 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;
case "stl" -> PointCloudIO.Format.STL;
case "obj" -> PointCloudIO.Format.OBJ;
default -> {
throw new RuntimeException("Unknown file type");
}
};

try (var input = new FileInputStream(file)) {
PointCloudIO.load(type, input, mesh, colors);
try {
PointCloudIO.load(file, mesh);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
Expand All @@ -86,8 +71,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) 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 @@ -40,7 +40,7 @@ public class MeshColorizeOps {
* @return SurfaceColor
*/
public static RenderMesh.SurfaceColor colorizeByVertex( VertexMesh mesh, int[] vertexColor ) {
return ( shapeIdx ) -> vertexColor[mesh.indexes.get(mesh.offsets.get(shapeIdx))];
return ( shapeIdx ) -> vertexColor[mesh.faceVertexes.get(mesh.faceOffsets.get(shapeIdx))];
}

/**
Expand All @@ -57,7 +57,7 @@ public static RenderMesh.SurfaceColor colorizeByNormal( VertexMesh mesh ) {
var axisZ = new Vector3D_F64(0, 0, 1);

for (int i = 0; i < mesh.size(); i++) {
mesh.getShape(i, facet);
mesh.getFaceVectors(i, facet);

// Handle case of invalid facet gracefully by assigning it to an arbitrary color
if (facet.size < 3) {
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) 2021, 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 @@ -38,6 +38,9 @@ public interface PointCloudReader {
*/
int size();

/** True if each point has a color */
boolean colors();

/**
* Copies the point
*/
Expand All @@ -58,6 +61,8 @@ static PointCloudReader wrap3FRGB( float[] cloud, float[] rgb, int offset, int l
@Override
public int size() {return length;}

@Override public boolean colors() {return true;}

@Override
public void get( int index, Point3D_F32 point ) {
int i = offset + index*3;
Expand Down Expand Up @@ -85,6 +90,8 @@ static PointCloudReader wrapF32( List<Point3D_F32> cloud ) {
return new PointCloudReader() {
@Override public int size() {return cloud.size();}

@Override public boolean colors() {return false;}

@Override public void get( int index, Point3D_F32 point ) {point.setTo(cloud.get(index));}

@Override public void get( int index, Point3D_F64 point ) {convert(cloud.get(index), point);}
Expand All @@ -97,6 +104,8 @@ static PointCloudReader wrapF64( List<Point3D_F64> cloud ) {
return new PointCloudReader() {
@Override public int size() {return cloud.size();}

@Override public boolean colors() {return false;}

@Override public void get( int index, Point3D_F32 point ) {convert(cloud.get(index), point);}

@Override public void get( int index, Point3D_F64 point ) {point.setTo(cloud.get(index));}
Expand All @@ -109,6 +118,8 @@ static PointCloudReader wrapF32RGB( List<Point3dRgbI_F32> cloud ) {
return new PointCloudReader() {
@Override public int size() {return cloud.size();}

@Override public boolean colors() {return true;}

@Override public void get( int index, Point3D_F32 point ) {point.setTo(cloud.get(index));}

@Override public void get( int index, Point3D_F64 point ) {convert(cloud.get(index), point);}
Expand All @@ -121,6 +132,8 @@ static PointCloudReader wrapF64RGB( List<Point3dRgbI_F64> cloud ) {
return new PointCloudReader() {
@Override public int size() {return cloud.size();}

@Override public boolean colors() {return true;}

@Override public void get( int index, Point3D_F32 point ) {convert(cloud.get(index), point);}

@Override public void get( int index, Point3D_F64 point ) {point.setTo(cloud.get(index));}
Expand All @@ -133,6 +146,8 @@ static PointCloudReader wrapF32( List<Point3D_F32> cloud, int[] rgb ) {
return new PointCloudReader() {
@Override public int size() {return cloud.size();}

@Override public boolean colors() {return true;}

@Override public void get( int index, Point3D_F32 point ) {point.setTo(cloud.get(index));}

@Override public void get( int index, Point3D_F64 point ) {convert(cloud.get(index), point);}
Expand All @@ -145,6 +160,8 @@ static PointCloudReader wrapF64( List<Point3D_F64> cloud, int[] rgb ) {
return new PointCloudReader() {
@Override public int size() {return cloud.size();}

@Override public boolean colors() {return true;}

@Override public void get( int index, Point3D_F32 point ) {convert(cloud.get(index), point);}

@Override public void get( int index, Point3D_F64 point ) {point.setTo(cloud.get(index));}
Expand All @@ -159,6 +176,8 @@ static PointCloudReader wrap( Generic op, int size ) {

@Override public int size() {return size;}

@Override public boolean colors() {return true;}

@Override public void get( int index, Point3D_F32 point ) {
op.get(index, p);
point.x = (float)p.x;
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();

/**
* Set the 3D location of the point
*/
void location( double x, double y, double z );

/**
* Adds a 3D point with color information
* Sets the points color
*/
void add( double x, double y, double z, int rgb );
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,23 +116,56 @@ 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);
}
};
}

static PointCloudWriter wrapF64( DogArray<Point3D_F64> cloud, DogArray_I32 colors ) {
return new PointCloudWriter() {
@Override public void initialize( int size, boolean hasColor ) {
cloud.reserve(size);
cloud.reset();
colors.reserve(size);
colors.reset();
}

@Override public void startPoint() {}

@Override public void stopPoint() {}

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

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

static PointCloudWriter wrapF32RGB( DogArray<Point3dRgbI_F32> cloud ) {
return new PointCloudWriter() {
@Override public void initialize( int size, boolean hasColor ) {
cloud.reserve(size);
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 +176,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
Loading
Loading