Python Tutorials | (back to the list of tutorials) |
The input file used in the example is this.
add_library('igeo') size(480, 360, IG.GL) IG.open("surface11.3dm") surfaces = IG.surfaces() for surf in surfaces : unum = 30 vnum = 30 uinc = 1.0/unum vinc = 1.0/vnum gap = 0.25 for i in range(unum) : for j in range(vnum) : pt1 = surf.pt( i*uinc, j*vinc ) pt2 = surf.pt( (i+1-gap)*uinc, j*vinc ) pt3 = surf.pt( (i+1-gap)*uinc, (j+1-gap)*vinc ) pt4 = surf.pt( i*uinc, (j+1-gap)*vinc ) ISurface(pt1,pt2,pt3,pt4) surf.del()
In the following code, divided rectangular cell is randomly triangulated. The surface is created by offset of the triangular or rectangular cell and extrusion.
add_library('igeo') size(480, 360, IG.GL) IG.open("surface11.3dm") surfaces = IG.surfaces() for surf in surfaces : unum = 30 vnum = 30 uinc = 1.0/unum vinc = 1.0/vnum offsetDist = -0.04 degree = 1 # 1 or 2 for i in range(unum) : for j in range(vnum) : pt1 = surf.pt( i*uinc, j*vinc ) pt2 = surf.pt( (i+1)*uinc, j*vinc ) pt3 = surf.pt( (i+1)*uinc, (j+1)*vinc ) pt4 = surf.pt( i*uinc, (j+1)*vinc ) if IRand.pct(30) : # rectangle crv = ICurve([ pt1, pt2, pt3, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -4).clr(0.2) elif IRand.pct(30) : # triangles crv = ICurve([ pt1, pt2, pt3 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -2).clr(0.2) crv2 = ICurve([ pt1, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -2).clr(0.2) else : # triangles in other direction crv = ICurve([ pt1, pt2, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -1).clr(0.2) crv2 = ICurve([ pt2, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -1).clr(0.2) surf.del()
If you change the variable degree from 1 to 2, it change the profile from polyline to curve and the extruded surface also get curved.
add_library('igeo') size(480, 360, IG.GL) IG.open("surface11.3dm") surfaces = IG.surfaces() for surf in surfaces : unum = 30 vnum = 30 uinc = 1.0/unum vinc = 1.0/vnum offsetDist = -0.04 degree = 2; # 1 or 2 for i in range(unum) : for j in range(vnum) : pt1 = surf.pt( i*uinc, j*vinc ) pt2 = surf.pt( (i+1)*uinc, j*vinc ) pt3 = surf.pt( (i+1)*uinc, (j+1)*vinc ) pt4 = surf.pt( i*uinc, (j+1)*vinc ) if IRand.pct(30) : # rectangle crv = ICurve([ pt1, pt2, pt3, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -4).clr(0.2) elif IRand.pct(30) : # triangles crv = ICurve([ pt1, pt2, pt3 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -2).clr(0.2) crv2 = ICurve([ pt1, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -2).clr(0.2) else : # triangles in other direction crv = ICurve([ pt1, pt2, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -1).clr(0.2) crv2 = ICurve([ pt2, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -1).clr(0.2) surf.del()
The following code takes 1 bitmap input. The bitmap defines void area. When the value of bitmap is less than 0.5 (dark), the geometry is created and otherwise not.
The below is "voidmap.jpg"
add_library('igeo') size(480, 360, IG.GL) IG.open("surface11.3dm") surfaces = IG.surfaces() map1 = IImageMap("voidmap.jpg") for surf in surfaces : unum = 30 vnum = 30 uinc = 1.0/unum vinc = 1.0/vnum offsetDist = -0.04 degree = 1; # 1 or 2 for i in range(unum) : for j in range(vnum) : pt1 = surf.pt( i*uinc, j*vinc ) pt2 = surf.pt( (i+1)*uinc, j*vinc ) pt3 = surf.pt( (i+1)*uinc, (j+1)*vinc ) pt4 = surf.pt( i*uinc, (j+1)*vinc ) if map1.get( (i+0.5)*uinc, (j+0.5)*vinc ) < 0.5 : if IRand.pct(30) : crv = ICurve([ pt1, pt2, pt3, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -3).clr(0.2) elif IRand.pct(30) : crv = ICurve([ pt1, pt2, pt3 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -2).clr(0.2) crv2 = ICurve([ pt1, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -2).clr(0.2) else : crv = ICurve([ pt1, pt2, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -1).clr(0.2) crv2 = ICurve([ pt2, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -1).clr(0.2) surf.del()
The following code takes 5 image maps. Each of them defines, void, height, randomization of height, condition to be triangulated or not, and color.
Here are "voidmap.jpg", "heightmap.jpg",
"heightRandomizeMap.jpg", "divisionmap.jpg" and
"colormap.jpg".
add_library('igeo') size(480, 360, IG.GL) IG.open("surface11.3dm") surfaces = IG.surfaces() map1 = IImageMap("voidmap.jpg") map2 = IImageMap("heightmap.jpg") map3 = IImageMap("heightRandomizeMap.jpg") map4 = IImageMap("divisionmap.jpg") map5 = IImageMap("colormap.jpg") for surf in surfaces : unum = 30 vnum = 30 uinc = 1.0/unum vinc = 1.0/vnum offsetDist = -0.04 degree = 1; # 1 or 2 maxHeight = 2 heightRandomizeRange = 2.5 for i in range(unum) : for j in range(vnum) : pt1 = surf.pt( i*uinc, j*vinc ) pt2 = surf.pt( (i+1)*uinc, j*vinc ) pt3 = surf.pt( (i+1)*uinc, (j+1)*vinc ) pt4 = surf.pt( i*uinc, (j+1)*vinc ) division = map4.get((i+0.5)*uinc,(j+0.5)*vinc)*maxHeight height = map2.get((i+0.5)*uinc,(j+0.5)*vinc)*maxHeight height += map3.get((i+0.5)*uinc,(j+0.5)*vinc) * \ IRand.get(0,heightRandomizeRange); if map1.get( (i+0.5)*uinc, (j+0.5)*vinc ) < 0.5 : if division < 0.5 : crv = ICurve([ pt1, pt2, pt3, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) elif IRand.pct(50) : crv = ICurve([ pt1, pt2, pt3 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) crv2 = ICurve([ pt1, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) else : crv = ICurve([ pt1, pt2, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) crv2 = ICurve([ pt2, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) surf.del()
The following is in case the variable degree is changed to 2.
add_library('igeo') size(480, 360, IG.GL) IG.open("surface11.3dm") surfaces = IG.surfaces() map1 = IImageMap("voidmap.jpg") map2 = IImageMap("heightmap.jpg") map3 = IImageMap("heightRandomizeMap.jpg") map4 = IImageMap("divisionmap.jpg") map5 = IImageMap("colormap.jpg") for surf in surfaces : unum = 30 vnum = 30 uinc = 1.0/unum vinc = 1.0/vnum offsetDist = -0.04 degree = 2 # 1 or 2 maxHeight = 2 heightRandomizeRange = 2.5 for i in range(unum) : for j in range(vnum) : pt1 = surf.pt( i*uinc, j*vinc ) pt2 = surf.pt( (i+1)*uinc, j*vinc ) pt3 = surf.pt( (i+1)*uinc, (j+1)*vinc ) pt4 = surf.pt( i*uinc, (j+1)*vinc ) division = map4.get((i+0.5)*uinc,(j+0.5)*vinc)*maxHeight height = map2.get((i+0.5)*uinc,(j+0.5)*vinc)*maxHeight height += map3.get((i+0.5)*uinc,(j+0.5)*vinc)*IRand.get(0,heightRandomizeRange) if map1.get( (i+0.5)*uinc, (j+0.5)*vinc ) < 0.5 : if division < 0.5 : crv = ICurve([ pt1, pt2, pt3, pt4 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) elif IRand.pct(50) : crv = ICurve([ pt1, pt2, pt3 ], degree, True) offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) crv2 = ICurve([ pt1, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) else : crv = ICurve([ pt1, pt2, pt4 ], degree, True); offCrv = IG.offset(crv, offsetDist) IG.extrude(offCrv, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) crv2 = ICurve([ pt2, pt3, pt4 ], degree, True) offCrv2 = IG.offset(crv2, offsetDist) IG.extrude(offCrv2, -height).clr(map5.clr((i+0.5)*uinc, (j+0.5)*vinc)) surf.del()