Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BRep topology (experimental) #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/boolean.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Boolean Example</title>
<script src="../build/js/verb.js"></script>
<script src="js/three.min.js"></script>
<script src="js/verbToThreeConversion.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/threeBasic.js"></script>

<style> body { margin: 0; } </style>
</head>
<body>
<script src="boolean.js"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions examples/boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function drawSolid(s){

s.faces().forEach(function(x){
addMeshToScene( x.toThreeGeometry(),
new THREE.MeshNormalMaterial(
{ side: THREE.FrontSide, wireframe: false, shading: THREE.SmoothShading, transparent: true, opacity: 0.95 } ));
});

s.halfEdges().forEach(function(x){
addLineToScene( [x.v.pt,x.nxt.v.pt] );
});

s.vertices().forEach(function(x){
addPointsToScene( [x.pt] );
});
}

threeSetup(true);

var ptsa = [[0,0,0], [10,0,0], [10,10,0], [0,10,0] ];
var a = verb.topo.Make.extrusion( ptsa, [0,0,10] );

var ptsb = [[5,5,-5], [15,5,-5], [15,15,-5], [5,15,-5] ];
var b = verb.topo.Make.extrusion( ptsb, [0,0,10] );

var res = verb.topo.Boolean.union( a, b, 1e-6 );

drawSolid( a );
drawSolid( b );

threeRender();
146 changes: 146 additions & 0 deletions examples/eulerOperators.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!DOCTYPE html>
<html>
<head>
<script src="../build/js/verb.js"></script>
<script src="js/three.min.js"></script>
<script src="js/verbToThreeConversion.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/threeBasic.js"></script>
<style> body { margin: 0; } </style>
</head>
<body>
<script>

function triangularLamina(){
var s = verb.topo.Solid.mvfs( [0,0,0] );

var e0 = s.f.l.e;
var v0 = s.v;
var f0 = s.f;

var nv0 = s.lmev( e0, e0, [10,0,0] );
nv1 = s.lmev( nv0.e, nv0.e, [10,10,0] );

console.log(v0.e.v.pt);
console.log(v0.e.nxt.nxt.v.pt);

var nf = s.lmef( v0.e, v0.e.nxt.nxt );

return s;
}

function triangularPrism(){
var s = triangularLamina();

var nvs = s.f.l.halfEdges().map(function(e){
return s.lmev( e, e, verb.core.Vec.add( e.v.pt, [0,0,10]) );
});

var nfs = nvs
.map(function(v){
return v.e;
})
.map(function(e){
var nf = s.lmef(e, e.nxt.nxt.nxt);
return nf;
});

return s;
}

function hollowPrism(){

var s = triangularPrism();

var tfl = s.faces().filter(function(x){
return verb.core.Vec.dot( x.normal(), [0,0,1] ) > 0;
});

var bf = s.faces().filter(function(x){
return verb.core.Vec.dot( x.normal(), [0,0,-1] ) > 0;
})[0];

var tf = tfl[0];
var e0 = tf.l.e.prv;

var nv = s.lmev( tf.l.e, tf.l.e, [2,1,10] );
var nl = s.lkemr(nv.e.prv);

var v0 = nl.e.v;

// make new face inside of this one
var nv1 = s.lmev( nl.e, nl.e, [9,1,10] );
var nv2 = s.lmev( nv1.e, nv1.e, [9,8,10] );
var nf = s.lmef( v0.e, v0.e.nxt.nxt );

// on each vertex of the outer loop of the new face, do an mev
var nvs = nf.l.halfEdges().map(function(e){
return s.lmev( e, e, verb.core.Vec.add( e.v.pt, [0,0,-10]) ); // this extends up to the top face
});

// now iterate around, introducing new faces on every side, now we're extruding the interior face
var nfs = nvs
.map(function(v){
return v.e;
})
.map(function(e){
return s.lmef(e, e.nxt.nxt.nxt);
});

s.lkfmrh( nf, bf );

return s;
}

function drawSolid(s){

s.faces().forEach(function(x){
addMeshToScene( x.toThreeGeometry(),
new THREE.MeshNormalMaterial(
{ side: THREE.FrontSide, wireframe: false, shading: THREE.SmoothShading, transparent: true, opacity: 0.95 } ));
});

s.halfEdges().forEach(function(x){
addLineToScene( [x.v.pt,x.nxt.v.pt] );
});

s.vertices().forEach(function(x){
addPointsToScene( [x.pt] );
});

}

threeSetup(true);

var pts = [];

var s = 32;
var w = 2 * Math.PI / s;
var r = 10;

for (var i = 0 ; i < s; i++){
pts.push( [ r * Math.cos( i*w ), r * Math.sin( i*w ), 0 ] );
}

var pts = [[0,0,0], [10,0,0], [10,10,0], [0,10,0] ];

var s = verb.topo.Make.extrusion( pts, [0,0,10] );

