MorphoGraphX  2.0-1-227
Triangle.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef TRIANGLE_HPP
12 #define TRIANGLE_HPP
13 
19 #include <Config.hpp>
20 #include <Types.hpp>
21 #include <Information.hpp>
22 
23 namespace mgx
24 {
31  struct Triangle
32  {
33  // Default constructor
34  Triangle() //: v{vertex(0),vertex(0),vertex(0)}
35  {
36  // Any way not to create them in the first place? need {0,0,0} initializer
37  for(int i = 0; i < 3; ++i)
38  v[i] = vertex(0);
39  }
40 
41  // Constructor
43  {
44  //if(v0.id() < v1.id() and v0.id() < v2.id()) {
45  if(v0 < v1 and v0 < v2) {
46  v[0] = v0; v[1] = v1; v[2] = v2;
47  } else if(v1 < v0 and v1 < v2) {
48  //} else if(v1.id() < v0.id() and v1.id() < v2.id()) {
49  v[0] = v1; v[1] = v2; v[2] = v0;
50  } else {
51  v[0] = v2; v[1] = v0; v[2] = v1;
52  }
53  }
54 
55  // Template to load to reorder data to match triangle
56  template<typename T>
57  static bool setTriangleData(vertex v1, vertex v2, vertex v3, Triangle &t, T &V1, T &V2, T &V3)
58  {
59  if(v1 == t.v[1]) {
60  std::swap(V2, V3);
61  std::swap(V1, V2);
62  } else if(v1 == t.v[2]) {
63  std::swap(V1, V2);
64  std::swap(V2, V3);
65  }
66  return true;
67  }
68 
69  // Template to load to reorder data to match triangle
70  template<typename T>
71  static bool setTriangleData(vertex v1, vertex v2, vertex v3, Triangle &t, T V[])
72  {
73  return setTriangleData(v1, v2, v3, t, V[0], V[1], V[2]);
74  }
75 
76  // == operator
77  bool operator==(const Triangle &other) const
78  {
79  return v[0] == other.v[0] and v[1] == other.v[1] and v[2] == other.v[2];
80  }
81 
82  // < operator
83  bool operator <(const Triangle &other) const
84  {
85  for(size_t i = 0; i < 3; ++i) {
86  if(v[i] < other.v[i])
87  return true;
88  if(v[i] > other.v[i])
89  return false;
90  }
91  return false;
92  }
93 
94  // <= operator
95  bool operator <=(const Triangle &other) const
96  {
97  for(size_t i = 0; i < 3; ++i) {
98  if(v[i] < other.v[i])
99  return true;
100  if(v[i] > other.v[i])
101  return false;
102  }
103  return true;
104  }
105 
106 
107  // Vertices of the triangle
108  vertex v[3];
109  };
110 
111  inline Triangle flipTri(const Triangle &t)
112  {
113  return Triangle(t.v[0], t.v[2], t.v[1]);
114  };
115 
116 
118  typedef std::unordered_map<Triangle, SymmetricTensor> TriSymTensorMap;
120  typedef std::pair<Triangle, SymmetricTensor> TriSymTensorPair;
121 }
122 
123 // Define hash for Triangle for use in unordered containers
124 namespace std
125 {
127  // Hash Triangle
128  template <> struct hash<mgx::Triangle>
129  {
130  size_t operator()(const mgx::Triangle &t) const
131  {
132  hash<mgx::vertex> h;
133  return h(t.v[0]) ^ h(t.v[1]) ^ h(t.v[2]);
134  }
135  };
137 }
138 #endif
mgx::Triangle::Triangle
Triangle()
Definition: Triangle.hpp:34
mgx::vertex
vvGraph::vertex_t vertex
Type of a vertex.
Definition: Misc.hpp:50
Information.hpp
mgx::Triangle::Triangle
Triangle(vertex v0, vertex v1, vertex v2)
Definition: Triangle.hpp:42
std::hash< mgx::Triangle >::operator()
size_t operator()(const mgx::Triangle &t) const
Definition: Triangle.hpp:130
mgx::Triangle::operator<=
bool operator<=(const Triangle &other) const
Definition: Triangle.hpp:95
mgx::Triangle::setTriangleData
static bool setTriangleData(vertex v1, vertex v2, vertex v3, Triangle &t, T &V1, T &V2, T &V3)
Definition: Triangle.hpp:57
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Triangle::setTriangleData
static bool setTriangleData(vertex v1, vertex v2, vertex v3, Triangle &t, T V[])
Definition: Triangle.hpp:71
mgx::swap
void swap(multiset_vector< Key, Compare, Allocator > &v1, multiset_vector< Key, Compare, Allocator > &v2)
Definition: SetVector.hpp:543
mgx::flipTri
Triangle flipTri(const Triangle &t)
Definition: Triangle.hpp:111
HASH_NS_EXIT
#define HASH_NS_EXIT
Definition: Common.hpp:14
mgx::TriSymTensorPair
std::pair< Triangle, SymmetricTensor > TriSymTensorPair
Element in TriSymTensorMap.
Definition: Triangle.hpp:120
mgx::Triangle::v
vertex v[3]
Definition: Triangle.hpp:108
mgx::Triangle
class Triangle Triangle.hpp <Triangle.hpp>
Definition: Triangle.hpp:31
mgx::TriSymTensorMap
std::unordered_map< Triangle, SymmetricTensor > TriSymTensorMap
Map a triangle to a symmetric tensor.
Definition: Triangle.hpp:114
HASH_NS_ENTER
#define HASH_NS_ENTER
Definition: Common.hpp:13
Types.hpp
mgx::Triangle::operator==
bool operator==(const Triangle &other) const
Definition: Triangle.hpp:77
mgx::Triangle::operator<
bool operator<(const Triangle &other) const
Definition: Triangle.hpp:83
mgx::Vertex
Definition: Vertex.hpp:58