This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_seed_bilateral_map.js
140 lines (112 loc) · 4.23 KB
/
multi_seed_bilateral_map.js
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
import { Color } from "three";
import { farthestPointSampling } from "./farthest_point_sampling";
import { findNearestNeighbors } from "./nearest_neighbor";
export function findMultiSeedBilateralMap({ geometry, graph, qType, logger, vertexCount = 3 }) {
const { vertices } = geometry;
const { farthestPoints: points } = farthestPointSampling(qType, vertices[0], vertexCount);
const {
allGeodesics: allGeodesics1,
distance: pathDistance1,
path: path1,
} = findNearestNeighbors({
graph,
qType,
logger,
points,
});
const {
allGeodesics: allGeodesics2,
distance: pathDistance2,
path: path2,
} = findNearestNeighbors({
graph,
qType,
logger,
points: [path1[path1.length - 1], path1[0]],
});
const allGeodesics = new Map([...allGeodesics1, ...allGeodesics2]);
const pathDistance = pathDistance1 + pathDistance2;
const path = [...path1, ...path2];
const source = path[0];
const distancesToSource = allGeodesics.get(source).distances;
const G = new Map();
let minDistance = Infinity,
maxDistance = 0;
let startTime, elapsedTime;
startTime = new Date();
logger && logger.log(`Finding Fuzzy Geodesic Scalar Field...`);
graph.vertices.forEach((x) => {
let distance = 0;
allGeodesics.forEach(({ distances }) => {
distance += distances.get(x);
});
distance =
(distance / (allGeodesics.size - 1) - pathDistance / allGeodesics.size) /
(pathDistance / allGeodesics.size);
minDistance = Math.min(distance, minDistance);
maxDistance = Math.max(distance, maxDistance);
G.set(x, distance);
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Calculating initial region...`);
G.forEach((distance, x) => {
if (distance > 0.3) {
G.set(x, Infinity);
}
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Calculating filtering region...`);
G.forEach((distance, x) => {
if (
distancesToSource.get(x) > pathDistance / allGeodesics.size ||
distancesToSource.get(x) > pathDistance / allGeodesics.size
) {
G.set(x, Infinity);
}
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Divide the ROI into bins...`);
const bilateralMap = [];
geometry.faces.forEach((face) => {
const v1 = geometry.vertices[face.a],
v2 = geometry.vertices[face.b],
v3 = geometry.vertices[face.c];
const color = new Color(0xcccccc);
if (G.get(v1) !== Infinity && G.get(v2) !== Infinity && G.get(v3) !== Infinity) {
const distance1 =
(distancesToSource.get(v1) * 20.0) / (pathDistance / allGeodesics.size),
distance2 = (distancesToSource.get(v2) * 20.0) / (pathDistance / allGeodesics.size),
distance3 = (distancesToSource.get(v3) * 20.0) / (pathDistance / allGeodesics.size),
hue = Math.floor((distance1 + distance2 + distance3) / 3.0);
color.setHSL((1 - (hue % 2)) * (2.0 / 3.0), 1.0, 0.5);
// Calculate the area using Heron's formula
const e1 = v1.distanceTo(v2),
e2 = v2.distanceTo(v3),
e3 = v1.distanceTo(v3);
const s = (e1 + e2 + e3) / 2.0;
bilateralMap[hue] = {
x: hue,
y:
((bilateralMap[hue] && bilateralMap[hue].y) || 0) +
Math.sqrt(s * (s - e1) * (s - e2) * (s - e3)),
};
}
face.color = color;
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
return {
path,
points,
scalarField: G,
minDistance,
maxDistance,
bilateralMap,
};
}