<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7156959992050056410</id><updated>2024-10-06T21:45:53.072-07:00</updated><category term="C++"/><category term="C"/><category term="C#"/><category term="Java"/><category term="file"/><category term="return"/><category term="3D"/><category term="Android"/><category term="Blender"/><category term="COM"/><category term="Euler"/><category term="IDL"/><category term="Linux"/><category term="Math"/><category term="NDK"/><category term="TLBimp"/><category term="Taylor"/><category term="Visual Studio"/><category term="WPF"/><category term="add"/><category term="alloc"/><category term="big"/><category term="cake"/><category term="cd"/><category term="change"/><category term="command"/><category term="create"/><category term="deserialize"/><category term="destination"/><category term="directory"/><category term="exercise"/><category term="exp"/><category term="field"/><category term="files"/><category term="find"/><category term="github"/><category term="info"/><category term="interview"/><category term="job"/><category term="link"/><category term="ls"/><category term="managed"/><category term="memory"/><category term="move"/><category term="piece"/><category term="position"/><category term="project"/><category term="pwd"/><category term="question"/><category term="questions"/><category term="quiz"/><category term="remove"/><category term="rename"/><category term="serialize"/><category term="series"/><category term="side"/><category term="size"/><category term="solution"/><category term="source"/><category term="struct"/><category term="terminal"/><category term="view"/><title type='text'>Stay a while and listen !</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-3696552254191105486</id><published>2015-09-08T07:52:00.004-07:00</published><updated>2015-09-14T23:10:07.410-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Euler"/><category scheme="http://www.blogger.com/atom/ns#" term="exercise"/><category scheme="http://www.blogger.com/atom/ns#" term="exp"/><category scheme="http://www.blogger.com/atom/ns#" term="interview"/><category scheme="http://www.blogger.com/atom/ns#" term="job"/><category scheme="http://www.blogger.com/atom/ns#" term="position"/><category scheme="http://www.blogger.com/atom/ns#" term="question"/><category scheme="http://www.blogger.com/atom/ns#" term="questions"/><category scheme="http://www.blogger.com/atom/ns#" term="quiz"/><category scheme="http://www.blogger.com/atom/ns#" term="return"/><category scheme="http://www.blogger.com/atom/ns#" term="series"/><category scheme="http://www.blogger.com/atom/ns#" term="solution"/><category scheme="http://www.blogger.com/atom/ns#" term="Taylor"/><title type='text'>C++ Interview</title><content type='html'>Questions to apply for C++ position:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8; font-family: Courier New, Courier, monospace; font-size: large;&quot;&gt;1.&lt;/span&gt; &amp;nbsp;What will this method return ?&lt;br /&gt;
&lt;div style=&quot;background: #000000; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cdcd00;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #00cd00;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;getWhenToDoSomething()&lt;/span&gt;
  &lt;span style=&quot;color: #cccccc;&quot;&gt;{&lt;/span&gt;
     &lt;span style=&quot;color: #cccccc;&quot;&gt;string&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;aziTodo(&lt;/span&gt;&lt;span style=&quot;color: #cd0000;&quot;&gt;&quot;today&quot;&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;);&lt;/span&gt;
     &lt;span style=&quot;color: #cdcd00;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;aziTodo.c_str();&lt;/span&gt;
  &lt;span style=&quot;color: #cccccc;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #9fc5e8; font-family: Courier New, Courier, monospace; font-size: large;&quot;&gt;2.&lt;/span&gt;&amp;nbsp;&amp;nbsp;And this beautiful code, what will he print ?&lt;br /&gt;
&lt;div style=&quot;background: #000000; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; &lt;span style=&quot;color: #00cd00;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;ptr&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cdcd00;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #00cd00;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #cd00cd;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;];&lt;/span&gt;
 &lt;span style=&quot;color: #cccccc;&quot;&gt;strcpy(ptr,&lt;/span&gt; &lt;span style=&quot;color: #cd0000;&quot;&gt;&quot;12345&quot;&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;);&lt;/span&gt;
 &lt;span style=&quot;color: #00cd00;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;x&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #00cd00;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;)ptr;&lt;/span&gt;
 &lt;span style=&quot;color: #cccccc;&quot;&gt;x&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #cd00cd;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;;&lt;/span&gt;
 &lt;span style=&quot;color: #00cd00;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;ptr2&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #00cd00;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;)x;&lt;/span&gt;
 &lt;span style=&quot;color: #cccccc;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #3399cc;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;ptr2&lt;/span&gt; &lt;span style=&quot;color: #3399cc;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #3399cc;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #cccccc;&quot;&gt;endl;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;background: #202020; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;/pre&gt;
&amp;nbsp; Tip: after casting from char to int, increment step will be bigger&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;line-height: 21.25px;&quot;&gt;&lt;span style=&quot;background-color: #444444; font-family: inherit;&quot;&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;    Next&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;be&lt;/span&gt; &lt;span style=&quot;color: #ffd966;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;dragons&lt;/span&gt;. &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;They&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;they&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;will&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;ask&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;MATH.&lt;/span&gt; 
        &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;While&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;hard&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;stuff,&lt;/span&gt;
             &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;use&lt;/span&gt; &lt;span style=&quot;color: #447fcf; text-decoration: underline;&quot;&gt;Google&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;search&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;when&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;necessary.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #a2c4c9; font-family: Courier New, Courier, monospace; font-size: large;&quot;&gt;3.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Write a class for &lt;span style=&quot;color: #9fc5e8;&quot;&gt;Complex &lt;/span&gt;numbers.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;Also choose a solution for the fact that there can be two constructors :&lt;br /&gt;
&lt;div style=&quot;background: #000000; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;   &lt;span style=&quot;color: #cccccc;&quot;&gt;Complex(&lt;/span&gt;&lt;span style=&quot;color: #00cd00;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;realNumber,&lt;/span&gt; &lt;span style=&quot;color: #00cd00;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;imaginaryNumber)&lt;/span&gt;
   &lt;span style=&quot;color: #cccccc;&quot;&gt;Complex(&lt;/span&gt;&lt;span style=&quot;color: #00cd00;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;angle,&lt;/span&gt; &lt;span style=&quot;color: #00cd00;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #cccccc;&quot;&gt;absoluteValue)&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;background: #202020; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; both taking 2 double arguments as parameters,

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;what is the solution to take care of both constructors ?

