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

Add copy method for Mesh #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 22 additions & 6 deletions src/main/java/net/imagej/mesh/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand Down Expand Up @@ -85,9 +85,25 @@
*/
public interface Mesh {

/** The mesh's collection of vertices. */
Vertices vertices();
/**
* The mesh's collection of vertices.
*/
Vertices vertices();

/**
* The mesh's collection of triangles.
*/
Triangles triangles();

/**
* Return a copy of the mesh.
*/
default Mesh copy() {
Mesh c = this.createVariable();
c.vertices().set(vertices().copy());
c.triangles().set(triangles().copy());
return c;
}

/** The mesh's collection of triangles. */
Triangles triangles();
Mesh createVariable();
}
18 changes: 9 additions & 9 deletions src/main/java/net/imagej/mesh/Meshes.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Meshes {
*/
public static RealPoint center(final Mesh m) {
RealPoint p = new RealPoint(0, 0, 0);
for (final Vertex v: m.vertices()) {
for (final Vertex v : m.vertices()) {
p.move(v);
}
for (int d = 0; d < 3; d++) {
Expand All @@ -63,7 +63,7 @@ public static float[] boundingBox(final net.imagej.mesh.Mesh mesh) {
final float[] boundingBox = new float[]{Float.POSITIVE_INFINITY,
Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY,
Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY};
for (final Vertex v: mesh.vertices()) {
for (final Vertex v : mesh.vertices()) {
final float x = v.xf(), y = v.yf(), z = v.zf();
if (x < boundingBox[0]) boundingBox[0] = x;
if (y < boundingBox[1]) boundingBox[1] = y;
Expand All @@ -85,7 +85,7 @@ public static void copy(final net.imagej.mesh.Mesh src,
final net.imagej.mesh.Mesh dest) {
final Map<Long, Long> vIndexMap = new HashMap<>();
// Copy the vertices, keeping track when indices change.
for (final Vertex v: src.vertices()) {
for (final Vertex v : src.vertices()) {
long srcIndex = v.index();
long destIndex = dest.vertices().add(//
v.x(), v.y(), v.z(), //
Expand All @@ -103,7 +103,7 @@ public static void copy(final net.imagej.mesh.Mesh src,
}
}
// Copy the triangles, taking care to use destination indices.
for (final Triangle tri: src.triangles()) {
for (final Triangle tri : src.triangles()) {
final long v0src = tri.vertex0();
final long v1src = tri.vertex1();
final long v2src = tri.vertex2();
Expand All @@ -125,7 +125,7 @@ public static void calculateNormals(net.imagej.mesh.Mesh src, net.imagej.mesh.Me

// Compute the triangle normals.
HashMap<Long, float[]> triNormals = new HashMap<>();
for (final Triangle tri: src.triangles()) {
for (final Triangle tri : src.triangles()) {
final int v0 = (int) tri.vertex0();
final int v1 = (int) tri.vertex1();
final int v2 = (int) tri.vertex2();
Expand Down Expand Up @@ -159,9 +159,9 @@ public static void calculateNormals(net.imagej.mesh.Mesh src, net.imagej.mesh.Me
// Next, compute the normals per vertex based on face normals
HashMap<Long, float[]> vNormals = new HashMap<>();// Note: these are cumulative until normalized by vNbrCount
float[] cumNormal, triNormal;
for (final Triangle tri: src.triangles()) {
for (final Triangle tri : src.triangles()) {
triNormal = triNormals.get(tri.index());
for (long idx: new long[]{tri.vertex0(), tri.vertex1(), tri.vertex2()}) {
for (long idx : new long[]{tri.vertex0(), tri.vertex1(), tri.vertex2()}) {
cumNormal = vNormals.getOrDefault(idx, new float[]{0, 0, 0});
cumNormal[0] += triNormal[0];
cumNormal[1] += triNormal[1];
Expand All @@ -175,7 +175,7 @@ public static void calculateNormals(net.imagej.mesh.Mesh src, net.imagej.mesh.Me
float[] vNormal;
double vNormalMag;
// Copy the vertices, keeping track when indices change.
for (final Vertex v: src.vertices()) {
for (final Vertex v : src.vertices()) {
long srcIndex = v.index();
vNormal = vNormals.get(v.index());
vNormalMag = Math.sqrt(Math.pow(vNormal[0], 2) + Math.pow(vNormal[1], 2) + Math.pow(vNormal[2], 2));
Expand All @@ -195,7 +195,7 @@ public static void calculateNormals(net.imagej.mesh.Mesh src, net.imagej.mesh.Me
}
}
// Copy the triangles, taking care to use destination indices.
for (final Triangle tri: src.triangles()) {
for (final Triangle tri : src.triangles()) {
final long v0src = tri.vertex0();
final long v1src = tri.vertex1();
final long v2src = tri.vertex2();
Expand Down
Loading