-
Notifications
You must be signed in to change notification settings - Fork 1
/
BackwardAlgorithm.m
363 lines (281 loc) · 12.8 KB
/
BackwardAlgorithm.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
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
(* :Title: BackwardAlgorithm, Version 1.0, 2016*)
(* :Author: Kacper Pluta, [email protected]
Laboratoire d'Informatique Gaspard-Monge - LIGM, A3SI, France
*)
(* :Summary:
A package which implements algorithm which allows to find intersection of lattices of preimages of
elements of a cyclic group in the remainder range. In details, it allows to predict where in the
transformed space non-injective and non-surjective points occur under 2D digitized rigid motions.
The algorithm was introduced in:
@inproceedings{PLUTA:DGCI:2016,
author = {Pluta, Kacper and Romon, Pascal and Kenmochi, Yukiko and Passat, Nicolas},
booktitle = {{Discrete Geometry for Computer Imagery}},
pages = {359–371},
title = {{Bijective rigid motions of the 2D Cartesian grid}},
url = {http://link.springer.com/chapter/10.1007/978-3-319-32360-2_28;
https://hal-upec-upem.archives-ouvertes.fr/hal-01275598/file/article.pdf},
year = {2016}
}
*)
(* :Context: BackwardAlgorithm` *)
(* :Package Version: 1.0 *)
(* : Copyright: Kacper Pluta, 2016
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Kacper Pluta BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)
(* :History:
Created by Kacper Pluta at University Paris-Est, 2016
*)
(* :Keywords:
2D digitized rigid motions, non-injective, non-surjective
*)
(* :Mathematica Version: 10.0.0 *)
BeginPackage["BackwardAlgorithm`"]
GetNonInjectiveRegion::usage = "GetNonInjectiveRegion[index, p, q] returns Rectange[]";
GetNonSurjectiveRegion::usage = "GetNonSurjectiveRegion[index, p, q] returns Rectange[]";
FindGroupMembersInRegions::usage = "FindGroupMembersInRegions[p, q, F] returns a set of integer
points";
IntersectionSetLatticesNonInjective::usage = "IntersectionSetLatticesNonInjective[p, q, t, set] returns
subset of the set.";
IntersectionSetLatticesNonSurjective::usage = "IntersectionSetLatticesNonSurjective[p, q, t, set] returns
subset of the set.";
Begin["`Private`"]
(*
Procedure: TestPythagoreanTripleGenerators
Summary:
This procedure tests if generators of primitive Pythagorean triple are correct. If not it throws
exceptions.
Parameters:
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1
*)
TestPythagoreanTripleGenerators[p_, q_] := (
If[GCD[p, q] != 1 || EvenQ[p - q], Throw["Pythagorean rotation generates are incorrect!"]];
)
(*
Procedure: TestTranslationVector
Summary:
This procedure tests if translation vector is valid.
Parameters:
t -- 2D vector with rational elements
*)
TestTranslationVector[t_] := (
If[Length[t] != 2, Throw["Translation vector has wrong dimension!"]];
If[NotElement[Head[t[[1]]], {Rational, Integer}] || NotElement[Head[t[[2]]], {Rational,
Integer}], Throw["Translation vector has irrational elements!"]];
)
(*
Procedure: GetNonInjectiveRegion
Summary:
This procedure returns a non-injective region in the remainder range.
Parameters:
index -- an index of given non-injective region where 1 - up, 2 - right, 3 - down and 4 left. For
details look into a paper cited in :Summary: of this package.
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
Output:
A non-injective region of a given index.
*)
GetNonInjectiveRegion[index_, p_, q_] := Module[{a, b, c},
TestPythagoreanTripleGenerators[p, q];
(*primitive Pythagorean triple*)
b = 2 p q; a = p^2 - q^2; c = p^2 + q^2;
(*up*)
If[index == 1, Return[Rectangle[{b/c - 1/2, -1/2}, {1/2, 1/2 - a/c}]]];
(*right*)
If[index == 2, Return[Rectangle[{-1/2, -1/2}, {1/2 - a/c, 1/2 - b/c}]]];
(*down*)
If[index == 3, Return[Rectangle[{-1/2, a/c - 1/2}, {1/2 - b/c, 1/2}]]];
(*left*)
If[index == 4, Return[Rectangle[{a/c - 1/2, b/c - 1/2}, {1/2, 1/2}]]]
]; (* end of GetNonInjectiveRegion *)
(*
Procedure: GetNonSurjectiveRegion
Summary:
This procedure returns a non-surjective region in the remainder range.
Parameters:
index -- an index of given non-surjective region where 1 - up, 2 - right, 3 - down and 4 left. For
details look into a paper cited in :Summary: of this package.
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
Output:
A non-surjective region of a given index.
*)
GetNonSurjectiveRegion[index_, p_, q_] := Module[{a, b, c},
TestPythagoreanTripleGenerators[p, q];
b = 2 p q; a = p^2 - q^2; c = p^2 + q^2;
(*up*)
If[index == 1, Return[Rectangle[{1/2 - a/c, 3/2 - a/c - b/c}, {b/c - 1/2, 1/2}]]];
(*right*)
If[index == 2, Return[Rectangle[{3/2 - a/c - b/c, 1/2 - b/c}, {1/2, a/c - 1/2}]]];
(*down*)
If[index == 3, Return[Rectangle[{1/2 - b/c, -1/2}, {a/c - 1/2, (a/c + b/c) - 3/2}]]];
(*left*)
If[index == 4, Return[Rectangle[{-1/2, 1/2 - a/c}, {a/c + b/c - 3/2, b/c - 1/2}]]];
]; (* end of GetNonSurjectiveRegion *)
(*
Procedure: FindGroupMembersInRegions
Summary:
This procedure returns coordinates of elements of a group induces \
on remainder range. These coordinates are integers. See more in a paper cited in :Summary: section
of this package.
Parameters:
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
F -- a non-surjective or non-injective frame obtained by GetNonInjectiveRegion or
GetNonSurjectiveRegion.
Output:
A set of integer points.
*)
FindGroupMembersInRegions[p_, q_, t_, F_] := Module[{c, s, tt},
TestPythagoreanTripleGenerators[p, q];
TestTranslationVector[t];
tt = t - Round[t];
c = p^2 + q^2;
s = Solve[({p/c, q/c}*u + {-q/c, p/c}*v) + tt \[Element] F, {u, v}, Integers];
Return[Transpose[{s[[All, 1, 2]], s[[All, 2, 2]]}]];
]; (* end of FindGroupMembersInRegions *)
(*
Procedure: BezoutCoefficients
Summary:
This procedure returns Bézout coefficients used to generate lattices of preimages of elements of
cyclic group induced on the remainder range. See more in a paper cited in :Summary: section of
this package.
Parameters:
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
Output:
Two pairs of integers such that p^2 u + q^2 v = 1 (first) and a x + b y = 1 (second).
*)
BezoutCoefficients[p_, q_] := Module[{a, b, c, first, second},
TestPythagoreanTripleGenerators[p, q];
b = 2*p*q; a = p^2 - q^2; c = p^2 + q^2; n = c;
first = ExtendedGCD[p^2, q^2][[2]];
second = ExtendedGCD[a, b][[2]];
If[EvenQ[first[[1]]] || EvenQ[first[[2]]],
first[[1]] = first[[1]] + q^2;
first[[2]] = first[[2]] - p^2;
];
Return[{first, second}];
]; (* end of BezoutCoefficients *)
(*
Procedure: LatticePoint
Summary:
This procedure calculate a point of the lattice of preimegaes for an element of a cyclic group
induced on the remainder range. See more in a paper cited in :Summary: section of this package.
Parameters:
element -- corrdinates of an element in a cyclic group
x -- index in (a, -b) direction
y -- index in c (coeffs[[2]]) direction
coeffs -- Bézout coefficients (see procedure BezoutCoefficients)
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
Output:
A point such that its image in the remainder range is a given element of a cyclic group.
*)
LatticePoint[element_, x_, y_, coeffs_, p_, q_] := Module[{a, b, c},
TestPythagoreanTripleGenerators[p, q];
b = 2 p*q; a = p^2 - q^2; c = p^2 + q^2;
If[ p^2 coeffs[[1]][[1]] + q^2 coeffs[[1]][[2]] != 1, Throw["The first Bézout coefficients are
inncorect!"] ];
If[ a coeffs[[2]][[1]] + b coeffs[[2]][[2]] != 1, Throw["The second Bézout coefficients are
inncorect!"] ];
Return[( element * p * ( coeffs[[1]][[1]] - coeffs[[1]][[2]] ) / 2 + x * {a, -b} + c * y *
coeffs[[2]])];
]; (* end of LatticePoint *)
(*
Procedure: IntersectionSetLatticesNonInjective
Summary:
This procedure returns integer points in a given set such that they have non-injective mapping in
the target space.
Parameters:
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
t -- 2D translation vector
set -- a rectangular region.
Output:
A set of integer points of the set such that their mapping under 2D digitized rigid motion is
non-injective.
*)
IntersectionSetLatticesNonInjective[p_, q_, t_, set_] := Module[{a, b, c, tt, FI, MI, nonInj, s, k,
j, coeffs},
TestPythagoreanTripleGenerators[p, q];
TestTranslationVector[t];
tt = t - Round[t];
b = 2 * p * q; a = p^2 - q^2; c = p^2 + q^2;
coeffs = BezoutCoefficients[p, q];
FI = { GetNonInjectiveRegion[1, p, q], GetNonInjectiveRegion[2, p, q],
GetNonInjectiveRegion[3, p, q], GetNonInjectiveRegion[4, p, q] };
MI = {};
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[1]]]];
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[2]]]];
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[3]]]];
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[4]]]];
nonInj = {};
Do[
s = Solve[( k * p * ( coeffs[[1]][[1]] - coeffs[[1]][[2]] ) / 2 + x * {a, -b} + c * y *
coeffs[[2]]) \[Element] set, {x, y}, Integers];
s = Transpose[{s[[All, 1, 2]], s[[All, 2, 2]]}];
If[s == {}, Continue[]];
AppendTo[nonInj, {}];
Do[nonInj[[-1]] = Join[nonInj[[-1]], {LatticePoint[k, j[[1]], j[[2]], coeffs, p, q]}], {j, s}];
, {k, MI}]; (* end of outer Do[] *)
Return[nonInj];
]; (* end of IntersectionSetLatticesNonInjective *)
(*
Procedure: IntersectionSetLatticesNonSurjective
Summary:
This procedure returns integer points in a given set such that in a neighbourhood of their images
in the target space exist points without preimage.
Parameters:
p -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
q -- generator of primitive Pythagorean triple such that p - q is odd and gcd of p and q is 1.
t -- 2D translation vector
set -- a rectangular region.
Output:
A set of integer points of the set such that in a nieghbourhood of their images in the target
space exist non-surjective points.
*)
IntersectionSetLatticesNonSurjective[p_, q_, t_, set_] := Module[{a, b, c, tt, FI, MI, coeffs,
nonInj, s, k, j},
TestPythagoreanTripleGenerators[p, q];
TestTranslationVector[t];
tt = t - Round[t];
b = 2 * p * q; a = p^2 - q^2; c = p^2 + q^2;
coeffs = BezoutCoefficients[p, q];
FI = { GetNonSurjectiveRegion[1, p, q], GetNonSurjectiveRegion[2, p, q],
GetNonSurjectiveRegion[3, p, q], GetNonSurjectiveRegion[4, p, q] };
MI = {};
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[1]]]];
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[2]]]];
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[3]]]];
MI = Join[MI, FindGroupMembersInRegions[p, q, t, FI[[4]]]];
nonInj = {};
Do[
s = Solve[( k * p * ( coeffs[[1]][[1]] - coeffs[[1]][[2]] ) / 2 + x * {a, -b} + c * y *
coeffs[[2]]) \[Element] set, {x, y}, Integers];
s = Transpose[{s[[All, 1, 2]], s[[All, 2, 2]]}];
If[s == {}, Continue[]];
AppendTo[nonInj, {}];
Do[nonInj[[-1]] = Join[nonInj[[-1]], {LatticePoint[k, j[[1]], j[[2]], coeffs, p, q]}], {j, s}];
, {k, MI}]; (* end of outer Do[] *)
Return[nonInj];
]; (* end of IntersectionSetLatticesNonSurjective *)
End[]
EndPackage[]