&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #a2c4c9; font-family: Courier New, Courier, monospace; font-size: large;&quot;&gt;4.&lt;/span&gt;&amp;nbsp; &amp;nbsp;Write the method to return the Taylor exponential function for x.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; The same as the &lt;span style=&quot;color: #9fc5e8;&quot;&gt;std::exp(double x)&lt;/span&gt; function. Provide the code to calculate the sum, the Taylor series sum( 1+ x + ...) and do not call &lt;span style=&quot;color: #9fc5e8;&quot;&gt;std::exp&lt;/span&gt;(just maybe in assert to verify results).&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpe-VNQjn1Uga7Wv3VH54l2AKqUFkQCOv_wIB92ObnQQCDtx2ETaDugKFWju-kEWm_Lxnh9S3JQzPom7MwXj8VCRoa1j3gfEGOPgH8LUJNya-sBfBwnvsm2DLjeDluf2_vBmbjjAsiIJOF/s1600/exToEuler.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;1&quot; height=&quot;191&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpe-VNQjn1Uga7Wv3VH54l2AKqUFkQCOv_wIB92ObnQQCDtx2ETaDugKFWju-kEWm_Lxnh9S3JQzPom7MwXj8VCRoa1j3gfEGOPgH8LUJNya-sBfBwnvsm2DLjeDluf2_vBmbjjAsiIJOF/s320/exToEuler.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #a2c4c9; font-family: Courier New, Courier, monospace; font-size: large;&quot;&gt;5.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Final level:&lt;/span&gt;&lt;/b&gt;&amp;nbsp;Euler&#39;s formula.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;Use both solutions from exercise 3. and 4. to calculate &lt;span style=&quot;color: #ffe599;&quot;&gt;e&lt;/span&gt; to power &lt;span style=&quot;color: #ffe599;&quot;&gt;i*x&lt;/span&gt;.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;Assert that the result is equal to &lt;span style=&quot;color: #ffe599;&quot;&gt;cos(x) + i*sin(x)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMerNcgyKjrbpynH27kyixkI_qsJODGhzQPC7ctAVF7PSUGifwGGQitpAtKU6Te6O3KIHhJ8ayG7eEhHFBRDmwzLbw4j6d7yzzgSYOZUYN-CTU4TrI64oZYyBoJfO7MrZCqtzw9ivSdbQh/s1600/exCloseToEuler.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;1&quot; height=&quot;140&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMerNcgyKjrbpynH27kyixkI_qsJODGhzQPC7ctAVF7PSUGifwGGQitpAtKU6Te6O3KIHhJ8ayG7eEhHFBRDmwzLbw4j6d7yzzgSYOZUYN-CTU4TrI64oZYyBoJfO7MrZCqtzw9ivSdbQh/s400/exCloseToEuler.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;font-size: large;&quot;&gt;5&lt;/span&gt; levels done,&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;next act will touch the map,&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Bogdan Nicolae Visoiu&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: #ffe599; font-size: large;&quot;&gt;GL HF&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/3696552254191105486/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2015/09/c-interview.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/3696552254191105486'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/3696552254191105486'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2015/09/c-interview.html' title='C++ Interview'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpe-VNQjn1Uga7Wv3VH54l2AKqUFkQCOv_wIB92ObnQQCDtx2ETaDugKFWju-kEWm_Lxnh9S3JQzPom7MwXj8VCRoa1j3gfEGOPgH8LUJNya-sBfBwnvsm2DLjeDluf2_vBmbjjAsiIJOF/s72-c/exToEuler.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-3381669914210416554</id><published>2015-07-12T22:56:00.002-07:00</published><updated>2015-09-07T23:23:43.616-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="big"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="cake"/><category scheme="http://www.blogger.com/atom/ns#" term="find"/><category scheme="http://www.blogger.com/atom/ns#" term="piece"/><category scheme="http://www.blogger.com/atom/ns#" term="side"/><title type='text'>Cake cuts</title><content type='html'>You are presented with a rectangular cake whose sides are of length X and Y.&lt;br /&gt;
The cake has been cut into (N + 1) pieces by making N&lt;br /&gt;
&amp;nbsp;straight cuts along the first side and N straight cuts along the second side.&lt;br /&gt;
&lt;br /&gt;
The cuts are represented by two non-empty zero-indexed arrays A and B&lt;br /&gt;
&amp;nbsp;consisting of N integers. More precisely, A[i] such that 0 ≤ i &amp;lt; N&lt;br /&gt;
&amp;nbsp;represents the position of a cut along the first side, and B[j] such that&lt;br /&gt;
&amp;nbsp;0 ≤ j &amp;lt; N represents the position of a cut along the second side.&lt;br /&gt;
We could also make a different number of cuts on the second side but for now the number of cuts is equal on both sides.&lt;br /&gt;
&lt;br /&gt;
The goal is to find the K-th piece of cake in order of size, starting with&lt;br /&gt;
&amp;nbsp;the largest piece first. We will consider the size of a piece to be its area.&lt;br /&gt;
&lt;br /&gt;
N is an integer within the range [1..40,000];&lt;br /&gt;
X and Y are integers within the range [2..400,000,000];&lt;br /&gt;
K is an integer within the range [1..(N+1)*(N+1)];&lt;br /&gt;
each element of array A is an integer within the range [1..X−1];&lt;br /&gt;
each element of array B is an integer within the range [1..Y−1];&lt;br /&gt;
A[i − 1] &amp;lt; A[i] and B[i − 1] &amp;lt; B[i], for every i such that 0 &amp;lt; i&amp;lt; N;&lt;br /&gt;
1 ≤ A[i] − A[i − 1], B[i] − B[i − 1] ≤ 10,000, for every i such that 0 &amp;lt; i &amp;lt; N;&lt;br /&gt;
1 ≤ A[0], B[0], X − A[N − 1], Y − B[N − 1] ≤ 10,000&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/Pzkgw/4f70d855ae0863cd0eda.js&quot;&gt;&lt;/script&gt;

