寻找房间中心zz

Finding the Centroid of a Room Boundary

It‘s been a while since my last post and I‘m sure most of you were like... "Where the hell is Don!".... it‘s ok! I‘m still around. I‘ve been busy working on stuff I can‘t talk about. Don‘t worry though, I‘m not a good secret keeper.
So this post is going to explain something that a bunch of folks have issues with that involves finding the actual centroid of a polygon, or in this case a Room element. Now let‘s be careful not to confuse a centroid with a pair of midpoints taken from the furthest X and Y planes... a centroid is more closely described as the center of mass within a polygon.

Now this is done by taking a series of 2D points and running some tricky math on them and dividing the points by 6x the area of the polygon. So to make it simple for you guys, I‘ve taken the liberty of sharing a couple of functions that makes this all possible. The samples here are in Revit 2011 format.
First you‘ll need a function that iterates through the boundary segments of a Room Element and builds up a series of 2D points taken from either endpoints of each segment (no need to worry about curved segments since they usually wont effect the centroid too much, or you can add the midpoint of the curve arc to get it closer).
This little Function will return a list of 2D PointF from a boundary of a Room element.

‘‘‘ <summary>
    ‘‘‘ Extract a List of 2D Points from a Room‘s Boundary
    ‘‘‘ </summary>
    ‘‘‘ <param name="p_room"></param>
    ‘‘‘ <remarks></remarks>
    Private Sub ExtractBoundaryPointsFromRoom(p_room As Room)
        ‘ The Points List
        Dim m_pts As New List(Of PointF)
        ‘ The Z Height
        Dim m_z As Double = 0
        ‘ Work with the Boundary
        Dim m_bsaa As Autodesk.Revit.DB.Architecture.BoundarySegmentArrayArray = m_room.Boundary
        ‘ Segment Array at Floor Level
        For Each bsa As Autodesk.Revit.DB.Architecture.BoundarySegmentArray In m_bsaa
            Try
                For Each bs As Autodesk.Revit.DB.Architecture.BoundarySegment In bsa
                    Dim m_c As Curve = bs.Curve
                    ‘ First Endpoint
                    Dim m_EndPoint1 As XYZ = m_c.EndPoint(0)
                    Dim m_PointF1 As New PointF(m_EndPoint1(0), m_EndPoint1(1))
                    m_pts.Add(m_PointF1)
                    ‘ Second Endpoint
                    Dim m_EndPoint2 As XYZ = m_c.EndPoint(1)
                    Dim m_PointF2 As New PointF(m_EndPoint2(0), m_EndPoint2(1))
                    m_pts.Add(m_PointF2)
                    ‘ The Height
                    m_z = m_EndPoint1(2)
                Next
            Catch ex As Exception

            End Try
        Next
        ‘ Return the 2D Centroid
        Dim m_2Dcentroid As PointF = FindCentroid(m_pts.ToArray, m_room.Area)
        ‘ Add the Floor Level of Boundary for Z Elevation
        InsertionPoint = New XYZ(m_2Dcentroid.X, m_2Dcentroid.Y, m_z)
    End Sub

The Function below will take a list of points (first gathered from the segments array of a room) and convert them to a real life centroid in 2D format. The Z elevation is pretty easy to figure out for a room and what ever you‘re using this for is typically going to use 0 or a preset elevation for the result anyway.

‘‘‘ <summary>
    ‘‘‘ Find 2D Centroid
    ‘‘‘ </summary>
    ‘‘‘ <param name="pts">Collection of Points Describing the Polygon</param>
    ‘‘‘ <param name="p_rmArea">The Area of the Polygon</param>
    ‘‘‘ <returns>2D Point (Pointf)</returns>
    ‘‘‘ <remarks>This Function Kicks Ass</remarks>
    Private Function FindCentroid(ByVal pts() As PointF, p_rmArea As Single) As PointF
        ‘ Add the First PT to the End of the Array (full circulation)
        ReDim Preserve pts(pts.Length)
        pts(pts.Length - 1) = New PointF(pts(0).X, pts(0).Y)
        ‘ Get the Centroid
        Dim X As Single = 0
        Dim Y As Single = 0
        Dim m_sf As Single
        ‘ This is Where the Magic Happens
        For i As Integer = 0 To pts.Length - 2
            m_sf = pts(i).X * pts(i + 1).Y - pts(i + 1).X * pts(i).Y
            X += (pts(i).X + pts(i + 1).X) * m_sf
            Y += (pts(i).Y + pts(i + 1).Y) * m_sf
        Next i
        ‘ Divide by 6X the Are of the Polygon
        X /= (6 * p_rmArea)
        Y /= (6 * p_rmArea)
        ‘ This is the Final Result
        Return New PointF(X, Y)
    End Function

That‘s all until next time...

寻找房间中心zz

时间: 2024-09-30 20:02:03

寻找房间中心zz的相关文章

12. Dubbo原理解析-注册中心之基于dubbo协议的简单注册中心实现

基于dubbo协议开源只是给出了默认一个注册中心实现SimpleRegistryService, 它只是一个简单实现,不支持集群,就是利用Map<String/*ip:port*/, Map<String/*service*/, URL>来存储服务地址, 具体不在啰嗦了,请读者翻看源代码,可作为自定义注册中的参考. 注册中心启动 SimpleRegistryService本身也是作为一个dubbo服务暴露. <dubbo:protocolport="9090"

