gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] gzz/gfx/libcoords Coords.cxx Coords.hxx


From: Tuomas J. Lukka
Subject: [Gzz-commits] gzz/gfx/libcoords Coords.cxx Coords.hxx
Date: Tue, 01 Oct 2002 03:55:45 -0400

CVSROOT:        /cvsroot/gzz
Module name:    gzz
Changes by:     Tuomas J. Lukka <address@hidden>        02/10/01 03:55:44

Modified files:
        gfx/libcoords  : Coords.cxx Coords.hxx 

Log message:
        Major coordsys revamp to get inverse coordsystems in hierarchies 
working; a major thinko there.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/gfx/libcoords/Coords.cxx.diff?tr1=1.19&tr2=1.20&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/gfx/libcoords/Coords.hxx.diff?tr1=1.9&tr2=1.10&r1=text&r2=text

Patches:
Index: gzz/gfx/libcoords/Coords.cxx
diff -c gzz/gfx/libcoords/Coords.cxx:1.19 gzz/gfx/libcoords/Coords.cxx:1.20
*** gzz/gfx/libcoords/Coords.cxx:1.19   Tue Oct  1 02:22:54 2002
--- gzz/gfx/libcoords/Coords.cxx        Tue Oct  1 03:55:44 2002
***************
*** 5,12 ****
--- 5,95 ----
  namespace Coords {
      DBGVAR(dbg, "Coords.general");
  
+     // The (STL-like) concept of transform:
+     // enum { NParams = n };
+     // void tr(const ZPt &from, ZPt &to) const
+     // bool canPerformGL()
+     // bool performGL()
+     // float nonlinearity(const ZPt &p, float radius)
+     // void setParams(array/iterator i)
+     // typedef X InverseType;
+     //
+     // This approach is needed because inverse transforms and transforms need
+     // to call their parents at different points.
+     //
+     // XXX Nonlinearity is calculated wrong!
+     
+     template<class Transform> class TransformCoordSysBase : public CoordSys {
+     protected:
+       Transform t;
+     public:
+       TransformCoordSysBase() : t() { }
+       TransformCoordSysBase(const Transform &tr) : t(tr) { }
+       virtual void setParams(float *params) {
+           t.setParams(params);
+       }
+       bool canPerformGL() { 
+           return t.canPerformGL() && super->canPerformGL();  
+       }
+     };
+ 
+     template<class Transform> class InverseTransformCoordSys;
+ 
+     template<class Transform> class TransformCoordSys : public 
TransformCoordSysBase<Transform> {
+     public:
+       virtual ZPt transform(const ZPt &p) const {
+           ZPt mp;
+           t.tr(p, mp);
+           return super->transform(mp);
+       }
+       virtual void vertex(const ZPt &p) const {
+           ZPt mp;
+           t.tr(p, mp);
+           super->vertex(mp);
+       }
+         virtual bool performGL() {
+           if(!super->performGL()) return false;
+           return t.performGL();
+       }
+ 
+       virtual CoordSys *createInverse() {
+           CoordSys *sinv = super->createInverse();
+           typedef InverseTransformCoordSys<typename Transform::InverseType> 
Inv;
+           Inv *ret = new Inv(sinv, t);
+           ret->setOriginal(this);
+           return ret;
+       }
+ 
+     };
+ 
+     template<class Transform> class InverseTransformCoordSys : public 
TransformCoordSysBase<Transform> {
+     public:
+       template<class Original> InverseTransformCoordSys(CoordSys *s, Original 
&o) : 
+               TransformCoordSysBase<Transform>(o.inverseTransform()) {
+           super = s;
+       }
+ 
+       virtual ZPt transform(const ZPt &p) const {
+           ZPt mp = super->transform(p);
+           ZPt res;
+           t.tr(mp, res);
+           return res;
+       }
+       virtual void vertex(const ZPt &p) const {
+           ZPt mp = transform(p);
+           glVertex3f(mp.x, mp.y, mp.z);
+       }
+         virtual bool performGL() {
+           if(!t.performGL()) return false;
+           return super->performGL();
+       }
+       void setOriginal(CoordSys *orig) { inverse = orig; }
+ 
+     };
+ 
      class RootCoords : public CoordSys {
      public:
+       virtual void setParams(float *params) { }
        virtual ZPt transform(const ZPt &p) const {
            return p;
        }
***************
*** 25,37 ****
      /** Affine coordinate system (in xy), offset in z.
       * Parameter layout: x, y, depth, xx, xy, yx, yy
       */
!     class AffineXYCoords : public CoordSys {
        float params[7];
      public:
        enum { NParams = 7 };
!       virtual void setParams(float *params) {
            for(int i=0; i<7; i++)
!               this->params[i] = params[i];
        }
        /** Perform the internal transformation of this 
         * coordsys.
--- 108,120 ----
      /** Affine coordinate system (in xy), offset in z.
       * Parameter layout: x, y, depth, xx, xy, yx, yy
       */
!     class AffineXYCoords {
        float params[7];
      public:
        enum { NParams = 7 };
!       template<class Ptr> void setParams(Ptr p) {
            for(int i=0; i<7; i++)
!               params[i] = p[i];
        }
        /** Perform the internal transformation of this 
         * coordsys.
***************
*** 41,59 ****
            to.y = params[1] + from.x * params[5] + from.y * params[6];
            to.z = params[2] + from.z;
        }
!       virtual ZPt transform(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           return super->transform(mp);
!       }
!       virtual void vertex(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           super->vertex(mp);
!       }
!       virtual bool canPerformGL() { return super->canPerformGL(); }
!         virtual bool performGL() {
!           if(!super->performGL()) return false;
              GLfloat matrix[16] = {
                  params[3], params[5], 0, 0,
                  params[4], params[6], 0, 0,
--- 124,131 ----
            to.y = params[1] + from.x * params[5] + from.y * params[6];
            to.z = params[2] + from.z;
        }
!       bool canPerformGL() { return true; }
!         bool performGL() {
              GLfloat matrix[16] = {
                  params[3], params[5], 0, 0,
                  params[4], params[6], 0, 0,
***************
*** 63,92 ****
              glMultMatrixf(matrix);
            return true;
          }
!       virtual float nonlinearity(const ZPt &p, float radius) { 
!           float mult = 0.5 * (fabs(params[3]) + fabs(params[6]));
!           ZPt p2;
!           tr(p, p2);
!           return mult * super->nonlinearity(p2, mult * radius);
        }
  
!     protected:
!       virtual CoordSys *createInverse() {
!           CoordSys *sup = super->createInverse();
!           AffineXYCoords *inv = new AffineXYCoords();
!           inv->setSuper(sup);
            double det = params[3] * params[6] - params[4] * params[5];
            // XXX If det small, trouble!!
!           inv->params[3] = params[6] / det;
!           inv->params[4] = -params[4] / det;
!           inv->params[5] = -params[5] / det;
!           inv->params[6] = params[3] / det;
! 
!           inv->params[0] = -(params[0] * inv->params[3] + 
!                               params[1] * inv->params[4]);
!           inv->params[1] = -(params[0] * inv->params[5] + 
!                               params[1] * inv->params[6]);
!           inv->params[2] = -params[2];
            return inv;
        }
  
--- 135,159 ----
              glMultMatrixf(matrix);
            return true;
          }
!       float nonlinearity(const ZPt &p, float radius) { 
!           return 0;
        }
  
!       typedef AffineXYCoords InverseType;
!       AffineXYCoords inverseTransform() {
!           AffineXYCoords inv;
            double det = params[3] * params[6] - params[4] * params[5];
            // XXX If det small, trouble!!
!           inv.params[3] = params[6] / det;
!           inv.params[4] = -params[4] / det;
!           inv.params[5] = -params[5] / det;
!           inv.params[6] = params[3] / det;
! 
!           inv.params[0] = -(params[0] * inv.params[3] + 
!                               params[1] * inv.params[4]);
!           inv.params[1] = -(params[0] * inv.params[5] + 
!                               params[1] * inv.params[6]);
!           inv.params[2] = -params[2];
            return inv;
        }
  
***************
*** 95,108 ****
      /** Rotation clockwise. 
       * Parameter layout: angle (degrees)
       */
!     class RotateXYCoords : public CoordSys {
!       float s, c;
      public:
        enum { NParams = 1 };
!       virtual void setParams(float *params) {
!           CoordSys::setParams(params);
!           s = sin(params[0] * M_PI / 180);
!           c = cos(params[0] * M_PI / 180);
        }
        /** Perform the internal transformation of this 
         * coordsys.
--- 162,177 ----
      /** Rotation clockwise. 
       * Parameter layout: angle (degrees)
       */
!     class RotateXYCoords {
!       float a, s, c;
      public:
        enum { NParams = 1 };
!       template<class Ptr> void setParams(Ptr p) {
!           a = p[0];
!       }
!       void angleWasSet() {
!           s = sin(a * M_PI / 180);
!           c = cos(a * M_PI / 180);
        }
        /** Perform the internal transformation of this 
         * coordsys.
***************
*** 112,151 ****
            to.y = -s * from.x + c * from.y; 
            to.z = from.z;
        }
!       virtual ZPt transform(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           return super->transform(mp);
        }
!       virtual void vertex(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           super->vertex(mp);
        }
!       virtual bool canPerformGL() { return super->canPerformGL(); }
!         virtual bool performGL() {
!           if(!super->performGL()) return false;
!           glRotatef(params[0], 0, 0, 1);
!           return true;
        }
      };
  
      /** Rotation around 3D vector.
       * Params: x, y, z, angle
       */
!     class RotateXYZCoords : public CoordSys {
!       ZVec vec; float s, c;
      public:
        enum { NParams = 4 };
!       virtual void setParams(float *params) {
!           CoordSys::setParams(params);
!           vec.x = params[0];
!           vec.y = params[1];
!           vec.z = params[2];
            vec = vec.normalized();
  
!           s = sin(params[3] * M_PI / 180);
!           c = cos(params[3] * M_PI / 180);
        }
        /** Perform the internal transformation of this 
         * coordsys.
--- 181,223 ----
            to.y = -s * from.x + c * from.y; 
            to.z = from.z;
        }
!       bool canPerformGL() { return true; }
!         bool performGL() {
!           glRotatef(a, 0, 0, 1);
!           return true;
        }
!       typedef RotateXYCoords InverseType;
!       RotateXYCoords inverseTransform() {
!           RotateXYCoords inv;
!           inv.a = -a;
!           inv.angleWasSet();
!           return inv;
        }
!       float nonlinearity(const ZPt &p, float radius) { 
!           return 0;
        }
      };
  
      /** Rotation around 3D vector.
       * Params: x, y, z, angle
       */
!     class RotateXYZCoords {
!       ZVec vec; float a, s, c;
      public:
        enum { NParams = 4 };
!       template<class Ptr> void setParams(Ptr p) {
!           vec.x = p[0];
!           vec.y = p[1];
!           vec.z = p[2];
            vec = vec.normalized();
+           a = p[3];
+ 
+           angleWasSet();
  
!       }
!       void angleWasSet() {
!           s = sin(a * M_PI / 180);
!           c = cos(a * M_PI / 180);
        }
        /** Perform the internal transformation of this 
         * coordsys.
***************
*** 159,195 ****
            ZVec ortho = para.crossp(vec).normalized();
            to = ZPt( same * vec + paral * (c * para - s * ortho) );
        }
!       virtual ZPt transform(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           return super->transform(mp);
!       }
!       virtual void vertex(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           super->vertex(mp);
!       }
!       virtual bool canPerformGL() { return super->canPerformGL(); }
          virtual bool performGL() {
!           if(!super->performGL()) return false;
!           glRotatef(params[3], vec.x, vec.y, vec.z);
            return true;
        }
!     } ;
!     ;
  
      /** Scale.
       * Params: sx, sy, sz
       */
!     class ScaleXYZCoords : public CoordSys {
        ZVec vec; 
      public:
        enum { NParams = 3 };
!       virtual void setParams(float *params) {
!           CoordSys::setParams(params);
!           vec.x = params[0];
!           vec.y = params[1];
!           vec.z = params[2];
        }
        /** Perform the internal transformation of this 
         * coordsys.
--- 231,266 ----
            ZVec ortho = para.crossp(vec).normalized();
            to = ZPt( same * vec + paral * (c * para - s * ortho) );
        }
!       virtual bool canPerformGL() { return true; }
          virtual bool performGL() {
!           glRotatef(a, vec.x, vec.y, vec.z);
            return true;
        }
!       typedef RotateXYZCoords InverseType;
!       RotateXYZCoords inverseTransform() {
!           RotateXYZCoords inv;
!           inv.vec = vec;
!           inv.a = -a;
!           inv.angleWasSet();
!           return inv;
!       }
!       float nonlinearity(const ZPt &p, float radius) { 
!           return 0;
!       }
!     };
  
      /** Scale.
       * Params: sx, sy, sz
       */
!     class ScaleXYZCoords {
        ZVec vec; 
+       friend class DistortCoords;
      public:
        enum { NParams = 3 };
!       template<class Ptr> void setParams(Ptr p) {
!           vec.x = p[0];
!           vec.y = p[1];
!           vec.z = p[2];
        }
        /** Perform the internal transformation of this 
         * coordsys.
***************
*** 199,222 ****
            to.y = from.y * vec.y;
            to.z = from.z * vec.z;
        }
!       virtual ZPt transform(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           return super->transform(mp);
!       }
!       virtual void vertex(const ZPt &p) const {
!           ZPt mp;
!           tr(p, mp);
!           super->vertex(mp);
!       }
!       virtual bool canPerformGL() { return super->canPerformGL(); }
          virtual bool performGL() {
-           if(!super->performGL()) return false;
            glScalef(vec.x, vec.y, vec.z);
            return true;
        }
      } ;
-     ;
  
  
      /** Distorted coordinate system.
--- 270,292 ----
            to.y = from.y * vec.y;
            to.z = from.z * vec.z;
        }
!       virtual bool canPerformGL() { return true; }
          virtual bool performGL() {
            glScalef(vec.x, vec.y, vec.z);
            return true;
        }
+       typedef ScaleXYZCoords InverseType;
+       ScaleXYZCoords inverseTransform() {
+           ScaleXYZCoords inv;
+           inv.vec.x = 1/vec.x; // XXX Numerical accuracy?
+           inv.vec.y = 1/vec.y;
+           inv.vec.z = 1/vec.z;
+           return inv;
+       }
+       float nonlinearity(const ZPt &p, float radius) { 
+           return 0;
+       }
      } ;
  
  
      /** Distorted coordinate system.
***************
*** 224,230 ****
       * W and h give the width and height in the inside coordinate system
       * of the zoomed area.
       */
!     class DistortCoords : public CoordSys {
        float x, y;
        float w, h;
        float mmin;
--- 294,300 ----
       * W and h give the width and height in the inside coordinate system
       * of the zoomed area.
       */
!     class DistortCoords {
        float x, y;
        float w, h;
        float mmin;
***************
*** 232,257 ****
        Fisheye::vector_mag_isotropic<Fisheye::scalar_mag_atan> distort;
      public:
        enum { NParams = 6 };
!       virtual void setParams(float *params) {
!           CoordSys::setParams(params);
!           x = params[0];
!           y = params[1];
!           mmax = exp(params[2]);
!           mmin = exp(params[3]);
!           w = params[4];
!           h = params[5];
            distort.f = Fisheye::scalar_mag_atan(mmax / mmin);
        }
-       virtual ZPt transform(const ZPt &p) const {
-           ZPt mp;
-           tr(p, mp);
-           return super->transform(mp);
-       }
-       virtual void vertex(const ZPt &p) const {
-           ZPt mp;
-           tr(p, mp);
-           super->vertex(mp);
-       }
        void tr(const ZPt &from, ZPt &to) const {
            ZPt p = ZPt((from.x-x) / w, (from.y-y)/ h, from.z);
            to = distort(p);
--- 302,316 ----
        Fisheye::vector_mag_isotropic<Fisheye::scalar_mag_atan> distort;
      public:
        enum { NParams = 6 };
!       template<class Ptr> void setParams(Ptr p) {
!           x = p[0];
!           y = p[1];
!           mmax = exp(p[2]);
!           mmin = exp(p[3]);
!           w = p[4];
!           h = p[5];
            distort.f = Fisheye::scalar_mag_atan(mmax / mmin);
        }
        void tr(const ZPt &from, ZPt &to) const {
            ZPt p = ZPt((from.x-x) / w, (from.y-y)/ h, from.z);
            to = distort(p);
***************
*** 260,266 ****
            to.y *= mmin;
            to.x += x; to.y += y;
        }
!       virtual float nonlinearity(const ZPt &p, float radius) { 
            float wh = 0.5*(w+h);
            float dist = hypot((p.x-x)/w, (p.y-y)/h) - radius/wh;
            if(dist < 0) dist = 0;
--- 319,334 ----
            to.y *= mmin;
            to.x += x; to.y += y;
        }
!       typedef ScaleXYZCoords InverseType; // XXX !!!
!       ScaleXYZCoords inverseTransform() {
!           ScaleXYZCoords inv;
!           inv.vec = ZVec(1,1,1); // XXX!
!           return inv;
!       }
!       bool canPerformGL() { return false; }
!       bool performGL() { return false; }
! 
!       float nonlinearity(const ZPt &p, float radius) { 
            float wh = 0.5*(w+h);
            float dist = hypot((p.x-x)/w, (p.y-y)/h) - radius/wh;
            if(dist < 0) dist = 0;
***************
*** 275,281 ****
      template<class C> class Factory : public SomeFactory {
      public:
        virtual int nparams() { return C::NParams; }
!       virtual CoordSys *create() { return new C(); }
      };
  
      extern SomeFactory* facs[];
--- 343,349 ----
      template<class C> class Factory : public SomeFactory {
      public:
        virtual int nparams() { return C::NParams; }
!       virtual CoordSys *create() { return new TransformCoordSys<C>(); }
      };
  
      extern SomeFactory* facs[];
Index: gzz/gfx/libcoords/Coords.hxx
diff -c gzz/gfx/libcoords/Coords.hxx:1.9 gzz/gfx/libcoords/Coords.hxx:1.10
*** gzz/gfx/libcoords/Coords.hxx:1.9    Mon Sep 30 04:15:42 2002
--- gzz/gfx/libcoords/Coords.hxx        Tue Oct  1 03:55:44 2002
***************
*** 11,25 ****
      class CoordSys {
      protected:
        CoordSys *super;
-       float *params;
        CoordSys *inverse;
      public:
        virtual void setSuper(CoordSys *super) {
            this->super = super;
        }
!       virtual void setParams(float *params) {
!           this->params = params;
!       }
  
        /** Get the inverse of this coordinate system.
         * Always returns non-null but it is not guaranteed
--- 11,24 ----
      class CoordSys {
      protected:
        CoordSys *super;
        CoordSys *inverse;
      public:
        virtual void setSuper(CoordSys *super) {
            this->super = super;
        }
!       /** Always call setSuper first!
!        */
!       virtual void setParams(float *params) = 0;
  
        /** Get the inverse of this coordinate system.
         * Always returns non-null but it is not guaranteed
***************
*** 74,80 ****
         */
        virtual bool performGL() ;
  
!       CoordSys() : super(0), params(0), inverse(0) {
        }
        virtual ~CoordSys() { 
            if(inverse) delete inverse;
--- 73,79 ----
         */
        virtual bool performGL() ;
  
!       CoordSys() : super(0), inverse(0) {
        }
        virtual ~CoordSys() { 
            if(inverse) delete inverse;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]