Skip to content

Commit

Permalink
bugfix in triangulatedSetRep reading + off export
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-gauthier-geosiris committed Jun 1, 2024
1 parent d1e6113 commit 6c5944d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.geosiris</groupId>
<artifactId>energyml-utils</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.0.10</version>
<version>1.0.11</version>
<organization>
<name>Geosiris</name>
<url>http://www.geosiris.com</url>
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/geosiris/energyml/data/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
import com.geosiris.energyml.exception.NotImplementedException;
import com.geosiris.energyml.exception.ObjectNotFoundNotError;
import com.geosiris.energyml.pkg.EPCFile;
import com.geosiris.energyml.pkg.EpcHdf5FileManager;
import com.geosiris.energyml.utils.EnergymlWorkspace;
import com.geosiris.energyml.utils.ObjectController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.geosiris.energyml.data.SurfaceMesh.exportObj;
import static com.geosiris.energyml.data.SurfaceMesh.exportOff;
import static com.geosiris.energyml.utils.EnergymlWorkspaceHelper.*;
import static com.geosiris.energyml.utils.ObjectController.searchAttributeMatchingNameWithPath;

Expand Down Expand Up @@ -318,7 +323,7 @@ public static List<GridedPointSetMesh> readGrid2dRepresentationPoints(Object ene
return meshes;
}

public static List<SurfaceMesh> readTriangulatedSetRepresentation (Object energymlObject, EnergymlWorkspace
public static List<SurfaceMesh> readTriangulatedSetRepresentation(Object energymlObject, EnergymlWorkspace
workspace) throws NotImplementedException, InvocationTargetException, IllegalAccessException {
List<SurfaceMesh> meshes = new ArrayList<>();
try {
Expand Down Expand Up @@ -359,7 +364,7 @@ public static List<SurfaceMesh> readTriangulatedSetRepresentation (Object energy
pointList.addAll(((List<List<?>>) pl).stream()
.map(l -> l.stream().map(v -> ((Number) v).doubleValue()).collect(Collectors.toList())).collect(Collectors.toList()));
} else { // pl given flat
for (int i = 0; i < pl.size() - 2; i++) {
for (int i = 0; i < pl.size() - 2; i+=3) {
pointList.add(new ArrayList<>(List.of(
((Number) pl.get(i)).doubleValue(),
((Number) pl.get(i + 1)).doubleValue(),
Expand All @@ -382,7 +387,7 @@ public static List<SurfaceMesh> readTriangulatedSetRepresentation (Object energy
trianglesList_obj.addAll(((List<List<?>>)indices).stream()
.map(l -> l.stream().map(v -> ((Number)v).longValue()).collect(Collectors.toList())).collect(Collectors.toList()));
}else{ // indices given flat
for(int i=0; i<indices.size()-2; i++){
for(int i=0; i<indices.size()-2; i+=3){
trianglesList_obj.add(new ArrayList<>(List.of(
((Number)indices.get(i)).longValue(),
((Number)indices.get(i+1)).longValue(),
Expand Down Expand Up @@ -417,4 +422,5 @@ public static List<SurfaceMesh> readTriangulatedSetRepresentation (Object energy

return meshes;
}

}
61 changes: 52 additions & 9 deletions src/main/java/com/geosiris/energyml/data/SurfaceMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package com.geosiris.energyml.data;

import com.geosiris.energyml.exception.NotImplementedException;
import com.geosiris.energyml.pkg.EpcHdf5FileManager;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -110,12 +114,13 @@ public static void exportObjElt(
);
if (colors != null && colors.size() > cpt && colors.get(cpt) != null && !colors.get(cpt).isEmpty())
{
pointPart.write(String.format("%s",
facePart.write(String.format("%s",
colors.get(cpt).stream().map(String::valueOf).collect(Collectors.joining(" "))).getBytes(StandardCharsets.UTF_8)
);
}
pointPart.write("\n".getBytes(StandardCharsets.UTF_8));
facePart.write("\n".getBytes(StandardCharsets.UTF_8));
}
cpt++;
}
}

Expand All @@ -136,29 +141,34 @@ public static void exportOffElt(
Long pointOffset,
List<List<Integer>> colors
) throws IOException {
Long offsetObj = 1L; // OBJ point indices starts at 1 not 0
for(var p: points) {
if (!p.isEmpty())
pointPart.write(String.format("%s\n",
p.stream().map(String::valueOf).collect(Collectors.joining(" "))).getBytes(StandardCharsets.UTF_8)
);
}
// cpt = 0
int cpt = 0;
for(var face: indices) {
if(!indices.isEmpty())
if(!indices.isEmpty()) {
facePart.write(
String.format("%s\n",
face.stream().map(x -> String.valueOf(x + pointOffset + offsetObj)).collect(Collectors.joining(" ")))
String.format("%d %s %s\n",
face.size(),
face.stream().map(x -> String.valueOf(x + pointOffset)).collect(Collectors.joining(" ")),
cpt >= colors.size() ? "" : colors.get(cpt).stream().map(String::valueOf).collect(Collectors.joining(" "))
)
.getBytes(
StandardCharsets.UTF_8)
);
}
cpt++;
}
}

public static void exportObj(
List<AbstractMesh> meshList,
OutputStream out,
String objName
String objName,
Boolean useOffset
) throws IOException {
out.write("# Generated by energyml-utils a Geosiris python module\n\n".getBytes(StandardCharsets.UTF_8));
if(objName != null){
Expand All @@ -173,12 +183,45 @@ public static void exportObj(
out,
(List<List<Double>>) m.pointList,
m.getEdgeIndices(),
pointOffset,
useOffset ? pointOffset : 0,
new ArrayList<>(),
m instanceof PolylineSetMesh ? "l" : "f"
);
pointOffset += m.pointList.size();
out.write("\n".getBytes(StandardCharsets.UTF_8));
}
}

public static void exportOff(
List<AbstractMesh> meshList,
OutputStream out,
String objName,
Boolean useOffset
) throws IOException {
out.write("OFF\n# Generated by energyml-utils a Geosiris python module\n\n".getBytes(StandardCharsets.UTF_8));
out.write(String.format("%d %d %d\n",
meshList.stream().map(AbstractMesh::getNbPoints).reduce(0L, Long::sum),
meshList.stream().map(AbstractMesh::getNbFaces).reduce(0L, Long::sum),
meshList.stream().map(AbstractMesh::getNbEdge).reduce(0L, Long::sum)
).getBytes(StandardCharsets.UTF_8)
);

ByteArrayOutputStream out_i = new ByteArrayOutputStream();

Long pointOffset = 0L;
for(var m: meshList){
exportOffElt(
out,
out_i,
(List<List<Double>>) m.pointList,
m.getEdgeIndices(),
useOffset ? pointOffset : 0,
new ArrayList<>()
);
pointOffset += m.pointList.size();
out.write("\n".getBytes(StandardCharsets.UTF_8));
}

out_i.writeTo(out);
}
}

0 comments on commit 6c5944d

Please sign in to comment.