1. #include <boost/assign.hpp>
  2. #include <boost/geometry/core/point_type.hpp>
  3. #include <boost/geometry/geometry.hpp>
  4. #include <boost/geometry/geometries/point_xy.hpp>
  5. #include <boost/geometry/geometries/linestring.hpp>
  6. #include <boost/geometry/geometries/box.hpp>
  7. #include <boost/geometry/geometries/ring.hpp>
  8. #include <boost/geometry/geometries/polygon.hpp>
  9.  
  10. #include <boost/geometry/algorithms/transform.hpp>
  11. #include <boost/geometry/strategies/transform/inverse_transformer.hpp>
  12. #include <boost/geometry/strategies/transform/matrix_transformers.hpp>
  13.  
  14. namespace bg = boost::geometry;
  15. typedef bg::model::d2::point_xy<double> DPoint;
  16. typedef bg::model::segment<DPoint> DSegment;
  17. typedef bg::model::linestring<DPoint> DLineString;
  18. typedef bg::model::box<DPoint> DBox;
  19. typedef bg::model::ring<DPoint, false> DRing;
  20. typedef bg::model::polygon<DPoint, false> DPolygon;
  21. //对Geometry库一些函数进行了封装,更加方便使用,包括(平移,旋转,缩放,求点到直线的垂足,通过比例求段上的点等等)
  22. namespace GeometryExtend
  23. {
  24. DPoint operator +(const DPoint& pt1, const DPoint& pt2)
  25. {
  26. DPoint pt(pt1);
  27. bg::add_point(pt, pt2);
  28. return pt;
  29. }
  30.  
  31. const DPoint& operator +=(DPoint& pt1, const DPoint& pt2)
  32. {
  33. bg::add_point(pt1, pt2);
  34. return pt1;
  35. }
  36.  
  37. DPoint operator -(const DPoint& pt1, const DPoint& pt2)
  38. {
  39. DPoint pt(pt1);
  40. bg::subtract_point(pt, pt2);
  41. return pt;
  42. }
  43.  
  44. const DPoint& operator -=(DPoint& pt1, const DPoint& pt2)
  45. {
  46. bg::subtract_point(pt1, pt2);
  47. return pt1;
  48. }
  49.  
  50. //////////////////////////////////////////////////////////////////////////
  51. // 平移变换
  52. //////////////////////////////////////////////////////////////////////////
  53. template<typename Geometry, typename CalculationType>
  54. struct translate_impl
  55. {
  56. static void apply(Geometry& geometry, const CalculationType& x, const CalculationType& y)
  57. {
  58. bg::strategy::transform::translate_transformer<CalculationType, 2, 2> t(x, y);
  59. bg::transform(geometry, geometry, t);
  60. }
  61. };
  62.  
  63. template<typename Geometry, typename CalculationType>
  64. struct translate_trais
  65. {
  66. static void apply(Geometry& geometry, const CalculationType& x, const CalculationType& y);
  67. };
  68.  
  69. #define TRANSLATE_TRAIS(Geometry) \
  70. template<typename CalculationType> \
  71. struct translate_trais<Geometry, CalculationType> \
  72. { \
  73. static void apply(Geometry& geometry, const CalculationType& x, const CalculationType& y) \
  74. { \
  75. translate_impl<Geometry, CalculationType>::apply(geometry, x, y); \
  76. } \
  77. };
  78.  
  79. TRANSLATE_TRAIS(DPoint)
  80. TRANSLATE_TRAIS(DSegment)
  81. TRANSLATE_TRAIS(DBox)
  82. TRANSLATE_TRAIS(DRing)
  83. TRANSLATE_TRAIS(DPolygon)
  84.  
  85. template<typename Geometry, typename CalculationType>
  86. void translate(Geometry& geometry, const CalculationType& x, const CalculationType& y)
  87. {
  88. translate_trais<Geometry, CalculationType>::apply(geometry, x, y);
  89. }
  90.  
  91. //////////////////////////////////////////////////////////////////////////
  92. // 旋转变换
  93. //////////////////////////////////////////////////////////////////////////
  94. template<typename Geometry, typename DegreeOrRadian, typename CalculationType>
  95. struct rotate_impl
  96. {
  97. static void apply(Geometry& geometry, const CalculationType& angle)
  98. {
  99. bg::strategy::transform::rotate_transformer<DegreeOrRadian, CalculationType, 2, 2> t(angle);
  100. bg::transform(geometry, geometry, t);
  101. }
  102. };
  103.  
  104. template<typename Geometry, typename DegreeOrRadian, typename CalculationType>
  105. struct rotate_trais
  106. {
  107. static void apply(Geometry& geometry, const CalculationType& angle);
  108. };
  109.  
  110. #define ROTATE_TRAIS(Geometry, DegreeOrRadian) \
  111. template<typename CalculationType> \
  112. struct rotate_trais<Geometry, DegreeOrRadian, CalculationType> \
  113. { \
  114. static void apply(Geometry& geometry, const CalculationType& angle) \
  115. { \
  116. rotate_impl<Geometry, DegreeOrRadian, CalculationType>::apply(geometry, angle); \
  117. } \
  118. };
  119.  
  120. ROTATE_TRAIS(DPoint, bg::degree)
  121. ROTATE_TRAIS(DPoint, bg::radian)
  122. ROTATE_TRAIS(DSegment, bg::degree)
  123. ROTATE_TRAIS(DSegment, bg::radian)
  124. ROTATE_TRAIS(DBox, bg::degree)
  125. ROTATE_TRAIS(DBox, bg::radian)
  126. ROTATE_TRAIS(DRing, bg::degree)
  127. ROTATE_TRAIS(DRing, bg::radian)
  128. ROTATE_TRAIS(DPolygon, bg::degree)
  129. ROTATE_TRAIS(DPolygon, bg::radian)
  130.  
  131. template<typename Geometry, typename DegreeOrRadian, typename CalculationType>
  132. void rotate(Geometry& geometry, const DegreeOrRadian&, const CalculationType& angle)
  133. {
  134. rotate_trais<Geometry, DegreeOrRadian, CalculationType>::apply(geometry, angle);
  135. }
  136.  
  137. template<typename Geometry, typename Point, typename DegreeOrRadian, typename CalculationType>
  138. void point_rotate(Geometry& geometry, const Point& point, const DegreeOrRadian& type, const CalculationType& angle)
  139. {
  140. Point pt(0, 0);
  141.  
  142. bg::subtract_point(pt, point);
  143. translate(geometry, bg::get<0>(pt), bg::get<1>(pt));
  144. rotate(geometry, type, angle);
  145. translate(geometry, bg::get<0>(point), bg::get<1>(point));
  146. }
  147.  
  148. //////////////////////////////////////////////////////////////////////////
  149. // 比例变形
  150. //////////////////////////////////////////////////////////////////////////
  151. template<typename Geometry, typename CalculationType>
  152. struct scale_impl
  153. {
  154. static void apply(Geometry& geometry, const CalculationType& scale_x, const CalculationType& scale_y)
  155. {
  156. bg::strategy::transform::scale_transformer<CalculationType, 2, 2> t(scale_x, scale_y);
  157. bg::transform(geometry, geometry, t);
  158. }
  159. };
  160.  
  161. template<typename Geometry, typename CalculationType>
  162. struct scale_trais
  163. {
  164. static void apply(Geometry& geometry, const CalculationType& scale_x, const CalculationType& scale_y);
  165. };
  166.  
  167. #define SCALE_TRAIS(Geometry) \
  168. template<typename CalculationType> \
  169. struct scale_trais<Geometry, CalculationType> \
  170. { \
  171. static void apply(Geometry& geometry, const CalculationType& scale_x, const CalculationType& scale_y) \
  172. { \
  173. scale_impl<Geometry, CalculationType>::apply(geometry, scale_x, scale_y); \
  174. } \
  175. };
  176.  
  177. SCALE_TRAIS(DPoint)
  178. SCALE_TRAIS(DSegment)
  179. SCALE_TRAIS(DBox)
  180. SCALE_TRAIS(DRing)
  181. SCALE_TRAIS(DPolygon)
  182.  
  183. template<typename Geometry, typename CalculationType>
  184. void scale(Geometry& geometry, const CalculationType& scale_x, const CalculationType& scale_y)
  185. {
  186. scale_trais<Geometry, CalculationType>::apply(geometry, scale_x, scale_y);
  187. }
  188.  
  189. //////////////////////////////////////////////////////////////////////////
  190. // 函数功能:
  191. // 扩充box
  192. //////////////////////////////////////////////////////////////////////////
  193. template<typename Geometry, typename CalculateType>
  194. void InflateBox(Geometry& geometry, CalculateType const& cx, CalculateType const& cy)
  195. {
  196. typedef typename bg::point_type<Geometry>::type point_type;
  197. point_type& ptMin = geometry.min_corner();
  198. point_type& ptMax = geometry.max_corner();
  199.  
  200. ptMin.x(ptMin.x() - cx);
  201. ptMin.y(ptMin.y() - cy);
  202. ptMax.x(ptMax.x() + cx);
  203. ptMax.y(ptMax.y() + cy);
  204. }
  205.  
  206. //////////////////////////////////////////////////////////////////////////
  207. // 函数功能:
  208. // 求点到线段的垂足;
  209. // 返回值:
  210. // true,垂足在线段上;
  211. // false,垂足在线段外;
  212. //////////////////////////////////////////////////////////////////////////
  213. template<typename Point, typename Segment>
  214. bool point_to_segment_org(Point const& point, Segment const& segment, Point& ptOut)
  215. {
  216. bool bInSegment = true;
  217.  
  218. try
  219. {
  220. typedef typename bg::point_type<Segment>::type point_type;
  221. point_type p[2] = {segment.first, segment.second};
  222.  
  223. if (boost::geometry::equals(point, p[0]) ||
  224. boost::geometry::equals(point, p[1]))
  225. {
  226. boost::geometry::assign_point(ptOut, point);
  227. bInSegment = true;
  228. throw bInSegment;
  229. }
  230.  
  231. point_type v(p[1]), w(point);
  232.  
  233. boost::geometry::subtract_point(v, p[0]);
  234. boost::geometry::subtract_point(w, p[0]);
  235.  
  236. typedef typename bg::select_calculation_type<Point, Segment, void>::type calculation_type;
  237.  
  238. calculation_type const zero = calculation_type();
  239. calculation_type const c1 = boost::geometry::dot_product(w, v);
  240. if (c1 < zero)
  241. {
  242. bInSegment = false;
  243. }
  244.  
  245. double const c2 = boost::geometry::dot_product(v, v);
  246. if (c2 < c1)
  247. {
  248. bInSegment = false;
  249. }
  250.  
  251. calculation_type const b = c1 / c2;
  252. DPoint projected(p[0]);
  253.  
  254. boost::geometry::multiply_value(v, b);
  255. boost::geometry::add_point(projected, v);
  256. boost::geometry::assign_point(ptOut, projected);
  257. }
  258. catch (bool)
  259. {
  260. }
  261.  
  262. return bInSegment;
  263. }
  264.  
  265. //////////////////////////////////////////////////////////////////////////
  266. // 函数功能:
  267. // 通过比例求段上的点;
  268. //////////////////////////////////////////////////////////////////////////
  269. template<typename Segment, typename Point>
  270. void get_segment_on_point_by_scale(Segment const& segment, double const& dScale, Point& ptOut)
  271. {
  272. typedef typename bg::point_type<Segment>::type point_type;
  273.  
  274. point_type p[2] = {segment.first, segment.second};
  275. point_type v(p[1]);
  276. point_type ptDest(p[0]);
  277.  
  278. boost::geometry::subtract_point(v, p[0]);
  279. boost::geometry::multiply_value(v, dScale);
  280. boost::geometry::add_point(ptDest, v);
  281. boost::geometry::assign_point(ptOut, ptDest);
  282. }
  283.  
  284. //////////////////////////////////////////////////////////////////////////
  285. // 函数功能:
  286. // 通过长度求段上的点;
  287. //////////////////////////////////////////////////////////////////////////
  288. template<typename Segment, typename Point>
  289. void get_segment_on_point_by_length(Segment const& segment, double const& dLength, Point& ptOut)
  290. {
  291. typedef typename bg::point_type<Segment>::type point_type;
  292.  
  293. point_type p[2] = {segment.first, segment.second};
  294. double dTotalLength = boost::geometry::distance(p[0], p[1]);
  295. double dScale = dLength / dTotalLength;
  296.  
  297. get_segment_on_point_by_scale(segment, dScale, ptOut);
  298. }
  299. }
  300.  
  301.  
  302. int main()
  303. {
  304. DBox box1(DPoint(100, 100), DPoint(300, 200));
  305. GeometryExtend::InflateBox(box1, 20, 10);
  306.  
  307. DSegment sg1(DPoint(100, 100), DPoint(300, 300));
  308. DPoint pt1(300, 100);
  309. DPoint pt2(100, 0);
  310. DPoint ptOut;
  311. bool bOnSegment;
  312.  
  313. bOnSegment = GeometryExtend::point_to_segment_org(pt1, sg1, ptOut);
  314. bOnSegment = GeometryExtend::point_to_segment_org(pt2, sg1, ptOut);
  315.  
  316. GeometryExtend::get_segment_on_point_by_scale(sg1, 0.5, ptOut);
  317. GeometryExtend::get_segment_on_point_by_length(sg1, 0, ptOut);
  318.  
  319. DPoint pt4;
  320. DSegment sg4;
  321. //平移变换
  322. bg::assign_point(pt4, pt1);
  323. GeometryExtend::translate(pt4, 10, 20);
  324. bg::assign(sg4, sg1);
  325. GeometryExtend::translate(sg4, 10, 20);
  326.  
  327. //旋转变形
  328. bg::assign(pt4, pt1);
  329. GeometryExtend::rotate(pt4, bg::degree(), 45.0);
  330. bg::assign(sg4, sg1);
  331. GeometryExtend::rotate(sg4, bg::degree(), 45.0);
  332.  
  333. //比例变换
  334. bg::assign(pt4, pt1);
  335. GeometryExtend::scale(pt4, 0.5, 0.2);
  336. bg::assign(sg4, sg1);
  337. GeometryExtend::scale(sg4, 0.5, 0.2);
  338.  
  339. return 0;
  340. }

