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的更多相关文章

  1. 配置中心 Spring Cloud config

    配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. 1.服务端 创建spring boot 项目 主要依赖 <dependenc ...

  2. Hadoop Web项目--Friend Find系统

    项目使用软件:Myeclipse10.0,JDK1.7,Hadoop2.6,MySQL5.6.EasyUI1.3.6.jQuery2.0,Spring4.1.3. Hibernate4.3.1,str ...

  3. PCA and kmeans MATLAB实现

    MATLAB基础知识 l  Imread:  读取图片信息: l  axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和  ...

  4. k-means算法初识

    基础知识: K-means聚类算法 聚类,简单地说就是把相似的东西分到一组.同 Classification (分类)不同,对于一个 classifier ,通常需要你告诉它“这个东西被分为某某类”. ...

  5. 单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)

    对于单链表来说,判断回文最简单的方法就是遍历链表,将链表中的元素复制到数组中,然后对数组进行判断是否是回文数组,但是这不符合O(1)的空间复杂度. 由于空间复杂度的要求,需要就地操作链表,不能开辟多余 ...

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

    k-means算法:      第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设 ...

  7. MLlib 中的聚类和分类

    聚类和分类是机器学习中两个常用的算法,聚类将数据分开为不同的集合,分类对新数据进行类别预测,下面将就两类算法进行介绍. 1. 聚类和分类(1)什么是聚类 聚类( Clustering)指将数据对象分组 ...

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

    一些小概念 1.反投影直方图的结果是一个概率映射,体现了已知图像内容出如今图像中特定位置的概率. 2.概率映射能够找到最初的位置,从最初的位置開始而且迭代移动,便能够找到精确的位置,这就是均值漂移算法 ...

  9. Spark:聚类算法

    Spark:聚类算法 Kmeans聚类 KMeans算法的基本思想是初始随机给定K个簇中心,按照最邻近原则把待分类样本点分到各个簇.然后按平均法重新计算各个簇的质心,从而确定新的簇心.一直迭代,直到簇 ...

随机推荐

  1. SQLServer视图

    视图简介:通过定义 SELECT 语句以检索将在视图中显示的数据来创建视图.SELECT 语句引用的数据表称为视图的基表.在SQL Server 2005系统中,可以把视图分为3种类型,即标准视图,索 ...

  2. SQLServer基本操作

    SQL 全名是结构化查询语言(Structured Query Language),是关系数据库管理系统的标准语言 1.分离数据库:将当前数据库文件和数据库引擎的关系断开,没有任何关系了,这样就可以随 ...

  3. Linux系统下设置Tomcat自启动

    需要将tomcat加入自启动队列中,则需要进行如下的操作: 以root用户登录系统: cd /etc/rc.d/init.d/ vi tomcat 文件内容参考如下: #!/bin/sh # # to ...

  4. PHP常用函数大全。

    php usleep() 函数延迟代码执行若干微秒. unpack() 函数从二进制字符串对数据进行解包. uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID. time_sleep_ ...

  5. nc 显示服务器开放的端口

    # nc -z -w xxxx.com - Connection to xxxx.com port [tcp/ftp] succeeded! Connection to xxxx.com port [ ...

  6. MVC – 3.EF(Entity Framework)

    1.实体框架(EF)简介 与ADO.NET的关系 全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Relational Mapping)框 ...

  7. 与你相遇好幸运,Sail.js新建模型控制器

    sails generate api user  创建了user的controller和models sails generate api user index 创建了user的controller和 ...

  8. C#值类型与引用类型

    值类型(Value Type),值类型实例通常分配在线程的堆栈(stack)上,并且不包含任何指向实例数据的指针,因为变量本身就包含了其实例数据.其在MSDN的定义为值类型直接包含它们的数据,值类型的 ...

  9. Linux性能分析工具的安装和使用

    转自:http://blog.chinaunix.net/uid-26488891-id-3118279.html Normal 0 7.8 磅 0 2 false false false EN-US ...

  10. 第一部分:使用iReport制作报表的详细过程(Windows环境下)

    提示:在有些板块,文中的图片看不到,建议到我的blog浏览文章:http://blog.csdn.net/jemlee2002/文章将会涉及3个方面的内容: 第一部分:使用iReport制作报表的详细 ...