-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2980 lines (2358 loc) · 119 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null,"activeClass":"disqus"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="Xiaoxing Chen's Blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Xiaoxing Chen's Blog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Xiaoxing">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://yoursite.com/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false
};
</script>
<title>Xiaoxing Chen's Blog</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="Toggle navigation bar">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<div>
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Xiaoxing Chen's Blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>About</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content">
<div class="posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="en">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2022/03/20/dual_number_auto_derivative_on_so3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="Xiaoxing">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Xiaoxing Chen's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2022/03/20/dual_number_auto_derivative_on_so3/" class="post-title-link" itemprop="url">Dual Number and Auto Derivative of Special Orthogonal Group</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2022-03-20 00:00:00" itemprop="dateCreated datePublished" datetime="2022-03-20T00:00:00+08:00">2022-03-20</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2022-06-14 01:01:35" itemprop="dateModified" datetime="2022-06-14T01:01:35+08:00">2022-06-14</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<span class="post-meta-item-text">Disqus: </span>
<a title="disqus" href="/2022/03/20/dual_number_auto_derivative_on_so3/#disqus_thread" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2022/03/20/dual_number_auto_derivative_on_so3/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<head>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<style>
.bib ol {
counter-reset: item
}
.bib li {
display: block;
counter-increment: item;
}
.bib li:before {
content: " ["counter(item)"] ";
}
</style>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/github.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<h1 id="background">Background</h1>
<p>
Finding the closed-form Jacobian matrix of a residual function is one of the most time consuming works
in solving a nonlinear optimization problem.
A work around is to use numerical Jacobian. i.e. manually give a small increment in every
variable and find out the increment in residual. However the selection of the increment
depends on the data magnitude. An inappropriate increment can lead to fluctuation before convergence.
</p>
<p>
In google <a href="http://ceres-solver.org/" target="_blank" rel="noopener">ceres-solver</a>, an auto derivative method is introduced to
liberate developers from complicated Jacobian debugging. This blog introduces the principle behind the auto derivative
and explains a <i>Perspective-N-Point</i> problem demo based on auto derivative and nonlinear optimization.
</p>
<h1 id="dual_number_albegra">Dual Number Algebra</h1>
<p>
The basic idea of auto derivative is to extend the input of a single variable function.
Consider function \(y = f(x)\), the input \(x\) is no longer a variable, but an out put
of another function with first order slop \( b\varepsilon \). Where \(\varepsilon\) is the basis of the
increment, real number \(b\) measures the magnitude of the increment. Therefore, variable \(x\)
is expressed as \(x = a + b \varepsilon\).
</p>
<p>Define \(\varepsilon ^2 = 0\) to drop infinitesimals with order higher than one.</p>
<h2 id="addition">Addition</h2>
\[
(a + b \varepsilon) + (c + d \varepsilon) = (a + c) + (b + d) \varepsilon
\]
<p>Identity of Addition:</p>
\[
(a + b \varepsilon) + (0 + 0 \varepsilon) = a + b \varepsilon
\]
<p>For the C++ implementation of dual number addition, see
<a href="https://github.com/XiaoxingChen/mxm/blob/e004ef892dce35436194b5b7a93c7e522c256fbe/inc/mxm/linalg_dual_number.h#L93" target="_blank" rel="noopener">
mxm/linalg_dual_number.h:93</a>.</p>
<h2 id="multiplication">Multiplication</h2>
\[
(a + b \varepsilon)(c + d \varepsilon) = ac + (ad + bc) \varepsilon
\]
<h3>Conjugate</h3>
<p>Denote \(x = a + b \varepsilon\), its conjugate \(\bar x\) is denote as: </p>
\[
\bar{x} = a - b \varepsilon
\]
<p>Then, \(\bar{x} x = a^2 - b^2 \varepsilon^2 = a^2\). </p>
<p>Identity of Multiplication:</p>
\[
(a + b \varepsilon)(1 + 0 \varepsilon) = a + b \varepsilon
\]
<p>Inversion of Multiplication:</p>
\[
\begin{align}
\frac{1}{a + b \varepsilon} &= \frac{a - b \varepsilon}{(a + b \varepsilon) (a - b \varepsilon)}\\
&= \frac{a - b \varepsilon}{a^2} \\
\end{align}
\]
<p>Where \(a \neq 0\). Dual number division uses conjugate property to eliminate the denominator.</p>
<p>For the C++ implementation of dual number multiplication, see
<a href="https://github.com/XiaoxingChen/mxm/blob/e004ef892dce35436194b5b7a93c7e522c256fbe/inc/mxm/linalg_dual_number.h#L22" target="_blank" rel="noopener">
mxm/linalg_dual_number.h:22</a>.</p>
<h2>Derivative</h2>
<p>
For all functions that can be expanded as Taylor Series at point \(x_0\) :
</p>
\[
\begin{align}
f(x) &= f(x_0)
+ \frac{f^ {\prime} (x_0)}{1!}(x-x_0)
+ \frac{f^ {\prime \prime} (x_0)}{2!} (x-x_0)^2
+ \frac{f^ {\prime \prime \prime} (x_0)}{3!} (x-x_0)^3
+ \cdots \\
&= \sum_{\infty }^{n=0} \frac{f^{(n)}(x_0)}{n!} (x-x_0)^n \\
\end{align}
\]
<p>Set \(x = a + b \varepsilon\) and expand the series at \(x_0 = a\), </p>
\[
\begin{align}
f(a + b \varepsilon) &= f(a)
+ \frac{f^ {\prime} (a)}{1!} (b\varepsilon)
+ \frac{f^ {\prime \prime} (a)}{2!} (b\varepsilon) ^2
+ \frac{f^ {\prime \prime \prime} (a)}{3!} (b\varepsilon) ^3
+ \cdots \\
&= f(a) + f^ {\prime} (a) (b\varepsilon) \tag{2-5}\\
\end{align}
\]
<p>i.e.</p>
\[
f^{\prime }(a) \varepsilon = \frac{ f(a + b \varepsilon) - f(a) }{b} \tag{2-6}
\]
<h1>Dual Number Elementary Functions</h1>
<p>
In order to use dual number in C++ project, simply overloading operators like <code>+ - * /</code>
is not enough. C++ built in functions such as <code>sin</code>, <code>cos</code>, <code>exp</code>, <code>log</code>
don't handle dual number input. The dual number class have to support all these functions.
</p>
<p>
Luckily, most elementary functions of dual numbers can be converted to the combination of C++ built in functions.
For example:
</p>
<p>By applying formula: \(\sin(u + v) = \sin(u) \cos(v) + \cos(u) \sin(v)\) and \(\lim_{x \to 0} \frac{\sin(x)}{x} = 1 \) </p>
\[
\begin{align}
\sin(a + b \varepsilon) &= \sin(a) \cos(b \varepsilon) + \cos(a) \sin(b \varepsilon) \\
&= \sin(a) \cdot 1 + \cos(a) b \varepsilon \\
&= \sin(a) + b \cos(a) \varepsilon
\end{align}
\]
<p>Or by applying formula (2-5): \(f(a + b \varepsilon) = f(a) + f'(a) (b \varepsilon)\) </p>
\[
\sin(a + b \varepsilon) = \sin(a) + b \cos(a) \varepsilon \tag{3-1}
\]
<p>Thus the C++ implementation of the <code>sin</code> function for dual number is:</p>
<pre>
template <typename DType> DualNumber <DType>
sin(const DualNumber < DType>& val)
{
return {std::sin(val(0)), std::cos(val(0)) * val(1)};
}
</pre>
<h2>Mostly Used Elementary Functions</h2>
\[
\begin{align}
\sin(a + b \varepsilon) &= \sin(a) + b \cos(a) \varepsilon \\
\arcsin(a + b \varepsilon) &= \arcsin(a) + \frac{b \varepsilon}{\sqrt{1-a^2}} \\
\cos(a + b \varepsilon) &= \cos(a) - b \sin(a) \varepsilon \\
\arccos(a + b \varepsilon) &= \arccos(a) - \frac{b \varepsilon}{\sqrt{1-a^2}} \\
\exp(a + b \varepsilon) &= \exp(a) + b \exp(a) \varepsilon \\
\ln(a + b \varepsilon) &= \ln(a) + \frac{a}{b} \varepsilon \\
(a + b \varepsilon)^ \alpha &= a^ \alpha + (\alpha b) a^{\alpha - 1} \varepsilon \\
\end{align}
\]
<p>For the C++ implementation of the elementary functions above, see
<a href="https://github.com/XiaoxingChen/mxm/blob/e004ef892dce35436194b5b7a93c7e522c256fbe/inc/mxm/linalg_dual_number.h#L138" target="_blank" rel="noopener">
mxm/linalg_dual_number.h:138</a>.</p>
<h1>Derivative of 3D Special Orthogonal Group</h1>
<p>
Consider a rotation axis \(\phi \in \mathbb{R}^{3} \) and \(\| \phi \| = 1 \),
a rotation along axis \(\phi\) with angle \(\theta\) can be expressed as \(f(\theta) = \exp (\theta \phi^ \wedge) \).
The \((\cdot )^ \wedge \) operation converts a 3D vector into a skew-symmetric matrix.
</p>
<p>Meanwhile, \(f'(\theta) = \phi^ \wedge \exp(\theta \phi^ \wedge)\) and \(f^{(n)}(\theta) = (\phi^ \wedge)^n \exp(\theta \phi^ \wedge)\) </p>
<p>The Taylor Series of \(f(\theta)\) at point \(\theta_0\) is:</p>
\[
\begin{align}
f(\theta) &= f(\theta_0) + \frac{f'(\theta_0)}{1!}(\theta - \theta_0) + \frac{f''(\theta_0)}{2!}(\theta - \theta_0)^2 + \cdots \\
&= ( I + \phi^ \wedge (\theta - \theta_0) + \frac{(\phi^ \wedge)^2}{2!}(\theta - \theta_0)^2 + \cdots ) f(\theta_0)\\
&= \left( \sum_{n=0}^{\infty } \frac{(\phi^ \wedge)^n}{n!} (\theta - \theta_0)^n \right) f(\theta_0)
\end{align}
\]
<p>Let \(\theta = a + b \varepsilon\) and \(\theta_0 = a\), then </p>
\[
f(a + b \varepsilon) = (I + \phi^ \wedge b \varepsilon) f(a)
\]
\[
\frac{f(a + b \varepsilon) - f(a)}{b} = \phi^ \wedge f(a) \varepsilon
\]
<p>The DoF of a 3D rotation is 3, therefore three orthogonal rotation vectors are required to
find all derivative of a rotation function. Normally the standard orthogonal basis
\([0,0,1]^\top, [0,1,0]^\top, [1,0,0]^\top\) are used as \(\phi\) .
</p>
<h2>Rodrigues Formula</h2>
<p>Rodrigues formula is essential the exponential map from \(\mathfrak{so}(3)\) to \(SO(3)\). </p>
<p>Denote \(\varphi = \theta \phi\) </p>
<!-- \[
\exp(\theta \phi) = I + \sin(\theta) \phi^ \wedge + \cos (\theta) \phi^ \wedge \phi^ \wedge
\] -->
\[
\exp(\varphi) = I + \frac{\sin(\theta)}{\theta} \varphi^ \wedge + \frac{1-\cos (\theta)}{\theta^2} \varphi^ \wedge \varphi^ \wedge
\]
<p>
The numerical accuracy decrease when \(\theta \rightarrow 0\).
In normal C++ implementation,
an <code>if</code> expression
is used to eliminate the singularity:
</p>
\[
\| \varphi \| < \epsilon \Rightarrow \exp(\varphi) = I
\tag{4-3}
\]
<p>
Where \(\epsilon\) is the machine accuracy.
However the expression is incorrect when dealing with dual number input.
</p>
<p>Denote </p>
\[
\varphi =
\left[
\begin{matrix}
a_1 + b_1 \varepsilon \\
a_2 + b_2 \varepsilon \\
a_3 + b_3 \varepsilon \\
\end{matrix}
\right]
\]
<p>
Denote \(a = [a_1, a_2, a_3]^\top\). \(\theta = 0 \Rightarrow \|a\| = 0
\Leftrightarrow
\varphi ^ \wedge \varphi ^ \wedge = 0 \) but \( \not \Rightarrow \varphi ^ \wedge = 0\).
Therefore, the expression (4-3) should be corrected to
</p>
\[
\| \varphi \| < \epsilon \Rightarrow \exp(\varphi) = I + \varphi^ \wedge
\tag{4-4}
\]
<p>
<a href="https://github.com/XiaoxingChen/mxm/blob/e004ef892dce35436194b5b7a93c7e522c256fbe/inc/mxm/lie_special_orthogonal.h#L173" target="_blank" rel="noopener">Here</a>
is the C++ implementation of the Rodrigues formula that supports dual number calculation.</p>
<h2>A Demo with Rotation Auto Derivative</h2>
<p>Perspective N Point(PnP) problem tends to find the camera pose by minimizing the reprojection error
between 2D points. It is a typical nonlinear optimization problem with the state vector
constrained on a 6 DoF manifold. The demo use dual number auto derivative to find the Jacobian matrix
and the method converged correctly.
</p>
<p>
See <a href="https://github.com/XiaoxingChen/mxm/blob/e004ef892dce35436194b5b7a93c7e522c256fbe/inc/mxm/cv_3d.h#L317" target="_blank" rel="noopener">mxm/cv_3d.h:317</a> for Jacobian matrix
calculation and
<a href="https://github.com/XiaoxingChen/mxm/blob/e004ef892dce35436194b5b7a93c7e522c256fbe/tests/test_cv_basic.cpp#L537" target="_blank" rel="noopener">tests/test_cv_basic.cpp:537</a>
for PnP problem verification pipeline.
</p>
<h1>Limitations</h1>
<p>
Consider real number expression \(c = \frac{ab}{b}\). It is easy to find \(c = a\) when \(b \neq 0\)
but hard to define the value of \(c\) when \(b = 0\).
Similarly for dual number: \(x \neq \sqrt[]{x^2}\) when \(x\) is a purely nonreal dual number, i.e. \(x = b \varepsilon\).
</p>
<h1 id="references">References</h1>
<ol class="bib">
<li>
Michael Penn,
<cite>
<a href="https://youtu.be/ceaNqdHdqtg" target="_blank" rel="noopener">"The strange cousin of the complex numbers - the dual numbers."</a>
</cite>
YouTube, 2022
</li>
<li>
Wikipedia,
<cite>
<a href="https://en.wikipedia.org/wiki/Dual_number" target="_blank" rel="noopener">
"Dual number."</a>
</cite>
</li>
</ol>
</body>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="en">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2022/01/21/radial_tangential_numerical_inversion/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="Xiaoxing">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Xiaoxing Chen's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2022/01/21/radial_tangential_numerical_inversion/" class="post-title-link" itemprop="url">Numerical Inversion of Radial Tangential Distortion Model</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2022-01-21 00:00:00" itemprop="dateCreated datePublished" datetime="2022-01-21T00:00:00+08:00">2022-01-21</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2022-03-22 05:34:22" itemprop="dateModified" datetime="2022-03-22T05:34:22+08:00">2022-03-22</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<span class="post-meta-item-text">Disqus: </span>
<a title="disqus" href="/2022/01/21/radial_tangential_numerical_inversion/#disqus_thread" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2022/01/21/radial_tangential_numerical_inversion/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<head>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<style>
.bib ol { counter-reset: item }
.bib li { display: block ; counter-increment: item; }
.bib li:before { content: " ["counter(item)"] "; }
pre.md{
background-color:#eee;
border-radius: 3px;
border: 1px solid #888 ;
}
table.md, table.md td, table.md th{
border: 1px solid black;
text-align: center;
border-collapse: collapse;
}
blockquote.md{
padding: 0 1em;
border-left: .25em solid #888;
}
</style>
</head>
<body>
<h1>Background</h1>
<p>
Radial tangential distortion model is the most commonly used model in pinhole camera calibration.
Its mathematical representation is shown as formula (1-1).
By observing the formula, it is fair to say that the coordinate mapping from undistorted to distorted is natural.
But the reverse of the process is difficult because the inverse function is in implicit form.
</p>
\[
\left[
\begin{matrix}
x_d \\
y_d \\
\end{matrix}
\right]
=
\left[
\begin{matrix}
x \\
y \\
\end{matrix}
\right]+
\underbrace{
\frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6}
\left[
\begin{matrix}
x \\
y \\
\end{matrix}
\right]
}_{\text{radial}}
+
\underbrace{
\left[
\begin{matrix}
2xy & r^2 + 2x^2 \\
r^2 + 2 y^2 & 2xy \\
\end{matrix}
\right]
\left[
\begin{matrix}
p_1 \\
p_2 \\
\end{matrix}
\right]
}_{\text{tangential}}
\tag{1-1}
\]
<p>Here \(r^2 = x^2 + y^2\) </p>
<p>
In engineering application, both directions of the mapping are used.
Therefore, the implementation in OpenCV may have some indications.
Here is a description that copied from OpenCV specification:
</p>
<blockquote class="md">
where <i>undistort</i> is an <b>approximate iterative algorithm</b> that estimates the normalized original point coordinates out of the normalized distorted point coordinates ("normalized" means that the coordinates do not depend on the camera matrix) <sup><a href="#ref1">[1]</a></sup>.
</blockquote>
<p>
<i>An approximate iterative algorithm</i> proves the difficulty to find the close form of the undistort mapping.
The <a href="https://github.com/opencv/opencv/blob/master/modules/calib3d/src/undistort.dispatch.cpp#L490" target="_blank" rel="noopener">implementation</a> <sup><a href="#ref2">[2]</a></sup> in OpenCV seems to call the distortion function several times and finally get the
solution of undistort mapping. The workflow looks like a Newton Method but without any derivative calculation.
</p>
<!-- <h2>Jacobi</h2>
<p>\(\frac{\partial d_r}{\partial x}\) </p>
\[
\frac{2 x k_1 + 4x(x^2+y^2) k_2 + 6x(x^2+y^2)^2 k_3}{1 + k_4 (x^2+y^2) + k_5 (x^2+y^2)^2 + k_6 (x^2+y^2)^3}
\]
\[
\frac{1 + k_1 (x^2+y^2) + k_2 (x^2+y^2)^2 + k_3 (x^2+y^2)^3}{1 + k_4 (x^2+y^2) + k_5 (x^2+y^2)^2 + k_6 (x^2+y^2)^3}
\]
\[
\left[
\begin{matrix}
\frac{\partial x_d}{\partial x} & \frac{\partial x_d}{\partial y} \\
\frac{\partial y_d}{\partial x} & \frac{\partial y_d}{\partial y} \\
\end{matrix}
\right]
= I +
\frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} I
\] -->
<h1>Mathematical Representation</h1>
<p>Given vector-valued function \(y = f(x)\) and vector \(y_0\), find vector \(x_0\) that satisfies \(f(x_0) = y_0\) .</p>
<p>The solution would be quite clear if \(x = f^{-1}(y)\) is an explicit function.
However, for the radial tangential model that mentioned above, it is an implicit function.
Therefore, an numerical method is required to find the solution.
</p>
\[
x_{n+1} = x_{n} + (x_0 - f(x_{n}))
\]
<p>Denote \(g(x) = x + x_0 - f(x)\), </p>
\[
x_{n+1} = g(x_n)
\]
<h1>A Controller's Approach</h1>
<p>Recall the the distort function moves a pixel a bit in a specific direction.
So the undistort function is to find the original coordinate of a distorted point.
The approach used in this blog is to add the error back to the original input and finally find the
numerical solution. It sounds like an incremental PID controller.
</p>
<h2>Notations</h2>
<ul>
<li>\(P_d\): distorted point coordinate </li>
<li>\(P_n\): point coordinate in n-th iteration</li>
<li>\(K_I\): coefficient of the integral feedback</li>
<li>\(f(x)\): distort function </li>
<li>\(e_n\): error in n-th iteration, \(e_n = P_d - f(P_n)\) </li>
</ul>
<h2>Iterative Formula</h2>
<center>
<figure>
<img src="/2022/01/21/radial_tangential_numerical_inversion/i_controller.svg" alt="">
<figcaption>
Fig.1 - Controller Diagram of the Integral Controller
</figcaption>
</figure>
</center>
\[
P_{n+1} = P_n + K_I e_n
\]
\[
P_{n+1} = P_n + K_I(P_d - f(P_n)) \tag{1-2}
\]
<p>Set \(K_I = 1.0\) to simplify the problem. here is the C++ implementation in <a href="https://github.com/XiaoxingChen/mxm/blob/develop/inc/mxm/model_camera.h#L82" target="_blank" rel="noopener">mxm/model_camera.h</a>: </p>
<pre class="md">
virtual Matrix<DType> undistort(const Matrix<DType>& homo_pts) const override
{
Matrix<DType> ret(homo_pts);
const size_t iter_max = 5;
Matrix<DType> tmp;
for(size_t i = 0; i <iter_max; i++)
{
tmp = distort(ret);
ret = (homo_pts - tmp) + ret;
}
return ret;
}
</pre>
<p>If the controller works, then \( \lim_{n \to \infty} e_n = 0\), i.e. \( \lim_{n \to \infty} P_d = f(P_n) \). </p>
<h2>Test Result</h2>
<p>The undistort accuracy turned out to be good.</p>
<p style="float: left; font-size: 12pt; text-align: center; width: 45%; margin-right: 1%; margin-bottom: 0.5em;">
<img src="https://user-images.githubusercontent.com/16934019/152280752-66bcb1b4-06db-4b56-a614-299216a30057.png" style="width: 100%">
Fig. 2-1: undistorted image</p>
<p style="float: left; font-size: 12pt; text-align: center; width: 45%; margin-right: 1%; margin-bottom: 0.5em;">
<img src="https://user-images.githubusercontent.com/16934019/152280769-4cfd0a85-db12-4918-a74d-487c7dc88c98.png" style="width: 100%">
Fig. 2-2: distorted</p>
<p style="clear: both;"></p>
<p style="float: left; font-size: 12pt; text-align: center; width: 45%; margin-right: 1%; margin-bottom: 0.5em;">
<img src="https://user-images.githubusercontent.com/16934019/152281184-7b7c2c85-68fd-4c9f-9fc9-39df5526fc15.png" style="width: 100%">
Fig. 2-3: original image</p>
<p style="float: left; font-size: 12pt; text-align: center; width: 45%; margin-right: 1%; margin-bottom: 0.5em;">
<img src="https://user-images.githubusercontent.com/16934019/152280775-28d3ad23-a6ed-44fc-bb3d-edcef5b7d7ac.png" style="width: 100%">
Fig. 2-4: diff between original image and distorted image</p>
<p style="clear: both;"></p>
<h1>Know-How and Know-Why</h1>
<p>
We have found out how the iteration method works, but still don't know why.
There are several questions left to be answered.
</p>
<ol>
<li>What's the condition for convergence of the iteration method?</li>
<li>What's the convergence rate?</li>
<li>What can we do if the iteration diverged?</li>
</ol>
<p>
With a great amount of searching on the Internet I have found the
standard name of the iteration method used by OpenCV is called: Fixed-Point Iteration.
</p>
<h2>Fixed-point Iteration</h2>
<p>In general, an iterative system has the form</p>
\[
u_{n+1} = g(u_n)
\]
<p>Where \(g: \mathbb{R}^{n} \rightarrow \mathbb{R}^{n} \) is a vector-valued function.</p>
<p>A <i>fixed point</i> or <i>equilibrium</i> of a discrete dynamical system is a vector
\(u^{\star} \in \mathbb{R}^{n}\) such that<sup><a href="#ref3">[3]</a></sup> </p>
\[
g(u^\star ) = u^ \star
\]
<p>
Convergence rate of the iteration: \(\rho(g'(x))\). The method is convergent only if \(\rho(g'(x)) < 1\).
If \(\rho(g'(x)) > 1\), the procedure diverges. If \(\rho(g'(x)) < 1\) but is close to \(1.0\), convergence is quite slow
<sup>
<a href="#ref4">[4]</a>
</sup>. Here \(\rho(\cdot)\) is the spectral radial of a matrix. \(g'(x)\) is the jacobian matrix of vector-valued function \(g(x)\).
</p>
<h1>References</h1>
<ol class="bib">
<li id="ref1">
OpenCV, <a href="https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#ga55c716492470bfe86b0ee9bf3a1f0f7e" target="_blank" rel="noopener">undistortPoints</a>
</li>
<li id="ref2">
OpenCV, <a href="https://github.com/opencv/opencv/blob/master/modules/calib3d/src/undistort.dispatch.cpp#L490" target="_blank" rel="noopener">undistort implementation</a>
</li>
<li id="ref3">
Peter J. Olver, <a href="https://www-users.cse.umn.edu/~olver/num_/lne.pdf" target="_blank" rel="noopener">Numerical Solution of Scalar Equations</a>, <i>Numerical Analysis Lecture Notes</i>
</li>
<li id="ref4">
Peter J. Olver, <a href="https://www-users.cse.umn.edu/~olver/num_/lns.pdf" target="_blank" rel="noopener">Vector-Valued Iteration</a>, <i>Numerical Analysis Lecture Notes</i>
</li>
<li id="ref5">
Joe D. Hoffman, <a href="http://freeit.free.fr/Finite%20Element/Hoffman,_Numerical_Methods_for_Engineers&Scientists,2001.pdf#page=155" target="_blank" rel="noopener">Fixed-Point Iteration</a> <i>Numerical Methods for Engineers and Scientists</i>
</li>
</ol>
</body>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="en">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/03/01/ray_tracing_4d_02/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="Xiaoxing">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Xiaoxing Chen's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2021/03/01/ray_tracing_4d_02/" class="post-title-link" itemprop="url">Ray Tracing on 4-Dimensional Scene</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2021-03-01 00:00:00" itemprop="dateCreated datePublished" datetime="2021-03-01T00:00:00+08:00">2021-03-01</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2022-06-09 04:41:11" itemprop="dateModified" datetime="2022-06-09T04:41:11+08:00">2022-06-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<span class="post-meta-item-text">Disqus: </span>
<a title="disqus" href="/2021/03/01/ray_tracing_4d_02/#disqus_thread" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2021/03/01/ray_tracing_4d_02/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<head>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<style>
.bib ol {
counter-reset: item
}
.bib li {
display: block;
counter-increment: item;
}
.bib li:before {
content: " ["counter(item)"] ";
}
</style>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/github.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<h1>N-1 Dimensional Objects in N Dimensional World</h1>
<p>In this chapter we are going to explore the perspective of a 4D creature when he looks down to our 3D world.</p>
<p>N Dimensional Objects have 0 thickness in the (N+1)th dimension.
When calculating the intersection between N+1 Ray and N-Simplex,
then equation is under constrained.
</p>
<p>
One solution is to remesh to geometry with N+1 Simplex.
For example a 3D ray is hard to cannot intersect with edges of a rectangle.
But after triangulate the rectangle to two triangles, the intersection will be possible.
Likewise, to intersect a 3D triangle mesh with a 4D ray, tetrahedralization is required.
</p>
<h2>Estimate the Number of Simplices</h2>
<ul>
<li>Surface of a regular triangle with edge length \(a\): \(\frac{\sqrt{3}}{4}a^2\) </li>
<li>Volume of a regular tetrahedron<sup>[1]</sup> with edge length \(a\): \(\frac{\sqrt{2}}{12}a^3\) </li>
</ul>
<p>For a sphere with radial \(r\), one need \(N_2\) triangle to triangulate the surface. </p>
\[
\begin{align}
N_2 &= \frac{S_{sphere}}{S_{triangle}} \\
&= \frac{4 \pi r^2}{ \frac{\sqrt{3}}{4}a^2 } \\
& \leq O \left( \left( \frac{r}{a} \right)^2 \right)
\end{align}
\]
<p>But \(N_3\) tetrahedrons to tetrahedralizate the volume: </p>
\[
\begin{align}
N_3 &= \frac{V_{sphere}}{V_{tetrahedron}} \\
&= \frac{ \frac{3}{4} \pi r^3}{ \frac{\sqrt{2}}{12}a^3 } \\
& \leq O \left( \left( \frac{r}{a} \right)^3 \right)
\end{align}
\]
<p>The estimation gives an upper bound because the sphere is the geometry with smallest surface area-volum-ratio.
But it still shows the computational complexity which changes from \(O(n^2)\) to \(O(n^3)\) when using a
tretraheron to mesh the geometry instead of triangle. </p>
<h1>Prism: A Better Solution</h1>