1 /*
   2  * Copyright (c) 2009, 2015, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 package com.javafx.experiments.utils3d.geom.transform;
  34 
  35 import com.javafx.experiments.utils3d.geom.*;
  36 
  37 /**
  38  *
  39  */
  40 public final class Identity extends BaseTransform {
  41     @Override
  42     public Degree getDegree() {
  43         return Degree.IDENTITY;
  44     }
  45 
  46     @Override
  47     public int getType() {
  48         return TYPE_IDENTITY;
  49     }
  50 
  51     @Override
  52     public boolean isIdentity() {
  53         return true;
  54     }
  55 
  56     @Override
  57     public boolean isTranslateOrIdentity() {
  58         return true;
  59     }
  60 
  61     @Override
  62     public boolean is2D() {
  63         return true;
  64     }
  65 
  66     @Override
  67     public double getDeterminant() {
  68         return 1.0;
  69     }
  70 
  71     @Override
  72     public Point2D transform(Point2D src, Point2D dst) {
  73         if (dst == null) dst = makePoint(src, dst);
  74         dst.setLocation(src);
  75         return dst;
  76     }
  77 
  78     @Override
  79     public Point2D inverseTransform(Point2D src, Point2D dst) {
  80         if (dst == null) dst = makePoint(src, dst);
  81         dst.setLocation(src);
  82         return dst;
  83     }
  84 
  85     @Override
  86     public Vec3d transform(Vec3d src, Vec3d dst) {
  87         if (dst == null) return new Vec3d(src);
  88         dst.set(src);
  89         return dst;
  90     }
  91 
  92     @Override
  93     public Vec3d deltaTransform(Vec3d src, Vec3d dst) {
  94         if (dst == null) return new Vec3d(src);
  95         dst.set(src);
  96         return dst;
  97     }
  98 
  99     @Override
 100     public Vec3d inverseTransform(Vec3d src, Vec3d dst) {
 101         if (dst == null) return new Vec3d(src);
 102         dst.set(src);
 103         return dst;
 104     }
 105 
 106     @Override
 107     public Vec3d inverseDeltaTransform(Vec3d src, Vec3d dst) {
 108         if (dst == null) return new Vec3d(src);
 109         dst.set(src);
 110         return dst;
 111     }
 112 
 113     public void transform(float[] srcPts, int srcOff,
 114                           float[] dstPts, int dstOff,
 115                           int numPts)
 116     {
 117         if (srcPts != dstPts || srcOff != dstOff) {
 118             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 119         }
 120     }
 121 
 122     public void transform(double[] srcPts, int srcOff,
 123                           double[] dstPts, int dstOff,
 124                           int numPts)
 125     {
 126         if (srcPts != dstPts || srcOff != dstOff) {
 127             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 128         }
 129     }
 130 
 131     public void transform(float[] srcPts, int srcOff,
 132                           double[] dstPts, int dstOff,
 133                           int numPts)
 134     {
 135         for (int i = 0; i < numPts; i++) {
 136             dstPts[dstOff++] = srcPts[srcOff++];
 137             dstPts[dstOff++] = srcPts[srcOff++];
 138         }
 139     }
 140 
 141     public void transform(double[] srcPts, int srcOff,
 142                           float[] dstPts, int dstOff,
 143                           int numPts)
 144     {
 145         for (int i = 0; i < numPts; i++) {
 146             dstPts[dstOff++] = (float) srcPts[srcOff++];
 147             dstPts[dstOff++] = (float) srcPts[srcOff++];
 148         }
 149     }
 150 
 151     @Override
 152     public void deltaTransform(float[] srcPts, int srcOff,
 153                                float[] dstPts, int dstOff,
 154                                int numPts)
 155     {
 156         if (srcPts != dstPts || srcOff != dstOff) {
 157             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 158         }
 159     }
 160 
 161     @Override
 162     public void deltaTransform(double[] srcPts, int srcOff,
 163                                double[] dstPts, int dstOff,
 164                                int numPts)
 165     {
 166         if (srcPts != dstPts || srcOff != dstOff) {
 167             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 168         }
 169     }
 170 
 171     public void inverseTransform(float[] srcPts, int srcOff,
 172                                  float[] dstPts, int dstOff,
 173                                  int numPts)
 174     {
 175         if (srcPts != dstPts || srcOff != dstOff) {
 176             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 177         }
 178     }
 179 
 180     public void inverseDeltaTransform(float[] srcPts, int srcOff,
 181                                       float[] dstPts, int dstOff,
 182                                       int numPts)
 183     {
 184         if (srcPts != dstPts || srcOff != dstOff) {
 185             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 186         }
 187     }
 188 
 189     public void inverseTransform(double[] srcPts, int srcOff,
 190                                  double[] dstPts, int dstOff,
 191                                  int numPts)
 192     {
 193         if (srcPts != dstPts || srcOff != dstOff) {
 194             System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2);
 195         }
 196     }
 197 
 198     @Override
 199     public BaseBounds transform(BaseBounds bounds, BaseBounds result) {
 200         if (result != bounds) {
 201             result = result.deriveWithNewBounds(bounds);
 202         }
 203         return result;
 204     }
 205 
 206     @Override
 207     public void transform(Rectangle rect, Rectangle result) {
 208         if (result != rect) {
 209             result.setBounds(rect);
 210         }
 211     }
 212 
 213     @Override
 214     public BaseBounds inverseTransform(BaseBounds bounds, BaseBounds result) {
 215         if (result != bounds) {
 216             result = result.deriveWithNewBounds(bounds);
 217         }
 218         return result;
 219     }
 220 
 221     @Override
 222     public void inverseTransform(Rectangle rect, Rectangle result) {
 223         if (result != rect) {
 224             result.setBounds(rect);
 225         }
 226     }
 227 
 228     @Override
 229     public void setToIdentity() {
 230     }
 231 
 232     @Override
 233     public void setTransform(BaseTransform xform) {
 234         if (!xform.isIdentity()) {
 235             degreeError(Degree.IDENTITY);
 236         }
 237     }
 238 
 239     @Override
 240     public void invert() {
 241     }
 242 
 243     @Override
 244     public void restoreTransform(double mxx, double myx,
 245                                  double mxy, double myy,
 246                                  double mxt, double myt)
 247     {
 248         if (mxx != 1.0 || myx != 0.0 ||
 249             mxy != 0.0 || myy != 1.0 ||
 250             mxt != 0.0 || myt != 0.0)
 251         {
 252             degreeError(Degree.IDENTITY);
 253         }
 254     }
 255 
 256     @Override
 257     public void restoreTransform(double mxx, double mxy, double mxz, double mxt,
 258                                  double myx, double myy, double myz, double myt,
 259                                  double mzx, double mzy, double mzz, double mzt)
 260     {
 261         if (mxx != 1.0 || mxy != 0.0 || mxz != 0.0 || mxt != 0.0 ||
 262             myx != 0.0 || myy != 1.0 || myz != 0.0 || myt != 0.0 ||
 263             mzx != 0.0 || mzy != 0.0 || mzz != 1.0 || mzt != 0.0)
 264         {
 265             degreeError(Degree.IDENTITY);
 266         }
 267     }
 268 
 269     @Override
 270     public BaseTransform deriveWithTranslation(double mxt, double myt) {
 271         return Translate2D.getInstance(mxt, myt);
 272     }
 273 
 274     @Override
 275     public BaseTransform deriveWithPreTranslation(double mxt, double myt) {
 276         return Translate2D.getInstance(mxt, myt);
 277     }
 278 
 279     @Override
 280     public BaseTransform deriveWithTranslation(double mxt, double myt, double mzt) {
 281         if (mzt == 0.0) {
 282             if (mxt == 0.0 && myt == 0.0) {
 283                 return this;
 284             }
 285             return new Translate2D(mxt, myt);
 286         }
 287         Affine3D a = new Affine3D();
 288         a.translate(mxt, myt, mzt);
 289         return a;
 290     }
 291 
 292     @Override
 293     public BaseTransform deriveWithScale(double mxx, double myy, double mzz) {
 294         if (mzz == 1.0) {
 295             if (mxx == 1.0 && myy == 1.0) {
 296                 return this;
 297             }
 298             Affine2D a = new Affine2D();
 299             a.scale(mxx, myy);
 300             return a;
 301         }
 302         Affine3D a = new Affine3D();
 303         a.scale(mxx, myy, mzz);
 304         return a;
 305 
 306     }
 307 
 308     @Override
 309     public BaseTransform deriveWithRotation(double theta,
 310             double axisX, double axisY, double axisZ) {
 311         if (theta == 0.0) {
 312             return this;
 313         }
 314         if (almostZero(axisX) && almostZero(axisY)) {
 315             if (axisZ == 0.0) {
 316                 return this;
 317             }
 318             Affine2D a = new Affine2D();
 319             if (axisZ > 0) {
 320                 a.rotate(theta);
 321             } else if (axisZ < 0) {
 322                 a.rotate(-theta);
 323             }
 324             return a;
 325         }
 326         Affine3D a = new Affine3D();
 327         a.rotate(theta, axisX, axisY, axisZ);
 328         return a;
 329     }
 330 
 331     @Override
 332     public BaseTransform deriveWithConcatenation(double mxx, double myx,
 333                                                  double mxy, double myy,
 334                                                  double mxt, double myt)
 335     {
 336         return getInstance(mxx, myx,
 337                            mxy, myy,
 338                            mxt, myt);
 339     }
 340 
 341     @Override
 342     public BaseTransform deriveWithConcatenation(
 343             double mxx, double mxy, double mxz, double mxt,
 344             double myx, double myy, double myz, double myt,
 345             double mzx, double mzy, double mzz, double mzt) {
 346         return getInstance(mxx, mxy, mxz, mxt,
 347                            myx, myy, myz, myt,
 348                            mzx, mzy, mzz, mzt);
 349     }
 350 
 351     @Override
 352     public BaseTransform deriveWithConcatenation(BaseTransform tx) {
 353         return getInstance(tx);
 354     }
 355 
 356     @Override
 357     public BaseTransform deriveWithPreConcatenation(BaseTransform tx) {
 358         return getInstance(tx);
 359     }
 360 
 361     @Override
 362     public BaseTransform deriveWithNewTransform(BaseTransform tx) {
 363         return getInstance(tx);
 364     }
 365 
 366     @Override
 367     public BaseTransform createInverse() {
 368         return this;
 369     }
 370 
 371     @Override
 372     public String toString() {
 373         return ("Identity[]");
 374     }
 375 
 376     @Override
 377     public BaseTransform copy() {
 378         return this;
 379     }
 380 
 381     @Override
 382     public boolean equals(Object obj) {
 383         return (obj instanceof BaseTransform &&
 384                 ((BaseTransform) obj).isIdentity());
 385     }
 386 
 387     @Override
 388     public int hashCode() {
 389         return 0;
 390     }
 391 }