BuildRegularGeoms.py

Go to the documentation of this file.
00001 """Python Module for building solid regular geometric objects.
00002 
00003 Return value are list of vectors, 3 vectors define a facet.
00004 
00005 Copyright (c) Berthold Grupp          2005
00006 License: LGPL
00007 
00008 Sample code for creating a mesh:
00009         facets = Cube(3.0, 4.0, 5.0)
00010         m = Mesh.newMesh()
00011         m.addFacets(facets)
00012 """
00013 
00014 
00015 import math
00016 
00017 def Sphere (radius, count):
00018     """Creates a sphere with a given radius.
00019     
00020     bla bla bla
00021     
00022     """
00023     return Ellipsoid(radius, radius, count)
00024 
00025 def Ellipsoid (lenX, lenY, count):
00026     polyline = []
00027     step = math.pi / count
00028     i = 0.0
00029     while (i < math.pi + step / 10.0):
00030         x = math.cos(i) * lenX
00031         y = math.sin(i) * lenY 
00032         polyline.append([x, y])
00033         i = i + step
00034 
00035     return RotationBody(polyline, count)    
00036 
00037 def Cylinder (radius, len, closed, edgelen, count):
00038     return Cone(radius, radius, len, closed, edgelen, count)
00039 
00040 def Cone (radius1, radius2, len, closed, edgelen, count):
00041     polyline = []
00042     if (closed):        
00043         step = radius2 / math.ceil(radius2 / edgelen)
00044         i = 0.0
00045         while (i < radius2 - step / 2.0):
00046             polyline.append([len, i])
00047             i = i + step
00048 
00049     ct = math.ceil(len / edgelen)        
00050     step = len / ct
00051     rstep = (radius1 - radius2) / ct
00052     i = len;
00053     r = radius2
00054     while (i > 0.0 + step / 2.0):
00055         polyline.append([i, r])
00056         i = i - step
00057         r = r + rstep
00058     polyline.append([0.0, radius1])
00059 
00060     if (closed):
00061         step = radius1 / math.ceil(radius1 / edgelen)
00062         i = radius1 - step
00063         while (i > 0.0 + step / 2.0):
00064             polyline.append([0.0, i])
00065             i = i - step
00066         polyline.append([0.0, 0.0])
00067         
00068     return RotationBody(polyline, count)
00069 
00070 def Toroid (radius1, radius2, count):
00071     polyline = []
00072     
00073     step = math.pi * 2.0 / count
00074     i = -math.pi
00075     while (i < math.pi +  step / 10.0):
00076         x = radius1 + math.cos(i) * radius2
00077         y = radius1 + math.sin(i) * radius2
00078         polyline.append([x, y])
00079         i = i + step
00080 
00081     return RotationBody(polyline, count)
00082          
00083 
00084 def RotationBody (polyline, count):
00085     """Build a rotation body from a given (closed) polyline, rotation axis is the X-Axis.
00086     
00087     Parameter: polyline: list of tuple of 2 floats (2d vector)
00088     
00089     """
00090     facets = []
00091 
00092     step = math.pi * 2.0 / count
00093     i = -math.pi;
00094     while (i < math.pi - step / 10.0):
00095         li = i + step
00096         for j in range(0, len(polyline) - 1):
00097             v1 = polyline[j]
00098             v2 = polyline[j+1]
00099             
00100             x1 = v1[0]
00101             y1 = v1[1] * math.cos(i)
00102             z1 = v1[1] * math.sin(i)
00103             x2 = v2[0]
00104             y2 = v2[1] * math.cos(i)
00105             z2 = v2[1] * math.sin(i)
00106             x3 = v1[0]
00107             y3 = v1[1] * math.cos(li)
00108             z3 = v1[1] * math.sin(li)
00109             x4 = v2[0]
00110             y4 = v2[1] * math.cos(li)
00111             z4 = v2[1] * math.sin(li)
00112 
00113             if (v1[1] != 0.0):            
00114                 facets.append([x1, y1, z1])
00115                 facets.append([x2, y2, z2])
00116                 facets.append([x3, y3, z3])
00117 
00118             if (v2[1] != 0.0):
00119                 facets.append([x2, y2, z2])
00120                 facets.append([x4, y4, z4])
00121                 facets.append([x3, y3, z3])
00122             
00123         i = i + step
00124         
00125     return facets;
00126 
00127 def Cube (lenX, lenY, lenZ):
00128     hx = lenX / 2.0
00129     hy = lenY / 2.0
00130     hz = lenZ / 2.0
00131     
00132     facets = []
00133     
00134     facets.append([-hx, -hy, -hz])
00135     facets.append([hx, -hy, -hz])
00136     facets.append([hx, -hy, hz])
00137 
00138     facets.append([-hx, -hy, -hz])
00139     facets.append([hx, -hy, hz])
00140     facets.append([-hx, -hy, hz])
00141 
00142     facets.append([-hx, hy, -hz])
00143     facets.append([hx, hy, hz])
00144     facets.append([hx, hy, -hz])
00145 
00146     facets.append([-hx, hy, -hz])
00147     facets.append([-hx, hy, hz])
00148     facets.append([hx, hy, hz])
00149 
00150     facets.append([-hx, -hy, -hz])
00151     facets.append([-hx, hy, hz])
00152     facets.append([-hx, hy, -hz])
00153 
00154     facets.append([-hx, -hy, -hz])
00155     facets.append([-hx, -hy, hz])
00156     facets.append([-hx, hy, hz])
00157 
00158     facets.append([hx, -hy, -hz])
00159     facets.append([hx, hy, -hz])
00160     facets.append([hx, hy, hz])
00161 
00162     facets.append([hx, -hy, -hz])
00163     facets.append([hx, hy, hz])
00164     facets.append([hx, -hy, hz])
00165 
00166     facets.append([-hx, -hy, -hz])
00167     facets.append([-hx, hy, -hz])
00168     facets.append([hx, hy, -hz])
00169 
00170     facets.append([-hx, -hy, -hz])
00171     facets.append([hx, hy, -hz])
00172     facets.append([hx, -hy, -hz])
00173 
00174     facets.append([-hx, -hy, hz])
00175     facets.append([hx, hy, hz])
00176     facets.append([-hx, hy, hz])
00177 
00178     facets.append([-hx, -hy, hz])
00179     facets.append([hx, -hy, hz])
00180     facets.append([hx, hy, hz])
00181     
00182     return facets
00183 
00184 def FineCube (lenX, lenY, lenZ, edgelen):
00185     hx = lenX / 2.0
00186     hy = lenY / 2.0
00187     hz = lenZ / 2.0
00188     cx = int(max(lenX / edgelen,1))
00189     dx = lenX / cx
00190     cy = int(max(lenY / edgelen,1))
00191     dy = lenY / cy
00192     cz = int(max(lenZ / edgelen,1))
00193     dz = lenZ / cz
00194     
00195     facets = []
00196 
00197     # z
00198     for i in range(0,cx):
00199         for j in range(0,cy):
00200             facets.append([-hx+(i+0)*dx, -hy+(j+0)*dy, -hz])
00201             facets.append([-hx+(i+0)*dx, -hy+(j+1)*dy, -hz])
00202             facets.append([-hx+(i+1)*dx, -hy+(j+1)*dy, -hz])
00203 
00204             facets.append([-hx+(i+0)*dx, -hy+(j+0)*dy, -hz])
00205             facets.append([-hx+(i+1)*dx, -hy+(j+1)*dy, -hz])
00206             facets.append([-hx+(i+1)*dx, -hy+(j+0)*dy, -hz])
00207 
00208             facets.append([-hx+(i+0)*dx, -hy+(j+0)*dy, hz])
00209             facets.append([-hx+(i+1)*dx, -hy+(j+1)*dy, hz])
00210             facets.append([-hx+(i+0)*dx, -hy+(j+1)*dy, hz])
00211 
00212             facets.append([-hx+(i+0)*dx, -hy+(j+0)*dy, hz])
00213             facets.append([-hx+(i+1)*dx, -hy+(j+0)*dy, hz])
00214             facets.append([-hx+(i+1)*dx, -hy+(j+1)*dy, hz])
00215 
00216     # y
00217     for i in range(0,cx):
00218         for j in range(0,cz):
00219             facets.append([-hx+(i+0)*dx, -hy, -hz+(j+0)*dz])
00220             facets.append([-hx+(i+1)*dx, -hy, -hz+(j+1)*dz])
00221             facets.append([-hx+(i+0)*dx, -hy, -hz+(j+1)*dz])
00222 
00223             facets.append([-hx+(i+0)*dx, -hy, -hz+(j+0)*dz])
00224             facets.append([-hx+(i+1)*dx, -hy, -hz+(j+0)*dz])
00225             facets.append([-hx+(i+1)*dx, -hy, -hz+(j+1)*dz])
00226 
00227             facets.append([-hx+(i+0)*dx, hy, -hz+(j+0)*dz])
00228             facets.append([-hx+(i+0)*dx, hy, -hz+(j+1)*dz])
00229             facets.append([-hx+(i+1)*dx, hy, -hz+(j+1)*dz])
00230 
00231             facets.append([-hx+(i+0)*dx, hy, -hz+(j+0)*dz])
00232             facets.append([-hx+(i+1)*dx, hy, -hz+(j+1)*dz])
00233             facets.append([-hx+(i+1)*dx, hy, -hz+(j+0)*dz])
00234 
00235     # x
00236     for i in range(0,cy):
00237         for j in range(0,cz):
00238             facets.append([-hx, -hy+(i+0)*dy, -hz+(j+0)*dz])
00239             facets.append([-hx, -hy+(i+0)*dy, -hz+(j+1)*dz])
00240             facets.append([-hx, -hy+(i+1)*dy, -hz+(j+1)*dz])
00241 
00242             facets.append([-hx, -hy+(i+0)*dy, -hz+(j+0)*dz])
00243             facets.append([-hx, -hy+(i+1)*dy, -hz+(j+1)*dz])
00244             facets.append([-hx, -hy+(i+1)*dy, -hz+(j+0)*dz])
00245 
00246             facets.append([hx, -hy+(i+0)*dy, -hz+(j+0)*dz])
00247             facets.append([hx, -hy+(i+1)*dy, -hz+(j+1)*dz])
00248             facets.append([hx, -hy+(i+0)*dy, -hz+(j+1)*dz])
00249 
00250             facets.append([hx, -hy+(i+0)*dy, -hz+(j+0)*dz])
00251             facets.append([hx, -hy+(i+1)*dy, -hz+(j+0)*dz])
00252             facets.append([hx, -hy+(i+1)*dy, -hz+(j+1)*dz])
00253 
00254     return facets
00255 
00256 def main ():
00257     Cylinder (10.0, 20.0, 1, 10, 10)
00258     
00259 if __name__ == "__main__":
00260     main()
00261         

Generated on Wed Nov 23 18:59:59 2011 for FreeCAD by  doxygen 1.6.1