Version: SMASH-3.1
smash::Angles Class Reference

#include <angles.h>

Angles provides a common interface for generating directions: i.e., two angles that should be interpreted as azimuthal and polar angles.

Usage:

#include "Angles.h"
Angles direction;
direction.distribute_isotropically();
double azimuthal_angle = direction.phi();
double cosine_of_polar_angle = direction.costheta();
double x_projection_of_vector = direction.x();
direction.set_phi(0);
double new_azimuthal_angle = direction.phi();
// new_azimuthal_angle == 0.
Angles()
Default constructor.
Definition: angles.h:65

Definition at line 59 of file angles.h.

Classes

struct  InvalidTheta
 Thrown for invalid values for theta. More...
 

Public Member Functions

 Angles ()
 Default constructor. More...
 
 Angles (double ph, double cost)
 
void distribute_isotropically ()
 Populate the object with a new direction. More...
 
void set_phi (const double phi)
 Sets the azimuthal angle. More...
 
void set_psi (const double psi)
 Sets the euler angle psi. More...
 
void set_costheta (const double cos)
 Set the polar angle from its cosine. More...
 
void set_theta (const double theta)
 Set the polar angle. More...
 
bool add_to_theta (const double delta)
 Advance polar angle. More...
 
bool add_to_theta (const double delta, const bool reverse)
 Advance polar angle. More...
 
double phi () const
 
double psi () const
 
double costheta () const
 
double sintheta () const
 
double x () const
 
double y () const
 
double z () const
 
ThreeVector threevec () const
 
double theta () const
 

Private Attributes

double phi_
 Azimuthal angle \(\varphi\). More...
 
double psi_
 Euler angle \(\psi\). More...
 
double costheta_
 Cosine of polar angle \(\cos\vartheta\). More...
 

Constructor & Destructor Documentation

◆ Angles() [1/2]

smash::Angles::Angles ( )
inline

Default constructor.

Returns
Angles pointing in x-direction.

Definition at line 65 of file angles.h.

65 : phi_(0), costheta_(0) {}
double phi_
Azimuthal angle .
Definition: angles.h:184
double costheta_
Cosine of polar angle .
Definition: angles.h:188

◆ Angles() [2/2]

smash::Angles::Angles ( double  ph,
double  cost 
)
inline
Returns
Angles with given phi and cos(theta).
Parameters
[in]phthe azimuthal angle
[in]costcosine of the polar angle

Definition at line 71 of file angles.h.

71  {
72  set_phi(ph);
73  set_costheta(cost);
74  }
void set_costheta(const double cos)
Set the polar angle from its cosine.
Definition: angles.h:221
void set_phi(const double phi)
Sets the azimuthal angle.
Definition: angles.h:205

Member Function Documentation

◆ distribute_isotropically()

void smash::Angles::distribute_isotropically ( )
inline

Populate the object with a new direction.

the direction is taken randomly from a homogeneous distribution, i.e., each point on a unit sphere is equally likely.

Definition at line 199 of file angles.h.

199  {
200  /* Isotropic distribution: phi in [0, 2pi) and cos(theta) in [-1,1]. */
201  phi_ = random::uniform(0.0, twopi);
202  costheta_ = random::uniform(-1.0, 1.0);
203 }
T uniform(T min, T max)
Definition: random.h:88
constexpr double twopi
.
Definition: constants.h:45

◆ set_phi()

void smash::Angles::set_phi ( const double  phi)
inline

Sets the azimuthal angle.

Parameters
[in]phiAny real number to set the azimuthal angle \(\varphi\) to.

Definition at line 205 of file angles.h.

205  {
206  /* Make sure that phi is in the range [0,2pi). */
207  phi_ = newphi;
208  if (newphi < 0 || newphi >= twopi) {
209  phi_ -= twopi * std::floor(newphi / twopi);
210  }
211 }

◆ set_psi()

void smash::Angles::set_psi ( const double  psi)
inline

Sets the euler angle psi.

Parameters
[in]psiAny real number to set the azimuthal angle \(\varphi\) to.

Definition at line 213 of file angles.h.

213  {
214  /* Make sure that phi is in the range [0,2pi). */
215  psi_ = newpsi;
216  if (newpsi < 0 || newpsi >= twopi) {
217  psi_ -= twopi * std::floor(newpsi / twopi);
218  }
219 }
double psi_
Euler angle .
Definition: angles.h:186

◆ set_costheta()

void smash::Angles::set_costheta ( const double  cos)
inline

Set the polar angle from its cosine.

This is the preferred way of setting the polar information.

Parameters
[in]cosCosine of the polar angle \(\cos\vartheta\).
Exceptions
InvalidThetaIf cos is not in range [-1 .. 1].

Definition at line 221 of file angles.h.

221  {
222  costheta_ = newcos;
223  /* check if costheta_ is in -1..1. If not, well. Error handling here
224  * is a lot harder than in the above. Still, I will silently do the
225  * same as above. Note, though, that costheta = 1 is allowed, even if
226  * it cannot be generated by distribute_isotropically(). */
227  if ((costheta_ < -1. - really_small) || (costheta_ > 1. + really_small)) {
228  throw InvalidTheta("Wrong value for costheta (must be in [-1,1]): " +
229  std::to_string(costheta_));
230  }
231  if (costheta_ < -1.) {
232  costheta_ = -1.;
233  } else if (costheta_ > 1.) {
234  costheta_ = 1.;
235  }
236 }
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37

◆ set_theta()

void smash::Angles::set_theta ( const double  theta)
inline

Set the polar angle.

