Version: SMASH-1.5
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.

Definition at line 59 of file angles.h.

Collaboration diagram for smash::Angles:
[legend]

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_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 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 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 costheta_
Cosine of polar angle .
Definition: angles.h:177
double phi_
Azimuthal angle .
Definition: angles.h:175

◆ 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_phi(const double phi)
Sets the azimuthal angle.
Definition: angles.h:194
void set_costheta(const double cos)
Set the polar angle from its cosine.
Definition: angles.h:202
Here is the call graph for this function:

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 188 of file angles.h.

188  {
189  /* Isotropic distribution: phi in [0, 2pi) and cos(theta) in [-1,1]. */
190  phi_ = random::uniform(0.0, twopi);
191  costheta_ = random::uniform(-1.0, 1.0);
192 }
double costheta_
Cosine of polar angle .
Definition: angles.h:177
constexpr double twopi
.
Definition: constants.h:39
T uniform(T min, T max)
Definition: random.h:85
double phi_
Azimuthal angle .
Definition: angles.h:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 194 of file angles.h.

194  {
195  /* Make sure that phi is in the range [0,2pi). */
196  phi_ = newphi;
197  if (newphi < 0 || newphi >= twopi) {
198  phi_ -= twopi * std::floor(newphi / twopi);
199  }
200 }
constexpr double twopi
.
Definition: constants.h:39
double phi_
Azimuthal angle .
Definition: angles.h:175
Here is the caller graph for this function:

◆ 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 202 of file angles.h.

202  {
203  costheta_ = newcos;
204  /* check if costheta_ is in -1..1. If not, well. Error handling here
205  * is a lot harder than in the above. Still, I will silently do the
206  * same as above. Note, though, that costheta = 1 is allowed, even if
207  * it cannot be generated by distribute_isotropically(). */
208  if ((costheta_ < -1. - really_small) || (costheta_ > 1. + really_small)) {
209  throw InvalidTheta("Wrong value for costheta (must be in [-1,1]): " +
210  std::to_string(costheta_));
211  }
212  if (costheta_ < -1.) {
213  costheta_ = -1.;
214  } else if (costheta_ > 1.) {
215  costheta_ = 1.;
216  }
217 }
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:34
double costheta_
Cosine of polar angle .
Definition: angles.h:177
Here is the caller graph for this function:

◆ 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 218 of file angles.h.

218  {
219  /* no error handling necessary, because this gives a sensible answer
220  * for every real number. */
221  set_costheta(std::cos(newtheta));
222 }
void set_costheta(const double cos)
Set the polar angle from its cosine.
Definition: angles.h:202
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 224 of file angles.h.

224  {
225  if (delta < -M_PI || delta > M_PI) {
226  throw InvalidTheta("Cannot advance polar angle by " +
227  std::to_string(delta));
228  }
229  double theta_plus_delta = delta + theta();
230  /* if sum is not in [0, PI], force it to be there:
231  * "upper" overflow:
232  * theta + delta + the_new_angle = 2*M_PI */
233  if (theta_plus_delta > M_PI) {
234  set_theta(twopi - theta_plus_delta);
235  // set_phi takes care that phi_ is in [0 .. 2*M_PI]
236  set_phi(phi() + M_PI);
237  return true; // meaning "we did change phi"
238  // "lower" overflow: theta + delta switches sign
239  } else if (theta_plus_delta < 0) {
240  set_theta(-theta_plus_delta);
241  set_phi(phi() + M_PI);
242  return true; // meaning "we did change phi"
243  // no overflow: set theta, do not touch phi:
244  } else {
245  set_theta(theta_plus_delta);
246  }
247  return false; // meaning "we did NOT change phi"
248 }
double theta() const
Definition: angles.h:272
void set_theta(const double theta)
Set the polar angle.
Definition: angles.h:218
double phi() const
Definition: angles.h:260
constexpr double twopi
.
Definition: constants.h:39
void set_phi(const double phi)
Sets the azimuthal angle.
Definition: angles.h:194
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 249 of file angles.h.

249  {
250  double plusminus_one = reverse ? -1.0 : +1.0;
251  bool this_reverse = add_to_theta(plusminus_one * delta);
252  /* if we had to reverse first time and now reverse again OR if we
253  * didn't reverse in either part, we do not reverse in total.
254  * else: if we reverse in one, but not the other part, we reverse in
255  * total. */
256  return this_reverse ^ reverse;
257 }
bool add_to_theta(const double delta)
Advance polar angle.
Definition: angles.h:224
Here is the call graph for this function:

◆ phi()

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

Definition at line 260 of file angles.h.

260 { return phi_; }
double phi_
Azimuthal angle .
Definition: angles.h:175
Here is the caller graph for this function:

◆ costheta()

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

Definition at line 259 of file angles.h.

259 { return costheta_; }
double costheta_
Cosine of polar angle .
Definition: angles.h:177
Here is the caller graph for this function:

◆ sintheta()

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

Definition at line 261 of file angles.h.

261  {
262  return std::sqrt(1.0 - costheta_ * costheta_);
263 }
double costheta_
Cosine of polar angle .
Definition: angles.h:177
Here is the caller graph for this function:

◆ x()

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

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

Definition at line 264 of file angles.h.

264 { return sintheta() * std::cos(phi_); }
double sintheta() const
Definition: angles.h:261
double phi_
Azimuthal angle .
Definition: angles.h:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ y()

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

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

Definition at line 265 of file angles.h.

265 { return sintheta() * std::sin(phi_); }
double sintheta() const
Definition: angles.h:261
double phi_
Azimuthal angle .
Definition: angles.h:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ z()

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

\(z = \cos\vartheta\)

Definition at line 266 of file angles.h.

266 { return costheta_; }
double costheta_
Cosine of polar angle .
Definition: angles.h:177
Here is the caller graph for this function:

◆ threevec()

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

Definition at line 268 of file angles.h.

268  {
269  return ThreeVector(x(), y(), z());
270 }
double y() const
Definition: angles.h:265
double z() const
Definition: angles.h:266
double x() const
Definition: angles.h:264
Here is the call graph for this function:
Here is the caller graph for this function:

◆ theta()

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

Definition at line 272 of file angles.h.

272 { return std::acos(costheta_); }
double costheta_
Cosine of polar angle .
Definition: angles.h:177
Here is the caller graph for this function:

Member Data Documentation

◆ phi_

double smash::Angles::phi_
private

Azimuthal angle \(\varphi\).

Definition at line 175 of file angles.h.

◆ costheta_

double smash::Angles::costheta_
private

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

Definition at line 177 of file angles.h.


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