<commit_before>//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010-2011 <NAME>
// Copyright (c) 2013 Project Chrono
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//

#ifndef CHQUATERNION_H
#define CHQUATERNION_H

//////////////////////////////////////////////////
//  
//   ChQuaternion.h
//
//   Math functions for :
//      - QUATERNIONS
//
//   HEADER file for CHRONO,
//	 Multibody dynamics engine
//
// ------------------------------------------------
//             www.deltaknowledge.com
// ------------------------------------------------
///////////////////////////////////////////////////


#include "core/ChVector.h"
#include "ChApiCE.h"


namespace chrono
{



///
/// QUATERNION:
///
/// Class defining quaternion objects, that is four-dimensional
/// numbers, also known as Eulero's parameters.
/// Quaternions are very handy when used to represent rotations in 3d.	
///
///  The quaternion object comes either with the template "ChQuaternion<type>" mode,
/// either in the 'shortcut' flavour, that is "Quaternion", which assumes
/// the type of the four scalars is double precision, so it is faster to type.
///  For example, for a declaration, you can write "ChQuaternion<double> foo;", 
/// as well as "Quaternion foo;" (less typing effort for the second..)
///

template <class Real = double>
class ChQuaternion
{
public:
	//
	// DATA
	//
	Real e0;
	Real e1;
	Real e2;
	Real e3;

	//
	// CONSTRUCTORS
	//

		/// Default constructor. 
		/// Note, it is a null quaternion {0,0,0,0}, not a {1,0,0,0} unit quaternion.
	ChQuaternion(): e0(0), e1(0), e2(0), e3(0) {}

		/// Constructor from four scalars. The first is the real part, others are i,j,k imaginary parts
	ChQuaternion(const Real ne0, const Real ne1, const Real ne2,const Real ne3) : e0(ne0), e1(ne1), e2(ne2), e3(ne3) {}

		/// Constructor from real part, and vector with i,j,k imaginary part.
	ChQuaternion(const Real ns, const ChVector<Real> nv) : e0(ns), e1(nv.x), e2(nv.y), e3(nv.z) {}

		/// Copy constructor 
	ChQuaternion(const ChQuaternion<Real>& other) : e0(other.e0),e1(other.e1),e2(other.e2),e3(other.e3) {}

		/// Copy constructor between vectors float<->double etc
	template <class RealB>
	ChQuaternion(const ChQuaternion<RealB>& other) : e0(other.e0),e1(other.e1),e2(other.e2),e3(other.e3) {}

	//
	// OPERATORS OVERLOADING
	//
	// Note: c++ automatically creates temporary objects to store intermediate
	// results in long formulas, such as a= b*c*d, so the usage of operators
	// may give slower results than a wise (less readable however) usage of
	// Dot(), Cross() etc.. Also pay attention to C++ operator precedence rules!

		/// Assignment operator: copy from another quaternion
	ChQuaternion<Real>& operator=(const ChQuaternion<Real>& other)
		{
			if (&other == this) return *this;
			e0 = other.e0;  e1 = other.e1;  e2 = other.e2;  e3 = other.e3;
			return *this;
		}

		/// Operator for sign change 
	ChQuaternion<Real> operator-() const { return ChQuaternion<Real>(-e0, -e1, -e2, -e3); }

		/// Operator for making a conjugate quaternion (the original is not changed)
		/// A conjugate quaternion has the vectorial part with changed sign
	ChQuaternion<Real>  operator!() const { return ChQuaternion<Real>(e0, -e1, -e2, -e3); }

		/// Operator for quaternion sum
	ChQuaternion<Real>  operator+(const ChQuaternion<Real>& other) const
		{
			return ChQuaternion<Real>(e0+other.e0, e1+other.e1, e2+other.e2, e3+other.e3);
		}
	ChQuaternion<Real>& operator+=(const ChQuaternion<Real>& other)
		{
			e0+=other.e0;  e1+=other.e1; e2+=other.e2;  e3+=other.e3;
			return *this;
		}

		/// Operator for quaternion difference
	ChQuaternion<Real>  operator-(const ChQuaternion<Real>& other) const
		{
			return ChQuaternion<Real>(e0-other.e0, e1-other.e1, e2-other.e2, e3-other.e3);
		}
	ChQuaternion<Real>& operator-=(const ChQuaternion<Real>& other)
		{
			e0-=other.e0;  e1-=other.e1; e2-=other.e2;  e3-=other.e3;
			return *this;
		}

