Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
particle.hpp
Go to the documentation of this file.
1
5#ifndef PARTICLE_H
6#define PARTICLE_H
7
9
13
14#include "src/system/ext/math8.h"
15#include <cstdint>
16
17namespace lampda::modes {
18
23
24static constexpr float cylinderRadius_m = LampTy::lampBodyRadius_mm / 1000.0f;
25static constexpr float angularSpeedGain = 50000;
26static constexpr float linearSpeedGain = 1.0;
27static constexpr float reboundCoeff = 0.1f;
28static constexpr float speedDampening = 0.92f;
29
30static constexpr float maxAngularSpeed_radS = 4 * c_TWO_PI;
31static constexpr float maxVerticalSpeed_mmS = 50;
32
33static constexpr float ledsPerTurn = LampTy::ledPerTurns;
34static constexpr float rowHeight_mm = LampTy::ledStripWidth_mm;
35static constexpr float ledLength_mm = LampTy::ledSize_mm;
38static constexpr float aroundTubeRatio = angularSpeedGain * cylinderRadius_m * cylinderRadius_m * cylinderRadius_m;
39
44{
46 Particle() = default;
47
49 Particle(const Particle& other) = default;
50
52 Particle(const utils::vec3d& positionCartesian) :
53 theta_rad(atan2(positionCartesian.y, positionCartesian.x)),
54 z_mm(positionCartesian.z)
55 {
57 }
58
60 Particle(const float positionTheta_rad, const float positionZ_mm) : theta_rad(positionTheta_rad), z_mm(positionZ_mm)
61 {
63 }
64
71 utils::vec2d compute_speed_increment(const utils::vec3d& accelerationCartesian_m, const float deltaTime_s) const
72 {
73 // only the acceleration tangent to the cylinder surface moves the particle
74 const float tangentialAcceleration =
75 accelerationCartesian_m.y * fast_cos(theta_rad) - accelerationCartesian_m.x * fast_sin(theta_rad);
76
77 return utils::vec2d(aroundTubeRatio / cylinderRadius_m * tangentialAcceleration * deltaTime_s,
78 linearSpeedGain * accelerationCartesian_m.z * deltaTime_s);
79 }
80
87 utils::vec2d update_speed(const utils::vec3d& accelerationCartesian_m, const float deltaTime_s)
88 {
89 const auto& speedIncrement = compute_speed_increment(accelerationCartesian_m, deltaTime_s);
90 // start by dampening the speed
92
93 // update speed
95 lmpd_constrain<float>(thetaSpeed_radS + speedIncrement.x, -maxAngularSpeed_radS, maxAngularSpeed_radS);
96 zSpeed_mS = lmpd_constrain<float>(zSpeed_mS + speedIncrement.y, -maxVerticalSpeed_mmS, maxVerticalSpeed_mmS);
97
98 return utils::vec2d(speedIncrement.x * cylinderRadius_mm, speedIncrement.y * 1000.0f);
99 }
100
107 void apply_acceleration(const utils::vec3d& accelerationCartesian_m,
108 const float deltaTime_s,
109 const bool shouldContrain = true)
110 {
111 update_speed(accelerationCartesian_m, deltaTime_s);
112
113 static constexpr float angularUnit = LampTy::maxWidthFloat / c_TWO_PI;
114 static constexpr float verticalUnit = LampTy::ledStripWidth_mm * 1.5f;
115
116 // update position (limit speed to pixel unit per dt)
117 const float angularPositionIncrement =
118 lmpd_constrain<float>(thetaSpeed_radS * deltaTime_s, -angularUnit, angularUnit);
119 const float verticalPositionIncrement =
120 lmpd_constrain<float>(zSpeed_mS * deltaTime_s, -verticalUnit, verticalUnit) * 1000.0f;
121
122 // update particle position
123 z_mm += verticalPositionIncrement;
124 theta_rad += angularPositionIncrement;
125
126 // limit the derivation of the angle
128
129 // constrain to the lamp body
130 if (shouldContrain)
132 else
134 }
135
142 Particle simulate_after_acceleration(const utils::vec3d& accelerationCartesian_m,
143 const float deltaTime_s,
144 const bool shouldContrain = true) const
145 {
146 Particle simulated(*this);
147 simulated.apply_acceleration(accelerationCartesian_m, deltaTime_s, shouldContrain);
148 return simulated;
149 }
150
155 {
157
158 // coordinates go from 0 to -max
159 if (z_mm > 0)
160 {
161 z_mm = 0;
164 }
165
167 {
171 }
172
173 // handle the real limits (led strip do not start and end at zero depth)
175 {
176 // we are too high above the first led
177 if (z_mm >= 0)
178 {
179 // go one unit lower
182 }
183 else
184 {
185 // go one unit above
188 }
189 // udpate index
191 }
192 }
193
195 void dampen_speed(const float dampenFactor)
196 {
197 thetaSpeed_radS *= dampenFactor;
198 zSpeed_mS *= dampenFactor;
199 }
200
201 void set_color(const uint32_t color) { compressedColor = colors::get_compressed_16b_color(color); }
202 uint32_t get_color() const { return colors::get_decompressed_32b_color(compressedColor); }
203
205 uint16_t to_lamp_index() const { return to_led_index(theta_rad, z_mm); }
208
216 static int16_t led_index_at(const float positionTheta_rad, const float positionZ_mm)
217 {
218 float turnFraction = positionTheta_rad * (1.0f / c_TWO_PI);
219 turnFraction -= fast_floor(turnFraction);
220 const float turn = fast_floor(-positionZ_mm * (1.0f / rowHeight_mm) - turnFraction + 0.5f);
221 return static_cast<int16_t>(fast_floor((turn + turnFraction) * ledsPerTurn + 0.5f));
222 }
223
225 int16_t led_index_at_offset(const utils::vec2d& offset_mm) const
226 {
227 return led_index_at(theta_rad + offset_mm.x * (1.0f / cylinderRadius_mm), z_mm + offset_mm.y);
228 }
229
236 static float potential_at_led(const utils::vec3d& accelerationCartesian_m, const int16_t ledIndex)
237 {
238 const float angle_rad = ledIndex * (c_TWO_PI / ledsPerTurn);
239 return -aroundTubeRatio * cylinderRadius_mm *
240 (accelerationCartesian_m.x * fast_cos(angle_rad) + accelerationCartesian_m.y * fast_sin(angle_rad)) -
241 accelerationCartesian_m.z * to_helix_z(ledIndex);
242 }
243
246 {
248 }
249
251 void move_by(const utils::vec2d& offset_mm, const int16_t ledIndex)
252 {
253 theta_rad += offset_mm.x * (1.0f / cylinderRadius_mm);
254 // offsets are small, so one turn is enough to stay in the 0 to 2*PI range
255 if (theta_rad < 0.0f)
257 else if (theta_rad >= c_TWO_PI)
259 z_mm += offset_mm.y;
260 _savedLampIndex = ledIndex;
261 }
262
263 float thetaSpeed_radS = 0.0f;
264 float zSpeed_mS = 0.0f;
265
266 float theta_rad = 0.0f;
267 float z_mm = 0.0f;
268
271
272private:
274 static float fast_floor(const float value)
275 {
276 const float truncated = static_cast<float>(static_cast<int32_t>(value));
277 return (truncated > value) ? truncated - 1.0f : truncated;
278 }
279
281 static float fast_sin(const float angle_rad)
282 {
283 float turns = angle_rad * (1.0f / c_TWO_PI);
284 turns -= fast_floor(turns + 0.5f);
285 return 8.0f * turns - 16.0f * turns * fabsf(turns);
286 }
287
289 static float fast_cos(const float angle_rad) { return fast_sin(angle_rad + c_HALF_PI); }
290
291 uint16_t compressedColor = colors::get_compressed_16b_color(colors::HTMLColorCode::White);
292};
293
294} // namespace lampda::modes
295
296#endif
@ White
Definition: palettes.hpp:231
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:14
static constexpr float angularSpeedGain
this gain compensate the angular acceleration for better display
Definition: particle.hpp:25
static constexpr float maxVerticalSpeed_mmS
max vertical speed in mm/S
Definition: particle.hpp:31
static constexpr uint16_t to_led_index(const float angle_rad, const float z)
convert a lamp coordinate to a led index
Definition: lamp_type.hpp:1332
static constexpr float to_helix_z(const int16_t n)
Convert a led index to an helix height coordinate.
Definition: lamp_type.hpp:1292
static constexpr float speedDampening
low speed dampening [0-0.99] at every steps (viscosity)
Definition: particle.hpp:28
static constexpr float linearSpeedGain
gain for the linear speeds
Definition: particle.hpp:26
static constexpr float rowHeight_mm
height gained by the helix in one turn
Definition: particle.hpp:34
static constexpr bool is_led_index_valid(const int16_t ledIndex)
True if the given strip index is a valid one.
Definition: lamp_type.hpp:1313
static constexpr float maxAngularSpeed_radS
max angular speed in radians/s
Definition: particle.hpp:30
static constexpr float cylinderRadius_m
radius of the cylinder, in meters
Definition: particle.hpp:24
static constexpr float ledsPerTurn
leds around the tube. Not an integer: rows are sheared
Definition: particle.hpp:33
static constexpr float cylinderRadius_mm
radius of the cylinder, in millimeters ratio between the speed gained around the tube and the physica...
Definition: particle.hpp:36
static constexpr float reboundCoeff
low rebound [0 - 1] on walls
Definition: particle.hpp:27
static constexpr float ledLength_mm
length of a led along the strip
Definition: particle.hpp:35
static constexpr float wrap_angle(const float angle_rad)
Wrap an angle in radians between 0 and 2*PI.
Definition: utils.h:115
static constexpr float c_TWO_PI
2*PI constant
Definition: constants.h:41
static constexpr float c_HALF_PI
PI/2 constant.
Definition: constants.h:42
Define a particle in 3D space. it has a position and speed.
Definition: particle.hpp:44
Particle(const utils::vec3d &positionCartesian)
Construct a Particle from a 3D cartesian position.
Definition: particle.hpp:52
void move_by(const utils::vec2d &offset_mm, const int16_t ledIndex)
Move this particle on the cylinder surface, to a led already computed from this offset.
Definition: particle.hpp:251
float theta_rad
position, in radians
Definition: particle.hpp:266
void apply_acceleration(const utils::vec3d &accelerationCartesian_m, const float deltaTime_s, const bool shouldContrain=true)
apply a cartesian acceleration to this particulate
Definition: particle.hpp:107
utils::vec2d compute_speed_increment(const utils::vec3d &accelerationCartesian_m, const float deltaTime_s) const
Compute the speed increment in the constraint lamp body.
Definition: particle.hpp:71
uint16_t to_lamp_index() const
Convert this particle coordinates to a constraint lamp index.
Definition: particle.hpp:205
int16_t _savedLampIndex
optimization
Definition: particle.hpp:270
utils::vec2d update_speed(const utils::vec3d &accelerationCartesian_m, const float deltaTime_s)
Dampen the speed and apply a cartesian acceleration to it, without moving the particle.
Definition: particle.hpp:87
int16_t to_lamp_index_no_bounds() const
Convert this particle coordinates to an unconstraint lamp index: the nearest led of the helix.
Definition: particle.hpp:207
Particle simulate_after_acceleration(const utils::vec3d &accelerationCartesian_m, const float deltaTime_s, const bool shouldContrain=true) const
Simulate this particle movement as a new particle.
Definition: particle.hpp:142
static int16_t led_index_at(const float positionTheta_rad, const float positionZ_mm)
Find the led nearest to a position, following the real helix of the strip. A led height depends on it...
Definition: particle.hpp:216
float thetaSpeed_radS
angular speed, in radian/seconds
Definition: particle.hpp:263
Particle(const Particle &other)=default
Copy constructor for a particle.
float zSpeed_mS
linear speed, in meter/seconds
Definition: particle.hpp:264
void constraint_into_lamp_body()
Contrain the particle movement to the cylinder, limiting the movement at the extremities.
Definition: particle.hpp:154
utils::vec2d get_surface_speed() const
Return the speed on the cylinder surface, in mm/s (around the tube, along the height)
Definition: particle.hpp:245
float z_mm
height, in millimeters.
Definition: particle.hpp:267
void dampen_speed(const float dampenFactor)
Bleed off some speed. Simulate a dampening factor (like friction loss)
Definition: particle.hpp:195
Particle()=default
Default particle init.
static float potential_at_led(const utils::vec3d &accelerationCartesian_m, const int16_t ledIndex)
Potential energy of a led under an acceleration, consistent with the acceleration of a particle.
Definition: particle.hpp:236
Particle(const float positionTheta_rad, const float positionZ_mm)
Construct a particle from an angle and height.
Definition: particle.hpp:60
int16_t led_index_at_offset(const utils::vec2d &offset_mm) const
Return the led under this particle, once moved by an offset on the cylinder surface (in mm).
Definition: particle.hpp:225
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:120
static constexpr float lampHeight_mm
Computation of the lamp height.
Definition: lamp_type.hpp:447
static constexpr float maxWidthFloat
Width as a precise floating point number, equal to stripXCoordinates.
Definition: lamp_type.hpp:379
static constexpr float ledStripWidth_mm
real size of the led strip in use
Definition: lamp_type.hpp:433
static constexpr float ledPerTurns
Computation of the led count per turns.
Definition: lamp_type.hpp:445
static constexpr float ledSize_mm
Number of leds per one turn of led strip.
Definition: lamp_type.hpp:441
static constexpr float lampBodyRadius_mm
real radius of the lamp body
Definition: lamp_type.hpp:430
2d vector in any space
Definition: vector_math.h:15
float x
x coordinate in 2D
Definition: vector_math.h:16
float y
y coordinate in 2D
Definition: vector_math.h:17
3d vector in any space
Definition: vector_math.h:54
float z
z coordinate of a 3D vector
Definition: vector_math.h:55
Define the system hardware constants.
User defined constants, relative to specific lamp types.
Define useful functions.
Define vectors and rotation matrices, and the possibility to rotate vectors.