














Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!















Assuming 푚 1 the measurement of 푡 1 in the device frame, 푚 2 the measurement of 푡 2 and 푚 3 =
푚 1 × 푚 2 we will have:
푡 1 = 0 푒푥 + 0 푒푦 + 1 푒푧 = 푒푧
′ 푥 +^ 푚^1 푦^ 푒
′ 푦 +^ 푚^1 푧^ 푒
′ 푧
′ 푥 +^ 푚^2 푦^ 푒
′ 푦 +^ 푚^2 푧
′ 푥 +^ 푚^3 푦^ 푒
′ 푦 +^ 푚^3 푧
From which we obtain the following matrixes:
− 1 = 푀
푇 → 표푟푖푒푛푡푎푡푖표푛 푚푎푡푟푖푥
퐸 푅퐷 = 푀
푇 퐷
3 Reference Frames, Orientations, Rotations
Earth-centered inertial (ECI) coordinate frames have the origins at the center of mass of
the earth and are fixed with respect to the stars (do not change with the rotation of the earth).
They use the vernal equinox as principal direction frames and the true north to define another
direction. ECI are useful to specify positions of objects in the sky.
Earth-centered earth-fixed (ECEF) is a geocentric cartesian coordinate system, that co-
rotates with the earth. The X-Y plane coincides with the equatorial plane; the Z-axis is
orthogonal to this plane points in the direction fo the North Pole. The X axis passes through
a reference meridian.
Tangent local reference frame (horizontal frame), is defined using the plane tangent to
the Earth’s surface at a specific location on the earth, where Z points up to the sky, Y points
towards the true North and X points towards East. This convention is called North-East-Up
(NEU). The tangent plane defines the horizon at that point and it is also called ground plane
(far line).
The Device Reference Frame is a right-handed orthonormal frame with the origin at the
middle of the screen with the Y axis pointing towards the top of the screen, X to the right and
Z out of the screen. So the screen lies in the XY plane and Z exits from the screen. We will
have that 푍 = 푋 × 푌 , 푋 = 푌 × 푍 and 푌 = 푍 × 푋. The reference frame of the device is fixed and
attached to the device as the device is rotated. A device uses a local frame where the Y axis
points towards the magnetic north.
Orientation means define the relationship between world and device frames. The orientation
of the device can be expressed by the directin cosines of its reference frame {푒
′ 푋
′ 푌
′ 푍
} w.r.t. the
earth reference frame {푒푋 , 푒푌 , 푒푍 }. Each axis has 3 direction cosines hence we need 9 information
that are conveniently organized as matrix R called Rotation Matrix or Orientation Matrix.
′ 푥 ·^ 푒푥^ 푒
′ 푦 ·^ 푒푥^ 푒
′ 푧 ·^ 푒푥
푒
′ 푥 ·^ 푒푦^ 푒
′ 푦 ·^ 푒푦^ 푒
′ 푧 ·^ 푒푦
푒
′ 푥 ·^ 푒푧^ 푒
′ 푦 ·^ 푒푧^ 푒
′ 푧 ·^ 푒푧
R has only three independent parameters. We need only 3 independent cosines to determine
R they are called Euler angles. The transpose of R is the orientation of the earth w.r.t. to the
mobile frame, the inverse of R is equal to the transpose of R.
The coordinates 푣퐸 of a vector v in the fixed frame are 푅푣퐷 where 푣퐷 are the coordinates of v
in the mobile frame.
Assuming
퐴 푅퐵 the orientation of a frame B respect to a frame A we can say that having three
frames (A,B,C) we can have
퐴 푅퐶 =
퐴 푅퐵
퐵 푅퐶.
Yaw angle is when the device is flat and aligned to the earth reference frame so Ψ define the
rotation around the z axis, and is the angle between the device y axis and the geographic north
(rotate to right is positive):
where c represents the cosine and s the sine.
Pitch angle is when we rotate the device around the x axis (rotate the top edge up is a negative
rotation):
Roll angle is when we rotate the device around the y axis (roll to right is a negative rotation):
To compose orientation we need to multiply various orientation matrixes.
Direct problem 푅 = 푅푧 푅푥 푅푦.
Inverse problem is to obtain from R 푦푎푤 = 푎푡푎푛 2 (푟 12 , 푟 22 ), 푝푖푡푐ℎ = 푎푠푖푛(−푟 32 ) and 푟표푙푙 =
푎푡푎푛 2 (−푟 31 , 푟 33 ).
4 Android Framework
Android is always little-endian.
Pixels are the smallest manageable picture element, the physical size of a pixel:
Virtual size of pixel → Density-independent pixel (dp). In android 1 dp is equal to 1
physical pixel in a 160 dpi screen. As example the physical length of a 411 dp screen is
another one. Syntactically it looks like sequential code, but it is not sequential. To run code
outside the main thread, Kotlin coroutines perform work on either the Default or IO dispatcher.
Coroutines follow a principle of structured concurrency which means that new coroutines
can be only launched in a specific coroutine scope which delimits the lifetime of the coroutine
(GlobalScope means that a coroutine dies when the application ends).
5 Coroutines
Kotlin simplifies asynchronous programming by coroutines:
Listing 1: Coroutine introduction
GlobalScope. launch {} // launches the code into a thread Global scope
// means that the thread ends as soon as the main thread finishes
GlobalScope. launch ( D i s p a t c h e r s. Main ){
Log. i (TAG, " GlobalScope :" + Thread. c u r r e n t T h r e a d ( ). name )
} // dispatcher allow to specify which thread will host the coroutine
GlobalScope. launch ( newSingleThreadContext ( "my" ) ) {
Log. i (TAG, " GlobalScope :" + Thread. c u r r e n t T h r e a d ( ). name )
} //a new thread can be created
Coroutines can be moved from one thread to another, this is useful for example to update the
UI:
Listing 2: Coroutine thread change
GlobalScope. launch ( D i s p a t c h e r s. IO ){
// make a long network call
Log. i (TAG, " GlobalScopeIO :" + Thread. c u r r e n t T h r e a d ( ). name )
withContext ( D i s p a t c h e r s. Main ){
Log. i (TAG, " GlobalScopeMAIN :"
Using coroutines allow to do series tasks optimizing performance as example if we use with-
Context we are delegating another thread to do work while the main thread keep working (not
blocked):
Listing 3: Coroutine and asynch
GlobalScope. launch { // from main I launch a coroutine
val c1 = async { makeCall ( 0 ) } // from coroutine launch in
// parallel makeCall
withContext ( Main ) { // switch to main thread tu update UI
btn. t e x t = c1. await ( ) // when makeCall ends
btn. setBackgroundColor ( Color .MAGENTA)
It may happen that we need to work with API of some sites:
Listing 4: Coroutine and asynch
var name="API LINK"+ "API KEY GIVEN BY SITE"
val u r l = URL( name )
val conn = u r l. openConnection ( ) as HttpURLConnection
try {
conn. run {
requestMethod="GET" //or PUT
val r = JSONObject ( InputStreamReader ( inputStream )
. readText ( ) )
var r e s = ""
Log. i ( "info" , r. t o S t r i n g ( ) )
r e s = ( ( r. get ( "weather" ) as JSONArray ) [ 0 ] as
JSONObject ). get ( " description " ) as String
// json file contains an element weather that is an
// array with one element , we pick it and from it we
// pick description from the dictionary in the array
return r e s
}
6 Homogeneous Coordinate and Transformations
Many apps need 2D Graphics, for this reason we need to have a mathematical model of the
screen and of the tools available for drawing.
User screen is a discrete matrix of pixels, however it is modelled as a region of a 2D Euclidian
space.
Pixels are replaced by geometric points represented through homogeneous coordinates, and
changes of shapes can be represented as linear transformations.
We use homogeneous coordinates because we can use algebraic rule for all points as linear
mapping (e.g. using a matrix), because we can use algebraic rule for projective mapping
(perspective division) and because it is possible to specify the origin point of a reference frame.
It is also possible to distinguish point that is a geometric entity that presents the physical
position in the space and vector that represents a direction in the space.
′ = 푇 푃 ⇒
′ to both negative x and y):
− 1 = 퐼
− 1 푃
′ = 푇
− 1 푇 푃 ⇒ 푃 = 푇
− 1 푃
′ ⇒
2D translations composition, more than a single translation:
′ = 푇 1 푇 2 푃 = 푇 2 푇 1 푃
′ 푥
푝푦 + 푡푦 + 푡
′ 푦
1
′ 푥
0 1 푡
′ 푦
0 0 1
Translation of a Square, a square is composed by 4 points so we need to translate each of
them in the new position using translation matrix:
′ 1 =^ 푇 푃^1
푃
′ 2
′ 3 =^ 푇 푃^3
푃
′ 4 =^ 푇 푃^4
2D transformations, reflection or mirror, in case we have a square in the first quadrant:
of reflection:
2D transformations, rotations:
푇 (Θ)
− 1 (Θ) = 푅
푇 (Θ)
2D transformations, composition of rotations:
2D transformations, isometries:
The isometry is the set of Translations, Reflections and Rotations, transformations denoted as
ISO(2) or E(2).
Algebraically it is a closed non-commutative group.
Reflections alone is not closed:
Apply a rotation first followed by a translation and then a mirror.
The set rotations (with no reflections) is called the Special Orthogonal group, SO(2).
The isometry (without reflections) is called the Special Euclidian group SE(2) or rigid body
transformations. 3DOF(푡푥 , 푡푦, 휃)
Composition of translations and rotations:
We will use isometries in:
the movement or change of position of a 2D object that is only allowed to move in a 2D
space.
2D transformations, similarities: a pure similarity changes the size of an object, not the
shape (in general combines isometries with scaling, length not preserved), 4DOF(푠, 푡푥 , 푡푦, 휃):
푠 cos Θ −푠 sin Θ 푡푥
푠 sin Θ 푠 cos Θ 푡푦
0 0 1
Coordinate change:
this matrix maps [− 1 , 1 ] × [− 1 , 1 ] 푡표 [ 0 , 푤] × [ 0 , ℎ].
2D transformations, affine: change the scales of an object not uniformly, hence it changes
the shape (i.e. angles), they preserve the ratio of lengths of two parallel segments, reflections
are non uniform scaling where 푠푥 , 푠푦 = −1, 6DOF(푠푥 , 푠푥푦, 푠푦, 푠푦푥 , 푡푦, 푡푥 ):
From the skew matrix we can obtain the shear matrix by putting 푠푥푦 = 0 (along y) or the skew
matrix by putting 푠푦푥 = 0 (along x), if none is equal to zero we obatin both transformations.
We have three different groups:
Any affine transformation M can be decomposed as 푀 = 푇 (푡 1 , 푡 2 )푅(휙)푅
− 1 (휃)푆(휆 1 , 휆 2 )푅(휃), this
is an application of the Singular Value Decomposition (SVD) theorem.
Projective transformations: have the last row with not zeros, the last element is one so we
have 8 independent coefficients to chose, send a point to a point not belonging to 푤 = 1.
To find the matrix from point with no translation we just have to do 푀 푃 = 푃
′ → 푀 푃푃
푃푃
− 1 → 푀 = 푃푃
− 1 we can use the same methods if we didn’t use perspective. If we use
perspective we can compute M by knowing how 4 points are mapped by M.
3D homogeneous coordinates work as for 2D but in this case we will have 푥, 푦, 푧, 푤.
7 Graphics 2D
To draw in kotlin we have to define a bitmap area (matrix of pixels), and a canvas area that
use bitmap as drawer area:
Listing 5: Basic graphics
l a t e i n t var p a i n t : Paint
l a t e i n t var canvas : Canvas
val bitmap = Bitmap. createBitmap ( width , h e i g h t ,
Bitmap. C o n f i g. ARGB 8888 )
canvas = Canvas ( bitmap )
p a i n t = Paint ( Paint. ANTI ALIAS FLAG)
p a i n t. apply {
s e t C o l o r ( Color .WHITE)
strokeWidth = 20 f
t e x t S i z e = 200 f
}
draw ( )
fun draw ( ) {
...
// w = width , h = height
// point draw
p a i n t. s t y l e = Paint. S t y l e .STROKE
canvas. drawPoint (w/2 f , h/2 f , p a i n t )
// line draw
p a i n t. s t y l e = Paint. S t y l e .STROKE
canvas. drawLine (w/3 f , h/3 f , 2 ∗w/3 f , 2 ∗h/3 f , p a i n t )
// draw a 12 sides figure
val s i d e s = 12
val rad = 600
val path = Path ( )
val a n g l e = 2. 0 ∗ Math. PI / s i d e s
val cx = w/2 f
val cy = h/2 f
path. moveTo (
cx + ( rad ∗ Math. c o s ( 0. 0 ) ). t o F l o a t ( ) ,
cy + ( rad ∗ Math. s i n ( 0. 0 ) ). t o F l o a t ( )
)
for ( i in 1 u n t i l s i d e s ) {
path. l i n e T o (
cx + ( rad ∗ Math. c o s ( a n g l e ∗ i ) ). t o F l o a t ( ) ,
cy + ( rad ∗ Math. s i n ( a n g l e ∗ i ) ). t o F l o a t ( )
)
path. c l o s e ( )
(w−image. width )/2 f ,
( h−image. h e i g h t )/2 f ,
null )}
}
i f ( i==max) t h i s. c a n c e l ( )
}
}
val t i m e r = Timer ( "mover" , true )
t i m e r. scheduleAtFixedRate ( t t , 0 , deltaT )
// draw a triangle
p a i n t. s t y l e=Paint. S t y l e .STROKE
val v e r t e x = f l o a t A r r a y O f (0 f , 0 f , 0 f ,w+0f ,w/2 f , h/2 f )
val c o l o r s = intArrayOf ( Color .BLUE, Color .RED, Color .YELLOW)
canvas. w i t h T r a n s l a t i o n ( 0 f , 0 f ) { d r a w V e r t i c e s (
Canvas. VertexMode. TRIANGLES,
v e r t e x. s i z e , v e r t e x , 0 ,
null , 0 ,
c o l o r s , 0 , null , 0 , 0 , p a i n t )}
}
8 Custom View
It is possible to create a custom view that periodically scroll horizontally an image to build a
dynamic background:
Listing 6: Custom Background
//in on create
val view = MyView( t h i s )
setContentView ( view )
img = BitmapFactory. decodeStream ( t h i s. a s s e t s. open ( "forest.jpg" ) )
val p o i n t = Point ( )
// get size of the screen
windowManager. d e f a u l t D i s p l a y. g e t S i z e ( p o i n t )
img=Bitmap. c r e a t e S c a l e d B i t m a p ( img , 2 ∗ p o i n t. x , ( 0. 5 f ∗( p o i n t. y ) ). t o I n t ( )
, f a l s e )
...
var i =
inner c l a s s MyView( c o n t e x t : Context ) : View ( c o n t e x t ){
override fun onDraw ( canvas : Canvas ) {
super. onDraw ( canvas )
canvas. w i t h T r a n s l a t i o n (0 f ) {
drawRGB ( 0 , 0 , 1 0 0 )
drawBitmap ( img , i +0f , 0 f , null )
}
i =( i −3)%canvas. width
// remove old drawed image and call again
// onDraw , showing new image with ’i’
// changed
i n v a l i d a t e ( )
}
9 Cannon Game
The general structure of a game/application that update canvas is very simple, our custom view
should have some function that over time/action update values used in the onDraw function
to re-display changes.
10 VRCOMPASS
Sensors are used in the following way:
Listing 7: Overview on sensors
l a t e i n t var sensorManager : SensorManager
override fun onCreate ( s a v e d I n s t a n c e S t a t e : Bundle? ) {
super. onCreate ( s a v e d I n s t a n c e S t a t e )
sensorManager = g e t S y s t e m S e r v i c e ( Context. SENSOR SERVICE) as
SensorManager
}
override fun o n S t a r t ( ) {
super. o n S t a r t ( )
sensorManager. r e g i s t e r L i s t e n e r ( myView , //or this (listener)
sensorManager. g e t D e f a u l t S e n s o r ( S e n s o r .TYPE ROTATION VECTOR) ,
//or *. TYPE_ACCELEROMETER or *. TYPE_MAGNETIC_FIELD
SensorManager .SENSOR DELAY NORMAL //*. SENSOR_DELAY_GAME
inner c l a s s MyView( c o n t e x t : Context ) : View ( c o n t e x t , null ) ,
S e n s o r E v e n t L i s t e n e r {
var mRotationVector = FloatArray ( 3 )
var mRotMatrix = FloatArray ( 1 6 )
...
override fun onSensorChanged ( e v e n t : SensorEvent ){
i f ( e v e n t. s e n s o r?. type== S e n s o r .TYPE ROTATION VECTOR){
In the pinhole camera all points are on focus, so the FOV depends on the distance 푑 of
the scene from the pinhole, 퐹푂푉 = 2 푑 푡푎푛(휙/ 2 ) = 2 푑 ℎ/ 2 푓 = 푑 ℎ/ 푓.
The World Reference frame is the frame used to express the position of the camera (or the
position of all the shapes in a scene). The position of the camera position is described with a
rigid body transformation. The camera can be moved, for example translated from a to b or
rotated. The position is described via the model-view matrix.
The Vanish Point (VP) is the point as 휆 → ∞, for which 푉 푃푥 = 푓 푑푥 /푑푧 and 푉 푃푦 = 푓 푑푦/푑푧.
All the lines with the same direction have the same vanishing point, in fact a vanishing point
represents the image point of a direction. Two parallell lines in the scene meets at infinity.
Once a point is projected we must still transform the resulting coordinates according to the
screen coordinates. This is a transformation called as Camera Matrix K. In general the focal
lengths can differ, and camera may introduce a skew (deformation), hence:
Meaning of the orientation matrix 푅: The coordinates 푣퐸 of a vector 푣 in the fixed frame are
푣퐸 = 푅푣퐷 where 푣퐷 are the coordinates of 푣 in the mobile frame. Meaning of 푅
− 1 = 푅
푇 is
푇 푣퐸. The columns of 푅 are the coordinates of the device frame respect to the earth.
The rows of 푅 are the coordinates of the earth frame axis with respect to the device frame.
12 CORE COMPONENTS
A content provider is a repository of informaton that share information among api, there is full
control of read/write access, there is a uniform access interface (can be implemented on a DB
or file), an example of content provider is calendar.
A content provider can be accessed launching an application that manages the provider, this
allows any app to access the content using an Intent targeting the provider.
Listing 9: Example of an Intent
//
val i = I n t e n t ( Alarm. Clock. ACTION SET ALARM)
s t a r t A c t i v i t y ( i )
Listing 10: Another example of an Intent
Calendar beginTime = Calendar. g e t I n s t a n c e ( ) ;
begintTime. set (2018 , 1 , 31 , 9 , 3 0 ) ; // ymd hm
Calendar endTime = Calendar. g e t I n s t a n c e ( ) ;
endTime. set (2018 , 1 , 31 , 15 , 0 ) ;
I n t e n t i n t e n t = new I n t e n t ( I n t e n t. ACTION INSERT ).
setData ( CalendarContr a c t. Events .CONTENT URI ).
putExtra ( CalendarCon t ra c t. EXTRA EVENT BEGIN TIME, beginTime.
g e t T i m e I n M i l l i s ( ) ).
putExtra ( CalendarCon t ra c t. EXTRA EVENT END TIME, endTime.
g e t T i m e I n M i l l i s ( ) ).
putExtra ( CalendarCon t ra c t. Events. TITLE , "MACC EXAM" ).
putExtra ( CalendarCon t ra c t. Events .EVENT LOCATION, "Room 41,
Via Eudossiana" ) ;
s t a r t A c t i v i t y ( i n t e n t ) ;
Example of an Intent with a result:
Listing 11: Intent with a result
@Override
protected v o i d onCreate ( Bundle s a v e d I n s t a n c e S t a t e ){
super. onCreate ( s a v e d I n s t a n c e S t a t e ) ;
setContentView (R. l a y o u t. a c t i v i t y m a i n ) ;
}
private v o i d t a k e P i c t u r e ( ) {
I n t e n t t a k e P i c t u r e I n t e n t = new I n t e n t ( MediaStore.
ACTION IMAGE CAPUTRE ) ;
i f ( t a k e P i c t u r e I n t e n t. r e s o l v e A c t i v i t y ( getPackageManager ( ) )
!= null ){
s t a r t A c t i v i t y F o r R e s u l t ( t a k e P i c t u r e I n t e n t ,
@Override
protected v o i d o n A c t i v i t y R e s u l t ( i n t requestCode , i n t r e s u l t C o d e ,
I n t e n t data ){
i f ( requestCode == REQUEST IMAGE CAPTURE && r e s u l t C o d e ==
RESULT OK){
Bundle e x t r a s = data. g e t E x t r a s ( ) ;
Bitmap imageBitmap = ( Bitmap ) e x t r a s. get ( "data" )
imageView. setImageBitmap ( imageBitmap ) ;
}
A Broadcast receiver it is a software component that reacts to system-wide events fired as
broadcast intents. A receiver has to register to specific broadcast intents. When the intent
occurs the onReceive method in the receiver is executed.
A service is an application component that runs in background, not interacting with the user,
for an indefinite period of time. A service is a component that android is aware of with its own
lifecycle. For CPU intensive operations (such as networking) a service should spawn its own
thread in which to do that work.
Permissions restrict the access to a software component (the caller must hold the permission).
For activity, services, content providers a security exception is thrown, for broadcast receivers
no exception (intent is just not delivered). The permission levels are:
to a requesting application at installation). An example is ACCESS WIFI STATE.
with the same certificate as the application that declared the permission. An example is
MANAGE MEDIA.