KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > DistantLight


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image;
19
20 import java.awt.Color JavaDoc;
21
22 /**
23  * A light source placed at the infinity, such that the light angle is
24  * constant over the whole surface.
25  *
26  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
27  * @version $Id: DistantLight.java,v 1.5 2005/03/27 08:58:32 cam Exp $
28  */

29 public class DistantLight extends AbstractLight {
30     /**
31      * The azimuth of the distant light, i.e., the angle of the light vector
32      * on the (X, Y) plane
33      */

34     private double azimuth;
35
36     /**
37      * The elevation of the distant light, i.e., the angle of the light
38      * vector on the (X, Z) plane.
39      */

40     private double elevation;
41
42     /**
43      * Light vector
44      */

45     private double Lx, Ly, Lz;
46
47     /**
48      * @return the DistantLight's azimuth
49      */

50     public double getAzimuth(){
51         return azimuth;
52     }
53
54     /**
55      * @return the DistantLight's elevation
56      */

57     public double getElevation(){
58         return elevation;
59     }
60
61     public DistantLight(double azimuth, double elevation, Color JavaDoc color){
62         super(color);
63
64         this.azimuth = azimuth;
65         this.elevation = elevation;
66
67         Lx = Math.cos(Math.PI*azimuth/180.)*Math.cos(Math.PI*elevation/180.);
68         Ly = Math.sin(Math.PI*azimuth/180.)*Math.cos(Math.PI*elevation/180.);
69         Lz = Math.sin(Math.PI*elevation/180);
70     }
71
72     /**
73      * @return true if the light is constant over the whole surface
74      */

75     public boolean isConstant(){
76         return true;
77     }
78
79     /**
80      * Computes the light vector in (x, y)
81      *
82      * @param x x-axis coordinate where the light should be computed
83      * @param y y-axis coordinate where the light should be computed
84      * @param L array of length 3 where the result is stored
85      */

86     public void getLight(final double x, final double y, final double z,
87                          final double L[]){
88         L[0] = Lx;
89         L[1] = Ly;
90         L[2] = Lz;
91     }
92
93     /**
94      * Returns a row of the light map, starting at (x, y) with dx
95      * increments, a given width, and z elevations stored in the
96      * fourth component on the N array.
97      *
98      * @param x x-axis coordinate where the light should be computed
99      * @param y y-axis coordinate where the light should be computed
100      * @param dx delta x for computing light vectors in user space
101      * @param width number of samples to compute on the x axis
102      * @param z array containing the z elevation for all the points
103      * @param lightRow array to store the light info to, if null it will
104      * be allocated for you and returned.
105      *
106      * @return an array width columns where each element
107      * is an array of three components representing the x, y and z
108      * components of the light vector. */

109     public double[][] getLightRow(double x, double y,
110                                   final double dx, final int width,
111                                   final double[][] z,
112                                   final double[][] lightRow) {
113         double [][] ret = lightRow;
114
115         if (ret == null) {
116             // If we are allocating then use the same light vector for
117
// all entries.
118
ret = new double[width][];
119
120             double[] CL = new double[3];
121             CL[0]=Lx;
122             CL[1]=Ly;
123             CL[2]=Lz;
124
125             for(int i=0; i<width; i++){
126                 ret[i] = CL;
127             }
128         } else {
129             final double lx = Lx;
130             final double ly = Ly;
131             final double lz = Lz;
132
133             for(int i=0; i<width; i++){
134                 ret[i][0] = lx;
135                 ret[i][1] = ly;
136                 ret[i][2] = lz;
137             }
138         }
139
140         return ret;
141     }
142 }
143
144
Popular Tags