Changeset 3628


Ignore:
Timestamp:
Apr 11, 2013, 3:26:04 PM (7 years ago)
Author:
ldelgass
Message:

Some cleanups in vrmath lib, e.g. add copy/assignment ops

Location:
trunk/packages/vizservers/nanovis/vrmath
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/vrmath/BBox.cpp

    r3494 r3628  
    2020}
    2121
    22 BBox::BBox(const BBox& bbox)
     22BBox::BBox(const BBox& other) :
     23    min(other.min),
     24    max(other.max)
    2325{
    24     min = bbox.min;
    25     max = bbox.max;     
    2626}
    2727
    28 BBox::BBox(const Vector3f& minv, const Vector3f& maxv)
     28BBox::BBox(const Vector3f& minv, const Vector3f& maxv) :
     29    min(minv),
     30    max(maxv)
    2931{
    30     min = minv;
    31     max = maxv;
    3232}
    3333
     
    4040bool BBox::isEmpty()
    4141{
    42     if ((min.x > max.x) || (min.y > max.y) || (min.z > max.z)) {
    43         return true;
    44     }
     42    return (isEmptyX() && isEmptyY() && isEmptyZ());
     43}
    4544
    46     return false;
     45bool BBox::isEmptyX()
     46{
     47    return (min.x > max.x);
     48}
     49
     50bool BBox::isEmptyY()
     51{
     52    return (min.y > max.y);
     53}
     54
     55bool BBox::isEmptyZ()
     56{
     57    return (min.z > max.z);
    4758}
    4859
     
    95106void BBox::transform(const BBox& box, const Matrix4x4d& mat)
    96107{
    97     float halfSizeX = (box.max.x - box.min.x) * 0.5f;
    98     float halfSizeY = (box.max.y - box.min.y) * 0.5f;
    99     float halfSizeZ = (box.max.z - box.min.z) * 0.5f;
    100 
    101     float centerX = (box.max.x + box.min.x) * 0.5f;
    102     float centerY = (box.max.y + box.min.y) * 0.5f;
    103     float centerZ = (box.max.z + box.min.z) * 0.5f;
     108    float x0 = box.min.x;
     109    float y0 = box.min.y;
     110    float z0 = box.min.z;
     111    float x1 = box.max.x;
     112    float y1 = box.max.y;
     113    float z1 = box.max.z;
    104114
    105115    Vector4f points[8];
    106116
    107     points[0].set(centerX + halfSizeX, centerY + halfSizeY, centerZ + halfSizeZ, 1);
    108     points[1].set(centerX + halfSizeX, centerY + halfSizeY, centerZ - halfSizeZ, 1);
    109     points[2].set(centerX - halfSizeX, centerY + halfSizeY, centerZ - halfSizeZ, 1);
    110     points[3].set(centerX - halfSizeX, centerY + halfSizeY, centerZ + halfSizeZ, 1);
    111     points[4].set(centerX - halfSizeX, centerY - halfSizeY, centerZ + halfSizeZ, 1);
    112     points[5].set(centerX - halfSizeX, centerY - halfSizeY, centerZ - halfSizeZ, 1);
    113     points[6].set(centerX + halfSizeX, centerY - halfSizeY, centerZ - halfSizeZ, 1);
    114     points[7].set(centerX + halfSizeX, centerY - halfSizeY, centerZ + halfSizeZ, 1);
     117    points[0].set(x0, y0, z0, 1);
     118    points[1].set(x1, y0, z0, 1);
     119    points[2].set(x0, y1, z0, 1);
     120    points[3].set(x0, y0, z1, 1);
     121    points[4].set(x1, y1, z0, 1);
     122    points[5].set(x1, y0, z1, 1);
     123    points[6].set(x0, y1, z1, 1);
     124    points[7].set(x1, y1, z1, 1);
    115125
    116     float minX, minY, minZ;
    117     float maxX, maxY, maxZ;
     126    float minX = FLT_MAX, minY = FLT_MAX, minZ = FLT_MAX;
     127    float maxX = -FLT_MAX, maxY = -FLT_MAX, maxZ = -FLT_MAX;
    118128
    119     points[0] = mat.transform(points[0]);
    120 
    121     minX = maxX = points[0].x;
    122     minY = maxY = points[0].y;
    123     minZ = maxZ = points[0].z;
    124 
    125     for (int i = 1; i < 8; i++) {
     129    for (int i = 0; i < 8; i++) {
    126130        points[i] = mat.transform(points[i]);
    127131
    128132        if (points[i].x > maxX) maxX = points[i].x;
    129         else if (points[i].x < minX) minX = points[i].x;
    130 
     133        if (points[i].x < minX) minX = points[i].x;
    131134        if (points[i].y > maxY) maxY = points[i].y;
    132         else if (points[i].y < minY) minY = points[i].y;
    133 
     135        if (points[i].y < minY) minY = points[i].y;
    134136        if (points[i].z > maxZ) maxZ = points[i].z;
    135         else if (points[i].z < minZ) minZ = points[i].z;
     137        if (points[i].z < minZ) minZ = points[i].z;
    136138    }
    137139
     
    149151}
    150152
    151 bool BBox::intersect(const Vector3f& point)
     153bool BBox::contains(const Vector3f& point)
    152154{
    153155    if ((point.x < min.x) || (point.x > max.x)) return false;
  • trunk/packages/vizservers/nanovis/vrmath/Makefile.in

    r3567 r3628  
    4040VRMATHLIB       = vrmath.a
    4141
    42 #               BBox.o \
    4342#               BPlane.o \
    4443#               Projection.o \
    4544
    4645OBJS            = \
     46                BBox.o \
    4747                LineSegment.o \
    4848                Matrix4x4f.o \
  • trunk/packages/vizservers/nanovis/vrmath/Matrix4x4d.cpp

    r3492 r3628  
    946946}
    947947
    948 void Matrix4x4d::setFloat(float *m)
     948void Matrix4x4d::setFloat(const float *m)
    949949{
    950950    for (int i = 0; i < 16; ++i) {
  • trunk/packages/vizservers/nanovis/vrmath/Matrix4x4f.cpp

    r3492 r3628  
    871871}
    872872
    873 void Matrix4x4f::setDouble(double *m)
     873void Matrix4x4f::setDouble(const double *m)
    874874{
    875875    for (int i = 0; i < 16; ++i) {
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/BBox.h

    r3494 r3628  
    3535    BBox(const Vector3f& min, const Vector3f& max);
    3636
     37    BBox& operator=(const BBox& other)
     38    {
     39        if (&other != this) {
     40            min = other.min;
     41            max = other.max;
     42        }
     43        return *this;
     44    }
     45
    3746    /**
    3847     * @brief make an empty bounding box
     
    5160     */
    5261    bool isEmpty();
     62    bool isEmptyX();
     63    bool isEmptyY();
     64    bool isEmptyZ();
    5365
    5466    /**
     
    6375
    6476    /**
    65      * @brief transform a bounding box with an matrix and set the bounding box
     77     * @brief transform a bounding box with a matrix and set the bounding box
    6678     */
    6779    void transform(const BBox& box, const Matrix4x4d& mat);
     
    7385
    7486    /**
    75      * @brief check if the bounding box intersect with a point
     87     * @brief check if the bounding box contains a point
    7688     */
    77     bool intersect(const Vector3f& point);
     89    bool contains(const Vector3f& point);
    7890
    7991    float getRadius() const;
     
    93105{
    94106    Vector3f temp;
    95     temp.x = (max.x+ min.x) * 0.5f;
     107    temp.x = (max.x + min.x) * 0.5f;
    96108    temp.y = (max.y + min.y) * 0.5f;
    97109    temp.z = (max.z + min.z) * 0.5f;
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Matrix4x4d.h

    r3492 r3628  
    3030    {
    3131        set(m);
     32    }
     33
     34    Matrix4x4d(const Matrix4x4d& other)
     35    {
     36        set(other.get());
     37    }
     38
     39    Matrix4x4d& operator=(const Matrix4x4d& other)
     40    {
     41        if (&other != this) {
     42            set(other.get());
     43        }
     44        return *this;
    3245    }
    3346
     
    113126     * @param m float matrix values
    114127     */
    115     void set(double *m);
     128    void set(const double *m);
    116129
    117     void setFloat(float *m);
     130    void setFloat(const float *m);
    118131
    119132    void print() const;
     
    136149}
    137150
    138 inline void Matrix4x4d::set(double *m)
     151inline void Matrix4x4d::set(const double *m)
    139152{
    140153    memcpy(_data, m, sizeof(double) * 16);
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Matrix4x4f.h

    r3492 r3628  
    3030    {
    3131        set(m);
     32    }
     33
     34    Matrix4x4f(const Matrix4x4f& other)
     35    {
     36        set(other.get());
     37    }
     38
     39    Matrix4x4f& operator=(const Matrix4x4f& other)
     40    {
     41        if (&other != this) {
     42            set(other.get());
     43        }
     44        return *this;
    3245    }
    3346
     
    113126     * @param m float matrix values
    114127     */
    115     void set(float *m);
     128    void set(const float *m);
    116129
    117130    /**
     
    119132     * @param m float matrix values
    120133     */
    121     void setDouble(double *m);
     134    void setDouble(const double *m);
    122135
    123136    void print() const;
     
    140153}
    141154
    142 inline void Matrix4x4f::set(float *m)
     155inline void Matrix4x4f::set(const float *m)
    143156{
    144157    memcpy(_data, m, sizeof(float) * 16);
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Quaternion.h

    r3492 r3628  
    2121    Quaternion(double x, double y, double z, double w);
    2222
    23     void set(double x1, double y1, double z1, double w1);
     23    Quaternion(const Quaternion& other) :
     24        x(other.x), y(other.y), z(other.z), w(other.w)
     25    {}
     26
     27    Quaternion& operator=(const Quaternion& other)
     28    {
     29        if (&other != this) {
     30            set(other.x, other.y, other.z, other.w);
     31        }
     32        return *this;
     33    }
     34
     35    void set(double x, double y, double z, double w);
    2436
    2537    const Quaternion& set(const Rotation& rot);
     
    7789}
    7890
    79 inline void Quaternion::set(double x1, double y1, double z1, double w1)
     91inline void Quaternion::set(double x, double y, double z, double w)
    8092{
    81     x = x1;
    82     y = y1;
    83     z = z1;
    84     w = w1;
     93    this->x = x;
     94    this->y = y;
     95    this->z = z;
     96    this->w = w;
    8597}
    8698
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Rotation.h

    r3492 r3628  
    2828    {}
    2929
    30     Rotation(const Rotation& rotation) :
    31         x(rotation.x), y(rotation.y), z(rotation.z), angle(rotation.angle)
     30    Rotation(const Rotation& other) :
     31        x(other.x), y(other.y), z(other.z), angle(other.angle)
    3232    {}
     33
     34    Rotation& operator=(const Rotation& other)
     35    {
     36        if (&other != this) {
     37            set(other.x, other.y, other.z, other.angle);
     38        }
     39        return *this;
     40    }
    3341
    3442    double getX() const
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Vector2f.h

    r3627 r3628  
    3030    Vector2f& operator=(const Vector2f& other)
    3131    {
    32         set(other.x, other.y);
     32        if (&other != this) {
     33            set(other.x, other.y);
     34        }
    3335        return *this;
    3436    }
    3537
    36     void set(float x1, float y1);
     38    void set(float x, float y);
    3739
    3840    void set(const Vector2f& v);
     
    4446    double distance(const Vector2f& v) const;
    4547
    46     double distance(float x1, float y1) const;
     48    double distance(float x, float y) const;
    4749
    4850    double distanceSquare(const Vector2f& v) const;
    4951
    50     double distanceSquare(float x1, float y1) const;
     52    double distanceSquare(float x, float y) const;
    5153
    5254    float x, y;
    5355};
    5456
    55 inline void Vector2f::set(float x1, float y1)
     57inline void Vector2f::set(float x, float y)
    5658{
    57     x = x1;
    58     y = y1;
     59    this->x = x;
     60    this->y = y;
    5961}
    6062
     
    8183}
    8284
    83 inline double Vector2f::distance(float x1, float y1) const
     85inline double Vector2f::distance(float x, float y) const
    8486{       
    85     double x2 = ((double)x1 - (double)x) , y2 = ((double)y1 - (double)y);
    86     return sqrt(x2 * x2 + y2 * y2);
     87    double x1 = ((double)x - (double)this->x) , y1 = ((double)y - (double)this->y);
     88    return sqrt(x1 * x1 + y1 * y1);
    8789}
    8890
     
    9395}
    9496
    95 inline double Vector2f::distanceSquare(float x1, float y1) const
     97inline double Vector2f::distanceSquare(float x, float y) const
    9698{       
    97     double x2 = ((double)x1 - (double)x) , y2 = ((double)y1 - (double)y);
    98     return (x2 * x2 + y2 * y2);
     99    double x1 = ((double)x - (double)this->x) , y1 = ((double)y - (double)this->y);
     100    return (x1 * x1 + y1 * y1);
    99101}
    100102
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Vector3f.h

    r3627 r3628  
    3030    Vector3f& operator=(const Vector3f& other)
    3131    {
    32         set(other.x, other.y, other.z);
     32        if (&other != this) {
     33            set(other.x, other.y, other.z);
     34        }
    3335        return *this;
    3436    }
    3537
    36     void set(float x1, float y1, float z1);
    37 
    38     void set(const Vector3f& v3);
     38    void set(float x, float y, float z);
     39
     40    void set(const Vector3f& v);
    3941
    4042    double length() const;
    4143
    42     double distance(const Vector3f& v3) const;
    43 
    44     double distance(float x1, float y1, float z1) const;
    45 
    46     double distanceSquare(const Vector3f& v3) const;
    47 
    48     double distanceSquare(float x1, float y1, float z1) const;
     44    double distance(const Vector3f& v) const;
     45
     46    double distance(float x, float y, float z) const;
     47
     48    double distanceSquare(const Vector3f& v) const;
     49
     50    double distanceSquare(float x, float y, float z) const;
    4951
    5052    double dot(const Vector3f& v) const;
    5153
    52     Vector3f cross(const Vector3f& v1) const;
     54    Vector3f cross(const Vector3f& v) const;
    5355
    5456    Vector3f normalize() const;
    5557
    56     Vector3f scale(const Vector3f& sc) const;
     58    Vector3f scale(const Vector3f& scale) const;
    5759
    5860    Vector3f scale(float scale) const;
     
    172174#endif
    173175
    174 inline void Vector3f::set(float x1, float y1, float z1)
    175 {
    176     x = x1;
    177     y = y1;
    178     z = z1;
    179 }
    180 
    181 inline void Vector3f::set(const Vector3f& v3)
    182 {
    183     x = v3.x;
    184     y = v3.y;
    185     z = v3.z;
     176inline void Vector3f::set(float x, float y, float z)
     177{
     178    this->x = x;
     179    this->y = y;
     180    this->z = z;
     181}
     182
     183inline void Vector3f::set(const Vector3f& v)
     184{
     185    x = v.x;
     186    y = v.y;
     187    z = v.z;
    186188}
    187189
     
    196198}
    197199
    198 inline double Vector3f::distance(const Vector3f& v3) const
    199 {
    200     double x1 = ((double)v3.x - (double)x) , y1 = ((double)v3.y - (double)y), z1 = ((double)v3.z - (double)z);
     200inline double Vector3f::distance(const Vector3f& v) const
     201{
     202    double x1 = ((double)v.x - (double)x) , y1 = ((double)v.y - (double)y), z1 = ((double)v.z - (double)z);
    201203    return sqrt(x1 * x1 + y1 * y1 + z1 * z1);
    202204}
    203205
    204 inline double Vector3f::distance(float x1, float y1, float z1) const
     206inline double Vector3f::distance(float x, float y, float z) const
    205207{       
    206     double x2 = ((double)x1 - (double)x) , y2 = ((double)y1 - (double)y), z2 = ((double)z1 - (double)z);
    207     return sqrt(x2 * x2 + y2 * y2 + z2 * z2);
    208 }
    209 
    210 inline double Vector3f::distanceSquare(const Vector3f& v3) const
     208    double x1 = ((double)x - (double)this->x) , y1 = ((double)y - (double)this->y), z1 = ((double)z - (double)this->z);
     209    return sqrt(x1 * x1 + y1 * y1 + z1 * z1);
     210}
     211
     212inline double Vector3f::distanceSquare(const Vector3f& v) const
    211213{       
    212     double x1 = ((double)v3.x - (double)x) , y1 = ((double)v3.y - (double)y), z1 = ((double)v3.z - (double)z);
     214    double x1 = ((double)v.x - (double)x) , y1 = ((double)v.y - (double)y), z1 = ((double)v.z - (double)z);
    213215    return (x1 * x1 + y1 * y1 + z1 * z1);
    214216}
    215217
    216 inline double Vector3f::distanceSquare(float x1, float y1, float z1) const
     218inline double Vector3f::distanceSquare(float x, float y, float z) const
    217219{       
    218     double x2 = ((double)x1 - (double)x) , y2 = ((double)y1 - (double)y), z2 = ((double)z1 - (double)z);
    219     return (x2 * x2 + y2 * y2 + z2 * z2);
     220    double x1 = ((double)x - (double)this->x) , y1 = ((double)y - (double)this->y), z1 = ((double)z - (double)this->z);
     221    return (x1 * x1 + y1 * y1 + z1 * z1);
    220222}
    221223
     
    242244}
    243245
    244 inline Vector3f Vector3f::scale(const Vector3f& sc) const
    245 {
    246     return Vector3f(x * sc.x, y * sc.y, z * sc.z);
     246inline Vector3f Vector3f::scale(const Vector3f& scale) const
     247{
     248    return Vector3f(x * scale.x, y * scale.y, z * scale.z);
    247249}
    248250
  • trunk/packages/vizservers/nanovis/vrmath/include/vrmath/Vector4f.h

    r3627 r3628  
    2424    {}
    2525
    26     Vector4f(const Vector3f& v3, float w1) :
    27         x(v3.x), y(v3.y), z(v3.z), w(w1)
     26    Vector4f(const Vector3f& v, float w1) :
     27        x(v.x), y(v.y), z(v.z), w(w1)
    2828    {}
    2929
     
    3232    {}
    3333
    34     void set(float x1, float y1, float z1, float w1);
     34    void set(float x, float y, float z, float w);
    3535 
    3636    void set(const Vector3f& v, float w);
    3737
    38     void set(const Vector4f& v4);
     38    void set(const Vector4f& v);
    3939
    4040    Vector4f& operator=(const Vector4f& other)
    4141    {
    42         set(other.x, other.y, other.z, other.w);
     42        if (&other != this) {
     43            set(other.x, other.y, other.z, other.w);
     44        }
    4345        return *this;
    4446    }
     
    9698}
    9799
    98 inline void Vector4f::set(float x1, float y1, float z1, float w1)
     100inline void Vector4f::set(float x, float y, float z, float w)
    99101{
    100     x = x1;
    101     y = y1;
    102     z = z1;
    103     w = w1;
     102    this->x = x;
     103    this->y = y;
     104    this->z = z;
     105    this->w = w;
    104106}
    105107
    106 inline void Vector4f::set(const Vector4f& v4)
    107 {
    108     x = v4.x;
    109     y = v4.y;
    110     z = v4.z;
    111     w = v4.w;
    112 }
    113 
    114 inline void Vector4f::set(const Vector3f& v, float w1)
     108inline void Vector4f::set(const Vector4f& v)
    115109{
    116110    x = v.x;
    117111    y = v.y;
    118112    z = v.z;
    119     w = w1;
     113    w = v.w;
    120114}
    121115
    122 inline float Vector4f::dot(const Vector4f& vec) const
     116inline void Vector4f::set(const Vector3f& v, float w)
    123117{
    124     return (x * vec.x + y * vec.y + z * vec.z + w * vec.w);
     118    x = v.x;
     119    y = v.y;
     120    z = v.z;
     121    this->w = w;
     122}
     123
     124inline float Vector4f::dot(const Vector4f& v) const
     125{
     126    return (x * v.x + y * v.y + z * v.z + w * v.w);
    125127}
    126128
Note: See TracChangeset for help on using the changeset viewer.