]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/wheel/RotatorHandler.java
Remove all dependencies on javax.vecmath.
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / wheel / RotatorHandler.java
index 53df784f0b88a612227afd9e74bc2432f9bd22f7..f364f5040b05a908bd4bbdc04109ab1fd85eb08d 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g2d.elementclass.wheel;\r
-\r
-import java.awt.geom.Point2D;\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import javax.vecmath.Vector2d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g2d.canvas.ICanvasContext;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.handler.InternalSize;\r
-import org.simantics.g2d.element.handler.Stateful;\r
-import org.simantics.g2d.element.handler.HandleMouseEvent;\r
-import org.simantics.g2d.element.handler.Heartbeat;\r
-import org.simantics.g2d.element.handler.LifeCycle;\r
-import org.simantics.g2d.element.handler.Rotate;\r
-import org.simantics.g2d.element.handler.impl.AbstractGrabbable;\r
-import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
-import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
-\r
-/**\r
- * This handlerer rotates element when it is touched.\r
- * \r
- * Hints used:\r
- *    KEY_VALUE\r
- *    KEY_MIN_VALUE                    optional\r
- *    KEY_MAX_VALUE                    optional\r
- *    KEY_FRICTION\r
- *    KEY_FACTOR\r
- *    KEY_GRAB_FRICTION\r
- *    KEY_MIN_POINTERS\r
- *    KEY_LOCKED\r
- * \r
- * @author Toni Kalajainen\r
- */\r
-public class RotatorHandler extends AbstractGrabbable implements HandleMouseEvent, LifeCycle, Heartbeat {\r
-\r
-       private static final long serialVersionUID = -3588513675880482627L;\r
-\r
-       public static final RotatorHandler INSTANCE = new RotatorHandler(); \r
-       \r
-       public static final double STRAY_DISTANCE = 1000;       \r
-       \r
-       public double initial_friction = 0.20;\r
-       public double initial_grab_friction = 0.99;\r
-       public double initial_factor = 0.025;\r
-       // When ang vel goes under tolerance, the motion is considered stopped\r
-       public double angVelTolerance = 0.5; \r
-       \r
-       /** Angular velocity, canvas specific variable */\r
-       public static final Key KEY_ANG_VEL = new KeyOf(Double.class);\r
-       /** Minimum number of pointers */\r
-       public static final Key KEY_MIN_POINTERS = new KeyOf(Integer.class);    \r
-       public static final Key KEY_GRAB_FRICTION = new KeyOf(Double.class);    \r
-       public static final Key KEY_FRICTION = new KeyOf(Double.class);\r
-       public static final Key KEY_FACTOR = new KeyOf(Double.class);\r
-\r
-       public RotatorHandler() {\r
-               super(1000.0);\r
-       }\r
-\r
-       @Override\r
-       protected void onDrag(GrabInfo gi, ICanvasContext ctx) {\r
-               IElement        e       = gi.e;\r
-               Point2D origo   = getOrigo(e, null);            \r
-               Point2D prevPos = gi.prevPosElement;\r
-               Point2D p               = gi.dragPosElement;\r
-               \r
-               // ---- Wheel is held! ----             \r
-               // position vector 0\r
-               Vector2d p0 = new Vector2d(prevPos.getX(), prevPos.getY());\r
-               // position vector 1\r
-               Vector2d p1 = new Vector2d(p.getX(), p.getY());\r
-               // motion vector\r
-               Vector2d f = new Vector2d(p1);\r
-               f.sub(p0);\r
-               // no movement\r
-               if (f.length()==0) return;\r
-               \r
-               // -- We are holding the wheel and we have moved the pointer --\r
-\r
-               // vector from origo to mouse\r
-               Vector2d odp0 = new Vector2d(p0.x - origo.getX(), p0.y - origo.getY());\r
-               Vector2d odp1 = new Vector2d(p1.x - origo.getX(), p1.y - origo.getY());\r
-               // convert motion into tangential and normal vectors            \r
-               // normal vector of the motion\r
-               Vector2d fn = new Vector2d(odp0);\r
-               fn.scale( f.dot(odp0) / odp0.lengthSquared() );\r
-               // tangential vector of the motion\r
-               Vector2d ft = new Vector2d(f);\r
-               ft.sub(fn);\r
-               \r
-               // momentum             \r
-               Vector3d r = new Vector3d(odp0.x, odp0.y, 0);\r
-               Vector3d F = new Vector3d(ft.x, ft.y, 0);\r
-               Vector3d M = new Vector3d();\r
-               M.cross(r, F);\r
-               if (!isGrabbed(e, ctx)) return;\r
-               \r
-               // Turn the wheel\r
-               double deltaAngle = odp0.angle(odp1);\r
-               if (M.z<0) deltaAngle *= -1;\r
-               \r
-               double deltaAngularVelocity = deltaAngle * 20;\r
-               setAngVel(e, getAngVel(e)+deltaAngularVelocity);\r
-\r
-               deltaAngle *= 0.297;\r
-               modifyValue(e, ctx, deltaAngle);                                \r
-       }\r
-\r
-       @Override\r
-       protected void onGrab(GrabInfo gi, ICanvasContext ctx) {\r
-       }\r
-\r
-       @Override\r
-       protected void onGrabCancel(GrabInfo gi, ICanvasContext ctx) {\r
-       }\r
-\r
-       @Override\r
-       protected boolean onGrabCheck(IElement e, ICanvasContext ctx, int pointerId, Point2D pickPos) {\r
-               return isEnabled(e);\r
-       }\r
-\r
-       @Override\r
-       protected void onRelease(GrabInfo gi, ICanvasContext ctx) {\r
-       }\r
-\r
-       @Override\r
-       public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {\r
-               \r
-        boolean isGrabbed = isGrabbed(e, ctx);\r
-        boolean isMoving = isMoving(e);\r
-        boolean isLocked = isLocked(e);\r
-               \r
-\r
-        // Check if the value has changed in the value source \r
-        if ((!isGrabbed && !isMoving)||isLocked) {\r
-            setAngVel(e, 0.0);\r
-            return;\r
-        }\r
-               \r
-        double angVel = getAngVel(e);\r
-        //System.out.println(angVel);\r
-        // not moving\r
-        if (angVel==0) return;\r
-        \r
-        // motion under tolerance\r
-               if (Math.abs(angVel)<angVelTolerance) {\r
-                       setAngVel(e, 0.0);\r
-                   // Stopped moving, write value\r
-                   if (!isGrabbed)\r
-                       ;//fireStoppedMoving();\r
-                   \r
-                       return;\r
-               }\r
-               \r
-               modifyValue(e, ctx, deltaTime * angVel * getFactor(e)* 0.01);\r
-               double f = (isGrabbed?getGrabFriction(e):getFriction(e));       \r
-               double dt = ((double)deltaTime)/1000;\r
-               angVel *= Math.pow(1-f, dt);\r
-               setAngVel(e, angVel);\r
-               ctx.getContentContext().setDirty();             \r
-       }\r
-\r
-       @Override\r
-       public void onElementActivated(IDiagram d, IElement e) {\r
-       }\r
-\r
-       @Override\r
-       public void onElementCreated(IElement e) {\r
-               e.setHint(KEY_MIN_POINTERS, 1);\r
-               e.setHint(ElementHints.KEY_VALUE, 0.0);\r
-               e.setHint(KEY_FRICTION, initial_friction);\r
-               e.setHint(KEY_FACTOR, initial_factor);\r
-               e.setHint(KEY_GRAB_FRICTION, initial_grab_friction);\r
-               e.setHint(KEY_ANG_VEL, 0.0);\r
-       }\r
-\r
-       @Override\r
-       public void onElementDeactivated(IDiagram d, IElement e) {\r
-       }\r
-\r
-       @Override\r
-       public void onElementDestroyed(IElement e) {\r
-       }\r
-\r
-\r
-    private double getFriction(IElement e) {\r
-       return e.getHint(KEY_FRICTION); \r
-    }\r
-    \r
-    private double getGrabFriction(IElement e) {\r
-       return e.getHint(KEY_GRAB_FRICTION);\r
-    }\r
-    \r
-    private double getFactor(IElement e) {\r
-       return e.getHint(KEY_FACTOR);\r
-    }    \r
-    \r
-       private double getValue(IElement e) {\r
-               return e.getHint(ElementHints.KEY_VALUE);\r
-       }\r
-       \r
-       private void setValue(IElement e, ICanvasContext ctx, double value) {\r
-               Double min = e.getHint(ElementHints.KEY_MIN_VALUE);\r
-               Double max = e.getHint(ElementHints.KEY_MAX_VALUE);\r
-               if (min!=null && value<min) value = min;\r
-               if (max!=null && value>max) value = max;\r
-               e.setHint(ElementHints.KEY_VALUE, value);\r
-               \r
-               Point2D origo = getOrigo(e, null);\r
-               \r
-               Rotate r = e.getElementClass().getSingleItem(Rotate.class);\r
-               double angle = r.getAngle(e);\r
-               ///System.out.println(angle);\r
-               double targetAngle = Math.IEEEremainder(value, Math.PI*2);\r
-               double diffrence = targetAngle - angle;\r
-               r.rotate(e, diffrence, origo);\r
-       }\r
-       \r
-       public Point2D getOrigo(IElement e, Point2D origo)\r
-       {\r
-               Rectangle2D size = new Rectangle2D.Double();\r
-               InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);\r
-               b.getBounds(e, size);\r
-               if (origo==null) origo = new Point2D.Double(size.getCenterX(),size.getCenterY());\r
-               return origo;\r
-       }\r
-       \r
-       public synchronized void modifyValue(IElement e, ICanvasContext ctx, double modification) {\r
-               Double                          position = getValue(e);\r
-               double value = position + modification;\r
-               setValue(e, ctx, value);\r
-       }\r
-       \r
-       \r
-       public int getMinPointers(IElement e) {\r
-               Integer i = e.getHint(KEY_MIN_POINTERS);\r
-               if (i==null) return 1;\r
-               return i;\r
-       }\r
-       \r
-       public double getAngVel(IElement e)\r
-       {\r
-               Double d = e.getHint(KEY_ANG_VEL);\r
-               if (d==null) return 0.0;\r
-               return d;\r
-       }\r
-       \r
-       public void setAngVel(IElement e, double value)\r
-       {\r
-               e.setHint(KEY_ANG_VEL, value);          \r
-       }\r
-       \r
-       public boolean isGrabbed(IElement e, ICanvasContext ctx) {\r
-           return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);      \r
-       }\r
-       \r
-       public boolean isMoving(IElement e) {\r
-           return getAngVel(e)!=0;\r
-       }\r
-       \r
-       public boolean isLocked(IElement e) {\r
-               Boolean b = e.getHint(ElementHints.KEY_LOCKED);\r
-               return b==null?false:b;\r
-       }\r
-       \r
-       public boolean isEnabled(IElement e) {\r
-               Stateful enabled = e.getElementClass().getAtMostOneItemOfClass(Stateful.class);\r
-               if (enabled==null) return true;\r
-               return enabled.isEnabled(e);\r
-       }\r
-       \r
-       public boolean isMoveable(IElement e) {\r
-               return true;\r
-       }               \r
-\r
-       \r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.elementclass.wheel;
+
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+
+import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
+import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.InternalSize;
+import org.simantics.g2d.element.handler.Stateful;
+import org.simantics.g2d.element.handler.HandleMouseEvent;
+import org.simantics.g2d.element.handler.Heartbeat;
+import org.simantics.g2d.element.handler.LifeCycle;
+import org.simantics.g2d.element.handler.Rotate;
+import org.simantics.g2d.element.handler.impl.AbstractGrabbable;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
+
+/**
+ * This handlerer rotates element when it is touched.
+ * 
+ * Hints used:
+ *    KEY_VALUE
+ *    KEY_MIN_VALUE                    optional
+ *    KEY_MAX_VALUE                    optional
+ *    KEY_FRICTION
+ *    KEY_FACTOR
+ *    KEY_GRAB_FRICTION
+ *    KEY_MIN_POINTERS
+ *    KEY_LOCKED
+ * 
+ * @author Toni Kalajainen
+ */
+public class RotatorHandler extends AbstractGrabbable implements HandleMouseEvent, LifeCycle, Heartbeat {
+
+       private static final long serialVersionUID = -3588513675880482627L;
+
+       public static final RotatorHandler INSTANCE = new RotatorHandler(); 
+       
+       public static final double STRAY_DISTANCE = 1000;
+       
+       public double initial_friction = 0.20;
+       public double initial_grab_friction = 0.99;
+       public double initial_factor = 0.025;
+       // When ang vel goes under tolerance, the motion is considered stopped
+       public double angVelTolerance = 0.5; 
+       
+       /** Angular velocity, canvas specific variable */
+       public static final Key KEY_ANG_VEL = new KeyOf(Double.class);
+       /** Minimum number of pointers */
+       public static final Key KEY_MIN_POINTERS = new KeyOf(Integer.class);
+       public static final Key KEY_GRAB_FRICTION = new KeyOf(Double.class);
+       public static final Key KEY_FRICTION = new KeyOf(Double.class);
+       public static final Key KEY_FACTOR = new KeyOf(Double.class);
+
+       public RotatorHandler() {
+               super(1000.0);
+       }
+
+       @Override
+       protected void onDrag(GrabInfo gi, ICanvasContext ctx) {
+               IElement        e       = gi.e;
+               Point2D origo   = getOrigo(e, null);
+               Point2D prevPos = gi.prevPosElement;
+               Point2D p               = gi.dragPosElement;
+               
+               // ---- Wheel is held! ----
+               // position vector 0
+               Vector2D p0 = new Vector2D(prevPos.getX(), prevPos.getY());
+               // position vector 1
+               Vector2D p1 = new Vector2D(p.getX(), p.getY());
+               // motion vector
+               Vector2D f = p1.subtract(p0);
+               // no movement
+               if (f.getNormSq()==0) return;
+               
+               // -- We are holding the wheel and we have moved the pointer --
+
+               // vector from origo to mouse
+               Vector2D odp0 = new Vector2D(p0.getX() - origo.getX(), p0.getY() - origo.getY());
+               Vector2D odp1 = new Vector2D(p1.getX() - origo.getX(), p1.getY() - origo.getY());
+               // convert motion into tangential and normal vectors            
+               // normal vector of the motion
+               Vector2D fn = odp0.scalarMultiply( f.dotProduct(odp0) / odp0.getNormSq() );
+               // tangential vector of the motion
+               Vector2D ft = f.subtract(fn);
+               
+               // momentum             
+               Vector3D r = new Vector3D(odp0.getX(), odp0.getY(), 0);
+               Vector3D F = new Vector3D(ft.getX(), ft.getY(), 0);
+               Vector3D M = r.crossProduct(F);
+               if (!isGrabbed(e, ctx)) return;
+               
+               // Turn the wheel
+               double deltaAngle = Vector2D.angle(odp0, odp1);
+               if (M.getZ()<0) deltaAngle *= -1;
+               
+               double deltaAngularVelocity = deltaAngle * 20;
+               setAngVel(e, getAngVel(e)+deltaAngularVelocity);
+
+               deltaAngle *= 0.297;
+               modifyValue(e, ctx, deltaAngle);
+       }
+
+       @Override
+       protected void onGrab(GrabInfo gi, ICanvasContext ctx) {
+       }
+
+       @Override
+       protected void onGrabCancel(GrabInfo gi, ICanvasContext ctx) {
+       }
+
+       @Override
+       protected boolean onGrabCheck(IElement e, ICanvasContext ctx, int pointerId, Point2D pickPos) {
+               return isEnabled(e);
+       }
+
+       @Override
+       protected void onRelease(GrabInfo gi, ICanvasContext ctx) {
+       }
+
+       @Override
+       public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {
+               
+        boolean isGrabbed = isGrabbed(e, ctx);
+        boolean isMoving = isMoving(e);
+        boolean isLocked = isLocked(e);
+               
+
+        // Check if the value has changed in the value source 
+        if ((!isGrabbed && !isMoving)||isLocked) {
+            setAngVel(e, 0.0);
+            return;
+        }
+               
+        double angVel = getAngVel(e);
+        //System.out.println(angVel);
+        // not moving
+        if (angVel==0) return;
+        
+        // motion under tolerance
+               if (Math.abs(angVel)<angVelTolerance) {
+                       setAngVel(e, 0.0);
+                   // Stopped moving, write value
+                   if (!isGrabbed)
+                       ;//fireStoppedMoving();
+                   
+                       return;
+               }
+               
+               modifyValue(e, ctx, deltaTime * angVel * getFactor(e)* 0.01);
+               double f = (isGrabbed?getGrabFriction(e):getFriction(e));       
+               double dt = ((double)deltaTime)/1000;
+               angVel *= Math.pow(1-f, dt);
+               setAngVel(e, angVel);
+               ctx.getContentContext().setDirty();
+       }
+
+       @Override
+       public void onElementActivated(IDiagram d, IElement e) {
+       }
+
+       @Override
+       public void onElementCreated(IElement e) {
+               e.setHint(KEY_MIN_POINTERS, 1);
+               e.setHint(ElementHints.KEY_VALUE, 0.0);
+               e.setHint(KEY_FRICTION, initial_friction);
+               e.setHint(KEY_FACTOR, initial_factor);
+               e.setHint(KEY_GRAB_FRICTION, initial_grab_friction);
+               e.setHint(KEY_ANG_VEL, 0.0);
+       }
+
+       @Override
+       public void onElementDeactivated(IDiagram d, IElement e) {
+       }
+
+       @Override
+       public void onElementDestroyed(IElement e) {
+       }
+
+
+    private double getFriction(IElement e) {
+       return e.getHint(KEY_FRICTION); 
+    }
+    
+    private double getGrabFriction(IElement e) {
+       return e.getHint(KEY_GRAB_FRICTION);
+    }
+    
+    private double getFactor(IElement e) {
+       return e.getHint(KEY_FACTOR);
+    }    
+    
+       private double getValue(IElement e) {
+               return e.getHint(ElementHints.KEY_VALUE);
+       }
+       
+       private void setValue(IElement e, ICanvasContext ctx, double value) {
+               Double min = e.getHint(ElementHints.KEY_MIN_VALUE);
+               Double max = e.getHint(ElementHints.KEY_MAX_VALUE);
+               if (min!=null && value<min) value = min;
+               if (max!=null && value>max) value = max;
+               e.setHint(ElementHints.KEY_VALUE, value);
+               
+               Point2D origo = getOrigo(e, null);
+               
+               Rotate r = e.getElementClass().getSingleItem(Rotate.class);
+               double angle = r.getAngle(e);
+               ///System.out.println(angle);
+               double targetAngle = Math.IEEEremainder(value, Math.PI*2);
+               double diffrence = targetAngle - angle;
+               r.rotate(e, diffrence, origo);
+       }
+       
+       public Point2D getOrigo(IElement e, Point2D origo)
+       {
+               Rectangle2D size = new Rectangle2D.Double();
+               InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);
+               b.getBounds(e, size);
+               if (origo==null) origo = new Point2D.Double(size.getCenterX(),size.getCenterY());
+               return origo;
+       }
+       
+       public synchronized void modifyValue(IElement e, ICanvasContext ctx, double modification) {
+               Double                          position = getValue(e);
+               double value = position + modification;
+               setValue(e, ctx, value);
+       }
+       
+       
+       public int getMinPointers(IElement e) {
+               Integer i = e.getHint(KEY_MIN_POINTERS);
+               if (i==null) return 1;
+               return i;
+       }
+       
+       public double getAngVel(IElement e)
+       {
+               Double d = e.getHint(KEY_ANG_VEL);
+               if (d==null) return 0.0;
+               return d;
+       }
+       
+       public void setAngVel(IElement e, double value)
+       {
+               e.setHint(KEY_ANG_VEL, value);
+       }
+       
+       public boolean isGrabbed(IElement e, ICanvasContext ctx) {
+           return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);
+       }
+       
+       public boolean isMoving(IElement e) {
+           return getAngVel(e)!=0;
+       }
+       
+       public boolean isLocked(IElement e) {
+               Boolean b = e.getHint(ElementHints.KEY_LOCKED);
+               return b==null?false:b;
+       }
+       
+       public boolean isEnabled(IElement e) {
+               Stateful enabled = e.getElementClass().getAtMostOneItemOfClass(Stateful.class);
+               if (enabled==null) return true;
+               return enabled.isEnabled(e);
+       }
+       
+       public boolean isMoveable(IElement e) {
+               return true;
+       }
+
+}