用分治和递归的思想——寻找最大子数组

寻找最大子数组的问题可描述为 输入: 一个数组,一个低位,一个高位 输出: 数组中从低位到高位中,连续和最大是多少 首先能想到的最直接的办法是暴力解决,遍历所有可能的序列组合,如果有n个元素,则需遍历的子序列有,复杂度为n2,稍有些经验的就能马上意识到,有很多重复计算在里面,比如最长的子序列计算,包含了之前所有子序列的计算.接下来我们使用分治的思想求解这个最大子序列,前一篇博文提过,分治的思想是将大问题分解为同等类型的子问题,再将子问题求解,然后合并子问题得出原问题的解,其中用到了递归的思想,因

场景应用:传统行业的再生之路

今天,许多传统行业都认识到,要么你被移动互联网改变,要么你主动发起这种改变.这种商业观念的变革,很大程度上源于移动互联网对传统行业愈演愈烈的改变上. 第一类:在传统行业中搭建全新中介平台 也就是移动互联网在传统的.买卖信息严重不对称的生意模式中挤入,搭建一个全新的中介平台. 大量的传统生意之所以存在,其实往往是建立在某种市场特权的垄断,或利用了买卖双方的信息不对称.而互联网的出现开始急剧地填平这种不对称的信息鸿沟.大名鼎鼎的淘宝.京东.携程,严格说都是硬生生地挤入进来,成为传统生意的中介平台,用

Spark MLlib KMeans聚类算法

1.1 KMeans聚类算法 1.1.1 基础理论 KMeans算法的基本思想是初始随机给定K个簇中心,按照最邻近原则把待分类样本点分到各个簇.然后按平均法重新计算各个簇的质心,从而确定新的簇心.一直迭代,直到簇心的移动距离小于某个给定的值. K-Means聚类算法主要分为三个步骤: (1)第一步是为待聚类的点寻找聚类中心: (2)第二步是计算每个点到聚类中心的距离,将每个点聚类到离该点最近的聚类中去: (3)第三步是计算每个聚类中所有点的坐标平均值,并将这个平均值作为新的聚类中心: 反复执行(

kmeans学习与实践

KMeans算法的基本思想是初始随机给定K个簇中心,按照最邻近原则把待分类样本点分到各个簇.然后按平均法重新计算各个簇的质心,从而确定新的簇心.一直迭代,直到簇心的移动距离小于某个给定的值.  K-Means聚类算法主要分为三个步骤:(1)第一步是为待聚类的点寻找聚类中心(2)第二步是计算每个点到聚类中心的距离,将每个点聚类到离该点最近的聚类中去(3)第三步是计算每个聚类中所有点的坐标平均值,并将这个平均值作为新的聚类中心反复执行(2).(3),直到聚类中心不再进行大范围移动或者聚类次数达到要求

仙境传说-按键精灵脚步研究

仙境传说是很早以前非常流行的一款网络游戏,其中的人物设置的非常可爱,尤其是一些夸张的头饰和百变的插卡系统让人回味无穷.虽然从游戏性来说仍然逃不出韩国游戏泡菜的怪圈(或者说是一款标准的泡菜游戏),但仍然是可圈可点的. 游戏中玩家要耗费大量时间进行练级,虽然现在的代理昆仑也有经验奖励的措施,比如高级经验书,高级JOB经验书,双倍经验时间区等,但是要让玩家,特别是高等级的玩家进行练级仍然是一件非常痛苦的事情.常有的事是,三转100~110的职业往往要在熔岩地图上打一个熔岩波利的怪物来进行性价比较高的练

PCA and kmeans MATLAB实现

MATLAB基础知识 l  Imread:  读取图片信息: l  axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和 z 轴范围以及颜色缩放范围(请参阅 caxis).v = axis 返回包含 x.y 和 z 轴缩放因子的行矢量.v 具有 4 或 6 个分量,具体分别取决于当前坐标轴是二维还是三维.返回值是当前坐标轴的 XLim.Ylim 和 ZLim 属性.   基于 x.y 和 z 数据的最小值和最大值,ax

聚类算法:K-means 算法(k均值算法)

k-means算法:      第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设定,例如可选开始的$K$个模式样本的向量值作为初始聚类中心.      第二步:逐个将需分类的模式样本$\{x\}$按最小距离准则分配给$K$个聚类中心中的某一个$z_j(1)$.假设$i=j$时, \[D_j (k) = \min \{ \left\| {x - z_i (k)} \right\|

opencv2对读书笔记——使用均值漂移算法查找物体

一些小概念 1.反投影直方图的结果是一个概率映射,体现了已知图像内容出如今图像中特定位置的概率. 2.概率映射能够找到最初的位置,从最初的位置開始而且迭代移动,便能够找到精确的位置,这就是均值漂移算法做的事情. 3.均值漂移算法是以迭代的方式锁定函数的局部最大值的. 关于均值漂移算法的过程(opencv) 事实上均值漂移算法就是寻找提前定义寻找区域中数据点的重心,或者说加权平均值.将寻找区域中心移动到数据点的重心处,并反复这个过程直到寻找区域重心收敛到一个稳定点. OpenCV中定义了两种终止条