| 1 | /* AffineTransform.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 2001 |
| 21 | djmw 20020315 GPL header |
| 22 | djmw 20041027 Added AffineTransform_extractMatrix |
| 23 | djmw 20050726 Added AffineTransform_extractTranslationVector |
| 24 | djmw 20071012 Added: o_CAN_WRITE_AS_ENCODING.h |
| 25 | djmw 20110304 Thing_new |
| 26 | */ |
| 27 | |
| 28 | #include "AffineTransform.h" |
| 29 | #include "NUM2.h" |
| 30 | |
| 31 | #include "oo_DESTROY.h" |
| 32 | #include "AffineTransform_def.h" |
| 33 | #include "oo_COPY.h" |
| 34 | #include "AffineTransform_def.h" |
| 35 | #include "oo_EQUAL.h" |
| 36 | #include "AffineTransform_def.h" |
| 37 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 38 | #include "AffineTransform_def.h" |
| 39 | #include "oo_WRITE_TEXT.h" |
| 40 | #include "AffineTransform_def.h" |
| 41 | #include "oo_WRITE_BINARY.h" |
| 42 | #include "AffineTransform_def.h" |
| 43 | #include "oo_READ_TEXT.h" |
| 44 | #include "AffineTransform_def.h" |
| 45 | #include "oo_READ_BINARY.h" |
| 46 | #include "AffineTransform_def.h" |
| 47 | #include "oo_DESCRIPTION.h" |
| 48 | #include "AffineTransform_def.h" |
| 49 | |
| 50 | void structAffineTransform :: v_transform (MATVU const& out, constMATVU const& in) { |
| 51 | Melder_assert (in.nrow == out.nrow); |
| 52 | Melder_assert (in.ncol == out.ncol); |
| 53 | mul_MAT_out (out, in, our r.get()); |
| 54 | out += our t.all(); |
| 55 | } |
| 56 | |
| 57 | autoAffineTransform structAffineTransform :: v_invert () { |
| 58 | autoAffineTransform thee = Data_copy (this); |
| 59 | constexpr double tolerance = 0.000001; |
| 60 | |
| 61 | MATpseudoInverse (thy r.get(), our r.get(), tolerance); |
| 62 | mul_VEC_out (thy t.get(), thy r.get(), our t.get()); |
| 63 | thy t.get() *= -1.0; |
| 64 | return thee; |
| 65 | } |
| 66 | |
| 67 | Thing_implement (AffineTransform, Daata, 0); |
| 68 | |
| 69 | void AffineTransform_init (AffineTransform me, integer dimension) { |
| 70 | Melder_require (dimension > 0, U"Dimensionality should be greater than zero."); |
| 71 | |
| 72 | my dimension = dimension; |
| 73 | my r = zero_MAT (dimension, dimension); |
| 74 | my t = zero_VEC (dimension); |
| 75 | } |
| 76 | |
| 77 | autoAffineTransform AffineTransform_create (integer dimension) { |
| 78 | try { |
| 79 | autoAffineTransform me = Thing_new (AffineTransform); |
| 80 | AffineTransform_init (me.get(), dimension); |
| 81 | return me; |
| 82 | } catch (MelderError) { |
| 83 | Melder_throw (U"AffineTransform not created."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | autoAffineTransform AffineTransform_invert (AffineTransform me) { |
| 88 | return my v_invert (); |
| 89 | } |
| 90 | |
| 91 | autoTableOfReal AffineTransform_extractMatrix (AffineTransform me) { |
| 92 | try { |
| 93 | autoTableOfReal thee = TableOfReal_create (my dimension, my dimension); |
| 94 | thy data.all() <<= my r.all(); |
| 95 | for (integer i = 1; i <= my dimension; i ++) { |
| 96 | char32 label [40]; |
| 97 | Melder_sprint (label,40, i); |
| 98 | TableOfReal_setRowLabel (thee.get(), i, label); |
| 99 | TableOfReal_setColumnLabel (thee.get(), i, label); |
| 100 | } |
| 101 | return thee; |
| 102 | } catch (MelderError) { |
| 103 | Melder_throw (me, U": transformation matrix not extracted."); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | autoTableOfReal AffineTransform_extractTranslationVector (AffineTransform me) { |
| 108 | try { |
| 109 | autoTableOfReal thee = TableOfReal_create (1, my dimension); |
| 110 | thy data.row (1) <<= my t.get(); |
| 111 | return thee; |
| 112 | } catch (MelderError) { |
| 113 | Melder_throw (me, U": translation vector not extracted."); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* End of file AffineTransform.cpp */ |