Last active
December 8, 2022 20:59
-
-
Save sketchpunk/c47abea728a012da30e3be655882d5a2 to your computer and use it in GitHub Desktop.
iterators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
const pnts = [-2,0,-2,-2,0,2,2,0,2,2,0,-2]; | |
for( let v of iterFlatVec3Buf( pnts ) ) console.log( v ); | |
instead of | |
const v = [0,0,0]; | |
for( let i=0; i < pnts.length; i+=3 ){ | |
v[0] = pnts[i+0]; | |
v[1] = pnts[i+1]; | |
v[2] = pnts[i+2]; | |
console.log( v ); | |
} | |
*/ | |
function iterFlatVec3Buf( buf ){ | |
let i = 0; | |
const len = buf.length; | |
const result = { value:[0,0,0], done:false }; | |
const next = ()=>{ | |
if( i >= len ) result.done = true; | |
else{ | |
result.value[ 0 ] = buf[ i++ ]; | |
result.value[ 1 ] = buf[ i++ ]; | |
result.value[ 2 ] = buf[ i++ ]; | |
} | |
return result; | |
}; | |
return { [Symbol.iterator](){ return { next }; } }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function iterFlatVec3Line( buf, isClosedLoop=false ){ | |
let i = 0; | |
let j = 0; | |
let jj = 0; | |
const a = [0,0,0]; | |
const b = [0,0,0]; | |
const cnt = buf.length / 3; | |
const iEnd = ( !isClosedLoop )? cnt-1 : cnt; | |
const result = { value:{ a, b }, done:false }; | |
const next = ()=>{ | |
if( i >= iEnd ) result.done = true; | |
else{ | |
j = i * 3; | |
jj = ( ( i + 1 ) % cnt ) * 3; | |
a[ 0 ] = buf[ j+0 ]; | |
a[ 1 ] = buf[ j+1 ]; | |
a[ 2 ] = buf[ j+2 ]; | |
b[ 0 ] = buf[ jj+0 ]; | |
b[ 1 ] = buf[ jj+1 ]; | |
b[ 2 ] = buf[ jj+2 ]; | |
i++; | |
} | |
return result; | |
}; | |
return { [Symbol.iterator](){ return { next }; } }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment