jjzjj

c++ - 分形编程 - 有什么方法可以优化此代码以进行实时渲染?

coder 2024-02-24 原文

除了降低最大迭代次数之外,我还想尽可能优化一些代码。我听说有一些方法可以检测循环,但我尝试以不同的方式实现它,但它要么变得更慢,要么产生垃圾。显示功能未显示,因为它不是减速的原因。

#pragma once
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>
#include <cstdint>
#include <complex>
#include <functional>
#include <vector>

using namespace std;

template<class T>
class Fractal
{
public:
    Fractal(void);
    ~Fractal(void);

    //the most important function
    vector<uint32_t> evaluate(const sf::Rect<T>& area, const sf::Vector2u& subdivisions);

    //set the iterative function
    typedef function<void(complex<T>&)> iterative_function;
    void setIterativeFunction(iterative_function func);

    //set the domain function
    typedef function<bool(complex<T>&)> domain_function;
    void setDomainFunction(domain_function func);

    //set the maximum number of escape iterations
    void setMaxIterations(const uint32_t iterations);

    //get maximum iterations
    uint32_t getMaxIterations() const;

    //a coordinates generator
    //generates the coordinates to evaluate the fractal
    class CoordinatesGenerator
    {
    public:
        CoordinatesGenerator(const sf::Rect<T>& area, const sf::Vector2u& subdivisions);
        ~CoordinatesGenerator();

        complex<T> operator()();
    private:
        const sf::Rect<T>& area_;
        const sf::Vector2u& subdivisions_;
        complex<T> coord_;
        sf::Vector2u pixel_;
    };
private:
    //the number of escape iterations
    uint32_t max_iterations_;

    //the tolerance where z must change
    T tolerance_;

    //the formula used for the iterative system
    iterative_function iter_function_;

    //the formula that decides either the given complex is inside or not the domain
    domain_function domain_function_;

    //returns the number of iterations that z has to do to escape
    uint32_t getIterations(complex<T> z) const;
};

template<class T>
Fractal<T>::Fractal()
{
    //setting max iterations to 1000 by default
    max_iterations_ = 1000;

    //setting standard Manderbot iterative function
    iter_function_ = iterative_function([](complex<T>& z)
    {
        z = z*z + complex<T>(1,0);
    });

    //setting standard Manderbot domain function
    domain_function_ = domain_function([](complex<T>& z)
    {
        return abs(z) < 2;
    });
}

// Fractal<T>::setIterativeFunction
// iterative_function func : the function on which the system iterates
// must match this signature : void(Complex<T>&)
template<class T>
void Fractal<T>::setIterativeFunction(iterative_function func)
{
    iter_function_ = func;
}

// Fractal<T>::setDomainFunction
// domain_function func : the function that determines if complex is inside domain
// must match this signature : bool(Complex<T>&)
template<class T>
void Fractal<T>::setDomainFunction(domain_function func)
{
    domain_function_ = func;
}

// Fractal<T>::setMaxIterations
// iterations : set the maximum iterations for escape
template<class T>
void Fractal<T>::setMaxIterations(const uint32_t iterations)
{
    max_iterations_ = iterations;
}

// vector<uint32_t> Fractal<T>::evaluate(const sf::Rect<T>& area, const sf::Vector2u& subdivisions)
// area: the fractal area to evaluate
// subdivisions : the number of subdivisions to evaluate
// return a vector of the number of iterations
// the vector is construction from x = 0 ... n, y = 0 ... n
template<class T>
vector<uint32_t> Fractal<T>::evaluate(const sf::Rect<T>& area, const sf::Vector2u& subdivisions)
{
    uint32_t temp;
    complex<T> z(area.left,area.top);
    uint32_t num_coordinates = (subdivisions.x)*(subdivisions.y);

    vector<uint32_t> result;
    vector<complex<T>> coordinates(num_coordinates);
    CoordinatesGenerator generator(area,subdivisions);
    generate(coordinates.begin(),coordinates.end(),generator);

    for(auto& z: coordinates)
    {
        temp = getIterations(z);
        result.push_back(temp);
    }
    return result;
}

// uint32_t Fractal<T>::getIterations(complex<T> z) const
// z : the complex number to evaluate
// return the number of iterations that z escapes domain
// using iterative and domain functions
template<class T>
uint32_t Fractal<T>::getIterations(complex<T> z) const
{
    static uint32_t result;
    result = 0;

    while(domain_function_(z) && result < max_iterations_)
    {
        iter_function_(z);
        result++;
    }
    return result;
}

// Fractal<T>::CoordinatesGenerator::CoordinatesGenerator(const sf::Rect<T>& area, const sf::Vector2u& subdivisions)
// area : the fractal area to evaluate
// subdivisions : the number of subdivisions
// used by STL algorithm
template<class T>
Fractal<T>::CoordinatesGenerator::CoordinatesGenerator(const sf::Rect<T>& area, const sf::Vector2u& subdivisions):
    area_(area),subdivisions_(subdivisions)
{
    coord_ = complex<T>(area_.left,area_.top);
    pixel_.x = 0;
    pixel_.y = 0;
}

template<class T>
Fractal<T>::CoordinatesGenerator::~CoordinatesGenerator()
{
}

// complex<T> Fractal<T>::CoordinatesGenerator::operator()()
// Generate coordinates to evaluate the fractal
// used by STL algorithm
template<class T>
complex<T> Fractal<T>::CoordinatesGenerator::operator()()
{
    //getting the variation of X and Y
    T deltaX = area_.width/static_cast<T>(subdivisions_.x);
    T deltaY = area_.height/static_cast<T>(subdivisions_.y);

    //creating the coordinate
    coord_ = complex<T>(static_cast<T>(pixel_.x)*deltaX + area_.left,static_cast<T>(pixel_.y)*deltaY + area_.top);

    //applying some changes to generate the next coordinate
    pixel_.x++;

    if(pixel_.x >= subdivisions_.x)
    {
        pixel_.y++;
        pixel_.x = 0;
    }

    return coord_;
}

template<class T>
Fractal<T>::~Fractal()
{
}

template<class T>
uint32_t Fractal<T>::getMaxIterations() const
{
    return max_iterations_;
}

最佳答案

我注意到你的函数返回了

vector<uint32_t> 

请确保您使用支持 C++11 的编译器,因为您可能会受益于移动语义。

关于c++ - 分形编程 - 有什么方法可以优化此代码以进行实时渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20647072/

有关c++ - 分形编程 - 有什么方法可以优化此代码以进行实时渲染?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  6. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  7. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  8. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  9. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

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

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

随机推荐