Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.transform;
027
028import javafx.beans.property.DoubleProperty;
029import javafx.beans.property.DoublePropertyBase;
030
031import com.sun.javafx.geom.transform.Affine3D;
032import javafx.geometry.Point2D;
033import javafx.geometry.Point3D;
034
035
036/**
037 * This class represents an {@code Affine} object that translates coordinates
038 * by the specified factors. The matrix representing the translating
039 * transformation is as follows:
040 * <pre>
041 *              [   1    0    0    x  ]
042 *              [   0    1    0    y  ]
043 *              [   0    0    1    z  ]
044 * </pre>
045 */
046
047public class Translate extends Transform {
048    
049    /**
050     * Creates a default Translate (identity).
051     */
052    public Translate() {
053    }
054
055    /**
056     * Creates a two-dimensional Translate.
057     * @param x the distance by which coordinates are translated in the
058     * X axis direction
059     * @param y the distance by which coordinates are translated in the
060     * Y axis direction
061     */
062    public Translate(double x, double y) {
063        setX(x);
064        setY(y);
065    }
066
067    /**
068     * Creates a three-dimensional Translate.
069     * @param x the distance by which coordinates are translated in the
070     * X axis direction
071     * @param y the distance by which coordinates are translated in the
072     * Y axis direction
073     * @param z the distance by which coordinates are translated in the
074     * Z axis direction
075     */
076    public Translate(double x, double y, double z) {
077        this(x, y);
078        setZ(z);
079    }
080
081    /**
082     * Defines the distance by which coordinates are translated in the
083     * X axis direction
084     */
085    private DoubleProperty x;
086
087
088    public final void setX(double value) {
089        xProperty().set(value);
090    }
091
092    public final double getX() {
093        return x == null ? 0.0 : x.get();
094    }
095
096    public final DoubleProperty xProperty() {
097        if (x == null) {
098            x = new DoublePropertyBase() {
099
100                @Override
101                public void invalidated() {
102                    transformChanged();
103                }
104
105                @Override
106                public Object getBean() {
107                    return Translate.this;
108                }
109
110                @Override
111                public String getName() {
112                    return "x";
113                }
114            };
115        }
116        return x;
117    }
118
119    /**
120     * Defines the distance by which coordinates are translated in the
121     * Y axis direction
122     */
123    private DoubleProperty y;
124
125
126    public final void setY(double value) {
127        yProperty().set(value);
128    }
129
130    public final double getY() {
131        return y == null ? 0.0 : y.get();
132    }
133
134    public final DoubleProperty yProperty() {
135        if (y == null) {
136            y = new DoublePropertyBase() {
137
138                @Override
139                public void invalidated() {
140                    transformChanged();
141                }
142
143                @Override
144                public Object getBean() {
145                    return Translate.this;
146                }
147
148                @Override
149                public String getName() {
150                    return "y";
151                }
152            };
153        }
154        return y;
155    }
156
157    /**
158     * Defines the distance by which coordinates are translated in the
159     * Z axis direction
160     */
161    private DoubleProperty z;
162
163
164    public final void setZ(double value) {
165        zProperty().set(value);
166    }
167
168    public final double getZ() {
169        return z == null ? 0.0 : z.get();
170    }
171
172    public final DoubleProperty zProperty() {
173        if (z == null) {
174            z = new DoublePropertyBase() {
175
176                @Override
177                public void invalidated() {
178                    transformChanged();
179                }
180
181                @Override
182                public Object getBean() {
183                    return Translate.this;
184                }
185
186                @Override
187                public String getName() {
188                    return "z";
189                }
190            };
191        }
192        return z;
193    }
194
195    /* *************************************************************************
196     *                                                                         *
197     *                         Element getters                                 *
198     *                                                                         *
199     **************************************************************************/
200
201    @Override
202    public double getTx() {
203        return getX();
204    }
205
206    @Override
207    public double getTy() {
208        return getY();
209    }
210
211    @Override
212    public double getTz() {
213        return getZ();
214    }
215
216    /* *************************************************************************
217     *                                                                         *
218     *                           State getters                                 *
219     *                                                                         *
220     **************************************************************************/
221
222    @Override
223    boolean computeIs2D() {
224        return getZ() == 0.0;
225    }
226
227    @Override
228    boolean computeIsIdentity() {
229        return getX() == 0.0 && getY() == 0.0 && getZ() == 0.0;
230    }
231
232    /* *************************************************************************
233     *                                                                         *
234     *                           Array getters                                 *
235     *                                                                         *
236     **************************************************************************/
237
238    @Override
239    void fill2DArray(double[] array) {
240        array[0] = 1.0;
241        array[1] = 0.0;
242        array[2] = getX();
243        array[3] = 0.0;
244        array[4] = 1.0;
245        array[5] = getY();
246    }
247
248    @Override
249    void fill3DArray(double[] array) {
250        array[0] = 1.0;
251        array[1] = 0.0;
252        array[2] = 0.0;
253        array[3] = getX();
254        array[4] = 0.0;
255        array[5] = 1.0;
256        array[6] = 0.0;
257        array[7] = getY();
258        array[8] = 0.0;
259        array[9] = 0.0;
260        array[10] = 1.0;
261        array[11] = getZ();
262    }
263
264    /* *************************************************************************
265     *                                                                         *
266     *                         Transform creators                              *
267     *                                                                         *
268     **************************************************************************/
269
270    @Override
271    public Transform createConcatenation(Transform transform) {
272        if (transform instanceof Translate) {
273            final Translate t = (Translate) transform;
274            return new Translate(
275                    getX() + t.getX(),
276                    getY() + t.getY(),
277                    getZ() + t.getZ());
278        }
279
280        if (transform instanceof Scale) {
281            final Scale s = (Scale) transform;
282
283            final double sx = s.getX();
284            final double sy = s.getY();
285            final double sz = s.getZ();
286
287            final double tx = getX();
288            final double ty = getY();
289            final double tz = getZ();
290
291            if ((tx == 0.0 || sx != 1.0) &&
292                    (ty == 0.0 || sy != 1.0) &&
293                    (tz == 0.0 || sz != 1.0)) {
294                return new Scale(
295                        sx, sy, sz,
296                        s.getPivotX() + (sx == 1.0 ? 0 : tx / (1.0 - sx)),
297                        s.getPivotY() + (sy == 1.0 ? 0 : ty / (1.0 - sy)),
298                        s.getPivotZ() + (sz == 1.0 ? 0 : tz / (1.0 - sz)));
299            }
300        }
301
302        if (transform instanceof Affine) {
303            Affine a = (Affine) transform.clone();
304            a.prepend(this);
305            return a;
306        }
307
308        final double txx = transform.getMxx();
309        final double txy = transform.getMxy();
310        final double txz = transform.getMxz();
311        final double ttx = transform.getTx();
312        final double tyx = transform.getMyx();
313        final double tyy = transform.getMyy();
314        final double tyz = transform.getMyz();
315        final double tty = transform.getTy();
316        final double tzx = transform.getMzx();
317        final double tzy = transform.getMzy();
318        final double tzz = transform.getMzz();
319        final double ttz = transform.getTz();
320        return new Affine(
321                txx, txy, txz, ttx + getX(),
322                tyx, tyy, tyz, tty + getY(),
323                tzx, tzy, tzz, ttz + getZ());
324    }
325
326    @Override
327    public Translate createInverse() {
328        return new Translate(-getX(), -getY(), -getZ());
329    }
330
331    @Override
332    public Translate clone() {
333        return new Translate(getX(), getY(), getZ());
334    }
335
336    /* *************************************************************************
337     *                                                                         *
338     *                     Transform, Inverse Transform                        *
339     *                                                                         *
340     **************************************************************************/
341
342    @Override
343    public Point2D transform(double x, double y) {
344        ensureCanTransform2DPoint();
345        return new Point2D(
346                x + getX(),
347                y + getY());
348    }
349
350    @Override
351    public Point3D transform(double x, double y, double z) {
352        return new Point3D(
353                x + getX(),
354                y + getY(),
355                z + getZ());
356    }
357
358    @Override
359    void transform2DPointsImpl(double[] srcPts, int srcOff,
360            double[] dstPts, int dstOff, int numPts) {
361        final double tx = getX();
362        final double ty = getY();
363
364        while (--numPts >= 0) {
365            final double x = srcPts[srcOff++];
366            final double y = srcPts[srcOff++];
367
368            dstPts[dstOff++] = x + tx;
369            dstPts[dstOff++] = y + ty;
370        }
371    }
372
373    @Override
374    void transform3DPointsImpl(double[] srcPts, int srcOff,
375            double[] dstPts, int dstOff, int numPts) {
376
377        final double tx = getX();
378        final double ty = getY();
379        final double tz = getZ();
380
381        while (--numPts >= 0) {
382            final double x = srcPts[srcOff++];
383            final double y = srcPts[srcOff++];
384            final double z = srcPts[srcOff++];
385
386            dstPts[dstOff++] = x + tx;
387            dstPts[dstOff++] = y + ty;
388            dstPts[dstOff++] = z + tz;
389        }
390    }
391
392    @Override
393    public Point2D deltaTransform(double x, double y) {
394        ensureCanTransform2DPoint();
395        return new Point2D(x, y);
396    }
397
398    @Override
399    public Point2D deltaTransform(Point2D point) {
400        if (point == null) {
401            throw new NullPointerException();
402        }
403        ensureCanTransform2DPoint();
404        return point;
405    }
406
407    @Override
408    public Point3D deltaTransform(double x, double y, double z) {
409        return new Point3D(x, y, z);
410    }
411
412    @Override
413    public Point3D deltaTransform(Point3D point) {
414        if (point == null) {
415            throw new NullPointerException();
416        }
417        return point;
418    }
419
420    @Override
421    public Point2D inverseTransform(double x, double y) {
422        ensureCanTransform2DPoint();
423        return new Point2D(
424                x - getX(),
425                y - getY());
426    }
427
428    @Override
429    public Point3D inverseTransform(double x, double y, double z) {
430        return new Point3D(
431                x - getX(),
432                y - getY(),
433                z - getZ());
434    }
435
436    @Override
437    void inverseTransform2DPointsImpl(double[] srcPts, int srcOff,
438            double[] dstPts, int dstOff, int numPts) {
439        final double tx = getX();
440        final double ty = getY();
441
442        while (--numPts >= 0) {
443            dstPts[dstOff++] = srcPts[srcOff++] - tx;
444            dstPts[dstOff++] = srcPts[srcOff++] - ty;
445        }
446    }
447
448    @Override
449    void inverseTransform3DPointsImpl(double[] srcPts, int srcOff,
450            double[] dstPts, int dstOff, int numPts) {
451
452        final double tx = getX();
453        final double ty = getY();
454        final double tz = getZ();
455
456        while (--numPts >= 0) {
457            dstPts[dstOff++] = srcPts[srcOff++] - tx;
458            dstPts[dstOff++] = srcPts[srcOff++] - ty;
459            dstPts[dstOff++] = srcPts[srcOff++] - tz;
460        }
461    }
462
463    @Override
464    public Point2D inverseDeltaTransform(double x, double y) {
465        ensureCanTransform2DPoint();
466        return new Point2D(x, y);
467    }
468
469    @Override
470    public Point2D inverseDeltaTransform(Point2D point) {
471        if (point == null) {
472            throw new NullPointerException();
473        }
474        ensureCanTransform2DPoint();
475        return point;
476    }
477
478    @Override
479    public Point3D inverseDeltaTransform(double x, double y, double z) {
480        return new Point3D(x, y, z);
481    }
482
483    @Override
484    public Point3D inverseDeltaTransform(Point3D point) {
485        if (point == null) {
486            throw new NullPointerException();
487        }
488        return point;
489    }
490
491    /* *************************************************************************
492     *                                                                         *
493     *                               Other API                                 *
494     *                                                                         *
495     **************************************************************************/
496
497    /**
498     * Returns a string representation of this {@code Translate} object.
499     * @return a string representation of this {@code Translate} object.
500     */
501    @Override
502    public String toString() {
503        final StringBuilder sb = new StringBuilder("Translate [");
504
505        sb.append("x=").append(getX());
506        sb.append(", y=").append(getY());
507        sb.append(", z=").append(getZ());
508
509        return sb.append("]").toString();
510    }
511
512    /* *************************************************************************
513     *                                                                         *
514     *                    Internal implementation stuff                        *
515     *                                                                         *
516     **************************************************************************/
517
518    /**
519     * @treatAsPrivate implementation detail
520     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
521     */
522    @Deprecated
523    @Override
524    public void impl_apply(final Affine3D trans) {
525        trans.translate(getX(), getY(), getZ());
526    }
527
528    @Override
529    void validate() {
530        getX();
531        getY();
532        getZ();
533    }
534
535    @Override
536    void appendTo(Affine a) {
537        a.appendTranslation(getTx(), getTy(), getTz());
538    }
539
540    @Override
541    void prependTo(Affine a) {
542        a.prependTranslation(getTx(), getTy(), getTz());
543    }
544}