Ejemplo n.º 1
0
        public static Shell Shell(this HoneybeeSchema.Room room)
        {
            if (room == null)
            {
                return(null);
            }

            List <HoneybeeSchema.Face> faces = room.Faces;

            if (faces == null && faces.Count < 3)
            {
                return(null);
            }

            List <Face3D> face3Ds = new List <Face3D>();

            foreach (HoneybeeSchema.Face face in room.Faces)
            {
                Face3D face3D = Convert.ToSAM(face.Geometry);
                if (face3D == null)
                {
                    return(null);
                }

                face3Ds.Add(face3D);
            }

            return(new Shell(face3Ds));
        }
Ejemplo n.º 2
0
        public static Shell ToSAM(this IEnumerable <Polyloop> polyloops)
        {
            if (polyloops == null || polyloops.Count() == 0)
            {
                return(null);
            }

            List <Face3D> face3Ds = new List <Face3D>();

            foreach (Polyloop polyloop in polyloops)
            {
                Polygon3D polygon3D = polyloop?.ToSAM();
                if (polygon3D == null)
                {
                    continue;
                }

                Face3D face3D = new Face3D(polygon3D);
                if (face3D != null)
                {
                    face3Ds.Add(face3D);
                }
            }

            if (face3Ds.Count == 0)
            {
                return(null);
            }

            return(new Shell(face3Ds));
        }
Ejemplo n.º 3
0
        public void Init()
        {
            // TODO uncomment below to create an instance of Door
            var face = new Face3D(
                new List <List <double> >()
            {
                new List <double>()
                {
                    0, 0, 0
                },
                new List <double>()
                {
                    0.5, 0.5, 0.5
                },
                new List <double>()
                {
                    1, 0, 0
                }
            });

            instance = new Door(
                "mainEntrance",
                face
                );
        }
Ejemplo n.º 4
0
        public static Face3D Transform(this Transform transform, Face3D face3D, bool convertUnits = true)
        {
            if (transform == null || face3D == null)
                return null;

            if (transform.IsIdentity)
                return new Face3D(face3D);

            Spatial.Plane plane = Transform(transform, face3D.GetPlane());
            if (plane == null)
                return null;

            Planar.IClosed2D externalEdge2D = plane.Convert(Transform(transform, face3D.GetExternalEdge3D(), convertUnits));
            if (externalEdge2D == null)
                return null;

            List<Planar.IClosed2D> internalEdges2D = null;

            List<IClosedPlanar3D> internalEdges3D = face3D.GetInternalEdge3Ds();
            if(internalEdges3D != null)
            {
                internalEdges2D = new List<Planar.IClosed2D>();
                foreach (IClosedPlanar3D internalEdge3D in internalEdges3D)
                {
                    Planar.IClosed2D internalEdge2D = plane.Convert(Transform(transform, internalEdge3D, convertUnits));
                    if (internalEdge2D == null)
                        continue;

                    internalEdges2D.Add(internalEdge2D);
                }
            }

            return Face3D.Create(plane, externalEdge2D, internalEdges2D, EdgeOrientationMethod.Undefined);
        }
Ejemplo n.º 5
0
        public static IOpening ToSAM_Opening(this EnergyAnalysisOpening energyAnalysisOpening, ConvertSettings convertSettings)
        {
            if (energyAnalysisOpening == null)
            {
                return(null);
            }

            IOpening result = convertSettings?.GetObject <IOpening>(energyAnalysisOpening.Id);

            if (result != null)
            {
                return(result);
            }

            Polygon3D polygon3D = energyAnalysisOpening.GetPolyloop().ToSAM();

            if (polygon3D == null)
            {
                return(null);
            }

            FamilyInstance familyInstance = Core.Revit.Query.Element(energyAnalysisOpening) as FamilyInstance;

            if (familyInstance == null)
            {
                return(null);
            }

            if (Core.Revit.Query.Simplified(familyInstance))
            {
                result = Core.Revit.Query.IJSAMObjects <IOpening>(familyInstance)?.FirstOrDefault();
                if (result != null)
                {
                    return(result);
                }
            }

            OpeningType openingType = ToSAM_OpeningType(familyInstance.Symbol, convertSettings);

            Point3D point3D_Location = Geometry.Revit.Query.LocationPoint3D(familyInstance);

            if (point3D_Location == null)
            {
                return(null);
            }

            Face3D face3D = new Face3D(Geometry.Spatial.Create.IClosedPlanar3D(polygon3D, point3D_Location));

            if (face3D == null)
            {
                return(null);
            }

            result = Analytical.Create.Opening(openingType, face3D);
            result.UpdateParameterSets(familyInstance);

            convertSettings?.Add(energyAnalysisOpening.Id, result);

            return(result);
        }
Ejemplo n.º 6
0
        public static List <Face3D> ToSAM_Face3Ds(this Mesh mesh)
        {
            List <Triangle3D> triangle3Ds = ToSAM_Triangle3Ds(mesh);

            if (triangle3Ds == null)
            {
                return(null);
            }

            List <Face3D> result = new List <Face3D>();

            foreach (Triangle3D triangle3D in triangle3Ds)
            {
                if (triangle3D == null || !triangle3D.IsValid())
                {
                    continue;
                }

                Face3D face3D = new Face3D(triangle3D);
                if (!face3D.IsValid())
                {
                    continue;
                }

                result.Add(face3D);
            }

            return(result);
        }
Ejemplo n.º 7
0
        //due to issue with plane calculation we rounding
        public static Spatial.Shell ToSAM(this Cell cell)
        {
            if (cell == null)
            {
                return(null);
            }

            IList <global::Topologic.Face> faces = cell.Faces;

            if (faces == null || faces.Count == 0)
            {
                return(null);
            }

            List <Face3D> face3Ds = new List <Face3D>();

            foreach (global::Topologic.Face face in faces)
            {
                Face3D face3D = face.ToSAM();
                if (face3D == null)
                {
                    continue;
                }

                face3Ds.Add(face3D);
            }

            return(new Spatial.Shell(face3Ds));
        }
