001/*****************************************************************************
002 *                         J3D.org Copyright (c) 2000
003 *                               Java Source
004 *
005 * This source is licensed under the GNU LGPL v2.1
006 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
007 *
008 ****************************************************************************/
009
010package jahuwaldt.j3d.geom;
011
012// Standard imports
013// none
014
015// Application specific imports
016// none
017
018/**
019 * A utility class that can be used to modify coordinate values of an item
020 * of geometry.
021 * <p>
022 *
023 * The utility class may be used as either a single shared instance or as a
024 * normal class. Sometimes you have a lot of different code all wanting to
025 * do similar modifications simultaneously and so having a single static-only
026 * class with synchronised methods would be very bad for performance.
027 *
028 * @author Justin Couch
029 * @version $Revision: 1.1 $
030 */
031public class CoordinateUtils
032{
033    /** The shared singleton instance, if needed */
034    private static CoordinateUtils sharedInstance;
035
036    /**
037     * Create a default instance of the utility class.
038     */
039    public CoordinateUtils()
040    {
041    }
042
043    /**
044     * Fetch the currently shared singleton instance.
045     *
046     * @return The current instance
047     */
048    public CoordinateUtils getSharedInstance()
049    {
050        if(sharedInstance == null)
051            sharedInstance = new CoordinateUtils();
052
053        return sharedInstance;
054    }
055
056    /**
057     * Translate, in place, the coordintes by the given amount in each
058     * direction. It assumes that the array has the coordinate values as
059     * a flat array of values.
060     *
061     * @param coords The source coordinate array to copy
062     * @param numCoords The number of valid coordinates in the array
063     * @param x The amount to translate in the x axis
064     * @param y The amount to translate in the y ayis
065     * @param z The amount to translate in the z azis
066     */
067    public void translate(float[] coords,
068                          int numCoords,
069                          float x,
070                          float y,
071                          float z)
072    {
073        int cnt = 0;
074
075        for(int i = 0; i < numCoords; i++)
076        {
077            coords[cnt++] += x;
078            coords[cnt++] += y;
079            coords[cnt++] += z;
080        }
081    }
082
083    /**
084     * Translate, in place, the coordintes by the given amount in each
085     * direction. It assumes that the array has the coordinate values as
086     * a flat array of values.
087     *
088     * @param coords The source coordinate array to copy
089     * @param numCoords The number of valid coordinates in the array
090     * @param x The amount to translate in the x axis
091     * @param y The amount to translate in the y ayis
092     * @param z The amount to translate in the z azis
093     */
094    public void translate(float[][] coords,
095                          int numCoords,
096                          float x,
097                          float y,
098                          float z)
099    {
100        for(int i = 0; i < numCoords; i++)
101        {
102            coords[i][0] += x;
103            coords[i][1] += y;
104            coords[i][2] += z;
105        }
106    }
107
108
109    /**
110     * Translate the coordintes by the given amount in each direction and
111     * place them in the destination array. It assumes that the array has the
112     * coordinate values as a flat array of values.
113     *
114     * @param srcCoords The source coordinate array to copy
115     * @param numCoords The number of valid coordinates in the array
116     * @param destCoords The array to copy the values into
117     * @param x The amount to translate in the x axis
118     * @param y The amount to translate in the y ayis
119     * @param z The amount to translate in the z azis
120     */
121    public void translate(float[] srcCoords,
122                          int numCoords,
123                          float[] destCoords,
124                          float x,
125                          float y,
126                          float z)
127    {
128        int cnt = 0;
129
130        for(int i = 0; i < numCoords; i++)
131        {
132            destCoords[cnt] = srcCoords[cnt] + x;
133            cnt++;
134            destCoords[cnt] = srcCoords[cnt] + y;
135            cnt++;
136            destCoords[cnt] = srcCoords[cnt] + y;
137            cnt++;
138        }
139    }
140
141    /**
142     * Translate the coordintes by the given amount in each direction and
143     * place them in the destination array. It assumes that the array has the
144     * coordinate values as a flat array of values.
145     *
146     * @param srcCoords The source coordinate array to copy
147     * @param numCoords The number of valid coordinates in the array
148     * @param destCoords The array to copy the values into
149     * @param x The amount to translate in the x axis
150     * @param y The amount to translate in the y ayis
151     * @param z The amount to translate in the z azis
152     */
153    public void translate(float[][] srcCoords,
154                          int numCoords,
155                          float[][] destCoords,
156                          float x,
157                          float y,
158                          float z)
159    {
160        for(int i = 0; i < numCoords; i++)
161        {
162            destCoords[i][0] = srcCoords[i][0] + x;
163            destCoords[i][1] = srcCoords[i][1] + y;
164            destCoords[i][2] = srcCoords[i][2] + y;
165        }
166    }
167}