Commit cedb7a00 authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

Brings back reference semantics to tgt::Vector4::xyz() and similar:

Commit dabdc917 fixed the tgt vector and matrix implementation to follow strict aliasing rules but also changed the semantics of the sub-vector/sub-matrix getters, which previously returned references but after returned copies.
While this did not break CAMPVis code, since nobody relied on it, the original reference semantics were much more intuitive. This commit brings them back but in a valid C++ way not breaking strict aliasing rules.

closes #545
parent c8bc14d8
......@@ -79,6 +79,7 @@ struct Matrix2 {
};
T elemRowCol[2][2];
T elem[4];
Vector2<T> columns[cols];
};
/*
......@@ -162,15 +163,15 @@ struct Matrix2 {
*/
/// return arbitrary row-vector similar to GLSL. <br>
/// BUT REMEMBER: GLSL gives you COLS NOT ROWS!
Vector2<T> operator [] (size_t i) const {
tgtAssert(i < size, "i must be less than size");
return Vector2<T>(elemRowCol[i]);
const Vector2<T>& operator [] (size_t i) const {
tgtAssert(i < cols, "i must be less than size");
return columns[i];
}
/// return arbitrary row-vector similar to GLSL. <br>
/// BUT REMEMBER: GLSL gives you COLS NOT ROWS!
Vector2<T> operator [] (size_t i) {
tgtAssert(i < size, "i must be less than size");
return Vector2<T>(elemRowCol[i]);
Vector2<T>& operator [] (size_t i) {
tgtAssert(i < cols, "i must be less than size");
return columns[i];
}
};
......@@ -218,6 +219,7 @@ struct Matrix3 {
};
T elemRowCol[3][3];
T elem[9];
Vector3<T> columns[cols];
};
/*
......@@ -361,15 +363,15 @@ struct Matrix3 {
*/
/// return arbitrary row-vector similar to GLSL. <br>
/// BUT REMEMBER: GLSL gives you COLS NOT ROWS!
Vector3<T> operator [] (size_t i) const {
tgtAssert(i < size, "i must be less than size");
return Vector3<T>(elemRowCol[i]);
const Vector3<T>& operator [] (size_t i) const {
tgtAssert(i < cols, "i must be less than size");
return columns[i];
}
/// return arbitrary row-vector similar to GLSL. <br>
/// BUT REMEMBER: GLSL gives you COLS NOT ROWS!
Vector3<T> operator [] (size_t i) {
tgtAssert(i < size, "i must be less than size");
return Vector3<T>(elemRowCol[i]);
Vector3<T>& operator [] (size_t i) {
tgtAssert(i < cols, "i must be less than size");
return columns[i];
}
};
......@@ -448,6 +450,7 @@ struct Matrix4 {
};
T elemRowCol[4][4];
T elem[16];
Vector4<T> columns[cols];
};
/*
......@@ -630,15 +633,15 @@ struct Matrix4 {
*/
/// return arbitrary row-vector similar to GLSL. <br>
/// BUT REMEMBER: GLSL gives you COLS NOT ROWS!
Vector4<T> operator [] (size_t i) const {
tgtAssert(i < size, "i must be less than size");
return Vector4<T>(elemRowCol[i]);
const Vector4<T>& operator [] (size_t i) const {
tgtAssert(i < cols, "i must be less than size");
return columns[i];
}
/// return arbitrary row-vector similar to GLSL. <br>
/// BUT REMEMBER: GLSL gives you COLS NOT ROWS!
Vector4<T> operator [] (size_t i) {
tgtAssert(i < size, "i must be less than size");
return Vector4<T>(elemRowCol[i]);
Vector4<T>& operator [] (size_t i) {
tgtAssert(i < cols, "i must be less than size");
return columns[i];
}
/**
......
......@@ -213,6 +213,9 @@ struct Vector2 {
for (size_t i = 0; i < v.size; ++i)
elem[i] = T(v.elem[i]);
}
static Vector2<T> createAsReference(T* v) {
}
/// Index operator
const T& operator [] (size_t index) const {
......@@ -263,9 +266,13 @@ struct Vector3 {
// workaround to prevent "dereferencing type-punned pointer will break strict-aliasing
// rules" warning with gcc and -fstrict-aliasing.
struct {
T _xy[size-1];
Vector2<T> _xy;
T _z;
};
struct {
T _x;
Vector2<T> _yz;
};
T elem[size];
};
......@@ -322,17 +329,17 @@ struct Vector3 {
/*
sub-vector getters
*/
Vector2<T> xy() const {
return Vector2<T>(_xy);
const Vector2<T>& xy() const {
return _xy;
}
Vector2<T> xy() {
return Vector2<T>(_xy);
Vector2<T>& xy() {
return _xy;
}
Vector2<T> yz() const {
return Vector2<T>(elem + 1);
const Vector2<T>& yz() const {
return _yz;
}
Vector2<T> yz() {
return Vector2<T>(elem + 1);
Vector2<T>& yz() {
return _yz;
}
/*
......@@ -380,9 +387,22 @@ struct Vector4 {
// workaround to prevent "dereferencing type-punned pointer will break strict-aliasing
// rules" warning with gcc and -fstrict-aliasing.
struct {
T _xyz[size-1];
Vector3<T> _xyz;
T _w;
};
struct {
T _x;
Vector3<T> _yzw;
};
struct {
Vector2<T> _xy;
Vector2<T> _zw;
};
struct {
T __x;
Vector2<T> _yz;
T __w;
};
T elem[size];
};
......@@ -467,35 +487,35 @@ struct Vector4 {
sub-vector getters
*/
Vector2<T> xy() const {
return xyz().xy();
const Vector2<T>& xy() const {
return _xy;
}
Vector2<T> yz() const {
return Vector2<T>(elem + 1);
const Vector2<T>& yz() const {
return _yz;
}
Vector2<T> zw() const {
return Vector2<T>(elem + 2);
const Vector2<T>& zw() const {
return _zw;
}
Vector3<T> xyz() const {
return Vector3<T>(_xyz);
const Vector3<T>& xyz() const {
return _xyz;
}
Vector3<T> yzw() const {
return Vector3<T>(elem + 1);
const Vector3<T>& yzw() const {
return _yzw;
}
Vector2<T> xy() {
return xyz().xy();
return _xy;
}
Vector2<T> yz() {
return Vector2<T>(elem + 1);
return _yz;
}
Vector2<T> zw() {
return Vector2<T>(elem + 2);
return _zw;
}
Vector3<T> xyz() {
return Vector3<T>(_xyz);
return _xyz;
}
Vector3<T> yzw() {
return Vector3<T>(elem + 1);
return _yzw;
}
/*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment