OpenMEEG
triangle.h
Go to the documentation of this file.
1 /*
2 Project Name : OpenMEEG
3 
4 © INRIA and ENPC (contributors: Geoffray ADDE, Maureen CLERC, Alexandre
5 GRAMFORT, Renaud KERIVEN, Jan KYBIC, Perrine LANDREAU, Théodore PAPADOPOULO,
6 Emmanuel OLIVI
7 Maureen.Clerc.AT.inria.fr, keriven.AT.certis.enpc.fr,
8 kybic.AT.fel.cvut.cz, papadop.AT.inria.fr)
9 
10 The OpenMEEG software is a C++ package for solving the forward/inverse
11 problems of electroencephalography and magnetoencephalography.
12 
13 This software is governed by the CeCILL-B license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL-B
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's authors, the holders of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL-B license and that you accept its terms.
38 */
39 
40 #pragma once
41 
42 #include <cstdlib>
43 #include <vector>
44 #include <set>
45 #include <vect3.h>
46 #include <vertex.h>
47 
48 namespace OpenMEEG {
49 
53 
54  class OPENMEEG_EXPORT Triangle {
55  public:
56 
57  typedef Vertex** iterator;
58  typedef const Vertex** const_iterator;
59 
61 
62  Triangle(): ind(-1) { }
63 
65 
66  Triangle(Vertex* pts[3],const unsigned index=-1): ind(index) {
67  for (unsigned i=0;i<3;++i)
68  vertices_[i] = pts[i];
69  }
70 
72 
73  Triangle(Vertex& p1,Vertex& p2,Vertex& p3,const unsigned index=-1): ind(index) {
74  vertices_[0] = &p1;
75  vertices_[1] = &p2;
76  vertices_[2] = &p3;
77  }
78 
80 
81  Triangle(Vertex* p1,Vertex* p2,Vertex* p3,const unsigned index=-1): ind(index) {
82  vertices_[0] = p1;
83  vertices_[1] = p2;
84  vertices_[2] = p3;
85  }
86 
88  // Having both [] and () doing different things is prone to errors. Also the %3 in indexing seems vety costly.
89 
90  Vertex* operator[](const unsigned& vindex) { return vertices_[vindex%3]; } // 0 <= 'index' <= '2'
91  const Vertex* operator[](const unsigned& vindex) const { return vertices_[vindex%3]; }
92  Vertex& operator()(const unsigned& vindex) { return *vertices_[vindex%3]; } // 0 <= 'index' <= '2'
93  const Vertex& operator()(const unsigned& vindex) const { return *vertices_[vindex%3]; }
94 
95  bool operator==(const Triangle& T) const { return (T[0]==vertices_[0])&(T[1]==vertices_[1])&(T[2]==vertices_[2]); }
96 
97  Vertex& vertex(const unsigned& vindex) { return operator()(vindex); }
98  const Vertex& vertex(const unsigned& vindex) const { return operator()(vindex); }
99 
101 
102  const_iterator begin() const { return const_iterator(vertices_); }
103  const_iterator end() const { return const_iterator(vertices_+3); }
104  iterator begin() { return iterator(vertices_); }
105  iterator end() { return iterator(vertices_+3); }
106 
107  // These (s[1-3]) are ugly.. Remove ?
108 
109  const Vertex& s1() const { return *vertices_[0]; }
110  const Vertex& s2() const { return *vertices_[1]; }
111  const Vertex& s3() const { return *vertices_[2]; }
112 
113  Vertex& s1() { return *vertices_[0]; }
114  Vertex& s2() { return *vertices_[1]; }
115  Vertex& s3() { return *vertices_[2]; }
116 
117  Normal& normal() { return normal_; }
118  const Normal& normal() const { return normal_; }
119 
120  double& area() { return area_; }
121  const double& area() const { return area_; }
122 
123  unsigned& index() { return ind; }
124  const unsigned& index() const { return ind; }
125 
126  // These two methods are ugly and used exactly once. There is probably a better way.
127 
128  const Vertex& prev(const Vertex& V) const {
129  if ( &V == vertices_[0]) {
130  return *vertices_[2];
131  } else if ( &V == vertices_[1] ) {
132  return *vertices_[0];
133  } else if ( &V == vertices_[2] ) {
134  return *vertices_[1];
135  } else {
136  static Vertex v;
137  return v;
138  }
139  }
140  const Vertex& next(const Vertex& V) const {
141  if ( &V == vertices_[0]) {
142  return *vertices_[1];
143  } else if ( &V == vertices_[1] ) {
144  return *vertices_[2];
145  } else if ( &V == vertices_[2] ) {
146  return *vertices_[0];
147  } else {
148  static Vertex v;
149  return v;
150  }
151  }
152 
153  Vect3 center() const {
154  return (*vertices_[0]+*vertices_[1]+*vertices_[2])/3;
155  }
156 
157  bool contains(const Vertex& p) const {
158  for (unsigned i=0;i<3;++i)
159  if (&vertex(i)==&p )
160  return true;
161  return false;
162  }
163 
165 
166  void flip() { std::swap(vertices_[0],vertices_[1]); }
167 
168  private:
169 
170  Vertex* vertices_[3];
171  double area_;
173  unsigned ind;
174  };
175 
176  typedef std::vector<Triangle> Triangles;
177 }
Triangle.
Definition: triangle.h:54
Vertex * operator[](const unsigned &vindex)
Operators.
Definition: triangle.h:90
Vertex & vertex(const unsigned &vindex)
Definition: triangle.h:97
const Vertex * operator[](const unsigned &vindex) const
Definition: triangle.h:91
const Vertex & s1() const
Definition: triangle.h:109
const Vertex & next(const Vertex &V) const
Definition: triangle.h:140
const_iterator end() const
Definition: triangle.h:103
const Vertex & vertex(const unsigned &vindex) const
Definition: triangle.h:98
bool contains(const Vertex &p) const
Definition: triangle.h:157
Vertex & s3()
Definition: triangle.h:115
const Vertex & s3() const
Definition: triangle.h:111
Vect3 center() const
Definition: triangle.h:153
Triangle(Vertex *pts[3], const unsigned index=-1)
Create a new triangle from a set of vertices.
Definition: triangle.h:66
unsigned & index()
Definition: triangle.h:123
const Normal & normal() const
Definition: triangle.h:118
const Vertex & operator()(const unsigned &vindex) const
Definition: triangle.h:93
void flip()
flip two of the three vertex address
Definition: triangle.h:166
const Vertex & prev(const Vertex &V) const
Definition: triangle.h:128
Normal & normal()
Definition: triangle.h:117
const Vertex ** const_iterator
Definition: triangle.h:58
Vertex & s2()
Definition: triangle.h:114
Vertex & s1()
Definition: triangle.h:113
Vertex & operator()(const unsigned &vindex)
Definition: triangle.h:92
bool operator==(const Triangle &T) const
Definition: triangle.h:95
unsigned ind
Index of the triangle.
Definition: triangle.h:173
Triangle(Vertex *p1, Vertex *p2, Vertex *p3, const unsigned index=-1)
Create a new triangle from a 3 vertex adresses.
Definition: triangle.h:81
Normal normal_
Normal.
Definition: triangle.h:172
const Vertex & s2() const
Definition: triangle.h:110
Triangle(Vertex &p1, Vertex &p2, Vertex &p3, const unsigned index=-1)
Create a new triangle from a 3 vertices.
Definition: triangle.h:73
const unsigned & index() const
Definition: triangle.h:124
iterator begin()
Definition: triangle.h:104
iterator end()
Definition: triangle.h:105
double area_
Area.
Definition: triangle.h:171
const double & area() const
Definition: triangle.h:121
Vertex ** iterator
Definition: triangle.h:57
double & area()
Definition: triangle.h:120
Triangle()
Constructors.
Definition: triangle.h:62
const_iterator begin() const
Iterators.
Definition: triangle.h:102
Vect3.
Definition: vect3.h:62
Vertex.
Definition: vertex.h:52
std::vector< Triangle > Triangles
Definition: triangle.h:176