OpenMEEG
Loading...
Searching...
No Matches
triangle.h
Go to the documentation of this file.
1// Project Name: OpenMEEG (http://openmeeg.github.io)
2// © INRIA and ENPC under the French open source license CeCILL-B.
3// See full copyright notice in the file LICENSE.txt
4// If you make a copy of this file, you must either:
5// - provide also LICENSE.txt and modify this header to refer to it.
6// - replace this header by the LICENSE.txt content.
7
8#pragma once
9
10#include <vector>
11#include <map>
12
13#include <vect3.h>
14#include <vertex.h>
15#include <edge.h>
16#include <OMExceptions.H>
17
18namespace OpenMEEG {
19
21
23
24 TriangleIndices(const unsigned i,const unsigned j,const unsigned k) {
25 indices[0] = i;
26 indices[1] = j;
27 indices[2] = k;
28 }
29
30 TriangleIndices(const unsigned ind[3]): TriangleIndices(ind[0],ind[1],ind[2]) { }
31
32 TriangleIndices(const TriangleIndices& ind) { std::copy(&ind[0],&ind[3],&indices[0]); }
33
35
36 unsigned& operator[](const unsigned i) { return indices[i]; }
37 const unsigned& operator[](const unsigned i) const { return indices[i]; }
38
39 unsigned indices[3];
40 };
41
44
45 class OPENMEEG_EXPORT Triangle {
46 public:
47
48 typedef Vertex** iterator;
49 typedef const Vertex** const_iterator;
50
52
53 Triangle() { } // Is this needed besides for SWIG ?
54
56
57 Triangle(Vertex* pts[3],const unsigned index=-1): ind(index) {
58 for (unsigned i=0;i<3;++i)
59 vertices_[i] = pts[i];
60 }
61
63
64 Triangle(Vertex* p1,Vertex* p2,Vertex* p3,const unsigned index=-1): vertices_{p1,p2,p3},ind(index) { }
65
67
68 Triangle(Vertex& p1,Vertex& p2,Vertex& p3,const unsigned index=-1): Triangle(&p1,&p2,&p3,index) { }
69
71
72 const_iterator begin() const { return const_iterator(vertices_); }
73 const_iterator end() const { return const_iterator(vertices_+3); }
74 iterator begin() { return iterator(vertices_); }
75 iterator end() { return iterator(vertices_+3); }
76
78
79 bool operator==(const Triangle& T) const {
80 return (&T.vertex(0)==&vertex(0)) && (&T.vertex(1)==&vertex(1)) && (&T.vertex(2)==&vertex(2));
81 }
82
83 Vertex& vertex(const unsigned& vindex) { return *vertices_[vindex]; }
84 const Vertex& vertex(const unsigned& vindex) const { return *vertices_[vindex]; }
85
86 Edge edge(const Vertex& V) const {
87 const unsigned indx = vertex_index(V);
88 return Edge(vertex(indices[indx][0]),vertex(indices[indx][1]));
89 }
90
91 Edges edges() const {
92 return { Edge(vertex(1),vertex(2)), Edge(vertex(2),vertex(0)), Edge(vertex(0),vertex(1)) };
93 }
94
95 Normal& normal() { return normal_; }
96 const Normal& normal() const { return normal_; }
97
98 double area() const { return area_; }
99 double& area() { return area_; }
100
101 unsigned& index() { return ind; }
102 unsigned index() const { return ind; }
103
104 Vect3 center() const { return (vertex(0)+vertex(1)+vertex(2))/3; }
105
106 bool contains(const Vertex& p) const {
107 for (unsigned i=0;i<3;++i)
108 if (&vertex(i)==&p )
109 return true;
110 return false;
111 }
112
114
115 void change_orientation() { std::swap(vertices_[0],vertices_[1]); }
116
118
119 bool intersects(const Triangle& triangle) const;
120
121 private:
122
123 unsigned vertex_index(const Vertex& V) const {
124 for (unsigned i=0;i<3;++i)
125 if (&vertex(i)==&V)
126 return i;
127 throw UnknownVertex();
128 }
129
130 static constexpr unsigned indices[3][2] = {{1,2},{2,0},{0,1}};
131
132 Vertex* vertices_[3];
133 double area_ = 0.0;
134 Normal normal_;
135 unsigned ind;
136 };
137
138 typedef std::vector<Triangle> Triangles;
139 typedef std::vector<Triangle*> TrianglesRefs;
140
141 typedef std::map<unsigned,unsigned> IndexMap;
142}
Edge Edge class.
Definition: edge.h:18
Triangle Triangle class.
Definition: triangle.h:45
double & area()
Definition: triangle.h:99
Edges edges() const
Definition: triangle.h:91
void change_orientation()
Change triangle orientation by flipping two of the vertices.
Definition: triangle.h:115
Edge edge(const Vertex &V) const
Definition: triangle.h:86
bool intersects(const Triangle &triangle) const
Check for intersection with another triangle.
const_iterator end() const
Definition: triangle.h:73
bool contains(const Vertex &p) const
Definition: triangle.h:106
Vect3 center() const
Definition: triangle.h:104
unsigned index() const
Definition: triangle.h:102
Triangle(Vertex *pts[3], const unsigned index=-1)
Create a new triangle from a set of vertices.
Definition: triangle.h:57
unsigned & index()
Definition: triangle.h:101
double area() const
Definition: triangle.h:98
const Vertex ** const_iterator
Definition: triangle.h:49
bool operator==(const Triangle &T) const
Operators.
Definition: triangle.h:79
Triangle(Vertex *p1, Vertex *p2, Vertex *p3, const unsigned index=-1)
Create a new triangle from a 3 vertex adresses.
Definition: triangle.h:64
Triangle(Vertex &p1, Vertex &p2, Vertex &p3, const unsigned index=-1)
Create a new triangle from a 3 vertices.
Definition: triangle.h:68
const Normal & normal() const
Definition: triangle.h:96
iterator begin()
Definition: triangle.h:74
iterator end()
Definition: triangle.h:75
const Vertex & vertex(const unsigned &vindex) const
Definition: triangle.h:84
Vertex ** iterator
Definition: triangle.h:48
Normal & normal()
Definition: triangle.h:95
Triangle()
Constructors.
Definition: triangle.h:53
const_iterator begin() const
Iterators.
Definition: triangle.h:72
Vertex & vertex(const unsigned &vindex)
Definition: triangle.h:83
Vect3.
Definition: vect3.h:28
Vertex.
Definition: vertex.h:20
std::vector< Triangle > Triangles
Definition: triangle.h:138
std::vector< Triangle * > TrianglesRefs
Definition: triangle.h:139
Vect3 Normal
Definition: vect3.h:131
std::vector< Edge > Edges
Definition: edge.h:41
std::map< unsigned, unsigned > IndexMap
Definition: triangle.h:141
TriangleIndices(const unsigned ind[3])
Definition: triangle.h:30
TriangleIndices & operator=(const TriangleIndices &)=default
const unsigned & operator[](const unsigned i) const
Definition: triangle.h:37
TriangleIndices(const unsigned i, const unsigned j, const unsigned k)
Definition: triangle.h:24
unsigned & operator[](const unsigned i)
Definition: triangle.h:36
TriangleIndices(const TriangleIndices &ind)
Definition: triangle.h:32