jjzjj

c++ - 从 2D C 列表创建 boost.geometry.model.polygon

coder 2024-02-02 原文

假设我有以下数据集

double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (ii = 0; ii < 100; ii++) {
    data[2*ii] = ii;
    data[2*ii + 1] = ii;
}

我如何根据这些数据创建 boost 多边形?

谢谢

最佳答案

一个完整的例子

#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>

// Some typedefs
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;
typedef bpl::polygon_traits<Polygon>::point_type Point;

int main() {

  // Your C-style data (assumed (x,y) pairs)
  double * data = (double *) malloc(sizeof(double) * 100 * 2);
  for (int ii = 0; ii < 100; ii++) {
    data[2*ii] = ii;
    data[2*ii + 1] = ii;
  }

  // Convert to points
  std::vector<Point> points;
  for (int i=0;i<100;++i)
    points.push_back(Point(data[2*i],data[2*i+1]));

  // Create a polygon
  Polygon polygon;
  polygon.set(points.begin(),points.end());

  // Do something with the polygon
  std::cout << "Perimeter : " << bpl::perimeter(polygon) << std::endl;
  std::cout << "Area      : " << bpl::area(polygon) << std::endl;

  return 0;
}

只是为了说明您实际拥有的灵 active :通过一些额外的 typedef 工作,可以定义您自己的 double 点类型,它可以别名到您的数据上,从而避免了中间拷贝...

#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>

// Define a point type which can be aliased to your 'C' points
struct Pt {
  double x;
  double y;
};

// Some typedefs
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;

// Add the necessary to use Pt
namespace boost { 
  namespace polygon {

    template <> struct geometry_concept<Pt> {typedef point_concept type;};

    template <> struct point_traits<Pt> {
      typedef double coordinate_type;

      static inline coordinate_type get(const Pt& pt,orientation_2d orient) {
    return (orient == HORIZONTAL ? pt.x : pt.y);
      }
    };

    template <> struct point_mutable_traits<Pt> {
      static inline void set(Pt& pt, orientation_2d orient, int value) {
    if(orient == HORIZONTAL)
      pt.x = value;
    else
      pt.y = value;
      }
      static inline Pt construct(double x,double y) {
    Pt r;
    r.x=x;
    r.y=y;
    return r;
      }
    };
  } 
}

int main() {

  // Your C-style data (assumed (x,y) pairs)
  double * data = (double *) malloc(sizeof(double) * 100 * 2);
  for (int ii = 0; ii < 100; ii++) {
    data[2*ii] = ii;
    data[2*ii + 1] = ii;
  }

  // Reinterpret your data as an array of Pt
  const Pt*const pts=reinterpret_cast<const Pt*>(data);

  // Create a polygon
  Polygon polygon;
  polygon.set(pts,pts+100);

  // Do something with the polygon
  std::cout << "Perimeter : " << bpl::perimeter(polygon) << std::endl;
  std::cout << "Area      : " << bpl::area(polygon) << std::endl;

  return 0;
}

而且这种趋势可以持续到custom polygon class .

关于c++ - 从 2D C 列表创建 boost.geometry.model.polygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464214/

有关c++ - 从 2D C 列表创建 boost.geometry.model.polygon的更多相关文章

  1. ruby-on-rails - rails : keeping DRY with ActiveRecord models that share similar complex attributes - 2

    这似乎应该有一个直截了当的答案,但在Google上花了很多时间,所以我找不到它。这可能是缺少正确关键字的情况。在我的RoR应用程序中,我有几个模型共享一种特定类型的字符串属性,该属性具有特殊验证和其他功能。我能想到的最接近的类似示例是表示URL的字符串。这会导致模型中出现大量重复(甚至单元测试中会出现更多重复),但我不确定如何让它更DRY。我能想到几个可能的方向...按照“validates_url_format_of”插件,但这只会让验证干给这个特殊的字符串它自己的模型,但这看起来很像重溶液为这个特殊的字符串创建一个ruby​​类,但是我如何得到ActiveRecord关联这个类模型

  2. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  7. ruby - 如何使用 RSpec::Core::RakeTask 创建 RSpec Rake 任务? - 2

    如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake

  8. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  9. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  10. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

随机推荐