/
LightField.m
171 lines (145 loc) · 5.14 KB
/
LightField.m
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
Package["ComputationalOptics`"]
PackageImport["GeneralUtilities`"]
PackageScope["nVal"]
PackageScope["lengthQuatityQ"]
PackageExport["LightField"]
Unprotect[LightField]
SetAttributes[LightField,ReadProtected]
SetUsage[LightField,
"LightField[type$, prop$] represents an object containing the light field information.",
"LightField['Types'] gives a list of the vaild light field types.",
"LightField[type$, 'Properties'] gives a list of the valid properties for the type$."
]
PackageExport["LightFieldQ"]
SetAttributes[LightFieldQ,ReadProtected]
SetUsage[LightFieldQ,
"LightFieldQ[expr$] yields True if expr$ is a valid LightField object.",
"LightFieldQ[expr$, type$] yields True if expr$ is a valid LightField object of type$."
]
PackageExport["LightFieldConvert"]
SetAttributes[LightFieldConvert,ReadProtected]
SetUsage[LightFieldConvert,
"LightFieldConvert[field$, type$] convert light field$ to type$.",
"LightFieldConvert[field$, type$, info$] convert light field$ to type$ with additional info$."
]
LightField::notype="`1` is not an available type."
LightField::ptprop="`1` is not a valid property."
LightField::setptv="`1` is not a valid data in part assignment."
LightField::setptp="`1` is not a valid property in part assignment."
LightField::setptvp="`1` is not a valid value for `2` in part assignment."
LightField::intplot="Cannot plot the intensity of light field with the type `1`."
$types=<|
"MonochromaticPlaneComplex"->{"Data","Wavelength","PhysicalSize"}
|>
HoldPattern@LightField["Types"]=Keys[$types]
HoldPattern@LightField[type_String,"Properties"]:=With[
{prop=$types[type]},
If[MissingQ[prop],
Message[LightField::notype,type];
Missing["NotAvailable",type],
prop
]
]
(obj_LightField?holdLightFieldQ)/;System`Private`HoldEntryQ[obj]:=With[
{valid=System`Private`HoldSetNoEntry[obj]},
valid
]
getType[HoldPattern@LightField[type_,_]]:=type
getProperty[HoldPattern@LightField[_,props_],All]:=props
getProperty[HoldPattern@LightField[_,props_],prop_]:=Lookup[
props,prop,
Message[LightField::ptprop,prop];Missing["NotAvailable",prop]
]
SetAttributes[setProperty,HoldFirst]
setProperty[sym_Symbol,All,val_]:=Block[
{tmp=Replace[sym,HoldPattern@LightField[type_,props_]:>LightField[type,val]]},
If[LightFieldQ[tmp],
sym=tmp;val,
Message[LightField::setptv,val];
$Failed
]
]
setProperty[sym_Symbol,prop_,val_]:=Block[
{tmp=sym,tmpprops},
tmpprops=getProperty[tmp,All];
If[KeyExistsQ[tmpprops,prop],
tmpprops[prop]=val;
tmp=Replace[tmp,HoldPattern@LightField[type_,props_]:>LightField[type,tmpprops]];
If[LightFieldQ[tmp],
sym=tmp;val,
Message[LightField::setptvp,val,prop];
$Failed
],
Message[LightField::setptp,prop];
$Failed
]
]
(obj_LightField?LightFieldQ)["Type"]:=getType[obj]
(obj_LightField?LightFieldQ)["Properties"]:=Keys@getProperty[obj,All]
(obj_LightField?LightFieldQ)["IntensityPlot",opt:OptionsPattern[MatrixPlot]]:=intensityPlot[obj,opt]
(obj_LightField?LightFieldQ)[prop_]:=getProperty[obj,prop]
SetAttributes[mutationHandler,HoldAllComplete]
mutationHandler[Set[(sym_Symbol?LightFieldQ)[prop_],val_]]:=With[
{result=setProperty[sym,prop,val]},
result/;!FailureQ[result]
]
mutationHandler[_]:=Language`MutationFallthrough
Language`SetMutationHandler[LightField,mutationHandler]
LightField/:MakeBoxes[
obj:HoldPattern@LightField["MonochromaticPlaneComplex",_],
fmt_
]/;BoxForm`UseIcons&&LightFieldQ[obj]:=ModuleScope[
alwaysGrid={
BoxForm`SummaryItem@{"Type: ",obj["Type"]},
BoxForm`SummaryItem@{"Wavelength: ",obj["Wavelength"]}
};
sometimesGrid={
BoxForm`SummaryItem@{"PhysicalSize: ",obj["PhysicalSize"]},
BoxForm`SummaryItem@{"DataSize: ",Dimensions[obj["Data"]]}
};
icon=obj["IntensityPlot",
MaxPlotPoints->32,PlotTheme->"Basic",
Frame->False,PlotLegends->None,
ElisionsDump`commonGraphicsOptions
];
BoxForm`ArrangeSummaryBox[LightField,obj,icon,alwaysGrid,sometimesGrid,fmt]
]
HoldPattern@LightFieldQ[
LightField["MonochromaticPlaneComplex",
KeyValuePattern[{
"Data"->_?MatrixQ,
"Wavelength"->_?lengthQuatityQ,
"PhysicalSize"->{_?lengthQuatityQ,_?lengthQuatityQ}
}]
]
]=True
LightFieldQ[_]=False
LightFieldQ[expr_,type_]:=LightFieldQ[expr] && getType[expr]===type
SetAttributes[holdLightFieldQ,HoldAllComplete]
holdLightFieldQ[expr_]:=LightFieldQ[Unevaluated[expr]]
LightFieldConvert[obj_,_]:=obj(*empty implementation*)
LightFieldConvert[obj_,_,_]:=obj
spectrumColor[wavelength_]:=With[
{base=ColorData["VisibleSpectrum"][wavelength]},
If[ColorDistance[Black, base]>0.5,
Darker[base,Clip[1-#,{0,1}]]&,
GrayLevel
]
]
intensityPlot[obj:HoldPattern@LightField["MonochromaticPlaneComplex",_],opt:OptionsPattern[MatrixPlot]]:=Scope[
{wx,wy}=nVal@obj["PhysicalSize"];
lambda=QuantityMagnitude[obj["Wavelength"],"Nanometers"];
MatrixPlot[
Abs[obj["Data"]]^2,
Normal@Merge[{
DataRange->{{-wx/2,wx/2},{-wy/2,wy/2}},
FrameTicks->{True,True},
ColorFunction->spectrumColor[lambda],
ColorFunctionScaling->True,
PlotLegends->Automatic,
opt
},Last]
]
]
intensityPlot[obj_LightField,___]:=(Message[LightField::intplot,getType@obj];$Failed)
Protect[LightField]