vertex.c++ 3.31 KB
#include "vertex.h"

const vertex& vertex::operator=(const vertex& v) {
    if(this==&v)
        return *this;

    original=v.original;
    transformed=v.transformed;
    trans_stage=v.trans_stage;

    return *this;
}

vertex operator*(const double& a, const vertex& v) {
    return v * a;
}

vertex vertex::operator*(const double& a) const {
    vertex v=*this;
    v.original=v.original*a;
    v.transformed=v.transformed*a;
    return v;
}

double vertex::operator% (const vertex& v) const {
    return transformed%v.transformed;
}

vertex vertex::operator| (const vertex& v) const {
    vertex w=*this;
    w.original=w.original|v.original;
    w.transformed=w.transformed|v.transformed;
    return w;
}

vertex vertex::operator+ (const vertex& v) const {
    vertex w=*this;
    w.original=w.original+v.original;
    w.transformed=w.transformed+v.transformed;
    return w;
}

vertex vertex::operator- (const vertex& v) const {
    vertex w=*this;
    w.original=w.original-v.original;
    w.transformed=w.transformed-v.transformed;
    return w;
}

vertex vertex::norm(void) const { 
    vertex w;
    w.original=original*(1/original.v_betr()); 
    w.transformed=transformed*(1/transformed.v_betr());
    return w;
}

double vertex::betr(void) const { 
    return transformed.v_betr(); 
}

void vertex::reset(void) { 
    if(trans_stage!=0) {
        trans_stage=0; 
        transformed=original; 
    }
}

void vertex::transform(const Mmn<double>& t_mat, int p) {
   if(trans_stage < p) {
      transformed=t_mat % transformed;
      trans_stage=p;
   }
}

void vertex::project_2d(double lcx, double sw, double sh, 
                        double ph_ar, double sy, int p) {
    if(trans_stage < p) {
        transformed[X]=transformed[X]/transformed[Z] * 
                       (lcx * sw) + 0.5*sw;
        transformed[Y]=-transformed[Y]/transformed[Z] * 
                       (lcx * ph_ar * sh) + 0.5*sy*sh;
        transformed[Z]=1/transformed[Z]; // 1/Z ist linear im projezierten
                                         // Polygon kann ich also über die
                                         // Polygonkanten interpolieren
                                         // brauche ich fuer z-Buffer, 
                                         // Texturemapping und evtl. noch mehr.

        trans_stage=p;
    }
}

inline double vertex::unproject_X(double tx, double tz, 
                                    double lcx, double sw) {
   return (tx - 0.5*sw) / (lcx * sw) / tz;
}

inline double vertex::unproject_Y(double ty, double tz,
                                    double lcx, double sh, 
                                    double ph_ar, double sy) {
   return (ty - 0.5*sy*sh) / (lcx * ph_ar * sh) / tz;
}

inline double vertex::unproject_Z(double tz) {
   return 1/tz;
}

inline double vertex::unproject_U(double tu, double tz, 
                                    double lcx, double sw) {
   return (tu - 0.5*sw) / (lcx * sw) / tz;
}

inline double vertex::unproject_V(double tv, double tz,
                                    double lcx, double sh, 
                                    double ph_ar, double sy) {
   return (tv - 0.5*sy*sh) / (lcx * ph_ar * sh) / tz;
}

void vertex_list::reset(void) {
    for(unsigned i=0; i<count; i++)
        content[i].reset();

    if(variable) {
       delete [] variable;
       variable=NULL;
       var_count=0;
    }
}