Ejemplo n.º 8
0
        public static HoneybeeSchema.Face3D ToLadybugTools(this Face3D face3D)
        {
            if (face3D == null)
            {
                return(null);
            }

            ISegmentable3D externalEdge = face3D.GetExternalEdge3D() as ISegmentable3D;

            if (externalEdge == null)
            {
                throw new System.NotImplementedException();
            }

            List <Point3D> point3Ds = externalEdge.GetPoints();

            if (point3Ds == null || point3Ds.Count < 3)
            {
                return(null);
            }

            List <List <double> > boundary = ToLadybugTools(point3Ds);

            if (boundary == null || boundary.Count == 0)
            {
                return(null);
            }

            List <List <List <double> > > holes         = new List <List <List <double> > >();
            List <IClosedPlanar3D>        internalEdges = face3D.GetInternalEdge3Ds();

            if (internalEdges != null && internalEdges.Count > 0)
            {
                foreach (IClosedPlanar3D internalEdge_Temp in internalEdges)
                {
                    ISegmentable3D internalEdge = internalEdge_Temp as ISegmentable3D;
                    if (internalEdge == null)
                    {
                        throw new System.NotImplementedException();
                    }

                    point3Ds = internalEdge.GetPoints();
                    if (point3Ds == null || point3Ds.Count < 3)
                    {
                        continue;
                    }

                    List <List <double> > hole = ToLadybugTools(point3Ds);
                    if (hole == null || hole.Count == 0)
                    {
                        continue;
                    }

                    holes.Add(hole);
                }
            }

            return(new HoneybeeSchema.Face3D(boundary, holes));
        }
Ejemplo n.º 9
0
        public static HoneybeeSchema.Door ToLadybugTools_Door(this Aperture aperture, MaterialLibrary materialLibrary = null, int index = -1, int index_Adjacent = -1, string adjacentPanelUniqueName = null, string adjacentSpaceUniqueName = null)
        {
            if (aperture == null)
            {
                return(null);
            }

            ApertureConstruction apertureConstruction = aperture.ApertureConstruction;

            if (apertureConstruction == null)
            {
                return(null);
            }

            MaterialType materialType = apertureConstruction.PaneConstructionLayers.MaterialType(materialLibrary);

            if (aperture.ApertureType != ApertureType.Door)
            {
                //Opaque Windows to be replaced by Doors
                if (materialType != MaterialType.Opaque)
                {
                    return(null);
                }
            }

            PlanarBoundary3D planarBoundary3D = aperture.PlanarBoundary3D;

            if (planarBoundary3D == null)
            {
                return(null);
            }

            HoneybeeSchema.AnyOf <Outdoors, Surface> anyOf = new Outdoors();

            Face3D face3D = planarBoundary3D.ToLadybugTools();

            if (!string.IsNullOrEmpty(adjacentPanelUniqueName) && !string.IsNullOrEmpty(adjacentSpaceUniqueName))
            {
                List <string> uniqueNames = new List <string>();
                uniqueNames.Add(Core.LadybugTools.Query.UniqueName(aperture, index_Adjacent));
                uniqueNames.Add(adjacentPanelUniqueName);
                uniqueNames.Add(adjacentSpaceUniqueName);

                anyOf = new Surface(uniqueNames);
            }

            DoorEnergyPropertiesAbridged apertureEnergyPropertiesAbridged = new DoorEnergyPropertiesAbridged(Query.UniqueName(apertureConstruction, !(index_Adjacent != -1 && index <= index_Adjacent)));

            HoneybeeSchema.Door door = new HoneybeeSchema.Door(
                identifier: Core.LadybugTools.Query.UniqueName(aperture, index),
                geometry: face3D,
                boundaryCondition: anyOf,
                properties: new DoorPropertiesAbridged(apertureEnergyPropertiesAbridged),
                displayName: aperture.Name);

            door.IsGlass = materialType == MaterialType.Transparent;
            return(door);
        }
