source: subversion/applications/rendering/parking/osray/osray_streets.py @ 22312

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

implemented lanes:forward and lanes:backward

File size: 8.5 KB
Line 
1# -*- coding: utf-8 -*-
2# by kay - basic functions
3
4from osray_db import *
5from osray_geom import *
6
7Radiosity = True
8
9highwaytypes = { # highwaytype : [color,lane_width_factor]
10    'motorway':['<1,0,0>',1.3],
11    'motorway_link':['<1,0,0>',1.3],
12    'trunk':['<0.9,1,0.9>',1.1],
13    'trunk_link':['<0.9,1,0.9>',1.1],
14    'primary':['<1,1,0.9>',1.0],
15    'primary_link':['<1,1,0.9>',1.0],
16    'secondary':['<1,0.9,0.9>',1.0],
17    'secondary_link':['<1,0.9,0.9>',1.0],
18    'tertiary':['<1,0.9,0.8>',1.0],
19    'residential':['<0.9,0.9,0.9>',0.8],
20    'living_street':['<0.8,0.8,0.9>',0.8],
21    'unclassified':['<0.8,0.8,0.8>',0.8],
22    'service':['<0.8,0.8,0.8>',0.6],
23    'pedestrian':['<0.8,0.9,0.8>',0.8],
24    'footway':['<0.8,0.9,0.8>',0.3],
25    'steps':['<0.6,0.7,0.6>',0.3],
26    'cycleway':['<0.8,0.8,0.9>',0.3],
27    'track':['<0.7,0.7,0.6>',0.9],
28    'bus_stop':['<1,0,0>',0.9],
29    'bus_station':['<1,0,0>',0.9],
30    'road':['<1,0,0>',0.9],
31    'path':['<0.8,0.9,0.8>',0.5]
32    }
33
34
35def pov_declare_highway_textures(f):
36    if Radiosity:
37        declare_highway_texture = """
38#declare texture_highway_{highwaytype} =
39    texture {{
40        pigment {{
41            color rgb {color}
42        }}
43        finish {{
44            diffuse 0.8
45            ambient 0
46        }}
47    }}
48"""
49    else:
50        declare_highway_texture = """
51#declare texture_highway_{highwaytype} =
52    texture {{
53        pigment {{
54            color rgb {color}
55        }}
56        finish {{
57            specular 0.5
58            roughness 0.05
59            ambient 0.3
60            /*reflection 0.5*/
61        }}
62    }}
63"""
64    for highwaytype in highwaytypes.keys():
65        highwayparams = highwaytypes.get(highwaytype)
66        color = highwayparams[0]
67        f.write(declare_highway_texture.format(color=color,highwaytype=highwaytype))
68    # texture for the highway casing
69    f.write(declare_highway_texture.format(color='<0.4,0.4,0.4>',highwaytype='casing'))
70
71
72def draw_lines(f,points,height,streetwidth,highwaytype):
73    numpoints = len(points)
74    f.write("object {")
75    f.write("union {")
76    #f.write("sphere_sweep {{ linear_spline, {0},\n".format(numpoints+0))
77
78    latlon_prev = None
79    for i,point in enumerate(points):
80        latlon = point.split(' ')
81        f.write(" sphere {{ <{x}, 0, {y}>,{d} }}\n".format(x=latlon[0],y=latlon[1],d=streetwidth))
82        if(latlon_prev != None):
83            f.write(" cylinder {{ <{x1}, 0, {y1}>,<{x2}, 0, {y2}>,{d} }}\n".format(x1=latlon[0],y1=latlon[1],x2=latlon_prev[0],y2=latlon_prev[1],d=streetwidth))
84        latlon_prev = latlon
85    f.write("}") # union ends
86    f.write("scale <1, 0.05, 1>")
87    f.write("translate <0, {z}, 0>".format(z=height))
88    #f.write("  } ") #end of intersection
89    f.write("""texture {{ texture_highway_{highwaytype} }}
90}}
91\n""".format(highwaytype=highwaytype))
92   
93def draw_lines_sweep(f,points,height,streetwidth,highwaytype):
94    numpoints = len(points)
95    #f.write("intersection { object { boundbox } ")
96    f.write("sphere_sweep {{ linear_spline, {0},\n".format(numpoints+0))
97
98    for i,point in enumerate(points):
99        latlon = point.split(' ')
100        #if (i==0):
101        #    f.write("  <{0}, {3}, {1}>,{2}\n".format(latlon[0],latlon[1],streetwidth,height))
102        f.write("  <{x}, 0, {y}>,{d}\n".format(x=latlon[0],y=latlon[1],d=streetwidth))
103        #if (i==numpoints-1):
104        #   f.write("  <{0}, {3}, {1}>,{2}\n".format(latlon[0],latlon[1],streetwidth,height))
105
106    #print highwayparams[0],highwayparams[1],streetwidth
107    f.write("  tolerance 1")
108    f.write("  scale <1, 0.05, 1>")
109    f.write("  translate <0, {z}, 0>".format(z=height))
110    #f.write("  } ") #end of intersection
111    f.write("""
112    texture {{ texture_highway_{highwaytype} }}
113}}
114\n""".format(highwaytype=highwaytype))
115
116def calculate_lanefactor(lanes,lanesfw,lanesbw,oneway):
117    if lanes==None:#
118        # no lanes specified: look if lanes:forward is specified
119        if lanesfw!=None and lanesbw!=None:
120            return parse_float_safely(lanesfw,1.0) + parse_float_safely(lanesbw,1.0) 
121        # none specified: use defaults
122        lanefactor = 2.0 # 2 lanes seems to be default
123        if parse_yes_no_safely(oneway,False)==True:
124            lanefactor = 1.2 # except for one-ways (make it a bit wider than 2 lanes/2)
125        return lanefactor
126    return parse_float_safely(lanes,2.0)
127
128def pov_highway(f,highway):
129    f.write("/* osm_id={0} */\n".format(highway['osm_id']))
130    highwaytype = highway['highway']
131    #highwaytype = 'secondary'
132    highwayparams = highwaytypes.get(highwaytype)
133    linestring = highway['way']
134    linestring = linestring[11:] # cut off the "LINESTRING("
135    linestring = linestring[:-1] # cut off the ")"
136    points = linestring.split(',')
137
138#    oneway = False
139#    if(highway['oneway']=='yes'):
140#        oneway=True
141
142    lanefactor = calculate_lanefactor(highway['lanes'],highway['lanesfw'],highway['lanesbw'],highway['oneway'])
143    lanewidth = 2.5 # m
144    streetwidth = lanewidth * lanefactor
145
146    layer = parse_int_safely(highway['layer'],default=0)
147    if layer<0:
148        layer=0 # FIXME
149    layerheight = 4.0*layer # 4 m per layer
150# draw road
151    draw_lines(f,points,layerheight,streetwidth*highwayparams[1],highwaytype)
152# draw casing
153    draw_lines(f,points,layerheight-0.05*lanefactor,1.2*streetwidth*highwayparams[1],'casing')
154
155def pov_highway_area(f,highway):
156    highwaytype = highway[1]
157    #highwaytype = 'secondary'
158    highwayparams = highwaytypes.get(highwaytype)
159
160    linestring = highway[2]
161    linestring = linestring[8:] # cut off the "POLYGON("
162    linestring = linestring[:-1] # cut off the ")"
163
164    heightstring = highway[3]
165    #print 'hs="{0}"'.format(heightstring)
166    height = 0.1
167
168    amenity = highway[4]
169    #print amenity
170   
171    points = linestring.split(',')#.strip('(').strip(')')
172    #print points
173
174    numpoints = len(points)
175    #f.write("polygon {{ {0},\n".format(numpoints))
176    f.write("prism {{ linear_spline 0, 0.01, {0},\n".format(numpoints))
177    f.write("/* osm_id={0} */\n".format(highway[0]))
178
179    for i,point in enumerate(points):
180        latlon = point.split(' ')
181        if (i==0):
182            firstpoint="<{0}, {1}>\n".format(latlon[0],latlon[1])
183        if (i!=numpoints-1):
184            f.write("  <{0}, {1}>,\n".format(latlon[0].strip('(').strip(')'),latlon[1].strip('(').strip(')')))
185        else:
186            f.write("  <{0}, {1}>\n".format(latlon[0].strip('(').strip(')'),latlon[1].strip('(').strip(')')))
187    #f.write(firstpoint)
188
189    color = highwayparams[0]
190    #color = "<0,1,0>"
191
192    #if height != 10.0:
193        #print 'height:', height
194        #color = '<0,1,0>'
195    f.write("""
196    texture {{ texture_highway_{highwaytype} }}
197    translate <0, {height}, 0>
198}}
199\n""".format(highwaytype=highwaytype,height=height))
200#    f.write("""
201#    texture {{
202#        pigment {{
203#            color rgb {0}
204#        }}
205#        finish {{
206#            specular 0.5
207#            roughness 0.05
208#            ambient 0.2
209#            /*reflection 0.5*/
210#        }}
211#    }}
212#    translate <0, {1}, 0>
213#}}
214#\n""".format(color,height))
215    #
216    # draw the casing
217    #
218    height = 0.08
219    linestring = highway[5]
220    linestring = linestring[8:] # cut off the "POLYGON("
221    linestring = linestring[:-1] # cut off the ")"
222    points = linestring.split(',')#.strip('(').strip(')')
223    numpoints = len(points)
224    f.write("prism {{ linear_spline 0, 0.01, {0},\n".format(numpoints))
225    f.write("/* casing of osm_id={0} */\n".format(highway[0]))
226
227    for i,point in enumerate(points):
228        latlon = point.split(' ')
229        if (i==0):
230            firstpoint="<{0}, {1}>\n".format(latlon[0],latlon[1])
231        if (i!=numpoints-1):
232            f.write("  <{0}, {1}>,\n".format(latlon[0].strip('(').strip(')'),latlon[1].strip('(').strip(')')))
233        else:
234            f.write("  <{0}, {1}>\n".format(latlon[0].strip('(').strip(')'),latlon[1].strip('(').strip(')')))
235
236    color = highwayparams[0]
237    f.write("""
238    texture {{
239        pigment {{
240            color rgb <0.3,0.3,0.3>
241        }}
242        finish {{
243            specular 0.05
244            roughness 0.05
245            /*reflection 0.5*/
246        }}
247    }}
248    translate <0, {height}, 0>
249}}
250\n""".format(height=height))
251
252
253def render_highways(f,osraydb,options):
254    Radiosity = options['radiosity']
255    pov_declare_highway_textures(f)
256    highways = []
257    for highwaytype in highwaytypes.iterkeys():
258        highways += osraydb.select_highways(highwaytype)
259
260    for highway in highways:
261        pov_highway(f,highway)
262        #pass
263   
264    highway_areas = []
265    for highwaytype in highwaytypes.iterkeys():
266        highway_areas += osraydb.select_highway_areas(highwaytype)
267   
268    for highway in highway_areas:
269        pov_highway_area(f,highway)
Note: See TracBrowser for help on using the repository browser.