| 1 | /*
 | 
|---|
| 2 |  * Triangle.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jun 15, 2015
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef TRIANGLE_HPP_
 | 
|---|
| 9 | #define TRIANGLE_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <array>
 | 
|---|
| 17 | #include <cassert>
 | 
|---|
| 18 | #include <utility>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "types.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | namespace detail {
 | 
|---|
| 23 | 
 | 
|---|
| 24 | struct Triangle
 | 
|---|
| 25 | {
 | 
|---|
| 26 |         //!> typedef for the triangle index type
 | 
|---|
| 27 |         typedef size_t index_t;
 | 
|---|
| 28 | 
 | 
|---|
| 29 |         //!> typedef for the structure to store triangle indices
 | 
|---|
| 30 |         typedef std::array<index_t, 3> indices_t;
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         //!> constant for invalid index
 | 
|---|
| 33 |         enum { INVALIDINDEX=-1 };
 | 
|---|
| 34 | 
 | 
|---|
| 35 |         /** Constructor for struct triangle.
 | 
|---|
| 36 |          *
 | 
|---|
| 37 |          */
 | 
|---|
| 38 |         Triangle()
 | 
|---|
| 39 |         {
 | 
|---|
| 40 |                 indices = {{
 | 
|---|
| 41 |                                 (index_t)INVALIDINDEX,
 | 
|---|
| 42 |                                 (index_t)INVALIDINDEX,
 | 
|---|
| 43 |                                 (index_t)INVALIDINDEX
 | 
|---|
| 44 |                 }};
 | 
|---|
| 45 |         }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         /** Constructor for struct triangle.
 | 
|---|
| 48 |          *
 | 
|---|
| 49 |          * \param _indices initial indices to set
 | 
|---|
| 50 |          */
 | 
|---|
| 51 |         Triangle(const indices_t &_indices) :
 | 
|---|
| 52 |                 indices(_indices)
 | 
|---|
| 53 |         {
 | 
|---|
| 54 |                 if (!isSorted())
 | 
|---|
| 55 |                         sortIndices();
 | 
|---|
| 56 |         }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         /** Setter for the indices of the triangle.
 | 
|---|
| 59 |          *
 | 
|---|
| 60 |          * \param _elements indices to be set (need not be sorted)
 | 
|---|
| 61 |          */
 | 
|---|
| 62 |         void set(const indices_t &_elements) {
 | 
|---|
| 63 |                 indices = _elements;
 | 
|---|
| 64 |                 if (!isSorted())
 | 
|---|
| 65 |                         sortIndices();
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         /** Getter for the indices.
 | 
|---|
| 69 |          *
 | 
|---|
| 70 |          * \return set of sorted indices
 | 
|---|
| 71 |          */
 | 
|---|
| 72 |         const indices_t &get() const {
 | 
|---|
| 73 |                 return indices;
 | 
|---|
| 74 |         }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         /** Less comparator for triangle, comparing indices.
 | 
|---|
| 77 |          *
 | 
|---|
| 78 |          * \return true - at least one index in this triangle is smaller
 | 
|---|
| 79 |          */
 | 
|---|
| 80 |         bool operator<(const Triangle &_other) const {
 | 
|---|
| 81 |                 return indices < _other.indices;
 | 
|---|
| 82 |         }
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         /** Comparator for equivalence of triangle.
 | 
|---|
| 85 |          *
 | 
|---|
| 86 |          * \return true - both index triples are the same
 | 
|---|
| 87 |          */
 | 
|---|
| 88 |         bool operator==(const Triangle &_other) const {
 | 
|---|
| 89 |                 return indices == _other.indices;
 | 
|---|
| 90 |         }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         /** Comparator for non-equivalence of triangle.
 | 
|---|
| 93 |          *
 | 
|---|
| 94 |          * \return true - both index triples are not the same
 | 
|---|
| 95 |          */
 | 
|---|
| 96 |         bool operator!=(const Triangle &_other) const {
 | 
|---|
| 97 |                 return !(*this == _other);
 | 
|---|
| 98 |         }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | private:
 | 
|---|
| 101 |         /** Sort the indices.
 | 
|---|
| 102 |          *
 | 
|---|
| 103 |          */
 | 
|---|
| 104 |         void sortIndices() {
 | 
|---|
| 105 |                 std::sort(indices.begin(), indices.end());
 | 
|---|
| 106 | //              if (indices[0] > indices[1]) {
 | 
|---|
| 107 | //                      if (indices[0] > indices[2])
 | 
|---|
| 108 | //                              std::swap(indices[0], indices[2]);
 | 
|---|
| 109 | //                      if (indices[0] > indices[1])
 | 
|---|
| 110 | //                              std::swap(indices[0], indices[1]);
 | 
|---|
| 111 | //              } else {
 | 
|---|
| 112 | //                      // last element in between or up front?
 | 
|---|
| 113 | //                      if (indices[0] > indices[2]) {
 | 
|---|
| 114 | //                              std::swap(indices[1], indices[2]);
 | 
|---|
| 115 | //                              std::swap(indices[0], indices[1]);
 | 
|---|
| 116 | //                      } else
 | 
|---|
| 117 | //                              std::swap(indices[1], indices[2]);
 | 
|---|
| 118 | //              }
 | 
|---|
| 119 |                 assert( isSorted() );
 | 
|---|
| 120 |         }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | private:
 | 
|---|
| 123 |         /** Check whether indices are sorted.
 | 
|---|
| 124 |          *
 | 
|---|
| 125 |          * \return true - indices are sorted ascendingly
 | 
|---|
| 126 |          */
 | 
|---|
| 127 |         bool isSorted() const
 | 
|---|
| 128 |         {
 | 
|---|
| 129 |                 return (( indices[0] <= indices[1]) && (indices[1] <= indices[2]));
 | 
|---|
| 130 |         }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | private:
 | 
|---|
| 133 |         //!> fixed size array for containing indices
 | 
|---|
| 134 |         indices_t indices;
 | 
|---|
| 135 | };
 | 
|---|
| 136 | 
 | 
|---|
| 137 | }; /* namespace detail */
 | 
|---|
| 138 | 
 | 
|---|
| 139 | std::ostream& operator<<(std::ostream &_ost, const detail::Triangle &_t) {
 | 
|---|
| 140 |         const detail::Triangle::indices_t &indices = _t.get();
 | 
|---|
| 141 |         _ost << "[" << indices[0] << ","  << indices[1] << "," << indices[2] << "]";
 | 
|---|
| 142 |         return _ost;
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | #endif /* TRIANGLE_HPP_ */
 | 
|---|