|
| 1 | +"""Method to translate a Color Room/Face objects to a VisualizationSet.""" |
| 2 | +import math |
| 3 | + |
| 4 | +from ladybug_geometry.geometry3d import Vector3D, Plane |
| 5 | +from ladybug_display.geometry3d import DisplayLineSegment3D, DisplayText3D |
| 6 | +from ladybug_display.visualization import VisualizationSet, ContextGeometry, \ |
| 7 | + AnalysisGeometry, VisualizationData |
| 8 | + |
| 9 | +from honeybee.face import Face |
| 10 | + |
| 11 | + |
| 12 | +def color_room_to_vis_set(color_room, include_wireframe=True, text_labels=False): |
| 13 | + """Translate a Honeybee ColorRoom to a VisualizationSet. |
| 14 | +
|
| 15 | + Args: |
| 16 | + color_room: A Honeybee ColorRoom object to be converted to a VisualizationSet. |
| 17 | + include_wireframe: Boolean to note whether a ContextGeometry just for |
| 18 | + the Wireframe (in LineSegment3D) should be included. (Default: True). |
| 19 | + text_labels: A boolean to note whether the attribute assigned to the |
| 20 | + ColorRoom should be expressed as a colored AnalysisGeometry (False) |
| 21 | + or a ContextGeometry as text labels (True). (Default: False). |
| 22 | +
|
| 23 | + Returns: |
| 24 | + A VisualizationSet object that represents the ColorRoom with an |
| 25 | + AnalysisGeometry when text_labels is False or a ContextGeometry when |
| 26 | + text_labels is True. It can also optionally include a ContextGeometry |
| 27 | + for the wireframe. |
| 28 | + """ |
| 29 | + # set up an empty visualization set |
| 30 | + vis_set = VisualizationSet('Room_{}'.format(color_room.attr_name), ()) |
| 31 | + vis_set.display_name = 'Room {}'.format( |
| 32 | + color_room.attr_name_end.replace('_', ' ').title()) |
| 33 | + |
| 34 | + # use text labels if requested |
| 35 | + if text_labels: |
| 36 | + # set up default variables |
| 37 | + label_text = [] |
| 38 | + txt_height = None if color_room.legend_parameters.is_text_height_default \ |
| 39 | + else color_room.legend_parameters.text_height |
| 40 | + font = color_room.legend_parameters.font |
| 41 | + # loop through the rooms and create the text labels |
| 42 | + for room_prop, room in zip(color_room.attributes, color_room.rooms): |
| 43 | + cent_pt = room.geometry.center # base point for the text |
| 44 | + base_plane = Plane(Vector3D(0, 0, 1), cent_pt) |
| 45 | + if txt_height is None: # auto-calculate default text height |
| 46 | + txt_len = len(room_prop) if len(room_prop) > 10 else 10 |
| 47 | + txt_h = (room.geometry.max.x - room.geometry.min.x) / txt_len |
| 48 | + else: |
| 49 | + txt_h = txt_height |
| 50 | + label = DisplayText3D( |
| 51 | + room_prop, base_plane, txt_h, font=font, |
| 52 | + horizontal_alignment='Center', vertical_alignment='Middle') |
| 53 | + label_text.append(label) # append everything to the list |
| 54 | + con_geo = ContextGeometry(vis_set.identifier, label_text) |
| 55 | + con_geo.display_name = vis_set.display_name |
| 56 | + vis_set.add_geometry(con_geo) |
| 57 | + else: # use a colored AnalysisGeometry |
| 58 | + # produce a range of values from the collected attributes |
| 59 | + attr_dict = {i: val for i, val in enumerate(color_room.attributes_unique)} |
| 60 | + attr_dict_rev = {val: i for i, val in attr_dict.items()} |
| 61 | + values = tuple(attr_dict_rev[r_attr] for r_attr in color_room.attributes) |
| 62 | + # produce legend parameters with an ordinal dict for the attributes |
| 63 | + l_par = color_room.legend_parameters.duplicate() |
| 64 | + l_par.segment_count = len(color_room.attributes_unique) |
| 65 | + l_par.ordinal_dictionary = attr_dict |
| 66 | + if l_par.is_title_default: |
| 67 | + l_par.title = color_room.attr_name_end.replace('_', ' ').title() |
| 68 | + # create the analysis geometry |
| 69 | + vis_data = VisualizationData(values, l_par) |
| 70 | + geo = tuple(room.geometry for room in color_room.rooms) |
| 71 | + a_geo = AnalysisGeometry(vis_set.identifier, geo, [vis_data]) |
| 72 | + a_geo.display_name = vis_set.display_name |
| 73 | + vis_set.add_geometry(a_geo) |
| 74 | + |
| 75 | + # loop through all of the rooms and add their wire frames |
| 76 | + if include_wireframe: |
| 77 | + vis_set.add_geometry(_room_wireframe(color_room.rooms)) |
| 78 | + return vis_set |
| 79 | + |
| 80 | + |
| 81 | +def color_face_to_vis_set(color_face, include_wireframe=True, text_labels=False): |
| 82 | + """Translate a Honeybee ColorFace to a VisualizationSet. |
| 83 | +
|
| 84 | + Args: |
| 85 | + color_face: A Honeybee ColorFace object to be converted to a VisualizationSet. |
| 86 | + include_wireframe: Boolean to note whether a ContextGeometry just for |
| 87 | + the Wireframe (in LineSegment3D) should be included. (Default: True). |
| 88 | + text_labels: A boolean to note whether the attribute assigned to the |
| 89 | + ColorFace should be expressed as a colored AnalysisGeometry (False) |
| 90 | + or a ContextGeometry as text labels (True). (Default: False). |
| 91 | +
|
| 92 | + Returns: |
| 93 | + A VisualizationSet object that represents the ColorFace with an |
| 94 | + AnalysisGeometry when text_labels is False or a ContextGeometry when |
| 95 | + text_labels is True. It can also optionally include a ContextGeometry |
| 96 | + for the wireframe. |
| 97 | + """ |
| 98 | + # set up an empty visualization set |
| 99 | + vis_set = VisualizationSet('Face_{}'.format(color_face.attr_name), ()) |
| 100 | + vis_set.display_name = 'Face {}'.format( |
| 101 | + color_face.attr_name_end.replace('_', ' ').title()) |
| 102 | + |
| 103 | + # use text labels if requested |
| 104 | + if text_labels: |
| 105 | + # set up default variables |
| 106 | + label_text = [] |
| 107 | + txt_height = None if color_face.legend_parameters.is_text_height_default \ |
| 108 | + else color_face.legend_parameters.text_height |
| 109 | + font = color_face.legend_parameters.font |
| 110 | + # loop through the faces and create the text labels |
| 111 | + for face_prop, f_geo in zip(color_face.attributes, color_face.flat_geometry): |
| 112 | + if face_prop != 'N/A': |
| 113 | + cent_pt = f_geo.center # base point for the text |
| 114 | + base_plane = Plane(f_geo.normal, cent_pt) |
| 115 | + if base_plane.y.z < 0: # base plane pointing downwards; rotate it |
| 116 | + base_plane = base_plane.rotate(base_plane.n, math.pi, base_plane.o) |
| 117 | + if txt_height is None: # auto-calculate default text height |
| 118 | + txt_len = len(face_prop) if len(face_prop) > 10 else 10 |
| 119 | + largest_dim = max( |
| 120 | + (f_geo.max.x - f_geo.min.x), (f_geo.max.y - f_geo.min.y)) |
| 121 | + txt_h = largest_dim / (txt_len * 2) |
| 122 | + else: |
| 123 | + txt_h = txt_height |
| 124 | + # move base plane origin a little to avoid overlaps of adjacent labels |
| 125 | + if base_plane.n.x != 0: |
| 126 | + m_vec = base_plane.y if base_plane.n.x < 0 else -base_plane.y |
| 127 | + else: |
| 128 | + m_vec = base_plane.y if base_plane.n.z < 0 else -base_plane.y |
| 129 | + base_plane = base_plane.move(m_vec * txt_h) |
| 130 | + # create the text label |
| 131 | + label = DisplayText3D( |
| 132 | + face_prop, base_plane, txt_h, font=font, |
| 133 | + horizontal_alignment='Center', vertical_alignment='Middle') |
| 134 | + label_text.append(label) # append everything to the list |
| 135 | + con_geo = ContextGeometry(vis_set.identifier, label_text) |
| 136 | + con_geo.display_name = vis_set.display_name |
| 137 | + vis_set.add_geometry(con_geo) |
| 138 | + else: # use a colored AnalysisGeometry |
| 139 | + # produce a range of values from the collected attributes |
| 140 | + attr_dict = {i: val for i, val in enumerate(color_face.attributes_unique)} |
| 141 | + attr_dict_rev = {val: i for i, val in attr_dict.items()} |
| 142 | + values = tuple(attr_dict_rev[r_attr] for r_attr in color_face.attributes) |
| 143 | + # produce legend parameters with an ordinal dict for the attributes |
| 144 | + l_par = color_face.legend_parameters.duplicate() |
| 145 | + l_par.segment_count = len(color_face.attributes_unique) |
| 146 | + l_par.ordinal_dictionary = attr_dict |
| 147 | + if l_par.is_title_default: |
| 148 | + l_par.title = color_face.attr_name_end.replace('_', ' ').title() |
| 149 | + # create the analysis geometry |
| 150 | + vis_data = VisualizationData(values, l_par) |
| 151 | + a_geo = AnalysisGeometry( |
| 152 | + vis_set.identifier, color_face.flat_geometry, [vis_data]) |
| 153 | + a_geo.display_name = vis_set.display_name |
| 154 | + vis_set.add_geometry(a_geo) |
| 155 | + |
| 156 | + # loop through all of the rooms and add their wire frames |
| 157 | + if include_wireframe: |
| 158 | + vis_set.add_geometry(_face_wireframe(color_face.flat_faces)) |
| 159 | + return vis_set |
| 160 | + |
| 161 | + |
| 162 | +def _room_wireframe(rooms): |
| 163 | + """Process Rooms into a ContextGeometry for Wireframe.""" |
| 164 | + wireframe = [] |
| 165 | + for room in rooms: |
| 166 | + for face in room.faces: |
| 167 | + _process_wireframe(face, wireframe, 2) |
| 168 | + for ap in face._apertures: |
| 169 | + _process_wireframe(ap, wireframe) |
| 170 | + for dr in face._doors: |
| 171 | + _process_wireframe(dr, wireframe) |
| 172 | + for shade in room.shades: |
| 173 | + sh_geo = shade.geometry |
| 174 | + for seg in sh_geo.boundary_segments: |
| 175 | + wireframe.append(DisplayLineSegment3D(seg, line_width=1)) |
| 176 | + if sh_geo.has_holes: |
| 177 | + for hole in sh_geo.holes: |
| 178 | + for seg in sh_geo.boundary_segments: |
| 179 | + wireframe.append(DisplayLineSegment3D(seg, line_width=1)) |
| 180 | + con_geo = ContextGeometry('Wireframe', wireframe) |
| 181 | + return con_geo |
| 182 | + |
| 183 | + |
| 184 | +def _face_wireframe(faces): |
| 185 | + """Process Faces into a ContextGeometry for Wireframe.""" |
| 186 | + wireframe = [] |
| 187 | + for face in faces: |
| 188 | + lw = 2 if isinstance(face, Face) else 1 |
| 189 | + face3d = face.geometry |
| 190 | + for seg in face3d.boundary_segments: |
| 191 | + wireframe.append(DisplayLineSegment3D(seg, line_width=lw)) |
| 192 | + if face3d.has_holes: |
| 193 | + for hole in face3d.holes: |
| 194 | + for seg in face3d.boundary_segments: |
| 195 | + wireframe.append(DisplayLineSegment3D(seg, line_width=lw)) |
| 196 | + con_geo = ContextGeometry('Wireframe', wireframe) |
| 197 | + return con_geo |
| 198 | + |
| 199 | + |
| 200 | +def _process_wireframe(geo_obj, wireframe, line_width=1): |
| 201 | + """Process the boundary and holes of a Honeybee geometry into DisplayLinesegment3D. |
| 202 | + """ |
| 203 | + face3d = geo_obj.geometry |
| 204 | + for seg in face3d.boundary_segments: |
| 205 | + wireframe.append(DisplayLineSegment3D(seg, line_width=line_width)) |
| 206 | + if face3d.has_holes: |
| 207 | + for hole in face3d.holes: |
| 208 | + for seg in face3d.boundary_segments: |
| 209 | + wireframe.append(DisplayLineSegment3D(seg, line_width=line_width)) |
| 210 | + for shade in geo_obj.shades: |
| 211 | + sh_geo = shade.geometry |
| 212 | + for seg in sh_geo.boundary_segments: |
| 213 | + wireframe.append(DisplayLineSegment3D(seg, line_width=1)) |
| 214 | + if sh_geo.has_holes: |
| 215 | + for hole in sh_geo.holes: |
| 216 | + for seg in sh_geo.boundary_segments: |
| 217 | + wireframe.append(DisplayLineSegment3D(seg, line_width=1)) |
0 commit comments