Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分形编程-有没有办法优化实时渲染的代码?_C++_C++11_Stl_Fractals - Fatal编程技术网

C++ 分形编程-有没有办法优化实时渲染的代码?

C++ 分形编程-有没有办法优化实时渲染的代码?,c++,c++11,stl,fractals,C++,C++11,Stl,Fractals,除了降低最大迭代次数之外,如果可能的话,我还想优化一些代码。我听说有一些方法可以检测循环,但我尝试用不同的方法实现它,要么速度变慢,要么变成垃圾。显示功能未显示,因为这不是减速的原因 #pragma once #include <SFML/Graphics/Rect.hpp> #include <SFML/System/Vector2.hpp> #include <cstdint> #include <complex> #include <f

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

#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_;
}
#pragma一次
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
模板
类分形
{
公众:
分形(空洞);
~Fractal(void);
//最重要的功能
向量评估(常数sf::Rect和area、常数sf::向量2U和细分);
//设置迭代函数
typedef函数迭代函数;
void setIterativeFunction(迭代函数func);
//设置域函数
typedef函数域\ u函数;
void setDomainFunction(domain_function func);
//设置转义迭代的最大次数
void setMaxIterations(const uint32_t迭代);
//获得最大迭代次数
uint32_t getMaxIterations()常量;
//坐标发生器
//生成坐标以计算分形
类坐标生成器
{
公众:
坐标生成器(常数sf::Rect和area,常数sf::Vector2u和细分);
~CoordinatesGenerator();
复算子();
私人:
常数sf::矩形和面积;
常数sf::向量2u和细分;
复杂协调;
sf::矢量2U像素;
};
私人:
//转义迭代的次数
uint32最大迭代次数;
//z必须改变的公差
T耐受性;
//用于迭代系统的公式
迭代函数iter函数;
//决定给定复合体是否在域内的公式
域函数域函数;
//返回z为转义必须执行的迭代次数
uint32_t getIterations(复数z)const;
};
模板
分形::分形()
{
//默认情况下,将最大迭代次数设置为1000
最大迭代次数=1000;
//设置标准曼德博特迭代函数
iter_函数=迭代函数([](复数&z)
{
z=z*z+络合物(1,0);
});
//设置标准Manderbot域函数
域函数=域函数([](复数&z)
{
返回abs(z)<2;
});
}
//分形::setIterativeFunction
//迭代函数func:系统迭代的函数
//必须匹配此签名:void(复杂&)
模板
void Fractal::setIterativeFunction(迭代函数func)
{
iter函数=func;
}
//分形::setDomainFunction
//domain_function func:确定复合体是否在域内的函数
//必须匹配此签名:bool(复杂&)
模板
void Fractal::setDomainFunction(域函数func)
{
域函数=func;
}
//分形::setMaxIterations
//迭代次数:设置转义的最大迭代次数
模板
void Fractal::setMaxIterations(const uint32_t迭代)
{
最大迭代次数=迭代次数;
}
//向量分形::求值(常数sf::矩形和面积,常数sf::向量2U和细分)
//面积:要评估的分形面积
//细分:要计算的细分数
//返回迭代次数的向量
//向量是从x=0构造的。。。n、 y=0。。。N
模板
向量分形::求值(常数sf::矩形和面积,常数sf::向量2U和细分)
{
uint32温度;
复合体z(区域。左侧,区域。顶部);
uint32_t num_坐标=(细分.x)*(细分.y);
矢量结果;
矢量坐标(num_坐标);
协调发电机(区域、分区);
生成(坐标.begin(),坐标.end(),生成器);
用于(自动和z:坐标)
{
temp=迭代次数(z);
结果:推回(温度);
}
返回结果;
}
//uint32_t分形::getIterations(复数z)常量
//z:要计算的复数
//返回z从域中转义的迭代次数
//使用迭代函数和域函数
模板
uint32_t分形::getIterations(复数z)常量
{
静态uint32_t结果;
结果=0;
while(域函数和结果<最大迭代次数)
{
iter函数(z);
结果++;
}
返回结果;
}
//分形::坐标生成器::坐标生成器(常数sf::矩形和面积,常数sf::矢量2U和细分)
//面积:要评估的分形面积
//细分:细分的数量
//用于STL算法
模板
分形::坐标生成器::坐标生成器(常数sf::矩形和面积,常数sf::矢量2U和细分):
面积(面积),细分(细分)
{
坐标=复合体(区域左,区域上);
像素x=0;
像素y=0;
}
模板
分形::CoordinatesGenerator::~CoordinatesGenerator()
{
}
//复杂分形::坐标生成器::运算符()()
//生成坐标以评估分形
//用于STL算法
模板
复杂分形::坐标生成器::运算符()()
{
//求X和Y的变化量
T deltaX=面积宽度/静态铸件(细分部分x);
T deltaY=面积高度/静态铸造(细分);
//创建坐标
坐标=复数(静态投影(像素x)*三角形+区域左,静态投影(像素y)*三角形+区域上);
//应用一些更改以生成下一个坐标
像素_ux++;
如果(像素ux>=细分x)
{
像素μy++;
像素x=0;
}
返回坐标;
}
模板
分形::~Fractal()
{
}
模板
uint32\u t分形::getMaxIterations()常量
{
返回最大迭代次数;
}

我注意到您的函数返回

vector<uint32_t> 
向量

请确保您使用启用C++11的编译器,因为您可能会从移动语义中获益。

@Quilliom:因为请求太具体,所以在这里被认为是离题的。优化方面,我们只能接受一般性的审查请求。@Jamal Right。我的错误