		/// Operator for element-wise multiplication (note that this is neither dot product
		/// nor quaternion product! Quaternion product  is % operator!)
	ChQuaternion<Real>  operator*(const ChQuaternion<Real>& other) const
		{
			return ChQuaternion<Real>(e0*other.e0, e1*other.e1, e2*other.e2, e3*other.e3);
		}
	ChQuaternion<Real>& operator*=(const ChQuaternion<Real>& other)
		{
			e0*=other.e0;  e1*=other.e1;  e2*=other.e2;  e3*=other.e3;
			return *this;<commit_msg>IMPORTANT change in ChQuaternion API.  Previously, the * operator was used for doing element-by-element product, that was almost useless. We assume that no one used it. So, now we use the * operator to perform the true quaternion operator, as it was done with % operator previously. The old % operator is made deprecated, then. Also, we added the >> operator and its in-place >>= operator, to do the product in 'specular' order, i.e. A*B is the same as B >> A. This makes things more coherent through the Chrono::ENgine API because the * operator represents the same 'meaning' also for ChFrame, ChFrameMoving, etc. , that is, it 'concatenates' transformations in direct order (in this cases, for quaternion it concatenates rotations). Similarly, the >> operator provides a concatenation of transformations in specular order.<commit_after>//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010-2011 <NAME>
// Copyright (c) 2013 Project Chrono
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//

#ifndef CHQUATERNION_H
#define CHQUATERNION_H

//////////////////////////////////////////////////
//  
//   ChQuaternion.h
//
//   Math functions for :
//      - QUATERNIONS
//
//   HEADER file for CHRONO,
//	 Multibody dynamics engine
//
// ------------------------------------------------
//             www.deltaknowledge.com
// ------------------------------------------------
///////////////////////////////////////////////////


#include "core/ChVector.h"
#include "ChApiCE.h"


namespace chrono
{



///
/// QUATERNION:
///
/// Class defining quaternion objects, that is four-dimensional
/// numbers, also known as Eulero's parameters.
/// Quaternions are very handy when used to represent rotations in 3d.	
///
///  The quaternion object comes either with the template "ChQuaternion<type>" mode,
/// either in the 'shortcut' flavour, that is "Quaternion", which assumes
/// the type of the four scalars is double precision, so it is faster to type.
///  For example, for a declaration, you can write "ChQuaternion<double> foo;", 
/// as well as "Quaternion foo;" (less typing effort for the second..)
///

template <class Real = double>
class ChQuaternion
{
public:
	//
	// DATA
	//
	Real e0;
	Real e1;
	Real e2;
	Real e3;

	//
	// CONSTRUCTORS
	//

		/// Default constructor. 
		/// Note, it is a null quaternion {0,0,0,0}, not a {1,0,0,0} unit quaternion.
	ChQuaternion(): e0(0), e1(0), e2(0), e3(0) {}

		/// Constructor from four scalars. The first is the real part, others are i,j,k imaginary parts
	ChQuaternion(const Real ne0, const Real ne1, const Real ne2,const Real ne3) : e0(ne0), e1(ne1), e2(ne2), e3(ne3) {}

		/// Constructor from real part, and vector with i,j,k imaginary part.
	ChQuaternion(const Real ns, const ChVector<Real> nv) : e0(ns), e1(nv.x), e2(nv.y), e3(nv.z) {}

		/// Copy constructor 
	ChQuaternion(const ChQuaternion<Real>& other) : e0(other.e0),e1(other.e1),e2(other.e2),e3(other.e3) {}

		/// Copy constructor between vectors float<->double etc
	template <class RealB>
	ChQuaternion(const ChQuaternion<RealB>& other) : e0(other.e0),e1(other.e1),e2(other.e2),e3(other.e3) {}

	//
	// OPERATORS OVERLOADING
	//
	// Note: c++ automatically creates temporary objects to store intermediate
	// results in long formulas, such as a= b*c*d, so the usage of operators
	// may give slower results than a wise (less readable however) usage of
	// Dot(), Cross() etc.. Also pay attention to C++ operator precedence rules!

		/// Assignment operator: copy from another quaternion
	ChQuaternion<Real>& operator=(const ChQuaternion<Real>& other)
		{
			if (&other == this) return *this;
			e0 = other.e0;  e1 = other.e1;  e2 = other.e2;  e3 = other.e3;
			return *this;
		}

		/// Operator for sign change 
	ChQuaternion<Real> operator-() const { return ChQuaternion<Real>(-e0, -e1, -e2, -e3); }

		/// Operator for making a conjugate quaternion (the original is not changed)
		/// A conjugate quaternion has the vectorial part with changed sign
	ChQuaternion<Real>  operator!() const { return ChQuaternion<Real>(e0, -e1, -e2, -e3); }

		/// Operator for quaternion sum
	ChQuaternion<Real>  operator+(const ChQuaternion<Real>& other) const
		{
			return ChQuaternion<Real>(e0+other.e0, e1+other.e1, e2+other.e2, e3+other.e3);
		}
	ChQuaternion<Real>& operator+=(const ChQuaternion<Real>& other)
		{
			e0+=other.e0;  e1+=other.e1; e2+=other.e2;  e3+=other.e3;
			return *this;
		}

		/// Operator for quaternion difference
	ChQuaternion<Real>  operator-(const ChQuaternion<Real>& other) const
		{
			return ChQuaternion<Real>(e0-other.e0, e1-other.e1, e2-other.e2, e3-other.e3);
		}
	ChQuaternion<Real>& operator-=(const ChQuaternion<Real>& other)
		{
			e0-=other.e0;  e1-=other.e1; e2-=other.e2;  e3-=other.e3;
			return *this;
		}

		/// NOTE
		/// The following * and *= operators had a different behaviour prior to 13/9/2014, 
		/// but we assume no one used * *= in that previous form (element-by-element
		/// product). Now * operator will be used for classical quaternion product,
		/// as the old % operator. This is be more congruent with the * operator for ChFrames etc.

		/// Operator for quaternion product: A*B means the typical quaternion product.
		/// Note: since unit quaternions can represent rotations, the product can represent a
		/// concatenation of rotations as:
		///    frame_rotation_2to0 = frame_rotation_1to0 * frame_rotation_2to1
		/// Note: pay attention to operator low precedence (see C++ precedence rules!)
		/// Note: quaternion product is not commutative.
	ChQuaternion<Real>  operator*(const ChQuaternion<Real>& other) const
		{
			ChQuaternion<Real> mr;
			mr.Cross(*this, other);
			return mr;
		}

		/// Operator for quaternion product and assignment: 
		/// A*=B means A'=A*B, with typical quaternion product.
		/// Note: since unit quaternions can represent rotations, the product can represent a
		/// post-concatenation of a rotation in a kinematic chain.
		/// Note: quaternion product is not commutative.
	ChQuaternion<Real>& operator*=(const ChQuaternion<Real>& other)
		{
			this->Cross(*this, other);
			return *this;
		}

		/// Operator for 'specular' quaternion product: A>>B = B*A . 
		/// Note: since unit quaternions can represent rotations, the product can represent a
		/// concatenation of rotations as:
		///    frame_rotation_2to0 = frame_rotation_2to1 >> frame_rotation_1to0 
		/// Note: pay attention to operator low precedence (see C++ precedence rules!)
		/// Note: quaternion product is not commutative.
	ChQuaternion<Real>  operator>>(const ChQuaternion<Real>& other) const
		{
			ChQuaternion<Real> mr;
			mr.Cross(other,*this);
			return mr;
		}

		/// Operator for quaternion 'specular' product and assignment: 
		/// A>>=B means A'=A>>B, or A'=B*A with typical quaternion product.
		/// Note: since unit quaternions can represent rotations, the product can represent a
		/// pre-concatenation of a rotation in a kinematic chain.
		/// Note: quaternion product is not commutative.
	ChQuaternion<Real>& operator>>=(const ChQuaternion<Real>& other)
		{
			this->Cross(other, *this);
			return *this;
{'fraction_non_alphanumeric': 0.16057595149882115, 'fraction_numerical': 0.021977096665543955, 'mean_word_length': 2.9962987886944816, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6055348', 'n_tokens_mistral': 4200, 'n_tokens_neox': 3851, 'n_words': 1244}