Javascript 3D convex-hull algorithm similar to Matlab's convhulln - javascript

I've been struggling to find a convex-hull library for Javascript that gives the same output as the Matlab convhulln function.
I am transcribing some code from Matlab to Javascript and I need to find triangles that make up the convex hull of a set of vertices. Matlab calculates the convex hull of some 3D points using convhulln (which makes use of the qhull function). In this case Matlab convhulln outputs a different set of triangle faces to that of quickhull3d when I pass the same vertices to each. Some faces coincide but in general most do not. The only similarity they have is they both output the same number of faces (although the actual vertices of the faces are not the same).
http://www.mathworks.com/help/matlab/ref/convhulln.html
In Javascript, I've tried different libraries and each one gives a different output..
From npm I've tried:
quickhull3d - https://github.com/maurizzzio/quickhull3d
convex-hull
delaunay-triangulate
most other algorithms have been designed for 2D points, so I have ruled them out.
Any thoughts, tips or feedback would be greatly appreciated! Thank you!
Here are the vertices I'm using and the results of both MATlab's convhulln and quickhull3d. Please note I've used a sorting algorithm to sort them in the from first vertices to last. This should not effect the actual triangle faces since I sorted them in exactly the same way in both the MATlab and JS code.
Here are plots of the two hulls.
Notice the holes. I have spoken to the developer of the quickhull3d algorithm (thanks for your response!!) and he has suggested it might be that the triangulation process he used is different to one matlab uses.
vertices = [
[ 0.9510565162951535, -0.3090169943749474, 0 ],
[ 0.5877852522924731, -0.8090169943749475, 0 ],
[ 6.123233995736766e-17, -1, 0 ],
[ -0.5591929034707466, 0.8290375725550418, 0 ],
[ -0.9510565162951535, -0.3090169943749475, 0 ],
[ -0.9510565162951536, 0.3090169943749473, 0 ],
[ -0.5877852522924732, 0.8090169943749473, 0 ],
[ -1.8369701987210297e-16, 1, 0 ],
[ 0.5877852522924729, 0.8090169943749476, 0 ],
[ 0.9510565162951535, 0.3090169943749476, 0 ],
[ 0.984807753012208, 0, -0.17364817766693033 ],
[ 0.30432233187297814, -0.9366078308002486, -0.17364817766693033 ],
[ -0.796726208379082, -0.5788554735638644, -0.17364817766693033 ],
[ -0.7967262083790821, 0.5788554735638641, -0.17364817766693033 ],
[ 0.3043223318729779, 0.9366078308002487, -0.17364817766693033 ],
[ 0.5000000000000001, -0.5, 0.7071067811865475 ],
[ -0.5, -0.5000000000000001, 0.7071067811865475 ],
[ -0.5000000000000001, 0.5, 0.7071067811865475 ],
[ 0.4999999999999999, 0.5000000000000001, 0.7071067811865475 ],
[ 6.123233995736766e-17, 0, 1 ]
]
triangles from quickhull3d:
dim = 36x3
trianglesqh = [
[ 0, 1, 11 ],
[ 0, 9, 18 ],
[ 0, 10, 9 ],
[ 0, 11, 10 ],
[ 0, 15, 1 ],
[ 0, 18, 15 ],
[ 1, 2, 11 ],
[ 1, 15, 2 ],
[ 2, 12, 11 ],
[ 2, 15, 16 ],
[ 2, 16, 12 ],
[ 3, 6, 17 ],
[ 3, 7, 14 ],
[ 3, 13, 6 ],
[ 3, 14, 13 ],
[ 3, 17, 7 ],
[ 4, 5, 13 ],
[ 4, 12, 16 ],
[ 4, 13, 12 ],
[ 4, 16, 17 ],
[ 4, 17, 5 ],
[ 5, 6, 13 ],
[ 5, 17, 6 ],
[ 7, 8, 14 ],
[ 7, 17, 18 ],
[ 7, 18, 8 ],
[ 8, 9, 10 ],
[ 8, 10, 14 ],
[ 8, 18, 9 ],
[ 10, 11, 12 ],
[ 10, 12, 14 ],
[ 12, 13, 14 ],
[ 15, 18, 19 ],
[ 15, 19, 16 ],
[ 16, 19, 17 ],
[ 17, 19, 18 ]
]
triangles from MATlab:
dim = 36x3
trianglesm = [
[ 0, 1, 11 ],
[ 0, 9, 18 ],
[ 0, 10, 9 ],
[ 0, 11, 10 ],
[ 0, 15, 1 ],
[ 0, 18, 15 ],
[ 1, 2, 11 ],
[ 1, 18, 2 ],
[ 2, 3, 11 ],
[ 2, 15, 16 ],
[ 2, 16, 3 ],
[ 3, 4, 12 ],
[ 3, 12, 11 ],
[ 3, 16, 4 ],
[ 4, 5, 12 ],
[ 4, 17, 5 ],
[ 5, 8, 13 ],
[ 5, 13, 12 ],
[ 5, 16, 17 ],
[ 5, 17, 6 ],
[ 6, 7, 14 ],
[ 6, 14, 13 ],
[ 6, 17, 7 ],
[ 7, 8, 14 ],
[ 7, 17, 18 ],
[ 7, 18, 8 ],
[ 8, 9, 10 ],
[ 8, 10, 14 ],
[ 8, 18, 9 ],
[ 10, 11, 14 ],
[ 11, 12, 13 ],
[ 11, 13, 14 ],
[ 15, 18, 19 ],
[ 15, 19, 16 ],
[ 16, 19, 17 ],
[ 17, 19, 18 ]
]

