forked from willsheffler/pymol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxyzGeom.py
1344 lines (1103 loc) · 41.8 KB
/
xyzGeom.py
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Easy 3D Linear Algebra, like xyz\* in rosetta
"""
from random import gauss, uniform
from math import pi, sqrt, sin, cos, acos, asin, atan2, degrees, radians, copysign
from itertools import chain, product, izip
import operator as op
import re
EPS = 0.000000001
SQRTEPS = sqrt(EPS)
def isint(x): return type(x) is int
def isfloat(x): return type(x) is float
def isnum(x): return isint(x) or isfloat(x)
def ispoint(x): return type(x) is Point
def ispoints(x): return type(x) is Points
def isvec(x): return type(x) is Vec
def isvecs(x): return type(x) is Vecs
def isvorpt(x): return isvec(x) or ispoint(x)
def isline(x): return type(x) is Line
def isplane(x): return type(x) is Plane
def ismat(x): return type(x) is Mat
def isxform(x): return type(x) is Xform
def islist(x): return type(x) is list
def istuple(x): return type(x) is tuple
def isiter(x): return hasattr(x, "__iter__")
def sametype(x, y): return type(x) is type(y)
def allints(*X): return reduce(op.and_, (type(x) is int for x in X), True)
def allfloats(*X): return reduce(op.and_, (type(x) is float for x in X), True)
def allnums(*X): return reduce(op.and_, (isint(x) or isfloat(x)
for x in X), True)
def allpoints(*X): return reduce(op.and_, (type(x) is Point for x in X), True)
def allvecs(*X): return reduce(op.and_, (type(x) is Vec for x in X), True)
def allvorpts(*X): return reduce(op.and_, (isvec(x) or ispoint(x)
for x in X), True)
def alllines(*X): return reduce(op.and_, (type(x) is Line for x in X), True)
def allplanes(*X): return reduce(op.and_, (type(x) is Plane for x in X), True)
def allmats(*X): return reduce(op.and_, (type(x) is Mat for x in X), True)
def allxforms(*X): return reduce(op.and_, (type(x) is Xform for x in X), True)
def alllists(*X): return reduce(op.and_, (type(x) is list for x in X), True)
def alltuples(*X): return reduce(op.and_, (type(x) is tuple for x in X), True)
def alliters(*X): return reduce(op.and_, (hasattr(x, "__iter__")
for x in X), True)
def anyints(*X): return reduce(op.or_, (type(x) is int for x in X), False)
def anyfloats(*X): return reduce(op.or_, (type(x) is float for x in X), False)
def anynums(*X): return reduce(op.or_, (isint(x) or isfloat(x)
for x in X), False)
def anypoints(*X): return reduce(op.or_, (type(x) is Point for x in X), False)
def anyvecs(*X): return reduce(op.or_, (type(x) is Vec for x in X), False)
def anyvorpts(*X): return reduce(op.or_, (isvec(x) or ispoint(x)
for x in X), False)
def anylines(*X): return reduce(op.or_, (type(x) is Line for x in X), False)
def anyplanes(*X): return reduce(op.or_, (type(x) is Plane for x in X), False)
def anymats(*X): return reduce(op.or_, (type(x) is Mat for x in X), False)
def anyxforms(*X): return reduce(op.or_, (type(x) is Xform for x in X), False)
def anylists(*X): return reduce(op.or_, (type(x) is list for x in X), False)
def anytuples(*X): return reduce(op.or_, (type(x) is tuple for x in X), False)
def anyiters(*X): return reduce(op.or_, (hasattr(x, "__iter__")
for x in X), False)
def typeerror(o, t1, t2):
raise TypeError("unsupported operand type(s) for " + o + ": '" +
type(t1).__name__ + "' and '" + type(t2).__name__ + "'")
def stripfloats(s):
"""
>>> stripfloats(" 1.10 100.00 0. 1.230000 3.34534500 (0.00000) ")
' 1.1 100 0 1.23 3.345345 (0) '
"""
s = re.sub(r"(\b\d+[.]\d*?)0+\b", r"\1", s)
s = re.sub(r"(\b\d+)[.]([ ,\s\)$])", r"\1\2", s)
return s
def sin_cos_range(x):
assert -1.001 < x < 1.001
return min(1.0, max(-1.0, x))
class Point(object):
"""a Point like xyzVector<Real> in rosetta
>>> p = Point(1,2,3)
>>> p
P(1.000000,2.000000,3.000000)
>>> print p
P(1,2,3)
>>> print ispoint(p),isvec(p)
True False
>>> 10+p
Traceback (most recent call last):
TypeError: unsupported operand type(s) for +: 'int' and 'Point'
>>> print 10*p
P(10,20,30)
>>> p.key()
(1.0, 2.0, 3.0)
elementwise mult
>>> p*p
Traceback (most recent call last):
TypeError: unsupported operand type(s) for *: 'Point' and 'Point'
>>> assert Point(1,0,-0) == Point(1,-0,0)
>>> round(Point(1,2,3).distance(Point(3,2,1)),6)
2.828427
>>> r = randpoint()
>>> assert r.distance(r) < EPS
>>> p.angle(p)
Traceback (most recent call last):
AttributeError: 'Point' object has no attribute 'angle'
>>> p.dot(p)
Traceback (most recent call last):
AttributeError: 'Point' object has no attribute 'dot'
>>> p.length()
Traceback (most recent call last):
AttributeError: 'Point' object has no attribute 'length'
"""
def __init__(self, x=0.0, y=None, z=None):
if y is None:
if isnum(x):
self.x, self.y, self.z = (float(x),) * 3
elif isvec(x) | ispoint(x):
self.x, self.y, self.z = x.x, x.y, x.z
elif isiter(x):
i = iter(x)
self.x, self.y, self.z = i.next(), i.next(), i.next()
else:
raise TypeError
elif z is not None:
assert isnum(x) and isnum(y) and isnum(z)
self.x, self.y, self.z = float(x), float(y), float(z)
else:
raise TypeError
assert allfloats(self.x, self.y, self.z)
def distance_squared(p, q):
if not allpoints(p, q):
raise TypeError("distance between vecs / ints doesn't make sense")
return reduce(op.add, ((f - g)**2 for f, g in zip(p, q)))
def distance(u, v): return sqrt(u.distance_squared(v))
def __sub__(u, r):
if allpoints(u, r):
return Vec(u.x - r.x, u.y - r.y, u.z - r.z)
return u + -r
def __rsub__(u, l):
return l + -u
def __eq__(self, other):
return (type(self) is type(other) and
abs(self.x - other.x) < EPS and
abs(self.y - other.y) < EPS and
abs(self.z - other.z) < EPS)
def rounded(self, sd):
return Vec(round(self.x, sd), round(self.y, sd), round(self.z, sd))
def __len__(v):
return 3
def abs(v):
return Vec(abs(v.x), abs(v.y), abs(v.z))
def __getitem__(v, i):
if i is 0:
return v.x
if i is 1:
return v.x
if i is 2:
return v.x
raise IndexError
def tuple(v):
return (v.x, v.y, v.z)
def key(v):
return v.rounded(6).tuple()
def __iter__(p):
yield p.x
yield p.y
yield p.z
def __mul__(p, a):
if isnum(a):
return Point(a * p.x, a * p.y, a * p.z)
typeerror('*', p, a)
def __rmul__(p, a):
if isnum(a):
return Point(a * p.x, a * p.y, a * p.z)
typeerror('*', a, p)
def __repr__(self):
return "P(%f,%f,%f)" % (self.x, self.y, self.z)
def __str__(self):
return stripfloats(repr(self))
class Points(list):
pass
class Vec(Point):
"""a 3D direction
>>> p = Point(1,1,1)
>>> v = Vec(1,2,3)
>>> p*v
Traceback (most recent call last):
TypeError: unsupported operand type(s) for *: 'Point' and 'Vec'
>>> v*p
Traceback (most recent call last):
TypeError: unsupported operand type(s) for *: 'Vec' and 'Point'
>>> v/p
Traceback (most recent call last):
TypeError: unsupported operand type(s) for /: 'Vec' and 'Point'
>>> print p+v
P(2,3,4)
>>> print p-v
P(0,-1,-2)
>>> print v-p
Traceback (most recent call last):
TypeError: bad operand type for unary -: 'Point'
>>> assert p-(p+v) == -v and p-p+v == v
>>> v.distance_squared(v)
Traceback (most recent call last):
TypeError: distance between vecs / ints doesn't make sense
pairwise +/-/*/div on vecs ok
>>> print v+v
V(2,4,6)
>>> print v-v
V(0,0,0)
>>> print v*v
V(1,4,9)
>>> print v/v
V(1,1,1)
"""
def __init__(self, *args, **kwargs):
super(Vec, self).__init__(*args, **kwargs)
def dot(u, v):
assert isvec(v)
return u.x * v.x + u.y * v.y + u.z * v.z
def cross(u, v):
assert isvec(v)
return Vec(u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x)
__and__ = dot
__or__ = cross
def normdot(u, v):
assert isvec(v)
return min(1.0, max(-1.0, u.dot(v) / u.length() / v.length()))
def angle(u, v):
assert isvec(v)
d = u.normdot(v)
if d > 1.0 - EPS:
return 0.0
if d < EPS - 1.0:
return pi
return acos(d)
def angle_degrees(u, v): return degrees(u.angle(v))
def lineangle(u, v):
assert isinstance(v, Vec)
if u.length() < SQRTEPS or v.length < SQRTEPS:
return 0.0
ang = abs(acos(u.normdot(v)))
return ang if ang < pi / 2.0 else pi - ang
def linemaxangle(u, v):
return math.pi - u.lineangle(v)
def lineangle_degrees(u, v): return degrees(lineangle(u, v))
def linemaxangle_degrees(u, v): return degrees(linemaxangle(u, v))
def length(u): return sqrt(u.dot(u))
def length_squared(u): return u.dot(u)
def unit(v):
if abs(v.x) > SQRTEPS:
return v / v.x
elif abs(v.y) > SQRTEPS:
return v / v.y
elif abs(v.z) > SQRTEPS:
return v / v.z
def normalize(u):
l = u.length()
u.x /= l
u.y /= l
u.z /= l
def normalized(u):
v = Vec(u)
v.normalize()
return v
def outer(u, v):
assert isvec(v)
return Mat(u.x * v.x, u.x * v.y, u.x * v.z,
u.y * v.x, u.y * v.y, u.y * v.z,
u.z * v.x, u.z * v.y, u.z * v.z)
def __add__(v, r):
assert isvorpt(v)
if isnum(r):
return type(v)(v.x + r, v.y + r, v.z + r)
elif isvorpt(r):
if isvec(v) and isvec(r):
return Vec(v.x + r.x, v.y + r.y, v.z + r.z)
if isvec(v) or isvec(r):
return Point(v.x + r.x, v.y + r.y, v.z + r.z)
raise TypeError
return v.__radd__(v)
def __radd__(v, r):
return v + r
def __mul__(p, a):
assert isvorpt(p)
if isnum(a):
return type(p)(f * a for f in p)
if allvecs(p, a):
return type(p)(f * g for f, g in izip(p, a))
if isvorpt(a):
typeerror('*', p, a)
else:
return a.__rmul__(p)
def __rmul__(u, a):
if anypoints(u, a):
typeerror('*', a, u)
return u * a
def __neg__(u):
return Vec(-u.x, -u.y, -u.z)
def __div__(u, a):
if anypoints(u, a):
typeerror('/', u, a)
if isnum(a):
return Vec(u.x / a, u.y / a, u.z / a)
if isvec(a):
return Vec(u.x / a.x, u.y / a.y, u.z / a.z)
return a.__rdiv__(u)
def __rdiv__(u, a):
return a / u
def __repr__(self):
return "V(%f,%f,%f)" % (self.x, self.y, self.z)
def __str__(self):
return stripfloats(repr(self))
def proj(v, u):
"""
>>> print Vec(1,1,1).proj(Vec(abs(gauss(0,10)),0,0))
V(1,0,0)
>>> print Vec(2,2,2).proj(Vec(abs(gauss(0,10)),0,0))
V(2,0,0)
>>> u,v = randvec(2)
>>> puv = v.proj(u).normalized()
>>> assert abs(abs(puv.dot(u.normalized()))-1.0) < EPS
"""
return u.dot(v) / u.dot(u) * u
def perp(v, u):
"""
>>> u = Vec(1,0,0); v = Vec(1,1,1)
>>> print v.perp(u)
V(0,1,1)
>>> u,v = randvec(2)
>>> assert abs(v.perp(u).dot(u)) < EPS
>>> assert abs( u.dot( v.perp(u) ) ) < EPS
"""
return v - v.proj(u)
Ux = Vec(1, 0, 0)
Uy = Vec(0, 1, 0)
Uz = Vec(0, 0, 1)
V0 = Vec(0, 0, 0)
Px = Point(1, 0, 0)
Py = Point(0, 1, 0)
Pz = Point(0, 0, 1)
P0 = Point(0, 0, 0)
class Vecs(list):
pass
def randpoint(n=1):
if n is 1:
return Point(gauss(0, 1), gauss(0, 1), gauss(0, 1))
return Points(Point(gauss(0, 1), gauss(0, 1), gauss(0, 1)) for i in range(n))
def randvec(n=1):
if n is 1:
return Vec(gauss(0, 1), gauss(0, 1), gauss(0, 1))
return Vecs(Vec(gauss(0, 1), gauss(0, 1), gauss(0, 1)) for i in range(n))
def randnorm(n=1):
"""
>>> assert abs(randnorm().length()-1.0) < 0.0000001
"""
if n is 1:
return randvec().normalized()
return Vecs(randvec().normalized() for i in range(n))
def coplanar(x1, x2, x3, x4):
"""
>>> u,v,w = randpoint(3)
>>> a,b,c = (gauss(0,10) for i in range(3))
>>> assert coplanar(u, v, w, u + a*(u-v) + b*(v-w) + c*(w-u) )
>>> assert not coplanar(u, v, w, u + a*(u-v) + b*(v-w) + c*(w-u) + randvec().cross(u-v) )
"""
if allpoints(x1, x2, x3, x4):
return abs((x3 - x1).dot((x2 - x1).cross(x4 - x3))) < SQRTEPS
raise NotImplementedError
def rmsd(l, m):
"""
>>> l,m = randpoint(6),randpoint(6)
>>> rmsd(l,l)
0.0
"""
assert ispoints(l)
assert ispoints(m)
rmsd = 0.0
for u, v in izip(l, m):
rmsd += u.distance_squared(v)
return sqrt(rmsd)
def dihedral(p1, p2, p3, p4=None):
"""
3 Vecs or 4 points
>>> dihedral_degrees(Px,Py,P0,Pz)
90.0
>>> dihedral_degrees(Px,P0,Py,Pz)
-90.0
>>> dihedral_degrees(Uy,Uz,Ux)
90.0
>>> dihedral_degrees(Uy,Uz,Ux)
90.0
"""
if allpoints(p1, p2, p3, p4):
a = (p2 - p1).normalized()
b = (p3 - p2).normalized()
c = (p4 - p3).normalized()
x = -a.dot(c) + a.dot(b) * b.dot(c)
y = a.dot(b.cross(c))
return atan2(y, x)
if allvecs(p1, p2, p3) and p4 is None:
return dihedral(P0, P0 + p1, P0 + p1 + p2, P0 + p1 + p2 + p3)
if anypoints(p1, p2, p3, p4):
raise NotImplementedError
def dihedral_degrees(p1, p2, p3, p4=None):
return degrees(dihedral(p1, p2, p3, p4))
def angle(p1, p2, p3=None):
if allvecs(p1, p2) and p3 is None:
return p1.angle(p2)
elif allpoints(p1, p2, p3):
a = (p2 - p1).normalized()
b = (p2 - p3).normalized()
return acos(a.dot(b))
class Line(object):
"""
from a direction and a point
>>> print Line(Ux,P0)
Line( P(0,0,0) + r * V(1,0,0) )
from two points:
>>> print Line(P0,Px)
Line( P(1,0,0) + r * V(1,0,0) )
>>> print Line(P0,P0)
Traceback (most recent call last):
assert direction.length_squared() > SQRTEPS
AssertionError
>>> assert Line(Ux,P0) == Line(Ux,Px)
>>> assert Line(Ux,P0) == Line(-Ux,Px)
>>> assert Line(Ux,P0) != Line(Ux,Py)
"""
def __init__(self, direction, position):
assert ispoint(position)
if ispoint(direction):
direction = position - direction
assert direction.length_squared() > SQRTEPS
self.d = direction.normalized()
self.p = position
def __eq__(l1, l2):
return (l1.d == l2.d or l1.d == -l2.d) and l1.d.lineangle(l1.p - l2.p) < EPS
def __str__(l):
return "Line( %s + r * %s )" % (str(l.p), str(l.d))
def __repr__(l):
return "Line(%s,%s)" % (repr(p.d), repr(p.p))
def distance(l, r):
"""
>>> l = Line(Uy,P0)
>>> l.distance(P0)
0.0
>>> round(l.distance(Px+Uz),8)
1.41421356
>>> round(Line(Ux,Px).distance(Point(3,2,1)) , 8)
2.23606798
>>> Line(Ux,P0).distance(Line(Uy,P0))
0.0
>>> l1 = Line(Ux,Point(0,1,2))
>>> l2 = Line(Ux,Point(3,2,1))
>>> round(l1.distance(l2) , 8)
1.41421356
>>> l3 = Line(Uz,99.0*Px)
>>> Line(Ux,10*Py).distance(l3)
10.0
# >>> X = randxform()
# >>> round(Line(X.R*Ux,X*Point(0,1,2)).distance(Line(X.R*Ux,X*Point(3,2,1))) , 8)
"""
if ispoint(r):
return (r - l.p).perp(l.d).length()
if isvec(r):
raise TypeError("Line distance to Vec not defined")
if isline(r):
a1 = l.d.normalized()
a2 = r.d.normalized()
if abs(a1.dot(a2)) > 0.9999:
return (r.p - l.p).perp(a1).length()
a = a1
b = a2
c = r.p - l.p
n = abs(c.dot(a.cross(b)))
d = a.cross(b).length()
if abs(d) < EPS:
return 0
return n / d
# def line_line_distance(a1,c1,a2,c2):
# """
# >>> line_line_distance(Ux,V0,Uy,V0)
# 0.0
# >>> round(line_line_distance(Ux,Vec(0,1,2),Ux,Vec(3,2,1)) , 8)
# 1.41421356
# >>> line_line_distance(Ux,10*Uy,Uz,99.0*Ux)
# 10.0
# # >>> X = randxform()
# # >>> round(line_line_distance(X.R*Ux,X*Vec(0,1,2),X.R*Ux,X*Vec(3,2,1)) , 8)
# # 1.41421356
# """
# a1 = a1.normalized()
# a2 = a2.normalized()
# if abs(a1.dot(a2)) > 0.9999: return (c1-c2).perp(a1).length()
# a = a1
# b = a2
# c = c2-c1
# n = abs(c.dot(a.cross(b)))
# d = a.cross(b).length()
# if abs(d) < EPS: return 0
# return n/d
def line_plane_intersection(l, l0, n, p0):
"""
>>> l = Ux
>>> l0 = randvec()
>>> n = Ux
>>> p0 = V0
>>> assert line_plane_intersection(l,l0,n,p0)[1] == Vec(0,l0.y,l0.z)
>>> n = randnorm()
>>> p0 = randvec().cross(n)
>>> l = randvec()
>>> l0 = p0+l*gauss(0,10)
>>> assert line_plane_intersection(l,l0,n,p0)[1] == p0
"""
n = n.normalized()
d = (p0 - l0).dot(n) / l.dot(n)
return d, d * l + l0
def slide_to_make_lines_intersect(dof, l, l0, m, m0):
"""
>>> v = randvec()
>>> assert abs(slide_to_make_lines_intersect(Ux,Uy,v,Uz,V0) + v.x ) < EPS
>>> dof,l,l0,m,m0 = randvec(5)
>>> d = slide_to_make_lines_intersect(dof,l,l0,m,m0)
>>> l0 = l0 + d*dof
>>> assert abs(Line(l,P0+l0).distance(Line(m,P0+m0))) < EPS
"""
n = l.cross(m)
p0 = m0
d, i = line_plane_intersection(dof, l0, n, p0)
assert ((i - l0).normalized().dot(dof.normalized()) - 1.0) < EPS
assert i - l0 == dof * d
return d
# def slide_to_make_lines_intersect(dof,l,l0,m,m0):
# """
# >>> v = randvec()
# >>> assert abs(slide_to_make_lines_intersect(Ux,Uy,v,Uz,V0) + v.x ) < EPS
# >>> dof,l,l0,m,m0 = randvec(5)
# >>> d = slide_to_make_lines_intersect(dof,l,l0,m,m0)
# >>> l0 = l0 + d*dof
# >>> assert abs(line_line_distance(l,l0,m,m0)) < EPS
# """
# l0 = Point(l0)
# m0 = Point(m0)
# n = l.cross(m)
# d,i = Plane(n,m0).intersection(Line(dof,l0))
# assert ( (i-l0).normalized().dot(dof.normalized()) - 1.0 ) < EPS
# assert i == dof*d+l0
# return d
class Plane(object):
"""
from normal and center
>>> print Plane(Ux,P0)
Plane(norm=V(1,0,0),p0=P(0,0,0))
from 3 points:
>>> print Plane(P0,Py,Pz)
Plane(norm=V(1,0,0),p0=P(0,0,0))
from line and point
>>> print Plane( Line(Uy,Pz), P0)
Plane(norm=V(1,0,0),p0=P(0,0,0))
from line and vec
>>> print Plane( Line(Uy,P0), Uz)
Plane(norm=V(1,0,0),p0=P(0,0,0))
>>> assert Plane( Line(Uy,P0), Uz) == Plane(-Ux,P0)
>>> assert Plane( Line(Uy,P0), Uz) != Plane(-Ux,P0+Vec(0.0001) )
"""
def __init__(self, a, b=None, c=None):
if isplane(a):
a, b = a.n, a.p
elif isline(a) and ispoint(b) and c is None:
a, b = a.d.cross(a.p - b), b
elif isline(a) and isvec(b) and c is None:
a, b = a.d.cross(b), a.p
if allpoints(a, b, c):
a, b = (a - b).cross(a - c), a
assert isvec(a) and ispoint(b)
assert a.length_squared() > SQRTEPS
self.n = a.normalized()
self.p = b
def __eq__(p1, p2):
return (p1.n == p2.n or p1.n == -p2.n) and abs(p1.n.dot(p1.p - p2.p)) < EPS
def __str__(p):
return "Plane(norm=%s,p0=%s)" % (str(p.n), str(p.p))
def __repr__(p):
return "Plane(%s,%s)" % (repr(p.n), repr(p.p))
def intersection(p, l):
"""
>>> l = Ux
>>> l0 = randpoint()
>>> n = Ux
>>> p0 = P0
>>> assert Plane(n,p0).intersection(Line(l,l0))[1] == Point(0,l0.y,l0.z)
>>> n = randnorm()
>>> p0 = P0 + randvec().cross(n)
>>> l = randvec()
>>> l0 = p0+l*gauss(0,10)
>>> assert Plane(n,p0).intersection(Line(l,l0))[1] == p0
"""
n = p.n.normalized()
d = (p.p - l.p).dot(n) / l.d.dot(n)
return d, d * l.d + l.p
# class Mat(object):
# """docstring for Mat
# >>> m = Mat(2,0,0,0,1,0,0,0,1)
# >>> print m
# Mat[ (2.000000,0.000000,0.000000), (0.000000,1.000000,0.000000), (0.000000,0.000000,1.000000) ]
# >>> print m*m
# Mat[ (4.000000,0.000000,0.000000), (0.000000,1.000000,0.000000), (0.000000,0.000000,1.000000) ]
# >>> print Mat(*range(1,10)) * Mat(*range(10,19))
# Mat[ (84.000000,90.000000,96.000000), (201.000000,216.000000,231.000000), (318.000000,342.000000,366.000000) ]
# >>> assert Mat(0.0,1.0,2.0,3,4,5,6,7,8) == Mat(-0,1,2,3,4,5.0,6.0,7.0,8.0)
# >>> print Mat(100,2,3,4,5,6,7,8,9).det()
# -297.0
# >>> m = Mat(100,2,3,4,5,6,7,8,9)
# >>> assert m * ~m == Imat
# """
# def __init__(self, xx=None, xy=None, xz=None, yx=None, yy=None, yz=None, zx=None, zy=None, zz=None):
# super(Mat, self).__init__()
# if xx is None: # identity default
# self.xx, self.xy, self.xz = 1.0,0.0,0.0
# self.yx, self.yy, self.yz = 0.0,1.0,0.0
# self.zx, self.zy, self.zz = 0.0,0.0,1.0
# elif xy is None and ismat(xx):
# self.xx, self.xy, self.xz = xx.xx, xx.xy, xx.xz
# self.yx, self.yy, self.yz = xx.yx, xx.yy, xx.yz
# self.zx, self.zy, self.zz = xx.zx, xx.zy, xx.zz
# elif yx is None and isvec(xx) and isvec(xy) and isvec(xz):
# self.xx, self.xy, self.xz = xx.x, xy.x, xz.x
# self.yx, self.yy, self.yz = xx.y, xy.y, xz.y
# self.zx, self.zy, self.zz = xx.z, xy.z, xz.z
# elif isnum(xx):
# self.xx, self.xy, self.xz = float(xx), float(xy), float(xz)
# self.yx, self.yy, self.yz = float(yx), float(yy), float(yz)
# self.zx, self.zy, self.zz = float(zx), float(zy), float(zz)
# else:
# assert not isnum(xx)
# assert not ismat(xx)
# assert not isvec(xx)
# raise TypeError
# assert isfloat(self.xx) and isfloat(self.xy) and isfloat(self.xz)
# assert isfloat(self.yx) and isfloat(self.yy) and isfloat(self.yz)
# assert isfloat(self.zx) and isfloat(self.zy) and isfloat(self.zz)
# def row(m,i):
# assert isint(i)
# if i is 0: return Vec(m.xx,m.xy,m.xz)
# elif i is 1: return Vec(m.yx,m.yy,m.yz)
# elif i is 2: return Vec(m.zx,m.zy,m.zz)
# else: assert 0 <= i and i <= 2
# def col(m,i):
# assert isint(i)
# if i is 0: return Vec(m.xx,m.yx,m.zx)
# elif i is 1: return Vec(m.xy,m.yy,m.zy)
# elif i is 2: return Vec(m.xz,m.yz,m.zz)
# else: assert 0 <= i and i <= 2
# def rowx(m): return m.row(0)
# def rowy(m): return m.row(1)
# def rowz(m): return m.row(2)
# def colx(m): return m.col(0)
# def coly(m): return m.col(1)
# def colz(m): return m.col(2)
# def __invert__(m): return Mat( m.zz*m.yy-m.zy*m.yz , -(m.zz*m.xy-m.zy*m.xz) , m.yz*m.xy-m.yy*m.xz ,
# -(m.zz*m.yx-m.zx*m.yz) , m.zz*m.xx-m.zx*m.xz , -(m.yz*m.xx-m.yx*m.xz) ,
# m.zy*m.yx-m.zx*m.yy , -(m.zy*m.xx-m.zx*m.xy) , m.yy*m.xx-m.yx*m.xy ) / m.det()
# def __mul__(m,r):
# if isnum(r): return Mat( r*m.xx, r*m.xy, r*m.xz, r*m.yx, r*m.yy, r*m.yz, r*m.zx, r*m.zy, r*m.zz )
# elif isvec(r): return Vec( m.rowx()*r, m.rowy()*r, m.rowz()*r )
# elif ismat(r): return Mat( m.rowx()&r.colx(), m.rowx()&r.coly(), m.rowx()&r.colz(),
# m.rowy()&r.colx(), m.rowy()&r.coly(), m.rowy()&r.colz(),
# m.rowz()&r.colx(), m.rowz()&r.coly(), m.rowz()&r.colz() )
# else: return r.__rmul__(m)
# def __rmul__(m,v):
# if isnum(v): return m*v
# elif isvec(v): return Vec( m.colx()*v, m.coly()*v, m.colz()*v )
# def __div__(m,v): return m*(1/v)
# def __add__(m,v):
# if isnum(v): return Mat(v +m.xx,v +m.xy,v +m.xz,v +m.yx,v +m.yy,v +m.yz,v +m.zx,v +m.zy,v +m.zz)
# elif ismat(v): return Mat(v.xx+m.xx,v.xy+m.xy,v.xz+m.xz,v.yx+m.yx,v.yy+m.yy,v.yz+m.yz,v.zx+m.zx,v.zy+m.zy,v.zz+m.zz)
# else: return v.__radd__(m)
# def __sub__(m,v): return m + -v
# def __neg__(m): return m * -1
# def __str__(m): return "Mat[ %s, %s, %s ]" % (str(m.rowx()),str(m.rowy()),str(m.rowz()))
# def transpose(m):
# m = Mat( m.xx, m.yx, m.zx, m.xy, m.yy, m.zy, m.xz, m.yz, m.zz )
# def transposed(m): return Mat( m.xx, m.yx, m.zx, m.xy, m.yy, m.zy, m.xz, m.yz, m.zz )
# def det(m):
# # a11 (a33 a22- a32 a23)- a21 ( a33 a12- a32 a13)+ a31( a23 a12- a22 a13)
# return m.xx*(m.zz*m.yy-m.zy*m.yz)-m.yx*(m.zz*m.xy-m.zy*m.xz)+m.zx*(m.yz*m.xy-m.yy*m.xz)
# def trace(m):
# return m.xx+m.yy+m.zz
# def add_diagonal(m,v):
# return Mat( v.x+m.xx, m.xy, m.xz, m.yx, v.y+m.yy, m.yz, m.zx, m.zy, v.z+m.zz )
# def is_rotation(m):
# return (m.colx().isnormal() and m.coly().isnormal() and m.colz().isnormal() and
# m.rowx().isnormal() and m.rowy().isnormal() and m.rowz().isnormal() )
# def __eq__(self,other): return ( abs(self.xx-other.xx) < EPS and
# abs(self.xy-other.xy) < EPS and
# abs(self.xz-other.xz) < EPS and
# abs(self.yx-other.yx) < EPS and
# abs(self.yy-other.yy) < EPS and
# abs(self.yz-other.yz) < EPS and
# abs(self.zx-other.zx) < EPS and
# abs(self.zy-other.zy) < EPS and
# abs(self.zz-other.zz) < EPS )
# def rotation_axis(R):
# """
# >>> axis ,ang = randnorm(),uniform(-pi,pi)
# >>> axis2,ang2 = rotation_matrix(axis,ang).rotation_axis()
# >>> assert abs( abs(ang) - abs(ang2) ) < EPS
# >>> assert axis == axis2 * copysign(1,ang*ang2)
# """
# cos_theta = sin_cos_range((R.trace()-1.0)/2.0);
# if cos_theta > -1.0+EPS and cos_theta < 1.0-EPS:
# x = ( 1.0 if R.zy > R.yz else -1.0 ) * sqrt( max(0.0, ( R.xx - cos_theta ) / ( 1.0 - cos_theta ) ) )
# y = ( 1.0 if R.xz > R.zx else -1.0 ) * sqrt( max(0.0, ( R.yy - cos_theta ) / ( 1.0 - cos_theta ) ) )
# z = ( 1.0 if R.yx > R.xy else -1.0 ) * sqrt( max(0.0, ( R.zz - cos_theta ) / ( 1.0 - cos_theta ) ) )
# theta = acos( cos_theta );
# assert abs( x*x + y*y + z*z - 1 ) <= 0.01
# return Vec(x,y,z),theta
# elif cos_theta >= 1.0-EPS: return Vec(1.0,0.0,0.0),0.0
# else:
# nnT = (R+Imat)/2.0
# x,y,z = 0.0,0.0,0.0;
# if nnT.xx > EPS:
# x = sqrt( nnT.xx )
# y = nnT.yx / x
# z = nnT.zx / x
# elif nnT.yy > EPS:
# x = 0
# y = sqrt(nnT.yy)
# z = nnT.zy / y
# else:
# assert( nnT.zz > EPS );
# x = 0
# y = 0
# z = sqrt( nnT.zz )
# assert abs( x*x + y*y + z*z - 1.0 ) <= 0.01
# return Vec( x, y, z ),pi
# Imat = Mat(1,0,0,0,1,0,0,0,1)
# def projection_matrix(v):
# m = Mat( v.x * v.x, v.x * v.y, v.x * v.z, v.y * v.x, v.y * v.y, v.y * v.z, v.z * v.x, v.z * v.y, v.z * v.z )
# return m / v.dot(v)
# def rotation_matrix(axis,angle):
# n = axis.normalized()
# sin_theta = sin( angle )
# cos_theta = cos( angle )
# R = projection_matrix(n)
# R *= 1.0 - cos_theta
# R.xx += cos_theta; R.xy -= sin_theta * n.z; R.xz += sin_theta * n.y
# R.yx += sin_theta * n.z; R.yy += cos_theta; R.yz -= sin_theta * n.x
# R.zx -= sin_theta * n.y; R.zy += sin_theta * n.x; R.zz += cos_theta
# return R;
# def rotation_matrix_degrees(axis,angle):
# """ get a rotation matrix
# >>> rx180 = rotation_matrix_degrees(Vec(1,0,0),180.0)
# >>> rx90 = rotation_matrix_degrees(Vec(1,0,0),90.0)
# >>> print rx90*rx90 == rx180
# True
# >>> r = rotation_matrix_degrees(Vec(1,0,0),45.0)
# >>> print r
# Mat[ (1.000000,0.000000,0.000000), (0.000000,0.707107,-0.707107), (0.000000,0.707107,0.707107) ]
# >>> assert r*r == rx90
# >>> assert r*r*r*r == rx180
# >>> assert r*r*r*r*r*r*r*r == Imat
# >>> assert ~r == r.transposed()
# >>> ang = uniform(0,1)*360.0-180.0
# >>> v = randvec()
# >>> axs = randnorm()
# >>> while(abs(v.dot(axs))>0.9): axs = randnorm()
# >>> u = rotation_matrix_degrees(projperp(v,axs),ang)*v
# >>> assert abs(u.angle_degrees(v)-abs(ang)) < SQRTEPS
# >>> test_rotation_mat()
# test_rotation_mat PASS
# """
# return rotation_matrix(axis,radians(angle))
# def test_rotation_mat():
# import random
# for i in range(100):
# a0 = randnorm()
# t0 = uniform(-pi,pi)
# a,t = rotation_matrix(a0,t0).rotation_axis()
# if t0 < 0.01: continue
# if abs(t-pi) < EPS:
# if (abs(a.x-a0.x) < 0.001 and abs(a.y-a0.y) < 0.001 and abs(a.z-a0.z) < 0.001) or \
# (abs(a.x+a0.x) < 0.001 and abs(a.y+a0.y) < 0.001 and abs(a.z+a0.z) < 0.001):
# continue
# else:
# print a0
# print a
# return False
# if not abs(t-t0) < EPS or not (a.normalized()-a0.normalized()).length() < EPS:
# print a0.normalized(), t0
# print a.normalized() , t
# print "FAIL"
# return
# print "test_rotation_mat PASS"