-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOFF_creation.py
1759 lines (1415 loc) · 73.5 KB
/
OFF_creation.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
# OFF contains
# - ypsilon() - should not be used
# - K_n() - should not be used
# - b_gen()
# - createShortHalfAxisArray()
# - ellipseResolution()
# - hyperbolaResolution()
# - createDoublePlanarNestedOFFwArray()
# - createSimpleDoublePlanarNestedOFFwArray()
# - createMonoPlanarNestedOFFwArray()
# - maskProcessing()
# - createToroidalNestedOFFwArray()
# - createNestedOFFwParam()
#-------------------------
#
import numpy as np
import os
import copy as cp
#-------------------------
#
from tools import *
#---------------------------------------------------------------------------------------------------
#
def ypsilon(z_pos, focal, b_halfaxis, bVerbose = True):
'''
Intermediary for b_gen()
Requirements :
- numpy.sqrt()
- numpy.float128
'''
y = (1-z_pos**2/(focal**2+b_halfaxis**2))*b_halfaxis**2
if (y > 0):
y = np.sqrt(y, dtype = np.float128)
else:
if bVerbose:
print('ypsilon() : warning y <= 0\ny set to 0')
y = 0
return y
def K_n(z_s, z_e, b_0, focal_l, n=1, bVerbose = True):
'''
Intermediary for b_gen()
'''
z_e_ellipse = z_e - focal_l
K_n = z_s**n/z_e**n*ypsilon(z_e_ellipse, focal_l, b_0, bVerbose = bVerbose)
return K_n
def b_gen(z_start, l, b_1, focal, n, bVerbose = True):
'''
Generation of *n*th correct short half axis. Based on O. Zimmer and R. Wagner algorithm.
Requirements :
- numpy.sqrt()
- numpy.float128
'''
k_n = K_n(z_start, z_start+l, b_1, focal, n+1, bVerbose = bVerbose)
C_1 = focal**2 - (z_start- focal)**2 - k_n**2
C_2 = k_n**2 * focal**2
C_3 = C_1**2/4+C_2
if C_3 > 0:
B = np.sqrt(C_3, dtype = np.float128)-C_1/2
if B >= 0:
b = np.sqrt(B, dtype = np.float128)
else:
if bVerbose:
print('b_gen() : warning B < 0\nb set to 0')
b = 0
else:
if bVerbose:
print('b_gen() : warning C_3 <= 0\nb set to 0')
b = 0
return b
#---------------------------------------------------------------------------------------------------
#
def createShortHalfAxisArray(z_start = 10, l = 10, L = 200, b_0 = (), b_1 = 1, nb_levels = 1, opticHalfWidth = 2,
bProtectSpacing = True, minSpacing = 0.01, bAdd = True,
bGetParamOnly = False, bVerbose = False, bWarnLimit = True,
):
'''
Return an array of short half axis, starting with b_0, extended with short half axis generated starting from b_1 and going toward the inside.
Input :
- z_start : (float) starting point of arc relative to focus
- l : (float) length of elliptical arc
- L : (float) ellipse focal distance * 2
- b_0 : (tupple) tupple of short half axis
- b_1 : (float) outer short half axis
if b_1 = 'limit', b_1 will be set to biggest elliptical arc fitting into opticHalfWidth
- nb_levels : (int) size of array output
- opticHalfWidth : (float)
if opticHalfWidth = 0, opticHalfWidth will adapat to fit biggest elliptic arc. In this case b_1 must not be set to 'limit'
- bProtectSpacing : (bool) if True, consecutives levels can't be closer than *minSpacing* meter, such levels will be deleted.
- minSpacing : (float) minimum space between too consecutives levels
- bAdd : (bool) if bProtectSpacing, after deleting too packed levels, add levels spaced of *minSpacing* meter
- bGetParamOnly : (bool) if True function only return *param* and do nothing except, if needed, adapting opticHalfWidth
- bVerbose : (bool) activate display of messages
- bWarnLimit : (bool) activate indicator (message) that maximum short axis is greater that strutural limits resulting in partial or non-existent levels
*bVerbose* superseed this boolean
Output :
- b_array : (numpy.array) contains, as much as possible, concatenation of b_0, b_1 and generic generated b.
- param : (dict) input values
Example :
- createShortHalfAxisArray(z_start = 10, l = 10, L = 200, b_0 = (3, 2.1), b_1 = 1.5, nb_levels = 8, opticHalfWidth = 2, bProtectSpacing = True, minSpacing = 1E-2)
- createShortHalfAxisArray(z_start = 10, l = 10, L = 200, b_1 = 1, nb_levels = 10, opticHalfWidth = 2, bProtectSpacing = True, minSpacing = 0.01, bVerbose = True)
Requirements :
- getEllipseOrdinate()
- getBiggestArc()
- b_gen()
- getShortHalfAxis()
- numpy.zeros()
- numpy.float128
- numpy.append()
- numpy.delete()
- numpy.sqrt()
- numpy.array()
'''
funcEllipseOrdi = getEllipseOrdinate
funcArc = getBiggestArc
funcBGen = b_gen
funcHalfAxis = getShortHalfAxis
param = {
"z_start" : z_start, "l" : l, "L" : L, "b_0" : b_0, "b_1" : b_1, "nb_levels" : nb_levels,
"opticHalfWidth" : opticHalfWidth,
"bProtectSpacing" : bProtectSpacing, "minSpacing" : minSpacing, "bAdd" : bAdd,
"bGetParamOnly" : bGetParamOnly, "bVerbose" : bVerbose, "bWarnLimit" : bWarnLimit,
}
if opticHalfWidth == 0: # Avoid display problems
if b_1 != 'limit':
if len(b_0) > 0 :
b_max = max(max(b_0),b_1)
else :
b_max = b_1
opticHalfWidth = funcEllipseOrdi(z_0 = z_start+l, b = b_max, L = L, bVerbose = bVerbose)
param['opticHalfWidth'] = opticHalfWidth
# To be used with clearDic() for intelligent calling of function
if bGetParamOnly:
return param
# Calculate the biggest optics that can fit in the space defined by opticHalfWidth
b_l = funcArc(l = l, z_start = z_start, L = L, tunnelHalfWidth = opticHalfWidth, bVerbose = bVerbose)
# If one set b_1 at 'limit', automatically set b_1 to b_limit
if (b_1 == 'limit'):
b_1 = b_l
# Find the maximum in all value given (b_0 is a tupple)
if len(b_0) > 0 :
b_max = max(max(b_0),b_1)
else :
b_max = b_1
# Create the resulting array
b_array = np.zeros(nb_levels, dtype = np.float128)
# Indicate if one, or more, ellipse will be cut
if bVerbose and bWarnLimit and (b_max > b_l):
print("Warning : b_max({}) is higher than the limit b_limit({}).".format(b_max, b_l))
# Begin the b_array with the values in b_0
sizeB0 = len(b_0)
if (sizeB0 >= nb_levels): # In case that the number of levels is lower than the dimension of b_0
b_array[:nb_levels] = np.array(b_0[:nb_levels])
else :
b_array[:sizeB0] = np.array(b_0)
b_array[sizeB0] = b_1
# Create the remaining generic levels
for n in range(nb_levels-sizeB0-1):
b_array[n + sizeB0 + 1] = funcBGen(z_start, l, b_1, L/2, n, bVerbose)
if bProtectSpacing:
## Removing levels
cptDel = 0
a_array = np.sqrt( (L / 2) ** 2 + b_array ** 2, dtype = np.float128)
y_pos = np.array([])
newNbLevels = param['nb_levels']
i = 0
while i != (len(b_array)): # delete the too close levels
a = a_array[i]
b = b_array[i]
z_pos = z_start
y_pos = np.append(y_pos,np.sqrt((1 - (z_pos - L / 2) **2 / (a ** 2))) * b)
if (i > 0) and len(b_array) > 1 and abs(y_pos [i-1] - y_pos[i]) < minSpacing:
b_array = np.delete(b_array,i)
y_pos = np.delete(y_pos,i)
newNbLevels -= 1
i -= 1
if cptDel == 0 and bVerbose: # Less messages
print("Warning : distance between consecutives levels was less than %0.1em. Faulty levels will be deleted."%minSpacing, end=' ')
cptDel += 1
i+=1
iEnd = len(y_pos)-1
if abs(y_pos[iEnd]) < minSpacing/2: # In case the one in the center is at a correct distance from neighbour but not of himself
b_array = np.delete(b_array,iEnd)
y_pos = np.delete(y_pos,iEnd)
newNbLevels -= 1
cptDel += 1
if cptDel > 0 and bVerbose:
print('{} levels have been deleted.'.format(cptDel))
## Adding levels
if bAdd :
diffLvl = nb_levels - newNbLevels
if diffLvl > 0: # if we deleted levels, we replaced them by minSpacing-spaced levels if we can
cptAdd = 0
cptAddBis = 0
for i in range(diffLvl):
y_minus = y_pos[-1] - minSpacing
if y_minus > 0:
b = funcHalfAxis(L/2-z_pos,y_minus)
b_array = np.append(b_array,b)
y_pos = np.append(y_pos, y_minus)
newNbLevels += 1
if cptAdd == 0 and bVerbose:
print('Constructing new levels with %0.1em spacing.'%minSpacing, end ='')
cptAddBis += 1
cptAdd += 1
iEnd = len(y_pos)-1
if abs(y_pos[iEnd]) < minSpacing/2: # In case the one in the center is at a correct distance from neighbour but not of himself
b_array = np.delete(b_array,iEnd)
y_pos = np.delete(y_pos,iEnd)
newNbLevels -= 1
cptAdd -= 1
if cptAdd > 0 and bVerbose:
print(' {} levels have been added.'.format(cptAdd))
else :
if cptAddBis > 0 and bVerbose:
print(' {} levels have been added.'.format(cptAdd))
# param['nb_levels'] = newNbLevels
return (np.array(b_array), param)
#---------------------------------------------------------------------------------------------------
#
def ellipseResolution(b_array, L, z_start, nb_segments, l, opticHalfWidth):
'''
Intermediary of create***NestedOFFwArray()
Requirements :
- numpy.flip()
- numpy.sort()
- numpy.sqrt()
- numpy.vstack()
- numpy.tile()
- numpy.array()
- numpy.arange()
- numpy.ones()
- numpy.float128
'''
b_array = np.flip(np.sort(b_array))
# Management of ellipse properties
b_array = b_array.reshape(-1, 1)
b2_array = b_array**2 ; b2_array
a2_array = (L/2)**2 + b_array**2
# Generation of evenly spaced circle in order to create the ellipse
z_pos = z_start + np.arange(nb_segments+1, dtype = np.float128)* l/nb_segments
zP = np.array([L/2 - z_pos for _ in range(len(b_array))], dtype = np.float128)
zP2 = zP**2
## Resizing array for array composition
a2_array = np.tile(a2_array, (1,len(z_pos)))
# Finding the radius of every circle
y2_array = (1-zP**2/a2_array) * b2_array
y_array = np.sqrt(y2_array, dtype = np.float128)
arrOW = opticHalfWidth * np.ones(len(y_array[0]), dtype = np.float128)
y_array = np.vstack((arrOW, y_array))
return z_pos, y_array
def hyperbolaResolution(b_array, a_h, z_start, nb_segments, l, opticHalfWidth, bToro = False):
'''
Intermediary of create***NestedOFFwArray()
Requirements :
- numpy.flip()
- numpy.sort()
- numpy.sqrt()
- numpy.vstack()
- numpy.tile()
- numpy.array()
- numpy.arange()
- numpy.ones()
- numpy.float128
'''
b_array = np.flip(np.sort(b_array))
c_array = np.sqrt(a_h**2 + b_array**2)
#print(c_array)
# Management of ellipse properties
b_array = b_array.reshape(-1, 1)
b2_array = b_array**2 ; b2_array
a2_array = a_h**2
# Generation of evenly spaced circle in order to create the ellipse
z_pos = z_start + np.arange(nb_segments+1, dtype = np.float128)* l/nb_segments
#print(z_pos)
zP = np.array([z_pos+c_array[idx] for idx in range(len(b_array))], dtype = np.float128)
#zP = np.array([z_pos for idx in range(len(b_array))], dtype = np.float128)
zP2 = zP**2
#print(zP)
## Resizing array for array composition
a2_array = np.tile(a2_array, (1,len(z_pos)))
# Finding the radius of every circle
#y2_array = (1-zP**2/a2_array) * b2_array
y2_array = b2_array*((zP)**2/a2_array-1)
#print(y2_array)
#y2_array = b_halfaxis**2*((z_pos-z_center)**2/a2_array-1)
y_array = np.sqrt(y2_array, dtype = np.float128)
if bToro == False:
arrOW = opticHalfWidth * np.ones(len(y_array[0]), dtype = np.float128)
y_array = np.vstack((arrOW, y_array))
return z_pos, y_array
#---------------------------------------------------------------------------------------------------
#
def createDoublePlanarNestedOFFwArray(L = 200, b_array = np.array([1.]),
z_start = 10,
l = 10,
nb_segments = 15,
alphaRad = 0, axis = 'x', RC = np.matrix([0,-.28,0], dtype = np.float128),
T = np.matrix([0,0,0], dtype = np.float128),
filename='defaultFile.off', opticHalfWidth = 0,
bBoundingBox = False,
bWolter = False,
bGetParamOnly = False, bVerbose = True,
):
'''
Write an OFF file of a double-planar nested optic based on a given array of short half axis.
Optic can be rotated and translated.
If opticHalfWidth != 0 and, resulting this, there are faces coinciding (located at opticHalfWidth -x or y), useless faces will be removed.
So when rotating or translating the optic, algorithm may not suppress coincided faces.
Input :
- L : (float) ellipse focal distance * 2
- b_array : (array) array of short half axis
- z_start : (float) starting point of arc relative to focus
- l : (float) length of elliptical arc
- nb_segments : (int)
- alphaRad : (float)(array)(rad)
see rotMatrix for more information
- axis : (str)
see rotMatrix for more information
- RC : (numpy.matrix) rotation center. Can be row or column vector, will be correctly transposed to correct form
- T : (numpy.matrix) translation vector. Can be row or column vector, will be correctly transposed to correct form
- filename : (str) output for OFF file
- opticHalfWidth : (float)
if opticHalfWidth = 0, opticHalfWidth will adapat to fit biggest elliptic arc. In this case b_1 must not be set to 'limit'
- bBoundingBox : (bool) generate a bounding box
- bWolter : (bool) generate hyperbolic instead of elliptic optic (used for Wolter optics)
- bGetParamOnly : (bool) if True function do nothing and only return *param*
- bVerbose : (bool) display of messages
Output :
- param : (dict) input values
Example :
- createDoublePlanarNestedOFFwArray(L = 200, b_array = np.array([2.,1.,.5]), z_start = 10, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 0, bBoundingBox = False, alphaRad = np.pi/26, axis = 'x', T = np.matrix([0,0,1]))
- createDoublePlanarNestedOFFwArray(L = 200, b_array = np.array([2.,1.,.5]), z_start = 10, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 0, bBoundingBox = False, alphaRad = [np.pi/26, np.pi/16], axis = 'xy', RC = '')
- createDoublePlanarNestedOFFwArray(L = 200, b_array = np.array([2.,1.,.5]), z_start = 10, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 0, bBoundingBox = False)
- createDoublePlanarNestedOFFwArray(L = 200, b_array = createShortHalfAxisArray(z_start = 16.5, l = 10, L = 200, b_0 = (), b_1 = 'limit', nb_levels = 200, opticHalfWidth = 2, minSpacing = 1E-2, bProtectSpacing = True, bVerbose = True)[0], z_start = 16.5, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 2, bBoundingBox = False, bVerbose = True)
Requirements :
- getEllipseOrdinate()
- rotMatrix()
- ellipseResolution()
- hyperbolaResolution()
- numpy.around()
- numpy.logical_and()
- numpy.shape()
- numpy.zeros()
- numpy.ones()
- numpy.array()
- numpy.tile()
- numpy.arange()
- numpy.min()
- numpy.max()
- numpy.vstack()
- numpy.matrix()
- numpy.float128
- copy.deepcopy()
'''
funcEllipseOrdi = getEllipseOrdinate
if type(RC) is not str and np.shape(RC) != (3,1):
RC = RC.T
if np.shape(T) != (3,1):
T = T.T
param = {
"L" : L, "l" : l, "z_start" : z_start, "opticHalfWidth" : opticHalfWidth,
"b_array" : b_array, "nb_segments" : nb_segments,
"alphaRad" : alphaRad, "axis" : axis, "RC" : RC,
"T" : T,
"bBoundingBox" : bBoundingBox, "filename" : filename,
"bGetParamOnly" : bGetParamOnly, "bVerbose" : bVerbose,
}
bAdaptative = False
if (opticHalfWidth == 0):
bAdaptative = True
opticHalfWidth = funcEllipseOrdi(z_0 = z_start+l, b = np.max(b_array), L = L, bVerbose = bVerbose)
param['opticHalfWidth'] = opticHalfWidth
# To be used with clearDic() for intelligent calling of function
if bGetParamOnly:
return param
##------------------------------------------------------------------------------------------------------------------------
# Ellipse calculations
#z_pos, y_array = ellipseResolution(b_array, L, z_start, nb_segments, l, opticHalfWidth)
if bWolter == False:
z_pos, y_array = ellipseResolution(b_array, L, z_start, nb_segments, l, opticHalfWidth)
else:
#For Wolter Option L == a_h == short hyperbola half_axis
z_pos, y_array = hyperbolaResolution(b_array, L, z_start, nb_segments, l, opticHalfWidth)
##------------------------------------------------------------------------------------------------------------------------
# Points creation
YY = np.vstack((y_array, np.flipud(-y_array)))
X = np.array([])
Y = np.array([])
for i in range(len(YY[0])):
YYY = np.tile(np.array([YY[:,i]]).transpose(), (1, len(YY)))
XXX = YYY.transpose()
if len(X) == 0:
X = [XXX]
Y = [YYY]
else:
X = np.vstack((X, [XXX]))
Y = np.vstack((Y, [YYY]))
Z = np.ones(np.shape(X))
for i in range(len(X)):
Z[i] *= z_pos[i]
IND = np.arange(np.shape(Z)[0]*np.shape(Z)[1]*np.shape(Z)[2]).reshape(np.shape(Z)) # Corresponding indices
##------------------------------------------------------------------------------------------------------------------------
# Rotation
if type(alphaRad) is list:
alphaRad = np.array(alphaRad) # for using abs()
if np.max(abs(alphaRad)) != 0:
# If wanted, setting rotation center to optic center
if type(RC) is str:
Va = []
for a in range(len(X)):
for l in range(len(X[a])):
for c in range(len(X[a][l])):
Va += [np.matrix([X[a,l,c], Y[a,l,c], Z[a,l,c]]).T] #Every coordinates of vertices in a matrix
RC = np.sum(Va, axis = 0) / len(Va) # center of optic
# Applying rotation
R = rotMatrix(t = alphaRad, axis = axis)
for a in range(len(X)):
for l in range(len(X[a])):
for c in range(len(X[a][l])):
V = np.matrix([X[a,l,c], Y[a,l,c], Z[a,l,c]]).T
Vr = R*(V-RC)+RC # Move to rotation point, rotate, then cancel first translation
Vrt = Vr + T # Translation
X[a,l,c] = Vrt[0]
Y[a,l,c] = Vrt[1]
Z[a,l,c] = Vrt[2]
elif (len(T) == 3) and not (np.min(T == np.matrix([0,0,0], dtype = np.float128).T)): # Only do translation
for a in range(len(X)):
for l in range(len(X[a])):
for c in range(len(X[a][l])):
V = np.matrix([X[a,l,c], Y[a,l,c], Z[a,l,c]]).T
Vt = V + T # Translation
X[a,l,c] = Vt[0]
Y[a,l,c] = Vt[1]
Z[a,l,c] = Vt[2]
##------------------------------------------------------------------------------------------------------------------------
# Width limit
if bAdaptative:
hoWXup = np.max(X)
hoWXlow = np.min(X)
hoWYup = np.max(Y)
hoWYlow = np.min(Y)
else:
hoWXup = opticHalfWidth
hoWXlow = -opticHalfWidth
hoWYup = opticHalfWidth
hoWYlow = -opticHalfWidth
X[X > hoWXup] = hoWXup
X[X < hoWXlow] = hoWXlow
Y[Y > hoWYup] = hoWYup
Y[Y < hoWYlow] = hoWYlow
# Z[Z > z_start + l] = z_start + l
# Z[Z < z_start] = z_start
##------------------------------------------------------------------------------------------------------------------------
# Faces deletion
MaskX = (abs(np.around(X, 8)) == round(opticHalfWidth, 8)) # avoid precision mistakes
MaskY = (abs(np.around(Y, 8)) == round(opticHalfWidth, 8))
MaskEtX = np.logical_and(MaskX[:-1], MaskX[1:])
MaskEtY = np.logical_and(MaskY[:-1], MaskY[1:])
if not bBoundingBox:
MaskEtEtY = np.zeros(np.shape(MaskEtY), bool)
for a in range(len(MaskEtY)):
mid = int(len(MaskEtY[a])/2)
MaskEtEtY[a,:mid] = np.logical_and(MaskEtY[a,1:mid+1], MaskEtY[a,:mid])
MaskEtEtY[a,mid:] = np.logical_and(MaskEtY[a,mid:], MaskEtY[a,mid-1:-1])
if np.min(MaskEtEtY[a]) == True: # If every faces are equal or greater than oW in the circle, enable to draw the "smallest" level
MaskEtEtY[a,mid-1] = np.zeros(len(MaskEtEtY[a,mid-1]), bool)
MaskEtEtY[a,mid] = np.zeros(len(MaskEtEtY[a,mid]), bool)
MaskEtY = cp.deepcopy(MaskEtEtY)
MaskEtEtX = np.zeros(np.shape(MaskEtX), bool)
for a in range(len(MaskEtX)):
mid = int(len(MaskEtX[a][0])/2)
MaskEtEtX[a,:,:mid] = np.logical_and(MaskEtX[a,:,1:mid+1], MaskEtX[a,:,:mid]);
MaskEtEtX[a,:,mid:] = np.logical_and(MaskEtX[a,:,mid:], MaskEtX[a,:,mid-1:-1])
if np.min(MaskEtEtX[a,:]) == True:
MaskEtEtX[a,:,mid-1] = np.zeros(len(MaskEtEtX[a,:,mid-1]), bool)
MaskEtEtX[a,:,mid] = np.zeros(len(MaskEtEtX[a,:,mid]), bool)
MaskEtX = cp.deepcopy(MaskEtEtX)
##------------------------------------------------------------------------------------------------------------------------
# OFF Creation
nbL = int(np.shape(YY)[0]/2)
facesH = np.zeros((2*(nbL-1)*(2*nbL-1))*nb_segments, object)
facesV = np.zeros((2*(nbL-1)*(2*nbL-1))*nb_segments, object)
facesBB = np.zeros((4*(2*nbL-1))*nb_segments, object)
cptFacesH = 0
for a in range(len(X)-1):
for l in range(1,len(X[a])-1):
for c in range(len(X[a][l])-1):
## Horizontal
if not(MaskEtX[a,l,c] and MaskEtX[a,l,c+1]) and not(MaskEtY[a,l,c] and MaskEtY[a,l,c+1]):
facesH[cptFacesH] = '4 {} {} {} {}\n'.format(IND[a,l,c], IND[a,l,c+1], IND[a+1,l,c+1], IND[a+1,l,c])
cptFacesH += 1
cptFacesV = 0
for a in range(len(X)-1):
for l in range(len(X[a])-1):
for c in range(1,len(X[a][l])-1):
## Vertical
if not(MaskEtX[a,l,c] and MaskEtX[a,l+1,c]) and not(MaskEtY[a,l,c] and MaskEtY[a,l+1,c]):
facesV[cptFacesV] = '4 {} {} {} {}\n'.format(IND[a,l,c], IND[a,l+1,c], IND[a+1,l+1,c], IND[a+1,l,c])
cptFacesV += 1
cptFacesBB = 0
if bBoundingBox:
for a in range(len(X)-1):
for l in range(len(X[a])-1):
for c in [0, len(X[a])-1]:
facesBB[cptFacesBB] = '4 {} {} {} {}\n'.format(IND[a,l,c], IND[a,l+1,c], IND[a+1,l+1,c], IND[a+1,l,c])
cptFacesBB += 1
for a in range(len(X)-1):
for l in [0, len(X[a])-1]:
for c in range(len(X[a][l])-1):
facesBB[cptFacesBB] = '4 {} {} {} {}\n'.format(IND[a,l,c], IND[a,l,c+1], IND[a+1,l,c+1], IND[a+1,l,c])
cptFacesBB += 1
f = open(filename,'w')
# Write OFF-File Header
f.write("OFF\n")
f.write("# Double planar nested optic \n")
f.write("# Number of vertex, number of faces, number of edges \n")
nbFaces = cptFacesH+cptFacesV
if bBoundingBox:
nbFaces += cptFacesBB
f.write("%d %d 0\n"%(np.shape(X)[0]*np.shape(X)[1]*np.shape(X)[2], nbFaces)) # Write the number of vertices (=corners) and faces ()
for a in range(len(X)):
for l in range(len(X[a])):
for c in range(len(X[a][l])):
f.write("%0.9f %0.9f %0.9f\n"%(X[a,l,c], Y[a,l,c], Z[a,l,c]-z_start))
for i in range(cptFacesH):
f.write(facesH[i])
for i in range(cptFacesV):
f.write(facesV[i])
for i in range(cptFacesBB):
f.write(facesBB[i])
f.close()
return(param)
#---------------------------------------------------------------------------------------------------
#
def createSimpleDoublePlanarNestedOFFwArray(L = 200,
b_array = np.array([1.]),
z_start = 10,
l = 10,
nb_segments = 15,
opticHalfWidth = 0,
filename='defaultFile.off',
bBoundingBox = False,
bWolter = False,
fOffsetOrigin = 0,
bGetParamOnly = False, bVerbose = True,
):
'''
Write an OFF file of a double-planar nested optic based on a given array of short half axis.
This version doesn't define crosing points and doesn't support rotation.
Input :
- L : (float) ellipse focal distance * 2
- b_array : (numpy.array) array of short half axis
- z_start : (float) starting point of arc relative to focus
- l : (float) length of elliptical arc
- nb_segments : (int)
- filename : (str)
- opticHalfWidth : (float)
if opticHalfWidth = 0, opticHalfWidth will adapat to fit biggest elliptic arc. In this case b_1 must not be set to 'limit'
- bBoundingBox : (bool)
- bWolter : (bool) generate hyperbolic instead of elliptic optic (used for Wolter optics)
- bGetParamOnly : (bool) if True function do nothing and only return *param*
- bVerbose : (str) activate display of messages
Output :
- param : (dict) input values
Example :
- createSimpleDoublePlanarNestedOFFwArray(L = 200, b_array = np.array([2.,1.,.5]), z_start = 10, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 0, bBoundingBox = False)
Requirements :
- getEllipseOrdinate()
- numpy.ndenumerate()
- numpy.array()
- numpy.sqrt()
- numpy.append()
'''
funcEllipseOrdi = getEllipseOrdinate
param = {
"L" : L, "l" : l, "z_start" : z_start, "opticHalfWidth" : opticHalfWidth,
"b_array" : b_array, "nb_segments" : nb_segments,
"bBoundingBox" : bBoundingBox, "filename" : filename,
"bGetParamOnly" : bGetParamOnly, "bVerbose" : bVerbose,
}
if (opticHalfWidth == 0):
opticHalfWidth = funcEllipseOrdi(z_0 = z_start+l, b = np.max(b_array), L = L, bVerbose = bVerbose)
param['opticHalfWidth'] = opticHalfWidth
# To be used with clearDic() for intelligent calling of function
if bGetParamOnly:
return param
# Init. routines
# Calculation of long half axis from foci distance and short half axis
a_array = np.sqrt( (L / 2) ** 2 + b_array ** 2)
## Numbers of vertices and faces for OFF File
nb_mirrors = len(b_array)
nb_vertex = 4 * nb_mirrors * (nb_segments + 1) * 2
nb_faces = 2 * nb_mirrors * nb_segments * 2
#Correction for bounding Box
if bBoundingBox == True:
nb_vertex += 8
nb_faces += 2 + 2
# Open OFF-File
f = open(filename,'w')
# Write OFF-File Header
f.write("OFF\n")
f.write("%d %d 0\n"%(nb_vertex, nb_faces)) # Write the number of vertices (=corners) and faces ()
#Check if bounding box should be written
if bBoundingBox == True:
z_end = z_start + l
# Vertices of Bounding Box: X,Y,Z in meters
f.write('%0.9f %0.9f %0.9f\n'%( opticHalfWidth, -opticHalfWidth, 0 + fOffsetOrigin)) # 0
f.write('%0.9f %0.9f %0.9f\n'%( opticHalfWidth, -opticHalfWidth, z_end - z_start + fOffsetOrigin)) # 1
f.write('%0.9f %0.9f %0.9f\n'%(-opticHalfWidth, -opticHalfWidth, z_end - z_start + fOffsetOrigin)) # 2
f.write('%0.9f %0.9f %0.9f\n'%(-opticHalfWidth, -opticHalfWidth, 0 + fOffsetOrigin)) # 3
f.write('%0.9f %0.9f %0.9f\n'%( opticHalfWidth, opticHalfWidth, 0 + fOffsetOrigin)) # 4
f.write('%0.9f %0.9f %0.9f\n'%( opticHalfWidth, opticHalfWidth, z_end - z_start + fOffsetOrigin)) # 5
f.write('%0.9f %0.9f %0.9f\n'%(-opticHalfWidth, opticHalfWidth, z_end - z_start + fOffsetOrigin)) # 6
f.write('%0.9f %0.9f %0.9f\n'%(-opticHalfWidth, opticHalfWidth, 0 + fOffsetOrigin)) # 7
# calculation of Vertices of the Mirrors
#1st loop over nested mirrors layers
aYPosV = []; aZPosV = []
+ fOffsetOrigin
cptYPos = 0
for idx, b in np.ndenumerate(b_array):
a = a_array[idx]
idx = idx[0]
aiYPosV = np.array([]); aiZPosV = np.array([])
# 2nd loop over mirror segments
for i in range(nb_segments + 1):
#calculation of ellipse points at mirror position
z_pos = z_start + i * (l / nb_segments)
if bWolter == False:
y_pos = (1 - (z_pos - L / 2) **2 / (a ** 2)) * (b ** 2)
else:
c = np.sqrt(L**2 + b**2)
y_pos = b**2*((z_pos+c)**2/L**2-1)
#print(c)
#print(z_pos, y_pos)
# Management of ellipse properties
#b_array = b_array.reshape(-1, 1)
#b2_array = b_array**2 ; b2_array
#a2_array = a_h**2
# Generation of evenly spaced circle in order to create the ellipse
#z_pos = z_start + np.arange(nb_segments+1, dtype = np.float128)* l/nb_segments
#print(z_pos)
#zP = np.array([z_pos+c_array[idx] for idx in range(len(b_array))], dtype = np.float128)
#zP = np.array([z_pos for idx in range(len(b_array))], dtype = np.float128)
#zP2 = zP**2
#print(zP)
## Resizing array for array composition
#a2_array = np.tile(a2_array, (1,len(z_pos)))
# Finding the radius of every circle
#y2_array = (1-zP**2/a2_array) * b2_array
#y2_array = b2_array*((zP)**2/a2_array-1)
#print(y2_array)
#y2_array = b_halfaxis**2*((z_pos-z_center)**2/a2_array-1)
#y_array = np.sqrt(y2_array, dtype = np.float128)
#if bToro == False:
# arrOW = opticHalfWidth * np.ones(len(y_array[0]), dtype = np.float128)
# y_array = np.vstack((arrOW, y_array))
#return z_pos, y_array
# make sure x is positive before taking the root
if y_pos > 0:
y_pos = np.sqrt(y_pos)
if y_pos > opticHalfWidth:
y_pos = opticHalfWidth
else:
if cptYPos == 0 and bVerbose:
print('Warning ypos < 0: %0.9f\n'%(y_pos))
cptYPos += 1
aiYPosV = np.append(aiYPosV, y_pos); aiZPosV = np.append(aiZPosV, z_pos)
#Write points
#upper part of ellipse
f.write('%0.9f %0.9f %0.9f\n'%( y_pos, -opticHalfWidth, z_pos - z_start + fOffsetOrigin)) # 4*i +8
f.write('%0.9f %0.9f %0.9f\n'%( y_pos, opticHalfWidth, z_pos - z_start + fOffsetOrigin)) # 4*+1+8
#lower part of ellipse
f.write('%0.9f %0.9f %0.9f\n'%(-y_pos, opticHalfWidth, z_pos - z_start + fOffsetOrigin)) # 4*+2+8
f.write('%0.9f %0.9f %0.9f\n'%(-y_pos, -opticHalfWidth, z_pos - z_start + fOffsetOrigin)) # 4*+3+8
aYPosV += [aiYPosV]
aZPosV += [aiZPosV]
aYPosV = np.array(aYPosV) ; aZPosV = np.array(aZPosV)
### 90° rotated optics
aYPosH = []; aZPosH = []
cptYpos = 0
for idx, b in np.ndenumerate(b_array):
a = a_array[idx]
idx = idx[0]
aiYPosH = np.array([]); aiZPosH = np.array([])
# 2nd loop over mirror segments
for i in range(nb_segments + 1):
#Calculate ellipse points at mirror position
z_pos = z_start + i * (l / nb_segments)
if bWolter == False:
y_pos = (1 - (z_pos - L / 2) **2 / (a ** 2)) * (b ** 2)
else:
c = np.sqrt(L**2 + b**2)
y_pos = b**2*((z_pos+c)**2/L**2-1)
# make sure x is positive before taking the root
if y_pos > 0:
y_pos = np.sqrt(y_pos)
if y_pos > opticHalfWidth:
y_pos = opticHalfWidth
else:
if cptYPos == 0 and bVerbose:
print('Warning ypos < 0: %0.9f\n'%(y_pos))
cptYPos += 1
aiYPosH = np.append(aiYPosH, y_pos); aiZPosH = np.append(aiZPosH, z_pos)
#left part of ellipse
f.write('%0.9f %0.9f %0.9f\n'%(-opticHalfWidth, -y_pos, z_pos - z_start + fOffsetOrigin)) # 4*+3+8
f.write('%0.9f %0.9f %0.9f\n'%(opticHalfWidth, -y_pos, z_pos - z_start + fOffsetOrigin)) # 4*i +8
#right part of ellipse
f.write('%0.9f %0.9f %0.9f\n'%(opticHalfWidth, y_pos, z_pos - z_start + fOffsetOrigin)) # 4*+1+8
f.write('%0.9f %0.9f %0.9f\n'%(-opticHalfWidth, y_pos, z_pos - z_start + fOffsetOrigin)) # 4*+2+8
aYPosH += [aiYPosH]
aZPosH += [aiZPosH]
aYPosH = np.array(aYPosH) ; aZPosH = np.array(aZPosH)
# Write Faces (= indexes of vertices that constitute each face)
index = 0 #index for bookkeeping
if bBoundingBox == True:
f.write('4 %d %d %d %d\n'%(0,1,2,3)) #/* bottom */
f.write('4 %d %d %d %d\n'%(4,5,6,7)) #/* top */
f.write('4 %d %d %d %d\n'%(0,1,5,4)) #/* left */
f.write('4 %d %d %d %d\n'%(3,2,6,7)) #/* right */
index = 8
for i in range(nb_mirrors): #loop over nested mirrors
for j in range(nb_segments): #loop over mirror segments#
f.write('4 %d %d %d %d\n'%(index, index+1, index+4+1, index+4)) # face of upper ellipse
f.write('4 %d %d %d %d\n'%(index+2, index+3, index+4+3, index+4+2)) # face of lower ellipse
index += 4
if (j == nb_segments-1):
index += 4
### 90° rotated optics
for i in range(nb_mirrors): #loop over nested mirrors
for j in range(nb_segments): #loop over mirror segments#
f.write('4 %d %d %d %d\n'%(index, index+1, index+4+1, index+4)) # face of left ellipse
f.write('4 %d %d %d %d\n'%(index+2, index+3, index+4+3, index+4+2)) # face of right ellipse
index += 4
if (j == nb_segments-1):
index += 4
#Close OFF-File
f.close()
# print(aYPosH,aZPosH)
return(param)
#---------------------------------------------------------------------------------------------------
#
def createMonoPlanarNestedOFFwArray(L = 200, b_array = np.array([1.]),
z_start = 10,
l = 10,
nb_segments = 15,
alphaRad = 0, axis = 'x', RC = np.matrix([0,-.28,0], dtype = np.float128),
T = np.matrix([0,0,0], dtype = np.float128),
bHorizontal = True,
filename = 'defaultFile.off', opticHalfWidth = 0,
bBoundingBox = False, bWolter = False,
bGetParamOnly = False, bVerbose = True,
):
'''
Write an OFF file of a mono-planar nested optic based on a given array of short half axis. Direction of planes is chosen by bHorizontal.
Optic can be rotated and translated.
If opticHalfWidth != 0 and, resulting this, there are faces coinciding (located at opticHalfWidth -x or y), useless faces will be removed.
So when rotating or translating the optic, algorithm may not suppress coincided faces.
Input :
- L : (float) ellipse focal distance * 2
- b_array : (array) array of short half axis
- z_start : (float) starting point of arc relative to focus
- l : (float) length of elliptical arc
- nb_segments : (int)
- alphaRad : (float)(array)(rad)
see rotMatrix for more information
- axis : (str)
see rotMatrix for more information
- RC : (numpy.matrix) rotation center. Can be row or column vector, will be correctly transposed to correct form
- T : (numpy.matrix) translation vector. Can be row or column vector, will be correctly transposed to correct form
- bHorizontal : (bool) if True build horizontal nested levels, if False vertical ones
- filename : (str) output for OFF file
- opticHalfWidth : (float)
if opticHalfWidth = 0, opticHalfWidth will adapat to fit biggest elliptic arc. In this case b_1 must not be set to 'limit'
- bBoundingBox : (bool) generate a bounding box
- bWolter : (bool) generate hyperbolic instead of elliptic optic (used for Wolter optics)
- bGetParamOnly : (bool) if True function do nothing and only return *param*
- bVerbose : (bool) activate display of messages
Output :
- param : (dict) input values
Example :
- createMonoPlanarNestedOFFwArray(bHorizontal = True, L = 200, b_array = np.array([2.,1.,.5]), z_start = 10, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 0, bBoundingBox = False, alphaRad = np.pi/26, axis = 'x', T = np.matrix([0,0,1]))
- createMonoPlanarNestedOFFwArray(bHorizontal = False, L = 200, b_array = np.array([2.,1.,.5]), z_start = 10, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 0, bBoundingBox = False, alphaRad = [np.pi/26, np.pi/16], axis = 'xy', RC = '')
- createMonoPlanarNestedOFFwArray(bHorizontal = True, L = 200, b_array = createShortHalfAxisArray(z_start = 16.5, l = 10, L = 200, b_0 = (), b_1 = 'limit', nb_levels = 200, opticHalfWidth = 2, minSpacing = 1E-2, bProtectSpacing = True, bVerbose = True)[0], z_start = 16.5, l = 10, nb_segments = 15, filename='exampleFile.off', opticHalfWidth = 2, bBoundingBox = False, bVerbose = True)
Requirements :
- getEllipseOrdinate()
- ellipseResolution()
- hyperbolaResolution()
- rotMatrix()
- numpy.around()
- numpy.logical_and()
- numpy.shape()
- numpy.zeros()
- numpy.ones()
- numpy.array()
- numpy.tile()
- numpy.arange()
- numpy.min()
- numpy.max()
- numpy.flip()
- numpy.sort()
- numpy.vstack()
- numpy.sqrt()
- numpy.matrix()
- numpy.float128
- copy.deepcopy()
'''
funcEllipseOrdi = getEllipseOrdinate