add compound assignment operators for 2D and 3D vector classes

This commit is contained in:
Sébastien Loriot 2016-08-24 15:16:18 +02:00
parent 6776246089
commit 151d52bb84
6 changed files with 173 additions and 56 deletions

View File

@ -81,24 +81,6 @@ Vector_2(const Kernel::FT &x, const Kernel::FT &y);
/// @} /// @}
/// \name Operations
/// @{
/*!
Test for equality: two vectors are equal, iff their \f$ x\f$ and \f$ y\f$
coordinates are equal. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator==(const Vector_2<Kernel> &w) const;
/*!
Test for inequality. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator!=(const Vector_2<Kernel> &w) const;
/// @}
/// \name Coordinate Access /// \name Coordinate Access
/// There are two sets of coordinate access functions, namely to the /// There are two sets of coordinate access functions, namely to the
/// homogeneous and to the %Cartesian coordinates. They can be used /// homogeneous and to the %Cartesian coordinates. They can be used
@ -197,26 +179,39 @@ Vector_2<Kernel> perpendicular(const Orientation &o) const;
/// \name Operators /// \name Operators
/// @{ /// @{
/// \ingroup Kernel_operator_plus /*!
///@{ Test for equality: two vectors are equal, iff their \f$ x\f$ and \f$ y\f$
coordinates are equal. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator==(const Vector_2<Kernel> &w) const;
/*!
Test for inequality. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator!=(const Vector_2<Kernel> &w) const;
/*! /*!
Addition. Addition.
*/ */
Vector_2<Kernel> operator+(const Vector_2<Kernel> &w) const; Vector_2<Kernel> operator+(const Vector_2<Kernel> &w) const;
// @} /*!
Addition.
*/
Vector_2<Kernel>& operator+=(const Vector_2<Kernel> &w);
/// \ingroup Kernel_operator_minus
///@{
/*! /*!
Subtraction. Subtraction.
*/ */
Vector_2<Kernel> operator-(const Vector_2<Kernel> &w) const; Vector_2<Kernel> operator-(const Vector_2<Kernel> &w) const;
/// @} /*!
Subtraction.
*/
Vector_2<Kernel>& operator-=(const Vector_2<Kernel> &w);
/*! /*!
returns the opposite vector. returns the opposite vector.
@ -233,13 +228,23 @@ Division by a scalar.
*/ */
Vector_2<Kernel> operator/(const Kernel::RT &s) const; Vector_2<Kernel> operator/(const Kernel::RT &s) const;
/*!
Division by a scalar.
*/
Vector_2<Kernel>& operator/=(const Kernel::RT &s);
/*!
Multiplication by a scalar.
*/
Vector_2<Kernel>& operator*=(const Kernel::RT &s);
/// @}
/*! /*!
returns the squared length of `v`. returns the squared length of `v`.
*/ */
Kernel::FT squared_length() const; Kernel::FT squared_length() const;
/// @}
}; /* end Vector_2 */ }; /* end Vector_2 */

View File