You could try the Bowyer-Watson algorithm but with circumsphere and tetrahedrons:https://en.m.wikipedia.org/wiki/Bowyer%E2%80%93Watson_algorithm.

Related

why am i getting <1 empty item> when using mapping in JavaScript

im using JSON to return an array.
Json:
const data = [{
"week": 1,
"lost": 10,
"recovery_timespan": [{
"week": 2,
"count": 1
}, {
"week": 3,
"count": 0
}],
"netLost": 10,
"netReturned": 20
}, {
"week": 2,
"lost": 7,
"recovery_timespan": [{
"week": 3,
"count": 1
}, {
"week": 4,
"count": 3
}],
"netLost": 30,
"netReturned": 200
}, {
"week": 3,
"lost": 8,
"recovery_timespan": [{
"week": 4,
"count": 1
}],
"netLost": 50,
"netReturned": 40
}];
i need to get the data into a array with lost,counts of recovery_timespan,netLost,netReturned.
Expected Output:
[ [ 10, 1, 0, 10, 20 ],
[ 7, 1, 3, 30, 200 ],
[ 8, 1, 50, 40 ] ]
My approach:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count),
,netLost,netReturned
]);
console.log(result);
and this return array with <1 empty item>:
[ [ 10, 1, 0, <1 empty item>, 10, 20 ],
[ 7, 1, 3, <1 empty item>, 30, 200 ],
[ 8, 1, <1 empty item>, 50, 40 ] ]
Wha is the issue here?
Why am i getting <1 empty item>
You have an extra comma:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count),
here ---> ,netLost,netReturned
]);
Just remove it.
You have an additional , after the nested map:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count), // <--
,netLost,netReturned
//^--
]);
That creates a hole in the array. That's why you are seeing <1 empty item> in the output
console.log([1,,2])
const res = data.map((el) => [
el.lost,
...el.recovery_timespan.map((timespan) => timespan.count),
/* extra comma here --> */, el.netLost,
el.netReturned
])
[ [ 10, 1, 0, 10, 20 ], [ 7, 1, 3, 30, 200 ], [ 8, 1, 50, 40 ] ]
Not completly sure, but maybe try this.
...recovery_timespan.map(({count}) => count.count)

What do the numbers in Youtube Suggestion API mean?

