summaryrefslogtreecommitdiff
path: root/calibration.e
blob: fc6b3e05f8578f0bfd8d7f8530656a3452d43ef0 (plain)
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
class CALIBRATION
-- This class is able to
-- - build some calibration grid with user assistance,
-- - save calibration grid,
-- - reload calibration grid,
-- - use calibration grid for coordinates conversion.

-- Calibration file format:
-- device_description_file_name
-- [offset_map] x
-- [offset_map] y
-- [parameter_offset] p0 x
-- [parameter_offset] p0 y
-- [parameter_offset] p1 x
-- [parameter_offset] p1 y
-- ...
-- [parameter_offset] pnb_param-1 x
-- [parameter_offset] pnb_param-1 y
--
--
-- offset_map format:
-- nb_point_x nb_point_y
-- offset
-- ...
-- offset (nb_point_x * nb_point_y values)
--
--
-- parameter_offset format:
-- nb_point_x nb_point_y nb_param_values
-- offset
-- ...
-- offset (nb_point_x * nb_point_y * nb_param_values values)
--
	
create {ANY}
	make, load_from

create {CALIBRATION_HANDLER}
	uncalibrated
	
feature {}
	make is
		do
		end

feature {ANY}
	write_to (os: OUTPUT_STREAM) is
		require
			os.is_connected
		local
			i: INTEGER
		do
			os.put_line(device_description_name)
			x_map.write_to(os)
			y_map.write_to(os)
			from
				i := x_parameters_maps.lower
			until
				i > x_parameters_maps.upper
			loop
				x_parameters_maps.item(i).write_to(os)
				y_parameters_maps.item(i).write_to(os)
				i := i + 1
			end
		ensure
			os.is_connected
		end

	load_from (data: INPUT_STREAM) is
		require
			data.is_connected
		local
			i: INTEGER
			po: PARAMETER_OFFSET
			device_file: TEXT_FILE_READ
		do
			data.read_line
			device_description_name := data.last_string.twin
			create device_file.connect_to(device_description_name)
			create device.load_from(device_file)
			device_file.disconnect
			create x_map.load_from(data, device)
			create y_map.load_from(data, device)
			from
				create x_parameters_maps.make(device.nb_parameters)
				create y_parameters_maps.make(device.nb_parameters)
				i := x_parameters_maps.lower
			until
				i > x_parameters_maps.upper
			loop
				create po.load_from(data, device, i + 1)
				x_parameters_maps.put(po, i)
				create po.load_from(data, device, i + 1)
				y_parameters_maps.put(po, i)
				i := i + 1
			end
		end


	convert (x_device, y_device: INTEGER ; params: FAST_ARRAY[INTEGER]) is
			-- Result is available in last_x, last_y
		require
			x_device.in_range(0, device.width)
			y_device.in_range(0, device.height)
			params /= Void implies params.count = device.nb_parameters
		local
			i: INTEGER
		do
			last_x := x_device + x_map.offset_at(x_device, y_device)
			last_y := y_device + y_map.offset_at(x_device, y_device)
			if params /= Void then
				from
					i := x_parameters_maps.lower
				until
					i > x_parameters_maps.upper
				loop
					last_x := last_x + x_parameters_maps.item(i).offset_at(x_device, y_device, params.item(i))
					last_y := last_y + y_parameters_maps.item(i).offset_at(x_device, y_device, params.item(i))
					i := i + 1
				end
			end
			if last_x < 0 then
				last_x := 0
			end
			if last_y < 0 then
				last_y := 0
			end
			if last_x > device.width then
				last_x := device.width
			end
			if last_y > device.height then
				last_y := device.height
			end
		ensure
			last_x.in_range(0, device.width)
			last_y.in_range(0, device.height)
		end

	last_x, last_y: INTEGER
			-- Last conversion result (in device coordinates)