@ -82,24 +82,6 @@ Vector_3(const Kernel::FT &x, const Kernel::FT &y, const Kernel::FT &z);
/// @} /// @}
/// \name Operations
/// @{
/*!
Test for equality: two vectors are equal, iff their \f$ x\f$, \f$ y\f$
and \f$ z\f$ coordinates are equal. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator==(const Vector_3<Kernel> &w) const;
/*!
Test for inequality. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator!=(const Vector_3<Kernel> &w) const;
/// @}
/// \name Coordinate Access /// \name Coordinate Access
/// There are two sets of coordinate access functions, namely to the /// There are two sets of coordinate access functions, namely to the
/// homogeneous and to the %Cartesian coordinates. They can be used /// homogeneous and to the %Cartesian coordinates. They can be used
@ -201,25 +183,38 @@ Direction_3<Kernel> direction() const;
/// \name Operators /// \name Operators
/// @{ /// @{
/// \ingroup Kernel_operator_plus /*!
///@{ Test for equality: two vectors are equal, iff their \f$ x\f$, \f$ y\f$
and \f$ z\f$ coordinates are equal. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator==(const Vector_3<Kernel> &w) const;
/*!
Test for inequality. You can compare a vector with the
`NULL_VECTOR`.
*/
bool operator!=(const Vector_3<Kernel> &w) const;
/*! /*!
Addition. Addition.
*/ */
Vector_3<Kernel> operator+(const Vector_3<Kernel> &w) const; Vector_3<Kernel> operator+(const Vector_3<Kernel> &w) const;
/// @} /*!
Addition.
/// \ingroup Kernel_operator_minus */
///@{ Vector_3<Kernel>& operator+=(const Vector_3<Kernel> &w);
/*! /*!
Subtraction. Subtraction.
*/ */
Vector_3<Kernel> operator-(const Vector_3<Kernel> &w) const; Vector_3<Kernel> operator-(const Vector_3<Kernel> &w) const;
/// @} /*!
Subtraction.
*/
Vector_3<Kernel>& operator-=(const Vector_3<Kernel> &w);
/*! /*!
Returns the opposite vector. Returns the opposite vector.
@ -232,17 +227,27 @@ Division by a scalar.
Vector_3<Kernel> operator/(const Kernel::RT &s) const; Vector_3<Kernel> operator/(const Kernel::RT &s) const;
/*! /*!
returns the squared length of `v`. Division by a scalar.
*/ */
Kernel::FT squared_length() const; Vector_3<Kernel>& operator/=(const Kernel::RT &s);
/*! /*!
returns the scalar product (= inner product) of the two vectors. returns the scalar product (= inner product) of the two vectors.
*/ */
Kernel::FT operator*(const Vector_3<Kernel> &w) const; Kernel::FT operator*(const Vector_3<Kernel> &w) const;
/*!
Multiplication by a scalar.
*/
Vector_3<Kernel>& operator*=(const Kernel::FT &s);
/// @} /// @}
/*!
returns the squared length of `v`.
*/
Kernel::FT squared_length() const;
}; /* end Vector_3 */ }; /* end Vector_3 */
/// \ingroup Kernel_operator_prod /// \ingroup Kernel_operator_prod

View File

