Last active
April 4, 2017 12:13
-
-
Save kaosat-dev/93a31eb038de5b748cc44fc466675e8f to your computer and use it in GitHub Desktop.
Partial.lenses: flat list to scene graph & vice versa
This file contains 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
import * as L from "partial.lenses" | |
import * as R from "ramda" | |
/* | |
- uu85 | |
- uu00 | |
- uu10 | |
- uu530 | |
- uu6320 | |
*/ | |
const graph = { | |
id: "uu99", | |
transforms: { | |
pos:[0,5,1], | |
rot:[0,-7,0], | |
sca:[1,1,1], | |
matrix: [1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1] | |
}, | |
children: [ | |
{ | |
id: 'uu00', | |
geometry: "C3BF1E70-0BE7-4E6D-B184-C9F1E84A3423", | |
transforms: { | |
pos:[0,5,1], | |
rot:[0,-7,0], | |
sca:[1,1,1], | |
matrix: [1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1] | |
}, | |
bounds: { | |
min:[0,-7,0], | |
max:[0,5,1] | |
}, | |
}, | |
] | |
} | |
const entititesList = [ | |
{ | |
id:'uu00', | |
parent:'uu85', | |
transforms: { | |
pos:[0,5,1], | |
rot:[0,-7,0], | |
sca:[1,1,1], | |
matrix: [1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1] | |
}, | |
bounds: { | |
min:[0,-7,0], | |
max:[0,5,1] | |
}, | |
}, | |
{id:'uu85', | |
parent:null, | |
transforms: { | |
pos:[-1,0,2.2], | |
rot:[0,9,36], | |
sca:[1,1,0.5] | |
}, | |
bounds: { | |
min:[-50,0,1], | |
max:[20,3,5] | |
}, | |
}, | |
{ | |
id:'uu10', | |
parent:'uu85', | |
transforms: { | |
pos:[0,0,1], | |
rot:[0,9,0], | |
sca:[1,-1,1] | |
}, | |
bounds: { | |
min:[0,0,1], | |
max:[0,3,5] | |
}, | |
}, | |
{ | |
id:'uu530', | |
parent:'uu10', | |
transforms: { | |
pos:[0,0,1], | |
rot:[0,9,0], | |
sca:[1,-1,1] | |
}, | |
bounds: { | |
min:[0,0,1], | |
max:[0,3,5] | |
}, | |
}, | |
{ | |
id:'uu6320', | |
parent:null, | |
transforms: { | |
pos:[0,0,1], | |
rot:[0,9,0], | |
sca:[1,-1,1] | |
}, | |
bounds: { | |
min:[0,0,1], | |
max:[0,3,5] | |
}, | |
}, | |
] | |
const flatten = [L.optional, L.lazy(rec => { | |
const elems = [L.elems, rec] | |
return L.choose(x => { | |
return (x instanceof Array ? elems : | |
x && x.hasOwnProperty('children') && x.children.length >0 ? [x=>{ | |
return [...x.children, L.remove(['children'], x)] // we return the current element's children AND itself, with no children | |
}, rec]: L.identity | |
) | |
}) | |
})] | |
const unFlatten = (entities, parent = {id: null}, tree = []) => { | |
let children = entities | |
.filter(x=>x!==undefined) | |
.filter(x=>x.parent === parent.id) //L.filter(x => x.parent === parent.id) | |
/*L.choose(x=>{ | |
console.log(x) | |
})*/ | |
if (!(children.length === 0)) { | |
//console.log('here') | |
if ( parent.id === null ) { | |
tree = children | |
//console.log('sdfs') | |
} else { | |
parent['children'] = children | |
} | |
children = children.map( child => unFlatten( entities, child ) ) | |
} | |
return tree[0] | |
} | |
console.clear() | |
let dataSimpleInitial = entititesList//[{id:1, parent:2},{id:2,parent:7},{id:7, parent:null}] | |
let result = 0 | |
const unflatenMods = [ | |
L.set([10], {id:102, parent:7}), | |
L.modify([L.elems], x=>(Object.assign({},x,{children:[]}))), | |
L.get(unFlatten)//L.collect(unFlatten) | |
] | |
const flattenMods = [ | |
L.collect(flatten), | |
L.remove([L.elems, "children"]), | |
] | |
var expect = (p, f) => x => p(x) ? f(x) : undefined | |
let inverso = L.iso( | |
expect(R.is(Array), unflatenMods), | |
expect(R.is(Object), flattenMods)) | |
let sceneGraph = L.get(unflatenMods, dataSimpleInitial)//L.set(L.replace(1, 2), 2, 0) | |
let flatList = L.get(flattenMods, sceneGraph) //L.getInverse(unflatenMods, dataSimple) | |
let foo = L.set(['children',0], 44, sceneGraph) | |
console.log('bla',foo) | |
//console.log(dataSimple, dataInit) | |
result = sceneGraph | |
/* | |
var sampleFlags = ["id-19", "id-76", "id-690"] | |
var flag = id => [x=>{console.log('step 4',x); return x}, | |
L.normalize(R.sortBy(R.identity)), | |
x=>{console.log('step 3',x); return x}, | |
L.find(R.equals(id)), | |
x=>{console.log('step 2',x); return x}, | |
L.replace(undefined, false), | |
x=>{console.log('step 1',x); return x}, | |
L.replace(id, true)] | |
//console.log(L.get(flag("id-76"), sampleFlags)) | |
console.log(L.set(flag("id-U92"),false, sampleFlags)) | |
*/ | |
document.getElementById('output').innerHTML = JSON.stringify(result, null, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment