寻找房间中心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的更多相关文章
- 配置中心 Spring Cloud config
配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. 1.服务端 创建spring boot 项目 主要依赖 <dependenc ...
- Hadoop Web项目--Friend Find系统
项目使用软件:Myeclipse10.0,JDK1.7,Hadoop2.6,MySQL5.6.EasyUI1.3.6.jQuery2.0,Spring4.1.3. Hibernate4.3.1,str ...
- PCA and kmeans MATLAB实现
MATLAB基础知识 l Imread: 读取图片信息: l axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和 ...
- k-means算法初识
基础知识: K-means聚类算法 聚类,简单地说就是把相似的东西分到一组.同 Classification (分类)不同,对于一个 classifier ,通常需要你告诉它“这个东西被分为某某类”. ...
- 单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)
对于单链表来说,判断回文最简单的方法就是遍历链表,将链表中的元素复制到数组中,然后对数组进行判断是否是回文数组,但是这不符合O(1)的空间复杂度. 由于空间复杂度的要求,需要就地操作链表,不能开辟多余 ...
- 聚类算法:K-means 算法(k均值算法)
k-means算法: 第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设 ...
- MLlib 中的聚类和分类
聚类和分类是机器学习中两个常用的算法,聚类将数据分开为不同的集合,分类对新数据进行类别预测,下面将就两类算法进行介绍. 1. 聚类和分类(1)什么是聚类 聚类( Clustering)指将数据对象分组 ...
- opencv2对读书笔记——使用均值漂移算法查找物体
一些小概念 1.反投影直方图的结果是一个概率映射,体现了已知图像内容出如今图像中特定位置的概率. 2.概率映射能够找到最初的位置,从最初的位置開始而且迭代移动,便能够找到精确的位置,这就是均值漂移算法 ...
- Spark:聚类算法
Spark:聚类算法 Kmeans聚类 KMeans算法的基本思想是初始随机给定K个簇中心,按照最邻近原则把待分类样本点分到各个簇.然后按平均法重新计算各个簇的质心,从而确定新的簇心.一直迭代,直到簇 ...
随机推荐
- chaper3_exerise_UVa455_周期串
#include<iostream> #include<cstring> #include<stdio.h> using namespace std; ; int ...
- JS_ECMA基本语法中的几种封装的小函数-2
大家好!今天继续给大家写一下ECMA中的剩下的小函数以及实用的实例: 首先先给大家说一下字符串.数组.数学方法以及json的一点小知识点: 字符串方法: str.length str.charAt(i ...
- 10.11 cocoapods安装
手动安装gem 手动下载 rubygem https://rubygems.org/pages/download#formats 10.11 cocoapods安装 sudo gem install ...
- DB2 create partitioned table
在Z上和开放平台上的创建方法还不太一样,两套人马开发出来的就是牛! 蛋疼…… 贴不同类型的几个例子感受一下,Z上的ASC,DESC不见了: CREATE TABLE foo(a INT) PARTIT ...
- 二、JavaScript语言--JS基础--JavaScript进阶篇--DOM对象 控制HTML元素
1.认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面 ...
- Linux 通过os进程pid找到端口号
[root@xxxx conf]# netstat -lutpn|grep tcp /java tcp /java tcp /java tomcat 监听的一些端口
- jquery学习笔记-----ajax
$(selector).load( url [,date] [,callback] ) url:请求页面的url地址 date:发送至服务器的key:value数据 callback:请求完成时的回调 ...
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...
- 【JAVA 其它流对象】
一.PrintStream类. 该流是字节流. public class PrintStream extends FilterOutputStream implements Appendable, C ...
- 图结构练习——最小生成树(prim算法(普里姆))
图结构练习——最小生成树 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 有n个城市,其中有些城市之间可以修建公路,修建不同 ...