&amp;nbsp; &amp;nbsp;- first the biggest piece is searched, then the second biggest piece .. all is done K times, and when the K iteration is reached, the size of the biggest piece of cake that was found, is returned</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/3381669914210416554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2015/07/cake-cuts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/3381669914210416554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/3381669914210416554'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2015/07/cake-cuts.html' title='Cake cuts'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-5875321760618006086</id><published>2014-09-07T07:02:00.000-07:00</published><updated>2014-09-07T07:02:33.319-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cd"/><category scheme="http://www.blogger.com/atom/ns#" term="change"/><category scheme="http://www.blogger.com/atom/ns#" term="command"/><category scheme="http://www.blogger.com/atom/ns#" term="create"/><category scheme="http://www.blogger.com/atom/ns#" term="destination"/><category scheme="http://www.blogger.com/atom/ns#" term="directory"/><category scheme="http://www.blogger.com/atom/ns#" term="file"/><category scheme="http://www.blogger.com/atom/ns#" term="info"/><category scheme="http://www.blogger.com/atom/ns#" term="link"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="ls"/><category scheme="http://www.blogger.com/atom/ns#" term="move"/><category scheme="http://www.blogger.com/atom/ns#" term="pwd"/><category scheme="http://www.blogger.com/atom/ns#" term="remove"/><category scheme="http://www.blogger.com/atom/ns#" term="rename"/><category scheme="http://www.blogger.com/atom/ns#" term="return"/><category scheme="http://www.blogger.com/atom/ns#" term="source"/><category scheme="http://www.blogger.com/atom/ns#" term="terminal"/><category scheme="http://www.blogger.com/atom/ns#" term="view"/><title type='text'>Linux File Commands</title><content type='html'>&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #e06666;&quot;&gt;pwd&lt;/span&gt;&lt;/b&gt;- Output the current directory that you are in&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;ls&lt;/span&gt;&lt;/b&gt; - list the content of the current directory&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;ls -a&lt;/span&gt;&lt;/b&gt; - list all the content, including hidden files&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: yellow;&quot;&gt;ls -l&lt;/span&gt;&lt;/b&gt; - list the content and its information&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #d9ead3;&quot;&gt;&lt;b&gt;cd someFolder&lt;/b&gt; &lt;/span&gt;– change directory to&amp;nbsp;someFolder&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #d9ead3;&quot;&gt;cd ..&lt;/span&gt;&lt;/b&gt; - Go up a directory&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #d9ead3;&quot;&gt;cd -&lt;/span&gt;&lt;/b&gt; - Return to the previous directory&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #8e7cc3;&quot;&gt;cp source destination&lt;/span&gt;&lt;span style=&quot;color: orange;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;– Copy sourceto destination&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #8e7cc3;&quot;&gt;cp -r source destination&lt;/span&gt;&lt;/b&gt; – Copy a folder recursively from source to destination&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #674ea7;&quot;&gt;mv source destination&lt;/span&gt;&amp;nbsp;&lt;/b&gt;- Move (or rename) a file from source to destination&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: orange;&quot;&gt;rm someFile&lt;/span&gt;&lt;/b&gt;- Remove&amp;nbsp;someFile&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: orange;&quot;&gt;rm -f someFile&amp;nbsp;&lt;/span&gt;&lt;/b&gt;- Remove&amp;nbsp;someFile&amp;nbsp;without prompt&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: orange;&quot;&gt;rm -r folder&lt;/span&gt;&amp;nbsp;&lt;/b&gt;- Remove a folder and its content recursively&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #ffd966;&quot;&gt;mkdir foldername&lt;/span&gt;&lt;/b&gt; – Create a new folder foldername&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #f1c232;&quot;&gt;rmdir foldername&lt;/span&gt; &lt;/b&gt;– Remove an empty folder&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #cfe2f3;&quot;&gt;file someFile&lt;/span&gt;&lt;/b&gt;&amp;nbsp;– Show the file type of&amp;nbsp;someFile&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #cfe2f3;&quot;&gt;cat file1 file2&lt;/span&gt;&amp;nbsp;&lt;/b&gt;– Concatenate file1to file 2&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #9fc5e8;&quot;&gt;cat &amp;gt; someFile&amp;nbsp;&lt;/span&gt;&lt;/b&gt;– Concatenate standard input to&amp;nbsp;someFile&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #9fc5e8;&quot;&gt;less someFile&amp;nbsp;&lt;/span&gt;&lt;/b&gt;- View and paginate&amp;nbsp;someFile&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #9fc5e8;&quot;&gt;head someFile&lt;/span&gt;&lt;/b&gt;&amp;nbsp;- Show first 10 lines of someFile&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;tail someFile&lt;/span&gt;&lt;/b&gt; - Show last 10 lines of someFile&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #6fa8dc;&quot;&gt;&lt;b&gt;chmod unknown file&lt;/b&gt; &lt;/span&gt;- Change file permission of file to unknown&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #6fa8dc;&quot;&gt;&lt;b&gt;chown user:group file&lt;/b&gt; &lt;/span&gt;- Change ownership of fileto userand group group&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;ln -s source destination&lt;/span&gt;&lt;/b&gt; – Create a link from source to destination&lt;/li&gt;
&lt;/ul&gt;
</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/5875321760618006086/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2014/09/linux-file-commands.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/5875321760618006086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/5875321760618006086'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2014/09/linux-file-commands.html' title='Linux File Commands'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-1241563230674519451</id><published>2014-01-21T06:53:00.000-08:00</published><updated>2015-09-07T23:43:44.505-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="add"/><category scheme="http://www.blogger.com/atom/ns#" term="file"/><category scheme="http://www.blogger.com/atom/ns#" term="files"/><category scheme="http://www.blogger.com/atom/ns#" term="github"/><category scheme="http://www.blogger.com/atom/ns#" term="project"/><title type='text'>Github - Add files</title><content type='html'>&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&lt;/span&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #202020; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;    &lt;span style=&quot;color: #d0d0d0&quot;&gt;For&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;file&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;safety&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;good&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;place&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;sharing,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;GitHub&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;is&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;place&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;your&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;project&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;should&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;be.&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Either&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;or&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;.&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0&quot;&gt;Project&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;are&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;located&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;on&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;your&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;computer&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;you&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;want&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;them&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;github,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;how&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;do&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;it&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;:&lt;/span&gt;

 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;register,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;login&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;github&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;site&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;on&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;site,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;use&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Create&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Repository&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;button&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;one&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;your&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;project&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;after&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;repository&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;was&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;created&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;you&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;need&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;copy&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;somewhere&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt;  &lt;span style=&quot;color: #d0d0d0&quot;&gt;www&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;address&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;of&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;your&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;repository.&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Something&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;like&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;https:&lt;/span&gt;&lt;span style=&quot;color: #999999; font-style: italic&quot;&gt;//github.com/User_Name/Repository_Name.git&lt;/span&gt;

&lt;span style=&quot;color: #d0d0d0&quot;&gt;Windows:&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;download&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;windows&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;application&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;GitHub&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Windows&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;commit&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #447fcf; text-decoration: underline&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;downloaded&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;application&lt;/span&gt;

