| 1 | /*
 | 
|---|
| 2 |  * Fragment.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Aug 8, 2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef FRAGMENT_HPP_
 | 
|---|
| 9 | #define FRAGMENT_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <iosfwd>
 | 
|---|
| 18 | #include <vector>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | class FragmentTest;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | class Fragment {
 | 
|---|
| 23 |   //!> grant ostream operator access
 | 
|---|
| 24 |   friend std::ostream & operator<<(std::ostream &ost, const Fragment &f);
 | 
|---|
| 25 |   //!> grant unit test access
 | 
|---|
| 26 |   friend class FragmentTest;
 | 
|---|
| 27 | public:
 | 
|---|
| 28 |   typedef std::vector<double> position_t;
 | 
|---|
| 29 |   typedef std::vector< position_t > positions_t;
 | 
|---|
| 30 |   typedef std::vector< double > charges_t;
 | 
|---|
| 31 |   typedef std::pair< position_t, double> nucleus_t;
 | 
|---|
| 32 |   typedef std::vector< nucleus_t > nuclei_t;
 | 
|---|
| 33 | 
 | 
|---|
| 34 |   /** Default constructor of class Fragment.
 | 
|---|
| 35 |    *
 | 
|---|
| 36 |    */
 | 
|---|
| 37 |   Fragment();
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   /** Default constructor of class Fragment.
 | 
|---|
| 40 |    *
 | 
|---|
| 41 |    */
 | 
|---|
| 42 |   Fragment(const nuclei_t &_nuclei) :
 | 
|---|
| 43 |     nuclei(_nuclei)
 | 
|---|
| 44 |   {}
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   /** Constructor of class Fragment.
 | 
|---|
| 47 |    *
 | 
|---|
| 48 |    * @param _positions given positions
 | 
|---|
| 49 |    * @param _charges given charges
 | 
|---|
| 50 |    */
 | 
|---|
| 51 |   Fragment(const positions_t &_positions, const charges_t &_charges);
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   /** Adding another fragment onto this one.
 | 
|---|
| 54 |    *
 | 
|---|
| 55 |    * \note The operation is area-conserving, i.e. the new area is the sum of
 | 
|---|
| 56 |    * both areas.
 | 
|---|
| 57 |    *
 | 
|---|
| 58 |    * @param other other fragment
 | 
|---|
| 59 |    * @return ref to this instance
 | 
|---|
| 60 |    */
 | 
|---|
| 61 |   Fragment& operator+=(const Fragment &other);
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   /** Assignment operator.
 | 
|---|
| 64 |    *
 | 
|---|
| 65 |    * @param other other fragment to make ourselves equal to
 | 
|---|
| 66 |    * @return ref to this instance
 | 
|---|
| 67 |    */
 | 
|---|
| 68 |   Fragment& operator=(const Fragment &other);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   /** Subtracting another fragment from this one.
 | 
|---|
| 71 |    *
 | 
|---|
| 72 |    * @param other other fragment
 | 
|---|
| 73 |    * @return ref to this instance
 | 
|---|
| 74 |    */
 | 
|---|
| 75 |   Fragment& operator-=(const Fragment &other);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   /** Getter for all stored positions.
 | 
|---|
| 78 |    *
 | 
|---|
| 79 |    * @return vector of positions
 | 
|---|
| 80 |    */
 | 
|---|
| 81 |   positions_t getPositions() const;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   /** Getter for all stored charges.
 | 
|---|
| 84 |    *
 | 
|---|
| 85 |    * @return vector of charges
 | 
|---|
| 86 |    */
 | 
|---|
| 87 |   charges_t getCharges() const;
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   /** Equality operator.
 | 
|---|
| 90 |    *
 | 
|---|
| 91 |    * @param other other instance to check against
 | 
|---|
| 92 |    * @return true - both are equal, false - some nucleus_t differ
 | 
|---|
| 93 |    */
 | 
|---|
| 94 |   bool operator==(const Fragment& other) const;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   bool operator!=(const Fragment& other) const
 | 
|---|
| 97 |   {
 | 
|---|
| 98 |     return (!(*this == other));
 | 
|---|
| 99 |   }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   /** Creates type nucleus_t from given \a position and \a charge.
 | 
|---|
| 102 |    *
 | 
|---|
| 103 |    * @param position position of nucleus to create
 | 
|---|
| 104 |    * @param charge charge of nucleus to create
 | 
|---|
| 105 |    * @return nucleus with given \a position and \a charge
 | 
|---|
| 106 |    */
 | 
|---|
| 107 |   static nucleus_t createNucleus(const position_t &position, const double charge);
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   /** Helper function to check whether two positions are equal.
 | 
|---|
| 110 |    *
 | 
|---|
| 111 |    * @param a first position
 | 
|---|
| 112 |    * @param b second position
 | 
|---|
| 113 |    * @return a equals b within numerical precision
 | 
|---|
| 114 |    */
 | 
|---|
| 115 |   static bool isPositionEqual(const position_t &a, const position_t &b);
 | 
|---|
| 116 | 
 | 
|---|
| 117 | private:
 | 
|---|
| 118 |   /** Helper function that checks whether this nuclei \b position is present.
 | 
|---|
| 119 |    *
 | 
|---|
| 120 |    * This operation is \f${\cal O}(n)\f$
 | 
|---|
| 121 |    *
 | 
|---|
| 122 |    * @param n nuclei to check
 | 
|---|
| 123 |    * @return true - is contained, false - is not contained
 | 
|---|
| 124 |    */
 | 
|---|
| 125 |   bool containsNuclei(const nucleus_t &n) const;
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   /** Seeks through all nuclei and removes one with matching \b position if found.
 | 
|---|
| 128 |    *
 | 
|---|
| 129 |    * @param n nuclei to remove
 | 
|---|
| 130 |    */
 | 
|---|
| 131 |   void removeNuclei(const nucleus_t &n);
 | 
|---|
| 132 | 
 | 
|---|
| 133 | private:
 | 
|---|
| 134 |   nuclei_t nuclei;
 | 
|---|
| 135 | };
 | 
|---|
| 136 | 
 | 
|---|
| 137 | /** Equality operator for two nuclei.
 | 
|---|
| 138 |  *
 | 
|---|
| 139 |  * @param a first nuclei
 | 
|---|
| 140 |  * @param b second nuclei
 | 
|---|
| 141 |  * @return true - both have same position and charge, false - either charge or position is different
 | 
|---|
| 142 |  */
 | 
|---|
| 143 | bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b);
 | 
|---|
| 144 | 
 | 
|---|
| 145 | std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n);
 | 
|---|
| 146 | 
 | 
|---|
| 147 | std::ostream & operator<<(std::ostream &ost, const Fragment &f);
 | 
|---|
| 148 | 
 | 
|---|
| 149 | template<typename T> T ZeroInstance();
 | 
|---|
| 150 | template<> Fragment ZeroInstance<Fragment>();
 | 
|---|
| 151 | 
 | 
|---|
| 152 | #endif /* FRAGMENT_HPP_ */
 | 
|---|