c++大規模プログラムの読解

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ふえるみん

c++大規模プログラムの読解

#1

投稿記事 by ふえるみん » 8年前

C++で書かれた物理シミュレーションに関する大規模プログラムの読解を行っています。
しかしながら、勉強不足もあり、わからない文法も多く、
調べようにもとっかかりが見つからない状況です。


文末にソースコードの一部を示します。
以下の点をご教授いただければ幸いです。

①64行目などの

sofa::simulation::Node::SPtr groot = sofa::simulation::getSimulation()->createNewGraph("root")

とはどういった文法なのでしょうか?はじめはSPtrクラスのgrootオブジェクトを生成しているのかと思ったのですが、
SPtrはいろいろなクラスに現れるようですし(93行目などにも似たような記述がある。)
=より右の意味もよくわかりません。(関数を代入してる??)

②85行目などの

WriteAccessor< Data<MechanicalObject3::VecDeriv> > velocities = *dof->write( VecId::velocity() );

といった文法もわかりません。<>とはなんなのでしょうか?右辺はポインタですしもう何が何やら・・・


初心者のため、ずれたことを聞いているかもしれせんが
以上に関してご教授いただければ幸いです。
ソースはオープンのものですので、必要な部分があれば追加します。
よろしくお願いします。

環境:windows7、visualstudio2010

コード:

 
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 RC 1        *
*                (c) 2006-2011 INRIA, USTL, UJF, CNRS, MGH                    *
*                                                                             *
* This program 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 2 of the License, or (at your option)   *
* any later version.                                                          *
*                                                                             *
* This program 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 this program; if not, write to the Free Software Foundation, Inc., 51  *
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
*******************************************************************************
*                            SOFA :: Applications                             *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
#include <sofa/helper/ArgumentParser.h>
#include <sofa/simulation/graph/DAGSimulation.h>
#include <sofa/simulation/common/Node.h>
#include <sofa/component/contextobject/Gravity.h>
#include <sofa/component/contextobject/CoordinateSystem.h>
#include <sofa/component/odesolver/EulerSolver.h>
#include <sofa/component/visualmodel/VisualStyle.h>
#include <sofa/core/objectmodel/Context.h>
#include <sofa/component/collision/SphereModel.h>
#include <sofa/core/VecId.h>
#include <sofa/gui/GUIManager.h>
#include <sofa/gui/Main.h>

#include <sofa/helper/system/glut.h>
#include <sofa/helper/accessor.h>



using sofa::component::odesolver::EulerSolver;
using namespace sofa::component::collision;
using sofa::core::objectmodel::Data;
using sofa::helper::ReadAccessor;
using sofa::helper::WriteAccessor;
using sofa::core::VecId;
using sofa::core::objectmodel::New;

//Using double by default, if you have SOFA_FLOAT in use in you sofa-default.cfg, then it will be FLOAT.
#include <sofa/component/typedef/Sofa_typedef.h>
// ---------------------------------------------------------------------
// ---
// ---------------------------------------------------------------------
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    sofa::helper::parse("This is a SOFA application.")
    (argc,argv);

    // The graph root node
    sofa::simulation::setSimulation(new sofa::simulation::graph::DAGSimulation());
    sofa::simulation::Node::SPtr groot = sofa::simulation::getSimulation()->createNewGraph("root");
    groot->setGravity( Coord3(0,-10,0) );

    // One solver for all the graph
    EulerSolver::SPtr solver = sofa::core::objectmodel::New<EulerSolver>();
    solver->setName("solver");
    solver->f_printLog.setValue(false);
    groot->addObject(solver);//


    // One node to define the particle
    sofa::simulation::Node::SPtr particule_node = groot.get()->createChild("particle_node");
    // The particule, i.e, its degrees of freedom : a point with a velocity
    MechanicalObject3::SPtr dof = sofa::core::objectmodel::New<MechanicalObject3>();
    dof->setName("particle");
    particule_node->addObject(dof);
    dof->resize(1);
    // get write access the particle positions vector
    WriteAccessor< Data<MechanicalObject3::VecCoord> > positions = *dof->write( VecId::position() );
    positions[0] = Coord3(0,0,0);
    // get write access the particle velocities vector
    WriteAccessor< Data<MechanicalObject3::VecDeriv> > velocities = *dof->write( VecId::velocity() );
    velocities[0] = Deriv3(0,0,0);
    
	// show the particle()
    dof->showObject.setValue(true);
    dof->showObjectScale.setValue(10.);

    // Its properties, i.e, a simple mass node
    UniformMass3::SPtr mass = sofa::core::objectmodel::New<UniformMass3>();
    mass->setName("mass");
    particule_node->addObject(mass);
    mass->setMass( 1 );

    // this currently reveals a bug
//    // attach a collision surface to the particle
//    SphereModel::SPtr sphere = New<SphereModel>();
//    particule_node->addObject(sphere);
//    sphere->defaultRadius.setValue(0.1);

    // Display Flags
    sofa::component::visualmodel::VisualStyle::SPtr style = sofa::core::objectmodel::New<sofa::component::visualmodel::VisualStyle>();
    groot->addObject(style);
    sofa::core::visual::DisplayFlags& flags = *style->displayFlags.beginEdit();
    flags.setShowBehaviorModels(true);
    flags.setShowCollisionModels(true);
    style->displayFlags.endEdit();

    sofa::simulation::graph::getSimulation()->init(groot.get());
    groot->setAnimate(false);

    //======================================
    // Set up the GUI
    sofa::gui::initMain();
    sofa::gui::GUIManager::Init(argv[0]);
    sofa::gui::GUIManager::createGUI(groot);
    sofa::gui::GUIManager::SetDimension(800,700);