In the current implementation, costheta is stored inside the object. Don't convert the angle to and from cosine, use the set-function for the thing you have at hand.

Parameters
[in]thetaAny real number to set the polar angle \(\vartheta\) to.

Definition at line 237 of file angles.h.

237  {
238  /* no error handling necessary, because this gives a sensible answer
239  * for every real number. */
240  set_costheta(std::cos(newtheta));
241 }

◆ add_to_theta() [1/2]

bool smash::Angles::add_to_theta ( const double  delta)
inline

Advance polar angle.

A positive addition means that we go towards the southpole.

See also
add_to_theta(const double& delta, const bool& reverse)
Parameters
[in]deltaAngle increment.
Returns
true if pole has been crossed.
Exceptions
InvalidThetaIf delta is not in [ \(-\pi\) .. \(\pi\)].

Definition at line 243 of file angles.h.

243  {
244  if (delta < -M_PI || delta > M_PI) {
245  throw InvalidTheta("Cannot advance polar angle by " +
246  std::to_string(delta));
247  }
248  double theta_plus_delta = delta + theta();
249  /* if sum is not in [0, PI], force it to be there:
250  * "upper" overflow:
251  * theta + delta + the_new_angle = 2*M_PI */
252  if (theta_plus_delta > M_PI) {
253  set_theta(twopi - theta_plus_delta);
254  // set_phi takes care that phi_ is in [0 .. 2*M_PI]
255  set_phi(phi() + M_PI);
256  return true; // meaning "we did change phi"
257  // "lower" overflow: theta + delta switches sign
258  } else if (theta_plus_delta < 0) {
259  set_theta(-theta_plus_delta);
260  set_phi(phi() + M_PI);
261  return true; // meaning "we did change phi"
262  // no overflow: set theta, do not touch phi:
263  } else {
264  set_theta(theta_plus_delta);
265  }
266  return false; // meaning "we did NOT change phi"
267 }
void set_theta(const double theta)
Set the polar angle.
Definition: angles.h:237
double theta() const
Definition: angles.h:292
double phi() const
Definition: angles.h:279

◆ add_to_theta() [2/2]

bool smash::Angles::add_to_theta ( const double  delta,
const bool  reverse 
)
inline

Advance polar angle.

When crossing a pole, azimuthal angle is changed by 180 degrees.

Parameters
[in]deltaAngle increment.
[in]reverseIf true, we start in the "far" hemisphere, meaning a positive delta will shift the object towards the north pole.
Returns
true if we end up in the far hemisphere, false if we end up in the original hemisphere.
Exceptions
InvalidThetaIf delta is not in [ \(-\pi\) .. \(\pi\)].

Definition at line 268 of file angles.h.

268  {
269  double plusminus_one = reverse ? -1.0 : +1.0;
270  bool this_reverse = add_to_theta(plusminus_one * delta);
271  /* if we had to reverse first time and now reverse again OR if we
272  * didn't reverse in either part, we do not reverse in total.
273  * else: if we reverse in one, but not the other part, we reverse in
274  * total. */
275  return this_reverse ^ reverse;
276 }
bool add_to_theta(const double delta)
Advance polar angle.
Definition: angles.h:243

◆ phi()

double smash::Angles::phi ( ) const
inline
Returns
Azimuthal angle.

Definition at line 279 of file angles.h.

279 { return phi_; }

◆ psi()

double smash::Angles::psi ( ) const
inline
Returns
Euler angle.

Definition at line 280 of file angles.h.

280 { return psi_; }

◆ costheta()

double smash::Angles::costheta ( ) const
inline
Returns
Cosine of polar angle.

Definition at line 278 of file angles.h.

278 { return costheta_; }

◆ sintheta()

double smash::Angles::sintheta ( ) const
inline
Returns
Sine of polar angle.

Definition at line 281 of file angles.h.

281  {
282  return std::sqrt(1.0 - costheta_ * costheta_);
283 }

◆ x()

double smash::Angles::x ( ) const
inline
Returns
\(x\) projection of the direction.

\(x = \sin\vartheta \cos\varphi\)

Definition at line 284 of file angles.h.

284 { return sintheta() * std::cos(phi_); }
double sintheta() const
Definition: angles.h:281

◆ y()

double smash::Angles::y ( ) const
inline
Returns
\(y\) projection of the direction.

\(y = \sin\vartheta \sin\varphi\)

Definition at line 285 of file angles.h.

285 { return sintheta() * std::sin(phi_); }

◆ z()

double smash::Angles::z ( ) const
inline
Returns
\(z\) projection of the direction.

\(z = \cos\vartheta\)

Definition at line 286 of file angles.h.

286 { return costheta_; }

◆ threevec()

ThreeVector smash::Angles::threevec ( ) const
inline
Returns
The unit three-vector corresponding to the angles.

Definition at line 288 of file angles.h.

288  {
289  return ThreeVector(x(), y(), z());
290 }
double z() const
Definition: angles.h:286
double x() const
Definition: angles.h:284
double y() const
Definition: angles.h:285

◆ theta()

double smash::Angles::theta ( ) const
inline
Returns
The polar angle.

Definition at line 292 of file angles.h.

292 { return std::acos(costheta_); }

Member Data Documentation

◆ phi_

double smash::Angles::phi_
private

Azimuthal angle \(\varphi\).

Definition at line 184 of file angles.h.

◆ psi_

double smash::Angles::psi_
private

Euler angle \(\psi\).

Definition at line 186 of file angles.h.

◆ costheta_

double smash::Angles::costheta_
private

Cosine of polar angle \(\cos\vartheta\).

Definition at line 188 of file angles.h.


The documentation for this class was generated from the following file: