SceneObjects
1# coding: utf-8 2import os 3from Element import Element 4import shutil 5from LSBoundingBox import LSBoundingBox 6from SDKUtility import OBJHelper 7import sys 8from collections import OrderedDict 9 10 11class SceneObject(Element): 12 def __init__(self, name, components=None): 13 super().__init__() 14 self.scene_obj_name = name 15 self.scene_obj_components = OrderedDict() 16 if components is not None: 17 self.scene_obj_components = components 18 19 def set_scene_obj_name(self, name): 20 self.scene_obj_name = name 21 22 def get_scene_obj_name(self): 23 return self.scene_obj_name 24 25 def set_component_op(self, comp_name, op_name): 26 self.scene_obj_components[comp_name]["op_name"] = op_name 27 28 def add_component_from_file(self, obj_path, op_name, temperature="-", color="0x006400ff", 29 is_turbid=False, leaf_density=-999.0, lad="-", hotspot_factor=-999.0): 30 # get filename 31 (dir_path, file_name) = os.path.split(obj_path) 32 component_name = self.scene_obj_name + "_" + file_name 33 if file_name not in self.scene_obj_components: 34 self.scene_obj_components[component_name] = {"op_name": op_name, "temperature": temperature, "color": color, 35 "is_turbid": is_turbid, "leaf_density": leaf_density, 36 "lad": lad, "hotspot_factor": hotspot_factor, 37 "obj_file_path": obj_path} 38 else: 39 print("Warning: Component " + component_name + "already exists.") 40 41 @staticmethod 42 def read_components(obj_path): 43 pass 44 45 46class SceneObjects(Element): 47 def __init__(self): 48 super().__init__() 49 self.cache_OBJ_file = True 50 self.ConvertOBJToBinaryFile = True 51 self.tree_pos_file = "instances.txt" 52 self.objects_file = "objects.txt" 53 54 # object and instances data 55 self._objects = {} 56 self._instances = {} 57 58 def init_objects_from_json(self, json_object): 59 self.cache_OBJ_file = json_object["scene"]["forest"]["CacheOBJFile"] 60 self.tree_pos_file = json_object["scene"]["forest"]["tree_pos_file"] 61 self.objects_file = json_object["scene"]["forest"]["objects_file"] 62 if "ConvertOBJToBinaryFile" in json_object["scene"]["forest"]: 63 self.ConvertOBJToBinaryFile = json_object["scene"]["forest"]["ConvertOBJToBinaryFile"] 64 # objects and instances 65 self.load_objects() 66 self.load_instances() 67 return self 68 69 def get_objects(self): 70 return self._objects 71 72 def get_instances(self): 73 return self._instances 74 75 def to_json_object(self): 76 json_obj = {"CacheOBJFile": self.cache_OBJ_file, 77 "ConvertOBJToBinaryFile":self.ConvertOBJToBinaryFile, 78 "tree_pos_file": self.tree_pos_file, 79 "objects_file": self.objects_file} 80 81 # save objects and instances 82 self.save_objects() 83 self.save_instances() 84 return json_obj 85 86 def load_objects(self): 87 objects_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects.txt") 88 if os.path.exists(objects_file_path): 89 f = open(objects_file_path) 90 for line in f: 91 arr = line.rstrip().split(" ") 92 components = {} 93 for i in range(1, len(arr), 8): 94 components[arr[i]] = {"op_name": arr[i+1], "temperature": arr[i+2], "color": arr[i+3], 95 "is_turbid": True if arr[i+4] == "True" or arr[i+4] == "true" else False, 96 "leaf_density": float(arr[i+5]), 97 "lad": arr[i+6], "hotspot_factor": float(arr[i+7]), 98 } 99 self._objects[arr[0]] = components 100 f.close() 101 102 def load_instances(self): 103 instances_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "instances.txt") 104 if os.path.exists(instances_file_path): 105 f = open(instances_file_path) 106 for line in f: 107 arr = line.strip().split(" ") 108 obj_name = arr[0] 109 coord = list(map(lambda x: float(x), arr[1:])) 110 if obj_name in self._instances: 111 self._instances[obj_name].append(coord) 112 else: 113 self._instances[obj_name] = [coord] 114 f.close() 115 116 def save_objects(self): 117 objects_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects.txt") 118 f = open(objects_file_path, "w") 119 for obj_name in self._objects: 120 out_str = obj_name 121 for comp_name in self._objects[obj_name]: 122 out_str += " " + comp_name 123 out_str += " " + self._objects[obj_name][comp_name]["op_name"] 124 out_str += " " + self._objects[obj_name][comp_name]["temperature"] 125 out_str += " " + self._objects[obj_name][comp_name]["color"] 126 out_str += " " + str(self._objects[obj_name][comp_name]["is_turbid"]) 127 out_str += " " + str(self._objects[obj_name][comp_name]["leaf_density"]) 128 out_str += " " + self._objects[obj_name][comp_name]["lad"] 129 out_str += " " + str(self._objects[obj_name][comp_name]["hotspot_factor"]) 130 out_str += "\n" 131 f.write(out_str) 132 f.close() 133 134 def save_instances(self): 135 instances_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "instances.txt") 136 if len(self._instances) != 0: 137 f = open(instances_file_path, "w") 138 for obj_name in self._instances: 139 for coord in self._instances[obj_name]: 140 out_str = obj_name 141 out_str += " " + " ".join(list(map(lambda x: str(x), coord))) + "\n" 142 f.write(out_str) 143 f.close() 144 else: 145 if os.path.exists(instances_file_path): 146 os.remove(instances_file_path) 147 148 def __is_scene_object_valid(self, scene_object: SceneObject): 149 # Check components 150 if len(scene_object.scene_obj_components) == 0: 151 print("Error: scene object " + scene_object.get_scene_obj_name() + "does not have components.") 152 return False 153 # Check optical property 154 for component_name in scene_object.scene_obj_components: 155 op_name = scene_object.scene_obj_components[component_name]["op_name"] 156 if not self.get_sim().get_scene().get_landscape().get_optical_properties().is_op_exist(op_name): 157 print("Error: optical property " + op_name + " does not exists, please define first.") 158 return False 159 return True 160 161 def __is_scene_object_exist(self, obj_name): 162 if obj_name in self._objects: 163 return True 164 return False 165 166 def __copy_components(self, components, override_file=True): 167 for comp_name in components: 168 obj_file_path = components[comp_name]["obj_file_path"] 169 dst_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 170 if os.path.exists(dst_file_path): 171 if override_file: 172 shutil.copy(obj_file_path, dst_file_path) 173 else: 174 shutil.copy(obj_file_path, dst_file_path) 175 176 def __copy_components_with_translate(self, components, translate_to_origin, override_file=True): 177 obj_bound_box = LSBoundingBox() 178 for comp_name in components: 179 obj_file_path = components[comp_name]["obj_file_path"] 180 bound_box = OBJHelper.get_obj_bound(obj_file_path) 181 obj_bound_box.add_child(bound_box) 182 offsetx = - 0.5 * (obj_bound_box.minX + obj_bound_box.maxX) 183 offsey = - obj_bound_box.minY 184 offsetz = -0.5 * (obj_bound_box.minZ + obj_bound_box.maxZ) 185 if translate_to_origin == "xy": 186 offsey = 0 187 for comp_name in components: 188 obj_file_path = components[comp_name]["obj_file_path"] 189 dst_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 190 write_file = False 191 if os.path.exists(dst_file_path): 192 if override_file: 193 write_file = True 194 else: 195 write_file = True 196 197 if write_file: 198 fout = open(dst_file_path, "w") 199 fin = open(obj_file_path) 200 for line in fin: 201 if line.startswith("v"): 202 (x, y, z) = list(map(lambda xx: float(xx), line[1:].strip().split(" "))) 203 newx, newy, new = x+offsetx, y+offsey, z+offsetz 204 fout.write("v %f %f %f\n" % (newx, newy, new)) 205 else: 206 fout.write(line) 207 fin.close() 208 fout.close() 209 210 # add a object 211 def add_object(self, scene_object: SceneObject, override_file=True, translate_to_origin="no"): 212 obj_name = scene_object.get_scene_obj_name() 213 # check if scene object is valid 214 if self.__is_scene_object_valid(scene_object): 215 if obj_name in self._objects: 216 print("Warning: scene object " + obj_name + " already exists.") 217 else: 218 self._objects[obj_name] = scene_object.scene_obj_components 219 if translate_to_origin == "no": 220 self.__copy_components(scene_object.scene_obj_components) 221 else: 222 print("INFO: Translating obj...") 223 self.__copy_components_with_translate(scene_object.scene_obj_components, 224 translate_to_origin) 225 226 def get_object(self, obj_name): 227 if obj_name not in self._objects: 228 print("Warning: scene object " + obj_name + " does not exist.") 229 sys.exit(0) 230 else: 231 return SceneObject(obj_name, self._objects[obj_name]) 232 233 def place_object_to(self, obj_name, x=50.0, y=50.0, z=0.0, rotate=0.0, 234 rotate_axis_x=None, rotate_axis_y=None, rotate_axis_z=None, 235 scale_extent_x=None, scale_extent_y=None, scale_extent_z=None): 236 if not self.__is_scene_object_exist(obj_name): 237 print("Error: scene object " + obj_name + " doest not exist.") 238 else: 239 rotate_factors = [rotate_axis_x, rotate_axis_y, rotate_axis_z] 240 scale_factors = [scale_extent_x, scale_extent_y, scale_extent_z] 241 has_rotation = all(item is not None for item in rotate_factors) 242 has_scale = all(item is not None for item in scale_factors) 243 if not has_rotation: 244 coord = [x, y, z, rotate] 245 elif has_rotation and (not has_scale): 246 coord = [x, y, z, rotate] + rotate_factors 247 elif has_rotation and has_scale: 248 coord = [x, y, z, rotate] + rotate_factors + scale_factors 249 else: 250 coord = [x, y, z, rotate] 251 if obj_name in self._instances: 252 self._instances[obj_name].append(coord) 253 else: 254 self._instances[obj_name] = [coord] 255 256 def read_object_bounding_box(self): 257 boundingbox_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects_boundingbox.txt") 258 if not os.path.exists(boundingbox_file_path): 259 return None 260 object_bounds = {} 261 f = open(boundingbox_file_path) 262 for line in f: 263 arr = line.split(":") 264 obj_name = arr[0] 265 coords = list(map(lambda x: float(x), arr[1].strip().split(" "))) 266 num_boundings = int(len(coords)/6) 267 obj_bound_box = LSBoundingBox() 268 for i in range(num_boundings): 269 comp_bound_box = LSBoundingBox() 270 comp_bound_box.set_value(coords[i*6: (i+1)*6]) 271 obj_bound_box.add_child(comp_bound_box) 272 object_bounds[obj_name] = obj_bound_box 273 return object_bounds 274 275 276 # For open the project with LESS GUI, this function must be executed before 277 def calculate_object_bounding_box(self): 278 if len(self._objects) > 0: 279 boundingbox_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects_boundingbox.txt") 280 fo = open(boundingbox_file_path, "w") 281 for obj_name in self._objects: 282 obj_bound_box = LSBoundingBox() 283 for comp_name in self._objects[obj_name]: 284 comp_obj_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 285 comp_bound_box = LSBoundingBox() 286 f = open(comp_obj_file_path) 287 for line in f: 288 if line.startswith("v "): 289 arr = line.strip().split(" ") 290 (x, y, z) = list(map(lambda xx: float(xx), arr[1:])) 291 if x < comp_bound_box.minX: 292 comp_bound_box.minX = x 293 if y < comp_bound_box.minY: 294 comp_bound_box.minY = y 295 if z < comp_bound_box.minZ: 296 comp_bound_box.minZ = z 297 if x > comp_bound_box.maxX: 298 comp_bound_box.maxX = x 299 if y > comp_bound_box.maxY: 300 comp_bound_box.maxY = y 301 if z > comp_bound_box.maxZ: 302 comp_bound_box.maxZ = z 303 f.close() 304 obj_bound_box.add_child(comp_bound_box) 305 fo.write(obj_name + ":" + obj_bound_box.to_string()+"\n") 306 fo.close()
class
SceneObject(Element.Element):
12class SceneObject(Element): 13 def __init__(self, name, components=None): 14 super().__init__() 15 self.scene_obj_name = name 16 self.scene_obj_components = OrderedDict() 17 if components is not None: 18 self.scene_obj_components = components 19 20 def set_scene_obj_name(self, name): 21 self.scene_obj_name = name 22 23 def get_scene_obj_name(self): 24 return self.scene_obj_name 25 26 def set_component_op(self, comp_name, op_name): 27 self.scene_obj_components[comp_name]["op_name"] = op_name 28 29 def add_component_from_file(self, obj_path, op_name, temperature="-", color="0x006400ff", 30 is_turbid=False, leaf_density=-999.0, lad="-", hotspot_factor=-999.0): 31 # get filename 32 (dir_path, file_name) = os.path.split(obj_path) 33 component_name = self.scene_obj_name + "_" + file_name 34 if file_name not in self.scene_obj_components: 35 self.scene_obj_components[component_name] = {"op_name": op_name, "temperature": temperature, "color": color, 36 "is_turbid": is_turbid, "leaf_density": leaf_density, 37 "lad": lad, "hotspot_factor": hotspot_factor, 38 "obj_file_path": obj_path} 39 else: 40 print("Warning: Component " + component_name + "already exists.") 41 42 @staticmethod 43 def read_components(obj_path): 44 pass
def
add_component_from_file( self, obj_path, op_name, temperature='-', color='0x006400ff', is_turbid=False, leaf_density=-999.0, lad='-', hotspot_factor=-999.0):
29 def add_component_from_file(self, obj_path, op_name, temperature="-", color="0x006400ff", 30 is_turbid=False, leaf_density=-999.0, lad="-", hotspot_factor=-999.0): 31 # get filename 32 (dir_path, file_name) = os.path.split(obj_path) 33 component_name = self.scene_obj_name + "_" + file_name 34 if file_name not in self.scene_obj_components: 35 self.scene_obj_components[component_name] = {"op_name": op_name, "temperature": temperature, "color": color, 36 "is_turbid": is_turbid, "leaf_density": leaf_density, 37 "lad": lad, "hotspot_factor": hotspot_factor, 38 "obj_file_path": obj_path} 39 else: 40 print("Warning: Component " + component_name + "already exists.")
Inherited Members
- Element.Element
- set_sim
- get_sim
class
SceneObjects(Element.Element):
47class SceneObjects(Element): 48 def __init__(self): 49 super().__init__() 50 self.cache_OBJ_file = True 51 self.ConvertOBJToBinaryFile = True 52 self.tree_pos_file = "instances.txt" 53 self.objects_file = "objects.txt" 54 55 # object and instances data 56 self._objects = {} 57 self._instances = {} 58 59 def init_objects_from_json(self, json_object): 60 self.cache_OBJ_file = json_object["scene"]["forest"]["CacheOBJFile"] 61 self.tree_pos_file = json_object["scene"]["forest"]["tree_pos_file"] 62 self.objects_file = json_object["scene"]["forest"]["objects_file"] 63 if "ConvertOBJToBinaryFile" in json_object["scene"]["forest"]: 64 self.ConvertOBJToBinaryFile = json_object["scene"]["forest"]["ConvertOBJToBinaryFile"] 65 # objects and instances 66 self.load_objects() 67 self.load_instances() 68 return self 69 70 def get_objects(self): 71 return self._objects 72 73 def get_instances(self): 74 return self._instances 75 76 def to_json_object(self): 77 json_obj = {"CacheOBJFile": self.cache_OBJ_file, 78 "ConvertOBJToBinaryFile":self.ConvertOBJToBinaryFile, 79 "tree_pos_file": self.tree_pos_file, 80 "objects_file": self.objects_file} 81 82 # save objects and instances 83 self.save_objects() 84 self.save_instances() 85 return json_obj 86 87 def load_objects(self): 88 objects_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects.txt") 89 if os.path.exists(objects_file_path): 90 f = open(objects_file_path) 91 for line in f: 92 arr = line.rstrip().split(" ") 93 components = {} 94 for i in range(1, len(arr), 8): 95 components[arr[i]] = {"op_name": arr[i+1], "temperature": arr[i+2], "color": arr[i+3], 96 "is_turbid": True if arr[i+4] == "True" or arr[i+4] == "true" else False, 97 "leaf_density": float(arr[i+5]), 98 "lad": arr[i+6], "hotspot_factor": float(arr[i+7]), 99 } 100 self._objects[arr[0]] = components 101 f.close() 102 103 def load_instances(self): 104 instances_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "instances.txt") 105 if os.path.exists(instances_file_path): 106 f = open(instances_file_path) 107 for line in f: 108 arr = line.strip().split(" ") 109 obj_name = arr[0] 110 coord = list(map(lambda x: float(x), arr[1:])) 111 if obj_name in self._instances: 112 self._instances[obj_name].append(coord) 113 else: 114 self._instances[obj_name] = [coord] 115 f.close() 116 117 def save_objects(self): 118 objects_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects.txt") 119 f = open(objects_file_path, "w") 120 for obj_name in self._objects: 121 out_str = obj_name 122 for comp_name in self._objects[obj_name]: 123 out_str += " " + comp_name 124 out_str += " " + self._objects[obj_name][comp_name]["op_name"] 125 out_str += " " + self._objects[obj_name][comp_name]["temperature"] 126 out_str += " " + self._objects[obj_name][comp_name]["color"] 127 out_str += " " + str(self._objects[obj_name][comp_name]["is_turbid"]) 128 out_str += " " + str(self._objects[obj_name][comp_name]["leaf_density"]) 129 out_str += " " + self._objects[obj_name][comp_name]["lad"] 130 out_str += " " + str(self._objects[obj_name][comp_name]["hotspot_factor"]) 131 out_str += "\n" 132 f.write(out_str) 133 f.close() 134 135 def save_instances(self): 136 instances_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "instances.txt") 137 if len(self._instances) != 0: 138 f = open(instances_file_path, "w") 139 for obj_name in self._instances: 140 for coord in self._instances[obj_name]: 141 out_str = obj_name 142 out_str += " " + " ".join(list(map(lambda x: str(x), coord))) + "\n" 143 f.write(out_str) 144 f.close() 145 else: 146 if os.path.exists(instances_file_path): 147 os.remove(instances_file_path) 148 149 def __is_scene_object_valid(self, scene_object: SceneObject): 150 # Check components 151 if len(scene_object.scene_obj_components) == 0: 152 print("Error: scene object " + scene_object.get_scene_obj_name() + "does not have components.") 153 return False 154 # Check optical property 155 for component_name in scene_object.scene_obj_components: 156 op_name = scene_object.scene_obj_components[component_name]["op_name"] 157 if not self.get_sim().get_scene().get_landscape().get_optical_properties().is_op_exist(op_name): 158 print("Error: optical property " + op_name + " does not exists, please define first.") 159 return False 160 return True 161 162 def __is_scene_object_exist(self, obj_name): 163 if obj_name in self._objects: 164 return True 165 return False 166 167 def __copy_components(self, components, override_file=True): 168 for comp_name in components: 169 obj_file_path = components[comp_name]["obj_file_path"] 170 dst_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 171 if os.path.exists(dst_file_path): 172 if override_file: 173 shutil.copy(obj_file_path, dst_file_path) 174 else: 175 shutil.copy(obj_file_path, dst_file_path) 176 177 def __copy_components_with_translate(self, components, translate_to_origin, override_file=True): 178 obj_bound_box = LSBoundingBox() 179 for comp_name in components: 180 obj_file_path = components[comp_name]["obj_file_path"] 181 bound_box = OBJHelper.get_obj_bound(obj_file_path) 182 obj_bound_box.add_child(bound_box) 183 offsetx = - 0.5 * (obj_bound_box.minX + obj_bound_box.maxX) 184 offsey = - obj_bound_box.minY 185 offsetz = -0.5 * (obj_bound_box.minZ + obj_bound_box.maxZ) 186 if translate_to_origin == "xy": 187 offsey = 0 188 for comp_name in components: 189 obj_file_path = components[comp_name]["obj_file_path"] 190 dst_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 191 write_file = False 192 if os.path.exists(dst_file_path): 193 if override_file: 194 write_file = True 195 else: 196 write_file = True 197 198 if write_file: 199 fout = open(dst_file_path, "w") 200 fin = open(obj_file_path) 201 for line in fin: 202 if line.startswith("v"): 203 (x, y, z) = list(map(lambda xx: float(xx), line[1:].strip().split(" "))) 204 newx, newy, new = x+offsetx, y+offsey, z+offsetz 205 fout.write("v %f %f %f\n" % (newx, newy, new)) 206 else: 207 fout.write(line) 208 fin.close() 209 fout.close() 210 211 # add a object 212 def add_object(self, scene_object: SceneObject, override_file=True, translate_to_origin="no"): 213 obj_name = scene_object.get_scene_obj_name() 214 # check if scene object is valid 215 if self.__is_scene_object_valid(scene_object): 216 if obj_name in self._objects: 217 print("Warning: scene object " + obj_name + " already exists.") 218 else: 219 self._objects[obj_name] = scene_object.scene_obj_components 220 if translate_to_origin == "no": 221 self.__copy_components(scene_object.scene_obj_components) 222 else: 223 print("INFO: Translating obj...") 224 self.__copy_components_with_translate(scene_object.scene_obj_components, 225 translate_to_origin) 226 227 def get_object(self, obj_name): 228 if obj_name not in self._objects: 229 print("Warning: scene object " + obj_name + " does not exist.") 230 sys.exit(0) 231 else: 232 return SceneObject(obj_name, self._objects[obj_name]) 233 234 def place_object_to(self, obj_name, x=50.0, y=50.0, z=0.0, rotate=0.0, 235 rotate_axis_x=None, rotate_axis_y=None, rotate_axis_z=None, 236 scale_extent_x=None, scale_extent_y=None, scale_extent_z=None): 237 if not self.__is_scene_object_exist(obj_name): 238 print("Error: scene object " + obj_name + " doest not exist.") 239 else: 240 rotate_factors = [rotate_axis_x, rotate_axis_y, rotate_axis_z] 241 scale_factors = [scale_extent_x, scale_extent_y, scale_extent_z] 242 has_rotation = all(item is not None for item in rotate_factors) 243 has_scale = all(item is not None for item in scale_factors) 244 if not has_rotation: 245 coord = [x, y, z, rotate] 246 elif has_rotation and (not has_scale): 247 coord = [x, y, z, rotate] + rotate_factors 248 elif has_rotation and has_scale: 249 coord = [x, y, z, rotate] + rotate_factors + scale_factors 250 else: 251 coord = [x, y, z, rotate] 252 if obj_name in self._instances: 253 self._instances[obj_name].append(coord) 254 else: 255 self._instances[obj_name] = [coord] 256 257 def read_object_bounding_box(self): 258 boundingbox_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects_boundingbox.txt") 259 if not os.path.exists(boundingbox_file_path): 260 return None 261 object_bounds = {} 262 f = open(boundingbox_file_path) 263 for line in f: 264 arr = line.split(":") 265 obj_name = arr[0] 266 coords = list(map(lambda x: float(x), arr[1].strip().split(" "))) 267 num_boundings = int(len(coords)/6) 268 obj_bound_box = LSBoundingBox() 269 for i in range(num_boundings): 270 comp_bound_box = LSBoundingBox() 271 comp_bound_box.set_value(coords[i*6: (i+1)*6]) 272 obj_bound_box.add_child(comp_bound_box) 273 object_bounds[obj_name] = obj_bound_box 274 return object_bounds 275 276 277 # For open the project with LESS GUI, this function must be executed before 278 def calculate_object_bounding_box(self): 279 if len(self._objects) > 0: 280 boundingbox_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects_boundingbox.txt") 281 fo = open(boundingbox_file_path, "w") 282 for obj_name in self._objects: 283 obj_bound_box = LSBoundingBox() 284 for comp_name in self._objects[obj_name]: 285 comp_obj_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 286 comp_bound_box = LSBoundingBox() 287 f = open(comp_obj_file_path) 288 for line in f: 289 if line.startswith("v "): 290 arr = line.strip().split(" ") 291 (x, y, z) = list(map(lambda xx: float(xx), arr[1:])) 292 if x < comp_bound_box.minX: 293 comp_bound_box.minX = x 294 if y < comp_bound_box.minY: 295 comp_bound_box.minY = y 296 if z < comp_bound_box.minZ: 297 comp_bound_box.minZ = z 298 if x > comp_bound_box.maxX: 299 comp_bound_box.maxX = x 300 if y > comp_bound_box.maxY: 301 comp_bound_box.maxY = y 302 if z > comp_bound_box.maxZ: 303 comp_bound_box.maxZ = z 304 f.close() 305 obj_bound_box.add_child(comp_bound_box) 306 fo.write(obj_name + ":" + obj_bound_box.to_string()+"\n") 307 fo.close()
def
init_objects_from_json(self, json_object):
59 def init_objects_from_json(self, json_object): 60 self.cache_OBJ_file = json_object["scene"]["forest"]["CacheOBJFile"] 61 self.tree_pos_file = json_object["scene"]["forest"]["tree_pos_file"] 62 self.objects_file = json_object["scene"]["forest"]["objects_file"] 63 if "ConvertOBJToBinaryFile" in json_object["scene"]["forest"]: 64 self.ConvertOBJToBinaryFile = json_object["scene"]["forest"]["ConvertOBJToBinaryFile"] 65 # objects and instances 66 self.load_objects() 67 self.load_instances() 68 return self
def
to_json_object(self):
76 def to_json_object(self): 77 json_obj = {"CacheOBJFile": self.cache_OBJ_file, 78 "ConvertOBJToBinaryFile":self.ConvertOBJToBinaryFile, 79 "tree_pos_file": self.tree_pos_file, 80 "objects_file": self.objects_file} 81 82 # save objects and instances 83 self.save_objects() 84 self.save_instances() 85 return json_obj
def
load_objects(self):
87 def load_objects(self): 88 objects_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects.txt") 89 if os.path.exists(objects_file_path): 90 f = open(objects_file_path) 91 for line in f: 92 arr = line.rstrip().split(" ") 93 components = {} 94 for i in range(1, len(arr), 8): 95 components[arr[i]] = {"op_name": arr[i+1], "temperature": arr[i+2], "color": arr[i+3], 96 "is_turbid": True if arr[i+4] == "True" or arr[i+4] == "true" else False, 97 "leaf_density": float(arr[i+5]), 98 "lad": arr[i+6], "hotspot_factor": float(arr[i+7]), 99 } 100 self._objects[arr[0]] = components 101 f.close()
def
load_instances(self):
103 def load_instances(self): 104 instances_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "instances.txt") 105 if os.path.exists(instances_file_path): 106 f = open(instances_file_path) 107 for line in f: 108 arr = line.strip().split(" ") 109 obj_name = arr[0] 110 coord = list(map(lambda x: float(x), arr[1:])) 111 if obj_name in self._instances: 112 self._instances[obj_name].append(coord) 113 else: 114 self._instances[obj_name] = [coord] 115 f.close()
def
save_objects(self):
117 def save_objects(self): 118 objects_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects.txt") 119 f = open(objects_file_path, "w") 120 for obj_name in self._objects: 121 out_str = obj_name 122 for comp_name in self._objects[obj_name]: 123 out_str += " " + comp_name 124 out_str += " " + self._objects[obj_name][comp_name]["op_name"] 125 out_str += " " + self._objects[obj_name][comp_name]["temperature"] 126 out_str += " " + self._objects[obj_name][comp_name]["color"] 127 out_str += " " + str(self._objects[obj_name][comp_name]["is_turbid"]) 128 out_str += " " + str(self._objects[obj_name][comp_name]["leaf_density"]) 129 out_str += " " + self._objects[obj_name][comp_name]["lad"] 130 out_str += " " + str(self._objects[obj_name][comp_name]["hotspot_factor"]) 131 out_str += "\n" 132 f.write(out_str) 133 f.close()
def
save_instances(self):
135 def save_instances(self): 136 instances_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "instances.txt") 137 if len(self._instances) != 0: 138 f = open(instances_file_path, "w") 139 for obj_name in self._instances: 140 for coord in self._instances[obj_name]: 141 out_str = obj_name 142 out_str += " " + " ".join(list(map(lambda x: str(x), coord))) + "\n" 143 f.write(out_str) 144 f.close() 145 else: 146 if os.path.exists(instances_file_path): 147 os.remove(instances_file_path)
def
add_object( self, scene_object: SceneObjects.SceneObject, override_file=True, translate_to_origin='no'):
212 def add_object(self, scene_object: SceneObject, override_file=True, translate_to_origin="no"): 213 obj_name = scene_object.get_scene_obj_name() 214 # check if scene object is valid 215 if self.__is_scene_object_valid(scene_object): 216 if obj_name in self._objects: 217 print("Warning: scene object " + obj_name + " already exists.") 218 else: 219 self._objects[obj_name] = scene_object.scene_obj_components 220 if translate_to_origin == "no": 221 self.__copy_components(scene_object.scene_obj_components) 222 else: 223 print("INFO: Translating obj...") 224 self.__copy_components_with_translate(scene_object.scene_obj_components, 225 translate_to_origin)
def
place_object_to( self, obj_name, x=50.0, y=50.0, z=0.0, rotate=0.0, rotate_axis_x=None, rotate_axis_y=None, rotate_axis_z=None, scale_extent_x=None, scale_extent_y=None, scale_extent_z=None):
234 def place_object_to(self, obj_name, x=50.0, y=50.0, z=0.0, rotate=0.0, 235 rotate_axis_x=None, rotate_axis_y=None, rotate_axis_z=None, 236 scale_extent_x=None, scale_extent_y=None, scale_extent_z=None): 237 if not self.__is_scene_object_exist(obj_name): 238 print("Error: scene object " + obj_name + " doest not exist.") 239 else: 240 rotate_factors = [rotate_axis_x, rotate_axis_y, rotate_axis_z] 241 scale_factors = [scale_extent_x, scale_extent_y, scale_extent_z] 242 has_rotation = all(item is not None for item in rotate_factors) 243 has_scale = all(item is not None for item in scale_factors) 244 if not has_rotation: 245 coord = [x, y, z, rotate] 246 elif has_rotation and (not has_scale): 247 coord = [x, y, z, rotate] + rotate_factors 248 elif has_rotation and has_scale: 249 coord = [x, y, z, rotate] + rotate_factors + scale_factors 250 else: 251 coord = [x, y, z, rotate] 252 if obj_name in self._instances: 253 self._instances[obj_name].append(coord) 254 else: 255 self._instances[obj_name] = [coord]
def
read_object_bounding_box(self):
257 def read_object_bounding_box(self): 258 boundingbox_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects_boundingbox.txt") 259 if not os.path.exists(boundingbox_file_path): 260 return None 261 object_bounds = {} 262 f = open(boundingbox_file_path) 263 for line in f: 264 arr = line.split(":") 265 obj_name = arr[0] 266 coords = list(map(lambda x: float(x), arr[1].strip().split(" "))) 267 num_boundings = int(len(coords)/6) 268 obj_bound_box = LSBoundingBox() 269 for i in range(num_boundings): 270 comp_bound_box = LSBoundingBox() 271 comp_bound_box.set_value(coords[i*6: (i+1)*6]) 272 obj_bound_box.add_child(comp_bound_box) 273 object_bounds[obj_name] = obj_bound_box 274 return object_bounds
def
calculate_object_bounding_box(self):
278 def calculate_object_bounding_box(self): 279 if len(self._objects) > 0: 280 boundingbox_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", "objects_boundingbox.txt") 281 fo = open(boundingbox_file_path, "w") 282 for obj_name in self._objects: 283 obj_bound_box = LSBoundingBox() 284 for comp_name in self._objects[obj_name]: 285 comp_obj_file_path = os.path.join(self.get_sim().get_sim_dir(), "Parameters", comp_name) 286 comp_bound_box = LSBoundingBox() 287 f = open(comp_obj_file_path) 288 for line in f: 289 if line.startswith("v "): 290 arr = line.strip().split(" ") 291 (x, y, z) = list(map(lambda xx: float(xx), arr[1:])) 292 if x < comp_bound_box.minX: 293 comp_bound_box.minX = x 294 if y < comp_bound_box.minY: 295 comp_bound_box.minY = y 296 if z < comp_bound_box.minZ: 297 comp_bound_box.minZ = z 298 if x > comp_bound_box.maxX: 299 comp_bound_box.maxX = x 300 if y > comp_bound_box.maxY: 301 comp_bound_box.maxY = y 302 if z > comp_bound_box.maxZ: 303 comp_bound_box.maxZ = z 304 f.close() 305 obj_bound_box.add_child(comp_bound_box) 306 fo.write(obj_name + ":" + obj_bound_box.to_string()+"\n") 307 fo.close()
Inherited Members
- Element.Element
- set_sim
- get_sim