001/*
002 *   Entity124_TransformationMatrix  -- This class encapsulates a transformation elment that allows translation & rotation.
003 *
004 *   Copyright (C) 2010-2016, Joseph A. Huwaldt.
005 *   All rights reserved.
006 *   
007 *   This library is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *   
012 *   This library is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 *   Lesser General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public License
018 *   along with this program; if not, write to the Free Software
019 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
021 *
022 *   Based on, but heavily modified from, IGESView ( http://ts.nist.gov/Standards/IGES/igesTools.cfm )
023 */
024package geomss.geom.reader.iges;
025
026import geomss.geom.GTransform;
027import java.io.IOException;
028import java.io.RandomAccessFile;
029import java.text.MessageFormat;
030import javax.measure.converter.UnitConverter;
031import javax.measure.unit.SI;
032import org.jscience.mathematics.vector.Float64Vector;
033
034/**
035 * <b><i>TRANSFORMATION MATRIX ENTITY</i></b> - This allows translation and rotation to be
036 * applied to other entities. This is used to define alternate coordinate systems for
037 * definition and viewing. The transformation matrix entity transforms three-row column
038 * vectors by means of matrix multiplication and then a vector addition. This entity can
039 * be considered as an "operator" entity in that it starts with the input vector, operates
040 * on it as described above, and produces the output vector.
041 * 
042 * <p>
043 * This entity, when read from an IGES file, is converted to a GTransform object. This
044 * entity type can not be written out to an IGES file.
045 * </p>
046 *
047 * <p> Modified by: Joseph A. Huwaldt </p>
048 * 
049 * @author JDN, Version 1.0
050 * @version September 13, 2016
051 */
052public class Entity124_TransformationMatrix extends Entity {
053
054    private GTransform mat = GTransform.IDENTITY;
055
056    /**
057     * Default constructor. Needs to initialize the internal matrix representation.
058     *
059     * @param p  part to which this entity is contained
060     * @param de Directory Entry for this entity
061     */
062    public Entity124_TransformationMatrix(Part p, DirEntry de) {
063        super(p, de);
064
065        if (Constants.DEBUG) {
066            System.out.println("Entity124 constructor called");
067        }
068
069    }
070
071    /**
072     * Checks to see if the entity is correct. The following restrictions are imposed:
073     *
074     * - The Transformation Matrix Pointer shall be 0
075     */
076    @Override
077    public void check() {
078    }
079
080    /**
081     * Read the Parameter Data from the String read in by the superclass.
082     *
083     * @param in input file
084     * @throws IOException if there is any problem reading the entity from the IGES file.
085     */
086    @Override
087    public void read(RandomAccessFile in) throws IOException {
088        if (Constants.DEBUG) {
089            System.out.println("Entity124.read() called");
090        }
091
092        if (getDirectoryEntry().getForm() != 0)
093            throw new IOException(MessageFormat.format(RESOURCES.getString("illegalForm"),
094                    getDirectoryEntry().getForm(), getTypeString()));
095
096        super.read(in);
097        String s = getPDString();
098
099        if (Constants.DEBUG) {
100            System.out.println("PD String = \"" + s + "\"");
101        }
102
103        //  Get a unit converter between the model units and meters.
104        UnitConverter u2meters = Constants.unit.getConverterTo(SI.METER);
105
106        //  Start reading in the matrix.
107        double r1 = getReal(s), r2 = getReal(s), r3 = getReal(s);
108        double t = u2meters.convert(getReal(s));        //  Converted to meters.
109        Float64Vector row0 = Float64Vector.valueOf(r1, r2, r3, t);
110
111        r1 = getReal(s);
112        r2 = getReal(s);
113        r3 = getReal(s);
114        t = u2meters.convert(getReal(s));
115        Float64Vector row1 = Float64Vector.valueOf(r1, r2, r3, t);
116
117        r1 = getReal(s);
118        r2 = getReal(s);
119        r3 = getReal(s);
120        t = u2meters.convert(getReal(s));
121        Float64Vector row2 = Float64Vector.valueOf(r1, r2, r3, t);
122        Float64Vector row3 = Float64Vector.valueOf(0, 0, 0, 1);
123
124        mat = GTransform.valueOf(row0, row1, row2, row3);
125
126        super.read_additional();
127    }
128
129    /**
130     * Returns a short String describing this Entity object's type.
131     *
132     * @return A short String describing this Entity object's type.
133     */
134    @Override
135    public String getTypeString() {
136        return "Entity124 - Transformation Matrix";
137    }
138
139    /**
140     * Dump to String.
141     *
142     * @return String containing the resulting text.
143     */
144    @Override
145    public String toString() {
146        StringBuilder outStr = new StringBuilder(super.toString());
147        outStr.append("\n");
148        outStr.append(mat.toString());
149        return outStr.toString();
150    }
151
152    /**
153     * Returns the internal matrix.
154     *
155     * @return GTransform set to the values of the transform matrix.
156     */
157    public GTransform getMat() {
158        return getMatrix(mat);
159    }
160}