/ *   
 
   *   P R O J E C T :   N y A R T o o l k i t C S 
 
   *   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
   *   T h i s   w o r k   i s   b a s e d   o n   t h e   o r i g i n a l   A R T o o l K i t   d e v e l o p e d   b y 
 
   *       H i r o k a z u   K a t o 
 
   *       M a r k   B i l l i n g h u r s t 
 
   *       H I T L a b ,   U n i v e r s i t y   o f   W a s h i n g t o n ,   S e a t t l e 
 
   *   h t t p : / / w w w . h i t l . w a s h i n g t o n . e d u / a r t o o l k i t / 
 
   * 
 
   *   T h e   N y A R T o o l k i t C S   i s   J a v a   e d i t i o n   A R T o o l K i t   c l a s s   l i b r a r y . 
 
   *   C o p y r i g h t   ( C ) 2 0 0 8 - 2 0 0 9   R y o   I i z u k a 
 
   * 
 
   *   T h i s   p r o g r a m   i s   f r e e   s o f t w a r e :   y o u   c a n   r e d i s t r i b u t e   i t   a n d / o r   m o d i f y 
 
   *   i t   u n d e r   t h e   t e r m s   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e   a s   p u b l i s h e d   b y 
 
   *   t h e   F r e e   S o f t w a r e   F o u n d a t i o n ,   e i t h e r   v e r s i o n   3   o f   t h e   L i c e n s e ,   o r 
 
   *   ( a t   y o u r   o p t i o n )   a n y   l a t e r   v e r s i o n . 
 
   *   
 
   *   T h i s   p r o g r a m   i s   d i s t r i b u t e d   i n   t h e   h o p e   t h a t   i t   w i l l   b e   u s e f u l , 
 
   *   b u t   W I T H O U T   A N Y   W A R R A N T Y ;   w i t h o u t   e v e n   t h e   i m p l i e d   w a r r a n t y   o f 
 
   *   M E R C H A N T A B I L I T Y   o r   F I T N E S S   F O R   A   P A R T I C U L A R   P U R P O S E .     S e e   t h e 
 
   *   G N U   G e n e r a l   P u b l i c   L i c e n s e   f o r   m o r e   d e t a i l s . 
 
   * 
 
   *   Y o u   s h o u l d   h a v e   r e c e i v e d   a   c o p y   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e 
 
   *   a l o n g   w i t h   t h i s   p r o g r a m .     I f   n o t ,   s e e   < h t t p : / / w w w . g n u . o r g / l i c e n s e s / > . 
 
   *   
 
   *   F o r   f u r t h e r   i n f o r m a t i o n   p l e a s e   c o n t a c t . 
 
   * 	 h t t p : / / n y a t l a . j p / n y a t o o l k i t / 
 
   * 	 < a i r m a i l ( a t ) e b o n y . p l a l a . o r . j p >   o r   < n y a t l a ( a t ) n y a t l a . j p > 
 
   *   
 
   * / 
 
 u s i n g   S y s t e m ; 
 
 n a m e s p a c e   j p . n y a t l a . n y a r t o o l k i t . c s . c o r e 
 
 { 
 
 
 
 
 
 
 
         / * * 
 
           *   S0n0000o00=jWjk0-[/ d000g0Y00
 
           *   00000k0o00A R T o o l K i t n0=jWjk0000000OD0~0Y00
 
           *   < p > 000000  -   
 
           *   S0n0000g0o00jk0wckMRn0^j0[^j|0jk0wck_n0^j0t`^j|h0|Ts0~0Y00
 
           *   00000h0t֊k0d0D0f0o00NNn0ǌe01 1 p a g e 0SgqW0f0O0`0U0D00
 
           *   h t t p : / / w w w . h i t l . w a s h i n g t o n . e d u / a r t o o l k i t / P a p e r s / A R T 0 2 - T u t o r i a l . p d f 
 
           *   < p r e > 
 
           *   x = x ( x i - x 0 ) , y = s ( y i - y 0 ) 
 
           *   d ^ 2 = x ^ 2 + y ^ 2 
 
           *   p = ( 1 - f d ^ 2 ) 
 
           *   x d = p x + x 0 , y d = p y + y 0 
 
           *   < / p r e > 
 
           *   < / p > 
 
           *   S0n0000o0{ @ l i n k   N y A R P a r a m } k0@b	gU000S0h00MRck0W0f0J000XSrn0Nio0naU00f0D0~0[000
 
           * / 
 
         p u b l i c   c l a s s   N y A R C a m e r a D i s t o r t i o n F a c t o r 
 
         { 
 
 
 
                 p r i v a t e   c o n s t   i n t   P D _ L O O P   =   3 ; 
 
                 p r i v a t e   d o u b l e   _ f 0 ; / / x 0 
 
                 p r i v a t e   d o u b l e   _ f 1 ; / / y 0 
 
                 p r i v a t e   d o u b l e   _ f 2 ; / / 1 0 0 0 0 0 0 0 0 . 0 * f 
 
                 p r i v a t e   d o u b l e   _ f 3 ; / / s 
 
 
 
 
 
                 / * * 
 
                   *   S0n0peo00SgqCQK00jk000000$P0000W0~0Y00
 
                   *   @ p a r a m   i _ r e f 
 
                   *   000CQn00000000
 
                   * / 
 
                 p u b l i c   v o i d   c o p y F r o m ( N y A R C a m e r a D i s t o r t i o n F a c t o r   i _ r e f ) 
 
                 { 
 
                         t h i s . _ f 0   =   i _ r e f . _ f 0 ; 
 
                         t h i s . _ f 1   =   i _ r e f . _ f 1 ; 
 
                         t h i s . _ f 2   =   i _ r e f . _ f 2 ; 
 
                         t h i s . _ f 3   =   i _ r e f . _ f 3 ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00MRn0$P0jk000000$Ph0W0f00S0n0000000k0000W0~0Y00
 
                   *   @ p a r a m   i _ f a c t o r 
 
                   *   jk000000$P0<h
}W0_0MR04  }g0B00_L0B00~0Y00
 
                   * / 
 
                 p u b l i c   v o i d   s e t V a l u e ( d o u b l e [ ]   i _ f a c t o r ) 
 
                 { 
 
                         t h i s . _ f 0   =   i _ f a c t o r [ 0 ] ; 
 
                         t h i s . _ f 1   =   i _ f a c t o r [ 1 ] ; 
 
                         t h i s . _ f 2   =   i _ f a c t o r [ 2 ] ; 
 
                         t h i s . _ f 3   =   i _ f a c t o r [ 3 ] ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo0000000$P0MRx0ԏW0~0Y00
 
                   *   @ p a r a m   o _ f a c t o r 
 
                   *   jk000000$Pn0QRHQMR04  }g0B00_L0B00~0Y00
 
                   * / 
 
                 p u b l i c   v o i d   g e t V a l u e ( d o u b l e [ ]   o _ f a c t o r ) 
 
                 { 
 
                         o _ f a c t o r [ 0 ]   =   t h i s . _ f 0 ; 
 
                         o _ f a c t o r [ 1 ]   =   t h i s . _ f 1 ; 
 
                         o _ f a c t o r [ 2 ]   =   t h i s . _ f 2 ; 
 
                         o _ f a c t o r [ 3 ]   =   t h i s . _ f 3 ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00jk00000000000
PW0~0Y00
 
                   *   00000$Po000000$Pn0'YM0U0`0Q00b'Y0So0.~\W0~0Y00
 
                   *   @ p a r a m   i _ s c a l e 
 
                   *   00000n0
Ps0
 
                   * / 
 
                 p u b l i c   v o i d   c h a n g e S c a l e ( d o u b l e   i _ s c a l e ) 
 
                 { 
 
                         t h i s . _ f 0   =   t h i s . _ f 0   *   i _ s c a l e ; / / X 
 
                         t h i s . _ f 1   =   t h i s . _ f 1   *   i _ s c a l e ; / / Y 
 
                         t h i s . _ f 2   =   t h i s . _ f 2   /   ( i _ s c a l e   *   i _ s c a l e ) ; / /   n e w p a r a m - > d i s t _ f a c t o r [ 2 ] = s o u r c e - > d i s t _ f a c t o r [ 2 ] /   ( s c a l e * s c a l e ) ; 
 
                         / / t h i s . f 3 = t h i s . f 3 ; / /   n e w p a r a m - > d i s t _ f a c t o r [ 3 ]   = s o u r c e - > d i s t _ f a c t o r [ 3 ] ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00^jp0t`^j|K00[^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i _ i n 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   o _ o u t 
 
                   *   	Yc_n0^j0SQ0S0000000
 
                   * / 
 
                 p u b l i c   v o i d   i d e a l 2 O b s e r v ( N y A R D o u b l e P o i n t 2 d   i _ i n ,   N y A R D o u b l e P o i n t 2 d   o _ o u t ) 
 
                 { 
 
                         d o u b l e   x   =   ( i _ i n . x   -   t h i s . _ f 0 )   *   t h i s . _ f 3 ; 
 
                         d o u b l e   y   =   ( i _ i n . y   -   t h i s . _ f 1 )   *   t h i s . _ f 3 ; 
 
                         i f   ( x   = =   0 . 0   & &   y   = =   0 . 0 ) 
 
                         { 
 
                                 o _ o u t . x   =   t h i s . _ f 0 ; 
 
                                 o _ o u t . y   =   t h i s . _ f 1 ; 
 
                         } 
 
                         e l s e 
 
                         { 
 
                                 d o u b l e   d   =   1 . 0   -   t h i s . _ f 2   /   1 0 0 0 0 0 0 0 0 . 0   *   ( x   *   x   +   y   *   y ) ; 
 
                                 o _ o u t . x   =   x   *   d   +   t h i s . _ f 0 ; 
 
                                 o _ o u t . y   =   y   *   d   +   t h i s . _ f 1 ; 
 
                         } 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00^jp0t`^j|K00[^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i _ i n 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   o _ o u t 
 
                   *   	Yc_n0^j0SQ0S0000000
 
                   * / 
 
                 p u b l i c   v o i d   i d e a l 2 O b s e r v ( N y A R D o u b l e P o i n t 2 d   i _ i n ,   N y A R I n t P o i n t 2 d   o _ o u t ) 
 
                 { 
 
                         t h i s . i d e a l 2 O b s e r v ( i _ i n . x ,   i _ i n . y ,   o _ o u t ) ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00^jp0t`^j|K00[^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i _ x 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   i _ y 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   o _ o u t 
 
                   *   	Yc_n0^j0SQ0S0000000
 
                   * / 
 
                 p u b l i c   v o i d   i d e a l 2 O b s e r v ( d o u b l e   i _ x ,   d o u b l e   i _ y ,   N y A R I n t P o i n t 2 d   o _ o u t ) 
 
                 { 
 
                         d o u b l e   x   =   ( i _ x   -   t h i s . _ f 0 )   *   t h i s . _ f 3 ; 
 
                         d o u b l e   y   =   ( i _ y   -   t h i s . _ f 1 )   *   t h i s . _ f 3 ; 
 
                         i f   ( x   = =   0 . 0   & &   y   = =   0 . 0 ) 
 
                         { 
 
                                 o _ o u t . x   =   ( i n t ) ( t h i s . _ f 0 ) ; 
 
                                 o _ o u t . y   =   ( i n t ) ( t h i s . _ f 1 ) ; 
 
                         } 
 
                         e l s e 
 
                         { 
 
                                 d o u b l e   d   =   1 . 0   -   t h i s . _ f 2   /   1 0 0 0 0 0 0 0 0 . 0   *   ( x   *   x   +   y   *   y ) ; 
 
                                 o _ o u t . x   =   ( i n t ) ( x   *   d   +   t h i s . _ f 0 ) ; 
 
                                 o _ o u t . y   =   ( i n t ) ( y   *   d   +   t h i s . _ f 1 ) ; 
 
                         } 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00pen0^jp00 NbW0f0t`^j|K00[^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i _ i n 
 
                   *   	YcCQn0^jMR
 
                   *   @ p a r a m   o _ o u t 
 
                   *   	Yc_n0^j0SQ0S0MR
 
                   *   @ p a r a m   i _ s i z e 
 
                   *   	YcY00^jn0Ppe0
 
                   * / 
 
                 p u b l i c   v o i d   i d e a l 2 O b s e r v B a t c h ( N y A R D o u b l e P o i n t 2 d [ ]   i _ i n ,   N y A R D o u b l e P o i n t 2 d [ ]   o _ o u t ,   i n t   i _ s i z e ) 
 
                 { 
 
                         d o u b l e   x ,   y ; 
 
                         d o u b l e   d 0   =   t h i s . _ f 0 ; 
 
                         d o u b l e   d 1   =   t h i s . _ f 1 ; 
 
                         d o u b l e   d 3   =   t h i s . _ f 3 ; 
 
                         d o u b l e   d 2 _ w   =   t h i s . _ f 2   /   1 0 0 0 0 0 0 0 0 . 0 ; 
 
                         f o r   ( i n t   i   =   0 ;   i   <   i _ s i z e ;   i + + ) 
 
                         { 
 
                                 x   =   ( i _ i n [ i ] . x   -   d 0 )   *   d 3 ; 
 
                                 y   =   ( i _ i n [ i ] . y   -   d 1 )   *   d 3 ; 
 
                                 i f   ( x   = =   0 . 0   & &   y   = =   0 . 0 ) 
 
                                 { 
 
                                         o _ o u t [ i ] . x   =   d 0 ; 
 
                                         o _ o u t [ i ] . y   =   d 1 ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         d o u b l e   d   =   1 . 0   -   d 2 _ w   *   ( x   *   x   +   y   *   y ) ; 
 
                                         o _ o u t [ i ] . x   =   x   *   d   +   d 0 ; 
 
                                         o _ o u t [ i ] . y   =   y   *   d   +   d 1 ; 
 
                                 } 
 
                         } 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00pen0^jp00 NbW0f0t`^j|K00[^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i _ i n 
 
                   *   	YcCQn0^jMR
 
                   *   @ p a r a m   o _ o u t 
 
                   *   	Yc_n0^j0SQ0S0MR
 
                   *   @ p a r a m   i _ s i z e 
 
                   *   	YcY00^jn0Ppe0
 
                   * / 
 
                 p u b l i c   v o i d   i d e a l 2 O b s e r v B a t c h ( N y A R D o u b l e P o i n t 2 d [ ]   i _ i n ,   N y A R I n t P o i n t 2 d [ ]   o _ o u t ,   i n t   i _ s i z e ) 
 
                 { 
 
                         d o u b l e   x ,   y ; 
 
                         d o u b l e   d 0   =   t h i s . _ f 0 ; 
 
                         d o u b l e   d 1   =   t h i s . _ f 1 ; 
 
                         d o u b l e   d 3   =   t h i s . _ f 3 ; 
 
                         d o u b l e   d 2 _ w   =   t h i s . _ f 2   /   1 0 0 0 0 0 0 0 0 . 0 ; 
 
                         f o r   ( i n t   i   =   0 ;   i   <   i _ s i z e ;   i + + ) 
 
                         { 
 
                                 x   =   ( i _ i n [ i ] . x   -   d 0 )   *   d 3 ; 
 
                                 y   =   ( i _ i n [ i ] . y   -   d 1 )   *   d 3 ; 
 
                                 i f   ( x   = =   0 . 0   & &   y   = =   0 . 0 ) 
 
                                 { 
 
                                         o _ o u t [ i ] . x   =   ( i n t ) d 0 ; 
 
                                         o _ o u t [ i ] . y   =   ( i n t ) d 1 ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         d o u b l e   d   =   1 . 0   -   d 2 _ w   *   ( x   *   x   +   y   *   y ) ; 
 
                                         o _ o u t [ i ] . x   =   ( i n t ) ( x   *   d   +   d 0 ) ; 
 
                                         o _ o u t [ i ] . y   =   ( i n t ) ( y   *   d   +   d 1 ) ; 
 
                                 } 
 
                         } 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00^j0[^j|K00t`^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i x 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   i y 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   o _ p o i n t 
 
                   *   	Yc_n0^j0SQ0S0000000
 
                   * / 
 
                 p u b l i c   v o i d   o b s e r v 2 I d e a l ( d o u b l e   i x ,   d o u b l e   i y ,   N y A R D o u b l e P o i n t 2 d   o _ p o i n t ) 
 
                 { 
 
                         d o u b l e   z 0 2 ,   z 0 ,   p ,   q ,   z ,   p x ,   p y ,   o p t t m p _ 1 ; 
 
                         d o u b l e   d 0   =   t h i s . _ f 0 ; 
 
                         d o u b l e   d 1   =   t h i s . _ f 1 ; 
 
 
 
                         p x   =   i x   -   d 0 ; 
 
                         p y   =   i y   -   d 1 ; 
 
                         p   =   t h i s . _ f 2   /   1 0 0 0 0 0 0 0 0 . 0 ; 
 
                         z 0 2   =   p x   *   p x   +   p y   *   p y ; 
 
                         q   =   z 0   =   M a t h . S q r t ( z 0 2 ) ; / /   O p t i m i z e / / q   =   z 0   =   M a t h . s q r t ( p x * p x +   p y * p y ) ; 
 
 
 
                         f o r   ( i n t   i   =   1 ;   ;   i + + ) 
 
                         { 
 
                                 i f   ( z 0   ! =   0 . 0 ) 
 
                                 { 
 
                                         / /   O p t i m i z e   o p t t m p _ 1 
 
                                         o p t t m p _ 1   =   p   *   z 0 2 ; 
 
                                         z   =   z 0   -   ( ( 1 . 0   -   o p t t m p _ 1 )   *   z 0   -   q )   /   ( 1 . 0   -   3 . 0   *   o p t t m p _ 1 ) ; 
 
                                         p x   =   p x   *   z   /   z 0 ; 
 
                                         p y   =   p y   *   z   /   z 0 ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         p x   =   0 . 0 ; 
 
                                         p y   =   0 . 0 ; 
 
                                         b r e a k ; 
 
                                 } 
 
                                 i f   ( i   = =   P D _ L O O P ) 
 
                                 { 
 
                                         b r e a k ; 
 
                                 } 
 
                                 z 0 2   =   p x   *   p x   +   p y   *   p y ; 
 
                                 z 0   =   M a t h . S q r t ( z 0 2 ) ; / /   O p t i m i z e / / z 0   =   M a t h . s q r t ( p x * p x +   p y * p y ) ; 
 
                         } 
 
                         o _ p o i n t . x   =   p x   /   t h i s . _ f 3   +   d 0 ; 
 
                         o _ p o i n t . y   =   p y   /   t h i s . _ f 3   +   d 1 ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00^j0[^j|K00t`^j|x0	YcW0~0Y00
 
                   *   @ p a r a m   i x 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   i y 
 
                   *   	YcCQn0^j
 
                   *   @ p a r a m   o _ v e c l i n e a r 
 
                   *   	Yc_n0^j0SQ0S00000000{ @ l i n k   N y A R V e c L i n e a r 2 d # x } h0{ @ l i n k   N y A R V e c L i n e a r 2 d # y } n00k0$P0000W0~0Y00
 
                   * / 
 
                 p u b l i c   v o i d   o b s e r v 2 I d e a l ( d o u b l e   i x ,   d o u b l e   i y ,   N y A R V e c L i n e a r 2 d   o _ v e c l i n e a r ) 
 
                 { 
 
                         d o u b l e   z 0 2 ,   z 0 ,   p ,   q ,   z ,   p x ,   p y ,   o p t t m p _ 1 ; 
 
                         d o u b l e   d 0   =   t h i s . _ f 0 ; 
 
                         d o u b l e   d 1   =   t h i s . _ f 1 ; 
 
 
 
                         p x   =   i x   -   d 0 ; 
 
                         p y   =   i y   -   d 1 ; 
 
                         p   =   t h i s . _ f 2   /   1 0 0 0 0 0 0 0 0 . 0 ; 
 
                         z 0 2   =   p x   *   p x   +   p y   *   p y ; 
 
                         q   =   z 0   =   M a t h . S q r t ( z 0 2 ) ; / /   O p t i m i z e / / q   =   z 0   =   M a t h . s q r t ( p x * p x +   p y * p y ) ; 
 
 
 
                         f o r   ( i n t   i   =   1 ;   ;   i + + ) 
 
                         { 
 
                                 i f   ( z 0   ! =   0 . 0 ) 
 
                                 { 
 
                                         / /   O p t i m i z e   o p t t m p _ 1 
 
                                         o p t t m p _ 1   =   p   *   z 0 2 ; 
 
                                         z   =   z 0   -   ( ( 1 . 0   -   o p t t m p _ 1 )   *   z 0   -   q )   /   ( 1 . 0   -   3 . 0   *   o p t t m p _ 1 ) ; 
 
                                         p x   =   p x   *   z   /   z 0 ; 
 
                                         p y   =   p y   *   z   /   z 0 ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         p x   =   0 . 0 ; 
 
                                         p y   =   0 . 0 ; 
 
                                         b r e a k ; 
 
                                 } 
 
                                 i f   ( i   = =   P D _ L O O P ) 
 
                                 { 
 
                                         b r e a k ; 
 
                                 } 
 
                                 z 0 2   =   p x   *   p x   +   p y   *   p y ; 
 
                                 z 0   =   M a t h . S q r t ( z 0 2 ) ; / /   O p t i m i z e / / z 0   =   M a t h . s q r t ( p x * p x +   p y * p y ) ; 
 
                         } 
 
                         o _ v e c l i n e a r . x   =   p x   /   t h i s . _ f 3   +   d 0 ; 
 
                         o _ v e c l i n e a r . y   =   p y   /   t h i s . _ f 3   +   d 1 ; 
 
                         r e t u r n ; 
 
                 } 
 
         } 
 
 } 