obplib 3.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,204 @@
1
+ # SPDX-FileCopyrightText: 2019,2020 Freemelt AB
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ """
6
+ Copyright 2019,2020 Freemelt AB
7
+
8
+ Licensed under the Apache License, Version 2.0 (the "License");
9
+ you may not use this file except in compliance with the License.
10
+ You may obtain a copy of the License at
11
+
12
+ http://www.apache.org/licenses/LICENSE-2.0
13
+
14
+ Unless required by applicable law or agreed to in writing, software
15
+ distributed under the License is distributed on an "AS IS" BASIS,
16
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ See the License for the specific language governing permissions and
18
+ limitations under the License.
19
+ """
20
+
21
+ # From PyPI
22
+ from google.protobuf.json_format import MessageToDict, ParseDict
23
+ import json
24
+
25
+ # Project
26
+ import obplib.OBP_pb2 as OBP
27
+ from obplib.Point import Point
28
+ from obplib.Beamparameters import Beamparameters
29
+
30
+
31
+ class AcceleratingCurve:
32
+ """
33
+ Constructor for the accelerating curve class
34
+
35
+ Args:
36
+ P1 (Point): The starting point
37
+ P2 (Point): First control point
38
+ P3 (Point): Second control point
39
+ P4 (Point): End point
40
+ speed (Int): Scan speed in µm/s
41
+ bp (Beamparamters): Desired beam parameters
42
+ """
43
+
44
+ def __init__(self, P1, P2, P3, P4, si, sf, bp):
45
+ self.P1 = P1
46
+ self.P2 = P2
47
+ self.P3 = P3
48
+ self.P4 = P4
49
+ self.si = si
50
+ self.sf = sf
51
+ self.bp = bp
52
+
53
+ @classmethod
54
+ def from_dict(cls, kw):
55
+ """
56
+ Recrates an instance of the accelerating curve class using a dictionary representation of said class. Assumes that that the given dictionary is in standard JSON structure.
57
+
58
+ Args:
59
+ kw: A dictionary representation of an accelerating curve
60
+ Returns:
61
+ cls: An instance of AcceleratingCurve recreated using kw
62
+ """
63
+ # Proto3 JSON omits zero fields and represents uint64 speeds as strings.
64
+ # Let protobuf restore defaults and types, including absent submessages.
65
+ curve = ParseDict(kw, OBP.AcceleratingCurve())
66
+ points = [Point(p.x, p.y) for p in (curve.p0, curve.p1, curve.p2, curve.p3)]
67
+ bp = Beamparameters(curve.params.spot_size, curve.params.beam_power)
68
+ return cls(*points, curve.si, curve.sf, bp)
69
+
70
+ def get_pb(self):
71
+ """
72
+ Returns a protobuf packet containing curve information
73
+
74
+ Returns:
75
+ packet (binary): A protobuf packet containing curve information
76
+ """
77
+
78
+ pkt = OBP.Packet()
79
+ accCurve = OBP.AcceleratingCurve()
80
+
81
+ accCurve.params.CopyFrom(self.bp.get_pb())
82
+
83
+ cp0 = OBP.Curve.Point()
84
+ cp1 = OBP.Curve.Point()
85
+ cp2 = OBP.Curve.Point()
86
+ cp3 = OBP.Curve.Point()
87
+
88
+ cp0.x = self.P1.get_x()
89
+ cp0.y = self.P1.get_y()
90
+
91
+ cp1.x = self.P2.get_x()
92
+ cp1.y = self.P2.get_y()
93
+
94
+ cp2.x = self.P3.get_x()
95
+ cp2.y = self.P3.get_y()
96
+
97
+ cp3.x = self.P4.get_x()
98
+ cp3.y = self.P4.get_y()
99
+
100
+ accCurve.p0.CopyFrom(cp0)
101
+ accCurve.p1.CopyFrom(cp1)
102
+ accCurve.p2.CopyFrom(cp2)
103
+ accCurve.p3.CopyFrom(cp3)
104
+
105
+ accCurve.si = int(self.si)
106
+ accCurve.sf = int(self.sf)
107
+
108
+ pkt.accelerating_curve.CopyFrom(accCurve)
109
+
110
+ return pkt
111
+
112
+ def get_packet(self, pkt):
113
+ """
114
+ Returns a serialize packet
115
+
116
+ Args:
117
+ packet (binary): A packet
118
+
119
+ Returns:
120
+ A serialized packet (String): A serialized packet
121
+ """
122
+
123
+ return pkt.SerializeToString()
124
+
125
+ def write_obp(self):
126
+ """
127
+ Overloaded writer method - is called by the FileHandler when writing to obp
128
+
129
+ Returns:
130
+ serialized (binary): A serialized protobuf packet
131
+ """
132
+ return self.get_packet(self.get_pb())
133
+
134
+ def get_obpj(self):
135
+ """
136
+ Overloaded writer method - is called by the FileHandler when writing to obpj
137
+
138
+ Returns:
139
+ d (dictionary): A dictionary representation of a protobuf packet
140
+ """
141
+ return MessageToDict(self.get_pb())
142
+
143
+ def __repr__(self):
144
+ return json.dumps(self.get_obpj(), indent=2)
145
+
146
+ def translate(self, v):
147
+ """
148
+ Returns a new AcceleratingCurve object which has been translated according to the given vector V
149
+
150
+ Args:
151
+ V (Vector): A translation vector
152
+
153
+ Returns:
154
+ AcceleratingCurve: An AcceleratingCurve which has been traslated according to the given vector
155
+ """
156
+ return AcceleratingCurve(
157
+ self.P1.translate(v),
158
+ self.P2.translate(v),
159
+ self.P3.translate(v),
160
+ self.P4.translate(v),
161
+ self.si,
162
+ self.sf,
163
+ self.bp,
164
+ )
165
+
166
+ def rotate(self, theta):
167
+ """
168
+ Returns a new AcceleratingCurve which has been rotated theta radians around the origin
169
+
170
+ Args:
171
+ theta (radians): The amount of radians which the AcceleratingCurve is to be rotated
172
+
173
+ Returns:
174
+ AcceleratingCurve: An AcceleratingCurve rotated theta radians relative to the origin
175
+ """
176
+ return AcceleratingCurve(
177
+ self.P1.rotate(theta),
178
+ self.P2.rotate(theta),
179
+ self.P3.rotate(theta),
180
+ self.P4.rotate(theta),
181
+ self.si,
182
+ self.sf,
183
+ self.bp,
184
+ )
185
+
186
+ def scale(self, factor):
187
+ """
188
+ Returns a new AcceleratingCurve scaled by the specified factor.
189
+
190
+ Args:
191
+ factor (float): The factor with which to scale the AcceleratingCurve
192
+
193
+ Returns:
194
+ AcceleratingCurve: An AcceleratingCurve scaled using the specified factor
195
+ """
196
+ return AcceleratingCurve(
197
+ self.P1.scale(factor),
198
+ self.P2.scale(factor),
199
+ self.P3.scale(factor),
200
+ self.P4.scale(factor),
201
+ self.si,
202
+ self.sf,
203
+ self.bp,
204
+ )
@@ -0,0 +1,158 @@
1
+ # SPDX-FileCopyrightText: 2019,2020 Freemelt AB
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ """
6
+ Copyright 2019,2020 Freemelt AB
7
+
8
+ Licensed under the Apache License, Version 2.0 (the "License");
9
+ you may not use this file except in compliance with the License.
10
+ You may obtain a copy of the License at
11
+
12
+ http://www.apache.org/licenses/LICENSE-2.0
13
+
14
+ Unless required by applicable law or agreed to in writing, software
15
+ distributed under the License is distributed on an "AS IS" BASIS,
16
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ See the License for the specific language governing permissions and
18
+ limitations under the License.
19
+ """
20
+
21
+ """@AcceleratingLine docstring
22
+ A class describing a straigh line drawn between points p1 and p2, with initial speed v0 and final speed v1.
23
+ """
24
+
25
+ # PyPI
26
+ from google.protobuf.json_format import MessageToDict
27
+ import json
28
+
29
+ # Project
30
+ import obplib.OBP_pb2 as OBP
31
+ from obplib.Point import Point
32
+ from obplib.Beamparameters import Beamparameters
33
+
34
+
35
+ class AcceleratingLine:
36
+ def __init__(self, p1, p2, si, sf, bp):
37
+ """
38
+ Constructor for the AcceleratingLine class
39
+
40
+ Args:
41
+ p1 (Point): The start point
42
+ p2 (Point): The end point
43
+ v0 (Int): Initial speed in µm/s
44
+ v1 (Int): Final speed in µm/s
45
+ bp (Beamparameters): Beam parameters
46
+ """
47
+ self.si = si
48
+ self.sf = sf
49
+ self.p1 = p1
50
+ self.p2 = p2
51
+ self.bp = bp
52
+
53
+ @classmethod
54
+ def from_dict(cls, kw):
55
+ """
56
+ Returns an instance of the AcceleratingLine class created using parameters specified in a dictionary
57
+
58
+ Args:
59
+ kw (dictionary): A dictionary representation of an AcceleratingLine object
60
+ """
61
+ v0 = kw["si"]
62
+ v1 = kw["sf"]
63
+ p0 = Point(kw["x0"], kw["y0"])
64
+ p1 = Point(kw["x1"], kw["y1"])
65
+ bp = Beamparameters(kw["params"]["spotSize"], kw["params"]["beamPower"])
66
+ return cls(p0, p1, v0, v1, bp)
67
+
68
+ def get_pb(self):
69
+ """
70
+ Returns a protobuf packet containing AcceleratingLine information
71
+
72
+ Returns:
73
+ Protobuf packet: A packet containing AccelerationLine info
74
+ """
75
+ pkt = OBP.Packet()
76
+ acc_line = OBP.AcceleratingLine()
77
+
78
+ acc_line.params.CopyFrom(self.bp.get_pb())
79
+
80
+ acc_line.x0 = self.p1.get_x()
81
+ acc_line.y0 = self.p1.get_y()
82
+ acc_line.x1 = self.p2.get_x()
83
+ acc_line.y1 = self.p2.get_y()
84
+
85
+ acc_line.si = int(self.si)
86
+ acc_line.sf = int(self.sf)
87
+
88
+ pkt.accelerating_line.CopyFrom(acc_line)
89
+
90
+ return pkt
91
+
92
+ def get_packet(self, pkt):
93
+ """
94
+ Returns a serialized packet
95
+ Args:
96
+ pkt (binary): A packet
97
+
98
+ Returns:
99
+ A serialized packet (String): A serialized packet
100
+ """
101
+ return pkt.SerializeToString()
102
+
103
+ def write_obp(self):
104
+ return self.get_packet(self.get_pb())
105
+
106
+ def get_obpj(self):
107
+ """
108
+ Overloaded writer method - is used by the FileHandler
109
+
110
+ Returns:
111
+ d (dictionary): A dictionary representation of a protobuf packet
112
+ """
113
+ return MessageToDict(self.get_pb())
114
+
115
+ def __repr__(self):
116
+ return json.dumps(self.get_obpj(), indent=2)
117
+
118
+ def translate(self, V):
119
+ """
120
+ Returns a new AcceleratomgLine object which has been translated according to the given vector V
121
+
122
+ Args:
123
+ V (Vector): A translation vector
124
+
125
+ Returns:
126
+ AcceleratingLine: An AcceleratingLine which has been traslated according to the given vector
127
+ """
128
+ return AcceleratingLine(
129
+ self.p1.translate(V), self.p2.translate(V), self.si, self.sf, self.bp
130
+ )
131
+
132
+ def rotate(self, theta):
133
+ """
134
+ Returns a new AcceleratingLine which has been rotated theta radians around the origin
135
+
136
+ Args:
137
+ theta (radians): The amount of radians which the AccelerartingLine is to be rotated
138
+
139
+ Returns:
140
+ AcceleratingLine: An AcceleratingLine rotated theta radians relative to the origin
141
+ """
142
+ return AcceleratingLine(
143
+ self.p1.rotate(theta), self.p2.rotate(theta), self.si, self.sf, self.bp
144
+ )
145
+
146
+ def scale(self, factor):
147
+ """
148
+ Returns a new AcceleratingLine scaled by the specified factor.
149
+
150
+ Args:
151
+ factor (float): The factor with which to scale the AcceleratingLine
152
+
153
+ Returns:
154
+ AcceleratingLine: An AcceleratingLine scaled using the specified factor
155
+ """
156
+ return AcceleratingLine(
157
+ self.p1.scale(factor), self.p2.scale(factor), self.si, self.sf, self.bp
158
+ )
@@ -0,0 +1,52 @@
1
+ # SPDX-FileCopyrightText: 2019,2020 Freemelt AB
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ """
6
+ Copyright 2019,2020 Freemelt AB
7
+
8
+ Licensed under the Apache License, Version 2.0 (the "License");
9
+ you may not use this file except in compliance with the License.
10
+ You may obtain a copy of the License at
11
+
12
+ http://www.apache.org/licenses/LICENSE-2.0
13
+
14
+ Unless required by applicable law or agreed to in writing, software
15
+ distributed under the License is distributed on an "AS IS" BASIS,
16
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ See the License for the specific language governing permissions and
18
+ limitations under the License.
19
+ """
20
+
21
+ """@Beamparameters docstring
22
+
23
+ This class represents parameters associated with a given beam segment.
24
+ """
25
+
26
+ # Project
27
+ import obplib.OBP_pb2 as OBP
28
+
29
+
30
+ class Beamparameters:
31
+ def __init__(self, spot_size, power):
32
+ """
33
+ Constructor for the Beamparameters class
34
+
35
+ Args:
36
+ spotSize (Int): Desired spot size in µm
37
+ power (Int): Desired beam power in W
38
+ """
39
+ self.spot_size = spot_size
40
+ self.power = power
41
+
42
+ def get_pb(self):
43
+ """
44
+ Returns a protobuf packet containing the specified beam paramters
45
+
46
+ Returns:
47
+ Beamparameters packet (binary): A packet containing beam parameter information
48
+ """
49
+ pkt = OBP.BeamParameters()
50
+ pkt.beam_power = self.power
51
+ pkt.spot_size = self.spot_size
52
+ return pkt
obplib/Curve.py ADDED
@@ -0,0 +1,246 @@
1
+ # SPDX-FileCopyrightText: 2019,2020 Freemelt AB
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ """
6
+ Copyright 2019,2020 Freemelt AB
7
+
8
+ Licensed under the Apache License, Version 2.0 (the "License");
9
+ you may not use this file except in compliance with the License.
10
+ You may obtain a copy of the License at
11
+
12
+ http://www.apache.org/licenses/LICENSE-2.0
13
+
14
+ Unless required by applicable law or agreed to in writing, software
15
+ distributed under the License is distributed on an "AS IS" BASIS,
16
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ See the License for the specific language governing permissions and
18
+ limitations under the License.
19
+ """
20
+
21
+ """@Curve docstring
22
+ A class which describes a cubic bezier curve corresponding to a given speed and given parameters.
23
+ The curve starts at P1, aims at P2 and ends at P4, via P3.
24
+ """
25
+
26
+ # PyPI
27
+ from google.protobuf.json_format import MessageToDict
28
+ from google.protobuf.message import Message
29
+ import math
30
+
31
+
32
+ import json
33
+
34
+
35
+ # Project
36
+ import obplib.OBP_pb2 as OBP
37
+ from obplib.Beamparameters import Beamparameters
38
+ from obplib.Point import Point
39
+ from obplib.PathSegment import PathSegment
40
+
41
+
42
+ class Curve(PathSegment):
43
+ def __init__(self, P1, P2, P3, P4, speed, bp):
44
+ """
45
+
46
+ Constructor for the Curve class
47
+
48
+ Args:
49
+ P1 (Point): The starting point
50
+ P2 (Point): First control point
51
+ P3 (Point): Second control point
52
+ P4 (Point): End point
53
+ speed (Int): Scan speed in µm/s
54
+ bp (Beamparamters): Desired beam parameters
55
+ """
56
+ super().__init__()
57
+ self.P1 = P1
58
+ self.P2 = P2
59
+ self.P3 = P3
60
+ self.P4 = P4
61
+ self.speed = speed
62
+ self.bp = bp
63
+
64
+ @classmethod
65
+ def from_dict(cls, kw):
66
+ """
67
+ Returns an instance of the Curve class created using parameters specified in a dictionary
68
+
69
+ Args:
70
+ kw (dictionary): A dictionary representation of a Curve object
71
+ """
72
+ P1 = Point(kw["p0"]["x"], kw["p0"]["y"])
73
+ P2 = Point(kw["p1"]["x"], kw["p1"]["y"])
74
+ P3 = Point(kw["p2"]["x"], kw["p2"]["y"])
75
+ P4 = Point(kw["p3"]["x"], kw["p3"]["y"])
76
+ bp = Beamparameters(kw["params"]["spotSize"], kw["params"]["beamPower"])
77
+ speed = int(kw["speed"])
78
+ return cls(P1, P2, P3, P4, speed, bp)
79
+
80
+ def get_pb(self) -> Message:
81
+ """
82
+ Returns a protobuf packet containing curve information
83
+
84
+ Returns:
85
+ pkt (binary): A protobuf packet containing curve information
86
+ """
87
+ pkt = OBP.Packet()
88
+ curve = OBP.Curve()
89
+
90
+ curve.speed = self.speed
91
+ curve.params.CopyFrom(self.bp.get_pb())
92
+
93
+ cp0 = OBP.Curve.Point()
94
+ cp1 = OBP.Curve.Point()
95
+ cp2 = OBP.Curve.Point()
96
+ cp3 = OBP.Curve.Point()
97
+
98
+ cp0.x = self.P1.get_x()
99
+ cp0.y = self.P1.get_y()
100
+
101
+ cp1.x = self.P2.get_x()
102
+ cp1.y = self.P2.get_y()
103
+
104
+ cp2.x = self.P3.get_x()
105
+ cp2.y = self.P3.get_y()
106
+
107
+ cp3.x = self.P4.get_x()
108
+ cp3.y = self.P4.get_y()
109
+
110
+ curve.p0.CopyFrom(cp0)
111
+ curve.p1.CopyFrom(cp1)
112
+ curve.p2.CopyFrom(cp2)
113
+ curve.p3.CopyFrom(cp3)
114
+
115
+ pkt.curve.CopyFrom(curve)
116
+
117
+ return pkt
118
+
119
+ def get_packet(self, packet) -> bytes:
120
+ """
121
+ Returns a serialized packet
122
+ Args:
123
+ packet (binary): A packet
124
+
125
+ Returns:
126
+ A serialized packet (String): A serialized packet
127
+ """
128
+ pkt = packet.SerializeToString()
129
+ return pkt
130
+
131
+ def write_obp(self) -> bytes:
132
+ return self.get_packet(self.get_pb())
133
+
134
+ def get_obpj(self) -> dict:
135
+ """
136
+ Overloaded writer method - is used by the FileHandler
137
+
138
+ Returns:
139
+ (dictionary): A dictionary representation of a protobuf packet
140
+ """
141
+ return MessageToDict(self.get_pb())
142
+
143
+ def __repr__(self):
144
+ return json.dumps(self.get_obpj(), indent=2)
145
+
146
+ def translate(self, V):
147
+ """
148
+ Returns a new curve that has been translated using the translation vector V
149
+
150
+ Args:
151
+ V (Vector): A translation vector
152
+
153
+ Returns:
154
+ Curve: A curve that has been translate according to the given vector
155
+ """
156
+ return Curve(
157
+ self.P1.translate(V),
158
+ self.P2.translate(V),
159
+ self.P3.translate(V),
160
+ self.P4.translate(V),
161
+ self.speed,
162
+ self.bp,
163
+ )
164
+
165
+ def rotate(self, theta):
166
+ """
167
+ Returns a new Curve which has been rotated theta radians around the origin
168
+
169
+ Args:
170
+ theta (radians): The amount of radians which the curve is to be rotated
171
+
172
+ Returns:
173
+ Curve: A curve rotated theta radians relative to the origin
174
+ """
175
+ return Curve(
176
+ self.P1.rotate(theta),
177
+ self.P2.rotate(theta),
178
+ self.P3.rotate(theta),
179
+ self.P4.rotate(theta),
180
+ self.speed,
181
+ self.bp,
182
+ )
183
+
184
+ def scale(self, factor):
185
+ """
186
+ Returns a new Curve scaled by the specified factor.
187
+
188
+ Args:
189
+ factor (float): The factor with which to scale the Curve
190
+
191
+ Returns:
192
+ Curve: A curve scaled using the specified factor
193
+ """
194
+ return Curve(
195
+ self.P1.scale(factor),
196
+ self.P2.scale(factor),
197
+ self.P3.scale(factor),
198
+ self.P4.scale(factor),
199
+ self.speed,
200
+ self.bp,
201
+ )
202
+
203
+ def _bez(self, p0, p1, p2, p3, tc):
204
+ t2 = tc * tc
205
+ t3 = t2 * tc
206
+
207
+ mt = 1 - tc
208
+ mt2 = mt * mt
209
+ mt3 = mt2 * mt
210
+
211
+ return p0 * mt3 + 3 * p1 * mt2 * tc + 3 * p2 * mt * t2 + p3 * t3
212
+
213
+ def arc_length_approximation(self):
214
+ dist = 0
215
+ curr_x = self.P1.get_x()
216
+ curr_y = self.P1.get_y()
217
+ approximation_steps = 32
218
+ for i in range(approximation_steps):
219
+ x = self._bez(
220
+ self.P1.get_x(),
221
+ self.P2.get_x(),
222
+ self.P3.get_x(),
223
+ self.P4.get_x(),
224
+ i / approximation_steps,
225
+ )
226
+ y = self._bez(
227
+ self.P1.get_y(),
228
+ self.P2.get_y(),
229
+ self.P3.get_y(),
230
+ self.P4.get_y(),
231
+ i / approximation_steps,
232
+ )
233
+
234
+ part_dist = math.sqrt(math.pow(curr_x - x, 2) + math.pow(curr_y - y, 2))
235
+
236
+ dist = part_dist + dist
237
+ curr_x = x
238
+ curr_y = y
239
+
240
+ return dist
241
+
242
+ def get_segment_length(self) -> float:
243
+ return self.arc_length_approximation()
244
+
245
+ def get_segment_duration(self) -> float:
246
+ return self.get_segment_length() / self.speed