feature {}
	partial_offset (x_device, y_device, param_index: INTEGER) is
			-- Sum offset with parameters set to 0, up to param_index excluded.
			-- Result is available in last_x, last_y
		require
			x_device.in_range(0, device.width)
			y_device.in_range(0, device.height)
			param_index.in_range(0, device.nb_parameters - 1)
		local
			i: INTEGER
		do
			last_x := x_map.offset_at(x_device, y_device)
			last_y := y_map.offset_at(x_device, y_device)
			from
				i := x_parameters_maps.lower
			until
				i >= param_index
			loop
				last_x := last_x + x_parameters_maps.item(i).offset_at(x_device, y_device, 0)
				last_y := last_y + y_parameters_maps.item(i).offset_at(x_device, y_device, 0)
				i := i + 1
			end
		end

feature {}
	file_tools: FILE_TOOLS
	
	uncalibrated(device_file_name: STRING) is
		require
			file_tools.file_exists(device_file_name)
		local
			i: INTEGER
			po: PARAMETER_OFFSET
			device_file: TEXT_FILE_READ
		do
			device_description_name := device_file_name
			create device_file.connect_to(device_file_name)
			create device.load_from(device_file)
			device_file.disconnect
			create x_map.uncalibrated(device)
			create y_map.uncalibrated(device)
			from
				create x_parameters_maps.make(device.nb_parameters)
				create y_parameters_maps.make(device.nb_parameters)
				i := x_parameters_maps.lower
			until
				i > x_parameters_maps.upper
			loop
				create po.uncalibrated(device, i + 1)
				x_parameters_maps.put(po, i)
				create po.uncalibrated(device, i + 1)
				y_parameters_maps.put(po, i)
				i := i + 1
			end
			create reference_points.make(device.nb_parameters)
		end

	reference_points: REFERENCE_POINTS

	rebuild_maps is
		local
			i: INTEGER
		do
 			from
 				i := x_parameters_maps.lower
 			until
 				i > x_parameters_maps.upper
 			loop
 				x_parameters_maps.item(i).reset
 				y_parameters_maps.item(i).reset
 				i := i + 1
 			end
			from
				build_position_maps
				i := x_parameters_maps.lower
			until
				i > x_parameters_maps.upper
			loop
				build_parameter_maps(i)
				i := i + 1
			end
		end

feature {} -- building maps
--	nb_intervals: INTEGER is 100 --*** auto ?
	nb_intervals: INTEGER is 10

	build_position_maps is
		local
			i, j: INTEGER
			xmap, ymap: FAST_ARRAY2[INTEGER]
		do
			from
				xmap := x_map.empty_map(nb_intervals + 1)
				ymap := y_map.empty_map(nb_intervals + 1)
				i := xmap.lower1
			until
				i > xmap.upper1
			loop
				from
					j := xmap.lower2
				until
					j > xmap.upper2
				loop
					xmap.put(build_x_offset_for(i/nb_intervals, j/nb_intervals), i, j)
					ymap.put(build_y_offset_for(i/nb_intervals, j/nb_intervals), i, j)
					j := j + 1
				end
				i := i + 1
			end
		end

	build_x_offset_for(x, y: REAL): INTEGER is
		local
			i, j, v: INTEGER
			continue: BOOLEAN
			left_index, right_index: INTEGER
			left_distance, right_distance: REAL
			left_error, right_error: INTEGER
			tmp, length, dev: REAL
		do
			from
				left_distance := device.nb_parameters * 3
				right_distance := left_distance
				left_index := -1
				right_index := -1
				i := reference_points.count - 1
			until
				i < 0
			loop
				from
					tmp := (reference_points.x_target(i) / device.width - x) * 3
					length := tmp * tmp
					tmp := reference_points.y_target(i) / device.height - y
					length := length + tmp * tmp
					j := device.nb_parameters - 1
					continue := True
				until
					j < 0 or not continue
				loop
					v := reference_points.parameter_value(i, j)
					dev := rate(v, device.min_parameter(j + 1), device.max_parameter(j + 1))
					continue := dev.in_range(0.4, 0.6)
					dev := dev - 0.5
					length := length + dev * dev
					j := j - 1
				end
				if continue then
					if reference_points.x_target(i) / device.width < x then
						if length < left_distance then
							left_index := i
							left_distance := length
						end
					else
						if length < right_distance then
							right_index := i
							right_distance := length
						end
					end
				end
				i := i - 1
			end
			if right_index = -1 then
				Result := reference_points.x_target(left_index) - reference_points.x_measure(left_index)
			elseif left_index = -1 then
				Result := reference_points.x_target(right_index) - reference_points.x_measure(right_index)
			else
				left_distance := left_distance.sqrt
				right_distance := right_distance.sqrt