//    sofa::gui::GUIManager::SetFullScreen();  // why does this not work ?


    //=======================================
    // Run the main loop
    sofa::gui::GUIManager::MainLoop(groot);

    return 0;
}


アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: c++大規模プログラムの読解

#2

投稿記事 by h2so5 » 8年前

ふえるみん さんが書きました:C++で書かれた物理シミュレーションに関する大規模プログラムの読解を行っています。
しかしながら、勉強不足もあり、わからない文法も多く、
調べようにもとっかかりが見つからない状況です。
C++の文法の基礎を勉強してください。
入門書くらいひと通り読んだほうがいいです。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: c++大規模プログラムの読解

#3

投稿記事 by みけCAT » 8年前

ふえるみん さんが書きました:ソースはオープンのものですので、必要な部分があれば追加します。
出典(URLなど)を提示した方がいいと思います。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

ふぇるみん
記事: 2
登録日時: 8年前

Re: c++大規模プログラムの読解

#4

投稿記事 by ふぇるみん » 8年前

回答ありがとうございます。

みけCAT さん
失礼しました。ソースコード一式は以下のサイトでDL可能です。
https://www.sofa-framework.org/

h2so5 (副管理人) さん
>C++の文法の基礎を勉強してください。
>入門書くらいひと通り読んだほうがいいです。
おしゃるとおりですね(^_^;)
ぐうの根も出ません。今後末永く勉強していくつもりですが、
時間に限りもあり、キーワードだけでもえられればと・・・

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: c++大規模プログラムの読解

#5

投稿記事 by h2so5 » 8年前

悪いことはいわないので入門書読んでください。
基礎が理解できない状態で読解を試みても時間の無駄なので。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: c++大規模プログラムの読解

#6

投稿記事 by みけCAT » 8年前

ふえるみん さんが書きました:①64行目などの

sofa::simulation::Node::SPtr groot = sofa::simulation::getSimulation()->createNewGraph("root")

とはどういった文法なのでしょうか?はじめはSPtrクラスのgrootオブジェクトを生成しているのかと思ったのですが、
SPtrはいろいろなクラスに現れるようですし(93行目などにも似たような記述がある。)
=より右の意味もよくわかりません。(関数を代入してる??)
::は「スコープ解決演算子」です。
->は「アロー演算子」です。
右辺で呼び出した関数の戻り値を左辺に代入しています(正確には=演算子の処理を呼び出しています)。
ふえるみん さんが書きました:②85行目などの

WriteAccessor< Data<MechanicalObject3::VecDeriv> > velocities = *dof->write( VecId::velocity() );

といった文法もわかりません。<>とはなんなのでしょうか?右辺はポインタですしもう何が何やら・・・
<>は中に「テンプレート引数」を書きます。
(=演算子の)右辺(を評価した値)がポインタかどうかはこの式を見ただけではわかりません。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Poco
記事: 161
登録日時: 13年前

Re: c++大規模プログラムの読解

#7

投稿記事 by Poco » 8年前

ふえるみん さんが書きました: (snip)
①64行目などの

sofa::simulation::Node::SPtr groot = sofa::simulation::getSimulation()->createNewGraph("root")

とはどういった文法なのでしょうか?はじめはSPtrクラスのgrootオブジェクトを生成しているのかと思ったのですが、
SPtrはいろいろなクラスに現れるようですし(93行目などにも似たような記述がある。)

②85行目などの

WriteAccessor< Data<MechanicalObject3::VecDeriv> > velocities = *dof->write( VecId::velocity() );

といった文法もわかりません。<>とはなんなのでしょうか?右辺はポインタですしもう何が何やら・・・

初心者のため、ずれたことを聞いているかもしれせんが
以上に関してご教授いただければ幸いです。
(snip)
ソースを読んでSPtrだけ調べました。
NodeクラスのおじいさんであるBaseクラスで定義されている型です。
他のクラスでも使われているのは、みんなBaseクラスの子孫だからでしょう、多分。

取り掛かりが掴めないとのことですが、開発元のドキュメントには目を通しましょう。
#APIリファレンスはアレな出来でしたが。

まずはh2so5さんがアドバイスしている通り、初心者であるなら入門書を読みましょう。

ソースコードの読解というは、きつい作業です。
何もわからないところから読み解くことで、点で理解したものが線になり、やがて面になります。
それでもソフトウェア自体を殆ど理解できません。ですが、解読を続けていくうちに、いきなり世界全体が見えてきます。
逆にそこまでいかないと、理解を深めていることが実感できません。
粘り強く頑張ってください。

ふぇるみん
記事: 2
登録日時: 8年前

Re: c++大規模プログラムの読解

#8

投稿記事 by ふぇるみん » 8年前

みけCATさん、 Pocoさん
アドバイスありがとうございました。
昨日深夜まで取組んだせいか、だいぶわかってきた気がします。
もう少し頑張ってみようと思います。

閉鎖

“C言語何でも質問掲示板” へ戻る