From 2a76147e3fc37ee609dd343c607cb51ae9b8b4e2 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Wed, 23 Oct 2019 18:52:06 +0300 Subject: [PATCH] Highlight constrain points gitlab #32 Change-Id: I6e2d36c9ac08040da8a46a67a85bc5a216a1cb08 --- .../g3d/tools/ConstraintDetector.java | 8 - org.simantics.plant3d/icons/crosshair.png | Bin 0 -> 661 bytes org.simantics.plant3d/icons/crosshair.svg | 78 +++++++ .../plant3d/actions/RoutePipeAction.java | 17 ++ .../plant3d/gizmo/ConstraintPointGizmo.java | 215 ++++++++++++++++++ 5 files changed, 310 insertions(+), 8 deletions(-) create mode 100644 org.simantics.plant3d/icons/crosshair.png create mode 100644 org.simantics.plant3d/icons/crosshair.svg create mode 100644 org.simantics.plant3d/src/org/simantics/plant3d/gizmo/ConstraintPointGizmo.java diff --git a/org.simantics.g3d/src/org/simantics/g3d/tools/ConstraintDetector.java b/org.simantics.g3d/src/org/simantics/g3d/tools/ConstraintDetector.java index 51e1a7c3..f1ed4ac4 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/tools/ConstraintDetector.java +++ b/org.simantics.g3d/src/org/simantics/g3d/tools/ConstraintDetector.java @@ -39,14 +39,6 @@ public abstract class ConstraintDetector { protected Color4d yColor = new Color4d(0.f,1.f,0.f,1.f); protected Color4d zColor = new Color4d(0.f,0.f,1.f,1.f); - -// public ConstraintDetector(ThreeDimensionalEditorBase editor) { -// this.editor = editor; -// ms = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState(); -// ms.setEmissive(new ColorRGBA(1.f,1.f,1.f,1.f)); -// ms.setColorMaterial(MaterialState.CM_EMISSIVE); -// } - public void clearConstraints() { //System.out.println("ConstraintDetector.clearConstraints()"); diff --git a/org.simantics.plant3d/icons/crosshair.png b/org.simantics.plant3d/icons/crosshair.png new file mode 100644 index 0000000000000000000000000000000000000000..379424ea7545e074b949490baca6eb300352ace3 GIT binary patch literal 661 zcmV;G0&4wR9J=0 zmoZNpK@f#MBETSns0~P|5?NSspH#F_04Y)`3TX2$qWlZmq##1+3{l$r21`T^X~C|M zjQ~-YV&2BXzJPSRZ@30rHC>)EksvemFRu#_~e%mKSPn>>_sDO&%MS;y79B0K(X6?XJ3K-gfSq38& z>};RM4x#A55nyu+0VUv-di*`M|BGqpn~7aa-4TPMH3cb#q>An8AK=g>!yRZVu8q+= z6^^s*Cqn_9e;}aaC$n~79h+)JVM&~RkEnny_Uz}dky;eC#n~coOR;~!32=?g^Lhcz z>*Bf=_Yh(Nt}rJ9HqTckXucv2#QDIYWt{`3z%O6{_&~m$sO#DVaYe1!C`Z&#+yLHT zh9b0NG5D4YHE(1U&m?>qjWVTss`m@=e8`K|a}*rwVWgKTP(O v|I8DAm^gHS-&!!3YKQUi$1(M3ICTN{x@CL|H`YoB00000NkvXXu0mjf^`#ut literal 0 HcmV?d00001 diff --git a/org.simantics.plant3d/icons/crosshair.svg b/org.simantics.plant3d/icons/crosshair.svg new file mode 100644 index 00000000..8c73e69d --- /dev/null +++ b/org.simantics.plant3d/icons/crosshair.svg @@ -0,0 +1,78 @@ + + + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java b/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java index 64d2d8c6..5c787037 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java @@ -23,6 +23,7 @@ import org.simantics.g3d.vtk.swt.InteractiveVtkComposite; import org.simantics.g3d.vtk.swt.vtkSwtAction; import org.simantics.g3d.vtk.utils.vtkUtil; import org.simantics.plant3d.Activator; +import org.simantics.plant3d.gizmo.ConstraintPointGizmo; import org.simantics.plant3d.gizmo.SplitPointSelectionGizmo; import org.simantics.plant3d.gizmo.TerminalSelectionGizmo; import org.simantics.plant3d.scenegraph.EndComponent; @@ -61,6 +62,7 @@ public class RoutePipeAction extends vtkSwtAction { private TranslateAxisGizmo translateAxisGizmo = new TranslateAxisGizmo(); private SplitPointSelectionGizmo splitPointSelectionGizmo; + private ConstraintPointGizmo constraintPointGizmo; private TerminalSelectionGizmo terminalSelectionGizmo; private NodeMap nodeMap; @@ -93,6 +95,7 @@ public class RoutePipeAction extends vtkSwtAction { nodeMap = root.getNodeMap(); splitPointSelectionGizmo = new SplitPointSelectionGizmo(panel); terminalSelectionGizmo = new TerminalSelectionGizmo(panel); + constraintPointGizmo = new ConstraintPointGizmo(panel); detector = new org.simantics.g3d.vtk.swt.ConstraintDetector(panel); } @@ -151,6 +154,8 @@ public class RoutePipeAction extends vtkSwtAction { splitPointSelectionGizmo.deattach(); if (terminalSelectionGizmo.isAttached()) terminalSelectionGizmo.deattach(); + if (constraintPointGizmo.isAttached()) + constraintPointGizmo.deattach(); if (infoActor != null) { panel.getRenderer().RemoveActor(infoActor); infoActor.Delete(); @@ -595,7 +600,10 @@ public class RoutePipeAction extends vtkSwtAction { private void updateConstraints() { detector.clearConstraints(); + constraintPointGizmo.clearPositions(); if (hoverObject == null) { + if (constraintPointGizmo.isAttached()) + constraintPointGizmo.deattach(); return; } if (hoverObject instanceof Nozzle) { @@ -608,10 +616,19 @@ public class RoutePipeAction extends vtkSwtAction { c.getEnds(p1, p2); detector.addContraintPoint(p1); detector.addContraintPoint(p2); + detector.addContraintPoint(new Point3d(c.getWorldPosition())); } else if (hoverObject instanceof TurnComponent) { TurnComponent n = (TurnComponent)hoverObject; detector.addContraintPoint(new Point3d(n.getWorldPosition())); } + if (detector.getConstraintPoints().size() > 0) { + for (Point3d p : detector.getConstraintPoints()) { + constraintPointGizmo.addPosition(new Vector3d(p)); + } + if (constraintPointGizmo.isAttached()) + constraintPointGizmo.deattach(); + constraintPointGizmo.attach(panel); + } } @Override diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/ConstraintPointGizmo.java b/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/ConstraintPointGizmo.java new file mode 100644 index 00000000..44d158f6 --- /dev/null +++ b/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/ConstraintPointGizmo.java @@ -0,0 +1,215 @@ +package org.simantics.plant3d.gizmo; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.vecmath.Point2d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.scenegraph.RenderListener; +import org.simantics.g3d.tools.PluginTools; +import org.simantics.g3d.vtk.common.VtkView; +import org.simantics.g3d.vtk.gizmo.vtkGizmo; +import org.simantics.g3d.vtk.utils.vtkUtil; +import org.simantics.plant3d.Activator; + +import vtk.vtkCellArray; +import vtk.vtkFloatArray; +import vtk.vtkPNGReader; +import vtk.vtkPoints; +import vtk.vtkPolyData; +import vtk.vtkPolyDataMapper2D; +import vtk.vtkPolygon; +import vtk.vtkProp; +import vtk.vtkRenderer; +import vtk.vtkTexture; +import vtk.vtkTexturedActor2D; + +public class ConstraintPointGizmo extends vtkGizmo { + + List props; + List positions; + + VtkView panel; + + private RenderListener listener; + + public ConstraintPointGizmo(VtkView panel) { + this.panel = panel; + props = new ArrayList<>(); + positions = new ArrayList<>(); + + this.listener = new RenderListener() { + + @Override + public void preRender() { + for (int i = 0; i < positions.size(); i++) { + Vector3d pos = positions.get(i); + vtkTexturedActor2D prop = props.get(i); + Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), pos); + prop.SetDisplayPosition((int)p.x, (int)p.y); + } + } + + @Override + public void postRender() { + + + } + }; + + } + + vtkRenderer getRenderer() { + return panel.getRenderer(); + } + + + @Override + public void attach(VtkView renderingPart) { + + panel.addListener(listener); + + super.attach(renderingPart); + + } + + @Override + public Collection getGizmo() { + List list = new ArrayList(); + for (vtkTexturedActor2D p : props) { + list.add(p); + } + return list; + } + + protected void attachActors() { + panel.lock(); + vtkRenderer ren = getRenderer(); + while (props.size() < positions.size()) + props.add(createActor()); + for (int i = 0; i < positions.size(); i++) { + vtkTexturedActor2D p = props.get(i); + ren.AddActor2D(p); + } + panel.unlock(); + } + + @Override + protected void deattachActors() { + panel.removeListener(listener); + panel.lock(); + vtkRenderer ren = getRenderer(); + for (vtkTexturedActor2D p : props) { + ren.RemoveActor2D(p); + } + panel.unlock(); + } + + + public void addPosition(Vector3d position) { + positions.add(position); + } + + public void clearPositions() { + positions.clear(); + } + + vtkPolyDataMapper2D mapper; + vtkTexture plusTex; + + double pw = 8; + + private vtkTexturedActor2D createActor() { + if (mapper == null) { + loadData(); + } + vtkTexturedActor2D nextProp = new vtkTexturedActor2D(); + + nextProp.SetMapper(mapper); + nextProp.SetTexture(plusTex); + nextProp.SetPickable(0); + nextProp.SetWidth(pw); + nextProp.SetHeight(pw); + nextProp.GetProperty().SetColor(new double[] {255.0,0.0,255.0}); + nextProp.GetProperty().Delete(); + + panel.addDeletable(nextProp); + + return nextProp; + } + + public void dispose() { + + } + + + private void loadData() { + String plusTexFile = PluginTools.getAbsolutePath(Activator.getDefault().getBundle(), "icons/crosshair.png"); + if (plusTexFile == null) + throw new RuntimeException("Cannot resolve required image files."); + + vtkPoints points = new vtkPoints(); + points.InsertNextPoint(-pw, -pw, 0.0); + points.InsertNextPoint( pw, -pw, 0.0); + points.InsertNextPoint( pw, pw, 0.0); + points.InsertNextPoint(-pw, pw, 0.0); + + + vtkCellArray cellArray = new vtkCellArray(); + vtkPolygon polygon = new vtkPolygon(); + polygon.GetPointIds().SetNumberOfIds(4); + polygon.GetPointIds().SetId(0, 0); + polygon.GetPointIds().SetId(1, 1); + polygon.GetPointIds().SetId(2, 2); + polygon.GetPointIds().SetId(3, 3); + + cellArray.InsertNextCell(polygon); + + vtkPolyData quad = new vtkPolyData(); + quad.SetPoints(points); + quad.SetPolys(cellArray); + + vtkFloatArray texCoords = new vtkFloatArray(); + + texCoords.SetNumberOfComponents(2); + + texCoords.InsertNextTuple2(0.0, 0.0); + texCoords.InsertNextTuple2(1.0, 0.0); + texCoords.InsertNextTuple2(1.0, 1.0); + texCoords.InsertNextTuple2(0.0, 1.0); + + quad.GetPointData().SetTCoords(texCoords); + + + vtkPNGReader plusReader = new vtkPNGReader(); + plusReader.SetFileName(plusTexFile); + + plusTex = new vtkTexture(); + plusTex.SetInputConnection(plusReader.GetOutputPort()); + plusTex.SetInterpolate(1); + + mapper = new vtkPolyDataMapper2D(); + mapper.SetInputData(quad); + + panel.addDeletable(mapper); + panel.addDeletable(plusTex); + + + plusReader.GetOutputPort().Delete(); + plusReader.Delete(); + quad.GetPointData().Delete(); + quad.Delete(); + points.Delete(); + polygon.GetPointIds().Delete(); + polygon.Delete(); + cellArray.Delete(); + texCoords.Delete(); + + } + + + + +} -- 2.47.1