"Fossies" - the Fresh Open Source Software Archive

Member "mathmod-branches-r508-trunk/pariso/parametric/ND/Matrix4D.cpp" (8 Mar 2021, 9162 Bytes) of package /linux/misc/mathmod-11.0-source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Matrix4D.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 10.1_vs_11.0.

    1 /***************************************************************************
    2  *   Copyright (C) 2021 by Abderrahman Taha                                *
    3  *   taha_ab@yahoo.fr                                                      *
    4  *                                                                         *
    5  *   This program 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     *
    8  *   (at your option) any later version.                                   *
    9  *                                                                         *
   10  *   This program is distributed in the hope that it will be useful,       *
   11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
   12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
   13  *   GNU General Public License for more details.                          *
   14  *                                                                         *
   15  *   You should have received a copy of the GNU General Public License     *
   16  *   along with this program; if not, write to the                         *
   17  *   Free Software Foundation, Inc.,                                       *
   18  *   51 Franklin Street, Fifth Floor,Boston, MA 02110-1301 USA             *
   19  ***************************************************************************/
   20 
   21 
   22 #include "Matrix4D.h"
   23 
   24 /** Create a new unit matrix */
   25 Matrix4D::Matrix4D()
   26 {
   27 
   28     pi = 3.14159265;
   29     xx = 1.0;
   30     yy = 1.0;
   31     zz = 1.0;
   32     ww = 1.0;
   33     ko = 1.0;
   34     xy = xz = xw = xo = 0;
   35     yx = yz = yw = yo = 0;
   36     zx = zy = zw = zo = 0;
   37     wx = wy = wz = wo = 0;
   38     kx = ky = kz = kw = 0;
   39 }
   40 
   41 
   42 void Matrix4D::mult(Matrix4D rhs)
   43 {
   44     double lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz + wx * rhs.xw;
   45     double lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz + wy * rhs.xw;
   46     double lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz + wz * rhs.xw;
   47     double lxw = xw * rhs.xx + yw * rhs.xy + zw * rhs.xz + ww * rhs.xw;
   48     double lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + wo * rhs.xw + rhs.xo;
   49 
   50     double lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz + wx * rhs.yw;
   51     double lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz + wy * rhs.yw;
   52     double lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz + wz * rhs.yw;
   53     double lyw = xw * rhs.yx + yw * rhs.yy + zw * rhs.yz + ww * rhs.yw;
   54     double lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + wo * rhs.yw + rhs.yo;
   55 
   56     double lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz + wx * rhs.zw;
   57     double lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz + wy * rhs.zw;
   58     double lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz + wz * rhs.zw;
   59     double lzw = xw * rhs.zx + yw * rhs.zy + zw * rhs.zz + ww * rhs.zw;
   60     double lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + wo * rhs.zw + rhs.zo;
   61 
   62     double lwx = xx * rhs.wx + yx * rhs.wy + zx * rhs.wz + wx * rhs.ww;
   63     double lwy = xy * rhs.wx + yy * rhs.wy + zy * rhs.wz + wy * rhs.ww;
   64     double lwz = xz * rhs.wx + yz * rhs.wy + zz * rhs.wz + wz * rhs.ww;
   65     double lww = xw * rhs.wx + yw * rhs.wy + zw * rhs.wz + ww * rhs.ww;
   66     double lwo = xo * rhs.wx + yo * rhs.wy + zo * rhs.wz + wo * rhs.ww + rhs.wo;
   67 
   68     xx = lxx;
   69     xy = lxy;
   70     xz = lxz;
   71     xw = lxw;
   72     xo = lxo;
   73 
   74     yx = lyx;
   75     yy = lyy;
   76     yz = lyz;
   77     yw = lyw;
   78     yo = lyo;
   79 
   80     zx = lzx;
   81     zy = lzy;
   82     zz = lzz;
   83     zw = lzw;
   84     zo = lzo;
   85 
   86     wx = lwx;
   87     wy = lwy;
   88     wz = lwz;
   89     ww = lww;
   90     wo = lwo;
   91 }
   92 
   93 /** rotate theta degrees about the yz plan */
   94 void Matrix4D::yzrot(double theta)
   95 {
   96     theta *= (pi / 180);
   97     double ct = cos(theta);
   98     double st = sin(theta);
   99 
  100     double Nyx = double(yx * ct + zx * st);
  101     double Nyy = double(yy * ct + zy * st);
  102     double Nyz = double(yz * ct + zz * st);
  103     double Nyw = double(yw * ct + zw * st);
  104     double Nyo = double(yo * ct + zo * st);
  105 
  106     double Nzx = double(zx * ct - yx * st);
  107     double Nzy = double(zy * ct - yy * st);
  108     double Nzz = double(zz * ct - yz * st);
  109     double Nzw = double(zw * ct - yw * st);
  110     double Nzo = double(zo * ct - yo * st);
  111 
  112     yo = Nyo;
  113     yx = Nyx;
  114     yy = Nyy;
  115     yz = Nyz;
  116     yw = Nyw;
  117 
  118     zo = Nzo;
  119     zx = Nzx;
  120     zy = Nzy;
  121     zz = Nzz;
  122     zw = Nzw;
  123 }
  124 
  125 /** rotate theta degrees about the xz plan */
  126 void Matrix4D::xzrot(double theta)
  127 {
  128     theta *= (pi / 180);
  129     double ct = cos(theta);
  130     double st = sin(theta);
  131 
  132     double Nxx = double(xx * ct + zx * st);
  133     double Nxy = double(xy * ct + zy * st);
  134     double Nxz = double(xz * ct + zz * st);
  135     double Nxw = double(xw * ct + zw * st);
  136     double Nxo = double(xo * ct + zo * st);
  137 
  138     double Nzx = double(zx * ct - xx * st);
  139     double Nzy = double(zy * ct - xy * st);
  140     double Nzz = double(zz * ct - xz * st);
  141     double Nzw = double(zw * ct - xw * st);
  142     double Nzo = double(zo * ct - xo * st);
  143 
  144     xo = Nxo;
  145     xx = Nxx;
  146     xy = Nxy;
  147     xz = Nxz;
  148     xw = Nxw;
  149 
  150     zo = Nzo;
  151     zx = Nzx;
  152     zy = Nzy;
  153     zz = Nzz;
  154     zw = Nzw;
  155 }
  156 
  157 /** rotate theta degrees about the  xy plan */
  158 void Matrix4D::xyrot(double theta)
  159 {
  160     theta *= (pi / 180);
  161     double ct = cos(theta);
  162     double st = sin(theta);
  163 
  164     double Nyx = double(yx * ct + xx * st);
  165     double Nyy = double(yy * ct + xy * st);
  166     double Nyz = double(yz * ct + xz * st);
  167     double Nyw = double(yw * ct + xw * st);
  168     double Nyo = double(yo * ct + xo * st);
  169 
  170     double Nxx = double(xx * ct - yx * st);
  171     double Nxy = double(xy * ct - yy * st);
  172     double Nxz = double(xz * ct - yz * st);
  173     double Nxw = double(xw * ct - yw * st);
  174     double Nxo = double(xo * ct - yo * st);
  175 
  176     yo = Nyo;
  177     yx = Nyx;
  178     yy = Nyy;
  179     yz = Nyz;
  180     yw = Nyw;
  181 
  182     xo = Nxo;
  183     xx = Nxx;
  184     xy = Nxy;
  185     xz = Nxz;
  186     xw = Nxw;
  187 }
  188 
  189 /** rotate theta degrees about the  xw plan */
  190 
  191 void Matrix4D::xwrot(double theta)
  192 {
  193     theta *= (pi / 180);
  194     double ct = cos(theta);
  195     double st = sin(theta);
  196 
  197     double Nwx = double(wx * ct + xx * st);
  198     double Nwy = double(wy * ct + xy * st);
  199     double Nwz = double(wz * ct + xz * st);
  200     double Nww = double(ww * ct + xw * st);
  201     double Nwo = double(wo * ct + xo * st);
  202 
  203     double Nxx = double(xx * ct - wx * st);
  204     double Nxy = double(xy * ct - wy * st);
  205     double Nxz = double(xz * ct - wz * st);
  206     double Nxw = double(xw * ct - ww * st);
  207     double Nxo = double(xo * ct - wo * st);
  208 
  209     wo = Nwo;
  210     wx = Nwx;
  211     wy = Nwy;
  212     wz = Nwz;
  213     ww = Nww;
  214 
  215     xo = Nxo;
  216     xx = Nxx;
  217     xy = Nxy;
  218     xz = Nxz;
  219     xw = Nxw;
  220 }
  221 
  222 /** rotate theta degrees about the  yw plan */
  223 
  224 void Matrix4D::ywrot(double theta)
  225 {
  226     theta *= (pi / 180);
  227     double ct = cos(theta);
  228     double st = sin(theta);
  229 
  230     double Nwx = double(wx * ct + yx * st);
  231     double Nwy = double(wy * ct + yy * st);
  232     double Nwz = double(wz * ct + yz * st);
  233     double Nww = double(ww * ct + yw * st);
  234     double Nwo = double(wo * ct + yo * st);
  235 
  236     double Nyx = double(yx * ct - wx * st);
  237     double Nyy = double(yy * ct - wy * st);
  238     double Nyz = double(yz * ct - wz * st);
  239     double Nyw = double(yw * ct - ww * st);
  240     double Nyo = double(yo * ct - wo * st);
  241 
  242     wo = Nwo;
  243     wx = Nwx;
  244     wy = Nwy;
  245     wz = Nwz;
  246     ww = Nww;
  247 
  248     yo = Nyo;
  249     yx = Nyx;
  250     yy = Nyy;
  251     yz = Nyz;
  252     yw = Nyw;
  253 }
  254 
  255 
  256 /** rotate theta degrees about the  yw plan */
  257 
  258 void Matrix4D::zwrot(double theta)
  259 {
  260     theta *= (pi / 180);
  261     double ct = cos(theta);
  262     double st = sin(theta);
  263 
  264     double Nwx = double(wx * ct + zx * st);
  265     double Nwy = double(wy * ct + zy * st);
  266     double Nwz = double(wz * ct + zz * st);
  267     double Nww = double(ww * ct + zw * st);
  268     double Nwo = double(wo * ct + zo * st);
  269 
  270     double Nzx = double(zx * ct - wx * st);
  271     double Nzy = double(zy * ct - wy * st);
  272     double Nzz = double(zz * ct - wz * st);
  273     double Nzw = double(zw * ct - ww * st);
  274     double Nzo = double(zo * ct - wo * st);
  275 
  276     wo = Nwo;
  277     wx = Nwx;
  278     wy = Nwy;
  279     wz = Nwz;
  280     ww = Nww;
  281 
  282     zo = Nzo;
  283     zx = Nzx;
  284     zy = Nzy;
  285     zz = Nzz;
  286     zw = Nzw;
  287 }
  288 
  289 
  290 /** Multiply this matrix by a second: M = M*R */
  291 
  292 
  293 /** Reinitialize to the unit matrix */
  294 void Matrix4D::unit()
  295 {
  296     xo = 0;
  297     xx = 1;
  298     xy = 0;
  299     xz = 0;
  300     xw = 0;
  301 
  302     yo = 0;
  303     yx = 0;
  304     yy = 1;
  305     yz = 0;
  306     yw = 0;
  307 
  308     zo = 0;
  309     zx = 0;
  310     zy = 0;
  311     zz = 1;
  312     zw = 0;
  313 
  314     wo = 0;
  315     wx = 0;
  316     wy = 0;
  317     wz = 0;
  318     ww = 1;
  319 
  320     ko = 1;
  321     kx = 0;
  322     ky = 0;
  323     kz = 0;
  324     kw = 0;
  325 };
  326 
  327 /** Translate the origin */
  328 void Matrix4D::translate(double x, double y, double z, double w)
  329 {
  330     xo += x;
  331     yo += y;
  332     zo += z;
  333     wo += w;
  334 }
  335 
  336 /** Scale by f in all dimensions */
  337 void Matrix4D::scale(double f)
  338 {
  339     xx *= f;
  340     xy *= f;
  341     xz *= f;
  342     xo *= f;
  343     yx *= f;
  344     yy *= f;
  345     yz *= f;
  346     yo *= f;
  347     zx *= f;
  348     zy *= f;
  349     zz *= f;
  350     zo *= f;
  351 }
  352 /** Scale along each axis independently */
  353 void Matrix4D::scale_2(double xf, double yf, double zf)
  354 {
  355     xx *= xf;
  356     xy *= xf;
  357     xz *= xf;
  358     xo *= xf;
  359     yx *= yf;
  360     yy *= yf;
  361     yz *= yf;
  362     yo *= yf;
  363     zx *= zf;
  364     zy *= zf;
  365     zz *= zf;
  366     zo *= zf;
  367 }