Here is a portion of the response from Youtube Suggestion API for the URL (https://clients1.google.com/complete/search?client=youtube&q=b%20walmart&gl=&jsonp=jsonpCB&_=1600401501454)
[
"b walmart",
[
[
"walmart b complex",
0,
[
22,
30
]
],
[
"b walmart",
0
],
[
"walmart b&m",
0,
[
22,
30
]
]
],
{
"k": 1,
"q": "295XbO_S8qtvvBAI-FdfMCmHaHk"
}
]
Could someone help me understand the significance of the numbers?
Thanks.

How to print words from object to specified position in javascript

I have object in javascript and keys of the object are words of my paragraph how to print it in paragraph.Value(key:value) holds its position.
Tried using for loop to fetch didnt work for me
var userdata= {
"\"Ten": [
0
],
"blue": [
1
],
"links\"": [
2
],
"have": [
3
],
"defined": [
4
],
"web": [
5,
36,
65
],
"search": [
6,
32,
37,
70,
90,
108,
126
],
"results": [
7,
33,
38,
71,
82,
99,
119
],
"for": [
8,
80
],
"the": [
9,
28,
56,
61,
69,
95,
105
],
"last": [
10
],
"fifteen": [
11
],
"years": [
12
],
"--": [
13
],
"snippets": [
14
],
"of": [
15,
30,
63,
97,
107,
125
],
"text": [
16
],
"combined": [
17
],
"with": [
18,
60
],
"document": [
19
],
"titles": [
20
],
"and": [
21,
46,
52,
85
],
"URLs.": [
22
],
"In": [
23
],
"this": [
24
],
"paper,": [
25
],
"we": [
26,
111,
114
],
"establish": [
27
],
"notion": [
29
],
"enhanced": [
31,
81,
98,
118
],
"that": [
34,
54,
75,
113
],
"extend": [
35
],
"to": [
39,
58,
103,
120
],
"include": [
40
],
"multimedia": [
41
],
"objects": [
42
],
"such": [
43
],
"as": [
44
],
"images": [
45
],
"video,": [
47
],
"intent-specific": [
48
],
"key": [
49
],
"value": [
50
],
"pairs,": [
51
],
"elements": [
53
],
"allow": [
55
],
"user": [
57
],
"interact": [
59
],
"contents": [
62
],
"a": [
64,
78,
122
],
"page": [
66
],
"directly": [
67
],
"from": [
68
],
"page.": [
72
],
"We": [
73,
92
],
"show": [
74,
112
],
"users": [
76,
102
],
"express": [
77
],
"preference": [
79
],
"both": [
83
],
"explicitly,": [
84
],
"when": [
86
],
"observed": [
87
],
"in": [
88,
100
],
"their": [
89
],
"behavior.": [
91
],
"also": [
93
],
"demonstrate": [
94
],
"effectiveness": [
96
],
"helping": [
101
],
"assess": [
104
],
"relevance": [
106
],
"results.": [
109
],
"Lastly,": [
110
],
"can": [
115
],
"efficiently": [
116
],
"generate": [
117
],
"cover": [
121
],
"significant": [
123
],
"fraction": [
124
],
"result": [
127
],
"pages.": [
128
]
};
"Ten blue links" have defined web search results for the last fifteen years -- snippets of text combined with document titles and URLs. In this paper, we establish the notion of enhanced search results that extend web search results to include multimedia objects such as images and video, intent-specific key value pairs, and elements that allow the user to interact with the contents of a web page directly from the search results page. We show that users express a preference for enhanced results both explicitly, and when observed in their search behavior. We also demonstrate the effectiveness of enhanced results in helping users to assess the relevance of search results. Lastly, we show that we can efficiently generate enhanced results to cover a significant fraction of search result pages.
Convert the object to word/indexes pair with Object.entries(). Iterate the entries using Array.reduce(). Inside the reduce, iterate the indexes with Array.forEach(), and assign each word to its index in the accumulator (r). Join the the array of words with a space.
const userdata = {"\"Ten":[0],"blue":[1],"links\"":[2],"have":[3],"defined":[4],"web":[5,36,65],"search":[6,32,37,70,90,108,126],"results":[7,33,38,71,82,99,119],"for":[8,80],"the":[9,28,56,61,69,95,105],"last":[10],"fifteen":[11],"years":[12],"--":[13],"snippets":[14],"of":[15,30,63,97,107,125],"text":[16],"combined":[17],"with":[18,60],"document":[19],"titles":[20],"and":[21,46,52,85],"URLs.":[22],"In":[23],"this":[24],"paper,":[25],"we":[26,111,114],"establish":[27],"notion":[29],"enhanced":[31,81,98,118],"that":[34,54,75,113],"extend":[35],"to":[39,58,103,120],"include":[40],"multimedia":[41],"objects":[42],"such":[43],"as":[44],"images":[45],"video,":[47],"intent-specific":[48],"key":[49],"value":[50],"pairs,":[51],"elements":[53],"allow":[55],"user":[57],"interact":[59],"contents":[62],"a":[64,78,122],"page":[66],"directly":[67],"from":[68],"page.":[72],"We":[73,92],"show":[74,112],"users":[76,102],"express":[77],"preference":[79],"both":[83],"explicitly,":[84],"when":[86],"observed":[87],"in":[88,100],"their":[89],"behavior.":[91],"also":[93],"demonstrate":[94],"effectiveness":[96],"helping":[101],"assess":[104],"relevance":[106],"results.":[109],"Lastly,":[110],"can":[115],"efficiently":[116],"generate":[117],"cover":[121],"significant":[123],"fraction":[124],"result":[127],"pages.":[128]};
const result = Object.entries(userdata)
.reduce((r, [word, indexes]) => {
indexes.forEach(index => r[index] = word);
return r;
}, [])
.join(' ');
console.log(result);
You can loop through that object and
get the key name (word)
use the provided positions (index) from userdata[word]
define in a result array the index and word to use, such as arrResult[index] = word.
And then, join that array in a string using ' ' as delimiter
In example :
var userdata = {"\"Ten":[0],"blue":[1],"links\"":[2],"have":[3],"defined":[4],"web":[5,36,65],"search":[6,32,37,70,90,108,126],"results":[7,33,38,71,82,99,119],"for":[8,80],"the":[9,28,56,61,69,95,105],"last":[10],"fifteen":[11],"years":[12],"--":[13],"snippets":[14],"of":[15,30,63,97,107,125],"text":[16],"combined":[17],"with":[18,60],"document":[19],"titles":[20],"and":[21,46,52,85],"URLs.":[22],"In":[23],"this":[24],"paper,":[25],"we":[26,111,114],"establish":[27],"notion":[29],"enhanced":[31,81,98,118],"that":[34,54,75,113],"extend":[35],"to":[39,58,103,120],"include":[40],"multimedia":[41],"objects":[42],"such":[43],"as":[44],"images":[45],"video,":[47],"intent-specific":[48],"key":[49],"value":[50],"pairs,":[51],"elements":[53],"allow":[55],"user":[57],"interact":[59],"contents":[62],"a":[64,78,122],"page":[66],"directly":[67],"from":[68],"page.":[72],"We":[73,92],"show":[74,112],"users":[76,102],"express":[77],"preference":[79],"both":[83],"explicitly,":[84],"when":[86],"observed":[87],"in":[88,100],"their":[89],"behavior.":[91],"also":[93],"demonstrate":[94],"effectiveness":[96],"helping":[101],"assess":[104],"relevance":[106],"results.":[109],"Lastly,":[110],"can":[115],"efficiently":[116],"generate":[117],"cover":[121],"significant":[123],"fraction":[124],"result":[127],"pages.":[128]};
let arrResult = [];
for (let word in userdata)
{
userdata[word].forEach((i) =>
{
arrResult[i] = word;
});
}
let result = arrResult.join(' ');
console.log(result);
In your case, your data is the property name itself, so, the easiest way is to get that names with:
var foo = Object.getOwnPropertyNames(userdata);
That function returns you all the property names of the object in an array, so you simply join it:
foo.join(" ");
Hope it suits for you.
EDIT
The above code is not a functional solution in this case, didn't consider the words' index. So the correct way to aproach the solution whould be something like Cid's answer (https://stackoverflow.com/a/55474582/9925983):
const userdata = {"\"Ten":[0],"blue":[1],"links\"":[2],"have":[3],"defined":[4],"web":[5,36,65],"search":[6,32,37,70,90,108,126],"results":[7,33,38,71,82,99,119],"for":[8,80],"the":[9,28,56,61,69,95,105],"last":[10],"fifteen":[11],"years":[12],"--":[13],"snippets":[14],"of":[15,30,63,97,107,125],"text":[16],"combined":[17],"with":[18,60],"document":[19],"titles":[20],"and":[21,46,52,85],"URLs.":[22],"In":[23],"this":[24],"paper,":[25],"we":[26,111,114],"establish":[27],"notion":[29],"enhanced":[31,81,98,118],"that":[34,54,75,113],"extend":[35],"to":[39,58,103,120],"include":[40],"multimedia":[41],"objects":[42],"such":[43],"as":[44],"images":[45],"video,":[47],"intent-specific":[48],"key":[49],"value":[50],"pairs,":[51],"elements":[53],"allow":[55],"user":[57],"interact":[59],"contents":[62],"a":[64,78,122],"page":[66],"directly":[67],"from":[68],"page.":[72],"We":[73,92],"show":[74,112],"users":[76,102],"express":[77],"preference":[79],"both":[83],"explicitly,":[84],"when":[86],"observed":[87],"in":[88,100],"their":[89],"behavior.":[91],"also":[93],"demonstrate":[94],"effectiveness":[96],"helping":[101],"assess":[104],"relevance":[106],"results.":[109],"Lastly,":[110],"can":[115],"efficiently":[116],"generate":[117],"cover":[121],"significant":[123],"fraction":[124],"result":[127],"pages.":[128]};
let words = [];
Object.getOwnPropertyNames(userdata).forEach(propertyName =>
userdata[propertyName].forEach(value => words[value] = propertyName)
);
const text = words.join(' ');
console.log(text);
His answer still being more readable and efficient in my opinion.

Why is JavaScript object value are change when I push another value in array? [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 5 years ago.
Here is my code:
var arr = [];
var obj = {};
(function() {
for(let i = 1; i <= 10; i++) {
arr.push(i);
obj[i] = arr;
}
})();
This code gave me this output:
{
'1': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'2': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'3': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'4': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'5': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'6': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'7': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'8': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'9': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'10': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
}
Why doesn't it give an output like this?
{
'1': [ 1 ],
'2': [ 1, 2 ],
'3': [ 1, 2, 3 ],
'4': [ 1, 2, 3, 4 ],
'5': [ 1, 2, 3, 4, 5 ],
'6': [ 1, 2, 3, 4, 5, 6 ],
'7': [ 1, 2, 3, 4, 5, 6, 7 ],
'8': [ 1, 2, 3, 4, 5, 6, 7, 8 ],
'9': [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
'10': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
}
When you push, you're pushing the reference to the same array each time. What you actually want is to create a new array each time rather than reference the same one.
Check out this SO answer for more info Javascript by reference vs. by value
You could do this by pushing a copy arr using arr.slice or Array#from. At the very end arr will have all 10 numbers in it, but obj will look exactly like your output.
var arr = [];
var obj = {};
(function() {
for(let i = 1; i <= 10; i++) {
arr.push(i);
obj[i] = arr.slice();
}
console.log(obj);
})();

JSON Syntax assistance

So I'm trying to manipulate some rave charts using JSON. The charts are used in Cognos and it uses visJSON if that helps. I am trying to add a trend line to my bar charts but all the syntax I have found isn't working. Any help would be greatly appreciated.
"id":"dataSet",
"fields":
[
{
"id":"categories",
"label":"",
"categories":
[
"abc",
"abc",
"abc"
]
},
{
"id":"series",
"label":"",
"categories":
[
"abc",
"abc",
"abc"
]
},
{
"id":"size",
"label":"abc"
}
],
"rows":
[
[
0,
0,
1500
],
[
0,
1,
1700
],
[
0,
2,
1600
],
[
1,
0,
2400
],
[
1,
1,
2200
],
[
1,
2,
2600
],
[
2,
0,
2800
],
[
2,
1,
1600
],
[
2,
2,
1800
]
]
}
],
A tool that you'd probably find helpful in the future:
http://jsonlint.com/
I ran your JSON through it and it returned this:
Parse error on line 1:
"id":"dataSet",
^
Expecting '{', '['
The problem was your extra array bracket at the end. Here it is corrected:
{
"id": "dataSet",
"fields": [
{
"id": "categories",
"label": "",
"categories": [
"abc",
"abc",
"abc"
]
},
{
"id": "series",
"label": "",
"categories": [
"abc",
"abc",
"abc"
]
},
{
"id": "size",
"label": "abc"
}
],
"rows": [
[
0,
0,
1500
],
[
0,
1,
1700
],
[
0,
2,
1600
],
[
1,
0,
2400
],
[
1,
1,
2200
],
[
1,
2,
2600
],
[
2,
0,
2800
],
[
2,
1,
1600
],
[
2,
2,
1800
]
]
}

Categories

Resources