@ -175,21 +175,57 @@ public:
return R().construct_difference_of_vectors_2_object()(*this,v); return R().construct_difference_of_vectors_2_object()(*this,v);
} }
Vector_2& operator-=(const Vector_2& v)
{
*this = R().construct_difference_of_vectors_2_object()(*this,v);
return *this;
}
Vector_2 operator+(const Vector_2& v) const Vector_2 operator+(const Vector_2& v) const
{ {
return R().construct_sum_of_vectors_2_object()(*this,v); return R().construct_sum_of_vectors_2_object()(*this,v);
} }
Vector_2& operator+=(const Vector_2& v)
{
*this = R().construct_sum_of_vectors_2_object()(*this,v);
return *this;
}
Vector_2 operator/(const RT& c) const Vector_2 operator/(const RT& c) const
{ {
return R().construct_divided_vector_2_object()(*this,c); return R().construct_divided_vector_2_object()(*this,c);
} }
Vector_2& operator/=(const RT& c)
{
*this = R().construct_divided_vector_2_object()(*this,c);
return *this;
}
Vector_2 operator/(const typename First_if_different<FT,RT>::Type & c) const Vector_2 operator/(const typename First_if_different<FT,RT>::Type & c) const
{ {
return R().construct_divided_vector_2_object()(*this,c); return R().construct_divided_vector_2_object()(*this,c);
} }
Vector_2& operator/=(const typename First_if_different<FT,RT>::Type & c)
{
*this = R().construct_divided_vector_2_object()(*this,c);
return *this;
}
Vector_2& operator*=(const RT& c)
{
*this = R().construct_scaled_vector_2_object()(*this,c);
return *this;
}
Vector_2& operator*=(const typename First_if_different<FT,RT>::Type & c)
{
*this = R().construct_scaled_vector_2_object()(*this,c);
return *this;
}
FT squared_length() const FT squared_length() const
{ {
return R().compute_squared_length_2_object()(*this); return R().compute_squared_length_2_object()(*this);

View File

@ -118,21 +118,57 @@ public:
return R().construct_difference_of_vectors_3_object()(*this,v); return R().construct_difference_of_vectors_3_object()(*this,v);
} }
Vector_3& operator-=(const Vector_3& v)
{
*this = R().construct_difference_of_vectors_3_object()(*this,v);
return *this;
}
Vector_3 operator+(const Vector_3& v) const Vector_3 operator+(const Vector_3& v) const
{ {
return R().construct_sum_of_vectors_3_object()(*this,v); return R().construct_sum_of_vectors_3_object()(*this,v);
} }
Vector_3& operator+=(const Vector_3& v)
{
*this = R().construct_sum_of_vectors_3_object()(*this,v);
return *this;
}
Vector_3 operator/(const RT& c) const Vector_3 operator/(const RT& c) const
{ {
return R().construct_divided_vector_3_object()(*this,c); return R().construct_divided_vector_3_object()(*this,c);
} }
Vector_3& operator/=(const RT& c)
{
*this = R().construct_divided_vector_3_object()(*this,c);
return *this;
}
Vector_3 operator/(const typename First_if_different<FT_,RT>::Type & c) const Vector_3 operator/(const typename First_if_different<FT_,RT>::Type & c) const
{ {
return R().construct_divided_vector_3_object()(*this,c); return R().construct_divided_vector_3_object()(*this,c);
} }
Vector_3& operator/=(const typename First_if_different<FT_,RT>::Type & c)
{
*this = R().construct_divided_vector_3_object()(*this,c);
return *this;
}
Vector_3& operator*=(const RT& c)
{
*this = R().construct_scaled_vector_3_object()(*this,c);
return *this;
}
Vector_3& operator*=(const typename First_if_different<FT_,RT>::Type & c)
{
*this = R().construct_scaled_vector_3_object()(*this,c);
return *this;
}
typename cpp11::result_of<typename R::Compute_x_3(Vector_3)>::type typename cpp11::result_of<typename R::Compute_x_3(Vector_3)>::type
x() const x() const
{ {

View File

@ -160,6 +160,24 @@ _test_cls_vector_2(const R& )
it2++; it2++;
assert(it == it2); assert(it == it2);
// test arithmetic operators
CGAL::Vector_2<R> v_1(2, 4);
const CGAL::Vector_2<R> v_1_const=v_1;
CGAL::Vector_2<R> v_2(1, 2);
v_1*=FT(2);
assert(v_1==v_1_const * FT(2));
v_1=v_1_const;
v_1/=FT(2);
assert(v_1==v_1_const / FT(2));
v_1=v_1_const;
v_1+=v_2;
assert(v_1==v_1_const + v_2);
v_1=v_1_const;
v_1-=v_2;
assert(v_1==v_1_const - v_2);
std::cout << "done" << std::endl; std::cout << "done" << std::endl;
return true; return true;
} }

View File

@ -156,6 +156,23 @@ _test_cls_vector_3(const R& )
it2++; it2++;
assert(it == it2); assert(it == it2);
// test arithmetic operators
CGAL::Vector_3<R> v_1(2, 4, 6);
const CGAL::Vector_3<R> v_1_const=v_1;
CGAL::Vector_3<R> v_2(1, 2, 3);
v_1*=FT(2);
assert(v_1==v_1_const * FT(2));
v_1=v_1_const;
v_1/=FT(2);
assert(v_1==v_1_const / FT(2));
v_1=v_1_const;
v_1+=v_2;
assert(v_1==v_1_const + v_2);
v_1=v_1_const;
v_1-=v_2;
assert(v_1==v_1_const - v_2);
std::cout << "done" << std::endl; std::cout << "done" << std::endl;
return true; return true;
} }