为了在OpenGL中便于显示,把待显示的模型的点坐标压缩到-1到1之间。
//寻找点集中最大和最小的x,y,z
vector<Point> Util::findMaxAndMin(vector<Point> v)
{
vector<Point> vector;
Point max, min;
max.x = v[0].x;
max.y = v[0].y;
max.z = v[0].z;
min.x = v[0].x;
min.y = v[0].y;
min.z = v[0].z;
for each (Point var in v)
{
if (var.x > max.x)
{
max.x = var.x;
}
if (var.y > max.y)
{
max.y = var.y;
}
if (var.z > max.z)
{
max.z = var.z;
}
if (var.x < min.x)
{
min.x = var.x;
}
if (var.y < min.y)
{
min.y = var.y;
}
if (var.z < min.z)
{
min.z = var.z;
}
}
vector.push_back(max);
vector.push_back(min);
return vector;
}
//进行标准化
vector<Point> Util::normalize1(vector<Point> p)
{
vector<Point> point_set = p;
vector<Point> mm = findMaxAndMin(point_set);
struct Point max_bound = mm[0];
struct Point min_bound = mm[1];
double middlex, middley, middlez;
middlex = (max_bound.x + min_bound.x) / 2;
middley = (max_bound.y + min_bound.y) / 2;
middlez = (max_bound.z + min_bound.z) / 2;
for (int i = 0; i < point_set.size(); i++) {
point_set[i].x = point_set[i].x - middlex;
point_set[i].y = point_set[i].y - middley;
point_set[i].z = point_set[i].z - middlez;
}
//scale based on x range
double scalecoe = (max_bound.x - min_bound.x) / 2;
for (int i = 0; i < point_set.size(); i++) {
point_set[i].x = point_set[i].x / scalecoe;
point_set[i].y = point_set[i].y / scalecoe;
point_set[i].z = point_set[i].z / scalecoe;
}
return point_set;
}