Ejemplo n.º 10
0
        public Triangle3D(Point3D v1, Point3D v2, Point3D v3)
        {
            List <Point3D> vertices = new List <Point3D>();

            vertices.Add(v1);
            vertices.Add(v2);
            vertices.Add(v3);
            _baseFace = new Face3D(vertices);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="dataAccess">
        /// The DA object is used to retrieve from inputs and store in outputs.
        /// </param>
        protected override void SolveInstance(IGH_DataAccess dataAccess)
        {
            ISAMGeometry sAMGeometry = null;

            if (!dataAccess.GetData(0, ref sAMGeometry) || sAMGeometry == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            Point3D point3D = sAMGeometry as Point3D;

            if (point3D != null)
            {
                dataAccess.SetData(0, Geometry.Topologic.Convert.ToTopologic(point3D));
                return;
            }

            ICurve3D curve3D = sAMGeometry as ICurve3D;

            if (curve3D != null)
            {
                dataAccess.SetData(0, Geometry.Topologic.Convert.ToTopologic(curve3D));
                return;
            }

            Polygon3D polygon3D = sAMGeometry as Polygon3D;

            if (polygon3D != null)
            {
                dataAccess.SetData(0, Geometry.Topologic.Convert.ToTopologic(polygon3D));
                return;
            }

            Face3D face3D = sAMGeometry as Face3D;

            if (face3D != null)
            {
                dataAccess.SetData(0, Geometry.Topologic.Convert.ToTopologic(face3D));
                return;
            }

            Shell shell = sAMGeometry as Shell;

            if (shell != null)
            {
                Brep brep = Rhino.Convert.ToRhino(shell);
                if (brep != null)
                {
                    dataAccess.SetData(0, brep.ToTopologic(Core.Tolerance.MacroDistance));
                    return;
                }
            }

            AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Cannot convert geometry");
        }
Ejemplo n.º 12
0
        public static double CalculateFacePitch(Face3D face)
        {
            if (face.Location == null || face.Mouth == null)
            {
                return(0);
            }

            return(CalculateAngle3D(face.Location.Value, face.Mouth.Value,
                                    new MCvPoint3D64f(face.Location.Value.x, face.Mouth.Value.y, face.Location.Value.z)));
        }
Ejemplo n.º 13
0
        public static double CalculateFaceYaw(Face3D face)
        {
            if (face.LeftEye == null || face.RightEye == null)
            {
                return(0);
            }

            return(CalculateAngle3D(face.LeftEye.Value, face.RightEye.Value,
                                    new MCvPoint3D64f(face.RightEye.Value.x, face.RightEye.Value.y, face.LeftEye.Value.z)));
        }
Ejemplo n.º 14
0
        public static double CalculateFaceRoll(Face3D face)
        {
            if (face.LeftEye == null || face.RightEye == null)
            {
                return(0);
            }

            return(CalculateAngle3D(face.LeftEye.Value, face.RightEye.Value,
                                    new MCvPoint3D64f(face.RightEye.Value.x, face.LeftEye.Value.y, face.RightEye.Value.z) /*the point directly under the right eye*/));
        }
Ejemplo n.º 15
0
        public static HashSet <Point3D> ExternalEdgePoint3Ds(this Face3D face3D, double tolerance = Core.Tolerance.Distance)
        {
            if (face3D == null)
            {
                return(null);
            }

            Face3D face3D_Temp = new Face3D(face3D);

            face3D_Temp.Normalize(tolerance);

            ISegmentable3D externalEdge = face3D_Temp.GetExternalEdge3D() as ISegmentable3D;

            if (externalEdge == null)
            {
                throw new NotImplementedException();
            }

            List <Point3D> point3Ds = externalEdge.GetPoints();

            if (point3Ds == null)
            {
                return(null);
            }

            //Vector3D normal = face3D.GetPlane().Normal;
            //if (normal == null)
            //    return null;

            //ISegmentable3D externalEdge = face3D.GetExternalEdge3D() as ISegmentable3D;
            //if (externalEdge == null)
            //    throw new NotImplementedException();

            //List<Point3D> point3Ds = externalEdge.GetPoints();
            //if (point3Ds == null)
            //    return null;

            //if (!Spatial.Query.Clockwise(point3Ds, normal, Core.Tolerance.Angle, tolerance))
            //    point3Ds.Reverse();

            HashSet <Point3D> result = new HashSet <Point3D>();

            if (point3Ds.Count == 0)
            {
                return(result);
            }

            for (int i = 0; i < point3Ds.Count; i++)
            {
                point3Ds[i].Round(tolerance);
                result.Add(point3Ds[i]);
            }

            return(result);
        }
Ejemplo n.º 16
0
        public static Face3D ToSAM(this HoneybeeSchema.Face3D face3D)
        {
            if (face3D == null)
            {
                return(null);
            }

            Plane plane = face3D.Plane?.ToSAM();

            IClosedPlanar3D externalEdge3D = null;

            List <Point3D> point3Ds = face3D.Boundary?.ToSAM();

            if (point3Ds == null || point3Ds.Count < 3)
            {
                return(null);
            }

            if (plane != null)
            {
                externalEdge3D = new Polygon3D(plane, point3Ds.ConvertAll(x => plane.Convert(x)));
            }
            else
            {
                externalEdge3D = new Polygon3D(point3Ds);
            }

            plane = externalEdge3D.GetPlane();
            if (plane == null)
            {
                return(null);
            }

            List <IClosedPlanar3D> internalEdge3Ds = null;

            if (face3D.Holes != null)
            {
                internalEdge3Ds = new List <IClosedPlanar3D>();
                foreach (List <List <double> > values in face3D.Holes)
                {
                    point3Ds = values?.ToSAM();
                    if (point3Ds == null || point3Ds.Count < 3)
                    {
                        continue;
                    }

                    internalEdge3Ds.Add(new Polygon3D(plane, point3Ds.ConvertAll(x => plane.Convert(x))));
                }
            }

            return(Face3D.Create(externalEdge3D.GetPlane(), plane.Convert(externalEdge3D), internalEdge3Ds?.ConvertAll(x => plane.Convert(x))));
        }
Ejemplo n.º 17
0
        public static global::Topologic.Face ToTopologic(this Face3D face3D)
        {
            IClosedPlanar3D closedPlanar3D = face3D.GetExternalEdge3D();

            if (closedPlanar3D is ICurvable3D)
            {
                List <Edge> edges = new List <Edge>();
                foreach (ICurve3D curve3D in ((ICurvable3D)closedPlanar3D).GetCurves())
                {
                    Edge edge = Edge.ByStartVertexEndVertex(ToTopologic(curve3D.GetStart()), ToTopologic(curve3D.GetEnd()));
                    edges.Add(edge);
                }

                return(global::Topologic.Face.ByEdges(edges));
            }
            return(null);
        }
Ejemplo n.º 18
0
        public static Plane ReferencePlane(this Face3D face3D, double tolerance = Core.Tolerance.Distance)
        {
            Plane plane = face3D?.GetPlane();

            if (plane == null)
            {
                return(null);
            }

            Vector3D normal = plane.Normal;

            Vector3D axis_Y = new Plane(plane, Point3D.Zero).Project(Vector3D.WorldZ);

            if (axis_Y.Length <= tolerance)
            {
                axis_Y = Vector3D.WorldY;
                if (normal.Z > 0)
                {
                    axis_Y.Negate();
                }
            }

            Vector3D axis_X = axis_Y.CrossProduct(normal);

            if (axis_X.Length <= tolerance)
            {
                axis_X = Vector3D.WorldX;
                if (normal.Z > 0)
                {
                    axis_X.Negate();
                }
            }

            Plane result = new Plane(plane.Origin, axis_X, axis_Y);

            List <Point2D> point2Ds = face3D.ExternalEdgePoint3Ds(tolerance)?.ToList().ConvertAll(x => result.Convert(x));

            if (point2Ds == null || point2Ds.Count == 0)
            {
                return(result);
            }

            Point2D point2D_Min = point2Ds.Min();

            return(new Plane(result.Convert(point2D_Min), axis_X, axis_Y));
        }
Ejemplo n.º 19
0
        public static Shade ToLadybugTools_Shade(this IPartition partition)
        {
            if (partition == null)
            {
                return(null);
            }

            Face3D face3D = partition.Face3D?.ToLadybugTools();

            if (face3D == null)
            {
                return(null);
            }

            ShadePropertiesAbridged shadePropertiesAbridged = new ShadePropertiesAbridged();

            Shade shade = new Shade(Query.UniqueName(partition), face3D, shadePropertiesAbridged, partition.Name);

            return(shade);
        }
Ejemplo n.º 20
0
        public static Shade ToLadybugTools_Shade(this Panel panel)
        {
            if (panel == null || panel.PanelType != PanelType.Shade)
            {
                return(null);
            }

            Face3D face3D = panel.GetFace3D(true)?.ToLadybugTools();

            if (face3D == null)
            {
                return(null);
            }

            ShadePropertiesAbridged shadePropertiesAbridged = new ShadePropertiesAbridged();

            Shade shade = new Shade(Query.UniqueName(panel), face3D, shadePropertiesAbridged, panel.Name);

            return(shade);
        }
Ejemplo n.º 21
0
        public static double Length(this Face3D face3D)
        {
            if (face3D == null)
            {
                return(double.NaN);
            }

            Planar.IClosed2D closed2D = face3D.ExternalEdge2D;
            if (closed2D == null)
            {
                return(double.NaN);
            }

            Planar.ISegmentable2D segmentable2D = closed2D as Planar.ISegmentable2D;
            if (segmentable2D == null)
            {
                throw new System.NotImplementedException();
            }

            return(Planar.Query.MaxDistance(segmentable2D.GetPoints(), out Planar.Point2D point2D_1, out Planar.Point2D point2D_2));
        }
Ejemplo n.º 22
0
        public static List <Panel> Panels(this List <Face> faces, PanelType panelType, Construction construction = null, double minArea = Tolerance.MacroDistance)
        {
            if (faces == null)
            {
                return(null);
            }

            List <ISAMGeometry3D> faces_SAM = new List <ISAMGeometry3D>();

            foreach (Face face in faces)
            {
                Face3D face3D = face.ToSAM();
                if (face3D == null)
                {
                    continue;
                }

                faces_SAM.Add(face3D);
            }

            return(Analytical.Create.Panels(faces_SAM, panelType, construction, minArea));
        }
Ejemplo n.º 23
0
        public static bool generate_Face3D(SdoGeometry geom, out Face3D face)
        {
            int[]    elInfo   = geom.ElemArrayOfInts;
            double[] ordArray = geom.OrdinatesArrayOfDoubles;

            int        noLoop      = elInfo.Length / 3; // first loop is the outerloop and the rest will be innerloop
            int        totalVerts  = ordArray.Length / 3;
            List <int> vertsInLoop = new List <int>();

            for (int i = 0; i < noLoop; i++)
            {
                if (i == noLoop - 1)
                {
                    vertsInLoop.Add((totalVerts - (elInfo[i * 3] - 1) / 3));
                }
                else
                {
                    vertsInLoop.Add((elInfo[(i + 1) * 3] - elInfo[i * 3]) / 3);
                }
            }

            int initPos = 0;
            List <List <Point3D> > vertLists = new List <List <Point3D> >();

            for (int i = 0; i < noLoop; i++)
            {
                List <Point3D> vertList = new List <Point3D>();
                for (int v = 0; v < vertsInLoop[i]; v++)
                {
                    initPos = elInfo[i * 3] - 1;
                    int     pos  = initPos + v * 3;
                    Point3D vert = new Point3D(ordArray[pos], ordArray[pos + 1], ordArray[pos + 2]);
                    vertList.Add(vert);
                }
                vertLists.Add(vertList);
            }
            face = new Face3D(vertLists);
            return(true);
        }
Ejemplo n.º 24
0
        public static double HydraulicDiameter(this Face3D face3D, double tolerance = Core.Tolerance.Distance)
        {
            Plane plane_Face3D = face3D?.GetPlane();

            if (plane_Face3D == null)
            {
                return(double.NaN);
            }
            Plane plane = Plane.WorldXY;

            if (plane.Perpendicular(plane_Face3D, tolerance))
            {
                return(0);
            }

            Face3D face3D_Project = plane.Project(face3D);

            if (face3D_Project == null || !face3D_Project.IsValid())
            {
                return(0);
            }

            Planar.ISegmentable2D segmentable2D = face3D_Project.ExternalEdge2D as Planar.ISegmentable2D;
            if (segmentable2D == null)
            {
                throw new System.NotImplementedException();
            }

            double area = face3D_Project.GetArea();

            if (area <= tolerance)
            {
                return(0);
            }

            //return (4 * area) / segmentable2D.GetLength();
            return(segmentable2D.GetLength());
        }
Ejemplo n.º 25
0
        public static List <List <Point3D> > InternalEdgesPoint3Ds(this Face3D face3D, double tolerance = Core.Tolerance.Distance)
        {
            if (face3D == null)
            {
                return(null);
            }

            List <IClosedPlanar3D> internalEdges = face3D.GetInternalEdge3Ds();

            if (internalEdges == null || internalEdges.Count == 0)
            {
                return(null);
            }

            List <List <Point3D> > result = new List <List <Point3D> >();

            foreach (IClosedPlanar3D closedPlanar3D in internalEdges)
            {
                ISegmentable3D internalEdge = closedPlanar3D as ISegmentable3D;
                if (internalEdge == null)
                {
                    throw new NotImplementedException();
                }

                List <Point3D> point3Ds = internalEdge.GetPoints();
                if (point3Ds == null || point3Ds.Count < 3)
                {
                    continue;
                }

                point3Ds.ForEach(x => x.Round(tolerance));
                result.Add(point3Ds);
            }

            return(result);
        }
Ejemplo n.º 26
0
        public static List <CurveLoop> ToRevit(this Face3D face3D, double tolerance = Core.Tolerance.MacroDistance)
        {
            if (face3D == null)
            {
                return(null);
            }

            List <CurveLoop> result = new List <CurveLoop>();

            foreach (IClosedPlanar3D closedPlanar3D in face3D.GetEdge3Ds())
            {
                List <Line> lines = closedPlanar3D.ToRevit(tolerance);
                if (lines == null)
                {
                    continue;
                }

                CurveLoop curveLoop = new CurveLoop();
                lines.ForEach(x => curveLoop.Append(x));
                result.Add(curveLoop);
            }

            return(result);
        }
        public void Init_HBObj_Test()
        {
            var face = new Face3D(
                new List <List <double> >()
            {
                new List <double>()
                {
                    0, 0, 0
                },
                new List <double>()
                {
                    0.5, 0.5, 0.5
                },
                new List <double>()
                {
                    1, 0, 0
                }
            });

            var door = new Door("mainEntrance", face);

            // Assert
            Assert.AreEqual(door.BoundaryCondition.Obj.GetType(), typeof(Outdoors));
        }
Ejemplo n.º 28
0
        private static Visual3D AddFace(Viewport3D viewport, Face3D face, Color color, Color? reflectColor = null)
        {
            Tuple<int[], Point3D[]> poly = face.GetPolygon(500);

            TriangleIndexed[] triangles = Math2D.GetTrianglesFromConvexPoly(poly.Item1, poly.Item2);

            return AddTriangles(viewport, triangles, color, reflectColor);
        }
Ejemplo n.º 29
0
        public static Face3D ToSAM(this global::Topologic.Face face)
        {
            if (face == null)
            {
                return(null);
            }

            Polygon3D polygon3D = null;

            Vector3D normal = new Vector3D(FaceUtility.NormalAtParameters(face, 0.5, 0.5));

            if (normal != null)
            {
                polygon3D = Spatial.Create.Polygon3D(normal, face.ExternalBoundary.Vertices?.ToList().ConvertAll(x => x.ToSAM()));
            }

            if (polygon3D == null)
            {
                polygon3D = ToSAM_Polygon3D(face.ExternalBoundary);
            }

            if (polygon3D == null)
            {
                return(null);
            }

            List <Polygon3D> polygon3Ds = new List <Polygon3D>()
            {
                polygon3D
            };

            IList <Wire> wires = face.InternalBoundaries;

            if (wires != null && wires.Count > 0)
            {
                foreach (Wire wire in wires)
                {
                    polygon3D = null;

                    if (normal != null)
                    {
                        polygon3D = Spatial.Create.Polygon3D(normal, wire.Vertices?.ToList().ConvertAll(x => x.ToSAM()));
                    }

                    if (polygon3D == null)
                    {
                        polygon3D = ToSAM_Polygon3D(wire);
                    }

                    if (polygon3D == null)
                    {
                        continue;
                    }

                    polygon3Ds.Add(polygon3D);
                }
            }

            Face3D result = Face3D.Create(polygon3Ds);

            return(result);
        }
Ejemplo n.º 30
0
        public static FamilyInstance ToRevit(this Aperture aperture, Document document, HostObject hostObject, Core.Revit.ConvertSettings convertSettings)
        {
            if (aperture == null || document == null)
            {
                return(null);
            }

            FamilyInstance result = convertSettings?.GetObject <FamilyInstance>(aperture.Guid);

            if (result != null)
            {
                return(result);
            }

            ApertureConstruction apertureConstruction = aperture.ApertureConstruction;

            if (apertureConstruction == null)
            {
                return(null);
            }

            FamilySymbol familySymbol = apertureConstruction.ToRevit(document, convertSettings);

            if (familySymbol == null)
            {
                familySymbol = Analytical.Query.DefaultApertureConstruction(hostObject.PanelType(), apertureConstruction.ApertureType).ToRevit(document, convertSettings); //Default Aperture Construction
            }
            if (familySymbol == null)
            {
                return(null);
            }

            Point3D point3D_Location = aperture.PlanarBoundary3D?.Plane?.Origin;

            if (point3D_Location == null)
            {
                return(null);
            }

            Level level = Geometry.Revit.Query.LowLevel(document, point3D_Location.Z);

            if (level == null)
            {
                return(null);
            }

            XYZ referenceDirection = new XYZ(0, 0, 0);

            if (hostObject is RoofBase)
            {
                Face3D face3D = aperture.GetFace3D();
                Geometry.Spatial.Plane plane = face3D.GetPlane();

                bool coplanar = plane.Coplanar(Geometry.Spatial.Plane.WorldXY);
                //if(coplanar)
                //{
                //    referenceDirection = new XYZ(0, 0, 1);
                //}

                result = document.Create.NewFamilyInstance(point3D_Location.ToRevit(), familySymbol, referenceDirection, hostObject, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                if (result == null)
                {
                    return(null);
                }

                //List<Geometry.Planar.Point2D> point2Ds = new List<Geometry.Planar.Point2D>();
                //IClosedPlanar3D closedPlanar3D = face3D.GetExternalEdge3D();
                //if (closedPlanar3D is ICurvable3D)
                //{
                //    List<ICurve3D> curve3Ds = ((ICurvable3D)closedPlanar3D).GetCurves();
                //    foreach (ICurve3D curve3D in curve3Ds)
                //    {
                //        ICurve3D curve3D_Temp = plane.Project(curve3D);
                //        point2Ds.Add(plane.Convert(curve3D_Temp.GetStart()));
                //    }
                //}

                //Geometry.Planar.Rectangle2D rectangle2D = Geometry.Planar.Create.Rectangle2D(point2Ds);
                Geometry.Planar.Rectangle2D rectangle2D = Analytical.Create.Rectangle2D(aperture.PlanarBoundary3D);
                if (rectangle2D == null)
                {
                    return(null);
                }

                document.Regenerate();
                result = document.GetElement(result.Id) as FamilyInstance;

                Vector3D handOrientation_FamilyInstance   = result.HandOrientation.ToSAM_Vector3D(false);
                Vector3D facingOrientation_FamilyInstance = result.FacingOrientation.ToSAM_Vector3D(false);


                double factor = 0;
                Geometry.Planar.Vector2D direction = rectangle2D.WidthDirection;
                if (!coplanar && Core.Query.Round(direction.Y) < 0)
                {
                    factor = System.Math.PI / 2;
                }

                Vector3D handOrienation_Aperture = plane.Convert(direction);

                Geometry.Spatial.Plane plane_FamilyInstance = new Geometry.Spatial.Plane(point3D_Location, handOrientation_FamilyInstance, facingOrientation_FamilyInstance);
                handOrienation_Aperture = plane_FamilyInstance.Project(handOrienation_Aperture);

                double angle = Geometry.Spatial.Query.SignedAngle(handOrientation_FamilyInstance, handOrienation_Aperture, plane.Normal);

                result.Location.Rotate(Line.CreateUnbound(point3D_Location.ToRevit(), plane.Normal.ToRevit(false)), angle + factor);
                //document.Regenerate();

                //BoundingBox3D boundingBox3D_familyInstance = familyInstance.BoundingBox3D();
                //BoundingBox3D boundingBox3D_Aperture = aperture.GetBoundingBox();
                //if(boundingBox3D_familyInstance.Min.Distance(boundingBox3D_Aperture.Min) > SAM.Core.Tolerance.MacroDistance)
                //    familyInstance.Location.Rotate(Line.CreateUnbound(point3D_Location.ToRevit(), plane.Normal.ToRevit(false)), System.Math.PI / 2);

                //Geometry.Planar.Rectangle2D rectangle2D = Geometry.Planar.Create.Rectangle2D(point2Ds);
                //Geometry.Planar.Vector2D direction = null;
                //if (rectangle2D.Height > rectangle2D.Width)
                //    direction = rectangle2D.HeightDirection;
                //else
                //    direction = rectangle2D.WidthDirection;

                //double angle = plane.Convert(direction).ToRevit(false).AngleTo(new XYZ(0, 1, 0));
                //angle = System.Math.PI  - angle;
                ////if (angle > System.Math.PI)
                ////    angle = -(angle - System.Math.PI);
                //if (direction.X < 0)
                //    angle = -angle;

                //familyInstance.Location.Rotate(Line.CreateUnbound(point3D_Location.ToRevit(), plane.Normal.ToRevit(false)), angle);
            }
            else
            {
                result = document.Create.NewFamilyInstance(point3D_Location.ToRevit(), familySymbol, hostObject, level, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
            }


            if (result == null)
            {
                return(null);
            }

            if (result.CanFlipHand)
            {
                document.Regenerate(); //This is needed to get flip correctly pushed to revit
                Vector3D axisX = result.HandOrientation.ToSAM_Vector3D(false);
                if (!axisX.SameHalf(aperture.Plane.AxisX))
                {
                    result.flipHand();
                }
            }

            if (result.CanFlipFacing)
            {
                document.Regenerate(); //This is needed to get flip correctly pushed to revit
                Vector3D normal = result.FacingOrientation.ToSAM_Vector3D(false);
                if (!normal.SameHalf(aperture.Plane.Normal))
                {
                    result.flipFacing();
                }
            }

            if (convertSettings.ConvertParameters)
            {
                Core.Revit.Modify.SetValues(result, aperture, new BuiltInParameter[] { BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM, BuiltInParameter.INSTANCE_HEAD_HEIGHT_PARAM, BuiltInParameter.FAMILY_LEVEL_PARAM, BuiltInParameter.SCHEDULE_LEVEL_PARAM });
                Core.Revit.Modify.SetValues(result, aperture, ActiveSetting.Setting);

                bool simplified = false;

                //Check if geometry is simplified
                if (!Geometry.Planar.Query.Rectangular(aperture.PlanarBoundary3D?.ExternalEdge2DLoop?.GetClosed2D(), Core.Tolerance.MacroDistance))
                {
                    simplified = true;
                }

                if (!simplified && result.Host is Autodesk.Revit.DB.Wall)
                {
                    Face3D face3D = aperture.GetFace3D();
                    Geometry.Spatial.Plane plane = face3D.GetPlane();

                    Geometry.Planar.Rectangle2D rectangle2D = Analytical.Create.Rectangle2D(aperture.PlanarBoundary3D);
                    if (rectangle2D != null)
                    {
                        Vector3D widthDirection  = plane.Convert(rectangle2D.WidthDirection);
                        Vector3D heightDirection = plane.Convert(rectangle2D.HeightDirection);

                        //TODO: Implement code for Tilted Walls
                        Vector3D vector3D_Z = Vector3D.WorldZ;

                        if (!widthDirection.AlmostSimilar(vector3D_Z) && !heightDirection.AlmostSimilar(vector3D_Z))
                        {
                            simplified = true;
                        }
                    }
                }

                Core.Revit.Modify.SetSimplified(result, simplified);
                Core.Revit.Modify.SetJson(result, aperture.ToJObject()?.ToString());
            }

            convertSettings?.Add(aperture.Guid, result);

            return(result);
        }
Ejemplo n.º 31
0
        private void ProcessFrameFindFaces()
        {
            var stereoCalibration = Options.StereoCalibrationOptions;

            if (stereoCalibration == null)
            {
                return;
            }

            var leftImageR  = new Image <Gray, byte>(_cameras[0].Image.Width, _cameras[0].Image.Height);
            var rightImageR = new Image <Gray, byte>(_cameras[1].Image.Width, _cameras[1].Image.Height);

            try
            {
                CvInvoke.cvRemap(_cameras[0].Image.Ptr, leftImageR.Ptr,
                                 stereoCalibration.MapXLeft, stereoCalibration.MapYLeft, 0, new MCvScalar(0));
            }
            catch (Exception ex)
            {
            }

            CvInvoke.cvRemap(_cameras[1].Image.Ptr, rightImageR.Ptr,
                             stereoCalibration.MapXRight, stereoCalibration.MapYRight, 0, new MCvScalar(0));

            // find first face points
            var leftFaceRegions  = Helper2D.GetFaceRegion2Ds(leftImageR, FaceWidth, FaceHeight, true, true);
            var rightFaceRegions = Helper2D.GetFaceRegion2Ds(rightImageR, FaceWidth, FaceHeight, true, true);

            FaceRegion2D leftFace;
            FaceRegion2D rightFace;

            if (leftFaceRegions != null &&
                rightFaceRegions != null &&
                (leftFace = leftFaceRegions.FirstOrDefault()) != null &&
                (rightFace = rightFaceRegions.FirstOrDefault()) != null)
            {
                if (leftFace.EyeAngle != 0)
                {
                    _leftRoll = leftFace.EyeAngle;
                }
                if (rightFace.EyeAngle != 0)
                {
                    _rightRoll = rightFace.EyeAngle;
                }

                var leftPoints  = new Point[4]; // face location, left eye, right eye, mouth
                var rightPoints = new Point[4];

                #region Points

                // face
                leftPoints[0]  = new Point(leftFace.Face.Location.X + leftFace.Face.Width / 2, leftFace.Face.Location.Y + leftFace.Face.Height / 2);
                rightPoints[0] = new Point(rightFace.Face.Location.X + rightFace.Face.Width / 2, rightFace.Face.Location.Y + rightFace.Face.Height / 2);

                // left eye
                if (leftFace.LeftEye != null && rightFace.LeftEye != null)
                {
                    leftPoints[1] = new Point(leftFace.Face.Location.X + leftFace.LeftEye.Location.X + leftFace.LeftEye.Width / 2,
                                              leftFace.Face.Location.Y + leftFace.LeftEye.Location.Y + leftFace.LeftEye.Height / 2);

                    rightPoints[1] = new Point(rightFace.Face.Location.X + rightFace.LeftEye.Location.X + rightFace.LeftEye.Width / 2,
                                               rightFace.Face.Location.Y + rightFace.LeftEye.Location.Y + rightFace.LeftEye.Height / 2);
                }

                // right eye
                if (leftFace.RightEye != null && rightFace.RightEye != null)
                {
                    leftPoints[2] = new Point(leftFace.Face.Location.X + leftFace.RightEye.Location.X + leftFace.RightEye.Width / 2,
                                              leftFace.Face.Location.Y + leftFace.RightEye.Location.Y + leftFace.RightEye.Height / 2);

                    rightPoints[2] = new Point(rightFace.Face.Location.X + rightFace.RightEye.Location.X + rightFace.RightEye.Width / 2,
                                               rightFace.Face.Location.Y + rightFace.RightEye.Location.Y + rightFace.RightEye.Height / 2);
                }

                // mouth
                if (leftFace.Mouth != null && rightFace.Mouth != null)
                {
                    leftPoints[3] = new Point(leftFace.Face.Location.X + leftFace.Mouth.Location.X + leftFace.Mouth.Width / 2,
                                              leftFace.Face.Location.Y + leftFace.Mouth.Location.Y + leftFace.Mouth.Height / 2);

                    rightPoints[3] = new Point(rightFace.Face.Location.X + rightFace.Mouth.Location.X + rightFace.Mouth.Width / 2,
                                               rightFace.Face.Location.Y + rightFace.Mouth.Location.Y + rightFace.Mouth.Height / 2);
                }

                #endregion

                #region Manual Point Cloud Calculation

                {
                    var pointCloud = new MCvPoint3D64f[leftPoints.Length];

                    #region Calculate Point Cloud

                    for (int i = 0; i < leftPoints.Length; i++)
                    {
                        if (leftPoints[i].X == 0 && leftPoints[i].Y == 0)
                        {
                            continue;
                        }

                        var d = rightPoints[i].X - leftPoints[i].X;

                        var X = leftPoints[i].X * stereoCalibration.Q[0, 0] + stereoCalibration.Q[0, 3];
                        var Y = leftPoints[i].Y * stereoCalibration.Q[1, 1] + stereoCalibration.Q[1, 3];
                        var Z = stereoCalibration.Q[2, 3];
                        var W = d * stereoCalibration.Q[3, 2] + stereoCalibration.Q[3, 3];

                        X = X / W;
                        Y = Y / W;
                        Z = Z / W;

                        leftImageR.Draw(string.Format("{0:0.0} {1:0.0} {2:0.0}", X, Y, Z), ref _font, leftPoints[i], new Gray(255));
                        rightImageR.Draw(string.Format("{0:0.0} {1:0.0} {2:0.0}", X, Y, Z), ref _font, rightPoints[i], new Gray(255));

                        pointCloud[i] = new MCvPoint3D64f(X, Y, Z);
                    }

                    #endregion


                    _foundFace3d = new Face3D()
                    {
                        Location = pointCloud[0].x == 0 && pointCloud[0].y == 0 && pointCloud[0].z == 0 ? (MCvPoint3D64f?)null : pointCloud[0],
                        LeftEye  = pointCloud[1].x == 0 && pointCloud[1].y == 0 && pointCloud[1].z == 0 ? (MCvPoint3D64f?)null : pointCloud[1],
                        RightEye = pointCloud[2].x == 0 && pointCloud[2].y == 0 && pointCloud[2].z == 0 ? (MCvPoint3D64f?)null : pointCloud[2],
                        Mouth    = pointCloud[3].x == 0 && pointCloud[3].y == 0 && pointCloud[3].z == 0 ? (MCvPoint3D64f?)null : pointCloud[3],
                    };

                    if (_foundFace3d.LeftEye != null &&
                        _foundFace3d.RightEye != null &&
                        _foundFace3d.Mouth != null)
                    {
                        var srcMatrix = new Matrix <float>(3, 4);

                        srcMatrix[0, 0] = (float)_foundFace3d.LeftEye.Value.x;
                        srcMatrix[1, 0] = (float)_foundFace3d.LeftEye.Value.y;
                        srcMatrix[2, 0] = (float)_foundFace3d.LeftEye.Value.z;

                        srcMatrix[0, 1] = (float)_foundFace3d.RightEye.Value.x;
                        srcMatrix[1, 1] = (float)_foundFace3d.RightEye.Value.y;
                        srcMatrix[2, 1] = (float)_foundFace3d.RightEye.Value.z;

                        srcMatrix[0, 2] = (float)_foundFace3d.Mouth.Value.x;
                        srcMatrix[1, 2] = (float)_foundFace3d.Mouth.Value.y;
                        srcMatrix[2, 2] = (float)_foundFace3d.Mouth.Value.z;

                        srcMatrix[0, 3] = (float)_foundFace3d.Location.Value.x;
                        srcMatrix[1, 3] = (float)_foundFace3d.Location.Value.y;
                        srcMatrix[2, 3] = (float)_foundFace3d.Location.Value.z;


                        var dstMatrix = new Matrix <float>(3, 4);

                        dstMatrix[0, 0] = (float)_foundFace3d.LeftEye.Value.x;
                        dstMatrix[1, 0] = (float)_foundFace3d.LeftEye.Value.y;
                        dstMatrix[2, 0] = (float)30;

                        dstMatrix[0, 1] = (float)_foundFace3d.RightEye.Value.x;
                        dstMatrix[1, 1] = (float)_foundFace3d.RightEye.Value.y;
                        dstMatrix[2, 1] = (float)30;

                        dstMatrix[0, 2] = (float)_foundFace3d.Mouth.Value.x;
                        dstMatrix[1, 2] = (float)_foundFace3d.Mouth.Value.y;
                        dstMatrix[2, 2] = (float)30;

                        dstMatrix[0, 3] = (float)_foundFace3d.Location.Value.x;
                        dstMatrix[1, 3] = (float)_foundFace3d.Location.Value.y;
                        dstMatrix[2, 3] = (float)30;

                        HomographyMatrix homographyMatrix = CameraCalibration.FindHomography(srcMatrix, dstMatrix, HOMOGRAPHY_METHOD.DEFAULT, 1);

                        if (homographyMatrix != null)
                        {
                            try
                            {
                                leftImageR = leftImageR.WarpPerspective(homographyMatrix, INTER.CV_INTER_LINEAR, WARP.CV_WARP_DEFAULT, new Gray(0));
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }

                #endregion

                #region Automatic Point Cloud

                {
                    _imagePointsDisparity = new Image <Gray, byte>(_cameras[0].Image.Width, _cameras[0].Image.Height);
                    _imagePointsLeft      = new Image <Gray, byte>(_cameras[0].Image.Width, _cameras[0].Image.Height, new Gray(255));
                    _imagePointsRight     = new Image <Gray, byte>(_cameras[0].Image.Width, _cameras[0].Image.Height, new Gray(255));

                    for (int i = 0; i < leftPoints.Length; i++)
                    {
                        if (leftPoints[i].X == 0 && leftPoints[i].Y == 0)
                        {
                            continue;
                        }

                        _imagePointsLeft.Draw(new Rectangle(new Point(leftPoints[i].X, leftPoints[i].Y), new Size(10, 10)), new Gray(0), 10);
                        _imagePointsRight.Draw(new Rectangle(new Point(rightPoints[i].X, rightPoints[i].Y), new Size(10, 10)), new Gray(0), 10);
                    }

                    var imagePointsDisparityGpu = new GpuImage <Gray, byte>(_imagePointsDisparity);

                    _stereoSolver.FindStereoCorrespondence(new GpuImage <Gray, byte>(_imagePointsLeft), new GpuImage <Gray, byte>(_imagePointsRight),
                                                           imagePointsDisparityGpu, null);

                    _imagePointsDisparity = imagePointsDisparityGpu.ToImage();



                    //MCvPoint3D32f[] pointCloud = PointCollection.ReprojectImageTo3D(_imagePointsDisparity, stereoCalibration.Q);

                    //var filteredPointCloud = pointCloud.
                    //    Where(item => item.z != 10000).
                    //    GroupBy(item => item.z).
                    //    Select(item => new
                    //    {
                    //        z = item.Key,
                    //        x = item.Average(point => point.x),
                    //        y = item.Average(point => point.y)
                    //    }).ToArray();

                    //for (int i = 0; i < filteredPointCloud.Length; i++)
                    //{
                    //    _imagePointsDisparity.Draw(string.Format("{0:0.0} {1:0.0} {2:0.0}", filteredPointCloud[i].x, filteredPointCloud[i].y, filteredPointCloud[i].z),
                    //        ref _font, new Point((int)filteredPointCloud[i].x, (int)filteredPointCloud[i].y), new Gray(255));
                    //}
                }

                #endregion
            }

            var oldLeft  = _cameras[0].Image;
            var oldRight = _cameras[1].Image;

            _cameras[0].Image = leftImageR;
            _cameras[1].Image = rightImageR;

            oldLeft.Dispose();
            oldRight.Dispose();
        }
Ejemplo n.º 32
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var s = System.Windows.Application.GetResourceStream(Source);
            if (s == null)
                throw new ArgumentException("Object not Found", "Source");

            using (s.Stream)
            using (var sr = new StreamReader(s.Stream, Encoding.ASCII))
            {
                MeshGeometry3D geom = new MeshGeometry3D();

                List<Point3D> verts = new List<Point3D>();
                List<Vector3D> normals = new List<Vector3D>();
                List<Face3D> faces = new List<Face3D>();

                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Length == 0 || line[0] == '#')
                        continue;

                    var ls = line.Split(' ');
                    switch (ls[0])
                    {
                        case "v":
                            {
                                float x, y, z;
                                float.TryParse(ls[1], NumberStyles.Any, CultureInfo.InvariantCulture, out x);
                                float.TryParse(ls[2], NumberStyles.Any, CultureInfo.InvariantCulture, out y);
                                float.TryParse(ls[3], NumberStyles.Any, CultureInfo.InvariantCulture, out z);
                                Point3D p = new Point3D(x, y, z);
                                verts.Add(p);
                                break;
                            }
                        case "vn":
                            {
                                float x, y, z;
                                float.TryParse(ls[1], NumberStyles.Any, CultureInfo.InvariantCulture, out x);
                                float.TryParse(ls[2], NumberStyles.Any, CultureInfo.InvariantCulture, out y);
                                float.TryParse(ls[3], NumberStyles.Any, CultureInfo.InvariantCulture, out z);
                                Vector3D p = new Vector3D(x, y, z);
                                normals.Add(p);
                                break;
                            }
                        case "f":
                            {
                                var v0 = ls[1].Split('/');
                                var v1 = ls[2].Split('/');
                                var v2 = ls[3].Split('/');
                                Face3D f = new Face3D
                                {
                                    A = new Vertex3D(verts[int.Parse(v0[0]) - 1], normals[int.Parse(v0[2]) - 1], new Vector3D()),
                                    B = new Vertex3D(verts[int.Parse(v1[0]) - 1], normals[int.Parse(v1[2]) - 1], new Vector3D()),
                                    C = new Vertex3D(verts[int.Parse(v2[0]) - 1], normals[int.Parse(v2[2]) - 1], new Vector3D()),
                                };
                                faces.Add(f);
                                break;
                            }
                    }
                }

                int i = 0;
                foreach (var face3D in faces)
                {
                    geom.Positions.Add(face3D.A.Position);
                    geom.Positions.Add(face3D.B.Position);
                    geom.Positions.Add(face3D.C.Position);

                    geom.Normals.Add(face3D.A.Normal);
                    geom.Normals.Add(face3D.B.Normal);
                    geom.Normals.Add(face3D.C.Normal);

                    geom.TriangleIndices.Add(i++);
                    geom.TriangleIndices.Add(i++);
                    geom.TriangleIndices.Add(i++);
                }

                geom.Freeze();

                return geom;
            }
        }