List of usage examples for org.apache.commons.math3.geometry.euclidean.threed Rotation applyTo
public Rotation applyTo(Rotation r)
From source file:jtrace.texture.TransparentFinish.java
/** * Obtain ray refracted according to Snell's law. * /* w w w . j ava 2s.c om*/ * @param incidentRay * @param normalRay * @return refracted ray */ Ray getRefractedRay(Ray incidentRay, Ray normalRay) { // Check direction of incident ray (inside to out or outside to in) // and adjust ior accordingly: double snellFactor; Vector3D axis; if (incidentRay.direction.dotProduct(normalRay.direction) < 0.0) { snellFactor = 1.0 / ior; axis = incidentRay.direction.crossProduct(normalRay.direction); } else { snellFactor = ior; axis = normalRay.direction.crossProduct(incidentRay.direction); } if (axis.getNorm() > 0) { axis = axis.normalize(); } else { return new Ray(normalRay.getOrigin(), incidentRay.direction); } double angle = Math .asin((incidentRay.direction.normalize().crossProduct(normalRay.direction).normalize()).getNorm()); double newAngle = Math.asin(snellFactor * Math.sin(angle)); Rotation rotation = new Rotation(axis, newAngle - angle); Vector3D refractedDir = rotation.applyTo(incidentRay.direction); return new Ray(normalRay.getOrigin(), refractedDir); }
From source file:msi.gama.metamodel.shape.GamaShape.java
/** * Same as above, but applies a (optional) rotation along a given vector and * (optional) translation to the geometry * /*w ww .j a v a 2s . co m*/ * @param source * cannot be null * @param geom * can be null * @param rotation * can be null, expressed in degrees * @param newLocation * can be null */ public GamaShape(final IShape source, final Geometry geom, final Double rotation, final GamaPoint vector, final ILocation newLocation) { this(source, geom); if (!isPoint() && rotation != null) { if (vector == null) { final Coordinate c = geometry.getCentroid().getCoordinate(); geometry.apply(AffineTransform3D.createRotationOz(FastMath.toRadians(rotation), c.x, c.y)); } else { final Vector3D v3D = new Vector3D(vector.getX(), vector.getY(), vector.getZ()); final Rotation rot = new Rotation(v3D, FastMath.toRadians(rotation)); for (final Coordinate c : this.getInnerGeometry().getCoordinates()) { final Vector3D result = rot.applyTo(new Vector3D(c.x, c.y, c.z)); c.x = result.getX(); c.y = result.getY(); c.z = result.getZ(); } } } if (newLocation != null) { setLocation(newLocation); } }
From source file:org.jtrfp.trcl.beh.AutoLeveling.java
@Override public void _tick(long timeInMillis) { final WorldObject parent = getParent(); final Vector3D oldHeading = parent.getHeading(); final Vector3D oldTop = parent.getTop(); final Vector3D oldCross = oldHeading.crossProduct(oldTop); final Vector3D newHeading = levelingAxis == LevelingAxis.HEADING ? new Vector3D( oldHeading.getX() * retainmentCoeff[0] + levelingVector.getX() * inverseRetainmentCoeff[0], oldHeading.getY() * retainmentCoeff[1] + levelingVector.getY() * inverseRetainmentCoeff[1], oldHeading.getZ() * retainmentCoeff[2] + levelingVector.getZ() * inverseRetainmentCoeff[2]) .normalize() : oldHeading.normalize();// ww w. j a v a2 s. c o m final Vector3D newTop = levelingAxis == LevelingAxis.TOP ? new Vector3D( oldTop.getX() * retainmentCoeff[0] + levelingVector.getX() * inverseRetainmentCoeff[0], oldTop.getY() * retainmentCoeff[1] + levelingVector.getY() * inverseRetainmentCoeff[1], oldTop.getZ() * retainmentCoeff[2] + levelingVector.getZ() * inverseRetainmentCoeff[2]).normalize() : oldTop.normalize(); final Vector3D newCross = levelingAxis == LevelingAxis.CROSS ? new Vector3D( oldCross.getX() * retainmentCoeff[0] + levelingVector.getX() * inverseRetainmentCoeff[0], oldCross.getY() * retainmentCoeff[1] + levelingVector.getY() * inverseRetainmentCoeff[1], oldCross.getZ() * retainmentCoeff[2] + levelingVector.getZ() * inverseRetainmentCoeff[2]) .normalize() : oldCross.normalize(); final Rotation topDelta = new Rotation(oldTop, newTop); final Rotation headingDelta = new Rotation(oldHeading, newHeading); final Rotation crossDelta = new Rotation(oldCross, newCross); parent.setHeading(crossDelta.applyTo(headingDelta.applyTo(topDelta.applyTo(oldHeading)))); parent.setTop(crossDelta.applyTo(headingDelta.applyTo(topDelta.applyTo(oldTop)))); }
From source file:org.jtrfp.trcl.beh.HorizAimAtPlayerBehavior.java
@Override public void _tick(long timeInMillis) { if (chaseTarget != null) { WorldObject thisObject = getParent(); final Player player = thisObject.getTr().getGame().getPlayer(); if (player.getBehavior().probeForBehavior(Cloakable.class).isCloaked()) return; final RotationalMomentumBehavior rmb = thisObject.getBehavior() .probeForBehavior(RotationalMomentumBehavior.class); assert !Vect3D.isAnyEqual(chaseTarget.getPosition(), Double.POSITIVE_INFINITY); assert !Vect3D.isAnyEqual(thisObject.getPosition(), Double.NEGATIVE_INFINITY); TR.twosComplimentSubtract(chaseTarget.getPosition(), thisObject.getPosition(), vectorToTargetVar); assert !Vect3D.isAnyNaN(vectorToTargetVar); assert !Vect3D.isAnyEqual(vectorToTargetVar, Double.POSITIVE_INFINITY); assert !Vect3D.isAnyEqual(vectorToTargetVar, Double.NEGATIVE_INFINITY); vectorToTargetVar[1] = 0;//from w ww . j a v a2 s. c om Vect3D.normalize(vectorToTargetVar, vectorToTargetVar); final Vector3D thisHeading = new Vector3D(thisObject.getHeading().getX(), 0, thisObject.getHeading().getZ()).normalize(); Vect3D.subtract(thisHeading.toArray(), vectorToTargetVar, headingVarianceDelta); if (Math.sqrt(headingVarianceDelta[2] * headingVarianceDelta[2] + headingVarianceDelta[0] * headingVarianceDelta[0]) < hysteresis) return; if (!reverse) Vect3D.negate(vectorToTargetVar); Rotation rot = new Rotation(new Vector3D(vectorToTargetVar), thisHeading); final Vector3D deltaVector = rot.applyTo(Vector3D.PLUS_K); if ((deltaVector.getZ() > 0 || deltaVector.getX() < 0) == leftHanded) { rmb.accellerateEquatorialMomentum(-equatorialAccelleration); } else { rmb.accellerateEquatorialMomentum(equatorialAccelleration); } } //end if(target!null) }
From source file:org.jtrfp.trcl.beh.phy.BouncesOffSurfaces.java
@Override public void collidedWithSurface(WorldObject wo, double[] surfaceNormal) { final WorldObject parent = getParent(); final Vector3D oldHeading = parent.getHeading(); final Vector3D oldTop = parent.getTop(); final Vector3D _surfaceNormal = new Vector3D(surfaceNormal); if (oldHeading == null) throw new NullPointerException("Parent heading is null."); if (surfaceNormal == null) throw new NullPointerException("Surface normal is null."); if (reflectHeading && new Rotation(oldHeading, _surfaceNormal).getAngle() > Math.PI / 2.) { Vector3D newHeading = (_surfaceNormal.scalarMultiply(_surfaceNormal.dotProduct(oldHeading) * -2) .add(oldHeading));//from w w w . ja v a2 s. c o m parent.setHeading(newHeading); final Rotation resultingRotation = new Rotation(oldHeading, newHeading); Vector3D newTop = resultingRotation.applyTo(oldTop); //if(newTop.getY()<0)newTop=newTop.negate(); parent.setTop(newTop); } //end if(should reflect) //if(parent instanceof Velocible){ final Velocible velocible = (Velocible) parent.probeForBehavior(Velocible.class); Vector3D oldVelocity = velocible.getVelocity(); if (oldVelocity.getNorm() == 0) oldVelocity = Vector3D.PLUS_I; if (new Rotation(oldVelocity.normalize(), _surfaceNormal).getAngle() > Math.PI / 2.) { velocible.setVelocity( (_surfaceNormal.scalarMultiply(_surfaceNormal.dotProduct(oldVelocity) * -2).add(oldVelocity)) .scalarMultiply(velocityRetainmentCoefficient)); //Nudge parent.setPosition( new Vector3D(parent.getPosition()).add(_surfaceNormal.scalarMultiply(1000.)).toArray()); } //end if(should bounce) //}//end if(Velocible) }
From source file:org.jtrfp.trcl.beh.phy.RotationalMomentumBehavior.java
@Override public void _tick(long tickTimeInMillis) { final WorldObject p = getParent(); Rotation rot; try {//from w w w. j ava 2 s .c o m rot = new Rotation(p.getTop(), equatorialMomentum); p.setHeading(rot.applyTo(p.getHeading())); rot = new Rotation(p.getHeading().crossProduct(p.getTop()), polarMomentum); p.setHeading(rot.applyTo(p.getHeading())); p.setTop(rot.applyTo(p.getTop())); rot = new Rotation(p.getHeading(), lateralMomentum); p.setTop(rot.applyTo(p.getTop())); } catch (MathIllegalArgumentException e) { } }
From source file:org.jtrfp.trcl.beh.RollLevelingBehavior.java
@Override public void _tick(long tickTimeMillis) { WorldObject p = getParent();/* ww w . jav a2 s .c o m*/ final double[] initHdng = p.getHeadingArray(); final double[] initTop = p.getTopArray(); //Escape on invalid cases if (initHdng[1] <= -1) return; if (initHdng[1] >= 1) return; if (initTop[1] == 0) return; //Create an imaginary heading/top where heading.y=0 imgHdng[0] = initHdng[0]; imgHdng[1] = 0; imgHdng[2] = initHdng[2]; Vect3D.normalize(imgHdng, imgHdng); //Create a rotation to convert back later after performing the roll adjustment. Rotation rot = new Rotation(new Vector3D(initHdng), new Vector3D(imgHdng)); Vector3D imgTop = rot.applyTo(new Vector3D(initTop)).normalize(); double topY = imgTop.getY(); if (topY == 0) return; //Retainment softener prevents gimbal swing effect when facing up or down. final double retainmentSoftener = Misc.clamp(Math.abs(initHdng[1]), 0, 1); final double softenedRetainment = retainment * (1. - retainmentSoftener) + retainmentSoftener; //Perform the roll adjustment using weighting supplied by softenedRetainer if (topY > 0) {//Rightside up, approach 1. topY = topY * softenedRetainment + 1 * (1. - softenedRetainment); } else {//Upside down, approach -1 topY = topY * softenedRetainment + -1 * (1. - softenedRetainment); } Vector3D newTop = rot.applyInverseTo(new Vector3D(imgTop.getX(), topY, imgTop.getZ()).normalize()); //Apply. This will automatically notify change. No need to change heading as it is intrinsically the same. p.setTop(newTop); }
From source file:org.jtrfp.trcl.LineSegment.java
public static Triangle[] buildTriPipe(Vector3D start, Vector3D end, TextureDescription texture, int thickness, Triangle[] dest, int destOffset) { Rotation rot = new Rotation(Vector3D.PLUS_K, end.subtract(start).normalize()); final double len = start.distance(end); // Start/*from w ww . j a v a2 s. co m*/ Vector3D sbl = new Vector3D(-thickness, -thickness, 0);// bottom left Vector3D sbr = new Vector3D(thickness, -thickness, 0);// bottom right Vector3D stp = new Vector3D(0, thickness, 0); // End Vector3D ebl = new Vector3D(-thickness, -thickness, len); Vector3D ebr = new Vector3D(thickness, -thickness, len); Vector3D etp = new Vector3D(0, thickness, len); Vector3D cl = new Vector3D(-1, 1, 0).normalize(); Vector3D cr = new Vector3D(1, 1, 0).normalize(); Vector3D cb = new Vector3D(0, -1, 0); cl = rot.applyTo(cl); cr = rot.applyTo(cr); cb = rot.applyTo(cb); sbl = rot.applyTo(sbl).add(start); sbr = rot.applyTo(sbr).add(start); stp = rot.applyTo(stp).add(start); ebl = rot.applyTo(ebl).add(start); ebr = rot.applyTo(ebr).add(start); etp = rot.applyTo(etp).add(start); final double u[] = { 0, 1, 1, 0 }; final double v[] = { 1, 1, 0, 0 }; // TOP LEFT Triangle.quad2Triangles(new double[] { sbl.getX(), stp.getX(), etp.getX(), ebl.getX() }, new double[] { sbl.getY(), stp.getY(), etp.getY(), ebl.getY() }, new double[] { sbl.getZ(), stp.getZ(), etp.getZ(), ebl.getZ() }, u, v, texture, RenderMode.STATIC, false, cl, dest, destOffset, "LineSegment.topLeft"); // TOP RIGHT Triangle.quad2Triangles(new double[] { sbr.getX(), stp.getX(), etp.getX(), ebr.getX() }, new double[] { sbr.getY(), stp.getY(), etp.getY(), ebr.getY() }, new double[] { sbr.getZ(), stp.getZ(), etp.getZ(), ebr.getZ() }, u, v, texture, RenderMode.STATIC, false, cr, dest, destOffset + 2, "LineSegment.topRight"); // BOTTOM Triangle.quad2Triangles(new double[] { sbl.getX(), sbr.getX(), ebr.getX(), ebl.getX() }, new double[] { sbl.getY(), sbr.getY(), ebr.getY(), ebl.getY() }, new double[] { sbr.getZ(), sbr.getZ(), ebr.getZ(), ebl.getZ() }, u, v, texture, RenderMode.STATIC, false, cb, dest, destOffset + 4, "LineSegment.bottom"); return dest; }
From source file:org.jtrfp.trcl.obj.BillboardSprite.java
@Override protected void recalculateTransRotMBuffer() { final Camera camera = getTr().mainRenderer.get().getCamera(); final Vector3D cLookAt = camera.getLookAtVector(); final Rotation rot = new Rotation(cLookAt, rotation); this.setHeading(rot.applyTo(cLookAt.negate())); this.setTop(rot.applyTo(camera.getUpVector())); super.recalculateTransRotMBuffer(); }
From source file:org.jtrfp.trcl.obj.ObjectDirection.java
public ObjectDirection(int legacyRoll, int legacyPitch, int legacyYaw) { Vector3D headingAccumulator, topAccumulator; Rotation rot; yaw = ((double) legacyYaw / 65535.) * 2 * Math.PI; roll = ((double) legacyRoll / 65535.) * 2 * Math.PI; tilt = ((double) legacyPitch / 65535.) * 2 * Math.PI; /*//from w w w . jav a2 s . c o m * Rotation hRot = new Rotation(//yaw only. new Vector3D(0,1,0), new * Vector3D(0,0,1), new Vector3D(0,1,0), new * Vector3D(Math.cos(yaw),0.,Math.sin(yaw))); heading = * hRot.applyTo(heading); */ topAccumulator = new Vector3D(0, 1, 0); /* * Rotation tRot = new Rotation(//Pitch and roll new Vector3D(0,1,0), * new Vector3D(0,1,0), new Vector3D(Math.sin(roll),1,Math.cos(roll)), * new Vector3D(0.,Math.cos(tilt),Math.cos(tilt))); */ headingAccumulator = Vector3D.PLUS_K; rot = new Rotation(Vector3D.PLUS_I, tilt); headingAccumulator = rot.applyTo(headingAccumulator); topAccumulator = rot.applyTo(topAccumulator); rot = new Rotation(Vector3D.PLUS_J, yaw + 1.5 * Math.PI); headingAccumulator = rot.applyTo(headingAccumulator); topAccumulator = rot.applyTo(topAccumulator); // Commit the values heading = headingAccumulator; top = topAccumulator; }