Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleTriangleMesh2.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 // std
26 #include <iostream>
27 
28 // BIAS
29 #include <Utils/TriangleMesh.hh>
30 
31 using namespace std;
32 using namespace BIAS;
33 
34 /**
35  * @brief Creates a TriangleMesh programmatically.
36  *
37  * The TriangleMesh consists of two non-connected subsegments and will look
38  * like this:
39  * 5-----6-----7 8-----9 <- point indices
40  * | /| /| | /|
41  * | 0 / | 2 / | | 4 / | <- triangle indices
42  * | / | / | | / |
43  * | / 1 | / 3 | | / 5 | <- triangle indices
44  * |/ |/ | |/ |
45  * 0-----1-----2 3-----4 <- point indices
46  *
47  * The composition is relatively simple. When processed by the algorithm no
48  * subsegmentes need to be fused.
49  *
50  * @author rwulff
51  * @date 02/2011
52  */
53 void GenerateMesh1(TriangleMesh& mesh)
54 {
55  vector<Vector3<double> > vertices;
56  vertices.push_back(Vector3<double>(0, 0, 0)); // point 0
57  vertices.push_back(Vector3<double>(1, 0, 0)); // point 1
58  vertices.push_back(Vector3<double>(2, 0, 0)); // point 2
59  vertices.push_back(Vector3<double>(3, 0, 0)); // point 3
60  vertices.push_back(Vector3<double>(4, 0, 0)); // point 4
61  vertices.push_back(Vector3<double>(0, 1, 0)); // point 5
62  vertices.push_back(Vector3<double>(1, 1, 0)); // point 6
63  vertices.push_back(Vector3<double>(2, 1, 0)); // point 7
64  vertices.push_back(Vector3<double>(3, 1, 0)); // point 8
65  vertices.push_back(Vector3<double>(4, 1, 0)); // point 9
66 
67  vector<Vector3<double> > normals;
68  for (size_t vertexCnt = 0; vertexCnt < vertices.size(); ++vertexCnt) {
69  normals.push_back(Vector3<double>(0, 0, 1));
70  }
71 
72  vector<Vector4<unsigned char> > colors;
73  for (size_t vertexCnt = 0; vertexCnt < vertices.size(); ++vertexCnt) {
74  colors.push_back(Vector4<unsigned char>(0, 0, 255, 255));
75  }
76 
77  vector<Vector<int> > triangleIndices;
78  Vector<int> tmpTriangle(3);
79  tmpTriangle[0] = 0; // triangle 0
80  tmpTriangle[1] = 6;
81  tmpTriangle[2] = 5;
82  triangleIndices.push_back(tmpTriangle);
83  tmpTriangle[0] = 0; // triangle 1
84  tmpTriangle[1] = 1;
85  tmpTriangle[2] = 6;
86  triangleIndices.push_back(tmpTriangle);
87  tmpTriangle[0] = 1; // triangle 2
88  tmpTriangle[1] = 7;
89  tmpTriangle[2] = 6;
90  triangleIndices.push_back(tmpTriangle);
91  tmpTriangle[0] = 1; // triangle 3
92  tmpTriangle[1] = 2;
93  tmpTriangle[2] = 7;
94  triangleIndices.push_back(tmpTriangle);
95  tmpTriangle[0] = 3; // triangle 4
96  tmpTriangle[1] = 9;
97  tmpTriangle[2] = 8;
98  triangleIndices.push_back(tmpTriangle);
99  tmpTriangle[0] = 3; // triangle 5
100  tmpTriangle[1] = 4;
101  tmpTriangle[2] = 9;
102  triangleIndices.push_back(tmpTriangle);
103 
104  mesh.SetMesh(vertices, normals, colors, triangleIndices);
105 }
106 
107 /**
108  * @brief Creates a TriangleMesh programmatically.
109  *
110  * The TriangleMesh consists of two non-connected subsegments and will look
111  * like this:
112  * 0-----4-----7-----3 10-----11 <- point indices
113  * | /| /| /| | /|
114  * | 0 / | 2 / | 4 / | | 6 / | <- triangle indices
115  * | / | / | / | | / |
116  * | / 1 | / 3 | / 5 | | / 7 | <- triangle indices
117  * |/ |/ |/ | |/ |
118  * 2-----6-----5-----1 8-----9 <- point indices
119  *
120  * The composition is a little more complex and requires fusion of segments
121  * during processing.
122  *
123  * @author rwulff
124  * @date 02/2011
125  */
126 void GenerateMesh2(TriangleMesh& mesh)
127 {
128  vector<Vector3<double> > vertices;
129  vertices.push_back(Vector3<double>(0, 1, 0)); // point 0
130  vertices.push_back(Vector3<double>(3, 0, 0)); // point 1
131  vertices.push_back(Vector3<double>(0, 0, 0)); // point 2
132  vertices.push_back(Vector3<double>(3, 1, 0)); // point 3
133  vertices.push_back(Vector3<double>(1, 1, 0)); // point 4
134  vertices.push_back(Vector3<double>(2, 0, 0)); // point 5
135  vertices.push_back(Vector3<double>(1, 0, 0)); // point 6
136  vertices.push_back(Vector3<double>(2, 1, 0)); // point 7
137  vertices.push_back(Vector3<double>(4, 0, 0)); // point 8
138  vertices.push_back(Vector3<double>(5, 0, 0)); // point 9
139  vertices.push_back(Vector3<double>(4, 1, 0)); // point 10
140  vertices.push_back(Vector3<double>(5, 1, 0)); // point 11
141 
142 
143  vector<Vector3<double> > normals;
144  for (size_t vertexCnt = 0; vertexCnt < vertices.size(); ++vertexCnt) {
145  normals.push_back(Vector3<double>(0, 0, 1));
146  }
147 
148  vector<Vector4<unsigned char> > colors;
149  for (size_t vertexCnt = 0; vertexCnt < vertices.size(); ++vertexCnt) {
150  colors.push_back(Vector4<unsigned char>(0, 0, 255, 255));
151  }
152 
153  vector<Vector<int> > triangleIndices;
154  Vector<int> tmpTriangle(3);
155  tmpTriangle[0] = 2; // triangle 0
156  tmpTriangle[1] = 4;
157  tmpTriangle[2] = 0;
158  triangleIndices.push_back(tmpTriangle);
159  tmpTriangle[0] = 2; // triangle 1
160  tmpTriangle[1] = 6;
161  tmpTriangle[2] = 4;
162  triangleIndices.push_back(tmpTriangle);
163  tmpTriangle[0] = 6; // triangle 2
164  tmpTriangle[1] = 7;
165  tmpTriangle[2] = 4;
166  triangleIndices.push_back(tmpTriangle);
167  tmpTriangle[0] = 6; // triangle 3
168  tmpTriangle[1] = 5;
169  tmpTriangle[2] = 7;
170  triangleIndices.push_back(tmpTriangle);
171  tmpTriangle[0] = 5; // triangle 4
172  tmpTriangle[1] = 3;
173  tmpTriangle[2] = 7;
174  triangleIndices.push_back(tmpTriangle);
175  tmpTriangle[0] = 5; // triangle 5
176  tmpTriangle[1] = 1;
177  tmpTriangle[2] = 3;
178  triangleIndices.push_back(tmpTriangle);
179  tmpTriangle[0] = 8; // triangle 6
180  tmpTriangle[1] = 11;
181  tmpTriangle[2] = 10;
182  triangleIndices.push_back(tmpTriangle);
183  tmpTriangle[0] = 8; // triangle 7
184  tmpTriangle[1] = 9;
185  tmpTriangle[2] = 11;
186  triangleIndices.push_back(tmpTriangle);
187 
188  mesh.SetMesh(vertices, normals, colors, triangleIndices);
189 }
190 
191 
192 /**
193  * @brief Creates a TriangleMesh and searches for the biggest connected segment.
194  *
195  * @author rwulff
196  * @date 02/2011
197  */
198 int main(int argc, char* argv[])
199 {
200  // create the mesh
201  cout << "creating triangle mesh..." << endl;
202 
203  TriangleMesh origMesh;
204 // GenerateMesh1(origMesh);
205  GenerateMesh2(origMesh);
206 
207  // get biggest connected segment
208  cout << "getting biggest segment..." << endl;
209 
210  TriangleMesh biggestSegment;
211  origMesh.GetBiggestConnectedSegment(biggestSegment);
212 
213  // output
214  cout << "orig mesh has " << origMesh.GetTriangleIndicesRef().size()
215  << " triangles" << endl;
216  cout << "biggest segment has " << biggestSegment.GetTriangleIndicesRef().size()
217  << " triangles" << endl;
218 
219  return 0;
220 }
const std::vector< BIAS::Vector< int > > & GetTriangleIndicesRef() const
void GetBiggestConnectedSegment(TriangleMesh &biggestSegment) const
Returns the biggest connected segment of this triangle mesh.
void SetMesh(BIAS::Image< unsigned char > texture, std::vector< BIAS::Vector3< double > > meshVertices, std::vector< BIAS::Vector3< double > > vertexNormals, std::vector< BIAS::Vector2< float > > textureCoords, std::vector< BIAS::Vector< int > > triangleIndices)
Definition: TriangleMesh.hh:97
Create and represent a 3D triangle mesh.
Definition: TriangleMesh.hh:84