boost库之geometry<二>的更多相关文章

  1. boost库之geometry

    环境:win732位旗舰版.VS2010旗舰版.boost 1.55.0版本.坐标系为MM_TEXT Geometry是一个开源的几何计算库,包含了几何图形最基本的操作(也支持复杂的操作),下面我们看 ...

  2. 【转】boost库之geometry

    #include <boost/assign.hpp> #include <boost/geometry/geometry.hpp> #include <boost/ge ...

  3. Boost 库Program Options--第二篇

    程式執行參數處理函式庫:Boost Program Options(2/N) 前一篇已經大致解釋了 Boost Program Options 基本上的使用方法.而這一篇,則來細講一下選項描述(opt ...

  4. (十二)boost库之多线程高级特性

    (十二)boost库之多线程高级特性 很多时候,线程不仅仅是执行一些耗时操作,可能我们还需要得到线程的返回值,一般的处理方法就是定义一个全局状态变量,不断轮训状态,就如我目前维护的一个项目,全局变量定 ...

  5. (二)boost库之字符串格式化

    (二)boost库之字符串格式化 程序中经常需要用到字符串格式化,就个人而言还是比较倾向于C格式的输出,如果只是打印日志,printf就够了,如果到生成字符串,获取你可以选择sprintf,但这些都是 ...

  6. C++ | boost库 类的序列化

    是的,这是今年的情人节,一篇还在研究怎么用的文章,文结的时候应该就用成功了. 恩,要有信心 神奇的分割线 不知何时装过boost库的header-only库, 所以ratslam中的boost是可以编 ...

  7. boost库学习之regex

    一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...

  8. boost库学习之开篇

    本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...

  9. boost库在windows下的编译和使用

    因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...

随机推荐

  1. hdu_5963_朋友(找规律)

    题目链接:hdu_5963_朋友 题意: 中文,不解释 题解: 把样例拿出来看看,你会发现以x为节点是否能赢,就是与x相连的边权值的和或者异或是否为奇数. #include<bits/stdc+ ...

  2. hdu_5894_hannnnah_j’s Biological Test(打表找规律)

    题目链接:hdu_5894_hannnnah_j’s Biological Test 题意: 有n个不同的位置围成一个圈,现在要安排m个人坐,每个人至少的间隔为k,问有多少种安排 题解: 先打表找规律 ...

  3. Vultr\DigitalOcean\Linode速度最快的vps机房推荐

    对大陆用户来说,香港.新加坡.韩国.台湾.日本,地理位置近,机房速度快.香港地区带宽成本太高,小水管跑不起来,没有性价比.韩国机房带宽充足,但成本也高.新加坡机房量少,像oneasiahost经常无法 ...

  4. 大数据阶乘(The factorial of large data)

    题目描述 Description 阶乘是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(long long类型)的一个数,求它的阶乘,请给出正确结果.为提高速 ...

  5. spring容器启动的加载过程(三)

    第十步: public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { /** * Load bean def ...

  6. Sersync同步过滤.svn文件夹

    Sersync同步过滤.svn文件夹 <filter start="true"> <exclude expression="(.*).svn(.*)&q ...

  7. As3.0 TextField

    一 TextField 对象的方法 方法 说明 TextField.addListener 加入接收触发事件如文本域内容变化或滚动变化的监听对象,触发事件可以参看最后一个表. TextField.ge ...

  8. 如何让图片在div里面剧中显示

    你可能有很多种方式,但是这种方式我觉得更加简单,供大家参考. 用一个 display:inline-block 的helper容器高度为height: 100% 并且vertical-align: m ...

  9. RFC3489 STUN之客户端所处环境探测流程与部分属性含义说明

    1 STUN客户端所处环境探测流程 1.1 流程图 1.2 流程图中Binding请求类型说明 类型1:Binding请求消息中不设置CHANGE-REQUEST,或若设置其相应更改IP与端口标志位都 ...

  10. boost log库

    http://blog.csdn.net/sheismylife/article/category/1820481