mirror of
https://github.com/Dark98/SliceBeam.git
synced 2026-07-02 16:49:02 +00:00
WIP: Flatten to surface feature; Fix rotating multiple axis
This commit is contained in:
@@ -85,6 +85,7 @@ public class BedMenuItem extends SimpleRecyclerItem<BedMenuItem.BedMenuItemHolde
|
||||
|
||||
private Path path = new Path();
|
||||
private float checkedProgress;
|
||||
private boolean enabled;
|
||||
|
||||
public BedMenuItemHolderView(Context context) {
|
||||
super(context);
|
||||
@@ -117,7 +118,7 @@ public class BedMenuItem extends SimpleRecyclerItem<BedMenuItem.BedMenuItemHolde
|
||||
int rad = ViewUtils.dp(16);
|
||||
canvas.drawRoundRect(0, 0, getWidth(), getHeight(), rad, rad, bgPaint);
|
||||
|
||||
if (checkedProgress != 0f) {
|
||||
if (enabled && checkedProgress != 0f) {
|
||||
if (checkedProgress == 1f) {
|
||||
canvas.drawRoundRect(0, 0, getWidth(), getHeight(), rad, rad, accentPaint);
|
||||
} else {
|
||||
@@ -144,6 +145,7 @@ public class BedMenuItem extends SimpleRecyclerItem<BedMenuItem.BedMenuItemHolde
|
||||
}
|
||||
|
||||
public void bind(BedMenuItem item) {
|
||||
enabled = item.isEnabled;
|
||||
title.setMaxLines(item.isSingleLine ? 1 : 2);
|
||||
title.setText(item.titleRes);
|
||||
icon.setImageResource(item.iconRes);
|
||||
|
||||
@@ -28,6 +28,7 @@ import ru.ytkab0bp.slicebeam.R;
|
||||
import ru.ytkab0bp.slicebeam.SliceBeam;
|
||||
import ru.ytkab0bp.slicebeam.components.BeamAlertDialogBuilder;
|
||||
import ru.ytkab0bp.slicebeam.components.UnfoldMenu;
|
||||
import ru.ytkab0bp.slicebeam.events.FlattenModeResetEvent;
|
||||
import ru.ytkab0bp.slicebeam.events.NeedSnackbarEvent;
|
||||
import ru.ytkab0bp.slicebeam.events.ObjectsListChangedEvent;
|
||||
import ru.ytkab0bp.slicebeam.events.SelectedObjectChangedEvent;
|
||||
@@ -52,31 +53,68 @@ public class OrientationMenu extends ListBedMenu {
|
||||
return Arrays.asList(
|
||||
new BedMenuItem(R.string.MenuOrientationArrange, R.drawable.grid_layout_outline_28).onClick(v -> {
|
||||
fragment.getGlView().arrange();
|
||||
fragment.getGlView().queueEvent(() -> {
|
||||
if (fragment.getGlView().getRenderer().invalidateFlattenMode()) {
|
||||
fragment.getGlView().requestRender();
|
||||
}
|
||||
});
|
||||
SliceBeam.EVENT_BUS.fireEvent(new NeedSnackbarEvent(R.string.MenuOrientationArrangeFinished));
|
||||
}).setEnabled(fragment.getGlView().getRenderer().getModel() != null),
|
||||
new SpaceItem(portrait ? ViewUtils.dp(8) : 0, portrait ? 0 : ViewUtils.dp(8)),
|
||||
new BedMenuItem(R.string.MenuOrientationPosition, R.drawable.menu_orientation_position_28).setEnabled(hasSelection()).onClick(v -> fragment.showUnfoldMenu(new PositionMenu(), v)),
|
||||
new BedMenuItem(R.string.MenuOrientationRotation, R.drawable.menu_orientation_rotation_28).setEnabled(hasSelection()).onClick(v -> fragment.showUnfoldMenu(new RotationMenu(), v))
|
||||
new BedMenuItem(R.string.MenuOrientationFlatten, R.drawable.menu_orientation_position_28).setEnabled(hasSelection()).setCheckable((buttonView, isChecked) -> {
|
||||
fragment.getGlView().getRenderer().setInFlattenMode(isChecked);
|
||||
fragment.getGlView().requestRender();
|
||||
}, false),
|
||||
new BedMenuItem(R.string.MenuOrientationPosition, R.drawable.menu_orientation_position_28).setEnabled(hasSelection()).onClick(v -> {
|
||||
if (fragment.getGlView().getRenderer().resetFlattenMode()) {
|
||||
fragment.getGlView().requestRender();
|
||||
((BedMenuItem) adapter.getItems().get(2)).isChecked = false;
|
||||
adapter.notifyItemChanged(2);
|
||||
}
|
||||
fragment.showUnfoldMenu(new PositionMenu(), v);
|
||||
}),
|
||||
new BedMenuItem(R.string.MenuOrientationRotation, R.drawable.menu_orientation_rotation_28).setEnabled(hasSelection()).onClick(v -> {
|
||||
if (fragment.getGlView().getRenderer().resetFlattenMode()) {
|
||||
fragment.getGlView().requestRender();
|
||||
((BedMenuItem) adapter.getItems().get(2)).isChecked = false;
|
||||
adapter.notifyItemChanged(2);
|
||||
}
|
||||
fragment.showUnfoldMenu(new RotationMenu(), v);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@EventHandler(runOnMainThread = true)
|
||||
public void onFlattenModeReset(FlattenModeResetEvent e) {
|
||||
((BedMenuItem) adapter.getItems().get(2)).isChecked = false;
|
||||
adapter.notifyItemChanged(2);
|
||||
}
|
||||
|
||||
@EventHandler(runOnMainThread = true)
|
||||
public void onObjectsChanged(ObjectsListChangedEvent e) {
|
||||
((BedMenuItem) adapter.getItems().get(0)).setEnabled(fragment.getGlView().getRenderer().getModel() != null);
|
||||
adapter.notifyItemChanged(0);
|
||||
|
||||
((BedMenuItem) adapter.getItems().get(2)).setEnabled(hasSelection());
|
||||
adapter.notifyItemChanged(2);
|
||||
((BedMenuItem) adapter.getItems().get(3)).setEnabled(hasSelection());
|
||||
adapter.notifyItemChanged(3);
|
||||
for (int i = 2; i <= 4; i++) {
|
||||
BedMenuItem item = (BedMenuItem) adapter.getItems().get(i);
|
||||
item.setEnabled(hasSelection());
|
||||
if (item.isCheckable) {
|
||||
item.isChecked = false;
|
||||
}
|
||||
adapter.notifyItemChanged(i);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(runOnMainThread = true)
|
||||
public void onSelectionChanged(SelectedObjectChangedEvent e) {
|
||||
((BedMenuItem) adapter.getItems().get(2)).setEnabled(hasSelection());
|
||||
adapter.notifyItemChanged(2);
|
||||
((BedMenuItem) adapter.getItems().get(3)).setEnabled(hasSelection());
|
||||
adapter.notifyItemChanged(3);
|
||||
for (int i = 2; i <= 4; i++) {
|
||||
BedMenuItem item = (BedMenuItem) adapter.getItems().get(i);
|
||||
item.setEnabled(hasSelection());
|
||||
if (item.isCheckable) {
|
||||
item.isChecked = false;
|
||||
}
|
||||
adapter.notifyItemChanged(i);
|
||||
}
|
||||
}
|
||||
|
||||
public final class PositionMenu extends UnfoldMenu {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package ru.ytkab0bp.slicebeam.events;
|
||||
|
||||
import ru.ytkab0bp.eventbus.Event;
|
||||
|
||||
@Event
|
||||
public class FlattenModeResetEvent {
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import ru.ytkab0bp.slicebeam.components.bed_menu.OrientationMenu;
|
||||
import ru.ytkab0bp.slicebeam.components.bed_menu.SliceMenu;
|
||||
import ru.ytkab0bp.slicebeam.components.bed_menu.TransformMenu;
|
||||
import ru.ytkab0bp.slicebeam.config.ConfigObject;
|
||||
import ru.ytkab0bp.slicebeam.events.FlattenModeResetEvent;
|
||||
import ru.ytkab0bp.slicebeam.events.NeedSnackbarEvent;
|
||||
import ru.ytkab0bp.slicebeam.events.SlicingProgressEvent;
|
||||
import ru.ytkab0bp.slicebeam.navigation.Fragment;
|
||||
@@ -398,6 +399,10 @@ public class BedFragment extends Fragment {
|
||||
}
|
||||
|
||||
private void selectMenu(Context ctx, boolean portrait, int slot) {
|
||||
if (glView.getRenderer().resetFlattenMode()) {
|
||||
glView.requestRender();
|
||||
SliceBeam.EVENT_BUS.fireEvent(new FlattenModeResetEvent());
|
||||
}
|
||||
isAnimatingMenu = true;
|
||||
|
||||
BedMenu prevMenu = menuMap.get(currentMenuSlot);
|
||||
|
||||
@@ -3,6 +3,7 @@ package ru.ytkab0bp.slicebeam.render;
|
||||
import static android.opengl.GLES30.*;
|
||||
import static ru.ytkab0bp.slicebeam.utils.DebugUtils.assertTrue;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -72,6 +73,10 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
private Vec3d rotate = new Vec3d();
|
||||
private ArrayList<GLModel.HitResult> raycastHits = new ArrayList<>();
|
||||
|
||||
private Vec3d bbMin = new Vec3d(), bbMax = new Vec3d();
|
||||
private boolean isInFlattenMode;
|
||||
private ArrayList<GLModel> flattenPlanes = new ArrayList<>();
|
||||
|
||||
public Camera getCamera() {
|
||||
return camera;
|
||||
}
|
||||
@@ -165,6 +170,36 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean invalidateFlattenMode() {
|
||||
if (isInFlattenMode) {
|
||||
setInFlattenMode(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean resetFlattenMode() {
|
||||
if (isInFlattenMode) {
|
||||
setInFlattenMode(false);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setInFlattenMode(boolean inFlattenMode) {
|
||||
isInFlattenMode = inFlattenMode;
|
||||
|
||||
for (int i = 0, c = flattenPlanes.size(); i < c; i++) {
|
||||
flattenPlanes.get(i).release();
|
||||
}
|
||||
flattenPlanes.clear();
|
||||
|
||||
if (isInFlattenMode) {
|
||||
List<GLModel> planes = model.createFlattenPlanes(selectedObject);
|
||||
flattenPlanes.addAll(planes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDrawFrame(GL10 gl) {
|
||||
if (backgroundModel == null) return; // Not initialized yet
|
||||
@@ -266,12 +301,12 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
if (selected) {
|
||||
shader.stopUsing();
|
||||
|
||||
GLShaderProgram dash = GLShadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
GLShaderProgram flat = GLShadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
glLineWidth(ViewUtils.dp(1.5f));
|
||||
|
||||
dash.startUsing();
|
||||
dash.setUniformMatrix4fv("view_model_matrix", outModelMatrix);
|
||||
dash.setUniformMatrix4fv("projection_matrix", projectionMatrix);
|
||||
flat.startUsing();
|
||||
flat.setUniformMatrix4fv("view_model_matrix", outModelMatrix);
|
||||
flat.setUniformMatrix4fv("projection_matrix", projectionMatrix);
|
||||
|
||||
if (selectionModel == null) {
|
||||
selectionModel = new GLModel();
|
||||
@@ -280,7 +315,30 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
selectionModel.setColor(hoverColor);
|
||||
selectionModel.render();
|
||||
|
||||
dash.stopUsing();
|
||||
flat.stopUsing();
|
||||
|
||||
shader.startUsing();
|
||||
}
|
||||
|
||||
if (isInFlattenMode) {
|
||||
shader.stopUsing();
|
||||
|
||||
GLShaderProgram flat = GLShadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
|
||||
flat.startUsing();
|
||||
glEnable(GL_BLEND);
|
||||
flat.setUniformMatrix4fv("view_model_matrix", outModelMatrix);
|
||||
flat.setUniformMatrix4fv("projection_matrix", projectionMatrix);
|
||||
|
||||
for (GLModel plane : flattenPlanes) {
|
||||
boolean hoveringPlane = plane.isHovering;
|
||||
int clr = ColorUtils.blendARGB(hoverColor, color, hoveringPlane ? 1 : 0);
|
||||
plane.setColor(ColorUtils.setAlphaComponent(clr, (int) (Color.alpha(clr) * 0.75f)));
|
||||
plane.render();
|
||||
}
|
||||
|
||||
glDisable(GL_BLEND);
|
||||
flat.stopUsing();
|
||||
|
||||
shader.startUsing();
|
||||
}
|
||||
@@ -337,9 +395,55 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
if (isInFlattenMode && (j == selectedObject || j == -1)) {
|
||||
int minPlane = -1;
|
||||
double minDistancePlane = Double.MAX_VALUE;
|
||||
|
||||
for (int i = 0, c = flattenPlanes.size(); i < c; i++) {
|
||||
GLModel glModel = flattenPlanes.get(i);
|
||||
glModel.getRaycaster().raycast(this, raycastHits, x, y);
|
||||
|
||||
double minDistanceRay = Double.MAX_VALUE;
|
||||
if (!raycastHits.isEmpty()) {
|
||||
for (GLModel.HitResult res : raycastHits) {
|
||||
double distance = res.position.distance(camera.position);
|
||||
if (distance < minDistanceRay) {
|
||||
minDistanceRay = distance;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (minDistanceRay < minDistancePlane) {
|
||||
minDistancePlane = minDistanceRay;
|
||||
minPlane = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (minPlane != -1) {
|
||||
GLModel glModel = flattenPlanes.get(minPlane);
|
||||
model.flattenRotate(selectedObject, glModel);
|
||||
model.getBoundingBoxExact(j, bbMin, bbMax);
|
||||
model.translate(j, 0, 0, -bbMin.z);
|
||||
|
||||
invalidateGlModel(selectedObject);
|
||||
for (int k = 0, l = flattenPlanes.size(); k < l; k++) {
|
||||
flattenPlanes.get(k).release();
|
||||
}
|
||||
flattenPlanes.clear();
|
||||
|
||||
selectedObject = -1;
|
||||
SliceBeam.EVENT_BUS.fireEvent(new SelectedObjectChangedEvent());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean render = j != selectedObject || j != -1;
|
||||
selectedObject = j == selectedObject ? -1 : j;
|
||||
if (render) {
|
||||
if (isInFlattenMode) {
|
||||
setInFlattenMode(false);
|
||||
}
|
||||
if (selectedObject == -1) {
|
||||
selX = selY = selZ = 0;
|
||||
selRotX = selRotY = selRotZ = 0;
|
||||
@@ -386,6 +490,44 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
if (isInFlattenMode) {
|
||||
int minPlane = -1;
|
||||
double minDistancePlane = Double.MAX_VALUE;
|
||||
|
||||
for (int i = 0, c = flattenPlanes.size(); i < c; i++) {
|
||||
GLModel glModel = flattenPlanes.get(i);
|
||||
glModel.getRaycaster().raycast(this, raycastHits, x, y);
|
||||
|
||||
double minDistanceRay = Double.MAX_VALUE;
|
||||
if (!raycastHits.isEmpty()) {
|
||||
for (GLModel.HitResult res : raycastHits) {
|
||||
double distance = res.position.distance(camera.position);
|
||||
if (distance < minDistanceRay) {
|
||||
minDistanceRay = distance;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (minDistanceRay < minDistancePlane) {
|
||||
minDistancePlane = minDistanceRay;
|
||||
minPlane = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (minPlane != -1) {
|
||||
for (int i = 0; i < flattenPlanes.size(); i++) {
|
||||
flattenPlanes.get(i).isHovering = i == minPlane;
|
||||
}
|
||||
render = true;
|
||||
} else {
|
||||
for (int i = 0; i < flattenPlanes.size(); i++) {
|
||||
if (flattenPlanes.get(i).isHovering) {
|
||||
flattenPlanes.get(i).isHovering = false;
|
||||
render = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return render;
|
||||
}
|
||||
|
||||
@@ -499,5 +641,11 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
glModels.get(i).release();
|
||||
}
|
||||
glModels.clear();
|
||||
|
||||
isInFlattenMode = false;
|
||||
for (int i = 0; i < flattenPlanes.size(); i++) {
|
||||
flattenPlanes.get(i).release();
|
||||
}
|
||||
flattenPlanes.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,11 +156,19 @@ public class Bed3D {
|
||||
}
|
||||
|
||||
public void release() {
|
||||
Native.bed_release(pointer);
|
||||
axes.release();
|
||||
if (pointer != 0) {
|
||||
Native.bed_release(pointer);
|
||||
axes.release();
|
||||
pointer = 0;
|
||||
|
||||
// triangles.release();
|
||||
// gridlines.release();
|
||||
// contourlines.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package ru.ytkab0bp.slicebeam.slic3r;
|
||||
import java.io.File;
|
||||
|
||||
public class GCodeProcessorResult {
|
||||
final long pointer;
|
||||
long pointer;
|
||||
|
||||
public GCodeProcessorResult(File f) {
|
||||
pointer = Native.gcoderesult_load_file(f.getAbsolutePath(), f.getName());
|
||||
@@ -26,6 +26,14 @@ public class GCodeProcessorResult {
|
||||
}
|
||||
|
||||
public void release() {
|
||||
Native.gcoderesult_release(pointer);
|
||||
if (pointer != 0) {
|
||||
Native.gcoderesult_release(pointer);
|
||||
pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class GCodeViewer {
|
||||
}
|
||||
};
|
||||
|
||||
private final long pointer;
|
||||
private long pointer;
|
||||
|
||||
public GCodeViewer() {
|
||||
pointer = Native.vgcode_create();
|
||||
@@ -156,6 +156,14 @@ public class GCodeViewer {
|
||||
}
|
||||
|
||||
public void release() {
|
||||
Native.vgcode_release(pointer);
|
||||
if (pointer != 0) {
|
||||
Native.vgcode_release(pointer);
|
||||
pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import static ru.ytkab0bp.slicebeam.utils.DebugUtils.assertTrue;
|
||||
import android.graphics.Color;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ru.ytkab0bp.slicebeam.render.Camera;
|
||||
import ru.ytkab0bp.slicebeam.render.GLRenderer;
|
||||
@@ -13,7 +12,7 @@ import ru.ytkab0bp.slicebeam.utils.Prefs;
|
||||
import ru.ytkab0bp.slicebeam.utils.Vec3d;
|
||||
|
||||
public class GLModel {
|
||||
private long pointer;
|
||||
long pointer;
|
||||
private MeshRaycaster raycaster;
|
||||
|
||||
public float hoverProgress;
|
||||
@@ -74,7 +73,10 @@ public class GLModel {
|
||||
}
|
||||
|
||||
public void release() {
|
||||
Native.glmodel_release(pointer);
|
||||
if (pointer != 0) {
|
||||
Native.glmodel_release(pointer);
|
||||
pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public MeshRaycaster getRaycaster() {
|
||||
@@ -86,7 +88,7 @@ public class GLModel {
|
||||
}
|
||||
|
||||
public final class MeshRaycaster {
|
||||
public List<HitResult> raycast(GLRenderer renderer, ArrayList<HitResult> list, float x, float y) {
|
||||
public void raycast(GLRenderer renderer, ArrayList<HitResult> list, float x, float y) {
|
||||
assertTrue(renderer != null);
|
||||
list.clear();
|
||||
|
||||
@@ -104,7 +106,6 @@ public class GLModel {
|
||||
new Vec3d(v[i + 3], v[i + 4], v[i + 5])
|
||||
));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import ru.ytkab0bp.slicebeam.SliceBeam;
|
||||
import ru.ytkab0bp.slicebeam.utils.IOUtils;
|
||||
|
||||
public class GLShaderProgram {
|
||||
final long pointer;
|
||||
long pointer;
|
||||
private static ThreadLocal<FloatBuffer> matrixBuffer = new ThreadLocal<FloatBuffer>() {
|
||||
@Nullable
|
||||
@Override
|
||||
@@ -140,6 +140,14 @@ public class GLShaderProgram {
|
||||
}
|
||||
|
||||
public void release() {
|
||||
Native.shader_release(pointer);
|
||||
if (pointer != 0) {
|
||||
Native.shader_release(pointer);
|
||||
pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package ru.ytkab0bp.slicebeam.slic3r;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import ru.ytkab0bp.slicebeam.utils.Vec3d;
|
||||
|
||||
public class Model {
|
||||
public final String key = UUID.randomUUID().toString();
|
||||
final long pointer;
|
||||
long pointer;
|
||||
|
||||
private double[] boundingExact;
|
||||
private double[] boundingApprox;
|
||||
@@ -92,6 +94,10 @@ public class Model {
|
||||
Native.model_rotate(pointer, i, x, y, z);
|
||||
}
|
||||
|
||||
public void flattenRotate(int i, GLModel surface) {
|
||||
Native.model_flatten_rotate(pointer, i, surface.pointer);
|
||||
}
|
||||
|
||||
public int getObjectsCount() {
|
||||
return Native.model_get_objects_count(pointer);
|
||||
}
|
||||
@@ -128,12 +134,29 @@ public class Model {
|
||||
vec.set(tr[0], tr[1], tr[2]);
|
||||
}
|
||||
|
||||
public List<GLModel> createFlattenPlanes(int i) {
|
||||
long[] ptr = Native.model_create_flatten_planes(pointer, i);
|
||||
List<GLModel> list = new ArrayList<>(ptr.length);
|
||||
for (long l : ptr) {
|
||||
list.add(new GLModel(l));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public GCodeProcessorResult slice(String configPath, String gcodePath, SliceListener listener) throws Slic3rRuntimeError {
|
||||
return new GCodeProcessorResult(Native.model_slice(pointer, configPath, gcodePath, listener));
|
||||
}
|
||||
|
||||
public void release() {
|
||||
Native.model_release(pointer);
|
||||
if (pointer != 0) {
|
||||
Native.model_release(pointer);
|
||||
pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
|
||||
public static Model merge(Model... models) {
|
||||
|
||||
@@ -68,6 +68,8 @@ class Native {
|
||||
static native void model_translate_global(long ptr, double x, double y, double z);
|
||||
static native void model_scale(long ptr, int i, double x, double y, double z);
|
||||
static native void model_rotate(long ptr, int i, double x, double y, double z);
|
||||
static native void model_flatten_rotate(long ptr, int i, long surfacePtr);
|
||||
static native long[] model_create_flatten_planes(long ptr, int i);
|
||||
static native long model_slice(long ptr, String configPath, String path, SliceListener listener) throws Slic3rRuntimeError;
|
||||
static native void model_release(long ptr);
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "libslic3r/Geometry.hpp"
|
||||
#include "libslic3r/Arrange.hpp"
|
||||
#include "libslic3r/AABBMesh.hpp"
|
||||
#include "libslic3r/Geometry/ConvexHull.hpp"
|
||||
#include "Viewer.hpp"
|
||||
|
||||
#include "GLModel.hpp"
|
||||
@@ -26,6 +27,11 @@ using namespace Slic3r::GUI;
|
||||
|
||||
#define TAG "SB_Native"
|
||||
|
||||
struct PlaneData {
|
||||
std::vector<Vec3d> vertices;
|
||||
Vec3d normal;
|
||||
float area;
|
||||
};
|
||||
struct ModelRef {
|
||||
Model model;
|
||||
std::string base_name;
|
||||
@@ -35,6 +41,7 @@ struct GLModelRef {
|
||||
TriangleMesh mesh;
|
||||
AABBMesh* emesh;
|
||||
std::vector<stl_normal> normals;
|
||||
Vec3d flatten_normal;
|
||||
};
|
||||
struct ShaderRef {
|
||||
GLShaderProgram program;
|
||||
@@ -471,7 +478,37 @@ extern "C" {
|
||||
Vec3d vec(x, y, z);
|
||||
ModelVolumePtrs ptrs = model->model.objects[i]->volumes;
|
||||
for (int i = 0, c = ptrs.size(); i < c; i++) {
|
||||
ptrs[i]->set_rotation(ptrs[i]->get_rotation() + vec);
|
||||
Vec3d current_rotation = ptrs[i]->get_rotation();
|
||||
|
||||
Eigen::Quaterniond q_current =
|
||||
Eigen::AngleAxisd(current_rotation[2], Eigen::Vector3d::UnitZ()) *
|
||||
Eigen::AngleAxisd(current_rotation[1], Eigen::Vector3d::UnitY()) *
|
||||
Eigen::AngleAxisd(current_rotation[0], Eigen::Vector3d::UnitX());
|
||||
|
||||
Eigen::Quaterniond q_delta =
|
||||
Eigen::AngleAxisd(vec[0], Eigen::Vector3d::UnitX()) *
|
||||
Eigen::AngleAxisd(vec[1], Eigen::Vector3d::UnitY()) *
|
||||
Eigen::AngleAxisd(vec[2], Eigen::Vector3d::UnitZ());
|
||||
|
||||
Eigen::Quaterniond q_result = q_delta * q_current;
|
||||
Eigen::Vector3d new_rotation = q_result.toRotationMatrix().eulerAngles(2, 1, 0);
|
||||
ptrs[i]->set_rotation(Vec3d(new_rotation[2], new_rotation[1], new_rotation[0]));
|
||||
}
|
||||
model->model.objects[i]->invalidate_bounding_box();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_ru_ytkab0bp_slicebeam_slic3r_Native_model_1flatten_1rotate(JNIEnv* env, jclass, jlong ptr, jint i, jlong surface_ptr) {
|
||||
ModelRef* model = (ModelRef *) (intptr_t) ptr;
|
||||
GLModelRef* surface = (GLModelRef*) (intptr_t) surface_ptr;
|
||||
|
||||
const Vec3d& normal = surface->flatten_normal;
|
||||
ModelVolumePtrs ptrs = model->model.objects[i]->volumes;
|
||||
for (int i = 0, c = ptrs.size(); i < c; i++) {
|
||||
auto vol = ptrs[i];
|
||||
const Geometry::Transformation& old_transform = vol->get_transformation();
|
||||
const Vec3d tnormal = normal;
|
||||
const Transform3d rotation_matrix = Transform3d(Eigen::Quaterniond().setFromTwoVectors(tnormal, -Vec3d::UnitZ()));
|
||||
vol->set_transformation(old_transform.get_offset_matrix() * rotation_matrix * old_transform.get_matrix_no_offset());
|
||||
}
|
||||
model->model.objects[i]->invalidate_bounding_box();
|
||||
}
|
||||
@@ -537,6 +574,237 @@ extern "C" {
|
||||
return arr;
|
||||
}
|
||||
|
||||
JNIEXPORT jlongArray JNICALL Java_ru_ytkab0bp_slicebeam_slic3r_Native_model_1create_1flatten_1planes(JNIEnv* env, jclass, jlong ptr, jint i) {
|
||||
ModelRef* ref = (ModelRef*) (intptr_t) ptr;
|
||||
const ModelObject* mo = ref->model.objects[i];
|
||||
TriangleMesh ch;
|
||||
Transform3d real_transform = Geometry::translation_transform(mo->bounding_box_exact().center());
|
||||
for (const ModelVolume* vol : mo->volumes) {
|
||||
if (vol->type() != ModelVolumeType::MODEL_PART)
|
||||
continue;
|
||||
TriangleMesh vol_ch = vol->get_convex_hull();
|
||||
vol_ch.transform(vol->get_matrix_no_offset());
|
||||
vol_ch.transform(real_transform);
|
||||
ch.merge(vol_ch);
|
||||
}
|
||||
ch = ch.convex_hull_3d();
|
||||
|
||||
std::vector<PlaneData> m_planes;
|
||||
|
||||
const Transform3d inst_matrix = mo->instances.front()->get_matrix_no_offset();
|
||||
|
||||
// Following constants are used for discarding too small polygons.
|
||||
const float minimal_area = 5.f; // in square mm (world coordinates)
|
||||
const float minimal_side = 1.f; // mm
|
||||
|
||||
const int num_of_facets = ch.facets_count();
|
||||
const std::vector<Vec3f> face_normals = its_face_normals(ch.its);
|
||||
const std::vector<Vec3i> face_neighbors = its_face_neighbors(ch.its);
|
||||
std::vector<int> facet_queue(num_of_facets, 0);
|
||||
std::vector<bool> facet_visited(num_of_facets, false);
|
||||
int facet_queue_cnt = 0;
|
||||
const stl_normal* normal_ptr = nullptr;
|
||||
int facet_idx = 0;
|
||||
while (true) {
|
||||
// Find next unvisited triangle:
|
||||
for (; facet_idx < num_of_facets; ++ facet_idx)
|
||||
if (!facet_visited[facet_idx]) {
|
||||
facet_queue[facet_queue_cnt ++] = facet_idx;
|
||||
facet_visited[facet_idx] = true;
|
||||
normal_ptr = &face_normals[facet_idx];
|
||||
m_planes.emplace_back();
|
||||
break;
|
||||
}
|
||||
if (facet_idx == num_of_facets)
|
||||
break; // Everything was visited already
|
||||
|
||||
while (facet_queue_cnt > 0) {
|
||||
int facet_idx = facet_queue[-- facet_queue_cnt];
|
||||
const stl_normal& this_normal = face_normals[facet_idx];
|
||||
if (std::abs(this_normal(0) - (*normal_ptr)(0)) < 0.001 && std::abs(this_normal(1) - (*normal_ptr)(1)) < 0.001 && std::abs(this_normal(2) - (*normal_ptr)(2)) < 0.001) {
|
||||
const Vec3i face = ch.its.indices[facet_idx];
|
||||
for (int j=0; j<3; ++j)
|
||||
m_planes.back().vertices.emplace_back(ch.its.vertices[face[j]].cast<double>());
|
||||
|
||||
facet_visited[facet_idx] = true;
|
||||
for (int j = 0; j < 3; ++ j)
|
||||
if (int neighbor_idx = face_neighbors[facet_idx][j]; neighbor_idx >= 0 && ! facet_visited[neighbor_idx])
|
||||
facet_queue[facet_queue_cnt ++] = neighbor_idx;
|
||||
}
|
||||
}
|
||||
m_planes.back().normal = normal_ptr->cast<double>();
|
||||
|
||||
Pointf3s& verts = m_planes.back().vertices;
|
||||
// Now we'll transform all the points into world coordinates, so that the areas, angles and distances
|
||||
// make real sense.
|
||||
verts = transform(verts, inst_matrix);
|
||||
|
||||
// if this is a just a very small triangle, remove it to speed up further calculations (it would be rejected later anyway):
|
||||
if (verts.size() == 3 &&
|
||||
((verts[0] - verts[1]).norm() < minimal_side
|
||||
|| (verts[0] - verts[2]).norm() < minimal_side
|
||||
|| (verts[1] - verts[2]).norm() < minimal_side))
|
||||
m_planes.pop_back();
|
||||
}
|
||||
|
||||
// Let's prepare transformation of the normal vector from mesh to instance coordinates.
|
||||
const Matrix3d normal_matrix = inst_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
|
||||
|
||||
// Now we'll go through all the polygons, transform the points into xy plane to process them:
|
||||
for (unsigned int polygon_id=0; polygon_id < m_planes.size(); ++polygon_id) {
|
||||
Pointf3s& polygon = m_planes[polygon_id].vertices;
|
||||
const Vec3d& normal = m_planes[polygon_id].normal;
|
||||
|
||||
// transform the normal according to the instance matrix:
|
||||
const Vec3d normal_transformed = normal_matrix * normal;
|
||||
|
||||
// We are going to rotate about z and y to flatten the plane
|
||||
Eigen::Quaterniond q;
|
||||
Transform3d m = Transform3d::Identity();
|
||||
m.matrix().block(0, 0, 3, 3) = q.setFromTwoVectors(normal_transformed, Vec3d::UnitZ()).toRotationMatrix();
|
||||
polygon = transform(polygon, m);
|
||||
|
||||
// Now to remove the inner points. We'll misuse Geometry::convex_hull for that, but since
|
||||
// it works in fixed point representation, we will rescale the polygon to avoid overflows.
|
||||
// And yes, it is a nasty thing to do. Whoever has time is free to refactor.
|
||||
Vec3d bb_size = BoundingBoxf3(polygon).size();
|
||||
float sf = std::min(1./bb_size(0), 1./bb_size(1));
|
||||
Transform3d tr = Geometry::scale_transform({ sf, sf, 1.f });
|
||||
polygon = transform(polygon, tr);
|
||||
polygon = Slic3r::Geometry::convex_hull(polygon);
|
||||
polygon = transform(polygon, tr.inverse());
|
||||
|
||||
// Calculate area of the polygons and discard ones that are too small
|
||||
float& area = m_planes[polygon_id].area;
|
||||
area = 0.f;
|
||||
for (unsigned int i = 0; i < polygon.size(); i++) // Shoelace formula
|
||||
area += polygon[i](0)*polygon[i + 1 < polygon.size() ? i + 1 : 0](1) - polygon[i + 1 < polygon.size() ? i + 1 : 0](0)*polygon[i](1);
|
||||
area = 0.5f * std::abs(area);
|
||||
|
||||
bool discard = false;
|
||||
if (area < minimal_area)
|
||||
discard = true;
|
||||
else {
|
||||
// We also check the inner angles and discard polygons with angles smaller than the following threshold
|
||||
const double angle_threshold = ::cos(10.0 * (double)PI / 180.0);
|
||||
|
||||
for (unsigned int i = 0; i < polygon.size(); ++i) {
|
||||
const Vec3d& prec = polygon[(i == 0) ? polygon.size() - 1 : i - 1];
|
||||
const Vec3d& curr = polygon[i];
|
||||
const Vec3d& next = polygon[(i == polygon.size() - 1) ? 0 : i + 1];
|
||||
|
||||
if ((prec - curr).normalized().dot((next - curr).normalized()) > angle_threshold) {
|
||||
discard = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (discard) {
|
||||
m_planes[polygon_id--] = std::move(m_planes.back());
|
||||
m_planes.pop_back();
|
||||
continue;
|
||||
}
|
||||
|
||||
// We will shrink the polygon a little bit so it does not touch the object edges:
|
||||
Vec3d centroid = std::accumulate(polygon.begin(), polygon.end(), Vec3d(0.0, 0.0, 0.0));
|
||||
centroid /= (double)polygon.size();
|
||||
for (auto& vertex : polygon)
|
||||
vertex = 0.9f*vertex + 0.1f*centroid;
|
||||
|
||||
// Polygon is now simple and convex, we'll round the corners to make them look nicer.
|
||||
// The algorithm takes a vertex, calculates middles of respective sides and moves the vertex
|
||||
// towards their average (controlled by 'aggressivity'). This is repeated k times.
|
||||
// In next iterations, the neighbours are not always taken at the middle (to increase the
|
||||
// rounding effect at the corners, where we need it most).
|
||||
const unsigned int k = 10; // number of iterations
|
||||
const float aggressivity = 0.2f; // agressivity
|
||||
const unsigned int N = polygon.size();
|
||||
std::vector<std::pair<unsigned int, unsigned int>> neighbours;
|
||||
if (k != 0) {
|
||||
Pointf3s points_out(2*k*N); // vector long enough to store the future vertices
|
||||
for (unsigned int j=0; j<N; ++j) {
|
||||
points_out[j*2*k] = polygon[j];
|
||||
neighbours.push_back(std::make_pair((int)(j*2*k-k) < 0 ? (N-1)*2*k+k : j*2*k-k, j*2*k+k));
|
||||
}
|
||||
|
||||
for (unsigned int i=0; i<k; ++i) {
|
||||
// Calculate middle of each edge so that neighbours points to something useful:
|
||||
for (unsigned int j=0; j<N; ++j)
|
||||
if (i==0)
|
||||
points_out[j*2*k+k] = 0.5f * (points_out[j*2*k] + points_out[j==N-1 ? 0 : (j+1)*2*k]);
|
||||
else {
|
||||
float r = 0.2+0.3/(k-1)*i; // the neighbours are not always taken in the middle
|
||||
points_out[neighbours[j].first] = r*points_out[j*2*k] + (1-r) * points_out[neighbours[j].first-1];
|
||||
points_out[neighbours[j].second] = r*points_out[j*2*k] + (1-r) * points_out[neighbours[j].second+1];
|
||||
}
|
||||
// Now we have a triangle and valid neighbours, we can do an iteration:
|
||||
for (unsigned int j=0; j<N; ++j)
|
||||
points_out[2*k*j] = (1-aggressivity) * points_out[2*k*j] +
|
||||
aggressivity*0.5f*(points_out[neighbours[j].first] + points_out[neighbours[j].second]);
|
||||
|
||||
for (auto& n : neighbours) {
|
||||
++n.first;
|
||||
--n.second;
|
||||
}
|
||||
}
|
||||
polygon = points_out; // replace the coarse polygon with the smooth one that we just created
|
||||
}
|
||||
|
||||
|
||||
// Raise a bit above the object surface to avoid flickering:
|
||||
for (auto& b : polygon)
|
||||
b(2) += 0.1f;
|
||||
|
||||
// Transform back to 3D (and also back to mesh coordinates)
|
||||
polygon = transform(polygon, inst_matrix.inverse() * m.inverse());
|
||||
}
|
||||
|
||||
// We'll sort the planes by area and only keep the 254 largest ones (because of the picking pass limitations):
|
||||
std::sort(m_planes.rbegin(), m_planes.rend(), [](const PlaneData& a, const PlaneData& b) { return a.area < b.area; });
|
||||
m_planes.resize(std::min((int)m_planes.size(), 254));
|
||||
|
||||
jlongArray arr = env->NewLongArray(m_planes.size());
|
||||
|
||||
// And finally create respective VBOs. The polygon is convex with
|
||||
// the vertices in order, so triangulation is trivial.
|
||||
for (int i = 0, s = m_planes.size(); i < s; i++) {
|
||||
auto& plane = m_planes[i];
|
||||
indexed_triangle_set its;
|
||||
its.vertices.reserve(plane.vertices.size());
|
||||
its.indices.reserve(plane.vertices.size() / 3);
|
||||
for (size_t i = 0; i < plane.vertices.size(); ++i) {
|
||||
its.vertices.emplace_back((Vec3f)plane.vertices[i].cast<float>());
|
||||
}
|
||||
for (size_t i = 1; i < plane.vertices.size() - 1; ++i) {
|
||||
its.indices.emplace_back(0, i, i + 1); // triangle fan
|
||||
}
|
||||
|
||||
if (Geometry::Transformation(inst_matrix).is_left_handed()) {
|
||||
// we need to swap face normals in case the object is mirrored
|
||||
// for the raycaster to work properly
|
||||
for (stl_triangle_vertex_indices& face : its.indices) {
|
||||
if (its_face_normal(its, face).cast<double>().dot(plane.normal) < 0.0)
|
||||
std::swap(face[1], face[2]);
|
||||
}
|
||||
}
|
||||
GLModelRef* ref = new GLModelRef();
|
||||
ref->mesh = TriangleMesh(its);
|
||||
ref->model.init_from(its);
|
||||
ref->flatten_normal = plane.normal;
|
||||
ref->emesh = new AABBMesh(its, true);
|
||||
ref->normals = its_face_normals(its);
|
||||
|
||||
jlong ptr = reinterpret_cast<jlong>(ref);
|
||||
env->SetLongArrayRegion(arr, i, 1, &ptr);
|
||||
|
||||
// vertices are no more needed, clear memory
|
||||
plane.vertices = std::vector<Vec3d>();
|
||||
}
|
||||
m_planes.clear();
|
||||
return arr;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_ru_ytkab0bp_slicebeam_slic3r_Native_model_1slice(JNIEnv* env, jclass, jlong ptr, jstring configPath, jstring path, jobject listener) {
|
||||
try {
|
||||
ModelRef* model = (ModelRef*) (intptr_t) ptr;
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
<string name="MenuOrientation">Ориентация</string>
|
||||
<string name="MenuOrientationArrange">Расст. модели</string>
|
||||
<string name="MenuOrientationArrangeFinished">Модели расставлены.</string>
|
||||
<string name="MenuOrientationFlatten">Поверхн.</string>
|
||||
<string name="MenuOrientationPosition">Позиция</string>
|
||||
<string name="MenuOrientationPositionX">X</string>
|
||||
<string name="MenuOrientationPositionY">Y</string>
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
<string name="MenuOrientation">Orientation</string>
|
||||
<string name="MenuOrientationArrange">Arrange models</string>
|
||||
<string name="MenuOrientationArrangeFinished">Models arranged.</string>
|
||||
<string name="MenuOrientationFlatten">Surface</string>
|
||||
<string name="MenuOrientationPosition">Position</string>
|
||||
<string name="MenuOrientationPositionX">X</string>
|
||||
<string name="MenuOrientationPositionY">Y</string>
|
||||
|
||||
Reference in New Issue
Block a user