001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image;
020:
021: import java.awt.Color;
022:
023: /**
024: * A light source placed at the infinity, such that the light angle is
025: * constant over the whole surface.
026: *
027: * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
028: * @version $Id: DistantLight.java 501495 2007-01-30 18:00:36Z dvholten $
029: */
030: public class DistantLight extends AbstractLight {
031: /**
032: * The azimuth of the distant light, i.e., the angle of the light vector
033: * on the (X, Y) plane
034: */
035: private double azimuth;
036:
037: /**
038: * The elevation of the distant light, i.e., the angle of the light
039: * vector on the (X, Z) plane.
040: */
041: private double elevation;
042:
043: /**
044: * Light vector
045: */
046: private double Lx, Ly, Lz;
047:
048: /**
049: * @return the DistantLight's azimuth
050: */
051: public double getAzimuth() {
052: return azimuth;
053: }
054:
055: /**
056: * @return the DistantLight's elevation
057: */
058: public double getElevation() {
059: return elevation;
060: }
061:
062: public DistantLight(double azimuth, double elevation, Color color) {
063: super (color);
064:
065: this .azimuth = azimuth;
066: this .elevation = elevation;
067:
068: Lx = Math.cos(Math.toRadians(azimuth))
069: * Math.cos(Math.toRadians(elevation));
070: Ly = Math.sin(Math.toRadians(azimuth))
071: * Math.cos(Math.toRadians(elevation));
072: Lz = Math.sin(Math.toRadians(elevation));
073: }
074:
075: /**
076: * @return true if the light is constant over the whole surface
077: */
078: public boolean isConstant() {
079: return true;
080: }
081:
082: /**
083: * Computes the light vector in (x, y)
084: *
085: * @param x x-axis coordinate where the light should be computed
086: * @param y y-axis coordinate where the light should be computed
087: * @param L array of length 3 where the result is stored
088: */
089: public void getLight(final double x, final double y,
090: final double z, final double[] L) {
091: L[0] = Lx;
092: L[1] = Ly;
093: L[2] = Lz;
094: }
095:
096: /**
097: * Returns a row of the light map, starting at (x, y) with dx
098: * increments, a given width, and z elevations stored in the
099: * fourth component on the N array.
100: *
101: * @param x x-axis coordinate where the light should be computed
102: * @param y y-axis coordinate where the light should be computed
103: * @param dx delta x for computing light vectors in user space
104: * @param width number of samples to compute on the x axis
105: * @param z array containing the z elevation for all the points
106: * @param lightRow array to store the light info to, if null it will
107: * be allocated for you and returned.
108: *
109: * @return an array width columns where each element
110: * is an array of three components representing the x, y and z
111: * components of the light vector. */
112: public double[][] getLightRow(double x, double y, final double dx,
113: final int width, final double[][] z,
114: final double[][] lightRow) {
115: double[][] ret = lightRow;
116:
117: if (ret == null) {
118: // If we are allocating then use the same light vector for
119: // all entries.
120: ret = new double[width][];
121:
122: double[] CL = new double[3];
123: CL[0] = Lx;
124: CL[1] = Ly;
125: CL[2] = Lz;
126:
127: for (int i = 0; i < width; i++) {
128: ret[i] = CL;
129: }
130: } else {
131: final double lx = Lx;
132: final double ly = Ly;
133: final double lz = Lz;
134:
135: for (int i = 0; i < width; i++) {
136: ret[i][0] = lx;
137: ret[i][1] = ly;
138: ret[i][2] = lz;
139: }
140: }
141:
142: return ret;
143: }
144: }
|