Back to index

texmacs  1.0.7.15
frame.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : frame.cpp
00004 * DESCRIPTION: coordinate frames
00005 * COPYRIGHT  : (C) 2003  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "frame.hpp"
00013 #include "matrix.hpp"
00014 
00015 /******************************************************************************
00016 * Scalings
00017 ******************************************************************************/
00018 
00019 struct scaling_rep: public frame_rep {
00020   double magnify;
00021   point  shift;
00022   scaling_rep (double m, point s): magnify (m), shift (s) { linear= true; }
00023   operator tree () {
00024     return tuple ("scale", as_string (magnify), as_tree (shift)); }
00025   point direct_transform (point p) { return shift + magnify * p; }
00026   point inverse_transform (point p) { return (p - shift) / magnify; }
00027   point jacobian (point p, point v, bool &error) {
00028     (void) p; error= false; return magnify * v; }
00029   point jacobian_of_inverse (point p, point v, bool &error) {
00030     (void) p; error= false; return v / magnify; }
00031   double direct_bound (point p, double eps) { (void) p; return eps / magnify; }
00032   double inverse_bound (point p, double eps) { (void) p; return eps * magnify; }
00033 };
00034 
00035 frame
00036 scaling (double magnify, point shift) {
00037   return tm_new<scaling_rep> (magnify, shift);
00038 }
00039 
00040 /******************************************************************************
00041 * Rotations
00042 ******************************************************************************/
00043 
00044 struct rotation_2D_rep: public frame_rep {
00045   point center;
00046   double angle;
00047   rotation_2D_rep (point o, double a): center (o), angle (a) { linear= true; }
00048   operator tree () {
00049     return tuple ("rotation_2D", as_tree (center), as_string (angle)); }
00050   point direct_transform (point p) { return rotate_2D (p, center, angle); }
00051   point inverse_transform (point p) { return rotate_2D (p, center, -angle); }
00052   point jacobian (point p, point v, bool &error) {
00053     (void) p; error= false; return rotate_2D (v, point (0.0, 0.0), angle); }
00054   point jacobian_of_inverse (point p, point v, bool &error) {
00055     (void) p; error= false; return rotate_2D (v, point (0.0, 0.0), -angle); }
00056   double direct_bound (point p, double eps) { (void) p; return eps; }
00057   double inverse_bound (point p, double eps) { (void) p; return eps; }
00058 };
00059 
00060 frame
00061 rotation_2D (point center, double angle) {
00062   return tm_new<rotation_2D_rep> (center, angle);
00063 }
00064 
00065 /******************************************************************************
00066 * Affine transformations
00067 ******************************************************************************/
00068 
00069 struct affine_2D_rep: public frame_rep {
00070   matrix<double> m, j;
00071   affine_2D_rep (matrix<double> m2): m (m2) {
00072     j= copy (m);
00073     linear= true; }
00074  // FIXME: Do we use "linear" in such a
00075  //   weakest sense for affine transforms ?
00076   operator tree () {
00077     return tuple ("affine_2D", as_tree (m)); }
00078   point direct_transform (point p) {
00079     point q= point (3), r;
00080     q[0]= p[0]; q[1]= p[1]; q[2]= 1.0;
00081     q= m * q;
00082     return point (q[0], q[1]); }
00083   point inverse_transform (point p) {
00084     FAILED ("not yet implemented");
00085     return p; }
00086   point jacobian (point p, point v, bool &error) {
00087     (void) p; error= false; return j * v; }
00088   point jacobian_of_inverse (point p, point v, bool &error) {
00089     (void) p; (void) v; (void) error;
00090     FAILED ("not yet implemented");
00091     return p;}
00092   double direct_bound (point p, double eps) { (void) p; return eps; }
00093   double inverse_bound (point p, double eps) { (void) p; return eps; }
00094 };
00095 
00096 frame
00097 affine_2D (matrix<double> m) {
00098   return tm_new<affine_2D_rep> (m);
00099 }
00100 
00101 /******************************************************************************
00102 * Compound frames
00103 ******************************************************************************/
00104 
00105 struct compound_frame_rep: public frame_rep {
00106   frame f1, f2;
00107   compound_frame_rep (frame f1b, frame f2b):
00108     f1 (f1b), f2 (f2b) { linear= f1->linear && f2->linear; }
00109   operator tree () { return tuple ("compound", (tree) f1, (tree) f2); }
00110   point direct_transform (point p) { return f1 (f2 (p)); }
00111   point inverse_transform (point p) { return f2 [f1 [p]]; }
00112   point jacobian (point p, point v, bool &error) {
00113     bool error2;
00114     point w2= f2->jacobian (p, v, error2);
00115     point w1= f1->jacobian (f2 (p), w2, error);
00116     error |= error2;
00117     return w1;
00118   }
00119   point jacobian_of_inverse (point p, point v, bool &error) {
00120     (void) p; (void) v; (void) error;
00121     FAILED ("not yet implemented");
00122     return p;
00123   }
00124   double direct_bound (point p, double eps) {
00125     return f1->direct_bound (f2(p), f2->direct_bound (p, eps)); }
00126   double inverse_bound (point p, double eps) {
00127     return f2->inverse_bound (f1[p], f1->inverse_bound (p, eps)); }
00128 };
00129 
00130 frame
00131 operator * (frame f1, frame f2) {
00132   return tm_new<compound_frame_rep> (f1, f2);
00133 }
00134 
00135 /******************************************************************************
00136 * Inverted frames
00137 ******************************************************************************/
00138 
00139 struct inverted_frame_rep: public frame_rep {
00140   frame f;
00141   inverted_frame_rep (frame f2): f (f2) { linear= f->linear; }
00142   operator tree () { return tuple ("inverse", (tree) f); }
00143   point direct_transform (point p) { return f [p]; }
00144   point inverse_transform (point p) { return f (p); }
00145   point jacobian (point p, point v, bool &error) {
00146     return f->jacobian_of_inverse (p, v, error); }
00147   point jacobian_of_inverse (point p, point v, bool &error) {
00148     return f->jacobian (p, v, error); }
00149   double direct_bound (point p, double eps) {
00150     return f->inverse_bound (p, eps); }
00151   double inverse_bound (point p, double eps) {
00152     return f->direct_bound (p, eps); }
00153 };
00154 
00155 frame
00156 invert (frame f) {
00157   return tm_new<inverted_frame_rep> (f);
00158 }