Skip to content

Instantly share code, notes, and snippets.

@donbright
Last active August 26, 2015 02:41
Show Gist options
  • Save donbright/0ba2934c6c9b7b69c411 to your computer and use it in GitHub Desktop.
Save donbright/0ba2934c6c9b7b69c411 to your computer and use it in GitHub Desktop.
OpenSCAD fake clipping plane module
// public domain, don bright 2015, http://github.com/donbright
/* Fake version of POV-ray clipping plane, using 'cube' object.
Input is Normal vector, Distance, and 'cubesize'.
Example:
difference() { sphere(3); fakeclipplane([1,1,2],2,10); }
see also
http://www.povray.org/documentation/view/3.6.0/297/
http://planning.cs.uiuc.edu/node102.html
http://www.songho.ca/opengl/gl_anglestoaxes.html
openscad source code transform.cc
*/
/*
arc sin (sine inverse) for four quadrants.
Input is x coord, y coord, and Spread ( y*y / ( x*x+y*y ) )
echo( full_asin(sqrt(3),1, 1/4 ) ); // returns 30 degrees
echo( full_asin(-sqrt(3),1, 1/4 ) ); // returns 150 degrees
echo( full_asin(-sqrt(3),-1, 1/4 ) ); // returns 210 degrees
echo( full_asin(sqrt(3),-1, 1/4 ) ); // returns -30 degrees
*/
function full_asin(x,y,S) = (
(x<0) ? 180-sign(y)*asin(sqrt(S)) : sign(y)*asin(sqrt(S))
);
module fakeclipplane(normal,dist,cubesize) {
nx = normal[0];
ny = normal[1];
nz = normal[2];
Qx = nx*nx;
Qy = ny*ny;
Qz = nz*nz;
Syaw = (Qy+Qx)==0 ? 0 : Qy/(Qy+Qx);
Spitch = Qz/(Qx+Qy+Qz);
roll = 0;
yaw = full_asin(nx,ny,Syaw);
pitch = -full_asin(abs(nx),nz,Spitch);
newdist = dist + cubesize/2;
position_scaler = newdist/sqrt(Qx+Qy+Qz);
newpos = normal * position_scaler;
//echo([nx,ny,nz],[Spitch,Syaw],"r,p,y",[roll,pitch,yaw],newpos);
translate(newpos) {
rotate([roll,pitch,yaw]) {
cube(cubesize,center=true);
}
}
}
difference() { sphere(3); fakeclipplane([1,1,2],2,10); }
//testing
//sphere(20);
//fakeclipplane([cos($t*360),sin($t*360*2),sin($t*360)],20,10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment