]> 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 01d60e58fc01f7e4cbf91bdb8bf9da397cb0e811..f364f5040b05a908bd4bbdc04109ab1fd85eb08d 100644 (file)
@@ -14,9 +14,8 @@ package org.simantics.g2d.elementclass.wheel;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 
-import javax.vecmath.Vector2d;
-import javax.vecmath.Vector3d;
-
+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;
@@ -52,7 +51,7 @@ public class RotatorHandler extends AbstractGrabbable implements HandleMouseEven
 
        public static final RotatorHandler INSTANCE = new RotatorHandler(); 
        
-       public static final double STRAY_DISTANCE = 1000;       
+       public static final double STRAY_DISTANCE = 1000;
        
        public double initial_friction = 0.20;
        public double initial_grab_friction = 0.99;
@@ -63,8 +62,8 @@ public class RotatorHandler extends AbstractGrabbable implements HandleMouseEven
        /** 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_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);
 
@@ -75,50 +74,46 @@ public class RotatorHandler extends AbstractGrabbable implements HandleMouseEven
        @Override
        protected void onDrag(GrabInfo gi, ICanvasContext ctx) {
                IElement        e       = gi.e;
-               Point2D origo   = getOrigo(e, null);            
+               Point2D origo   = getOrigo(e, null);
                Point2D prevPos = gi.prevPosElement;
                Point2D p               = gi.dragPosElement;
                
-               // ---- Wheel is held! ----             
+               // ---- Wheel is held! ----
                // position vector 0
-               Vector2d p0 = new Vector2d(prevPos.getX(), prevPos.getY());
+               Vector2D p0 = new Vector2D(prevPos.getX(), prevPos.getY());
                // position vector 1
-               Vector2d p1 = new Vector2d(p.getX(), p.getY());
+               Vector2D p1 = new Vector2D(p.getX(), p.getY());
                // motion vector
-               Vector2d f = new Vector2d(p1);
-               f.sub(p0);
+               Vector2D f = p1.subtract(p0);
                // no movement
-               if (f.length()==0) return;
+               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.x - origo.getX(), p0.y - origo.getY());
-               Vector2d odp1 = new Vector2d(p1.x - origo.getX(), p1.y - origo.getY());
+               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 = new Vector2d(odp0);
-               fn.scale( f.dot(odp0) / odp0.lengthSquared() );
+               Vector2D fn = odp0.scalarMultiply( f.dotProduct(odp0) / odp0.getNormSq() );
                // tangential vector of the motion
-               Vector2d ft = new Vector2d(f);
-               ft.sub(fn);
+               Vector2D ft = f.subtract(fn);
                
                // momentum             
-               Vector3d r = new Vector3d(odp0.x, odp0.y, 0);
-               Vector3d F = new Vector3d(ft.x, ft.y, 0);
-               Vector3d M = new Vector3d();
-               M.cross(r, F);
+               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 = odp0.angle(odp1);
-               if (M.z<0) deltaAngle *= -1;
+               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);                                
+               modifyValue(e, ctx, deltaAngle);
        }
 
        @Override
@@ -172,7 +167,7 @@ public class RotatorHandler extends AbstractGrabbable implements HandleMouseEven
                double dt = ((double)deltaTime)/1000;
                angVel *= Math.pow(1-f, dt);
                setAngVel(e, angVel);
-               ctx.getContentContext().setDirty();             
+               ctx.getContentContext().setDirty();
        }
 
        @Override
@@ -262,11 +257,11 @@ public class RotatorHandler extends AbstractGrabbable implements HandleMouseEven
        
        public void setAngVel(IElement e, double value)
        {
-               e.setHint(KEY_ANG_VEL, value);          
+               e.setHint(KEY_ANG_VEL, value);
        }
        
        public boolean isGrabbed(IElement e, ICanvasContext ctx) {
-           return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);      
+           return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);
        }
        
        public boolean isMoving(IElement e) {
@@ -286,8 +281,6 @@ public class RotatorHandler extends AbstractGrabbable implements HandleMouseEven
        
        public boolean isMoveable(IElement e) {
                return true;
-       }               
+       }
 
-       
-       
 }