&lt;span style=&quot;color: #d0d0d0&quot;&gt;Linux:&lt;/span&gt;  
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #447fcf; text-decoration: underline&quot;&gt;terminal&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;:&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;go&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color: #a61717; background-color: #e3d2d2&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;s&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;directory&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;cd&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the_project&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;git&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;init&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;start&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;git&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;all&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;local&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;list&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;git&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;*&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;commit&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;git&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;commit&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;-m&lt;/span&gt; &lt;span style=&quot;color: #ed9d13&quot;&gt;&amp;quot;commit&amp;quot;&lt;/span&gt; 
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;tell&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;your&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;computer&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;where&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;the&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; 
 &lt;span style=&quot;color: #d0d0d0&quot;&gt;git&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;remote&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;origin&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;https:&lt;/span&gt;&lt;span style=&quot;color: #999999; font-style: italic&quot;&gt;//github.com/User/RepName.git&lt;/span&gt;
              &lt;span style=&quot;color: #d0d0d0&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;add&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;to&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;github&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;git&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;push&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;origin&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;master&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;span style=&quot;font-size: 21.600000381469727px;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/1241563230674519451/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2014/01/github-add-files.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/1241563230674519451'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/1241563230674519451'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2014/01/github-add-files.html' title='Github - Add files'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-4700476130132753092</id><published>2013-04-20T06:09:00.000-07:00</published><updated>2013-04-20T07:38:08.340-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="WPF"/><title type='text'>Shader effects in .NET</title><content type='html'>&lt;div class=&quot;western&quot;&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Starting with .NET 3.5 SP1 hardware accelerated effects can imbue your WPF applications with superpowers.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;You assign shaders to any UIElement&amp;nbsp;via the Effect property.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;The way to do that is &lt;span style=&quot;color: #6aa84f;&quot;&gt;myButton.Effect = myEffect&lt;/span&gt;, where:&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;myEffect can be any object derived from System.Windows.Media.Effects&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;it can be &lt;span style=&quot;color: #6aa84f;&quot;&gt;BlurEffect, DropShadowEffect or ShaderEffect&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;first 2 are build-in shaders and you can use them straight away(MyButton.Effect = new BlurEffect() { ... };) and ShaderEffect is the fun and customisable one&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;u&gt;do not use BitmapEffect &lt;/u&gt;or any derived classes as it is obsolete and badly designed&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;a ShaderEffect has 2 parts : a shader written in HLSL(a text file that contains some HLSL code and has the .fx extension) and a C# wrapper (.cs class file) that will allow you to use the shader in your .NET application&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;i&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;I provide full code example for a wpf application that does color&amp;nbsp;manipulation&amp;nbsp;(RGB gain, brightness, saturation, contrast, gamma ) at&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://github.com/Pzkgw/WpfColorManipulationShader&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;https://github.com/Pzkgw/WpfColorManipulationShader&lt;/a&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;. Please feel free to use it.&lt;/span&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;/div&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;To create a ShaderEffect :&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;compile the .fx text file containing the shader code and obtain a binary .px file&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;write the C# wrapper&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;To use a ShaderEffect:&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;the C# wrapper will define what properties are connected to the shader&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;a property is connected to a certain register and the shader will use the same register. Registers used for sharing constants start with C letter (eg: C0,C1 ..), and the ones used for sharing images(samplers) start with S. The first one available is S1, as S0 will be reserved for the input image.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;in the following example both the shader and the wrapper will use the saturation information from the C0 register&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div .6em=&quot;&quot; .8em=&quot;&quot; border-width:.1em=&quot;&quot; border:solid=&quot;&quot; gray=&quot;&quot; overflow:auto=&quot;&quot; padding:.2em=&quot;&quot; style=&quot;background: #000000;&quot; width:auto=&quot;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;        &lt;span style=&quot;color: green;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;readonly&lt;/span&gt; DependencyProperty SaturationProperty =
            DependencyProperty.Register(&quot;Saturation&quot;, &lt;span style=&quot;color: green;&quot;&gt;typeof&lt;/span&gt;(&lt;span style=&quot;color: #303090;&quot;&gt;double&lt;/span&gt;), &lt;span style=&quot;color: green;&quot;&gt;typeof&lt;/span&gt;(ColorManiPulatorEffect),
            &lt;span style=&quot;color: green;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #0060b0;&quot;&gt;UIPropertyMetadata&lt;/span&gt;(&lt;span style=&quot;color: #6000e0;&quot;&gt;1.0&lt;/span&gt;, PixelShaderConstantCallback(&lt;span style=&quot;color: #6000e0;&quot;&gt;0&lt;/span&gt;))&lt;span style=&quot;color: grey;&quot;&gt;/* assigned to sampler register C0 */&lt;/span&gt;);

        &lt;span style=&quot;color: #303090;&quot;&gt;double&lt;/span&gt; Saturation
        {
            &lt;span style=&quot;color: green;&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: green;&quot;&gt;return&lt;/span&gt; (&lt;span style=&quot;color: #303090;&quot;&gt;double&lt;/span&gt;)GetValue(SaturationProperty); }
            &lt;span style=&quot;color: green;&quot;&gt;set&lt;/span&gt; { SetValue(SaturationProperty, &lt;span style=&quot;color: green;&quot;&gt;value&lt;/span&gt;); }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;in case you need to send a image or a LUT to the shader :&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- create a WritableBitmap&lt;/span&gt;&lt;/div&gt;
&lt;span id=&quot;goog_464414557&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;western&quot;&gt;
&lt;div .1em=&quot;&quot; .6em=&quot;&quot; .8em=&quot;&quot; border-width:.1em=&quot;&quot; border:solid=&quot;&quot; gray=&quot;&quot; overflow:auto=&quot;&quot; padding:.2em=&quot;&quot; width:auto=&quot;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;       &lt;span style=&quot;background-color: black;&quot;&gt; wBmp = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;new&lt;/span&gt; WriteableBitmap(&lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;256&lt;/span&gt;, &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;96&lt;/span&gt;, &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;96&lt;/span&gt;, PixelFormats.Rgb24, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;null&lt;/span&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif; line-height: 125%; white-space: pre;&quot;&gt;         - make a buffer to hold image information you need to send and add the information(in this case a 3 x 256 LUT):&lt;/span&gt;&lt;br /&gt;
&lt;div .1em=&quot;&quot; .6em=&quot;&quot; .8em=&quot;&quot; border-width:.1em=&quot;&quot; border:solid=&quot;&quot; gray=&quot;&quot; overflow:auto=&quot;&quot; padding:.2em=&quot;&quot; width:auto=&quot;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;i&gt;&lt;span style=&quot;color: #303090; font-weight: bold;&quot;&gt;        &lt;span style=&quot;background-color: black;&quot;&gt;byte&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: black;&quot;&gt;[] buff = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #303090; font-weight: bold;&quot;&gt;byte&lt;/span&gt;[&lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;768&lt;/span&gt;];&lt;/span&gt;&lt;/i&gt;
&lt;/pre&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: black;&quot;&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/pre&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - send information to graphics board&lt;/span&gt;&lt;br /&gt;
&lt;div .6em=&quot;&quot; .8em=&quot;&quot; border-width:.1em=&quot;&quot; border:solid=&quot;&quot; gray=&quot;&quot; overflow:auto=&quot;&quot; padding:.2em=&quot;&quot; style=&quot;background-color: black;&quot; width:auto=&quot;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;i&gt;            wBmp.WritePixels(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;new&lt;/span&gt; Int32Rect(&lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;0&lt;/span&gt;,
                wBmp.PixelWidth, wBmp.PixelHeight),
                buff, wBmp.PixelWidth * wBmp.Format.BitsPerPixel / &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;8&lt;/span&gt;, &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;0&lt;/span&gt;);

            LutColor = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;new&lt;/span&gt; ImageBrush(wBmp);&lt;/i&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- define the dependency property:&lt;/span&gt;&lt;br /&gt;
&lt;div .6em=&quot;&quot; .8em=&quot;&quot; border-width:.1em=&quot;&quot; border:solid=&quot;&quot; gray=&quot;&quot; overflow:auto=&quot;&quot; padding:.2em=&quot;&quot; style=&quot;background-color: black;&quot; width:auto=&quot;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;i&gt;        &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;readonly&lt;/span&gt; DependencyProperty LutColorProperty =
            ShaderEffect.RegisterPixelShaderSamplerProperty(&quot;LutColor&quot;,
            &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;typeof&lt;/span&gt;(ColorManiPulatorEffect), &lt;span style=&quot;color: #6000e0; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;/* assigned to sampler register S1 */&lt;/span&gt;);

        Brush LutColor
        {
            &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;return&lt;/span&gt; (Brush)GetValue(LutColorProperty); }
            &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;set&lt;/span&gt; { SetValue(LutColorProperty, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;value&lt;/span&gt;); }
        }&lt;/i&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;How to write the shader:&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;i suggest you use a separated tool to write the shader, one that allows you to view real time what your shader looks like on a target image before you start the integration in your .Net application&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;i used &lt;b&gt;Fx Composer &lt;/b&gt;from&amp;nbsp;&lt;a href=&quot;https://developer.nvidia.com/fx-composer&quot;&gt;https://developer.nvidia.com/fx-composer&lt;/a&gt;, it&#39;s a bit more than what you need for a basic shader develop but it&#39;s very good at what it does and it allows you to visualy test your shader&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;however a simple shader can be written using notepad, adding some simple code and saving the file with a fx extension :&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div .6em=&quot;&quot; .8em=&quot;&quot; border-width:.1em=&quot;&quot; border:solid=&quot;&quot; gray=&quot;&quot; overflow:auto=&quot;&quot; padding:.2em=&quot;&quot; style=&quot;background-color: black;&quot; width:auto=&quot;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;i&gt;sampler2D inSampler : register(S0);&lt;span style=&quot;color: grey;&quot;&gt;//input image, sent using register S0&lt;/span&gt;
&lt;span style=&quot;color: #303090; font-weight: bold;&quot;&gt;float&lt;/span&gt; brightness : register(C0);&lt;span style=&quot;color: grey;&quot;&gt;//input value, sent using register C0&lt;/span&gt;

float4 &lt;span style=&quot;color: #0060b0; font-weight: bold;&quot;&gt;main&lt;/span&gt;(float2 uv : TEXCOORD) : COLOR {&lt;span style=&quot;color: grey;&quot;&gt;//declaration of shader body&lt;/span&gt;
  float4 color = tex2D(inSampler, uv);&lt;span style=&quot;color: grey;&quot;&gt;//get the source color at the current uv texture coordinates &lt;/span&gt;
&lt;span style=&quot;color: grey;&quot;&gt;//as a vector of 4 float values r,g,b,a&lt;/span&gt;
  &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;return&lt;/span&gt; color * brightness;&lt;span style=&quot;color: grey;&quot;&gt;//multiply all the values with the brightness&lt;/span&gt;
}&lt;/i&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;Compile that .fx file and get a .px file:&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;install Direct x SDK from&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://www.microsoft.com/en-us/download/details.aspx?id=6812&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;http://www.microsoft.com/en-us/download/details.aspx?id=6812&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;for &lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Windows 8 check this&amp;nbsp;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/windows/desktop/ee663275%28v=vs.85%29.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/windows/desktop/ee663275%28v=vs.85%29.aspx&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;go to Visual Studio\Tools\External Tools , click add and&amp;nbsp;introduce&amp;nbsp;the details:&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;Title : Directx compiler&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;Command : Path to fxc.exe(fx compiler) in DxSdk instalation&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;Arguments : &lt;i&gt;/DWPF /Od /T ps_2_0 /E $(ItemFileName)PS /Fo$(ProjectDir)$(ItemFileName).ps $(ItemPath)&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Initial directory : $(TargetDir)&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Uncheck &quot;Close on exit&quot; to make sure output of fx compilation is visible&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;Add fx file to solution explorer and select it , and use the new added tool&lt;/span&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Resulted ps file must be added to solution and set &quot;Build action&quot; to Resource(!!!)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;alternative&lt;/b&gt;, you can also try addins :&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;for build:&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://code.msdn.microsoft.com/silverlight/HLSL-Shader-Build-Task-285e9b53&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;HLSL Shader Build Task&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;for syntax(optional):&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://nshader.codeplex.com/&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;NShader&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/4700476130132753092/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2013/04/shader-effects-in-net.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/4700476130132753092'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/4700476130132753092'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2013/04/shader-effects-in-net.html' title='Shader effects in .NET'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-7824458051532076351</id><published>2012-11-05T06:13:00.002-08:00</published><updated>2015-09-08T00:30:01.527-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="3D"/><category scheme="http://www.blogger.com/atom/ns#" term="Blender"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Math"/><title type='text'>3D Math: Rotate to point</title><content type='html'>&lt;h3&gt;
&lt;b&gt;1. Problem description:&lt;/b&gt;&lt;/h3&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; We have :&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - 3d object that is (let&#39;s assume), centered on the Y axis&lt;br /&gt;
&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge6I215jpkF9O7kD6hCRkCts6SmL1w2mIJ41Woln70kLOlAm5SWFj7RYfl2OTvtRqGi-pBF4VqMeV8kQcswHHzBUZP8Wl8s4djG0Hs2E_Gx4SsUbEqlZ4UlZicdaHvW1-8uXlnXnt53xSp/s1600/ConeRotation..png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; margin-bottom: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;268&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge6I215jpkF9O7kD6hCRkCts6SmL1w2mIJ41Woln70kLOlAm5SWFj7RYfl2OTvtRqGi-pBF4VqMeV8kQcswHHzBUZP8Wl8s4djG0Hs2E_Gx4SsUbEqlZ4UlZicdaHvW1-8uXlnXnt53xSp/s640/ConeRotation..png&quot; width=&quot;580&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Cone was rotated to face the point (x1,y1,z1)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- arbitrary point in space v1 with the coordinates (x1,y1,z1)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; We want :&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - obtain the rotation matrix or Quaternion that will rotate your object towards the arbitrary point (look at)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - so, input parameter is v1 and output will be a 4x4 transformation matrix&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; Optional :&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - other implementations to this issue will also take as input parameters a &lt;b&gt;up vector&amp;nbsp;&lt;/b&gt;(in this case is the OY axis) and a rotation center (i used (0,0,0))&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;b&gt;2. Implementation :&lt;/b&gt;&lt;/h3&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- we solve this by finding the &lt;a href=&quot;http://en.wikipedia.org/wiki/Axis-angle_representation&quot;&gt;axis-angle&amp;nbsp;representation&lt;/a&gt;&amp;nbsp;for the rotation&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- i see this as the most simplest way, as many frameworks will already have implementations for the all math required (&lt;a href=&quot;http://www.fundza.com/vectors/normalize/index.html&quot;&gt;Vector&amp;nbsp;Normalization&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Cross_product&quot;&gt;Cross Product&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Dot_product&quot;&gt;Dot Product&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Rotation_matrix#Conversion_from_and_to_axis-angle&quot;&gt;Axis-Angle to Matrix conversion&lt;/a&gt;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- code example, using the Vector3, Matrix4 and Quaternion classes from&amp;nbsp;&lt;a href=&quot;http://libgdx.badlogicgames.com/&quot;&gt;LibGdx&lt;/a&gt;&amp;nbsp; :&lt;br /&gt;



&lt;div style=&quot;background: #202020; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;nor&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;();&lt;/span&gt;  
&lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;float&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;angle&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #3677a9&quot;&gt;180&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;f&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Math.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;PI&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;Math.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;acos&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;dot&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(Yaxis)));&lt;/span&gt;

  &lt;span style=&quot;color: #d0d0d0&quot;&gt;vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;crs&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(Yaxis);&lt;/span&gt;

  &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;(vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;()&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3677a9&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #d0d0d0&quot;&gt;vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(Zaxis);&lt;/span&gt;
  &lt;span style=&quot;color: #d0d0d0&quot;&gt;}&lt;/span&gt;
  &lt;span style=&quot;color: #d0d0d0&quot;&gt;quaterion.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(vec,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;angle);&lt;/span&gt;
  &lt;span style=&quot;color: #d0d0d0&quot;&gt;vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;crs&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(Yaxis);&lt;/span&gt;
  &lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;(vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;()&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #3677a9&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;{&lt;/span&gt;   &lt;span style=&quot;color: #d0d0d0&quot;&gt;vec.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(Zaxis);&lt;/span&gt;  &lt;span style=&quot;color: #d0d0d0&quot;&gt;}&lt;/span&gt;  &lt;span style=&quot;color: #d0d0d0&quot;&gt;quaterion.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(vec,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;angle);&lt;/span&gt;  &lt;span style=&quot;color: #d0d0d0&quot;&gt;transformMatrix.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;rotate&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;(quaterion.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb&quot;&gt;conjugate&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;());&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;/blockquote&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1. &lt;a href=&quot;http://www.fundza.com/vectors/normalize/index.html&quot;&gt;Normalize v1&lt;/a&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp;Find the angle between OY&amp;nbsp;and v1 :&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - calculate the &lt;a href=&quot;http://en.wikipedia.org/wiki/Dot_product&quot;&gt;dot product&lt;/a&gt;&amp;nbsp;between the 2 vectors to get the cosine of the angle between them&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - get the angle value in radians by applying arccosine&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - convert to degrees if necessary&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3. Calculate &lt;a href=&quot;http://en.wikipedia.org/wiki/Cross_product&quot;&gt;cross product&lt;/a&gt; between the vectors, to obtain the rotation axis, the vector that is perpendicular on both OY&amp;nbsp;and v1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4. If the cross product is zero (vectors are parallel), we use OZ as rotation axis&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5. We now have a rotation axis and a rotation angle.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;This can be used as input to create a transformation matrix or a Quaternion(indirect aproach).&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If, &amp;nbsp;using something like the code above, make sure your roation matrix is set to identity&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If, for some reason you need to do the math yourself, see&amp;nbsp;&lt;a href=&quot;http://en.wikipedia.org/wiki/Rotation_matrix#Conversion_from_and_to_axis-angle&quot;&gt;Conversion_from_and_to_axis-angle&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/7824458051532076351/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2012/11/3d-math-rotate-to-point.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/7824458051532076351'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/7824458051532076351'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2012/11/3d-math-rotate-to-point.html' title='3D Math: Rotate to point'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge6I215jpkF9O7kD6hCRkCts6SmL1w2mIJ41Woln70kLOlAm5SWFj7RYfl2OTvtRqGi-pBF4VqMeV8kQcswHHzBUZP8Wl8s4djG0Hs2E_Gx4SsUbEqlZ4UlZicdaHvW1-8uXlnXnt53xSp/s72-c/ConeRotation..png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-8809169984472592817</id><published>2012-10-06T06:52:00.001-07:00</published><updated>2015-09-08T00:29:15.801-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="NDK"/><title type='text'>Android NDK, simple step by step guide</title><content type='html'>&lt;a href=&quot;http://developer.android.com/tools/sdk/ndk/index.html&quot;&gt;NDK&lt;/a&gt;&amp;nbsp;allows you to use native(C, C++) code for your android application and it&#39;s a delicate subject.&lt;br /&gt;
&lt;br /&gt;
The book &quot;&lt;a href=&quot;http://www.packtpub.com/android-ndk-beginners-guide/book&quot;&gt;Android NDK&amp;nbsp;Beginner&#39;s Guide&lt;/a&gt;&quot; by&amp;nbsp;Sylvain Ratabouil is 425 pages long and it&#39;s just a &quot;beginner&#39;s guide&quot;. I will try to make this as short as possible, and provide a quick step by step guide to a working NDK example.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;What you need :&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; - working Android&amp;nbsp;environment. I will use Eclipse on Windows for this so it will be&amp;nbsp;easier to have the same.&amp;nbsp;Working means you already&amp;nbsp;succeed with&amp;nbsp;a &lt;a href=&quot;http://www.mkyong.com/android/android-hello-world-example/&quot;&gt;Hello World example&lt;/a&gt;&lt;br /&gt;
&amp;nbsp;- Android NDK - check&amp;nbsp;&lt;a href=&quot;http://developer.android.com/tools/sdk/ndk/index.html&quot;&gt;http://developer.android.com/tools/sdk/ndk/index.html&lt;/a&gt;. As a alternative in future developemnts you can also use &lt;a href=&quot;http://www.crystax.net/en/android/ndk&quot;&gt;CristaX&#39;s custom made NDK&lt;/a&gt;, which has some improvements but for this example the one from Google is enough&lt;br /&gt;
&amp;nbsp;- &lt;a href=&quot;http://cygwin.com/install.html&quot;&gt;Cygwin &lt;/a&gt;- only for windows devices&lt;br /&gt;
&amp;nbsp;- for simplicity, NDK should just be unzipped to a directory and Cygwin installed via installer. No headaches with&amp;nbsp;environment&amp;nbsp;variables &amp;amp; stuff for now. &amp;nbsp;For this example, it should be enough.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The steps :&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
So, we will do the sum of two numbers, written in native code and used in the android project &amp;nbsp;:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;1. Prepare your android project &amp;nbsp;:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- you need a android project in Eclipse to begin with&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- in the root directory(same level with &quot;src&quot;), create a new directory called &quot;jni&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- in a package of your choosing(for me : &lt;i&gt;com.blogspot.soiuz - &lt;/i&gt;replace all later&amp;nbsp;occurrences&amp;nbsp;with your package name) create the&amp;nbsp;&lt;i&gt;SimpleLib &lt;/i&gt;java class and add the code :&lt;br /&gt;
&lt;div style=&quot;background: #202020; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;com.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb;&quot;&gt;blogspot&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb;&quot;&gt;soiuz&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;;&lt;/span&gt; 
&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #447fcf; text-decoration: underline;&quot;&gt;SimpleLib&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0;&quot;&gt;System.&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb;&quot;&gt;loadLibrary&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;sLib&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;);&lt;/span&gt;
 &lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
  &lt;span style=&quot;color: #999999; font-style: italic;&quot;&gt;// Native  - sum of two numbers&lt;/span&gt;
 &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;native&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: #447fcf;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;a,&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;b);&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;br /&gt;&lt;/blockquote&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - this is the minimum you need :&amp;nbsp;&lt;i&gt;loadLibrary &lt;/i&gt;with the name of the native library&amp;nbsp;we will later create(in this case &quot;sLib&quot;) and at least one function declaration to use\test(in this case, adding of two numbers).&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- based on the code here we will automatically generate a header file, so this is important&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- open Command Prompt, and navigate to the &lt;i&gt;bin &lt;/i&gt;directory of your android project&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- to create a header file, we will use &lt;a href=&quot;http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javah.html&quot;&gt;javah &lt;/a&gt;from the JDK tools by calling :&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;i&gt;javah -jni&amp;nbsp;com.blogspot.soiuz.SimpleLib&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- &lt;/i&gt;if it says it cannot find the class, make sure you can navigate from the bin directory, to SimpleLib.java by going&amp;nbsp;&lt;i&gt;com\blogspot\soiuz\SimpleLib. &lt;/i&gt;EG: you might need to step into the directory &lt;i&gt;classes &lt;/i&gt;and run javah from there.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- in the pointed directory you will have &lt;i&gt;com_blogspot_soiuz_SimpleLib.h,&amp;nbsp;&lt;/i&gt;This is the C header&amp;nbsp;file that we need to implement next.&lt;br /&gt;
&lt;i&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- &lt;/i&gt;move this file to the &lt;i&gt;jni &lt;/i&gt;folder and refresh the folder in Eclipse&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- if you look into the file, you will have the add function written in a wierd manner : JNIEXPORT jlong JNICALL Java_com_blogspot_soiuz_SimpleLib_add , this is standard &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_Native_Interface&quot;&gt;jni signature&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2. Implementing the C library :&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -&amp;nbsp;&amp;nbsp;right-click on the jni folder and choose New→File. Save&amp;nbsp;it as&amp;nbsp;sLib.c&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - &amp;nbsp;add code to it :&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #202020; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cd2828; font-weight: bold;&quot;&gt;#include &quot;com_blogspot_soiuz_SimpleLib.h&quot;&lt;/span&gt;
&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: #447fcf;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;a,&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;b)&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;a+b;&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0;&quot;&gt;JNIEXPORT&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;jlong&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;JNICALL&lt;/span&gt; &lt;span style=&quot;color: #447fcf;&quot;&gt;Java_com_blogspot_soiuz_sLib_add&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(JNIEnv&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;*env,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;jclass&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;obj,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;jint&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;a,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;jint&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;b)&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;add(a,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;b);&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - so we : add the header, implement our method in C, copy the method declaration for the generated header file and the variable names and call the native function. Easy.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;3. The Makefile :&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - we have some files and &lt;a href=&quot;http://www.ankara-gtug.org/2011/12/29/understanding-android-makefile-android-mk/&quot;&gt;this makefile &lt;/a&gt;will tell how they will be used&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - so, again, New --&amp;gt;File and create Android.mk&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - add text to it :&lt;br /&gt;


&lt;div style=&quot;background: #202020; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;LOCAL_PATH&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;:=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;$(call&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;my-dir)&lt;/span&gt;
&lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;include&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;$(CLEAR_VARS)&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0&quot;&gt;LOCAL_MODULE&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;:=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;sLib&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0&quot;&gt;LOCAL_SRC_FILES&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;:=&lt;/span&gt; &lt;span style=&quot;color: #447fcf; text-decoration: underline&quot;&gt;sLib&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0&quot;&gt;.c&lt;/span&gt;
&lt;span style=&quot;color: #6ab825; font-weight: bold&quot;&gt;include&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0&quot;&gt;$(BUILD_SHARED_LIBRARY)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;br /&gt;&lt;/blockquote&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - no spaces empty lines,&amp;nbsp;indentation.All text should start from begin of line&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - in the makefile we need to specify : Module library - sLib, files we want to build - sLib.c&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;4. Building all :&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- for this we need 2 tools : NDK , and if you want to do this on windows you also need &lt;a href=&quot;http://cygwin.com/install.html&quot;&gt;Cygwin&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- assuming both are installed and configured, open cygwin and execute the 2 next commands&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- &amp;nbsp;&lt;i&gt;cd /cygdrive/d/workspace/AndroidNdk&lt;/i&gt; (navigates to android project root directory. All drives are mapped under cygdrive)&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- &amp;nbsp;&lt;i&gt;/cygdrive/d/install/android-ndk-r6b/ndk-build (&lt;/i&gt;form the Ndk installation directory, it will&amp;nbsp;execute ndk-build)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- if all was ok, it will show some text saying that &lt;i&gt;libsLib.so &lt;/i&gt;was created in&amp;nbsp;&lt;i&gt;\libs\armeabi\&lt;/i&gt;.This is the needed library&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;5. Using, testing&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - &lt;i&gt;SimpleLib.add(1,2);&lt;/i&gt; This is all you need to do&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - class SimpleLib is&amp;nbsp;already&amp;nbsp;created inside the android project and i suggest you make a class like that to act as a intermediary between java code and native library&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; </content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/8809169984472592817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2012/10/android-ndk-simple-step-by-step-guide.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/8809169984472592817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/8809169984472592817'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2012/10/android-ndk-simple-step-by-step-guide.html' title='Android NDK, simple step by step guide'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-5604226157500248726</id><published>2011-12-19T06:59:00.000-08:00</published><updated>2015-09-08T00:28:33.547-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="COM"/><category scheme="http://www.blogger.com/atom/ns#" term="IDL"/><category scheme="http://www.blogger.com/atom/ns#" term="managed"/><category scheme="http://www.blogger.com/atom/ns#" term="TLBimp"/><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio"/><title type='text'>COM to managed</title><content type='html'>So, you need to work with COM.&lt;br /&gt;
You have :&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; - idl interface(a .idl file that contains the interface of the COM dll)&lt;br /&gt;
You need to make:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; - Com dll that implements the interface&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;1. The hard way, i do not want to go into details of how atl and com code will try to sabotage you on every step.&lt;br /&gt;
&lt;br /&gt;
2. The way: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - use Visual Studio tools. To be more exact , open Visual Studio Command prompt&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - execute &lt;i&gt;msil myIdl.idl&lt;/i&gt; , more info at: &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/f7dy01k1%28v=vs.80%29.aspx&quot;&gt;Msil &lt;/a&gt;. This will create a &lt;i&gt;tbl &lt;/i&gt;file that contains the binary form of the idl interface&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - tbl file will be created by default only if&amp;nbsp; the idl file contains the &lt;i&gt;library &lt;/i&gt;tag. If your idl contains only &lt;i&gt;interface &lt;/i&gt;tags , you need to create another idl file that contains library and references the idl with interfaces. Also if no &lt;i&gt;library &lt;/i&gt;tags are present the output when executing msil will not be a tbl file, just c and h files that you can reference from your code.&lt;br /&gt;
&lt;br /&gt;
Example of a file that contains library and references the interfaces(most important line is &lt;i&gt;import &quot;MyIdl.idl&quot;;&lt;/i&gt;) :&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/Pzkgw/dcac9449506033d6e719.js&quot;&gt;&lt;/script&gt;

- ok, now you have a tlb file, next step is: tlbimp MyIdl.tlb , documentation at &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/tt0cf3sx%28v=vs.80%29.aspx&quot;&gt;Tlb import&lt;/a&gt;&lt;br /&gt;
- this will create a managed dll that you can just reference in your application, use it and implement the interfaces&lt;br /&gt;
- optional step : in the dll you will find already made classes that use the interfaces and have the methods marked as virtual. You can make your class on top of this class or you can just use the interfaces in a direct way (your class on top of the interfaces) (oh yeah , on top means derived from).&lt;br /&gt;
In fact, .NET framework 4 has a problem if you choose to use the classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
FINAL STEP : COM registration&lt;br /&gt;
- so you made this nice dll that implements the interfaces, last thing to do is register this dll&lt;br /&gt;
- command to execute for registration is &lt;i&gt;regasm /codebase MyDll.dll&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
- however, you might find out that this thing fails horribly, so :&lt;br /&gt;
- in AssemblyInfo set ComVisible to true&lt;br /&gt;
- what you want to do is add some lines of code to the classes in the dll that you want to register for COM in order to create registry entries :&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/Pzkgw/547a35a20e95e1a01077.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
Hope it helped .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/5604226157500248726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2011/12/com-to-managed.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/5604226157500248726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/5604226157500248726'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2011/12/com-to-managed.html' title='COM to managed'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156959992050056410.post-3133292722477920346</id><published>2011-09-13T04:46:00.000-07:00</published><updated>2015-09-08T00:36:46.753-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="alloc"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="deserialize"/><category scheme="http://www.blogger.com/atom/ns#" term="field"/><category scheme="http://www.blogger.com/atom/ns#" term="memory"/><category scheme="http://www.blogger.com/atom/ns#" term="serialize"/><category scheme="http://www.blogger.com/atom/ns#" term="size"/><category scheme="http://www.blogger.com/atom/ns#" term="struct"/><title type='text'>Struct serialize and deserialize</title><content type='html'>&lt;br /&gt;
A while ago i had to make some structures in C# that you can easy serialize/deserialize. &amp;nbsp;The constraint was to have the structure and the fields mirror the serialization (eg : for a struct 3 bytes struct we have a 3 bytes serialization stream).&lt;br /&gt;
&lt;br /&gt;
First&amp;nbsp;find out&amp;nbsp;: for c# and c++ structs the memory allocation is &#39;optimized&#39; and the fields may not be sequential or may not occupy the expected size in bytes.&lt;br /&gt;
&lt;br /&gt;
Solution(C#) : use&amp;nbsp;[StructLayout(LayoutKind.Sequential, Pack = 1)] attribute&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/Pzkgw/2a777cf1cd4c82106f6b.js&quot;&gt;&lt;/script&gt;




The serialize and deserialize functions try to save/load the pointed memory :&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #202020; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;        &lt;span style=&quot;color: #999999; font-style: italic;&quot;&gt;//serialize any object by geting the unmanaged byte stream&lt;/span&gt;
        &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;internal&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: #447fcf;&quot;&gt;rawSerialize&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;object&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;anything)&lt;/span&gt;
        &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawSize&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.SizeOf(anything);&lt;/span&gt;
            &lt;span style=&quot;color: #d0d0d0;&quot;&gt;IntPtr&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;buffer&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.AllocHGlobal(rawSize);&lt;/span&gt;
            &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.StructureToPtr(anything,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;buffer,&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;);&lt;/span&gt;
            &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawDatas&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;[rawSize];&lt;/span&gt;
            &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.Copy(buffer,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawDatas,&lt;/span&gt; &lt;span style=&quot;color: #3677a9;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawSize);&lt;/span&gt;
            &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.FreeHGlobal(buffer);&lt;/span&gt;
            &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawDatas;&lt;/span&gt;
        &lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;

        &lt;span style=&quot;color: #999999; font-style: italic;&quot;&gt;//deserialize byte stream&lt;/span&gt;
        &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;internal&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;object&lt;/span&gt; &lt;span style=&quot;color: #447fcf;&quot;&gt;rawDeserialize&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawData,&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;ref&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;ulong&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;position,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Type&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;anyType)&lt;/span&gt;
        &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;object&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;retVal&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;;&lt;/span&gt;

            &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;(rawData&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;null&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawData.Length&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #3677a9;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawsize&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.SizeOf(anyType);&lt;/span&gt;
                &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;(rawsize&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawData.LongLength)&lt;/span&gt;
                    &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;;&lt;/span&gt;
                &lt;span style=&quot;color: #d0d0d0;&quot;&gt;IntPtr&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;buffer&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.AllocHGlobal(rawsize);&lt;/span&gt;
                &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.Copy(rawData,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;)position,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;buffer,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;rawsize);&lt;/span&gt;
                &lt;span style=&quot;color: #d0d0d0;&quot;&gt;retVal&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.PtrToStructure(buffer,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;anyType);&lt;/span&gt;
                &lt;span style=&quot;color: #d0d0d0;&quot;&gt;Marshal.FreeHGlobal(buffer);&lt;/span&gt;
                &lt;span style=&quot;color: #d0d0d0;&quot;&gt;position&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;ulong&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;)rawsize;&lt;/span&gt;
            &lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;

            &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;retVal;&lt;/span&gt;
        &lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
Hope it was helpful &amp;nbsp;: D&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://soiuz.blogspot.com/feeds/3133292722477920346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://soiuz.blogspot.com/2011/09/serialize-and-deserialize-structs-in-c.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/3133292722477920346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156959992050056410/posts/default/3133292722477920346'/><link rel='alternate' type='text/html' href='http://soiuz.blogspot.com/2011/09/serialize-and-deserialize-structs-in-c.html' title='Struct serialize and deserialize'/><author><name>RobotAtWork</name><uri>http://www.blogger.com/profile/14231676151596127366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>