1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#![macro_use]
use crate::f32::{X_VEC, Y_VEC};
// Agnostic to SIMD and non-simd
// `$f` here could be a primitive like `f32`, or a SIMD primitive like `f32x8`.
macro_rules! create_vec_shared {
($f:ident, $vec3_ty:ident, $vec4_ty:ident) => {
impl $vec3_ty {
/// Calculates the Hadamard product (element-wise multiplication).
pub fn hadamard_product(self, rhs: Self) -> Self {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
z: self.z * rhs.z,
}
}
/// Returns the vector magnitude squared
pub fn magnitude_squared(self) -> $f {
self.x.powi(2) + self.y.powi(2) + self.z.powi(2)
}
/// Returns the vector magnitude
pub fn magnitude(&self) -> $f {
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
}
/// Normalize, modifying in place.
pub fn normalize(&mut self) {
let mag = self.magnitude();
self.x /= mag;
self.y /= mag;
self.z /= mag;
}
/// Returns the normalized version of the vector.
pub fn to_normalized(self) -> Self {
self / self.magnitude()
}
/// Returns the dot product with another vector.
pub fn dot(&self, rhs: Self) -> $f {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
/// Calculate the cross product.
pub fn cross(&self, rhs: Self) -> Self {
Self {
x: self.y * rhs.z - self.z * rhs.y,
y: self.z * rhs.x - self.x * rhs.z,
z: self.x * rhs.y - self.y * rhs.x,
}
}
/// Project a vector onto a plane defined by its normal vector. Assumes self and `plane_norm`
/// are unit vectors.
pub fn project_to_plane(self, plane_norm: Self) -> Self {
self - plane_norm * self.dot(plane_norm)
}
/// Projects this vector onto another vector.
pub fn project_to_vec(self, other: Self) -> Self {
other * (self.dot(other) / other.magnitude_squared())
}
}
impl Default for $vec3_ty {
fn default() -> Self {
Self::new_zero()
}
}
impl Add for $vec3_ty {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl AddAssign for $vec3_ty {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl Add<$f> for $vec3_ty {
type Output = Self;
fn add(self, rhs: $f) -> Self::Output {
Self {
x: self.x + rhs,
y: self.y + rhs,
z: self.z + rhs,
}
}
}
impl AddAssign<$f> for $vec3_ty {
fn add_assign(&mut self, rhs: $f) {
self.x += rhs;
self.y += rhs;
self.z += rhs;
}
}
impl Sub for $vec3_ty {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl SubAssign for $vec3_ty {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl Sub<$f> for $vec3_ty {
type Output = Self;
fn sub(self, rhs: $f) -> Self::Output {
Self {
x: self.x - rhs,
y: self.y - rhs,
z: self.z - rhs,
}
}
}
impl SubAssign<$f> for $vec3_ty {
fn sub_assign(&mut self, rhs: $f) {
self.x -= rhs;
self.y -= rhs;
self.z -= rhs;
}
}
impl Mul<$f> for $vec3_ty {
type Output = Self;
fn mul(self, rhs: $f) -> Self::Output {
Self {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
}
}
}
impl MulAssign<$f> for $vec3_ty {
fn mul_assign(&mut self, rhs: $f) {
self.x = self.x * rhs;
self.y = self.y * rhs;
self.z = self.z * rhs;
}
}
impl Div<$f> for $vec3_ty {
type Output = Self;
fn div(self, rhs: $f) -> Self::Output {
Self {
x: self.x / rhs,
y: self.y / rhs,
z: self.z / rhs,
}
}
}
impl DivAssign<$f> for $vec3_ty {
fn div_assign(&mut self, rhs: $f) {
self.x = self.x / rhs;
self.y = self.y / rhs;
self.z = self.z / rhs;
}
}
impl $vec4_ty {
/// Calculates the Hadamard product (element-wise multiplication).
pub fn hadamard_product(self, rhs: Self) -> Self {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
z: self.z * rhs.z,
w: self.w * rhs.w,
}
}
/// Returns the vector magnitude squared
pub fn magnitude_squared(self) -> $f {
self.x.powi(2) + self.y.powi(2) + self.z.powi(2) + self.w.powi(2)
}
/// Returns the vector magnitude
pub fn magnitude(&self) -> $f {
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2) + self.w.powi(2)).sqrt()
}
/// Normalize, modifying in place.
pub fn normalize(&mut self) {
let mag = self.magnitude();
self.x /= mag;
self.y /= mag;
self.z /= mag;
self.w /= mag;
}
/// Returns the normalized version of the vector.
pub fn to_normalized(self) -> Self {
self / self.magnitude()
}
/// Returns the dot product with another vector.
pub fn dot(&self, rhs: Self) -> $f {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z + self.w * rhs.w
}
/// Project a vector onto a plane defined by its normal vector. Assumes self and `plane_norm`
/// are unit vectors.
pub fn project_to_plane(self, plane_norm: Self) -> Self {
self - plane_norm * self.dot(plane_norm)
}
/// Projects this vector onto another vector.
pub fn project_to_vec(self, other: Self) -> Self {
other * (self.dot(other) / other.magnitude_squared())
}
}
impl Default for $vec4_ty {
fn default() -> Self {
Self::new_zero()
}
}
impl Add for $vec4_ty {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
w: self.w + rhs.w,
}
}
}
impl AddAssign for $vec4_ty {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
self.w += rhs.w;
}
}
impl Add<$f> for $vec4_ty {
type Output = Self;
fn add(self, rhs: $f) -> Self::Output {
Self {
x: self.x + rhs,
y: self.y + rhs,
z: self.z + rhs,
w: self.w + rhs,
}
}
}
impl AddAssign<$f> for $vec4_ty {
fn add_assign(&mut self, rhs: $f) {
self.x += rhs;
self.y += rhs;
self.z += rhs;
self.w += rhs;
}
}
impl Sub for $vec4_ty {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
w: self.w - rhs.w,
}
}
}
impl SubAssign for $vec4_ty {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
self.w -= rhs.w;
}
}
impl Sub<$f> for $vec4_ty {
type Output = Self;
fn sub(self, rhs: $f) -> Self::Output {
Self {
x: self.x - rhs,
y: self.y - rhs,
z: self.z - rhs,
w: self.w - rhs,
}
}
}
impl SubAssign<$f> for $vec4_ty {
fn sub_assign(&mut self, rhs: $f) {
self.x -= rhs;
self.y -= rhs;
self.z -= rhs;
self.w -= rhs;
}
}
impl Mul<$f> for $vec4_ty {
type Output = Self;
fn mul(self, rhs: $f) -> Self::Output {
Self {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
w: self.w * rhs,
}
}
}
impl MulAssign<$f> for $vec4_ty {
fn mul_assign(&mut self, rhs: $f) {
self.x = self.x * rhs;
self.y = self.y * rhs;
self.z = self.z * rhs;
self.w = self.w * rhs;
}
}
impl Div<$f> for $vec4_ty {
type Output = Self;
fn div(self, rhs: $f) -> Self::Output {
Self {
x: self.x / rhs,
y: self.y / rhs,
z: self.z / rhs,
w: self.w / rhs,
}
}
}
impl DivAssign<$f> for $vec4_ty {
fn div_assign(&mut self, rhs: $f) {
self.x = self.x / rhs;
self.y = self.y / rhs;
self.z = self.z / rhs;
self.w = self.w / rhs;
}
}
};
}
macro_rules! create_vec {
($f:ident) => {
/// A len-2 column vector.
#[derive(Default, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "encode", derive(Encode, Decode))]
pub struct Vec2 {
pub x: $f,
pub y: $f,
}
impl Vec2 {
pub fn new(x: $f, y: $f) -> Self {
Self { x, y }
}
pub fn magnitude(&self) -> $f {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
/// Radians, CW from north.
pub fn track(&self) -> $f {
self.x.atan2(self.y)
}
/// Returns the remaining scalar after removing the nth element (0-based).
/// For example:
/// truncate_n(0) => drops .x, returns y
/// truncate_n(1) => drops .y, returns x
pub fn truncate_n(&self, n: usize) -> $f {
match n {
0 => self.y,
1 => self.x,
_ => panic!("{} is out of range for Vec2", n),
}
}
}
#[cfg(feature = "std")]
impl fmt::Display for Vec2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "|{:.4}, {:.4}|", self.x, self.y)?;
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "encode", derive(Encode, Decode))]
/// A len-3 column vector.
pub struct Vec3 {
pub x: $f,
pub y: $f,
pub z: $f,
}
impl From<[$f; 3]> for Vec3 {
fn from(v: [$f; 3]) -> Self {
Self {
x: v[0],
y: v[1],
z: v[2],
}
}
}
impl Neg for Vec3 {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
x: -self.x,
y: -self.y,
z: -self.z,
}
}
}
impl Sum<Vec3> for Vec3 {
fn sum<I: Iterator<Item = Vec3>>(iter: I) -> Vec3 {
iter.fold(Vec3::new_zero(), |a, b| a + b)
}
}
impl Vec3 {
pub const fn new(x: $f, y: $f, z: $f) -> Self {
Self { x, y, z }
}
pub const fn new_zero() -> Self {
Self {
x: 0.,
y: 0.,
z: 0.,
}
}
pub const fn x() -> Self {
Self {
x: 1.,
y: 0.,
z: 0.,
}
}
pub const fn y() -> Self {
Self {
x: 0.,
y: 1.,
z: 0.,
}
}
pub const fn z() -> Self {
Self {
x: 0.,
y: 0.,
z: 1.,
}
}
/// Construct from the first 3 values in a slice: &[x, y, z].
pub fn from_slice(slice: &[$f]) -> Result<Self, crate::BufError> {
if slice.len() < 3 {
return Err(crate::BufError {});
}
Ok(Self {
x: slice[0],
y: slice[1],
z: slice[2],
})
}
/// Convert to a len-3 array: [x, y, z].
pub fn to_arr(&self) -> [$f; 3] {
[self.x, self.y, self.z]
}
pub fn splat(val: $f) -> Self {
Self {
x: val,
y: val,
z: val,
}
}
/// Returns this vector with the nth element removed (0-based).
/// For example:
/// truncate_n(0) => drops .x, returns Vec2(y, z)
/// truncate_n(1) => drops .y, returns Vec2(x, z)
/// truncate_n(2) => drops .z, returns Vec2(x, y)
pub fn truncate_n(&self, n: usize) -> Vec2 {
match n {
0 => Vec2::new(self.y, self.z),
1 => Vec2::new(self.x, self.z),
2 => Vec2::new(self.x, self.y),
_ => panic!("{} is out of range for Vec3", n),
}
}
/// Returns a sum of all elements
pub fn sum(&self) -> $f {
self.x + self.y + self.z
}
/// Returns an arbitrary unit vector perpendicular to it.
pub fn any_perpendicular(&self) -> Self {
// The vec we cross with it determines the resulting direction.
let v = self.to_normalized();
if v.magnitude_squared() < 1e-12 {
return X_VEC;
}
// Pick an axis least aligned with v, so the cross product is well-conditioned.
let a = if v.x.abs() < 0.9 { X_VEC } else { Y_VEC };
v.cross(a).to_normalized()
}
pub fn min(self, other: Self) -> Self {
Self {
x: self.x.min(other.x),
y: self.y.min(other.y),
z: self.z.min(other.z),
}
}
pub fn max(self, other: Self) -> Self {
Self {
x: self.x.max(other.x),
y: self.y.max(other.y),
z: self.z.max(other.z),
}
}
}
#[cfg(feature = "std")]
impl fmt::Display for Vec3 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "|{:.4}, {:.4}, {:.4}|", self.x, self.y, self.z)?;
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "encode", derive(Encode, Decode))]
/// A len-4 column vector
pub struct Vec4 {
pub x: $f,
pub y: $f,
pub z: $f,
pub w: $f,
}
impl From<[$f; 4]> for Vec4 {
fn from(v: [$f; 4]) -> Self {
Self {
x: v[0],
y: v[1],
z: v[2],
w: v[3],
}
}
}
impl Neg for Vec4 {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
x: -self.x,
y: -self.y,
z: -self.z,
w: -self.w,
}
}
}
impl Sum<Vec4> for Vec4 {
fn sum<I: Iterator<Item = Vec4>>(iter: I) -> Vec4 {
iter.fold(Vec4::new_zero(), |a, b| a + b)
}
}
impl Vec4 {
pub fn new(x: $f, y: $f, z: $f, u: $f) -> Self {
Self { x, y, z, w: u }
}
pub const fn new_zero() -> Self {
Self {
x: 0.,
y: 0.,
z: 0.,
w: 0.,
}
}
/// Construct from the first 4 values in a slice: &[x, y, z, w].
pub fn from_slice(slice: &[$f]) -> Result<Self, crate::BufError> {
if slice.len() < 4 {
return Err(crate::BufError {});
}
Ok(Self {
x: slice[0],
y: slice[1],
z: slice[2],
w: slice[3],
})
}
/// Convert to a len-4 array: [x, y, z, w].
pub fn to_arr(&self) -> [$f; 4] {
[self.x, self.y, self.z, self.w]
}
pub fn splat(val: $f) -> Self {
Self {
x: val,
y: val,
z: val,
w: val,
}
}
/// Remove the nth element. Used in our inverse calculations.
pub fn truncate_n(&self, n: usize) -> Vec3 {
match n {
0 => Vec3::new(self.y, self.z, self.w),
1 => Vec3::new(self.x, self.z, self.w),
2 => Vec3::new(self.x, self.y, self.w),
3 => Vec3::new(self.x, self.y, self.z),
_ => panic!("{:?} is out of range", n),
}
}
/// Returns a sum of all elements
pub fn sum(&self) -> $f {
self.x + self.y + self.z + self.w
}
/// Remove the w element.
pub fn xyz(&self) -> Vec3 {
Vec3::new(self.x, self.y, self.z)
}
}
/// Calculate the dihedral angle between 4 positions. The positions must be in order by
/// connection/bond, although both directions produce identical results. Compared to `calc_dihedral_angle()`,
/// this function's API is more clear if you have the set of positions directly.
pub fn calc_dihedral_angle_v2(posits: &(Vec3, Vec3, Vec3, Vec3)) -> $f {
let mid = posits.1 - posits.2;
let adj_next = posits.2 - posits.3;
let adj_prev = posits.0 - posits.1;
calc_dihedral_angle(mid, adj_next, adj_prev)
}
/// Calculate the dihedral angle between 4 positions (3 bonds). Compared to `calc_dihedral_angle_v2()`,
/// this function's API is more clear if you have the bonds/connections, but not the positions.
/// this might happen if you are doing certain vector operations, for example.
///
/// The `bonds` are one position, subtracted from the next.
///
/// Order matters. For posits 0-1-2-3, with connections 0-1, 1-2, 2-3:
/// middle: posit 1 - posit 2.
/// adj next: posit 2 - posit 3
/// adj prev: posit 0 - posit 1
pub fn calc_dihedral_angle(
bond_middle: Vec3,
bond_adj_next: Vec3,
bond_adj_prev: Vec3,
) -> $f {
// Project the next and previous bonds onto the plane that has this bond as its normal.
// Re-normalize after projecting.
let mid_norm = bond_middle.to_normalized();
let bond1_on_plane = bond_adj_next.project_to_plane(mid_norm).to_normalized();
let bond2_on_plane = -bond_adj_prev.project_to_plane(mid_norm).to_normalized();
let result = bond1_on_plane.dot(bond2_on_plane).acos();
// This can happen perhaps due to numerical precision problems on systems
// where all 4 points are coplanar.
// An alternative we could use instead of this check, starting at the line above:
// x = cos φ, y = sin φ (see Allen & Tildesley §4.5)
//let x = b1.dot(b2);
//let y = b1.cross(b2).dot(mid_norm);
//let mut φ = y.atan2(x); // range (-π, π]
//if φ < 0.0 { φ += TAU; } // put it into [0, τ) if you prefer
if result.is_nan() {
return 0.;
}
// The dot product approach to angles between vectors only covers half of possible
// rotations; use a determinant of the 3 vectors as matrix columns to determine if what we
// need to modify is on the second half.
let det = det_from_cols(bond1_on_plane, bond2_on_plane, mid_norm);
if det < 0. { result } else { TAU - result }
}
#[cfg(feature = "std")]
impl fmt::Display for Vec4 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"|{:.4}, {:.4}, {:.4}, {:.4}|",
self.x, self.y, self.z, self.w
)?;
Ok(())
}
}
#[cfg(feature = "cuda")]
/// Convert a collection of `Vec3` into Cuda arrays of float3.
/// Note: Ignore resources that say you need to pad to 16-bytes to map to float3; they're wrong.
pub fn vec3s_to_dev(stream: &Arc<CudaStream>, data_host: &[Vec3]) -> CudaSlice<$f> {
let mut result = Vec::with_capacity(data_host.len() * 3);
// todo: Ref etcs A/R; you are making a double copy here.
for v in data_host {
result.push(v.x as $f);
result.push(v.y as $f);
result.push(v.z as $f);
}
stream.memcpy_stod(&result).unwrap()
}
#[cfg(feature = "cuda")]
/// Convert a Cuda array of `float3` into Vec3.
/// Note: Ignore resources that say you need to pad to 16-bytes to map to float3; they're wrong.
pub fn vec3s_from_dev(stream: &Arc<CudaStream>, data_dev: &CudaSlice<$f>) -> Vec<Vec3> {
let data_host = stream.memcpy_dtov(data_dev).unwrap();
data_host
.chunks_exact(3)
.map(|chunk| Vec3::new(chunk[0], chunk[1], chunk[2]))
.collect()
}
#[cfg(feature = "cuda")]
unsafe impl cudarc::driver::DeviceRepr for Vec3 {}
};
}