/// <summary> /// Creates a transformed copy of the polygon /// </summary> /// <param name="transform"></param> /// <returns></returns> public XPolygon Transformed(XbimMatrix3D transform) { XPolygon transformed = new XPolygon(); ForEach(p => transformed.Add(transform.Transform(p))); return(transformed); }
internal void SetCenterInMeters(XbimVector3D modelTranslation) { var translation = XbimMatrix3D.CreateTranslation(modelTranslation * OneMeter); var scaling = XbimMatrix3D.CreateScale(1 / OneMeter); Transform = translation * scaling; }
public void MatrixArrayConversion() { var m = XbimMatrix3D.CreateTranslation(10, 20, 30); m.RotateAroundXAxis(Math.PI / 4); m.Scale(.05); var outM = m.ToArray(true); var rback = XbimMatrix3D.FromArray(outM); Assert.AreEqual(rback.M11, m.M11); Assert.AreEqual(rback.M12, m.M12); Assert.AreEqual(rback.M13, m.M13); Assert.AreEqual(rback.M14, m.M14); Assert.AreEqual(rback.M21, m.M21); Assert.AreEqual(rback.M22, m.M22); Assert.AreEqual(rback.M23, m.M23); Assert.AreEqual(rback.M24, m.M24); Assert.AreEqual(rback.M31, m.M31); Assert.AreEqual(rback.M32, m.M32); Assert.AreEqual(rback.M33, m.M33); Assert.AreEqual(rback.M34, m.M34); Assert.AreEqual(rback.OffsetX, m.OffsetX); Assert.AreEqual(rback.OffsetY, m.OffsetY); Assert.AreEqual(rback.OffsetZ, m.OffsetZ); Assert.AreEqual(rback.M44, m.M44); }
public static Matrix3D ToMatrix3D(this XbimMatrix3D m) { return(new Matrix3D(m.M11, m.M12, m.M13, m.M14, m.M21, m.M22, m.M23, m.M24, m.M31, m.M32, m.M33, m.M34, m.OffsetX, m.OffsetY, m.OffsetZ, m.M44)); }
public static WpfMeshGeometry3D GetGeometry(IPersistEntity entity, XbimMatrix3D modelTransform, Material mat) { var tgt = new WpfMeshGeometry3D(mat, mat); tgt.BeginUpdate(); using (var geomstore = entity.Model.GeometryStore) using (var geomReader = geomstore.BeginRead()) { foreach (var shapeInstance in geomReader.ShapeInstancesOfEntity(entity).Where(x => x.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded)) { IXbimShapeGeometryData shapegeom = geomReader.ShapeGeometry(shapeInstance.ShapeGeometryLabel); if (shapegeom.Format != (byte)XbimGeometryType.PolyhedronBinary) { continue; } var transform = shapeInstance.Transformation * modelTransform; tgt.Add( shapegeom.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, transform, (short)entity.Model.UserDefinedId ); } } tgt.EndUpdate(); return(tgt); }
public XbimMatrix3D ToMatrix3D(ConcurrentDictionary <int, object> maps = null) { object transform; if (maps != null && maps.TryGetValue(EntityLabel, out transform)) //already converted it just return cached { return((XbimMatrix3D)transform); } if (RefDirection != null) { XbimVector3D v = RefDirection.XbimVector3D(); v.Normalized(); transform = new XbimMatrix3D(v.X, v.Y, 0, 0, v.Y, v.X, 0, 0, 0, 0, 1, 0, Location.X, Location.Y, 0, 1); } else { transform = new XbimMatrix3D(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, Location.X, Location.Y, Location.Z, 1); } if (maps != null) { maps.TryAdd(EntityLabel, transform); } return((XbimMatrix3D)transform); }
public static void AddElements(this MeshGeometry3D m, IPersistIfcEntity item, XbimMatrix3D wcsTransform) { var fromModel = item.ModelOf as XbimModel; if (fromModel == null || !(item is IfcProduct)) { return; } switch (fromModel.GeometrySupportLevel) { case 2: var context = new Xbim3DModelContext(fromModel); var productShape = context.ShapeInstancesOf((IfcProduct)item) .Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); if (!productShape.Any() && item is IfcFeatureElement) { productShape = context.ShapeInstancesOf((IfcProduct)item) .Where( s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); } if (!productShape.Any()) { return; } foreach (var shapeInstance in productShape) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: m.Read(shapeGeom.ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; case XbimGeometryType.Polyhedron: m.Read(((XbimShapeGeometry)shapeGeom).ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; } } break; case 1: var xm3d = new XbimMeshGeometry3D(); var geomDataSet = fromModel.GetGeometryData(item.EntityLabel, XbimGeometryType.TriangulatedMesh); foreach (var geomData in geomDataSet) { var gd = geomData.TransformBy(wcsTransform); xm3d.Add(gd); } m.Add(xm3d); break; } }
public static RotateTransform3D GetRotateTransform3D(this XbimMatrix3D m) { RotateTransform3D r = new RotateTransform3D(); XbimQuaternion xq = m.GetRotationQuaternion(); r.Rotation = new QuaternionRotation3D(new Quaternion(xq.X, xq.Y, xq.Z, xq.W * (180.0 / Math.PI))); return(r); }
public List <ModelUIElement3D> CreateModelUiElementsDs(IfcModel model, List <IfcGloballyUniqueId> elementIdsList, bool visualizeUnselectedElementsTransparent = true, DiffuseMaterial overrideMaterial = null) { tempMaterialLibrary = new Dictionary <string, DiffuseMaterial>(); // Refresh the selected Models SelectedModels = new Dictionary <string, GeometryModel3D>(); VisualizedModels = new Dictionary <string, ModelUIElement3D>(); xModel = model.GetModel(); context = model.GetModelContext(); // Loop through Entities and visualze them in the viewport var res = new HashSet <IfcGloballyUniqueId>(elementIdsList); var elementList = new List <ModelUIElement3D>(); // GET GEOREFERENCING var wcsTransformation = new XbimMatrix3D(); var myIfcSite = xModel.Instances.OfType <Xbim.Ifc2x3.ProductExtension.IfcSite>(); var ifcSites = myIfcSite as IList <Xbim.Ifc2x3.ProductExtension.IfcSite> ?? myIfcSite.ToList(); if (ifcSites.Count == 1) { Xbim.Ifc2x3.ProductExtension.IfcSite mySite = ifcSites.First(); IfcLocalPlacement relplacement = mySite.ObjectPlacement.ReferencedByPlacements.First(); wcsTransformation = relplacement.ToMatrix3D(); } foreach (var item in xModel.Instances.OfType <IIfcProduct>()) { if (visualizeUnselectedElementsTransparent == false) { if (!res.Contains(item.GlobalId)) { continue; } } // Get the Material var mat = new DiffuseMaterial(); if (overrideMaterial == null) { mat = res.Contains(item.GlobalId) ? GeometryHandler.GetStyleFromXbimModel(item, context) : GeometryHandler.GetStyleFromXbimModel(item, context, 0.03); } else { mat = overrideMaterial; } var m = GeometryHandler.WriteTriangles(item, context, wcsTransformation); tempMaterialLibrary.Add(item.GlobalId, mat); var element = CreateModelUIElement3D(m, mat); element.MouseDown += ElementOnMouseDown; elementList.Add(element); VisualizedModels.Add(item.GlobalId, element); } return(elementList); }
public BimShape(int ProductLabel, short TypeID, int InstanceLabel, int StyleLabel, XbimMatrix3D Matrix = new XbimMatrix3D()) { ifcProductLabel = ProductLabel; ifcTypeID = TypeID; instanceLabel = InstanceLabel; styleLabel = StyleLabel; triangulations = new List <BimTriangulation>(); transform = Matrix; }
private void WriteMatrix(JsonWriter writer, XbimMatrix3D m) { writer.WriteStartArray(); //matrix elements writer.WriteValue(m.M11); writer.WriteValue(m.M12); writer.WriteValue(m.M13); writer.WriteValue(m.M14); writer.WriteValue(m.M21); writer.WriteValue(m.M22); writer.WriteValue(m.M23); writer.WriteValue(m.M24); writer.WriteValue(m.M31); writer.WriteValue(m.M32); writer.WriteValue(m.M33); writer.WriteValue(m.M34); writer.WriteValue(m.OffsetX); writer.WriteValue(m.OffsetY); writer.WriteValue(m.OffsetZ); writer.WriteValue(m.M44); writer.WriteEndArray(); //end of matrix elements }
/// <summary> /// Shofts the points int he polygon relative to the origin. /// </summary> /// <remarks> /// Because XbimPoint3 wont allow us to modify the X,Y,Z values then we have to create new points /// </remarks> /// <param name="origin"></param> public void Normalize(XbimPoint3D origin) { XbimVector3D offset = new XbimVector3D(-origin.X, -origin.Y, -origin.Z); XbimMatrix3D tr = new XbimMatrix3D(offset); List <XbimPoint3D> temp = new List <XbimPoint3D>(this); Clear(); temp.ForEach(pt => Add(tr.Transform(pt))); }
/// <summary> /// If translation is defined, returns matrix translated by the vector /// </summary> /// <param name="matrix">Input matrix</param> /// <param name="translation">Translation</param> /// <returns>Translated matrix</returns> private static XbimMatrix3D Translate(XbimMatrix3D matrix, IVector3D translation) { if (translation == null) { return(matrix); } var translationMatrix = XbimMatrix3D.CreateTranslation(translation.X, translation.Y, translation.Z); return(XbimMatrix3D.Multiply(matrix, translationMatrix)); }
private static void DumpData(StringBuilder sb, byte[] ShapeData) { XbimTriangulatedModelStream m = new XbimTriangulatedModelStream(ShapeData); TextMeshDumper md = new TextMeshDumper(sb); XbimMatrix3D id = XbimMatrix3D.Identity; m.BuildWithNormals(md, id); // sb.Append(m.ToString()); }
public XbimShapeInstance(int id = -1) { _instanceLabel = id; _ifcTypeId = 0; _ifcProductLabel = 0; _styleLabel = 0; _shapeLabel = -1; _representationContext = 0; _representationType = XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded; _transformation = XbimMatrix3D.Identity; _boundingBox = XbimRect3D.Empty; }
internal void AddMesh(byte[] mesh, XbimMatrix3D?transform = null) { int indexBase = Positions.Count; bool needRotate = false; bool needTransform = false; XbimQuaternion xq = new XbimQuaternion(0.0, 0.0, 0.0, 1.0); XbimMatrix3D transformValue = XbimMatrix3D.Identity; if (transform.HasValue) { transformValue = transform.Value; needTransform = !transformValue.IsIdentity; xq = transformValue.GetRotationQuaternion(); // we have to build a rotation transform from the quaternion (to tranform normals later on) needRotate = !xq.IsIdentity(); } using (var ms = new MemoryStream(mesh)) using (var br = new BinaryReader(ms)) { var t = br.ReadShapeTriangulation(); List <float[]> pts; List <int> idx; t.ToPointsWithNormalsAndIndices(out pts, out idx); // add to lists // // Commented because of https://github.com/xBimTeam/XbimGltf/issues/2 //Positions.Capacity += pts.Count; //Normals.Capacity += pts.Count; //Indices.Capacity += idx.Count; foreach (var floatsArray in pts) { var tmpPosition = new XbimPoint3D(floatsArray[0], floatsArray[1], floatsArray[2]); if (needTransform) { tmpPosition = transformValue.Transform(tmpPosition); } Positions.Add(tmpPosition); var tmpNormal = new XbimVector3D(floatsArray[3], floatsArray[4], floatsArray[5]); if (needRotate) //transform the normal if we have to { XbimQuaternion.Transform(ref tmpNormal, ref xq, out tmpNormal); } Normals.Add(tmpNormal); } foreach (var index in idx) { Indices.Add(index + indexBase); } } }
private void TestRotationCreation(XbimPoint3D from, XbimPoint3D to) { var m = XbimMatrix3D.CreateRotation(from, to); var toForTest = m.Transform(from); var toNormalised = (to - new XbimPoint3D(0, 0, 0)).Normalized(); var toNormP = new XbimPoint3D( toNormalised.X, toNormalised.Y, toNormalised.Z ); Assert.IsTrue(IsTolerableDifference(toNormP, toForTest)); }
public static Bitub.Dto.Scene.Transform ToRotation(this XbimMatrix3D t, double scale = 1.0) { return(new Bitub.Dto.Scene.Transform { R = new Rotation { // XbimMatrix is transposed (left hand chaining) Rx = new XbimVector3D(t.M11, t.M21, t.M31).ToXYZ(), Ry = new XbimVector3D(t.M12, t.M22, t.M32).ToXYZ(), Rz = new XbimVector3D(t.M13, t.M23, t.M33).ToXYZ(), }, T = t.Translation.ToXYZ(scale) }); }
public static IIfcLocalPlacement NewLocalPlacement(this IModel s, XbimMatrix3D transform, bool scaleUp = false) { switch (s.SchemaVersion) { case Xbim.Common.Step21.XbimSchemaVersion.Ifc2X3: return(s.NewIfc2x3FullPlacement(transform, scaleUp)); case Xbim.Common.Step21.XbimSchemaVersion.Ifc4: case Xbim.Common.Step21.XbimSchemaVersion.Ifc4x1: return(s.NewIfc4FullPlacement(transform, scaleUp)); } throw new NotImplementedException($"Not implemented schema version ${s.SchemaVersion}"); }
/// <summary> /// This function centralises the extraction of a product placement, but it needs the support of XbimPlacementTree and an XbimGeometryEngine /// We should probably find a conceptual place for it somewhere in the scene, where these are cached. /// </summary> public static XbimMatrix3D GetTransform(IIfcProduct product, XbimPlacementTree tree, XbimGeometryEngine engine) { XbimMatrix3D placementTransform = XbimMatrix3D.Identity; if (product.ObjectPlacement is IIfcLocalPlacement) { placementTransform = tree[product.ObjectPlacement.EntityLabel]; } else if (product.ObjectPlacement is IIfcGridPlacement) { placementTransform = engine.ToMatrix3D((IIfcGridPlacement)product.ObjectPlacement, null); } return(placementTransform); }
public void QuaternionTests() { var q = new XbimQuaternion(); Assert.AreEqual(true, q.IsIdentity(), "Uninitialised quaternion should be identity."); q = new XbimQuaternion(0.0f, 0.0f, 0.0f, 1.0f); Assert.AreEqual(true, q.IsIdentity(), "Should be identity when initialised with floats."); var mat = new XbimMatrix3D(); q = mat.GetRotationQuaternion(); Assert.AreEqual(true, q.IsIdentity(), "Quaternion from identity matrix shold be identity."); }
public static IfcLocalPlacement NewIfc4FullPlacement(this IModel s, XbimMatrix3D transform, bool scaleUp = false) { var localPlacement = s.Instances.New <IfcLocalPlacement>(); var placement = s.Instances.New <IfcAxis2Placement3D>(); placement.Location = s.NewIfcPoint <IfcCartesianPoint>(transform.Translation, scaleUp); if (!transform.GetRotationQuaternion().IsIdentity()) { placement.RefDirection = s.NewIfcDirection <IfcDirection>(transform.Backward, false); placement.Axis = s.NewIfcDirection <IfcDirection>(transform.Up, false); } localPlacement.RelativePlacement = placement; return(localPlacement); }
public double GetArea() { // the normal can be taken from the product of two segments on the polyline if (Count() < 3) { return(double.NaN); } var normal = Normal() * -1; var firstSegment = FirstSegment(); var up = XbimVector3D.CrossProduct(normal, firstSegment); var campos = new XbimVector3D( _geomPoints[0].Point.X, _geomPoints[0].Point.Y, _geomPoints[0].Point.Z ); var target = campos + normal; var m = XbimMatrix3D.CreateLookAt(campos, target, up); var point = new XbimPoint3D[Count()]; for (var i = 0; i < point.Length; i++) { var pBefore = new XbimPoint3D( _geomPoints[i].Point.X, _geomPoints[i].Point.Y, _geomPoints[i].Point.Z ); var pAft = m.Transform(pBefore); point[i] = pAft; } // http://stackoverflow.com/questions/2553149/area-of-a-irregular-shape // it assumes that the last point is NOT the same of the first one, but it tolerates the case. double area = 0.0f; var numVertices = Count(); for (var i = 0; i < numVertices - 1; ++i) { area += point[i].X * point[i + 1].Y - point[i + 1].X * point[i].Y; } area += point[numVertices - 1].X * point[0].Y - point[0].X * point[numVertices - 1].Y; area /= 2.0; return(area); }
public MyBimShapeInstance(int product, short type, int instanceLabel, int styleLabel, XbimMatrix3D matrix = default) { this.productLabel = product; this.typeId = type; this.instanceLabel = instanceLabel; this.styleLabel = styleLabel; this.triangulations = new List <MyBimTriangulation>(); transform = matrix; xbimShapeInstance = new XbimShapeInstance(this.instanceLabel) { IfcProductLabel = productLabel, IfcTypeId = typeId, StyleLabel = this.styleLabel, Transformation = transform }; }
public static Bitub.Dto.Scene.Transform ToQuaternion(this XbimMatrix3D t, double scale = 1.0) { var q = t.GetRotationQuaternion(); return(new Bitub.Dto.Scene.Transform { Q = new Quaternion { X = q.X, Y = q.Y, Z = q.Z, W = q.W }, T = t.Translation.ToXYZ(scale) }); }
public void QuaternionAndMatrixOrientationTests() { var t = new XbimMatrix3D(new XbimVector3D(1, 1, 0)); t.RotateAroundZAxis(Math.PI / 2); // Left chaining matrix operation => transposed in Xbim Assert.AreEqual(new XbimVector3D(0, 1, 0), t.Right); Assert.AreEqual(new XbimVector3D(-1, 0, 0), t.Up); Assert.AreEqual(new XbimVector3D(0, 0, 1), t.Backward); var q = t.GetRotationQuaternion(); Assert.AreEqual(Math.Sqrt(2) / 2, q.W, 1e-5); Assert.AreEqual(Math.Sqrt(2) / 2, q.Z, 1e-5); Assert.AreEqual(0, q.X, 1e-8); Assert.AreEqual(0, q.Y, 1e-8); }
public static XbimMatrix3D ToMatrix3D(this IIfcObjectPlacement objPlacement) { var lp = objPlacement as IIfcLocalPlacement; if (lp != null) { XbimMatrix3D local = lp.RelativePlacement.ToMatrix3D(); if (lp.PlacementRelTo != null) { return(local * lp.PlacementRelTo.ToMatrix3D()); } return(local); } else // { throw new NotImplementedException($"Placement of type {objPlacement.GetType().Name} is not implemented"); } }
/// <summary> /// Builds a windows XbimMatrix3D from a CartesianTransformationOperator3DnonUniform /// </summary> /// <param name = "ct3D"></param> /// <returns></returns> public static XbimMatrix3D ToMatrix3D(this IfcCartesianTransformationOperator3DnonUniform ct3D, ConcurrentDictionary <int, Object> maps = null) { if (maps == null) { return(ConvertCartesianTransformationOperator3DnonUniform(ct3D)); } else { object transform; if (maps.TryGetValue(ct3D.EntityLabel, out transform)) //already converted it just return cached { return((XbimMatrix3D)transform); } XbimMatrix3D matrix = ConvertCartesianTransformationOperator3DnonUniform(ct3D); maps.TryAdd(ct3D.EntityLabel, matrix); return(matrix); } }
/// <summary> /// Get point on arc by given start point, start direction, arc radius, /// is counter-clockwise and distance along. /// </summary> /// <param name="start"></param> /// <param name="dir"></param> /// <param name="radius"></param> /// <param name="isCCW"></param> /// <param name="dist"></param> /// <returns></returns> public static (XbimPoint3D pt, XbimVector3D vec) GetPointOnCurve(XbimPoint3D start, XbimVector3D dir, double radius, bool isCCW, double dist) { // Compute the location of arc center. var zAxis = new XbimVector3D(0, 0, 1); var start2center = isCCW ? zAxis.CrossProduct(dir) : dir.CrossProduct(zAxis); var center = start + radius * start2center; // Compute the location of arc end point var theta = isCCW ? dist / radius : -dist / radius; var center2start = start2center.Negated(); var mat = new XbimMatrix3D(); mat.RotateAroundZAxis(theta); var center2end = mat.Transform(center2start); var lateral = isCCW ? center2end.Negated() : center2end; return(center + radius * center2end, lateral); }
public void GetGeometryFromXbimModel_IFC4(MeshGeometry3D m, IPersistEntity item, XbimMatrix3D wcsTransform) { if (item.Model == null || !(item is Xbim.Ifc4.Kernel.IfcProduct)) { return; } var context = new Xbim3DModelContext(item.Model); var productShape = context.ShapeInstancesOf((Xbim.Ifc4.Interfaces.IIfcProduct)item) .Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); if (!productShape.Any() && item is Xbim.Ifc4.Interfaces.IIfcFeatureElement) { productShape = context.ShapeInstancesOf((Xbim.Ifc4.Interfaces.IIfcProduct)item) .Where( s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); } if (!productShape.Any()) { return; } foreach (var shapeInstance in productShape) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: m.Read(shapeGeom.ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; case XbimGeometryType.Polyhedron: m.Read(((XbimShapeGeometry)shapeGeom).ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; } } }