-- 				io.put_string(once "x = "); io.put_real(x); io.put_string(once "  y = "); io.put_real(y); io.put_new_line
-- 				io.put_string(once "ld = "); io.put_real(left_distance); io.put_character(' '); io.put_integer(left_index);io.put_new_line
-- 				io.put_string(once "rd = "); io.put_real(right_distance); io.put_character(' '); io.put_integer(right_index); io.put_new_line
				left_error := reference_points.x_target(left_index) - reference_points.x_measure(left_index)
				right_error := reference_points.x_target(right_index) - reference_points.x_measure(right_index)
-- 				io.put_string(once "le = "); io.put_real(left_error); io.put_new_line
-- 				io.put_string(once "re = "); io.put_real(right_error); io.put_new_line
				Result := ((right_error*left_distance + left_error*right_distance) / (left_distance + right_distance)).force_to_integer_32
-- 				io.put_string(once "R = "); io.put_integer(Result); io.put_new_line; io.put_new_line
			end
		end

	build_y_offset_for(x, y: REAL): INTEGER is
		local
			i, j, v: INTEGER
			continue: BOOLEAN
			up_index, down_index: INTEGER
			up_distance, down_distance: REAL
			up_error, down_error: INTEGER
			tmp, length, dev: REAL
		do
			from
				up_distance := device.nb_parameters * 3
				down_distance := up_distance
				up_index := -1
				down_index := -1
				i := reference_points.count - 1
			until
				i < 0
			loop
				from
					tmp := reference_points.x_target(i) / device.width - x
					length := tmp * tmp
					tmp := (reference_points.y_target(i) / device.height - y) * 3
					length := length + tmp * tmp
					j := device.nb_parameters - 1
					continue := True
				until
					j < 0 or not continue
				loop
					v := reference_points.parameter_value(i, j)
					dev := rate(v, device.min_parameter(j + 1), device.max_parameter(j + 1))
					continue := dev.in_range(0.4, 0.6)
					dev := dev - 0.5
					length := length + dev * dev
					j := j - 1
				end
				if continue then
					if reference_points.y_target(i) / device.height < y then
						if length < up_distance then
							up_index := i
							up_distance := length
						end
					else
						if length < down_distance then
							down_index := i
							down_distance := length
						end
					end
				end
				i := i - 1
			end
			if down_index = -1 then
				Result := reference_points.y_target(up_index) - reference_points.y_measure(up_index)
			elseif up_index = -1 then
				Result := reference_points.y_target(down_index) - reference_points.y_measure(down_index)
			else
				up_distance := up_distance.sqrt
				down_distance := down_distance.sqrt
				up_error := reference_points.y_target(up_index) - reference_points.y_measure(up_index)
				down_error := reference_points.y_target(down_index) - reference_points.y_measure(down_index)
				Result := ((down_error*up_distance + up_error*down_distance) / (up_distance + down_distance)).force_to_integer_32
			end
		end

	param_division: INTEGER is 4
	
	build_parameter_maps(param: INTEGER) is
		require
			param.in_range(0, device.nb_parameters - 1)
		local
			i, j, k: INTEGER
			xmap, ymap: FAST_ARRAY3[INTEGER]
			hstep, vstep: INTEGER
			correction_x, correction_y: INTEGER
		do
			hstep := x_map.horizontal_step.to_integer_32
			vstep := y_map.vertical_step.to_integer_32
			xmap := x_parameters_maps.item(param).empty_map(nb_intervals + 1, param_division + 1)
			ymap := y_parameters_maps.item(param).empty_map(nb_intervals + 1, param_division + 1)
			from
				i := xmap.lower1
			until
				i > xmap.upper1
			loop
				from
					j := xmap.lower2
				until
					j > xmap.upper2
				loop
					from
						k := xmap.lower3
						partial_offset(i * hstep, j * vstep, param)
						correction_x := last_x
						correction_y := last_y
					until
						k > xmap.upper3
					loop
						xmap.put(build_x_offset_for_parameter(i/nb_intervals, j/nb_intervals, k / param_division, param, correction_x), i, j, k)
						ymap.put(build_y_offset_for_parameter(i/nb_intervals, j/nb_intervals, k / param_division, param, correction_y), i, j, k)
						k := k + 1
					end
					j := j + 1
				end
				i := i + 1
			end
		end

	build_x_offset_for_parameter(x, y, z: REAL; index: INTEGER; cumulated_correction: INTEGER): INTEGER is
		local
			i, j, v: INTEGER
			continue: BOOLEAN
			left_index, right_index: INTEGER
			left_distance, right_distance: REAL
			left_error, right_error: INTEGER
			tmp, length, dev: REAL
		do
			from
				left_distance := device.nb_parameters * 3
				right_distance := left_distance
				left_index := -1
				right_index := -1
				i := reference_points.count - 1
			until
				i < 0
			loop
				from
					tmp := reference_points.x_target(i) / device.width - x
					length := tmp * tmp
					tmp := reference_points.y_target(i) / device.height - y
					length := length + tmp * tmp
					j := device.nb_parameters - 1
					continue := True
				until
					j < 0 or not continue
				loop
					v := reference_points.parameter_value(i, j)
					dev := rate(v, device.min_parameter(j + 1), device.max_parameter(j + 1))
					if j = index then
						continue := dev.in_range(z - 0.15, z + 0.15)
						dev := (dev - z) * 3
					else
						continue := dev.in_range(0.4, 0.6)
						dev := dev - 0.5
					end
					length := length + dev * dev
					j := j - 1
				end
				if continue then
					if reference_points.x_target(i) / device.width < x then
						if length < left_distance then
							left_index := i
							left_distance := length
						end
					else
						if length < right_distance then
							right_index := i
							right_distance := length
						end
					end
				end
				i := i - 1
			end
			if right_index /= -1 then
				if left_index /= -1 then
					left_distance := left_distance.sqrt
					right_distance := right_distance.sqrt
					left_error := reference_points.x_target(left_index) - reference_points.x_measure(left_index)
					right_error := reference_points.x_target(right_index) - reference_points.x_measure(right_index)
					Result := ((right_error*left_distance + left_error*right_distance) / (left_distance + right_distance)).force_to_integer_32 - cumulated_correction
				else
					Result := reference_points.x_target(right_index) - reference_points.x_measure(right_index) - cumulated_correction
				end
			else
				if left_index /= -1 then
					Result := reference_points.x_target(left_index) - reference_points.x_measure(left_index) - cumulated_correction
				else
					Result := 0
				end
			end
		end

	build_y_offset_for_parameter(x, y, z: REAL; index: INTEGER; cumulated_correction: INTEGER): INTEGER is
		do
		end

	rate(value, min, max: INTEGER): REAL is
		require
			value.in_range(min, max)
		do
			Result := (value - min) / (max - min)
		ensure
			Result.in_range(0, 1)
		end

feature {CALIBRATION_HANDLER}
	add_measure(x_target, y_target: INTEGER; x_measure, y_measure: INTEGER; parameters_values: FAST_ARRAY[INTEGER]) is
		require
			device.valid_x(x_target)
			device.valid_y(y_target)
			device.valid_x(x_measure)
			device.valid_y(y_measure)
			device.valid_parameters(parameters_values)
		do
			reference_points.add_point(x_target, y_target, x_measure, y_measure, parameters_values)
			rebuild_maps
		end

	x_map, y_map: OFFSET_MAP

	x_parameters_maps, y_parameters_maps: FAST_ARRAY[PARAMETER_OFFSET]

	device: DEVICE_DESCRIPTION

feature {}
	device_description_name: STRING

invariant
	(x_parameters_maps /= Void) = (y_parameters_maps /= Void)
	
	x_parameters_maps /= Void implies x_parameters_maps.count = y_parameters_maps.count

	device.nb_parameters = 0 implies x_parameters_maps = Void

	device.nb_parameters /= 0 implies x_parameters_maps.count = device.nb_parameters
end