List of usage examples for org.apache.commons.math3.geometry.euclidean.threed Vector3D normalize
public Vector3D normalize() throws MathArithmeticException
From source file:com.mapr.synth.drive.GeoPoint.java
public GeoPoint(Vector3D r) { this.r = r.normalize(); }
From source file:com.mapr.synth.drive.GeoPoint.java
public void setPosition(Vector3D position) { this.r = position.normalize(); }
From source file:ch.zweivelo.renderer.simple.shapes.Plane.java
public Plane(final Vector3D origin, final Vector3D normal) { super(Color.BLUE); this.origin = origin; this.normal = normal.normalize(); }
From source file:jtrace.texture.DiffuseFinish.java
@Override public Colour layerFinish(SceneObject object, Colour pigmentColour, Colour colour) { Colour diffuseColour = new Colour(0, 0, 0); Ray normalRay = object.getNormalRay(); for (LightSource light : object.getVisibleLights()) { // Determine distance to and direction of light source Vector3D lightDir = light.getLocation().subtract(normalRay.origin); double lightDistanceSq = lightDir.getNormSq(); lightDir = lightDir.normalize(); // Projection of light source direction onto surface normal: double projection = lightDir.dotProduct(normalRay.direction); if (projection > 0.0) { // Determine degree of illumination: double illum = projection * light.getIntensity(lightDistanceSq); // Scale light colour by illumination and add to diffuse colour: diffuseColour = diffuseColour.add(light.getColour().scale(illum)); }/*from w w w .j a v a 2 s .co m*/ } return colour.add(pigmentColour.filter(diffuseColour).scale(diffuse)); }
From source file:jtrace.texture.SpecularFinish.java
@Override public Colour layerFinish(SceneObject object, Colour pigmentColour, Colour colour) { Colour specularColour = new Colour(0, 0, 0); Ray incidentRay = object.getIncidentRay(); Ray normalRay = object.getNormalRay(); Ray reflectedRay = object.getReflectedRay(); for (LightSource light : object.getVisibleLights()) { // Determine distance and direction to light: Vector3D lightDir = light.getLocation().subtract(normalRay.origin); double lightDistanceSq = lightDir.getNormSq(); lightDir = lightDir.normalize(); // Projection of light direction onto reflected ray: double projection = lightDir.dotProduct(reflectedRay.direction); if (projection > 0) { // Digree of illumination: double illum = Math.pow(projection, tightness) * light.getIntensity(lightDistanceSq); // Scale light colour by intensity and add to specular colour: specularColour = specularColour.add(light.getColour().scale(illum)); }/*from www . j a v a2s. c o m*/ } return colour.add(specularColour); }
From source file:jtrace.Camera.java
/** * Create camera at location directed at pointAt with the top of the * frame defined by the up vector and FOV defined by fovUp and fovRight. * //from w ww . j a v a2 s. com * @param location * @param pointAt * @param up * @param fovUp * @param fovRight */ public Camera(Vector3D location, Vector3D pointAt, Vector3D up, double fovUp, double fovRight) { this.location = location; // Ensure up vector is normalized: up = up.normalize(); // Obtain direction vector from pointAt location: this.direction = pointAt.subtract(location).normalize(); // Use approximate up vector to determine true up and right vectors: this.right = direction.crossProduct(up).normalize(); this.up = this.right.crossProduct(direction); this.fovUp = fovUp; this.fovRight = fovRight; }
From source file:jtrace.object.Sphere.java
@Override public double getFirstCollisionObjectFrame(Ray ray) { Vector3D displacement = ray.getOrigin(); double a = ray.getDirection().getNormSq(); double b = 2.0 * ray.getDirection().dotProduct(displacement); double c = displacement.getNormSq() - 1.0; // Check for miss: if (b * b < 4.0 * a * c) return Double.POSITIVE_INFINITY; // Determine actual intersections double alpha0 = -0.5 * b / a; double dalpha = 0.5 * Math.sqrt(b * b - 4.0 * a * c) / a; double alphaPlus = alpha0 + dalpha; double alphaMinus = alpha0 - dalpha; // Abort if no intersections in front of us if (alphaMinus < 0 && alphaPlus < 0) return Double.POSITIVE_INFINITY; // Find closest intersection in front of us double alpha; if (alphaMinus < alphaPlus && alphaMinus > 0) alpha = alphaMinus;/*from w w w .j a v a 2s . com*/ else alpha = alphaPlus; // Record incident and normal rays incidentRay = ray; Vector3D collisionLocation = ray.direction.scalarMultiply(alpha).add(ray.origin); Vector3D normal = collisionLocation.normalize(); normalRay = new Ray(collisionLocation, normal); return alpha; }
From source file:jtrace.Camera.java
/** * Obtain camera ray to trace through scene. * //from w ww.j a v a 2 s . c o m * @param width Width of image in pixels. * @param height Height of image in pixels. * @param x X-coordinate of pixel to trace. * @param y Y-coordinate of pixel to trace. * @return */ public Ray getRay(int width, int height, int x, int y) { // Determine angles in each direction. Note the negative // in the expression for the vertical angle - this flips // the image in that direction to account for the matrix // coordinate scheme used in images. double tanThetaUp = -fovUp * (y / (double) height - 0.5); double tanThetaRight = fovRight * (x / (double) width - 0.5); Vector3D raydir = new Vector3D(1.0, direction); raydir = raydir.add(tanThetaUp, up); raydir = raydir.add(tanThetaRight, right); return new Ray(location, raydir.normalize()); }
From source file:com.mapr.synth.drive.GeoPoint.java
public Vector3D east(Vector3D r) { Vector3D ux = r.crossProduct(Z); if (ux.getNorm() < 1e-4) { // near the poles (i.e. < 640 meters from them), the definition of east is difficult ux = this.r.crossProduct(X); }//w w w . j a v a2 s. c om ux = ux.normalize(); return ux; }
From source file:jtrace.texture.TransparentFinish.java
/** * Obtain ray refracted according to Snell's law. * //w w w . j a va 2 s.co m * @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); }