black ifcclash

This commit is contained in:
htlcnn
2020-11-01 19:23:15 +07:00
committed by Dion Moult
parent d6881e833d
commit 892be5444c
2 changed files with 146 additions and 196 deletions
+110 -108
View File
@@ -22,43 +22,43 @@ class IfcClasher:
self.settings = settings
self.geom_settings = ifcopenshell.geom.settings()
self.clash_sets = []
self.clash_data = {'meshes': {}}
self.global_data = {'meshes': {}, 'matrices': {}}
self.clash_data = {"meshes": {}}
self.global_data = {"meshes": {}, "matrices": {}}
def clash(self):
for clash_set in self.clash_sets:
self.process_clash_set(clash_set)
def process_clash_set(self, clash_set):
for ab in ['a', 'b']:
self.settings.logger.info(f'Creating collision manager {ab} ...')
clash_set[f'{ab}_cm'] = collision.CollisionManager()
self.settings.logger.info(f'Loading files {ab} ...')
for ab in ["a", "b"]:
self.settings.logger.info(f"Creating collision manager {ab} ...")
clash_set[f"{ab}_cm"] = collision.CollisionManager()
self.settings.logger.info(f"Loading files {ab} ...")
for data in clash_set[ab]:
data['ifc'] = ifcopenshell.open(data['file'])
self.patch_ifc(data['ifc'])
self.settings.logger.info(f'Creating collision data for {ab} ...')
if len(data['ifc'].by_type('IfcElement')) > 0:
self.add_collision_objects(data, clash_set[f'{ab}_cm'])
data["ifc"] = ifcopenshell.open(data["file"])
self.patch_ifc(data["ifc"])
self.settings.logger.info(f"Creating collision data for {ab} ...")
if len(data["ifc"].by_type("IfcElement")) > 0:
self.add_collision_objects(data, clash_set[f"{ab}_cm"])
if 'b' in clash_set and clash_set['b']:
results = clash_set['a_cm'].in_collision_other(clash_set['b_cm'], return_data=True)
if "b" in clash_set and clash_set["b"]:
results = clash_set["a_cm"].in_collision_other(clash_set["b_cm"], return_data=True)
else:
results = clash_set['a_cm'].in_collision_internal(return_data=True)
results = clash_set["a_cm"].in_collision_internal(return_data=True)
if not results[0]:
return
tolerance = clash_set['tolerance'] if 'tolerance' in clash_set else 0.01
clash_set['clashes'] = {}
tolerance = clash_set["tolerance"] if "tolerance" in clash_set else 0.01
clash_set["clashes"] = {}
for contact in results[1]:
a_global_id, b_global_id = contact.names
a = self.get_element(clash_set['a'], a_global_id)
if 'b' in clash_set and clash_set['b']:
b = self.get_element(clash_set['b'], b_global_id)
a = self.get_element(clash_set["a"], a_global_id)
if "b" in clash_set and clash_set["b"]:
b = self.get_element(clash_set["b"], b_global_id)
else:
b = self.get_element(clash_set['a'], b_global_id)
b = self.get_element(clash_set["a"], b_global_id)
if contact.raw.penetration_depth < tolerance:
continue
@@ -72,7 +72,7 @@ class IfcClasher:
# triangle-triangle intersection test. Optimistically, this skips
# the false positives. Conservatively, we let the user manually deal
# with the false positives and we mark it as a clash.
is_optimistic = True # TODO: let user configure this
is_optimistic = True # TODO: let user configure this
if is_optimistic and tolerance != 0:
# We'll now check if the contact data's two faces are actually
@@ -82,17 +82,21 @@ class IfcClasher:
# please help rewrite this.
# Get vertices of clashing tris
p1 = self.global_data['meshes'][contact.names[0]].faces[contact.index(contact.names[0])]
p2 = self.global_data['meshes'][contact.names[1]].faces[contact.index(contact.names[1])]
m1 = self.global_data['matrices'][contact.names[0]]
m2 = self.global_data['matrices'][contact.names[1]]
p1 = self.global_data["meshes"][contact.names[0]].faces[contact.index(contact.names[0])]
p2 = self.global_data["meshes"][contact.names[1]].faces[contact.index(contact.names[1])]
m1 = self.global_data["matrices"][contact.names[0]]
m2 = self.global_data["matrices"][contact.names[1]]
v1 = []
v2 = []
for v in p1:
v1.append((m1 @ np.array([*self.global_data['meshes'][contact.names[0]].vertices[v], 1]))[0:3].round(2))
v1.append(
(m1 @ np.array([*self.global_data["meshes"][contact.names[0]].vertices[v], 1]))[0:3].round(2)
)
for v in p2:
v2.append((m2 @ np.array([*self.global_data['meshes'][contact.names[1]].vertices[v], 1]))[0:3].round(2))
v2.append(
(m2 @ np.array([*self.global_data["meshes"][contact.names[1]].vertices[v], 1]))[0:3].round(2)
)
tri1_x = 0
tri2_x = 0
@@ -111,75 +115,89 @@ class IfcClasher:
# This is probably two triangles which just touch
continue
key = f'{a_global_id}-{b_global_id}'
key = f"{a_global_id}-{b_global_id}"
if key in clash_set['clashes'] \
and clash_set['clashes'][key]['penetration_depth'] > contact.raw.penetration_depth:
if (
key in clash_set["clashes"]
and clash_set["clashes"][key]["penetration_depth"] > contact.raw.penetration_depth
):
continue
clash_set['clashes'][key] = {
'a_global_id': a_global_id,
'b_global_id': b_global_id,
'a_ifc_class': a.is_a(),
'b_ifc_class': b.is_a(),
'a_name': a.Name,
'b_name': b.Name,
'normal': list(contact.raw.normal),
'position': list(contact.raw.pos),
'penetration_depth': contact.raw.penetration_depth
clash_set["clashes"][key] = {
"a_global_id": a_global_id,
"b_global_id": b_global_id,
"a_ifc_class": a.is_a(),
"b_ifc_class": b.is_a(),
"a_name": a.Name,
"b_name": b.Name,
"normal": list(contact.raw.normal),
"position": list(contact.raw.pos),
"penetration_depth": contact.raw.penetration_depth,
}
# https://stackoverflow.com/questions/42740765/intersection-between-line-and-triangle-in-3d
def intersect_line_triangle(self, q1, q2, p1, p2, p3):
def signed_tetra_volume(a,b,c,d):
return np.sign(np.dot(np.cross(b-a,c-a),d-a)/6.0)
def signed_tetra_volume(a, b, c, d):
return np.sign(np.dot(np.cross(b - a, c - a), d - a) / 6.0)
s1 = signed_tetra_volume(q1,p1,p2,p3)
s2 = signed_tetra_volume(q2,p1,p2,p3)
s1 = signed_tetra_volume(q1, p1, p2, p3)
s2 = signed_tetra_volume(q2, p1, p2, p3)
if s1 != s2:
s3 = signed_tetra_volume(q1,q2,p1,p2)
s4 = signed_tetra_volume(q1,q2,p2,p3)
s5 = signed_tetra_volume(q1,q2,p3,p1)
s3 = signed_tetra_volume(q1, q2, p1, p2)
s4 = signed_tetra_volume(q1, q2, p2, p3)
s5 = signed_tetra_volume(q1, q2, p3, p1)
if s3 == s4 and s4 == s5:
n = np.cross(p2-p1,p3-p1)
t = -np.dot(q1,n-p1) / np.dot(q1,q2-q1)
return q1 + t * (q2-q1)
n = np.cross(p2 - p1, p3 - p1)
t = -np.dot(q1, n - p1) / np.dot(q1, q2 - q1)
return q1 + t * (q2 - q1)
return None
def export(self):
results = self.clash_sets.copy()
for result in results:
del result['a_cm']
del result['b_cm']
for ab in ['a', 'b']:
del result["a_cm"]
del result["b_cm"]
for ab in ["a", "b"]:
for data in result[ab]:
if 'ifc' in data:
del data['ifc']
with open(self.settings.output, 'w', encoding='utf-8') as clashes_file:
if "ifc" in data:
del data["ifc"]
with open(self.settings.output, "w", encoding="utf-8") as clashes_file:
json.dump(results, clashes_file, indent=4)
def get_element(self, clash_group, global_id):
for data in clash_group:
try:
element = data['ifc'].by_guid(global_id)
element = data["ifc"].by_guid(global_id)
if element:
return element
except:
pass
def add_collision_objects(self, data, cm):
self.clash_data['meshes'] = {}
self.clash_data["meshes"] = {}
selector = ifcopenshell.util.selector.Selector()
if 'selector' not in data:
iterator = ifcopenshell.geom.iterator(self.geom_settings, data['ifc'], multiprocessing.cpu_count(),
exclude=(data['ifc'].by_type('IfcSpatialStructureElement')))
elif data['mode'] == 'e':
iterator = ifcopenshell.geom.iterator(self.geom_settings, data['ifc'], multiprocessing.cpu_count(),
exclude=selector.parse(data['ifc'], data['selector']))
elif data['mode'] == 'i':
iterator = ifcopenshell.geom.iterator(self.geom_settings, data['ifc'], multiprocessing.cpu_count(),
include=selector.parse(data['ifc'], data['selector']))
if "selector" not in data:
iterator = ifcopenshell.geom.iterator(
self.geom_settings,
data["ifc"],
multiprocessing.cpu_count(),
exclude=(data["ifc"].by_type("IfcSpatialStructureElement")),
)
elif data["mode"] == "e":
iterator = ifcopenshell.geom.iterator(
self.geom_settings,
data["ifc"],
multiprocessing.cpu_count(),
exclude=selector.parse(data["ifc"], data["selector"]),
)
elif data["mode"] == "i":
iterator = ifcopenshell.geom.iterator(
self.geom_settings,
data["ifc"],
multiprocessing.cpu_count(),
include=selector.parse(data["ifc"], data["selector"]),
)
valid_file = iterator.initialize()
if not valid_file:
return False
@@ -196,46 +214,37 @@ class IfcClasher:
def add_collision_object(self, data, cm, shape):
if shape is None:
return
element = data['ifc'].by_id(shape.guid)
self.settings.logger.info('Creating object {}'.format(element))
mesh_name = f'mesh-{shape.geometry.id}'
if mesh_name in self.clash_data['meshes']:
mesh = self.clash_data['meshes'][mesh_name]
element = data["ifc"].by_id(shape.guid)
self.settings.logger.info("Creating object {}".format(element))
mesh_name = f"mesh-{shape.geometry.id}"
if mesh_name in self.clash_data["meshes"]:
mesh = self.clash_data["meshes"][mesh_name]
else:
mesh = self.create_mesh(shape)
self.clash_data['meshes'][mesh_name] = mesh
self.global_data['meshes'][shape.guid] = mesh
self.clash_data["meshes"][mesh_name] = mesh
self.global_data["meshes"][shape.guid] = mesh
m = shape.transformation.matrix.data
mat = np.array(
[
[m[0], m[3], m[6], m[9]],
[m[1], m[4], m[7], m[10]],
[m[2], m[5], m[8], m[11]],
[0, 0, 0, 1]
]
)
mat = np.array([[m[0], m[3], m[6], m[9]], [m[1], m[4], m[7], m[10]], [m[2], m[5], m[8], m[11]], [0, 0, 0, 1]])
mat.transpose()
self.global_data['matrices'][shape.guid] = mat
self.global_data["matrices"][shape.guid] = mat
cm.add_object(shape.guid, mesh, mat)
def create_mesh(self, shape):
f = shape.geometry.faces
v = shape.geometry.verts
mesh = Mesh()
mesh.vertices = np.array([[v[i], v[i + 1], v[i + 2]]
for i in range(0, len(v), 3)])
mesh.faces = np.array([[f[i], f[i + 1], f[i + 2]]
for i in range(0, len(f), 3)])
mesh.vertices = np.array([[v[i], v[i + 1], v[i + 2]] for i in range(0, len(v), 3)])
mesh.faces = np.array([[f[i], f[i + 1], f[i + 2]] for i in range(0, len(f), 3)])
return mesh
def patch_ifc(self, ifc_file):
project = ifc_file.by_type('IfcProject')[0]
sites = self.find_decomposed_ifc_class(project, 'IfcSite')
project = ifc_file.by_type("IfcProject")[0]
sites = self.find_decomposed_ifc_class(project, "IfcSite")
for site in sites:
self.patch_placement_to_origin(site)
buildings = self.find_decomposed_ifc_class(project, 'IfcBuilding')
buildings = self.find_decomposed_ifc_class(project, "IfcBuilding")
for building in buildings:
self.patch_placement_to_origin(building)
@@ -252,43 +261,36 @@ class IfcClasher:
return results
def patch_placement_to_origin(self, element):
element.ObjectPlacement.RelativePlacement.Location.Coordinates = (0., 0., 0.)
element.ObjectPlacement.RelativePlacement.Location.Coordinates = (0.0, 0.0, 0.0)
if element.ObjectPlacement.RelativePlacement.Axis:
element.ObjectPlacement.RelativePlacement.Axis.DirectionRatios = (0., 0., 1.)
element.ObjectPlacement.RelativePlacement.Axis.DirectionRatios = (0.0, 0.0, 1.0)
if element.ObjectPlacement.RelativePlacement.RefDirection:
element.ObjectPlacement.RelativePlacement.RefDirection.DirectionRatios = (1., 0., 0.)
element.ObjectPlacement.RelativePlacement.RefDirection.DirectionRatios = (1.0, 0.0, 0.0)
class IfcClashSettings:
def __init__(self):
self.logger = None
self.output = 'clashes.json'
self.output = "clashes.json"
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Clashes geometry between two IFC files')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Clashes geometry between two IFC files")
parser.add_argument("input", type=str, help="A JSON dataset describing a series of clashsets")
parser.add_argument(
'input',
type=str,
help='A JSON dataset describing a series of clashsets')
parser.add_argument(
'-o',
'--output',
type=str,
help='The JSON diff file to output. Defaults to output.json',
default='output.json')
"-o", "--output", type=str, help="The JSON diff file to output. Defaults to output.json", default="output.json"
)
args = parser.parse_args()
settings = IfcClashSettings()
settings.output = args.output
settings.logger = logging.getLogger('Clash')
settings.logger = logging.getLogger("Clash")
settings.logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
settings.logger.addHandler(handler)
ifc_clasher = IfcClasher(settings)
with open(args.input, 'r') as clash_sets_file:
with open(args.input, "r") as clash_sets_file:
ifc_clasher.clash_sets = json.loads(clash_sets_file.read())
ifc_clasher.clash()
ifc_clasher.export()