From: Marko Luukkainen Date: Wed, 23 Oct 2019 15:52:06 +0000 (+0300) Subject: Highlight constrain points X-Git-Tag: v1.43.0~174 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2a76147e3fc37ee609dd343c607cb51ae9b8b4e2;p=simantics%2F3d.git Highlight constrain points gitlab #32 Change-Id: I6e2d36c9ac08040da8a46a67a85bc5a216a1cb08 --- 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 00000000..379424ea Binary files /dev/null and b/org.simantics.plant3d/icons/crosshair.png differ 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(); + + } + + + + +}