# miniSageCAD

## by mechadense, published

## Description

A tool for Constructive Solid Geometry like OpenSCAD but written in Sage http://www.sagemath.org/

There is already quite some functionality in very little code.

You can (via the Jmole 3D viewer in sage) preview & turn the 3d output before saving to *.stl file. With Sage you have a fully blown computer algebra system CAS at hand. Transfinite unbounded objects can be used for construction.

Sages implicit_plot3d is not made for this purpouse so I think this is more useful for experimentation with system design than useful construction work. But I might be wrong.

In this context I like to mention ImplicitCAD (written by Christopher Olah) http://www.implicitcad.org/ https://github.com/colah/ImplicitCAD/blob/master/Graphics/Implicit/Primitives.hs which Is ment to deliver good performance.

I started to code miniSageCAD because while trying sage it became patently obvious to me how easy It'd be to get a minimal useful CSG system.

I wanted to preserve symbolic derivability for all csg expressions (to have symbolic access to the surface normals). I found that only lambdas are symbolically derivable but those lambda expressions can not have line breaks. This makes it necessary to break more complex objects down into subfunctions. A good thing for documentation (names) but bad in that it clutteres the global namespace. Also obstucted by this is the idea to e.g. transport the positions of the edges of a cube up the csg graph accessible in e linearly-transformed way.

** edit: **
new in v0.04:

```
corrected translate
arbitrary arity union and intersection
prism rod, prism
inshell, onshell, outshell
zfunction
pieslicer
difference2
gyroid (left & right)
```

## Recent Comments

view allunion(){

hull(){

translate([0,0,10])rotate(15,[180,0,0])cylinder(h=0.1,r=5);

translate([0,-15,50])rotate(45,[180,0,0])cylinder(h=0.1,r=12);

}

hull(){

translate([0,-15,50])rotate(45,[180,0,0])cylinder(h=0.1,r=12);

translate([0,-30,70])rotate(60,[180,0,0])cylinder(h=0.1,r=20);

}}

## Tags

## License

## Give a Shout Out

## Instructions

Either try it via the public worksheet (note: they are currently disabled 2012-01-04) http://www.sagenb.org/home/pub/5036/ Or download it and use it within your own sage system.

If you make somthing with it please upload your results.

#### File Name

#### Downloads

#### Size

Please Login to Comment

union(){

hull(){

translate([0,0,10])rotate(15,[180,0,0])cylinder(h=0.1,r=5);

translate([0,-15,50])rotate(45,[180,0,0])cylinder(h=0.1,r=12);

}

hull(){

translate([0,-15,50])rotate(45,[180,0,0])cylinder(h=0.1,r=12);

translate([0,-30,70])rotate(60,[180,0,0])cylinder(h=0.1,r=20);

}}

The reason is that convex hulls are rather nontrivial:

http://en.wikipedia.org/wiki/Convex_hull_algorithms

I think todays practically used algorithms are all vertex based.

You'd have to find a general way to generate an implicit function Surf(c):={x,y,z|f_hull(x,y,z)=c} such that forall c: Surf(c)=boundary(hull(Surf1(c),surf2(c),..)) without relying on vertex based algorithms. I guess that there isn't a noniterative direct way to do that. A hull of compositioned objects (planes & tubes) like your example is even harder. Concrete example: For easier reasoning imagine the convex hull of two circles in a two dimensional room. The implicit function for a circle is a cone. You'd have to generate a "roof" from this two cones only with elementary functions (provable impossible?).

Special cases could be implemented though. And I think this (with auto case choice) is a good way to go. Sadly two arbitrary orientated circles like in your specific case will not work this way. But e.g. a truncated cone as convex hull for two spheres will. In Haskell types could carry the necessary information or monads, but in sage I see hurdles/limits.

Another (less powerful) approach for things similar to this would be to to define an implicit function by the minimal distance to an user choosen parametrically defined spacecurve x(t),y(t),z(t).

The reason is that convex hulls are rather nontrivial:

http://en.wikipedia.org/wiki/Convex_hull_algorithms

I think todays practically used algorithms are all vertex based.

You'd have to find a general way to generate an implicit function Surf(c):={x,y,z|f_hull(x,y,z)=c} such that forall c: Surf(c)=boundary(hull(Surf1(c),surf2(c),..)) without relying on vertex based algorithms. I guess that there isn't a noniterative direct way to do that. A hull of compositioned objects (planes & tubes) like your example is even harder. Concrete example: For easier reasoning imagine the convex hull of two circles in a two dimensional room. The implicit function for a circle is a cone. You'd have to generate a "roof" from this two cones only with elementary functions (provable impossible?).

Special cases could be implemented though. And I think this (with auto case choice) is a good way to go. Sadly two arbitrary orientated circles like in your specific case will not work this way. But e.g. a truncated cone as convex hull for two spheres will. In Haskell types could carry the necessary information or monads, but in sage I see hurdles/limits.

Another (less powerful) approach for things similar to this would be to to define an implicit function by the minimal distance to an user choosen parametrically defined spacecurve x(t),y(t),z(t).