1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | */ |
---|
6 | #include <stdlib.h> |
---|
7 | #include <stdio.h> |
---|
8 | |
---|
9 | #include <vrmath/Vector3f.h> |
---|
10 | #include <vrmath/Vector4f.h> |
---|
11 | |
---|
12 | #include "PointSet.h" |
---|
13 | #include "PCASplit.h" |
---|
14 | |
---|
15 | void |
---|
16 | PointSet::initialize(vrmath::Vector4f *values, const unsigned int count, |
---|
17 | const vrmath::Vector3f& scale, const vrmath::Vector3f& origin, |
---|
18 | float min, float max) |
---|
19 | { |
---|
20 | PCA::PCASplit pcaSplit; |
---|
21 | |
---|
22 | _scale = scale; |
---|
23 | _origin = origin; |
---|
24 | _min = min; |
---|
25 | _max = max; |
---|
26 | |
---|
27 | PCA::Point *points = (PCA::Point *)malloc(sizeof(PCA::Point) * count); |
---|
28 | for (unsigned int i = 0; i < count; ++i) { |
---|
29 | points[i].position.set(values[i].x, values[i].y, values[i].z); |
---|
30 | points[i].color.set(1.0f, 1.0f, 1.0f, 0.2f); |
---|
31 | points[i].value = values[i].w; |
---|
32 | points[i].size = 0.001; |
---|
33 | } |
---|
34 | |
---|
35 | if (_cluster != 0) { |
---|
36 | delete _cluster; |
---|
37 | } |
---|
38 | |
---|
39 | pcaSplit.setMinDistance(scale.length() / 3.0f); |
---|
40 | _cluster = pcaSplit.doIt(points, count); |
---|
41 | } |
---|
42 | |
---|
43 | void |
---|
44 | PointSet::updateColor(float *color, int colorCount) |
---|
45 | { |
---|
46 | if (_cluster == 0) { |
---|
47 | return; |
---|
48 | } |
---|
49 | int level = 4; |
---|
50 | |
---|
51 | PCA::Cluster *cluster = _cluster->startPointerCluster[level - 1]; |
---|
52 | PCA::Cluster *c = &(cluster[0]); |
---|
53 | PCA::Cluster *end = &(cluster[_cluster->numOfClusters[level - 1] - 1]); |
---|
54 | |
---|
55 | vrmath::Vector3f pos; |
---|
56 | |
---|
57 | PCA::Point *points; |
---|
58 | vrmath::Vector4f *colors = (vrmath::Vector4f *)color; |
---|
59 | int numOfPoints; |
---|
60 | int index; |
---|
61 | float d = _max - _min; |
---|
62 | for (; c <= end; c = (PCA::Cluster *)((char *)c + sizeof(PCA::Cluster))) { |
---|
63 | points = c->points; |
---|
64 | numOfPoints = c->numOfPoints; |
---|
65 | |
---|
66 | for (int i = 0; i < numOfPoints; ++i) { |
---|
67 | index = (int)((points[i].value - _min) / d * (colorCount - 1)); |
---|
68 | if (index >= colorCount) { |
---|
69 | index = colorCount - 1; |
---|
70 | } |
---|
71 | points[i].color = colors[index]; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|