Your code sucks, let's fix it - DPC UnConRafael Dohms
How do you measure the quality of your code? Performance and testing are just one aspect of code, in order to meet deadlines and make maintenance quicker you also need your code to be readable, decoupled and generally easier to comprehend and work with. This talk will go over tips and exercises to help you identify trouble areas, refactor them and train you to write better code in future projects. Come make your code look and function better.
セル生産方式におけるロボットの活用には様々な問題があるが,その一つとして 3 体以上の物体の組み立てが挙げられる.一般に,複数物体を同時に組み立てる際は,対象の部品をそれぞれロボットアームまたは治具でそれぞれ独立に保持することで組み立てを遂行すると考えられる.ただし,この方法ではロボットアームや治具を部品数と同じ数だけ必要とし,部品数が多いほどコスト面や設置スペースの関係で無駄が多くなる.この課題に対して音𣷓らは組み立て対象物に働く接触力等の解析により,治具等で固定されていない対象物が組み立て作業中に運動しにくい状態となる条件を求めた.すなわち,環境中の非把持対象物のロバスト性を考慮して,組み立て作業条件を検討している.本研究ではこの方策に基づいて,複数物体の組み立て作業を単腕マニピュレータで実行することを目的とする.このとき,対象物のロバスト性を考慮することで,仮組状態の複数物体を同時に扱う手法を提案する.作業対象としてパイプジョイントの組み立てを挙げ,簡易な道具を用いることで単腕マニピュレータで複数物体を同時に把持できることを示す.さらに,作業成功率の向上のために RGB-D カメラを用いた物体の位置検出に基づくロボット制御及び動作計画を実装する.
This paper discusses assembly operations using a single manipulator and a parallel gripper to simultaneously
grasp multiple objects and hold the group of temporarily assembled objects. Multiple robots and jigs generally operate
assembly tasks by constraining the target objects mechanically or geometrically to prevent them from moving. It is
necessary to analyze the physical interaction between the objects for such constraints to achieve the tasks with a single
gripper. In this paper, we focus on assembling pipe joints as an example and discuss constraining the motion of the
objects. Our demonstration shows that a simple tool can facilitate holding multiple objects with a single gripper.
【DLゼミ】XFeat: Accelerated Features for Lightweight Image Matchingharmonylab
公開URL:https://arxiv.org/pdf/2404.19174
出典:Guilherme Potje, Felipe Cadar, Andre Araujo, Renato Martins, Erickson R. ascimento: XFeat: Accelerated Features for Lightweight Image Matching, Proceedings of the 2024 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR) (2023)
概要:リソース効率に優れた特徴点マッチングのための軽量なアーキテクチャ「XFeat(Accelerated Features)」を提案します。手法は、局所的な特徴点の検出、抽出、マッチングのための畳み込みニューラルネットワークの基本的な設計を再検討します。特に、リソースが限られたデバイス向けに迅速かつ堅牢なアルゴリズムが必要とされるため、解像度を可能な限り高く保ちながら、ネットワークのチャネル数を制限します。さらに、スパース下でのマッチングを選択できる設計となっており、ナビゲーションやARなどのアプリケーションに適しています。XFeatは、高速かつ同等以上の精度を実現し、一般的なラップトップのCPU上でリアルタイムで動作します。
26. void Class::method() { void Class::method() {
for (外側ループ) { for (外側ループ) {
for (内側ループ) { for (内側ループ) {
if (条件) { method1(…);
処理; }
} }
} }
}
} void Class::method1(…) {
if (!条件)
return;
処理;
}
Before After
制御分の内側から順番にメソッド化していきます。
27. void Class::method() { void Class::method() {
for (外側ループ) { for (外側ループ)
for (内側ループ) { method2(…);
method1(); }
}
} void Class::method2(…) {
} for (内側ループ)
method1(…);
}
Before After
28. void Class::method() { void Class::method() {
for (外側ループ) { for (外側ループ)
for (内側ループ) { method2(…);
if (条件) { }
処理; void Class::method2(…) {
} for (内側ループ)
} method1(…);
} }
} void Class::method1(…) {
if (!条件)
return;
処理;
}
Before After
制御文ごとにメソッド化され、ネストが1段階となりました。
29. for (int i = 0; i < 5; ++i) {
if (a[i] == num) {
return true;
}
}
return false;
Before
return find(&a[0], &a[5], num) != &a[5];
After
検索などのループは、標準の関数を利用しましょう。STLのfindに変更しました。
30. totalAge = 0;
totalSalary = 0;
for (int i = 0; i < 5; ++i) {
totalAge += ages[i];
totalSalary += salarys[i];
}
Before
totalAge = 0;
for (int i = 0; i < 5; ++i)
totalAge += ages[i];
totalSalary = 0
for (int i = 0; i < 5; ++i)
totalSalary += salarys[i]; After
ループの内側で2つのことを扱うと、メソッド化が困難です。ループを分割します。
31. totalAge = 0;
for (int i = 0; i < 5; ++i)
totalAge += ages[i];
totalSalary = 0;
for (int i = 0; i < 5; ++i)
totalSalary += salarys[i];
Before
totalAge = accumulate(&ages[0], &ages[5], 0);
totalSalary = accumulate(&salarys[0], &salarys[5], 0);
After
配列の合計はSTLのaccumulate関数で求められます。
32. totalAge = accumulate(&ages[0], &ages[5], 0);
totalSalary = accumulate(&salarys[0], &salarys[5], 0);
Before
int Class::totalAge() {
return accumulate(&ages[0], &ages[5], 0);
}
int Class::totalSalary() {
return accumulate(&salarys[0], &salarys[5], 0);
} After
さらにメソッド化をしてみました。
33. totalAge = 0;
totalSalary = 0;
for (int i = 0; i < 5; ++i) {
totalAge += ages[i];
totalSalary += salarys[i];
}
Before
int Class::totalAge() {
return accumulate(&ages[0], &ages[5], 0);
}
int Class::totalSalary() {
return accumulate(&salarys[0], &salarys[5], 0);
} After
Martin Fowlerの「Split Loop」というリファクタング例になります。
36. int method() { int method() {
int result; if (!条件1)
if (条件1) { return 10;
if (条件2) { if (条件2)
result = 20; return 20;
} else { return 30;
result = 30; }
}
} else {
result = 10;
}
return result;
}
Before After
左と右のコードは同等の処理を行います。早期リターンを使うと単純になります。
37. switch (actorType) { class Actor {
case PLAYER: public:
virtual void update() = 0;
updatePlayer();
};
break;
case ENEMY:
updateEnemy(); actor->update();
break;
case BULLET:
updateBullet();
break;
}
Before After
「State/Strategyによるタイプコードの置き換え」というリファクタリングです。
38. int method() { int method() {
int result; return 条件 ? 20: 30;
if (条件) }
result = 20;
else
result = 30;
return result;
}
Before After
三項演算子(?)は使いすぎると、わかりにくくなりますが、単純なケースでは有用です。
39. if (x < 0) {
x = 0;
} else if (x > 640) {
x = 640;
}
Before
x = max(0, min(640, x));
After
上記のようなif文は、よく見かけますが、STLのmin,max関数で書き直せます。
40. if (x < 0) {
x = 0;
} else if (x > 640) {
x = 640;
}
Before
x = clamp(x, 0, 640);
float clamp(float x, float bottom, float top) {
return max(bottom, min(top, x));
}
After
さらにclampという関数を作ってみます。C++であれば、関数テンプレート化しましょう。
43. class Game { class Game {
private: private:
int score; Score score;
int limitTime; LimitTime time;
}; };
class Score {
private:
int score;
};
class LimitTime {
private:
int time;
};
Before After
得点や制限時間をクラス化します。「すべてがオブジェクト」になっていきます。
57. class Player { class Player {
private: private:
float x; Vector2 position;
float y; Angle angle;
float angle; Life life;
int life; };
}; class Angle {
private:
float angle;
};
class Life {
private:
int life;
};
Before After
状態変数が2つになるまで、段階的に変更していきます。
58. class Player { class Player {
private: private:
Vector2 position; Transform pose;
Angle angle; Life life;
Life life; };
};
class Transform {
private:
Vector2 position;
Angle angle;
};
Before After
positionとangleは座標変換を扱うクラスとして、まとめられそうです。
59. class Player { class Player {
private: private:
float x; Transform pose;
float y; Life life;
float angle; };
int life;
};
Before After
状態変数が2つになりました。
60. class Player { class Player {
public: public:
Player(float x, Player(Transform& pose,
float y, Life& life);
float angle, void update(Time& time);
int life); private:
void update( Transform pose;
float time); Life life;
private: };
float x;
float y;
float angle;
int life;
};
Before After
右側のクラスは、すべてがオブジェクトと関連しています。抽象度が高くなるわけです。
63. class Game {
private:
std::list<Actor*> actors;
std::list<Particles*> particles;
};
Before
class ActorManager {
std::list<Actor*> actors;
};
class ParticleManager {
std::list<Particle*> particles;
}; After
2つのコレクションを1つのクラスで扱えば、おそらく複雑なクラスになるでしょう。
64. class Game {
private:
std::list<Actor*> actors;
std::list<Particles*> particles;
};
Before
class Game {
private:
ActorManager actors;
ParticleMananger particles;
};
After
専用のクラスを作って、そちらに委譲してしまいます。