source: subversion/applications/rendering/parking/osray/osray.py @ 22386

Last change on this file since 22386 was 22386, checked in by kdrangmeister, 9 years ago

landuse-types

File size: 7.7 KB
Line 
1# -*- coding: utf-8 -*-
2# by kay drangmeister
3
4import sys
5import commands
6import psycopg2
7import csv
8import re
9import string
10from numpy import *
11from osray_db import *
12from osray_ground import *
13from osray_streets import *
14from osray_buildings import *
15from osray_furniture import *
16from optparse import OptionParser
17
18Radiosity = True
19gutter_factor = 1.2
20
21def pov_globals(f):
22    if Radiosity:
23        globsettings = """
24#include "colors.inc"
25global_settings {{
26    assumed_gamma 1.0
27    noise_generator 2
28
29    ambient_light rgb <0.1,0.1,0.1>
30    radiosity {{
31        count 30
32        error_bound 0.01
33        recursion_limit 3
34        pretrace_end 0.0005
35        brightness 1
36    }}
37}}
38"""
39    else:
40        globsettings = """
41#include "colors.inc"
42global_settings {{
43    assumed_gamma 2.0
44    noise_generator 2
45    ambient_light rgb <0.8,0.8,0.8>
46}}
47"""
48    f.write(globsettings.format())
49
50def pov_camera(f,osraydb,options):
51
52    bottom = float(osraydb.bottom)
53    left = float(osraydb.left)
54    top = float(osraydb.top)
55    right = float(osraydb.right)
56    map_size_x = right-left
57    map_size_y = top-bottom
58    middle_x = avg(left,right)
59    middle_y = avg(bottom,top)
60
61    print "map size x = ",map_size_x
62    print "map size y = ",map_size_y
63   
64    image_aspect = float(options['width'])/float(options['height'])
65    map_aspect = map_size_x/map_size_y
66
67    isometric_angle = 20 # in °
68    zoom = gutter_factor
69    zoom = map_size_y/zoom
70
71    f.write("""
72camera {{
73   orthographic
74   location <0, 10000, 0>
75   sky <0, 1, 0>
76   direction <0, 0, 1>
77   right <{aspect}*{zoom}, 0, 0>
78   up <0, 1*{zoom}*cos(radians({angle})), 0> /* this stretches in y to compensate for the rotate below */
79   look_at <0, 0, 0>
80   rotate <-{angle},0,0>
81   scale <1,1,1>
82   translate <{middle_x},0,{middle_y}>
83}}
84""".format(middle_x=middle_x,middle_y=middle_y,zoom=zoom,angle=isometric_angle,aspect=map_aspect))
85    if Radiosity:
86        ground_finish = """
87    finish {
88        diffuse 0.7
89        ambient 0.0
90    }
91"""       
92    else:
93        ground_finish = """
94    finish {
95        specular 0.5
96        roughness 0.05
97        ambient 0.2
98    }
99"""
100    f.write("""
101/* ground */
102box {{
103    <{left}, -0.5, {bottom}>, <{right}, -0.2, {top}>
104    texture {{
105        pigment {{
106            /* color rgb <1, 1, 1> */
107            image_map {{
108                png "scene-osray-landuse-texture.png"
109                map_type 0
110                interpolate 2
111            }}
112        }}
113        rotate <90,0,0>
114        scale <{xs},1,{ys}>
115        translate <{left},0,{bottom}>
116    }}
117    {finish}
118}}
119/* sky */
120sky_sphere {{
121    pigment {{
122        gradient y
123        color_map {{
124/*
125            [ 0.5 color rgb <0.5,0.6,0.6> ]
126            [ 1.0 color rgb <0.5,0.5,0.6> ]
127*/
128            [ 0.5 color rgb <0.12,0.12,0.12> ]
129            [ 1.0 color rgb <0.1,0.1,0.1> ]
130        }}
131        scale 20000
132        translate -10
133    }}
134}}
135""".format(left=left,bottom=bottom,right=right,top=top,xs=map_size_x,ys=map_size_y,finish=ground_finish))
136
137    #f.write("""light_source {{ <{0}, 50000, {1}>, rgb <0.5, 0.5, 0.5> }}\n""".format(avg(left*1.5,right*0.5),avg(bottom*1.5,top*0.5)))
138    #f.write("""light_source {{ <{0}, 5000, {1}>, rgb <0.5, 0.5, 0.5> }}\n""".format(avg(left*0.5,right*1.5),avg(bottom*1.5,top*0.5)))
139    #f.write("""light_source {{ <300000+{0}, 1000000, -1000000+{1}>, rgb <1, 1, 1> fade_power 0 }}\n""".format(avg(left,right),avg(bottom,top)))
140    if Radiosity:
141        arealight_sun = ""
142        arealight_sky = ""
143        if options['hq']:
144            arealight_sun = """    area_light <100000, 0, 0>, <0, 0, 100000>, 3, 3
145    adaptive 1
146    circular
147"""
148            arealight_sky = """    area_light <1000000, 0, 0>, <0, 0, 1000000>, 3, 3
149    adaptive 1
150    circular
151"""
152        f.write("""
153/* The Sun */
154light_source {{
155    <0, 1000000,0>,
156    rgb <1, 1, 0.9>
157    {arealight}
158    rotate <45,10,0>
159    translate <{middle_x},0,{middle_y}>
160}}
161\n""".format(middle_x=middle_x,middle_y=middle_y,arealight=arealight_sun))
162        f.write("""
163/* Sky blue */
164light_source {{
165    <0, 1000000,0>,
166    rgb <0.1, 0.1, 0.2>
167    {arealight}
168    translate <{middle_x},0,{middle_y}>
169}}
170\n""".format(middle_x=middle_x,middle_y=middle_y,arealight=arealight_sky))
171    else:
172        arealight_sun = ""
173        arealight_sky = ""
174        if options['hq']:
175            arealight_sun = """    area_light <100000, 0, 0>, <0, 0, 100000>, 6, 6
176    adaptive 1
177    circular
178"""
179            arealight_sky = """    area_light <1000000, 0, 0>, <0, 0, 1000000>, 6, 6
180    adaptive 1
181    circular
182"""
183        f.write("""
184/* The Sun */
185light_source {{
186    <0, 1000000,0>,
187    rgb <1, 1, 0.9>
188    {arealight}
189    rotate <45,10,0>
190    translate <{middle_x},0,{middle_y}>
191}}
192\n""".format(middle_x=middle_x,middle_y=middle_y,arealight=arealight_sun))
193        f.write("""
194/* Sky blue */
195light_source {{
196    <0, 1000000,0>,
197    rgb <0.2, 0.2, 0.22>
198    {arealight}
199    translate <{middle_x},0,{middle_y}>
200}}
201\n""".format(middle_x=middle_x,middle_y=middle_y,arealight=arealight_sky))
202
203    left,bottom,right,top = scale_coords(left,bottom,right,top,1.1)
204    f.write("""
205#declare boundbox = box {{
206    <0, -1, 0>, <1, 1, 1>
207    scale <{sizex},50,{sizey}>
208    translate <{left},0,{bottom}>
209}}
210""".format(left=left,bottom=bottom,right=right,top=top,sizex=right-left,sizey=top-bottom))
211
212def main(options):
213    print "In main(). Options: ",options
214
215    bbox = options['bbox']
216    bbox = scale_bbox(bbox,gutter_factor)
217    options['bbox']=bbox
218
219    create_textures = not(options['quick'])
220    osraydb = OsrayDB(options)
221
222    f = open('/home/kayd/workspace/Parking/osray/scene-osray.pov', 'w')
223
224    pov_globals(f)
225    pov_camera(f,osraydb,options)
226   
227    if create_textures:
228        create_ground_texture(osraydb,options,'/home/kayd/workspace/Parking/osray/scene-osray-landuse-texture.pov')
229        #pass
230   
231    render_highways(f,osraydb,options)
232    render_buidings(f,osraydb,options)
233    render_furniture(f,osraydb,options)
234    f.close()
235   
236    command = 'povray'
237    image_parameter = '-Iscene-osray.pov'
238    image_dimension_parameters = "-W"+str(options['width'])+" -H"+str(options['height'])
239    if options['hq']==True:
240        antialiasing_parameters = '+A0.1 +AM2 +R3 -J'
241        misc_parameters = '+UV +Q9'
242    else:
243        antialiasing_parameters = '-A'
244        misc_parameters = '+UV +Q4'
245    commandline = string.join([command,image_parameter,image_dimension_parameters,antialiasing_parameters,misc_parameters])
246    result = commands.getstatusoutput(commandline)
247    print result[1]
248    osraydb.shutdown()
249
250if __name__ == "__main__":
251    parser = OptionParser()
252    parser.add_option("-d", "--dsn", dest="dsn", help="database name and host (e.g. 'dbname=gis')", default="dbname=gis")
253    parser.add_option("-p", "--prefix", dest="prefix", help="database table prefix (e.g. 'planet_osm')", default="planet_osm")
254    parser.add_option("-q", "--quick", action="store_true", dest="quick", default=False, help="don't generate textures, but use existing ones")
255    parser.add_option("-b", "--bbox", dest="bbox", help="the bounding box as string ('9.94861 49.79293,9.96912 49.80629')", default="9.94861 49.79293,9.96912 49.80629")
256    parser.add_option("-H", "--height", dest="height", help="image height in pixels", default="768", type='int')
257    parser.add_option("-W", "--width", dest="width", help="image width in pixels", default="1024", type='int')
258    parser.add_option("-Q", "--high-quality", action="store_true", dest="hq", default=False, help="high quality rendering")
259    parser.add_option("-R", "--radiosity", action="store_true", dest="radiosity", default=False, help="radiosity rendering")
260    (options, args) = parser.parse_args()
261    #if options.a and options.b:
262    #    parser.error("options -a and -b are mutually exclusive")
263    print options
264    main(options.__dict__)
265    sys.exit(0)
Note: See TracBrowser for help on using the repository browser.