IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> OpenFoam中C++对整型列表创建、操作以及循环器的使用 -> 正文阅读

[游戏开发]OpenFoam中C++对整型列表创建、操作以及循环器的使用

OpenFoam列表的创建与循环器

提示:这里的程序仅仅是对$FOAM_APP/test/circulator文件的注释与添加


前言

提示:在OpenFOAM中最常用的数据类型之一便是列表,因对OpenFOAM的熟悉程度较,在此仅仅说一下撸代码撸到的一个类型

  • face 是一个继承于labelList的类,在OpenFOAM中,将labelList类看做是存放整数列表的类。
  • 在OpenFOAM中对列表的创建、旋转、修改内部值以及列表的比较。
  • 对列表中的数据处理,除去for循环之外的,另一种circulator的类。优点是,可以在循环的时候可以看到当前元素的前一个和后一个元素,提前做出判断。

`

一、程序注释

代码如下:

/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2012-2018 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application
    Test-circulator

Description
    circulator就好像一个容器,这个容器存放着一个整数循环的列表,
    与for循环的区别是。这个循环可以同时提取出列表中相邻的三个数值进行操作
    练习List表的创建,旋转,倒转,替换
    face中compare()比较函数的使用
    face可以看做整数列表
\*---------------------------------------------------------------------------*/

#include "List.H"
#include "ListOps.H"
#include "face.H"
#include "Circulator.H"
#include "ConstCirculator.H"


using namespace Foam;


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:

int main(int argc, char *argv[])
{
    Info<< "Test the implementation of a circular iterator(迭代循环器)" << nl << endl;

    Info<< "Test const circulator. First go forwards, then backwards."
        << nl << endl;
//identity(4)创建一个列表
    Info<<"identity(4)创建的数据类型:"<<typeid(identity(4)).name()<<
    " "<< identity(4)<<endl;
    Info<<"identity(8)创建的数据类型:"<<typeid(identity(8)).name()<<
    " "<< identity(8)<<endl;
    
//对应网格顶点的面整数列表类
    face f(identity(4)); 
    Info <<f<<endl;

 //从面构造循环器,创建一个循环容器的对象cStart,通过面标签列表对象f来初始化它
    ConstCirculator<face> cStart(f);
//首先检查循环器是否有一个可以迭代的大小
//然后在支点开始循环列表并结束
    Info<<cStart.size()<<endl;
    if (cStart.size()) do
    {
        Info<< "Iterate forwards over face (prev/curr/next) : "
            << cStart.prev() << " / " << cStart() << " / " << cStart.next()
            << endl;

    } while (cStart.circulate(CirculatorBase::direction::clockwise));

    if (cStart.size()) do
    {
        Info<< "Iterate backwards over face : " << cStart() << endl;

    } while (cStart.circulate(CirculatorBase::direction::anticlockwise));


    Info<< nl << nl << "Test non-const(可以改变参量的) circulator" << nl << endl;

    Circulator<face> cStart2(f);

    Info<< "输出先前的标签列表对象f中的量个值: " << f[0]<<" "<<f[2] << endl;

    if (cStart2.size()) do
    {
        Info<< "Iterate forwards over face (prev/curr/next) : "
            << cStart2.prev() << " / " << cStart2() << " / " << cStart2.next()
            << endl;

    } while (cStart2.circulate(CirculatorBase::direction::clockwise));
    //类似于for循环,以此取当前的元素进行加1操作
    if (cStart2.size()) do
    {
        Info<< "Iterate forwards over face, adding 1 to each element : "
            << cStart2();

        cStart2() += 1;

        Info<< " -> " << cStart2() << endl;
    } while (cStart2.circulate(CirculatorBase::direction::clockwise));
//while内的direction可以控制循环方向,顺时针还是逆时针

/******************************************************************************/
    Info<< "Face after : " << f << endl;


    Info<< nl << nl << "Compare two faces: " << endl;

    face a(identity(5));
    Info<< "Compare " << a << " and " << a << " Match = " << face::compare(a, a)
        << endl;
//倒放列表
    face b(reverseList(a));
    Info<< "Compare " << a << " and " << b << " Match = " << face::compare(a, b)
        << endl;
//改变列表中的某一个值
    face c(a);
    c[4] = 3;
    Info<< "Compare " << a << " and " << c << " Match = " << face::compare(a, c)
        << endl;
//列表正向旋转两个单位
    face d(rotateList(a, 2));
    Info<< "Compare " << a << " and " << d << " Match = " << face::compare(a, d)
        << endl;
//创建5个1的整数列表,列表中的都是数字1
//        如果是labelList a(5),则创建了5个0的列表
    face g(labelList(5, 1));
    face h(g);
    Info<< "Compare " << g[2] << " and " << h << " Match = " << face::compare(g, h)
        << endl;

    g[0] = 2;
    h[3] = 2;
    Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
        << endl;

    g[4] = 3;
    h[4] = 3;
    Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
        << endl;

  
//face::compare()函数比较两个列表时,如果元素一样或一个列表由另一个列表旋转得到结果为1
//        如果元素一样,但不是通过旋转得到的,结果为-1;
//      如果元素不一样,结果为0;
    face face1(identity(1));
    Info<< "Compare " << face1 << " and " << face1
        << " Match = " << face::compare(face1, face1) << endl;

    face face2(identity(1)+1);
    Info<< "Compare " << face1 << " and " << face2
        << " Match = " << face::compare(face1, face2) << endl;

/******************************************************************************/
    Info<< nl << nl << "Zero face" << nl << endl;

    face fZero;
    Info<<"fZero创建了一个:"<<fZero<<endl;
    Circulator<face> cZero(fZero);
//因为cZero的大小为零,所以没进去
    if (cZero.size()) do
    {
        Info<<"进去了"<<endl;
        Info<< "Iterate forwards over face : " << cZero() << endl;
        Info<<"结束了"<<endl;
    } while (cZero.circulate(CirculatorBase::direction::clockwise));

    fZero = face(identity(5));

    // circulator was invalidated so reset
    cZero = Circulator<face>(fZero);

    do
    {
        Info<< "Iterate forwards over face : " << cZero() << endl;

    } while (cZero.circulate(CirculatorBase::direction::clockwise));


/*****************************************************************************/

    Info<< nl << nl << "Simultaneously go forwards/backwards over face " << f
        << nl << endl;

    ConstCirculator<face> circForward(f);
    ConstCirculator<face> circBackward(f);

//如果两个都不是零内容,才进入
    Info<<f<<endl;
    if (circForward.size() && circBackward.size()) do
    {
        Info<< "Iterate over face forwards : " << circForward()
            << ", backwards : " << circBackward() << endl;
    }
    while
    (
        circForward.circulate(CirculatorBase::direction::clockwise),
        circBackward.circulate(CirculatorBase::direction::anticlockwise)
    );

    Info<< "\nEnd\n" << endl;

    return 0;
}


// ************************************************************************* //

二、结果

代码如下(示例):

tsing@tsing:~/OpenFOAM/OpenFOAM-9/applications/test/Circulator$ Test-Circulator 
Test the implementation of a circular iterator(迭代循环器)

Test const circulator. First go forwards, then backwards.

identity(4)创建的数据类型:N4Foam4ListIiEE 4(0 1 2 3)
identity(8)创建的数据类型:N4Foam4ListIiEE 8(0 1 2 3 4 5 6 7)
4(0 1 2 3)
4
Iterate forwards over face (prev/curr/next) : 3 / 0 / 1
Iterate forwards over face (prev/curr/next) : 0 / 1 / 2
Iterate forwards over face (prev/curr/next) : 1 / 2 / 3
Iterate forwards over face (prev/curr/next) : 2 / 3 / 0
Iterate backwards over face : 0
Iterate backwards over face : 3
Iterate backwards over face : 2
Iterate backwards over face : 1


Test non-const(可以改变参量的) circulator

输出先前的标签列表对象f中的量个值: 0 2
Iterate forwards over face (prev/curr/next) : 3 / 0 / 1
Iterate forwards over face (prev/curr/next) : 0 / 1 / 2
Iterate forwards over face (prev/curr/next) : 1 / 2 / 3
Iterate forwards over face (prev/curr/next) : 2 / 3 / 0
Iterate forwards over face, adding 1 to each element : 0 -> 1
Iterate forwards over face, adding 1 to each element : 1 -> 2
Iterate forwards over face, adding 1 to each element : 2 -> 3
Iterate forwards over face, adding 1 to each element : 3 -> 4
Face after : 4(1 2 3 4)


Compare two faces: 
Compare 5(0 1 2 3 4) and 5(0 1 2 3 4) Match = 1
Compare 5(0 1 2 3 4) and 5(4 3 2 1 0) Match = -1
Compare 5(0 1 2 3 4) and 5(0 1 2 3 3) Match = 0
Compare 5(0 1 2 3 4) and 5(3 4 0 1 2) Match = 1
Compare 1 and 5{1} Match = 1
Compare 5(2 1 1 1 1) and 5(1 1 1 2 1) Match = 1
Compare 5(2 1 1 1 3) and 5(1 1 1 2 3) Match = -1
Compare 1(0) and 1(0) Match = 1
Compare 1(0) and 1(1) Match = 0


Zero face

fZero创建了一个:0()
Iterate forwards over face : 0
Iterate forwards over face : 1
Iterate forwards over face : 2
Iterate forwards over face : 3
Iterate forwards over face : 4


Simultaneously go forwards/backwards over face 4(1 2 3 4)

4(1 2 3 4)
Iterate over face forwards : 1, backwards : 1
Iterate over face forwards : 2, backwards : 4
Iterate over face forwards : 3, backwards : 3
Iterate over face forwards : 4, backwards : 2

End

三、官网对circulator类的解释

代码如下

Class
     Foam::ConstCirculator
 
 Description
     Walks over a container as if it were circular. The container must have the
     following members defined:
         - value_type
         - size_type
         - difference_type
         - const_iterator
         - const_reference
 
     Examples:
 
     \code
         face f(identity(5));
 
         // Construct circulator from the face
         ConstCirculator<face> circ(f);
 
         // First check that the circulator has a size to iterate over.
         // Then circulate around the list starting and finishing at the fulcrum.
         if (circ.size()) do
         {
             Info<< "Iterate forwards over face : " << circ() << endl;
 
         } while (circ.circulate(CirculatorBase::direction::clockwise));
     \endcode
 
     \code
         face f(identity(5));
 
         ConstCirculator<face> circClockwise(f);
         ConstCirculator<face> circAnticlockwise(f);
 
         if (circClockwise.size() && circAnticlockwise.size()) do
         {
             Info<< "Iterate forward over face :" << circClockwise() << endl;
             Info<< "Iterate backward over face:" << circAnticlockwise() << endl;
         }
         while
         (
             circClockwise.circulate(CirculatorBase::direction::clockwise),
             circAnticlockwise.circulate
             (
                 CirculatorBase::direction::anticlockwise
             )
         );
     \endcode
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-03-21 21:25:47  更:2022-03-21 21:26:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/16 19:01:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码