console.log( "volume", verb.topo.Analyze.volume( s ) );
console.log( "area", verb.topo.Analyze.area( s ) );

var n = [ 1 / Math.sqrt(3), 1 / Math.sqrt(3), 1 / Math.sqrt(3) ];
var p = { n : n, o : [5,5,5] };

var r = verb.topo.Split.split( s, p );

drawSolid( r.item0 );
// drawSolid( r.item1 );

threeRender();

</script>

</body>

</html>
25 changes: 25 additions & 0 deletions examples/js/cytoscape.min.js

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions examples/topoViz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<title>TopoViz</title>
<script src="js/jquery.min.js"></script>
<script src="js/cytoscape.min.js"></script>

<script src="../build/js/verb.js"></script>
<script src="js/three.min.js"></script>
<script src="js/verbToThreeConversion.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/threeBasic.js"></script>

<script src="topoViz.js"></script>
<style>
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
}

#info {
color: #c88;
font-size: 1em;
position: absolute;
z-index: -1;
left: 1em;
top: 1em;
}
</style>
</head>

<body>
<div id="cy"></div>
</body>
</html>
78 changes: 78 additions & 0 deletions examples/topoViz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
$(function(){ // on dom ready

var pts = [[0,0,0], [10,0,0], [10,10,0] ] //, [20,10,0], [20,20,0], [0,20,0] ]
var s = verb.topo.Make.extrusion( pts, [0,0,10] ); // hollowPrism();

var p = { n : [0,0,1], o : [0,0,5] };
var r = verb.topo.Split.split( s, p );

// s = r.item1;

var faces = s.faces();
// var vertices = s.vertices();

var eles = faces;

var graphEdges = eles.reduce(function(a, f){
return a.concat(
f.neighbors().map(function(fo){
return { data: { source: f.id.toString(), target: fo.id.toString() } };
}));
}, []);

var graphNodes = eles.map(function(x){

// var pos = verb.core.Vec.sub( x.l.e.v.pt, [5,15,-2]);
//
// var r = 3 * pos[2];
//
// var xp = x.pt[0] + r * pos[0];
// var yp = x.pt[1] + r * pos[1];

return { data: { id: x.id.toString(), name: 'Ele' + x.id.toString() } };
});

$('#cy').cytoscape({
style: cytoscape.stylesheet()
.selector('node')
.css({
'content': 'data(name)'
})
.selector('edge')
.css({
'target-arrow-shape': 'triangle'
})
.selector(':selected')
.css({
'background-color': 'black',
'line-color': 'black',
'target-arrow-color': 'black',
'source-arrow-color': 'black'
})
.selector('.faded')
.css({
'opacity': 0.25,
'text-opacity': 0
}),

elements: {
nodes: graphNodes,
edges: graphEdges
},

layout: {
name: 'grid'
},

// on graph initial layout done (could be async depending on layout...)
ready: function(){
window.cy = this;

// giddy up...

cy.elements().unselectify();

}
});

}); // on dom ready
71 changes: 71 additions & 0 deletions src/verb/topo/Analyze.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package verb.topo;

import verb.core.IDoublyLinkedList;
import verb.core.DoublyLinkedListExtensions;
using verb.core.DoublyLinkedListExtensions;

import verb.core.Vec;
using verb.core.Vec;

using Lambda;

@:expose('topo.Analyze')
class Analyze {

public static function volume(s : Solid, o : Array<Float> = null) : Float {
if (o == null) o = [0.0,0.0,0.0];
var v = 0.0;

for (f in s.f.iter()){
for (l in f.l.iter()){
var se : HalfEdge = l.e;
var ce = se.nxt;
do {
v += tetravol( se.v.pt, ce.v.pt, ce.nxt.v.pt, o );
ce = ce.nxt;
} while ( ce.nxt != se );
}
}

return v / 6.0;
}

//helper method for volume of a solid, as an optimization does not mult by 6
private static function tetravol( a : Array<Float>, b : Array<Float>, c : Array<Float>, d : Array<Float> ){
return ( a.sub(d) ).dot( b.sub(d).cross( c.sub(d) ) );
}

public static function area(s : Solid) : Float {
return s.f.iter().fold(function(f,a){ return a + faceArea( f ); }, 0.0);
}

public static function faceArea(f : Face) : Float {
var n = f.normal();
return f.l.iter().fold(function(l,a){ return a + loopArea( l, n ); }, 0.0);
}

public static function loopArea(l : Loop, n : Array<Float> = null) : Float {
var e = l.e;
if (e == e.nxt || e == e.nxt.nxt) return 0.0;

var v = 0.0;

if (n == null) n = l.f.normal();

var se = l.e;
var o = l.e.v.pt;
var ce = se.nxt;

do {
var a = ce.v.pt;
var b = ce.nxt.v.pt;

v += n.dot( a.sub(o).cross(b.sub(o)) );

ce = ce.nxt;
} while ( ce.nxt != se );

return v / 2;
}
}

Loading