Commit 59b9977e authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD #78] Added missing functions and their test. This should preliminarily

finish the implementation of WValue and WVector3D and thus #78.
parent 8181010d
......@@ -91,6 +91,14 @@ public:
return ( m_components == rhs.m_components );
}
/**
* Compares two WValues and returns true if they contain the different data.
*/
bool operator!=( const WValue& rhs ) const
{
return ( m_components != rhs.m_components );
}
/**
* Assigns the contents of its argument to the contents of this WValue.
*/
......@@ -112,7 +120,7 @@ public:
}
/**
* Subtracts a the argument componentwise to the components of this WValue
* Subtracts the argument componentwise from the components of this WValue
*/
WValue& operator-=( const WValue& rhs )
{
......@@ -212,6 +220,26 @@ public:
return normSquare;
}
/**
* Make the norm of this WValue be 1 by dividing by WValue::norm()
*/
void normalize()
{
double currentNorm = norm();
for( unsigned int i = 0; i < m_components.size(); ++i )
m_components[i] /= currentNorm;
}
/**
* Return a normalized version of the current WValue without modifying it.
*/
WValue normalized() const
{
WValue result = *this;
result.normalize();
return result;
}
protected:
private:
/**
......
......@@ -42,25 +42,39 @@ public:
* Produces a zero vector.
*/
WVector3D();
/**
* Produces a vector consisting of the given components.
*/
WVector3D( double x, double y, double z );
/**
* Copies the values of the given WVector3D.
*/
WVector3D( const WVector3D& newVector );
//
// These functions will come soon. Together with their tests.
//
// TODO(wiebel): implement the following member functions!
// const double operator*( const WVector3D &factor2 ) const;
// const WVector3D crossproduct( const WVector3D& factor2 ) const;
// const bool operator==( const WVector3D& rhs ) const;
// const bool operator!=( const WVector3D& rhs ) const;
// void normalize();
// const WVector3D normalized() const;
/**
* Compute the cross product of the current WValue with the parameter.
*/
WVector3D crossProduct( const WVector3D& factor2 ) const
{
WVector3D result;
result[0] = (*this)[1] * factor2[2] - (*this)[2] * factor2[1];
result[1] = (*this)[2] * factor2[0] - (*this)[0] * factor2[2];
result[2] = (*this)[0] * factor2[1] - (*this)[1] * factor2[0];
return result;
}
/**
* Compute the dot product of the current WValue with the parameter.
*/
double dotProduct( const WVector3D& factor2 ) const
{
double result = 0.0;
for ( unsigned int i = 0; i < 3; ++i )
result += (*this)[i] * factor2[i];
return result;
}
protected:
private:
......
......@@ -92,7 +92,7 @@ public:
}
/**
* == operator should return true if the WValues have contain the same elements and false if the don't.
* == operator should return true if the WValues contain the same elements and false if the don't.
*/
void testEqualityOperator( void )
{
......@@ -116,6 +116,31 @@ public:
TS_ASSERT_EQUALS( value1 == value2, false );
}
/**
* != operator should return false if the WValues contain the same elements and false if the don't.
*/
void testInEqualityOperator( void )
{
const size_t size = 3;
const double a = 1.2, b = 3.4, c = 5.6;
WValue< double > value1( size );
WValue< double > value2( size );
value1[0] = a;
value1[1] = b;
value1[2] = c;
value2[0] = a;
value2[1] = b;
value2[2] = c;
TS_ASSERT_EQUALS( value1 != value2, false );
value2[0] += 1;
TS_ASSERT_EQUALS( value1 != value2, true );
}
/**
* assignment operator= should assign the correct values
*/
......@@ -484,6 +509,49 @@ public:
TS_ASSERT_DELTA( value1.normSquare(), 44.36, delta );
}
/**
* test normalization of the current WValue
*/
void testNormalize( void )
{
const size_t size = 3;
const double a = 1.2, b = 3.4, c = 5.6;
WValue< double > value1( size );
value1[0] = a;
value1[1] = b;
value1[2] = c;
TS_ASSERT( std::abs( value1.norm() - 1. ) > 1e-9 );
value1.normalize();
TS_ASSERT_DELTA( value1.norm(), 1., delta );
}
/**
* test returning normalized version
*/
void testNormalized( void )
{
const size_t size = 3;
const double a = 1.2, b = 3.4, c = 5.6;
WValue< double > value1( size );
value1[0] = a;
value1[1] = b;
value1[2] = c;
WValue< double > valueCopy = value1;
TS_ASSERT( std::abs( value1.norm() - 1. ) > 1e-9 );
WValue< double > value2 = value1.normalized();
// value1 should not have been changed
TS_ASSERT( std::abs( value1.norm() - 1. ) > 1e-9 );
TS_ASSERT_EQUALS( value1, valueCopy );
// value2 should contain the normalized version
TS_ASSERT_DELTA( value2.norm(), 1., delta );
}
/**
* scaling operator, scalar left hand side
*/
......
......@@ -31,6 +31,21 @@ using wmath::WVector3D;
class WVector3DTest : public CxxTest::TestSuite
{
private:
double delta;
double deltaLarge;
public:
/**
* Called before every test.
*/
void setUp( void )
{
delta = 1e-12;
deltaLarge = 1e-10;
}
public:
/**
* Instatiation should throw nothing.
......@@ -121,6 +136,63 @@ public:
TS_ASSERT_EQUALS( vec[1], b );
TS_ASSERT_EQUALS( vec[2], c );
}
/**
* Test cross product
*/
void testCrossProduct( void )
{
double a = 32.32;
double b = 42.42;
double c = 23.23;
const WVector3D vec( a, b, c );
const WVector3D vec2( a + 1.1, b + 2.2, c + 3.3 );
WVector3D tmp;
// product with self should be zero vector
tmp = vec.crossProduct( vec );
TS_ASSERT_DELTA( tmp[0], 0., delta );
TS_ASSERT_DELTA( tmp[1], 0., delta );
TS_ASSERT_DELTA( tmp[2], 0., delta );
// test for some example
tmp = vec.crossProduct( vec2 );
TS_ASSERT_DELTA( tmp[0], 88.88, delta );
TS_ASSERT_DELTA( tmp[1], -81.103, delta );
TS_ASSERT_DELTA( tmp[2], 24.442, delta );
// ensure orthogonality
TS_ASSERT_DELTA( tmp.dotProduct( vec ), 0., deltaLarge );
TS_ASSERT_DELTA( tmp.dotProduct( vec2 ), 0., deltaLarge );
}
/**
* Test dot product
*/
void testDotProduct( void )
{
double a = 32.32;
double b = 42.42;
double c = 23.23;
const WVector3D vec( a, b, c );
const WVector3D vec2( a + 1.1, b + 2.2, c + 3.3 );
double tmp;
// product with self has to be squared norm
tmp = vec.dotProduct( vec );
TS_ASSERT_EQUALS( tmp, vec.normSquare() );
// test for some example
tmp = vec.dotProduct( vec2 );
TS_ASSERT_DELTA( tmp, 3589.2067, delta );
// product with orthogonal vector has to be zero
WVector3D tmpOrtho( 88.88, -81.103, 24.442 );
tmp = vec.dotProduct( tmpOrtho );
TS_ASSERT_DELTA( tmp, 0., delta );
}
};
#endif // WVECTOR3D_TEST_H
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