/** * Returns the scaled vector s*u for this vector u. * @param s the scale factor. * @return the scaled vector. */ public Vector3 times(double s) { return new Vector3(x*s,y*s,z*s); }
/** * Gets the unit-vector normal to this plane. The vector points toward * the space above the plane. * @return the unit-vector normal. */ public Vector3 getNormal() { return new Vector3(_a,_b,_c); }
/** * Gets the translate vector for this view. * @return the translate vector. */ public Vector3 getTranslate() { return new Vector3(_translate); }
/** * Returns the vector difference u-v for this vector u. * @param v the other vector. * @return the vector difference u-v */ public Vector3 minus(Vector3 v) { return new Vector3(x-v.x,y-v.y,z-v.z); }
/** * Returns the vector v = p-q, for this point p and the specified point q. * @param q the point q. * @return the vector v = p-q. */ public Vector3 minus(Point3 q) { return new Vector3(x-q.x,y-q.y,z-q.z); }
/** * Returns the negation -u of this vector u. * @return the negation -u. */ public Vector3 negate() { return new Vector3(-x,-y,-z); }
/** * Returns the vector sum u+v for this vector u. * @param v the other vector. * @return the vector sum u+v */ public Vector3 plus(Vector3 v) { return new Vector3(x+v.x,y+v.y,z+v.z); }
/** * Returns the cross product of this vector u and the specified vector v. * @param v the vector v. * @return the cross product. */ public Vector3 cross(Vector3 v) { return new Vector3(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x); } }
/** * Returns the product Mv of this matrix M and a vector v. * Uses only the upper-left 3-by-3 elements of this matrix. * @param v the vector v. * @return the product Mv. */ public Vector3 times(Vector3 v) { double vx = v.x; double vy = v.y; double vz = v.z; double ux = m[ 0]*vx + m[ 4]*vy + m[ 8]*vz; double uy = m[ 1]*vx + m[ 5]*vy + m[ 9]*vz; double uz = m[ 2]*vx + m[ 6]*vy + m[10]*vz; return new Vector3(ux,uy,uz); }
/** * Returns the product M'v of the transpose of this matrix M and a vector v. * Uses only the upper-left 3-by-3 elements of this matrix. * @param v the vector v. * @return the product M'v. */ public Vector3 transposeTimes(Vector3 v) { double vx = v.x; double vy = v.y; double vz = v.z; double ux = m[ 0]*vx + m[ 1]*vy + m[ 2]*vz; double uy = m[ 4]*vx + m[ 5]*vy + m[ 6]*vz; double uz = m[ 8]*vx + m[ 9]*vy + m[10]*vz; return new Vector3(ux,uy,uz); }
/** * Returns the unit vector with the same direction as this vector. * @return the unit vector. */ public Vector3 normalize() { double d = length(); double s = (d>0.0)?1.0/d:1.0; return new Vector3(x*s,y*s,z*s); }
/** * Sets the translate vector for this view. * @param translate the translate vector. */ public void setTranslate(Vector3 translate) { if (_translate.equals(translate)) return; _translate = new Vector3(translate); updateView(); }
private void init() { _scale = 1.0; _translate = new Vector3(0.0,0.0,0.0); _azimuth = 40.0; _elevation = 25.0; _projection = Projection.PERSPECTIVE; _ndraw = 0; _stopwatch = new Stopwatch(); _stopwatch.start(); }
private static Vector3 randomVector3() { double x = _random.nextDouble(); double y = _random.nextDouble(); double z = _random.nextDouble(); return new Vector3(x,y,z); }
public void dragBegin(DragContext dc) { Point3 p = dc.getPointWorld(); Vector3 n = null; Axis axis = _frame.getAxis(); if (axis==Axis.X) { n = new Vector3(1.0,0.0,0.0); } else if (axis==Axis.Y) { n = new Vector3(0.0,1.0,0.0); } else if (axis==Axis.Z) { n = new Vector3(0.0,0.0,1.0); } MouseEvent event = dc.getMouseEvent(); Point3 origin = getLocation(); Plane plane = new Plane(p,n); Matrix44 worldToPixel = dc.getWorldToPixel(); _mouseOnPlane = new MouseOnPlane(event,origin,plane,worldToPixel); } public void drag(DragContext dc) {
public void dragBegin(DragContext dc) { Point3 origin = dc.getPointWorld(); Vector3 normal = null; Axis axis = _frame.getAxis(); if (axis==Axis.X) { normal = new Vector3(1.0,0.0,0.0); } else if (axis==Axis.Y) { normal = new Vector3(0.0,1.0,0.0); } else if (axis==Axis.Z) { normal = new Vector3(0.0,0.0,1.0); } Plane plane = new Plane(origin,normal); MouseEvent event = dc.getMouseEvent(); Matrix44 worldToPixel = dc.getWorldToPixel(); if (event.isControlDown() || event.isAltDown()) { // Alt/Option for Mac _mouseConstrained = new MouseOnPlane(event,origin,plane,worldToPixel); } else { _mouseConstrained = new MouseOnLine(event,origin,normal,worldToPixel); } _origin = origin; _qa = _frame.getCornerMin(); _qb = _frame.getCornerMax(); } public void drag(DragContext dc) {
public void testVector() { int ntrial = 10; for (int itrial=0; itrial<ntrial; ++itrial) { Vector3 u = randomVector3(); Vector3 v = randomVector3(); Vector3 vc = new Vector3(v); assertEquals(v,v.negate().negate()); assertEquals(v,vc.negateEquals().negateEquals()); assertEquals(1.0,v.normalize().length()); assertEquals(1.0,vc.normalizeEquals().length()); assertEquals(1.0,v.normalize().lengthSquared()); assertEquals(v.dot(v),v.lengthSquared()); assertEquals(0.0,u.cross(v).dot(u)); assertEquals(0.0,u.cross(v).dot(v)); } }