<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Abed on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Abed on Medium]]></description>
        <link>https://medium.com/@aboelbisher.176?source=rss-d42e92112f3f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GBAvWMd4tCPC7kR4AuXO1w@2x.jpeg</url>
            <title>Stories by Muhammad Abed on Medium</title>
            <link>https://medium.com/@aboelbisher.176?source=rss-d42e92112f3f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 11:56:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aboelbisher.176/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rendering Objects in Augmented Reality with Metal in IOS]]></title>
            <link>https://medium.com/tech-at-trax/rendering-objects-in-augmented-reality-with-metal-in-ios-96602cc0db40?source=rss-d42e92112f3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/96602cc0db40</guid>
            <category><![CDATA[ar]]></category>
            <category><![CDATA[rendering]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[gpu]]></category>
            <category><![CDATA[metal]]></category>
            <dc:creator><![CDATA[Muhammad Abed]]></dc:creator>
            <pubDate>Sun, 03 May 2020 10:54:34 GMT</pubDate>
            <atom:updated>2020-05-03T10:54:34.588Z</atom:updated>
            <content:encoded><![CDATA[<p><em>NOTE: before you dive into this, you need to be familiar with: Swift, IOS development, ARKit, basic linear algebra, basic 2D metal rendering.</em></p><p><em>If you are, then go grab yourself a cup of coffee and come back, I’ll wait for you!</em></p><p>My story begins with the <a href="https://traxretail.com/">Trax Retail App</a>. In short we use our crowdsourcing app to gather data about retail stores by taking pictures of the store shelves. We use these pictures to extract (using complicated machine learning algorithms) valuable data that retailers can use in order to know and monitor what’s happening in their stores.</p><p>We had one major problem - users were uploading pictures with bad angles, in order to enhance the user’s experience, we decided to integrate AR in our app, and we saw that users that use AR had better quality pictures!</p><h3>The Trax App</h3><p>You open the AR camera, let it detect the shelf</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/0*-pyUi8v8Ss-6gE3u" /></figure><p>After the AR session detects the shelf, the user can start taking pictures:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/0*yXAJpVzQvhnXrEnc" /></figure><p>With each picture the user takes, we draw a rectangle (plain) on the shelf to mark the area that the user covered:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/0*j1ObUMLfJa4kaUfw" /></figure><p>In our app we use SceneKit to render these turquoise walls, but because I love challenges, I found myself looking at how to render objects in AR using Apple’s <a href="https://en.wikipedia.org/wiki/Metal_(API)">Metal</a>. I had a lot of challenges doing it, due to the lack of proper and detailed documentation from Apple (as always), and the surprisingly poor and old material on the internet, yet I loved every single moment of it.</p><p>Moving to Trax and the joy of working on this part of our app made me very passionate about it.</p><p>So if you are a challenge lover, and you like to go deep and understand how these libraries work, then you’ve come to the right place!</p><h3><strong>Math Background</strong></h3><h4>Handedness</h4><p>Handedness refers to the orientation of the z-axis in a given 3D space. If the z-axis conforms to the so-called right-hand rule, then the space is said to be <em>right-handed</em>. Alternatively, the space is <em>left-handed</em>.</p><p>We can use both, as long as we understand this and know how to move between one and the other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/0*6N9DLZMF3BEjbMft" /></figure><h4>Linear Transformations</h4><p>Geometric transformation is a function that maps one point to another point.</p><p>Linear transformation (also called linear mapping, linear function) is a <a href="https://en.wikipedia.org/wiki/Map_(mathematics)">mapping function.</a></p><p><em>V</em> → <em>W</em> between two <a href="https://en.wikipedia.org/wiki/Module_(mathematics)">modules</a> (for example, two <a href="https://en.wikipedia.org/wiki/Vector_space">vector spaces</a>) that preserve (in the sense defined below) the operations of addition and <a href="https://en.wikipedia.org/wiki/Scalar_(mathematics)">scalar</a> multiplication — wikipedia</p><p>f(u + v) = f(u) + f(v) //addition</p><p>f(c*u) =c*f(u), c is scalar // scalar multiplication</p><p>Each linear transformation can be written as a matrix.</p><p>The most common transformations in computer graphics are Scale, Rotation and Translation.</p><p>Scale and Rotation are linear transformations (we won’t be covering proofs here, or it’d be a course in linear algebra 😅). After playing with Translation a little, you’ll find out that it’s not a linear transformation, so we need a trick to make it work.</p><h4>Scale</h4><p>Can be represented by the following 3X3 matrix</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*BJGXMCGBYp7DPzUcXhSPwA.png" /></figure><h4><strong>Rotation</strong></h4><p>If <em>u</em> is the direction vector and the angle (in radians), then the rotation matrix should look like this…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xNgb47IpNvJqwCuF" /></figure><p>We won’t be covering the source of this matrix here, but you are free to check out this wikipedia <a href="https://en.wikipedia.org/wiki/Rotation_matrix">post</a> which explains it very well.</p><h4><strong>Translation</strong></h4><p>In order to translate the point <em>p</em> by the normalized translation vector <em>t,</em> we need this simple equation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/0*61rs29KRqjfKV1wa" /></figure><p>We can’t make a 3X3 matrix that we multiply with the point, to get this result.</p><p>To do this we will use a handy and widely known trick, we’ll add a new dimension <em>w</em>, and make it equal 1, so we’ll have this 4X4 matrix.</p><p>So when you multiply it with a point (with addition of <em>w</em> dimension), you get a 4D point, and by removing the fourth dimension we get our desired point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/0*sHu6pcm3geBtysC3" /></figure><p>The above linear transformation (in 4D) is called a <a href="https://en.wikipedia.org/wiki/Shear_mapping">Shear transformation</a>.</p><h4><strong>Projection</strong></h4><p>One of our goals in 3D rendering is to “squash” 3D objects into 2D, and create the perspective illusion. We achieve this by creating a perspective projection matrix.</p><p>To understand how a perspective projection matrix works, you need this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/0*7ChLrOJhso6jtNad" /></figure><p>This is called the view frustum. By choosing an aspect ratio (ratio between width and height of viewport) and a field of view, we implicitly determine the <em>clipping planes</em> that make up the sides of the view frustum.</p><p>The pyramidal shape of the frustum is a natural outcome of our demand for perspective. The perspective projection scales point relative to their distance from the virtual camera, which in turn causes the sloped sides of the view frustum to become parallel as it undergoes this transformation. Points far away are squeezed closer to the axis of view, which causes the phenomenon of <em>foreshortening</em>, an important depth cue.</p><h3>Clip Space</h3><p>The device has it’s coordinate system, and it expects the given points to render, to respect this coordinate system: x[-1,1], y[-1,1], z[0,1]. This is called the clip space of the system. It’s where the system determines which triangles should be shown, which are clipped, which are hidden.</p><p>Now that we know what the clip space is, we can construct the projection matrix that scales points from one clip space to another :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/0*qMKx9W5KsP0EAhBf" /></figure><p>All the symbols refer to the view frustum illustration.</p><p>The construction of this matrix is beyond the scope of this post, but you’re free to check <a href="https://en.wikipedia.org/wiki/Camera_matrix">this</a> and <a href="https://en.wikipedia.org/wiki/3D_projection">this</a> wikipedia post about it.</p><h3><strong>Code</strong></h3><p>To put this into practice, we’ll make a little app that puts a 3D cube on the location you clicked every time you click on the screen.</p><p>We won’t cover the Metal setup and the Assets setup here, since I am assuming you know all these things.</p><p>We’ll start with the Vertex Data function (functions we set of necessary data for drawing) before we pass the data to the shaders.</p><p>The first interesting function is the one where we set the necessary matrices, this is how we define our frustum:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8648cf0cdf044cb627a80bb4b958627c/href">https://medium.com/media/8648cf0cdf044cb627a80bb4b958627c/href</a></iframe><p>You can see that at the start of the function, we set the view matrix and the projection matrix — by setting view port size, near and far properties we talked about.</p><p>The other lines are for setting the light, in order to give our shapes 3D lighting.</p><p>We won’t be covering this here, but you’re welcome to <a href="https://en.wikipedia.org/wiki/Computer_graphics_lighting">read about it</a>.</p><p>Next, we’ll update the anchors:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77f27bad5224ee9e19af3f18b501e21d/href">https://medium.com/media/77f27bad5224ee9e19af3f18b501e21d/href</a></iframe><p>We’re going through the anchors here, and from these anchors we create a model matrix — the matrix that defines the object we want to draw as a result of your click on the screen.</p><p>That’s the two main functions we use to set the data for drawing (Vertex Data stage).</p><p>Wasn’t really as tough and scary as you thought it would be! Right!? Now that you understand the whole purpose of this “complicated” mathematics, you can now connect the dots to figure out why we need all these matrices.</p><p>Next we’re moving to the shader functions, this is where we apply all the mathematics we’ve learned.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b1c085c9c6ef0f33b36e0ec7989e8be/href">https://medium.com/media/2b1c085c9c6ef0f33b36e0ec7989e8be/href</a></iframe><p>Here we simply apply each vertex position with projection, model and any other transformations (Translation, Rotation, Scale). We also set the Eye Position (frustum top point).</p><p>Don’t worry about the lighting properties, they’re just there to make things look better, we won’t be covering the lighting here.</p><p>The surprise is that you can find the whole source code if you open a new project in your Xcode and select the Augmented Reality App, and then choose Metal for Content Technology in the next screen.</p><p>But now instead of starting from zero, you can focus on the important parts — Yes, the parts we covered here — and leave the “complicated” technical setup for later.</p><p>From this point you can start playing with AR using Metal, trying to create a more interactive app, handling clicks and swipes on these cubes you create, and trying to Rotate or Scale them.</p><p>In short… HAVE FUN!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96602cc0db40" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tech-at-trax/rendering-objects-in-augmented-reality-with-metal-in-ios-96602cc0db40">Rendering Objects in Augmented Reality with Metal in IOS</a> was originally published in <a href="https://medium.com/tech-at-trax">Tech at Trax</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hacking React Native Profiler]]></title>
            <link>https://medium.com/@aboelbisher.176/hacking-react-native-profiler-762e800b49ea?source=rss-d42e92112f3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/762e800b49ea</guid>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[profiling]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Muhammad Abed]]></dc:creator>
            <pubDate>Tue, 16 Apr 2019 17:27:05 GMT</pubDate>
            <atom:updated>2019-04-17T12:29:25.601Z</atom:updated>
            <content:encoded><![CDATA[<p>My story begins with the <a href="https://itunes.apple.com/us/app/wix-com/id1099748482?mt=8">Wix App</a>, Wix’s official native IOS/Android App.</p><ul><li>It is written in React Native</li><li>More than 40 developers are working on this project (and we’re growing so fast!)</li></ul><p>With a scale like this, it’s becoming harder to maintain good performance.</p><p>So we decided that we need to know more about what’s going on in the native side of react-native, and lucky me, I got this task!.</p><p><em>before you dive into this you need to be familiar with react-native, ios, c, c++, objective-c, </em>JavaScriptCore<em>. If you don’t have knowledge in some of these subjects I highly encourage you to go and read about them. In my opinion (regardless if you’re reading this or not) if you want to become a skilled programmer and you want to learn the programming dark arts, you have to have a good knowledge in most of them.</em></p><p>Mainly I was supposed to collect all the “Events” that happens in react-native framework and report them to our amazing tool <a href="https://github.com/wix/DetoxInstruments">DetoxInstruments</a>, developed by <a href="https://medium.com/u/b717150cccfc">Leo Natan</a>. If you didn’t use this amazing tool before, check this free gift as soon as you finish reading this.</p><p>And to make this a little bit more challenging, we decided that we don’t want to do any changes in the react-native code, because changing the react-natvie side makes it harder to integrate and maintain the code.<br>So we decided that “hack it from outside”!</p><p><em>By “Events” I mean the main functions calls; initialisations, setups …</em></p><p>So, where do I start ?! it’s huge pile of code!. After a quick search I understood that the react-native framework is poorly documented, no questions raised so no answers were given.</p><p>My luck didn’t run out yet, I was blessed with my colleague <a href="https://medium.com/u/1df70630cd63">Guillaume Laudet</a> Work, He started working on this, but didn’t manage to complete his work, it was a great start though.</p><blockquote>Enough talking !, lets get to the interesting stuff…</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*rI2wXrtiLqfL7nd3DLod5w.png" /></figure><p>Fortunately we found out that facebook had done most of the hard work; They’ve made a system for tracking and profiling the react-native framework. You can find the file that contains all the code for the profiling <a href="https://github.com/facebook/react-native/tree/master/React/Profiler">here</a>, and if you look closely at the <a href="https://github.com/facebook/react-native/blob/master/React/Profiler/RCTProfile.m">RCTProfile.m</a> file you’ll find a flag by the name <a href="https://github.com/facebook/react-native/blob/3c2300f4fe1106a1c55baffb548eed2ca99e99ba/React/Profiler/RCTProfile.m#L44">RCTProfileProfiling</a>, and turning it on will actually crash your app and you’ll get an error about a function that you’re trying to call that doesn’t exist.</p><p>Ok that’s progress, I’ll just implement these functions using JSCore.<br>But wait! to implement a JS function from the native side, I need JSContext, how should I get this context?.<br>There are a couple of ways to hack this problem and every solution has it’s own assumptions. I chose to attack this by “dynamically rebinding symbols” and Ironically facebook has made a very little library (two files) called <a href="https://github.com/facebook/fishhook">fishhook</a>, written in c. It does something similar to the objective-c’s swizzling, but to C functions in IOS environment :D.</p><p>I’ll assume that the App has a single JSVirtualMachine (all the JSContexts in the App can talk to each other and pass objects). In rare situations apps might need more than this.</p><p>Because in react-native the JS side is is common between android and IOS, and due to the way react-native works, it has to call the C function <a href="http://js object make typedarray example">JSObjectMake</a> (if you read the documentation you’ll understand why), and if you look at the function’s definition you’ll notice that one of its inputs is actually a JSContextRef.<br>And if we do this early enough (before react initialisations; bridge, rootView) we’ll nail this. We’ll just hold a reference to this context and use it to implement these JS functions, OOOOH YEAAAAH !!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*Wz-8WBkaUcq1F21m44J1wQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92c1eb2d014f087c703761ee598b76ee/href">https://medium.com/media/92c1eb2d014f087c703761ee598b76ee/href</a></iframe><p>Firstly this is the application’s main.m file (if you start your project through react-native cli init command, and actually we can perform this code before the bridge and the “RCTRootView”, so I chose to put it in main.m.</p><p>If you look on “hackJSObjectMakeFunction”, the first check is to make sure that react-native installed in the project, Basically I check if the RCTBridge class exists.<br>Then I use <a href="https://github.com/facebook/fishhook">fishhook</a> to replace the “<a href="http://js object make typedarray example">JSObjectMake</a>” function of the JavaScriptCore, with my “__hacked_JSOBjectMake” function which does nothing but getting the global “JSContext” from “JSContextRef” and implementing the missing javascript functions, and then it calls the original implementation (because otherwise nothing will work). <br>The Last section of this function is swizzling the bridge setup function (objective-c swizzle), as before, we save the original implementation and replace it with ours.</p><p>lets look at this “__hacked_RCTBridge_setUp” function; what it actually does, it searches for “RCTProfileInit” function and call it. <br>after a lot of searching I found out that this is the only function that turns on the profiling flag. No code was calling it, so it was part of the react-native profiling preparations we talked about.</p><blockquote>Now run your application …</blockquote><p>Now before everything, the app didn’t actually crash, and you’ll start seeing that these functions get called.</p><p>But it’s not the only place where “Events” get captured, actually I noticed that the JS side calls contain mainly the uses of require functions (which is what is happening on the JS side), but the main purpose of this was to track what’s happening in the native side of the react-native.</p><p>So, we’re moving on to the fun side; the native side!</p><p>After reading the <a href="https://github.com/facebook/react-native/blob/master/React/Profiler/RCTProfile.m">RCTProfile.m</a> file, I noticed that there are 7 functions that get called to register all kind of events :</p><ul><li>_RCTProfileBeginEvent, _RCTProfileEndEvent: for starting and ending a profile within the same call stack frame.</li><li>RCTProfileBeginAsyncEvent, RCTProfileEndAsyncEvent: for starting and ending a profile where the end can either occur on another thread or out of the current stack frame, eg await the returned cookie variable should be used as input into the endAsyncEvent call to end the profile.</li><li>RCTProfileImmediateEvent: for immediate events.</li><li>_RCTProfileBeginFlowEvent, _RCTProfileEndFlowEvent: beginning and ending flow events.</li></ul><p>If you look into the function’s implementation you’ll notice 3 things; a macro “CHECK” which basically checks if the profiling flag is on, and check if a variable called “callbacks” of struct “RCTProfileCallbacks” isn’t NULL (we’ll get to that) and then calls a function.</p><p>let’s look at this “RCTProfileCallbacks” struct.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16c5185a75076dcbd5b8f10fd36fcf91/href">https://medium.com/media/16c5185a75076dcbd5b8f10fd36fcf91/href</a></iframe><p>The “RCTProfileCallbacks” is just a bunch of function pointers that get called from the events functions (shown above). The “systrace_arg_t” is just a handy parameter for profiling (after running the whole thing together I noticed that it’s always NULL).</p><p>So in order to complete this, we have to do two things: <br>- initialise the RCTProfiler (done). <br>- implement the callbacks.</p><p>Implementing these callbacks is as easy as you think</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab806884469e0b54a37dd66ba960a819/href">https://medium.com/media/ab806884469e0b54a37dd66ba960a819/href</a></iframe><p>We’ll declare an instance of the struct “RCTProfileCallbacks” , with our implemented callbacks.</p><p>and finally, we must “inject” this instance to the “RCTProfile”, And for this, the react-native guys provided us with the necessary function, “RCTProfileRegisterCallbacks”.</p><p>in order to call this function we’ll use <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html">dlsym function</a> :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24488b9e4939e2490d1d8a3ff6e300dd/href">https://medium.com/media/24488b9e4939e2490d1d8a3ff6e300dd/href</a></iframe><p>It’s nothing but getting the “RCTProfileRegisterCallbacks” function and calling it.</p><p>And finally we’ll call this at the start of the “didFinishLaunchingWithOptions”.to make sure that the recording starts before the app launch.</p><p>Finally, I would like to say that I had so much fun doing this. The most fun was actually discovering something that’s not documented and not meant to be discovered.</p><p>as a reference I uploaded the whole <a href="https://github.com/aboelbisher/HackRNProfiler">example project</a>.</p><p>If you have any questions please feel free to leave a comment and I’ll answer you as soon as possible.</p><p>Thank you for reading this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=762e800b49ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>