我想知道是否有一种方法可以遍历 boost 点模型的维度。我正在尝试创建一个函数来对两个自定义点进行计算,具有可定义的维数。换句话说,每个点的维数将匹配,但它们不会是一个常量值。我想在每个维度上做相同的操作,所以我需要做一个循环来实现这一点。
我想要做的事情的一个例子是:
for(std::size_t dim = 0; dim < D; dim++){
CoordinateType d = get<dim>();
//do stuff to d
set<dim>(d);
}
我知道这行不通,因为 d 不是编译时常量。
谢谢!
最佳答案
作为替代方法,我认为您应该能够将 Boost Geometry 点模型调整为 Fusion 序列。
#include <iostream>
namespace bg = boost::geometry;
namespace fus = boost::fusion;
int main() {
bg::model::point<double, 7, bg::cs::cartesian> p1;
// set some nice values
p1.set<0>(7); p1.set<1>(14); p1.set<2>(21); p1.set<3>(28);
p1.set<4>(35); p1.set<5>(42); p1.set<6>(49);
fus::for_each(fus::as_vector(p1), [](double x) { std::cout << x << ' '; });
}
打印
7 14 21 28 35 42 49
这是非常通用的(并且会为您提供比 for_each 更多的算法)。在示例中,我没有完全讲完所以你实际上可以说 for_each(p1, f) 而不是 for_each(as_vector(p1), f) 但你知道...读者的谚语练习。
这里涉及一些扩展“胶水”代码。我只是遵循了文档 here
在此处查看完整 list :
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/as_vector.hpp>
namespace bg_to_fusion {
using namespace boost;
struct bg_point_tag;
struct example_struct_iterator_tag;
template<typename Point, int Pos>
struct point_iterator
: fusion::iterator_base<point_iterator<Point, Pos> >
{
BOOST_STATIC_ASSERT(Pos >=0 && Pos <geometry::traits::dimension<typename remove_cv<Point>::type>::value);
typedef Point point_type;
typedef mpl::int_<Pos> index;
//typedef fusion::random_access_traversal_tag category;
typedef fusion::forward_traversal_tag category;
point_iterator(Point& p) : point_(p) {}
Point& point_;
};
}
namespace boost { namespace fusion {
// tag dispatch
namespace traits {
template <typename T, size_t dims, typename cs>
struct tag_of<geometry::model::point<T, dims, cs> > {
typedef bg_to_fusion::bg_point_tag type;
};
template <typename Point, int Pos>
struct tag_of<bg_to_fusion::point_iterator<Point, Pos> > {
typedef bg_to_fusion::example_struct_iterator_tag type;
};
}
namespace extension {
//////////////////////////////////////////////////////
// Point extension implementations
template<>
struct is_sequence_impl<bg_to_fusion::bg_point_tag>
{
template<typename T>
struct apply : mpl::true_ {};
};
template <>
struct size_impl<bg_to_fusion::bg_point_tag> {
template <typename Point>
struct apply : mpl::integral_c<size_t, geometry::traits::dimension<typename remove_cv<Point>::type>::value> { };
};
// begin
template<>
struct begin_impl<bg_to_fusion::bg_point_tag>
{
template<typename Point>
struct apply
{
typedef typename bg_to_fusion::point_iterator<Point, 0> type;
static type
call(Point& p)
{
return type(p);
}
};
};
// end
template<>
struct end_impl<bg_to_fusion::bg_point_tag>
{
template<typename Point> struct apply {
typedef typename bg_to_fusion::point_iterator<Point, geometry::traits::dimension<Point>::value> type;
static type call(Point& p) {
return type(p);
}
};
};
////////////////////////
// Iterator extension implementations
// value_of
template <>
struct value_of_impl<bg_to_fusion::example_struct_iterator_tag> {
template<typename Iterator> struct apply;
template<typename Point, int Pos>
struct apply<bg_to_fusion::point_iterator<Point, Pos> > {
typedef typename geometry::traits::coordinate_type<typename remove_cv<Point>::type>::type type;
};
};
// deref
template<>
struct deref_impl<bg_to_fusion::example_struct_iterator_tag>
{
template<typename Iterator>
struct apply;
template<typename Point, int Pos>
struct apply<bg_to_fusion::point_iterator<Point, Pos> >
{
typedef typename geometry::traits::coordinate_type<typename remove_cv<Point>::type>::type coordinate_type;
//typedef typename mpl::if_<is_const<Point>, coordinate_type const&, coordinate_type&>::type type;
typedef coordinate_type type;
static type
call(bg_to_fusion::point_iterator<Point, Pos> const& it) {
return it.point_.template get<Pos>();
}
};
};
// next
template<>
struct next_impl<bg_to_fusion::example_struct_iterator_tag> {
template<typename Iterator> struct apply
{
typedef typename Iterator::point_type point_type;
typedef typename Iterator::index index;
typedef typename bg_to_fusion::point_iterator<point_type, index::value + 1> type;
static type
call(Iterator const& i) {
return type(i.point_);
}
};
};
}
} }
关于c++ - c++ Boost循环遍历模型的维度::点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175368/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("