-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.h
More file actions
executable file
·404 lines (272 loc) · 6.62 KB
/
view.h
File metadata and controls
executable file
·404 lines (272 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#ifndef _rt_H
#define _rt_H
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <GL/glut.h>
using namespace std;
//
// Sample code for physics simulation
//
// Implements cloth simulation
class Vector3f;
class Triangle;
class TriangleMesh;
class Edge;
//vertex
class Vector3f {
public:
float _item[3];
float & operator [] (int i) {
return _item[i];
}
float operator [] (int i) const {
return _item[i];
}
Vector3f(float x, float y, float z)
{ _item[0] = x ; _item[1] = y ; _item[2] = z; };
Vector3f() {};
Vector3f & operator = (Vector3f & obj)
{
_item[0] = obj[0];
_item[1] = obj[1];
_item[2] = obj[2];
return *this;
};
Vector3f & operator += (const Vector3f & obj)
{
_item[0] += obj[0];
_item[1] += obj[1];
_item[2] += obj[2];
return *this;
};
Vector3f & operator -= (const Vector3f & obj)
{
_item[0] -= obj[0];
_item[1] -= obj[1];
_item[2] -= obj[2];
return *this;
};
Vector3f & operator /= (const float & f)
{
_item[0] /= f;
_item[1] /= f;
_item[2] /= f;
return *this;
};
Vector3f & operator *= (const float & f)
{
_item[0] *= f;
_item[1] *= f;
_item[2] *= f;
return *this;
};
float distance(Vector3f & v)
{
float ret=0.f;
ret = (v[0]-_item[0])*(v[0]-_item[0])
+ (v[1]-_item[1])*(v[1]-_item[1])
+ (v[2]-_item[2])*(v[2]-_item[2]);
ret = sqrt(ret);
return ret;
}
float dot(const Vector3f & input) const
{
return input._item[0]*_item[0] + input._item[1]*_item[1] + input._item[2]*_item[2] ;
}
float normalize()
{
float ret = sqrt(_item[0]*_item[0] + _item[1]*_item[1] + _item[2]*_item[2]);
for (int i = 0; i < 3; i++) _item[i] /= ret;
return ret;
}
friend Vector3f operator % ( const Vector3f & ob1, const Vector3f & ob2);
friend Vector3f operator - ( const Vector3f & ob1, const Vector3f & ob2);
friend Vector3f operator + ( const Vector3f & ob1, const Vector3f & ob2);
};
Vector3f operator % ( const Vector3f & ob1, const Vector3f & ob2)
{
int i; // loop counter
Vector3f ret; // return value
int N_DV3 = 3;
for (i = 0; i < N_DV3; i++)
ret._item[ i ] =
ob1[ ( i + 1 ) % N_DV3 ] * ob2[ ( i + 2 ) % N_DV3 ]
- ob2[ ( i + 1 ) % N_DV3 ] * ob1[ ( i + 2 ) % N_DV3 ];
return ret;
}
Vector3f operator + (const Vector3f& v1, const Vector3f& v2)
{
Vector3f v(v1);
for (int i = 0; i < 3; i++)
v[i] += v2[i];
return v;
}
Vector3f operator - (const Vector3f& v1, const Vector3f& v2)
{
Vector3f v(v1);
for (int i = 0; i < 3 ; i++)
v[i] -= v2[i];
return v;
}
float distance(Vector3f & v1, Vector3f & v2)
{
float ret;
ret = sqrt((v2[0]-v1[0])*(v2[0]-v1[0]) + (v2[1]-v1[1])*(v2[1]-v1[1]) + (v2[2]-v1[2])*(v2[2]-v1[2]));
return ret;
}
ostream & operator << (ostream & stream, Vector3f & obj)
{
stream << obj[0] << ' ' << obj[1] << ' ' << obj[2] << ' ';
return stream; //vic
};
class Triangle {
friend class TriangleMesh;
int _id;
int _vertex[3];
int _normal[3];
int _edge[3];
float _min, _max;
float _color;
float _area;
float _ratio;
public:
Triangle(int v1, int v2, int v3, int n1, int n2, int n3)
{
_vertex[0] = v1; _vertex[1] = v2; _vertex[2] = v3;
_normal[0] = n1; _normal[1] = n2; _normal[2] = n3;
};
void setMorseMinMax(float min, float max)
{
_min = min; _max = max;
}
void getMorseMinMax(float & min, float & max)
{
min = _min; max = _max;
}
void setEdge(int e1, int e2, int e3)
{
_edge[0] = e1;
_edge[1] = e2;
_edge[2] = e3;
};
int edge(int i) { return _edge[i];};
int id() { return _id;};
void setColor(float f) { _color = f ;};
float color() { return _color;};
};
class Edge {
friend bool contain(Edge & e, map < pair <int, int> , Edge > & list) ;
friend int edgeID(Edge & e, map < pair <int, int> , Edge > & list) ;
friend bool contain(Edge & e, vector < Edge > & list) ;
friend int edgeID(Edge & e, vector < Edge > & list) ;
int _v1,_v2;
vector <int> _trig_list;
float length;
int _id;
public:
Edge () {};
Edge (int i, int j)
{
_v1 = i; _v2 = j;
};
bool operator == (Edge & e)
{
if (this->_v1 == e._v1 && this->_v2 == e._v2) return true;
if (this->_v1 == e._v2 && this->_v2 == e._v1) return true;
return false;
}
void add_triangle(int trig)
{
for (int i = 0; i < _trig_list.size(); i++)
if (trig == _trig_list[i]) return;
_trig_list.push_back(trig);
}
void other_trig(const int trig, vector <int> & others)
{
for (int i = 0; i < _trig_list.size(); i++) {
if (_trig_list[i] == trig) continue;
else others.push_back(_trig_list[i]);
}
}
void setId(int id) { _id = id;};
int id() { return _id;};
int v1() { return _v1;};
int v2() { return _v2;};
vector <int> getTrigList() { return _trig_list ;};
};
struct Node {
int _id;
vector<int> edges_to;
vector<float> edges_cost;
bool done;
float cost;
};
float fmax(float f1,float f2, float f3) {
float f = f1;
if (f < f2) f = f2;
if (f < f3) f = f3;
return f;
};
float fmin(float f1,float f2, float f3) {
float f = f1;
if (f > f2) f = f2;
if (f > f3) f = f3;
return f;
};
class TriangleMesh
{
public:
vector <Vector3f> _v;
vector <Vector3f> _vn;
vector <Triangle> _trig;
vector <Node> _node;
vector <Edge> _edge;
// map <pair < int, int > , Edge > _edge;
float _xmax, _xmin, _ymax, _ymin, _zmin, _zmax;
TriangleMesh(char * filename) { loadFile(filename) ;};
TriangleMesh() {};
void loadFile(char * filename);
int trigNum() { return _trig.size() ;};
void getTriangleVertices(int i, Vector3f & v1, Vector3f & v2, Vector3f & v3)
{
v1 = _v[_trig[i]._vertex[0]];
v2 = _v[_trig[i]._vertex[1]];
v3 = _v[_trig[i]._vertex[2]];
}
void getTriangleNormals(int i, Vector3f & v1, Vector3f & v2, Vector3f & v3)
{
v1 = _vn[_trig[i]._normal[0]];
v2 = _vn[_trig[i]._normal[1]];
v3 = _vn[_trig[i]._normal[2]];
}
void getMorseValue(int i, float & v1, float & v2, float & v3)
{
v1 = _node[_trig[i]._vertex[0]].cost;
v2 = _node[_trig[i]._vertex[1]].cost;
v3 = _node[_trig[i]._vertex[2]].cost;
}
float color(int i) { return _trig[i].color();};
void setMorseMinMax(int i, float min, float max)
{
_trig[i].setMorseMinMax(min,max);
}
void getMorseMinMax(int i, float & min, float & max)
{
_trig[i].getMorseMinMax(min,max);
}
void calcTriangleArea()
{
Vector3f v1,v2,v3;
for (int i = 0 ;i < _trig.size(); i++)
{
getTriangleVertices(i, v1,v2,v3);
v3 -= v1;
v2 -= v1;
_trig[i]._area = 0.5f*sqrt(v3.dot(v3)*v2.dot(v2) - (v3.dot(v2)*(v3.dot(v2))));
// cout << "trig " << i << " v2 " << v2 << " v3 " << v3 << " area = " << _trig[i]._area << endl;
}
}
};
#endif //_rt_H