-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.cpp
70 lines (59 loc) · 2.19 KB
/
Vertex.cpp
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
// *********************************************************
// Vertex Class
// Author : Tamy Boubekeur ([email protected]).
// Copyright (C) 2008 Tamy Boubekeur.
// All rights reserved.
// *********************************************************
#include "Vertex.h"
#include <cmath>
#include <algorithm>
using namespace std;
static const unsigned int SIZE_OF_VERTEX = 10;
ostream & operator<< (ostream & output, const Vertex & v) {
output << v.getPos () << endl << v.getNormal ();
return output;
}
void Vertex::interpolate (const Vertex & u, const Vertex & v, float alpha) {
setPos (Vec3Df::interpolate (u.getPos (), v.getPos (), alpha));
Vec3Df normal = Vec3Df::interpolate (u.getNormal (), v.getNormal (), alpha);
normal.normalize ();
setNormal (normal);
}
// ------------------------------------
// Static Members Methods.
// ------------------------------------
void Vertex::computeAveragePosAndRadius (std::vector<Vertex> & vertices,
Vec3Df & center, float & radius) {
center = Vec3Df (0.0, 0.0, 0.0);
for (unsigned int i = 0; i < vertices.size (); i++)
center += vertices[i].getPos ();
center /= float (vertices.size ());
radius = 0.0;
for (unsigned int i = 0; i < vertices.size (); i++) {
float vDistance = Vec3Df::distance (center, vertices[i].getPos ());
if (vDistance > radius)
radius = vDistance;
}
}
void Vertex::scaleToUnitBox (vector<Vertex> & vertices,
Vec3Df & center, float & scaleToUnit) {
computeAveragePosAndRadius (vertices, center, scaleToUnit);
for (unsigned int i = 0; i < vertices.size (); i++)
vertices[i].setPos (Vec3Df::segment (center, vertices[i].getPos ()) / scaleToUnit);
}
//struct KDTree::kdtree (Voxel V, Rayon r){
// if (V.feuille){
// return smallestIntersection
// }
//}
void Vertex::normalizeNormals (vector<Vertex> & vertices) {
for (std::vector<Vertex>::iterator it = vertices.begin ();
it != vertices.end ();
it++) {
Vec3Df n = it->getNormal ();
if (n != Vec3Df (0.0, 0.0, 0.0)) {
n.normalize ();
it->setNormal (n);
}
}
}