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