/*========================================================================= * * Copyright NumFOCUS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ #ifndef itkCovariantVector_hxx #define itkCovariantVector_hxx #include "itkMath.h" #include "itkNumericTraits.h" namespace itk { template CovariantVector::CovariantVector(const ValueType & r) : Superclass{ r } {} template CovariantVector & CovariantVector::operator=(const ValueType r[VVectorDimension]) { BaseArray::operator=(r); return *this; } template auto CovariantVector::operator+=(const Self & vec) -> const Self & { for (unsigned int i = 0; i < VVectorDimension; ++i) { (*this)[i] += vec[i]; } return *this; } template auto CovariantVector::operator-=(const Self & vec) -> const Self & { for (unsigned int i = 0; i < VVectorDimension; ++i) { (*this)[i] -= vec[i]; } return *this; } template CovariantVector CovariantVector::operator-() const { Self result; for (unsigned int i = 0; i < VVectorDimension; ++i) { result[i] = -(*this)[i]; } return result; } template auto CovariantVector::operator+(const Self & vec) const -> Self { Self result; for (unsigned int i = 0; i < VVectorDimension; ++i) { result[i] = (*this)[i] + vec[i]; } return result; } template auto CovariantVector::operator-(const Self & vec) const -> Self { Self result; for (unsigned int i = 0; i < VVectorDimension; ++i) { result[i] = (*this)[i] - vec[i]; } return result; } template typename CovariantVector::ValueType CovariantVector::operator*( const Self & other) const { typename NumericTraits::AccumulateType value = T{}; for (unsigned int i = 0; i < VVectorDimension; ++i) { value += (*this)[i] * other[i]; } return static_cast(value); } template typename CovariantVector::ValueType CovariantVector::operator*( const Vector & other) const { typename NumericTraits::AccumulateType value = T{}; for (unsigned int i = 0; i < VVectorDimension; ++i) { value += (*this)[i] * other[i]; } return value; } template auto CovariantVector::GetSquaredNorm() const -> RealValueType { RealValueType sum{}; for (unsigned int i = 0; i < VVectorDimension; ++i) { const RealValueType value = (*this)[i]; sum += value * value; } return sum; } template auto CovariantVector::GetNorm() const -> RealValueType { return std::sqrt(this->GetSquaredNorm()); } template auto CovariantVector::Normalize() -> RealValueType { const RealValueType norm = this->GetNorm(); for (unsigned int i = 0; i < VVectorDimension; ++i) { (*this)[i] /= norm; } return norm; } template void CovariantVector::SetVnlVector(const vnl_vector & v) { for (unsigned int i = 0; i < v.size(); ++i) { (*this)[i] = v(i); } } template vnl_vector_ref CovariantVector::GetVnlVector() { return vnl_vector_ref(VVectorDimension, this->GetDataPointer()); } template vnl_vector CovariantVector::GetVnlVector() const { return vnl_vector(this->GetDataPointer(), VVectorDimension); } } // end namespace itk #endif