Free Electron
OrbiterSystem.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #pragma once
8 
9 namespace fe
10 {
11 namespace ext
12 {
13 
14 // !HACK
15 static bool locked = false;
16 
17 /**
18  * Test-only.
19  *
20  * Simple system that orbits objects around a point.
21  */
22 class OrbiterSystem : virtual public Stepper
23 {
24 public:
25  void orbitersAdded(const t_note_id &a_note_id)
26  {
27  if (locked)
28  return;
29  locked = true;
30 
31  orbiterAccessorSet.bind(m_rg_dataset->scope());
32 
33  // Find orbiter records.
34  orbiterRecords.clear();
35  orbiterAccessorSet.filter(orbiterRecords, m_rg_dataset);
36 
37  locked = false;
38  };
39 
40  void step(t_moa_real dt) override
41  {
42  // !HACK
43  if (locked)
44  return;
45  locked = true;
46 
47  // Loop over each box.
48  for (fe::Record orbiterRecord : orbiterRecords)
49  {
50  fe::Real radius = orbiterAccessorSet.orbitRadius(orbiterRecord);
51  fe::Real angularVelocity = 180.0 * M_PI/180.0; // 180 deg/s
52  orbiterAccessorSet.orbitAngle(orbiterRecord) += angularVelocity * dt;
53 
54  fe::SpatialVector offsetFromCenter = orbiterAccessorSet.orbitPoint(orbiterRecord);
55  offsetFromCenter[0] += radius * cos(orbiterAccessorSet.orbitAngle(orbiterRecord));
56  offsetFromCenter[1] += radius * sin(orbiterAccessorSet.orbitAngle(orbiterRecord));
57 
58  orbiterAccessorSet.transform(orbiterRecord).translation() =
59  offsetFromCenter + orbiterAccessorSet.orbitPoint(orbiterRecord);
60  }
61 
62  locked = false;
63  // feLog("orbit dt: %f\n", dt);
64  };
65 
66  void connectOrchestrator(fe::sp<OrchestratorI> a_spOrchestrator) override
67  {
68  Stepper::connectOrchestrator(a_spOrchestrator);
69 
70  a_spOrchestrator->connect(this, &OrbiterSystem::orbitersAdded, "orbitersAdded");
71  a_spOrchestrator->connect(this, &OrbiterSystem::orbitersAdded, FE_NOTE_COMPILE);
72  }
73 
74  AsOrbiter orbiterAccessorSet;
75  std::vector<fe::Record> orbiterRecords;
76 };
77 
78 } /* namespace ext */
79 } /* namespace fe */
void step(t_moa_real dt) override
Move system forward in time by a timestep.
Definition: OrbiterSystem.h:40
kernel
Definition: namespace.dox:3
virtual void connectOrchestrator(sp< OrchestratorI > a_spOrchestrator)
Callback for the System to register itself to the orchestrator.
Definition: Stepper.h:58
Time Stepping System.
Definition: Stepper.h:15
fe::Accessor< fe::Real > orbitRadius
Radius of orbit.
Definition: AsOrbiter.h:39
void connectOrchestrator(fe::sp< OrchestratorI > a_spOrchestrator) override
Callback for the System to register itself to the orchestrator.
Definition: OrbiterSystem.h:66
Reference to an instance of a Layout.
Definition: RecordSB.h:35
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53
fe::Accessor< fe::SpatialTransform > transform
Object transform to modify.
Definition: AsOrbiter.h:30
Test-only.
Definition: AsOrbiter.h:19
fe::Accessor< fe::SpatialVector > orbitPoint
Point to orbit about.
Definition: AsOrbiter.h:37
Test-only.
Definition: OrbiterSystem.h:22
fe::Accessor< fe::Real > orbitAngle
Current orbit angle.
Definition: AsOrbiter.h:35